-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_syscalls.c
48 lines (44 loc) · 1.36 KB
/
check_syscalls.c
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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <inttypes.h>
FILE *check_fopen(char *filename, char *mode) {
FILE *res = fopen(filename, mode);
if (res == NULL) {
if (mode[0] == 'w')
fprintf(stderr, "Failed to open file %s for writing!\n", filename);
else if (mode[0] == 'a')
fprintf(stderr, "Failed to open file %s for appending!\n", filename);
else
fprintf(stderr, "Failed to open file %s for reading!\n", filename);
exit(1);
}
return res;
}
void *check_realloc(void *ptr, size_t size, char *reason) {
void *res = realloc(ptr, size);
if ((res == NULL) && (size > 0)) {
fprintf(stderr, "[Error] Failed to allocate %zu bytes of memory (%s)!\n", size, reason);
assert(0);
}
return res;
}
/* #ifdef CHECK_FILE_IO */
/* size_t my_fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream) */
/* { */
/* size_t nwritten; */
/* nwritten = fwrite(ptr, size, nmemb, stream); */
/* if(nwritten != nmemb) */
/* { */
/* fprintf(stderr,"I/O error (fwrite) has occured.\n"); */
/* fprintf(stderr,"Instead of writing nmemb=%zu, I got nwritten = %zu ..exiting\n",nmemb,nwritten); */
/* exit(EXIT_FAILURE); */
/* } */
/* return nwritten; */
/* } */
/* #else */
/* size_t my_fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream) */
/* { */
/* return fwrite(ptr, size, nmemb, stream); */
/* } */
/* #endif */