1/* 2 * Copyright (C) 2006 by Darren Reed. 3 * 4 * See the IPFILTER.LICENCE file for details on licencing. 5 * 6 * $Id: load_file.c,v 1.1.2.1 2006/08/25 21:13:04 darrenr Exp $ 7 */ 8 9#include "ipf.h" 10 11alist_t * 12load_file(char *filename) 13{ 14 alist_t *a, *rtop, *rbot; 15 char *s, line[1024], *t; 16 int linenum, not; 17 FILE *fp; 18 19 fp = fopen(filename + 7, "r"); 20 if (fp == NULL) { 21 fprintf(stderr, "load_file cannot open '%s'\n", filename); 22 return NULL; 23 } 24 25 a = NULL; 26 rtop = NULL; 27 rbot = NULL; 28 linenum = 0; 29 30 while (fgets(line, sizeof(line) - 1, fp)) { 31 line[sizeof(line) - 1] = '\0'; 32 linenum++; 33 /* 34 * Hunt for CR/LF. If no LF, stop processing. 35 */ 36 s = strchr(line, '\n'); 37 if (s == NULL) { 38 fprintf(stderr, "%d:%s: line too long\n", linenum, filename); 39 fclose(fp); 40 alist_free(rtop); 41 return NULL; 42 } 43 44 *s = '\0'; 45 s = strchr(line, '\r'); 46 if (s != NULL) 47 *s = '\0'; 48 for (t = line; isspace(*t); t++) 49 ; 50 if (*t == '!') { 51 not = 1; 52 t++; 53 } else 54 not = 0; 55 56 /* 57 * Remove comment markers 58 */ 59 for (s = t; *s; s++) { 60 if (*s == '#') 61 *s = '\0'; 62 } 63 if (!*t) 64 continue; 65 /* 66 * Trim off tailing white spaces 67 */ 68 s = strlen(t) + t - 1; 69 while (isspace(*s)) 70 *s-- = '\0'; 71 72 if (isdigit(*t)) { 73 a = alist_new(4, t); 74 a->al_not = not; 75 if (rbot != NULL) 76 rbot->al_next = a; 77 else 78 rtop = a; 79 rbot = a; 80 } else { 81 fprintf(stderr, "%s: unrecognised content line %d\n", 82 filename, linenum); 83 } 84 } 85 fclose(fp); 86 87 return rtop; 88} 89