forked from Skycrab/Linux-C-Web-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_config.c
98 lines (80 loc) · 1.41 KB
/
parse_config.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
#include "parse.h"
#include "wrap.h"
extern char *cwd;
FILE *configfp=NULL;
static FILE *getfp(char *path)
{
configfp=Fopen(path,"r");
return configfp;
}
static char* getconfig(char* name)
{
/*
pointer meaning:
...port...=...8000...
| | | | |
*fs | | | *be f->forward b-> back
*fe | *bs s->start e-> end
*equal
*/
static char info[64];
int find=0;
char tmp[256],fore[64],back[64],tmpcwd[MAXLINE];
char *fs,*fe,*equal,*bs,*be,*start;
strcpy(tmpcwd,cwd);
strcat(tmpcwd,"/");
FILE *fp=getfp(strcat(tmpcwd,"config.ini"));
while(fgets(tmp,255,fp)!=NULL)
{
start=tmp;
equal=strchr(tmp,'=');
while(isblank(*start))
++start;
fs=start;
if(*fs=='#')
continue;
while(isalpha(*start))
++start;
fe=start-1;
strncpy(fore,fs,fe-fs+1);
fore[fe-fs+1]='\0';
if(strcmp(fore,name)!=0)
continue;
find=1;
start=equal+1;
while(isblank(*start))
++start;
bs=start;
while(!isblank(*start)&&*start!='\n')
++start;
be=start-1;
strncpy(back,bs,be-bs+1);
back[be-bs+1]='\0';
strcpy(info,back);
break;
}
if(find)
return info;
else
return NULL;
}
char* Getconfig(char* name)
{
char *p=getconfig(name);
if(p==NULL)
{
syslog(LOG_ERR,"there is no %s in the configure",name);
exit(0);
}
else
return p;
}
/* parse_config test
int main()
{
char *s=getconfig("port");
printf("%s\n",s);
s=getconfig("root");
printf("%s\n",s);
}
*/