utils.c (763B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 char * 6 read_file(const char *filename) 7 { 8 long fsize; 9 char *source; 10 FILE *fp = fopen(filename, "r"); 11 if (fp == NULL) { 12 fprintf(stderr, "file not found\n"); 13 return NULL; 14 } 15 16 fseek(fp, 0, SEEK_END); 17 fsize = ftell(fp); 18 fseek(fp, 0, SEEK_SET); 19 20 source = malloc(fsize + 1); 21 fread(source, fsize, 1, fp); 22 fclose(fp); 23 24 source[fsize] = 0; 25 return source; 26 } 27 28 void 29 separate_file_from_path(const char *fullpath, char **out_path, char **out_filename) 30 { 31 char *path = strdup(fullpath); 32 char *filename = strrchr(path, '/'); 33 34 if (filename == NULL) { 35 printf("No path found\n"); 36 exit(1); 37 } 38 39 *filename = '\0'; 40 filename++; 41 *out_path = strdup(path); 42 *out_filename = strdup(filename); 43 free(path); 44 }