-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomlib.c
49 lines (38 loc) · 941 Bytes
/
customlib.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
49
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "customlib.h"
void* malloc_c(const size_t size) {
void* buffer = malloc(size);
if (buffer == NULL) {
fprintf(stderr, "malloc failed: %s", strerror(errno));
exit(errno);
}
return buffer;
}
void free_c(void* block) {
free(block);
}
FILE* fopen_c(const char* file_name, const char* mode) {
FILE* file = fopen(file_name, mode);
if (file == NULL) {
fprintf(stderr, "fopen failed: %s", strerror(errno));
exit(errno);
}
return file;
}
size_t fread_c(void* buffer, size_t element_size, size_t element_count, FILE* stream) {
size_t size = fread(buffer, element_size, element_count, stream);
if (ferror(stream)) {
fprintf(stderr, "fread failed: %s", strerror(errno));
exit(errno);
}
return size;
}
void fclose_c(FILE* stream) {
if (fclose(stream)) {
fprintf(stderr, "fclose failed: %s", strerror(errno));
exit(errno);
}
}