File: settings.c

package info (click to toggle)
renattach 1.1.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 144 kB
  • ctags: 45
  • sloc: ansic: 694; sh: 209; makefile: 62
file content (197 lines) | stat: -rw-r--r-- 3,814 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*

settings.c
Deals with configuration file (CONF_FILE)

Copyright (C) 2001, Jem E. Berkes

*/

#include "defs.h"
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


int mem_error()
/* Error when buffer space can not be allocated */
{
fprintf(stderr, "Fatal error: unable to allocate required memory!\n");
fprintf(stderr, "This most likely indicates that the input stream (e-mail)\n");
fprintf(stderr, "has exceedingly long lines\n");
return -1;
}


int tokenize_list(char* thelist, int verbose)
/*
Goes through the list, (optionally displaying each entry) and returns
count of actual entries in the list. Destroys the list.
*/
{
char *token;
int count=0;

token = strtok(thelist, CONF_TOKS);
while (token != NULL)
	{
	count++;
	if (verbose == 1)
		printf("[%s] ", token);
	token = strtok(NULL, CONF_TOKS);
	}

return count;
}


/*
Reads good list and bad list from configuration file
Returns 0 if OK or -1 if failed
*/
int read_lists(	char** good_list, char** bad_list,
				char** gl_scratch, char** bl_scratch)
{
int line_len, line_num=0, good_len=DEF_BUFSIZE, bad_len=DEF_BUFSIZE;
FILE* cf;
char buffer[DEF_BUFSIZE];
char *temp_bigger;

*good_list = (char*)malloc(DEF_BUFSIZE);
if (*good_list == NULL)
	return mem_error();

*bad_list = (char*)malloc(DEF_BUFSIZE);
if (*bad_list == NULL)
	{
	free(*good_list);
	return mem_error();
	}

if ( (access(CONF_FILE, R_OK) == -1) ||
		((cf=fopen(CONF_FILE, "r")) == NULL) )
	{
	fprintf(stderr, "Error: unable to open configuration file %s\n", CONF_FILE);
	free(*good_list);
	free(*bad_list);
	return -1;
	}

**good_list = 0;
**bad_list = 0;

/*
All lines in cf must begin with "#" (comment), "good:", or "bad:"
*/

while (fgets(buffer, DEF_BUFSIZE, cf) != NULL)
	{
	++line_num;
	line_len = strlen(buffer);
	if (line_len == DEF_BUFSIZE-1)
		{
		fclose(cf);
		free(*good_list);
		free(*bad_list);
		fprintf(stderr, "Error: oversized line in %s line %d\n", CONF_FILE, line_num);
		return -1;
		}

	if (*buffer=='\n' || *buffer=='#')
		continue;
	else if (strncmp(buffer, "good:", 5) == 0)
		{
		while ( (strlen(*good_list)+line_len) >= (unsigned int)good_len)
			{
			if (good_len >= BUF_LIMIT)
				temp_bigger = NULL;
			else
				{
				good_len *= 2;
				temp_bigger = (char*)malloc(good_len);
				}
			if (temp_bigger == NULL)
				{
				free(*good_list);
				free(*bad_list);
				fclose(cf);
				return mem_error();
				}
			strcpy(temp_bigger, *good_list);
			free(*good_list);
			*good_list = temp_bigger;
			}
		strcat(*good_list, buffer+5);
		}
	
	else if (strncmp(buffer, "bad:", 4) == 0)
		{
		while ( (strlen(*bad_list)+line_len) >= (unsigned int)bad_len)
			{
			if (bad_len >= BUF_LIMIT)
				temp_bigger = NULL;
			else
				{
				bad_len *= 2;
				temp_bigger = (char*)malloc(bad_len);
				}
			if (temp_bigger == NULL)
				{
				free(*good_list);
				free(*bad_list);
				fclose(cf);
				return mem_error();
				}
			strcpy(temp_bigger, *bad_list);
			free(*bad_list);
			*bad_list = temp_bigger;
			}
		strcat(*bad_list, buffer+4);
		}

	else
		{
		fclose(cf);
		free(*good_list);
		free(*bad_list);
		fprintf(stderr, "Error: syntax error in %s line %d\n", CONF_FILE, line_num);
		return -1;
		}
	}

fclose(cf);

*gl_scratch = (char*)malloc(good_len);
if (*gl_scratch == NULL)
	{
	free(*good_list);
	free(*bad_list);
	return mem_error();
	}

*bl_scratch = (char*)malloc(bad_len);
if (bl_scratch == NULL)
	{
	free(*good_list);
	free(*bad_list);
	free(*gl_scratch);
	return mem_error();
	}

strcpy(*gl_scratch, *good_list);
strcpy(*bl_scratch, *bad_list);

if (	(tokenize_list(*gl_scratch, 0)==0) ||
		(tokenize_list(*bl_scratch, 0)==0) )
	{
	free(*good_list);
	free(*bad_list);
	free(*gl_scratch);
	free(*bl_scratch);
	fprintf(stderr, "Error: either goodlist or badlist is empty\n");
	return -1;
	}
return 0;
}