forked from johanvon/mariotcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
68 lines (64 loc) · 1.87 KB
/
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
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
CONF *conf = NULL;
sint32 init_conf() {
uint32 size = sizeof(CONF);
conf = (CONF *) malloc(size);
if (!conf) {
fprintf(stderr, "[error]init_conf, malloc conf failed.\n");
return -1;
}
memset(conf, 0, size);
FILE * fp;
char line[1024];
char name[128];
char value[128];
fp = fopen("init.conf", "r");
if (fp == NULL) {
fprintf(stderr, "[error] init_conf, fopen failed, errno: %d %m\n", errno);
return -1;
}
memset(line, 0, sizeof(line));
while (fgets(line, 1024, fp)) {
if (line[0] == '#' || strcmp(line, "") == 0) {
continue;
}
memset(name, 0, sizeof(name));
memset(value, 0, sizeof(value));
sscanf(line, "%s %s", name, value);
if (strcmp(name, "PORT") == 0) {
conf->port = atoi(value);
} else if (strcmp(name, "TIMEOUT") == 0) {
conf->timeout = atoi(value);
} else if (strcmp(name, "WORKERNUM") == 0) {
conf->workernum = atoi(value);
} else if (strcmp(name, "CONNNUM") == 0) {
conf->connnum = atoi(value);
} else if (strcmp(name, "USERNUM") == 0) {
conf->usernum = atoi(value);
} else if (strcmp(name, "FILENUM") == 0) {
conf->filenum = atoi(value);
}
memset(line, 0, sizeof(line));
}
//check conf
if (0 == conf->port) {
fprintf(stderr, "[error] init_conf, port is 0\n");
fclose(fp);
return -1;
}
fprintf(stdout, "\n>>>>>>>>>>>>>>>init_conf>>>>>>>>>>>>>>>\n");
fprintf(stdout, "%-24s%u\n", "port", conf->port);
fprintf(stdout, "%-24s%u\n", "timeout", conf->timeout);
fprintf(stdout, "%-24s%u\n", "workernum", conf->workernum);
fprintf(stdout, "%-24s%u\n", "connnum", conf->connnum);
fprintf(stdout, "%-24s%u\n", "usernum", conf->usernum);
fprintf(stdout, "%-24s%u\n", "filenum", conf->filenum);
fprintf(stdout, "\n");
fflush(stdout);
fclose(fp);
return 0;
}