From: Jonathan McDowell Date: Sat, 2 Nov 2013 04:15:04 +0000 (-0700) Subject: Try to use a user specific configuration file if available X-Git-Tag: onak-0.4.3~49 X-Git-Url: https://the.earth.li/gitweb/?a=commitdiff_plain;h=25515318015210c5f201950031bc0269f945f48a;p=onak.git Try to use a user specific configuration file if available Try looking for $XDG_CONFIG_HOME/onak.conf (or $HOME/.config/onak.conf if $XDG_CONFIG_HOME is not set) before looking for the system wide config file. This allows users to use their own config file without having to always use the -c option to provide it. --- diff --git a/onak-conf.c b/onak-conf.c index ec61cd2..a29a58b 100644 --- a/onak-conf.c +++ b/onak-conf.c @@ -92,10 +92,28 @@ void readconfig(const char *configfile) { FILE *conffile; char curline[1024]; int i; + char *dir, *conf; + size_t len; curline[1023] = 0; if (configfile == NULL) { - conffile = fopen(CONFIGFILE, "r"); + conffile = NULL; + if ((dir = getenv("XDG_CONFIG_HOME")) != NULL) { + len = strlen(dir) + 1 + 9 + 1; /* dir + / + onak.conf + NUL */ + conf = malloc(len); + snprintf(conf, len, "%s/onak.conf", dir); + conffile = fopen(conf, "r"); + free(conf); + } else if ((dir = getenv("HOME")) != NULL) { + len = strlen(dir) + 18 + 1; /* dir + /.config/onak.conf + NUL */ + conf = malloc(len); + snprintf(conf, len, "%s/.config/onak.conf", dir); + conffile = fopen(conf, "r"); + free(conf); + } + if (conffile == NULL) { + conffile = fopen(CONFIGFILE, "r"); + } } else { conffile = fopen(configfile, "r"); }