-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafeUtil.c
More file actions
executable file
·81 lines (69 loc) · 1.6 KB
/
safeUtil.c
File metadata and controls
executable file
·81 lines (69 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Writen by Hugh Smith, April 2020
//
// Put in system calls with error checking
// and and an s to the name: srealloc()
// keep the function paramaters same as system call
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include "networks.h"
#include "safeUtil.h"
int safeRecv(int socketNum, uint8_t * buffer, int bufferLen, int flag)
{
int bytesReceived = recv(socketNum, buffer, bufferLen, flag);
if (bytesReceived < 0)
{
if (errno == ECONNRESET)
{
bytesReceived = 0;
}
else
{
perror("recv call");
exit(-1);
}
}
return bytesReceived ;
}
int safeSend(int socketNum, uint8_t * buffer, int bufferLen, int flag)
{
int bytesSent = 0;
if ((bytesSent = send(socketNum, buffer, bufferLen, flag)) < 0)
{
perror("recv call");
exit(-1);
}
return bytesSent;
}
void * srealloc(void *ptr, size_t size)
{
void * returnValue = NULL;
if ((returnValue = realloc(ptr, size)) == NULL)
{
printf("Error on realloc (tried for size: %d\n", (int) size);
exit(-1);
}
return returnValue;
}
void * sCalloc(size_t nmemb, size_t size)
{
void * returnValue = NULL;
if ((returnValue = calloc(nmemb, size)) == NULL)
{
perror("calloc");
exit(-1);
}
return returnValue;
}