From 25515318015210c5f201950031bc0269f945f48a Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Fri, 1 Nov 2013 21:15:04 -0700 Subject: [PATCH] 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. --- onak-conf.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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"); } -- 2.39.2