/* * pkg - Configuration * https://rawnix.org * * Copyright (c) 2025 zorz@gmx.com * SPDX-License-Identifier: BSD-2-Clause */ #include "config.h" #include "paths.h" #include 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; }