-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathloadconfig.c
126 lines (119 loc) · 2.77 KB
/
loadconfig.c
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
/*
* Ismail Yenigul's famous config reader/parser
* This code derived from EnderUNIX isoqlog project.
*
* http://www.enderunix.org/isoqlog
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include "defs.h"
#include "loadconfig.h"
void loadconfig(char *cfgfile)
{
FILE *fd = NULL;
char buf[BIGBUFSIZE];
char keyword[TINYBUFSIZE];
char value[BIGBUFSIZE];
int lenbuf = 0;
char *cp1 = NULL, *cp2 = NULL;
char *variables[] = { "Invalid",
"query_name_server1",
"query_name_server2",
"name_server1",
"name_server2",
"name_server1_extra",
"name_server2_extra",
"dns_srv_sfw",
"dns_conf_file",
"html_output_dir",
"lang_file"
};
int i, j, key, line, keyword_nums = sizeof(variables)/sizeof(char *);
if ((fd = fopen(cfgfile, "r")) == NULL) {
fprintf(stderr, "loadconfig: cannot open checkdns configuration file %s, exiting...\n", cfgfile);
exit(-1);
}
line = 0;
while ((fgets(buf, BIGBUFSIZE - 1, fd)) != NULL) {
line++;
if (buf[0] == '#')
continue;
if ((lenbuf = strlen(buf)) <= 1)
continue;
cp1 = buf;
cp2 = keyword;
j = 0;
while (isspace((int)*cp1) && ((cp1 - buf) < lenbuf))
cp1++;
while(isgraph((int)*cp1) && *cp1 != '=' && (j++ < TINYBUFSIZE - 1) && (cp1 - buf) < lenbuf)
*cp2++ = *cp1++;
*cp2 = '\0';
cp2 = value;
while ((*cp1 != '\0') && (*cp1 !='\n') && (*cp1 !='=') && ((cp1 - buf) < lenbuf))
cp1++;
cp1++;
while (isspace((int)*cp1) && ((cp1 - buf) < lenbuf))
cp1++;
if (*cp1 == '"')
cp1++;
j = 0;
while ((*cp1 != '\0') && (*cp1 !='\n') && (*cp1 !='"') && (j++ < TINYBUFSIZE - 1) && ((cp1 - buf) < lenbuf))
*cp2++ = *cp1++;
*cp2-- = '\0';
if (keyword[0] =='\0' || value[0] =='\0')
continue;
key = 0;
for (i = 0; i < keyword_nums; i++) {
if ((strncmp(keyword, variables[i], TINYBUFSIZE)) == 0) {
key = i;
break;
}
}
switch(key) {
case 0:
fprintf(stderr, "illegal keyword in config file: %s\n", keyword);
break;
case 1: {
strncpy(query_name_server1, value, TINYBUFSIZE-1);
break;
}
case 2: {
strncpy(query_name_server2, value, TINYBUFSIZE-1);
break;
}
case 3:
strncpy(name_server1, value, TINYBUFSIZE-1);
break;
case 4:
strncpy(name_server2, value, TINYBUFSIZE-1);
break;
case 5:
strncpy(name_server1_extra, value, TINYBUFSIZE-1);
break;
case 6:
strncpy(name_server2_extra, value, TINYBUFSIZE-1);
break;
case 7:
strncpy(dns_srv_sfw, value, TINYBUFSIZE-1);
break;
case 8:
strncpy(dns_conf_file, value, TINYBUFSIZE-1);
break;
case 9:
strncpy(html_output_dir, value, TINYBUFSIZE-1);
break;
case 10:
strncpy(lang_file, value, TINYBUFSIZE-1);
break;
}
}
fclose(fd);
}