blob: 5071f776862ff20cca7de719021e677bee7ffe02 (
plain)
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
|
#include <os/os.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
char *
os_file_read_as_string(Arena *arena, char *filepath, size_t *len)
{
int fd = open(filepath, O_RDONLY);
if (fd == -1) {
printf("can't open file %s for reading\n", filepath);
return 0;
}
struct stat statbuf;
if (fstat(fd, &statbuf) == -1) {
printf("can't fstat file %s\n", filepath);
return 0;
}
char *dest = arena_push(arena, statbuf.st_size+1);
ssize_t bytes_read = read(fd, dest, statbuf.st_size);
if (bytes_read != statbuf.st_size) {
printf("only read %ld/%ld bytes\n", statbuf.st_size, bytes_read);
arena->size_used -= statbuf.st_size+1; // Todo: cleanup
return 0;
}
dest[bytes_read] = '\0';
*len = statbuf.st_size;
return dest;
}
|