file.c (602B)
1 #include <stdbool.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <sys/stat.h> 6 #include <unistd.h> 7 8 size_t 9 fsize(const char *name) { 10 struct stat st; 11 12 stat(name, &st); 13 14 return st.st_size; 15 } 16 17 bool 18 valid_path(const char *name) { 19 return access(name, F_OK) == 0; 20 } 21 22 bool 23 is_directory(const char *name) { 24 struct stat st; 25 26 stat(name, &st); 27 28 return S_ISDIR(st.st_mode); 29 } 30 31 char * 32 read_file(const char *name) { 33 char *buf; 34 size_t length; 35 36 FILE *f = fopen(name, "r"); 37 length = fsize(name); 38 39 buf = calloc(length + 1, 1); 40 41 fread(buf, 1, length, f); 42 43 fclose(f); 44 45 return buf; 46 }