summaryrefslogtreecommitdiffstats
path: root/contrib/ipfilter/lib/load_file.c
blob: a1d1f70b5c330d2fb7a5c576cab37b5c69701758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright (C) 2012 by Darren Reed.
 *
 * See the IPFILTER.LICENCE file for details on licencing.
 *
 * $Id: load_file.c,v 1.6.2.2 2012/07/22 08:04:24 darren_r Exp $
 */

#include "ipf.h"
#include <ctype.h>

alist_t *
load_file(char *filename)
{
	alist_t *a, *rtop, *rbot;
	char *s, line[1024], *t;
	int linenum, not;
	FILE *fp;

	fp = fopen(filename + 7, "r");
	if (fp == NULL) {
		fprintf(stderr, "load_file cannot open '%s'\n", filename);
		return NULL;
	}

	a = NULL;
	rtop = NULL;
	rbot = NULL;
	linenum = 0;

	while (fgets(line, sizeof(line) - 1, fp)) {
		line[sizeof(line) - 1] = '\0';
		linenum++;
		/*
		 * Hunt for CR/LF.  If no LF, stop processing.
		 */
		s = strchr(line, '\n');
		if (s == NULL) {
			fprintf(stderr, "%d:%s: line too long\n",
				linenum, filename);
			fclose(fp);
			alist_free(rtop);
			return NULL;
		}

		/*
		 * Remove trailing spaces
		 */
		for (; ISSPACE(*s); s--)
			*s = '\0';

		s = strchr(line, '\r');
		if (s != NULL)
			*s = '\0';
		for (t = line; ISSPACE(*t); t++)
			;
		if (*t == '!') {
			not = 1;
			t++;
		} else
			not = 0;

		/*
		 * Remove comment markers
		 */
		s = strchr(t, '#');
		if (s != NULL) {
			*s = '\0';
			if (s == t)
				continue;
		}

		/*
		 * Trim off tailing white spaces
		 */
		s = strlen(t) + t - 1;
		while (ISSPACE(*s))
			*s-- = '\0';

		a = alist_new(AF_UNSPEC, t);
		if (a != NULL) {
			a->al_not = not;
			if (rbot != NULL)
				rbot->al_next = a;
			else
				rtop = a;
			rbot = a;
		} else {
			fprintf(stderr, "%s:%d unrecognised content :%s\n",
				filename, linenum, t);
		}
	}
	fclose(fp);

	return rtop;
}
OpenPOWER on IntegriCloud