-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf2c.c
66 lines (57 loc) · 1.67 KB
/
conf2c.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int conf2c(const char *confpath);
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("conf2c: No config filepath given. Checking the current dir for it..\n");
if (access(".config", F_OK) == 0) {
int ret = conf2c(".config");
return ret;
} else {
printf("conf2c: error: No '.config' given\n");
return -1;
}
}
const char *confpath = argv[1];
conf2c(confpath);
}
int conf2c(const char *confpath) {
FILE *conf_p, *chdr_p;
char line[256];
conf_p = fopen(confpath, "r");
if (!conf_p) {
printf("conf2c: error: '%s' doesn't exist\n", conf_p);
return -1;
}
chdr_p = fopen("config.h", "w");
fprintf(chdr_p, "#ifndef _CONFIG_H\n#define _CONFIG_H\n\n");
while (fgets(line, sizeof(line), conf_p)) {
/* Ignore lines starting with '#' */
if (line[0] == '#') {
continue;
}
/* Check for '='. Note: config value should be less than 50. */
for (int i = 0; i < 50; i++) {
if (line[i] == '=') {
/* Save the config option */
char *confopt = line[i+1];
/* Remove the equals sign, the config option and
* the newline character from 'line'.
*/
line[i] = ' ';
line[i+1] = ' ';
line[i+2] = ' ';
/* Now write the config value and the option */
fprintf(chdr_p, "#define %s ", line);
fprintf(chdr_p, "'%s'\n", &confopt);
} else {
continue;
}
}
}
fprintf(chdr_p, "\n#endif //_CONFIG_H");
fclose(chdr_p);
fclose(conf_p);
return 0;
}