|
|
@@ -27,6 +27,7 @@
|
|
|
#include <string.h>
|
|
|
#include <errno.h>
|
|
|
#include <ctype.h>
|
|
|
+#include <dirent.h>
|
|
|
#include <stdarg.h>
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
@@ -110,9 +111,64 @@ void myfileopt(const char* fn, const struct cmdinfo* cmdinfos) {
|
|
|
if (fclose(file)) ohshite(_("error closing configuration file `%.255s'"), fn);
|
|
|
}
|
|
|
|
|
|
+static int
|
|
|
+valid_config_filename(const struct dirent *dent)
|
|
|
+{
|
|
|
+ const char *c;
|
|
|
+
|
|
|
+ if (dent->d_name[0] == '.')
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ for (c = dent->d_name; *c; c++)
|
|
|
+ if (!isalnum(*c) && *c != '_' && *c != '-')
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ if (*c == '\0')
|
|
|
+ return 1;
|
|
|
+ else
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static void
|
|
|
+load_config_dir(const char *prog, const struct cmdinfo* cmdinfos)
|
|
|
+{
|
|
|
+ char *dirname;
|
|
|
+ struct dirent **dlist;
|
|
|
+ int dlist_n, i;
|
|
|
+
|
|
|
+ dirname = m_malloc(strlen(CONFIGDIR "/.cfg.d") + strlen(prog) + 1);
|
|
|
+ sprintf(dirname, "%s/%s.cfg.d", CONFIGDIR, prog);
|
|
|
+
|
|
|
+ dlist_n = scandir(dirname, &dlist, valid_config_filename, alphasort);
|
|
|
+ if (dlist_n < 0) {
|
|
|
+ if (errno == ENOENT) {
|
|
|
+ free(dirname);
|
|
|
+ return;
|
|
|
+ } else
|
|
|
+ ohshite(_("error opening configuration directory '%s'"), dirname);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (i = 0; i < dlist_n; i++) {
|
|
|
+ char *filename;
|
|
|
+
|
|
|
+ filename = m_malloc(strlen(dirname) + 1 + strlen(dlist[i]->d_name) + 1);
|
|
|
+ sprintf(filename, "%s/%s", dirname, dlist[i]->d_name);
|
|
|
+
|
|
|
+ myfileopt(filename, cmdinfos);
|
|
|
+
|
|
|
+ free(filename);
|
|
|
+ }
|
|
|
+
|
|
|
+ free(dirname);
|
|
|
+ free(dlist);
|
|
|
+}
|
|
|
+
|
|
|
void loadcfgfile(const char *prog, const struct cmdinfo* cmdinfos) {
|
|
|
char *home, *file;
|
|
|
int l1, l2;
|
|
|
+
|
|
|
+ load_config_dir(prog, cmdinfos);
|
|
|
+
|
|
|
l1 = strlen(CONFIGDIR "/.cfg") + strlen(prog);
|
|
|
file = m_malloc(l1 + 1);
|
|
|
sprintf(file, CONFIGDIR "/%s.cfg", prog);
|