From 415b5cc0e8df9471ffdea34e56f7608a778bd502 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Wed, 4 Mar 2009 09:16:18 +0100 Subject: [PATCH] Load configuration file from ~/.i3/config or /etc/i3/config --- include/config.h | 2 +- src/config.c | 42 ++++++++++++++++++++++++++++++++---------- src/mainx.c | 2 +- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/include/config.h b/include/config.h index 5ebdc1db..4aa0192b 100644 --- a/include/config.h +++ b/include/config.h @@ -9,6 +9,6 @@ struct Config { const char *font; }; -void load_configuration(const char *configfile); +void load_configuration(const char *override_configfile); #endif diff --git a/src/config.c b/src/config.c index 1b4e1c34..1cc0e57f 100644 --- a/src/config.c +++ b/src/config.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "i3.h" #include "util.h" @@ -20,10 +21,27 @@ Config config; /* - * Reads the configuration from the given file + * This function resolves ~ in pathnames. * */ -void load_configuration(const char *configfile) { +static char *glob_path(const char *path) { + static glob_t globbuf; + if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) + die("glob() failed"); + char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path); + globfree(&globbuf); + return result; +} + + +/* + * Reads the configuration from ~/.i3/config or /etc/i3/config if not found. + * + * If you specify override_configpath, only this path is used to look for a + * configuration file. + * + */ +void load_configuration(const char *override_configpath) { #define OPTION_STRING(name) \ if (strcasecmp(key, #name) == 0) { \ config.name = sstrdup(value); \ @@ -37,14 +55,18 @@ void load_configuration(const char *configfile) { /* Clear the old config or initialize the data structure */ memset(&config, 0, sizeof(config)); - /* check if the file exists */ - struct stat buf; - if (stat(configfile, &buf) < 0) - return; - - FILE *handle = fopen(configfile, "r"); - if (handle == NULL) - die("Could not open configfile\n"); + FILE *handle; + if (override_configpath != NULL) { + if ((handle = fopen(override_configpath, "r")) == NULL) + die("Could not open configfile \"%s\".\n", override_configpath); + } else { + /* We first check for ~/.i3/config, then for /etc/i3/config */ + char *globbed = glob_path("~/.i3/config"); + if ((handle = fopen(globbed, "r")) == NULL) + if ((handle = fopen("/etc/i3/config", "r")) == NULL) + die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed); + free(globbed); + } char key[512], value[512], buffer[1026]; while (!feof(handle)) { diff --git a/src/mainx.c b/src/mainx.c index f67b455f..4dbe7794 100644 --- a/src/mainx.c +++ b/src/mainx.c @@ -304,7 +304,7 @@ int main(int argc, char *argv[], char *env[]) { byChild = alloc_table(); byParent = alloc_table(); - load_configuration("i3.config"); + load_configuration(NULL); c = xcb_connect(NULL, &screens); -- 2.39.5