-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_unix.c
46 lines (39 loc) · 839 Bytes
/
os_unix.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
/* see LICENSE for licensing details */
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
typedef struct timespec os_filetime;
typedef struct {
size filesize;
os_filetime timestamp;
} os_file_stats;
static os_file_stats
os_get_file_stats(char *file)
{
struct stat sb;
if (stat(file, &sb) < 0)
return (os_file_stats){0};
return (os_file_stats){
.filesize = sb.st_size,
.timestamp = sb.st_mtim,
};
}
static s8
os_read_file(Arena *a, char *file, size filesize)
{
s8 ret = {0};
i32 fd = open(file, O_RDONLY);
if (fd < 0)
return ret;
ret.data = alloc(a, u8, filesize);
ret.len = filesize;
if (ret.len != read(fd, ret.data, ret.len))
ret.len = 0;
close(fd);
return ret;
}
static i32
os_compare_filetime(os_filetime a, os_filetime b)
{
return (a.tv_sec - b.tv_sec) + (a.tv_nsec - b.tv_nsec);
}