os_unix.c (839B)
1 /* see LICENSE for licensing details */ 2 #include <fcntl.h> 3 #include <sys/stat.h> 4 #include <unistd.h> 5 6 typedef struct timespec os_filetime; 7 8 typedef struct { 9 size filesize; 10 os_filetime timestamp; 11 } os_file_stats; 12 13 static os_file_stats 14 os_get_file_stats(char *file) 15 { 16 struct stat sb; 17 if (stat(file, &sb) < 0) 18 return (os_file_stats){0}; 19 return (os_file_stats){ 20 .filesize = sb.st_size, 21 .timestamp = sb.st_mtim, 22 }; 23 } 24 25 static s8 26 os_read_file(Arena *a, char *file, size filesize) 27 { 28 s8 ret = {0}; 29 i32 fd = open(file, O_RDONLY); 30 if (fd < 0) 31 return ret; 32 ret.data = alloc(a, u8, filesize); 33 ret.len = filesize; 34 35 if (ret.len != read(fd, ret.data, ret.len)) 36 ret.len = 0; 37 38 close(fd); 39 return ret; 40 } 41 42 static i32 43 os_compare_filetime(os_filetime a, os_filetime b) 44 { 45 return (a.tv_sec - b.tv_sec) + (a.tv_nsec - b.tv_nsec); 46 }