author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 03 Jan 2009 21:41:51 +0100 | |
changeset 33 | 56571d34d754 |
parent 18 | 4435146391ae |
child 36 | 6889aacd038e |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* $Id: config.c 51 2008-01-10 00:19:39Z mbroeker $ |
|
3 |
* $URL: http://localhost/svn/c/mcbot/trunk/src/config.c $ |
|
4 |
* |
|
5 |
*/ |
|
6 |
||
7 |
#include <stdio.h> |
|
8 |
#include <stdlib.h> |
|
9 |
#include <string.h> |
|
10 |
||
13
d3554afaa768
mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
11
diff
changeset
|
11 |
#include <compat.h> |
0 | 12 |
#include <config.h> |
33
56571d34d754
safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
18
diff
changeset
|
13 |
#include "common.h" |
0 | 14 |
|
15 |
const |
|
16 |
char *CONFIG_OPTIONS[] = { |
|
17 |
"NICK", "PASSWORD", "SERVER", "PORT", |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
18 |
"CHANNEL", "TOPIC", NULL |
0 | 19 |
}; |
20 |
||
21 |
int config (UC * uc, char *fname) |
|
22 |
{ |
|
23 |
FILE *f; |
|
33
56571d34d754
safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
18
diff
changeset
|
24 |
char buffer[DEFAULT_BUF_SIZE]; |
10
311ea5fa60dd
Code cleanups and debian changelogs
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
25 |
char **line; |
0 | 26 |
char *token; |
27 |
char *value; |
|
28 |
int i = 0; |
|
29 |
||
30 |
if ((f = fopen (fname, "r")) == NULL) |
|
31 |
return -1; |
|
32 |
||
33 |
while (CONFIG_OPTIONS[i] != NULL) |
|
34 |
i++; |
|
35 |
||
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
36 |
line = calloc ((size_t) (i + 1), sizeof (char *)); |
0 | 37 |
|
38 |
/* |
|
39 |
* We can easily provide default values ... |
|
40 |
*/ |
|
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
41 |
uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL; |
0 | 42 |
uc->port = 6667; |
43 |
||
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
44 |
while (!feof (f)) { |
33
56571d34d754
safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
18
diff
changeset
|
45 |
(void)fgets (buffer, sizeof (buffer), f); |
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
46 |
token = buffer; |
0 | 47 |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
48 |
while (*token == '\t') /* Eat trailing tabs */ |
0 | 49 |
token++; |
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
50 |
while (*token == ' ') /* Eat trailing whitespaces */ |
0 | 51 |
token++; |
52 |
||
53 |
token = strtok (token, ":"); |
|
54 |
||
55 |
if (token != NULL) { |
|
56 |
value = strtok (NULL, "\n"); |
|
57 |
i = 0; |
|
58 |
while (CONFIG_OPTIONS[i] != NULL) { |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
59 |
if (!strcmp (CONFIG_OPTIONS[i], token)) { |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
60 |
if (value) |
13
d3554afaa768
mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
11
diff
changeset
|
61 |
line[i] = compat_strdup (value); |
0 | 62 |
} |
63 |
i++; |
|
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
if (fclose (f) != 0) |
|
69 |
return -1; |
|
70 |
||
71 |
i = 0; |
|
72 |
while (CONFIG_OPTIONS[i] != NULL) { |
|
73 |
if (line[i] != NULL) |
|
74 |
switch (i) { |
|
75 |
case 0: /* NICK */ |
|
13
d3554afaa768
mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
11
diff
changeset
|
76 |
uc->nick = compat_strdup (line[i]); |
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
77 |
free (line[i]); |
0 | 78 |
break; |
79 |
case 1: /* PASSWORD */ |
|
13
d3554afaa768
mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
11
diff
changeset
|
80 |
uc->pass = compat_strdup (line[i]); |
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
81 |
free (line[i]); |
0 | 82 |
break; |
83 |
case 2: /* SERVER */ |
|
13
d3554afaa768
mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
11
diff
changeset
|
84 |
uc->server = compat_strdup (line[i]); |
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
85 |
free (line[i]); |
0 | 86 |
break; |
87 |
case 3: /* PORT */ |
|
88 |
uc->port = atoi (line[i]); |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
89 |
free (line[i]); |
0 | 90 |
break; |
91 |
case 4: /* CHANNEL */ |
|
13
d3554afaa768
mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
11
diff
changeset
|
92 |
uc->channel = compat_strdup (line[i]); |
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
93 |
free (line[i]); |
0 | 94 |
break; |
95 |
case 5: /* TOPIC */ |
|
13
d3554afaa768
mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
11
diff
changeset
|
96 |
uc->topic = compat_strdup (line[i]); |
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
97 |
free (line[i]); |
0 | 98 |
break; |
99 |
} |
|
100 |
i++; |
|
101 |
} |
|
102 |
||
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
103 |
if (line != NULL) |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
104 |
free (line); |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
105 |
|
18
4435146391ae
LOGIN without Password added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
13
diff
changeset
|
106 |
if (!(uc->nick && uc->server && uc->channel)) |
0 | 107 |
return -2; |
108 |
||
109 |
return 0; |
|
110 |
} |