blob: 051e85a716542385187445f0d2fb2f92db1ed9e2 (
plain)
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
|
/*
* pkg - Configuration
* https://rawnix.org
*
* Copyright (c) 2025 zorz@gmx.com
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "config.h"
#include "paths.h"
#include <ctype.h>
config_t *config_new(void)
{
config_t *c;
c = xmalloc(sizeof(config_t));
c->portdir_count = 0;
c->portdirs = NULL;
return c;
}
void config_free(config_t *c)
{
size_t i;
if (!c)
return;
for (i = 0; i < c->portdir_count; i++)
free(c->portdirs[i]);
free(c->portdirs);
free(c);
}
static void config_add_portdir(config_t *c, const char *dir)
{
c->portdirs = xrealloc(c->portdirs,
(c->portdir_count + 1) * sizeof(char *));
c->portdirs[c->portdir_count++] = xstrdup(dir);
}
int config_load(config_t *c, const char *path)
{
FILE *fp;
char line[MAX_LINE];
char *key, *val, *p;
fp = fopen(path ? path : CONFIG_FILE, "r");
if (!fp)
return 0; /* no config file is acceptable */
while (fgets(line, sizeof(line), fp)) {
p = strchr(line, '#');
if (p)
*p = '\0';
key = str_trim(line);
if (!key || !*key)
continue;
/* split key/value on first whitespace */
val = key;
while (*val && !isspace((unsigned char)*val))
val++;
if (*val) {
*val++ = '\0';
while (isspace((unsigned char)*val))
val++;
}
if (strcmp(key, "portdir") == 0 && val && *val)
config_add_portdir(c, val);
}
fclose(fp);
return 0;
}
|