-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioutils.c
52 lines (42 loc) · 862 Bytes
/
ioutils.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
50
51
52
#include "ioutils.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
size_t
file_read(const char *filename, char **r_buf)
{
assert(filename != NULL);
assert(r_buf != NULL);
*r_buf = NULL;
size_t size = 0;
FILE *fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "unable to open file '%s'\n", filename);
goto error;
}
// read file size
fseek(fp, 0, SEEK_END);
size = ftell(fp);
rewind(fp);
// allocate a buffer for its contents
*r_buf = malloc(size + 1);
if (!*r_buf) {
fprintf(stderr, "could not allocate %lu bytes for file contents\n", size + 1);
goto error;
}
(*r_buf)[size] = 0; // NUL-terminator
// read the file
if (fread(*r_buf, 1, size, fp) != size) {
fprintf(stderr, "I/O error\n");
goto error;
}
cleanup:
if (fp) {
fclose(fp);
}
return size;
error:
size = 0;
free(*r_buf);
goto cleanup;
}