ed.c (896B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stdbool.h> 4 #include <string.h> 5 6 #include "types.h" 7 #include "util.h" 8 #include "parse.h" 9 10 dl_list * 11 convert_file(char *file) { 12 dl_list *current = NULL, *prev = NULL, *start; 13 char *line_end; 14 bool end = false; 15 16 while (!end) { 17 if ((line_end = strchr(file, '\n'))) 18 *line_end = 0; 19 else { 20 *(line_end = strchr(file, 0)) = 0; 21 end = true; 22 } 23 24 if (!current) { 25 current = zmalloc(sizeof(dl_list)); 26 start = current; 27 } 28 29 current->str = strdup(file); 30 current->next = zmalloc(sizeof(dl_list)); 31 current->prev = prev; 32 prev = current; 33 current = current->next; 34 35 file = line_end + 1; 36 } 37 38 return start; 39 } 40 41 int 42 main() { 43 FILE *f = fopen("ed.c", "r"); 44 char *raw_file; 45 dl_list *file; 46 47 if (!f) 48 return 1; 49 50 raw_file = drain_file(f); 51 52 file = convert_file(raw_file); 53 54 fclose(f); 55 free(raw_file); 56 free_dl_list(file); 57 return 0; 58 }