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
127
128
129
130
131
132
133
134
|
/*
arfnet2-search: Fast file indexer and search
Copyright (C) 2025 arf20 (Ángel Ruiz Fernandez)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
config.c: Configuration parser
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
unsigned short port = 0;
char *tmpl_path = NULL, *root = NULL, *app_subdir = NULL,
*result_subdir = NULL;
int magic_enable = 0, period = 86400;
int
config_load(const char *conf_path)
{
FILE *cfgf = fopen(conf_path, "r");
if (!cfgf) {
fprintf(stderr, "Error opening config: %s\n", strerror(errno));
return -1;
}
printf("config:\n");
char line[256];
while (fgets(line, sizeof(line), cfgf)) {
if (*line == '#' || *line == '\n')
continue;
char *separator = strchr(line, '=');
if (!separator) {
fprintf(stderr, "[config] malformed line: %s\n", line);
continue;
}
*separator = '\0';
char *value = separator + 1;
if (strcmp(line, "port") == 0) {
port = atoi(value);
printf("\tport: %d\n", port);
if (port == 0) {
fprintf(stderr, "[config] invalid port: %s\n", line);
return -1;
}
}
else if (strcmp(line, "template") == 0) {
value[strlen(value) - 1] = '\0';
tmpl_path = strdup(value);
printf("\ttemplate: %s\n", tmpl_path);
}
else if (strcmp(line, "root") == 0) {
value[strlen(value) - 1] = '\0';
root = strdup(value);
printf("\troot: %s\n", root);
}
else if (strcmp(line, "app_subdir") == 0) {
value[strlen(value) - 1] = '\0';
app_subdir = strdup(value);
printf("\tapp_subdir: %s\n", app_subdir);
}
else if (strcmp(line, "result_subdir") == 0) {
value[strlen(value) - 1] = '\0';
result_subdir = strdup(value);
printf("\tresult_subdir: %s\n", result_subdir);
}
else if (strcmp(line, "magic") == 0) {
value[strlen(value) - 1] = '\0';
magic_enable = (strcmp(value, "true") == 0);
printf("\tmagic: %d\n", magic_enable);
}
else if (strcmp(line, "period") == 0) {
value[strlen(value) - 1] = '\0';
period = atoi(value);
printf("\tperiod: %d\n", period);
}
else {
fprintf(stderr, "[config] unknown key: %s\n", line);
continue;
}
}
fclose(cfgf);
if (port == 0) {
fprintf(stderr, "[config] W: no port, using default\n");
port = DEFAULT_PORT;
}
if (!tmpl_path) {
fprintf(stderr, "[config] W: no template file given, using default\n");
tmpl_path = DEFAULT_TMPL_PATH;
}
if (!root) {
fprintf(stderr, "[config] E: no root given\n");
return -1;
}
if (!app_subdir) {
fprintf(stderr, "[config] E: no application subdirectory given\n");
return -1;
}
if (!result_subdir) {
fprintf(stderr, "[config] E: no result link subdirectory given\n");
return -1;
}
return 0;
}
|