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