From f5a7d6e3931aa248de271e481b21b1275ec3084c Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 11 Mar 2014 17:57:30 +0100 Subject: If even HOME is missing look up the user's home directory from passwd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/config-ini.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/config-ini.c b/src/config-ini.c index 4541a0f..3183ba1 100644 --- a/src/config-ini.c +++ b/src/config-ini.c @@ -22,6 +22,10 @@ #include #include #include +#ifndef _WIN32 +# include +# include +#endif #include "config-ini.h" @@ -59,6 +63,14 @@ open_config_file(const char *filepath) snprintf(cp, sizeof(cp), "%s/.config/redshift.conf", env); filepath = cp; +#ifndef _WIN32 + } else { + struct passwd *pwd = getpwuid(getuid()); + char *home = pwd->pw_dir; + snprintf(cp, sizeof(cp), + "%s/.config/redshift.conf", home); + filepath = cp; +#endif } if (filepath != NULL) { -- cgit v1.2.3-70-g09d2 From 058db22adaab28c148436743a38ce46bb031166b Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 11 Mar 2014 17:58:08 +0100 Subject: Add src/redshift to .gitignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 8744db9..b405004 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,7 @@ stamp-h1 src/redshift-gtk/defs.py src/redshift-gtk/redshift-gtk contrib/redshift.spec +src/redshift # gettext /po/POTFILES -- cgit v1.2.3-70-g09d2 From bb155ac18597f0b91c7cc7c612286b70bd943815 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 11 Mar 2014 18:19:47 +0100 Subject: When looking for a redshift.conf file do not just check environment variable, also check that the file exists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/config-ini.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/config-ini.c b/src/config-ini.c index 3183ba1..8449a8d 100644 --- a/src/config-ini.c +++ b/src/config-ini.c @@ -22,9 +22,11 @@ #include #include #include +#include +#include +#include #ifndef _WIN32 # include -# include #endif #include "config-ini.h" @@ -48,28 +50,38 @@ open_config_file(const char *filepath) if (filepath == NULL) { char cp[MAX_CONFIG_PATH]; char *env; + struct stat attr; - if ((env = getenv("XDG_CONFIG_HOME")) != NULL && + if (filepath == NULL && (env = getenv("XDG_CONFIG_HOME")) != NULL && env[0] != '\0') { snprintf(cp, sizeof(cp), "%s/redshift.conf", env); - filepath = cp; + if (!stat(cp, &attr)) + filepath = cp; + } #ifdef _WIN32 - } else if ((env = getenv("localappdata")) != NULL && env[0] != '\0') { + if (filepath == NULL && (env = getenv("localappdata")) != NULL && + env[0] != '\0') { snprintf(cp, sizeof(cp), "%s\\redshift.conf", env); - filepath = cp; + if (!stat(cp, &attr)) + filepath = cp; + } #endif - } else if ((env = getenv("HOME")) != NULL && env[0] != '\0') { + if (filepath == NULL && (env = getenv("HOME")) != NULL && + env[0] != '\0') { snprintf(cp, sizeof(cp), "%s/.config/redshift.conf", env); - filepath = cp; + if (!stat(cp, &attr)) + filepath = cp; + } #ifndef _WIN32 - } else { + if (filepath == NULL) { struct passwd *pwd = getpwuid(getuid()); char *home = pwd->pw_dir; snprintf(cp, sizeof(cp), "%s/.config/redshift.conf", home); - filepath = cp; + if (!stat(cp, &attr)) + filepath = cp; #endif } -- cgit v1.2.3-70-g09d2 From 68deadbda4a9ddc508d421591ddb902d22c85f97 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 11 Mar 2014 18:33:00 +0100 Subject: Look for configuration file in XDG_CONFIG_DIRS and /etc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/config-ini.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/config-ini.c b/src/config-ini.c index 8449a8d..bc8e00a 100644 --- a/src/config-ini.c +++ b/src/config-ini.c @@ -82,16 +82,43 @@ open_config_file(const char *filepath) "%s/.config/redshift.conf", home); if (!stat(cp, &attr)) filepath = cp; -#endif } + if (filepath == NULL && (env = getenv("XDG_CONFIG_DIRS")) != NULL && + env[0] != '\0') { + int conf_dirs_len = strlen(env); + int conf_dir_ptr = 0; + char *conf_dir = alloca((conf_dirs_len + 1) * sizeof(char)); + int i; + for (i = 0; i < conf_dirs_len; i++) { + char c = env[i]; + if (c == ':' || c == '\0') { + conf_dir[conf_dir_ptr] = '\0'; + conf_dir_ptr = 0; + if (conf_dir[0] != '\0') { + snprintf(cp, sizeof(cp), + "%s/redshift.conf", conf_dir); + if (!stat(cp, &attr)) { + filepath = cp; + break; + } + } + } else + conf_dir[conf_dir_ptr++] = c; + } + } + if (filepath == NULL) { + snprintf(cp, sizeof(cp), + "%s/redshift.conf", "/etc"); + if (!stat(cp, &attr)) + filepath = cp; + } +#endif if (filepath != NULL) { f = fopen(filepath, "r"); if (f != NULL) return f; else if (f == NULL && errno != ENOENT) return NULL; } - - /* TODO look in getenv("XDG_CONFIG_DIRS") */ } else { f = fopen(filepath, "r"); if (f == NULL) { -- cgit v1.2.3-70-g09d2 From 9a33837a3f67d1896243d071455a0c3f0fd41729 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 12 Mar 2014 21:06:14 +0100 Subject: fopen directly instead of stat:ing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/config-ini.c | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/src/config-ini.c b/src/config-ini.c index bc8e00a..01519b5 100644 --- a/src/config-ini.c +++ b/src/config-ini.c @@ -48,42 +48,38 @@ open_config_file(const char *filepath) FILE *f = NULL; if (filepath == NULL) { + FILE *f = NULL; char cp[MAX_CONFIG_PATH]; char *env; - struct stat attr; - if (filepath == NULL && (env = getenv("XDG_CONFIG_HOME")) != NULL && + if (f == NULL && (env = getenv("XDG_CONFIG_HOME")) != NULL && env[0] != '\0') { snprintf(cp, sizeof(cp), "%s/redshift.conf", env); - if (!stat(cp, &attr)) - filepath = cp; + f = fopen(cp, "r"); } #ifdef _WIN32 - if (filepath == NULL && (env = getenv("localappdata")) != NULL && + if (f == NULL && (env = getenv("localappdata")) != NULL && env[0] != '\0') { snprintf(cp, sizeof(cp), "%s\\redshift.conf", env); - if (!stat(cp, &attr)) - filepath = cp; + f = fopen(cp, "r"); } #endif - if (filepath == NULL && (env = getenv("HOME")) != NULL && + if (f == NULL && (env = getenv("HOME")) != NULL && env[0] != '\0') { snprintf(cp, sizeof(cp), "%s/.config/redshift.conf", env); - if (!stat(cp, &attr)) - filepath = cp; + f = fopen(cp, "r"); } #ifndef _WIN32 - if (filepath == NULL) { + if (f == NULL) { struct passwd *pwd = getpwuid(getuid()); char *home = pwd->pw_dir; snprintf(cp, sizeof(cp), "%s/.config/redshift.conf", home); - if (!stat(cp, &attr)) - filepath = cp; + f = fopen(cp, "r"); } - if (filepath == NULL && (env = getenv("XDG_CONFIG_DIRS")) != NULL && + if (f == NULL && (env = getenv("XDG_CONFIG_DIRS")) != NULL && env[0] != '\0') { int conf_dirs_len = strlen(env); int conf_dir_ptr = 0; @@ -97,28 +93,21 @@ open_config_file(const char *filepath) if (conf_dir[0] != '\0') { snprintf(cp, sizeof(cp), "%s/redshift.conf", conf_dir); - if (!stat(cp, &attr)) { - filepath = cp; + if ((f = fopen(cp, "r")) != NULL) break; - } } } else conf_dir[conf_dir_ptr++] = c; } } - if (filepath == NULL) { + if (f == NULL) { snprintf(cp, sizeof(cp), "%s/redshift.conf", "/etc"); - if (!stat(cp, &attr)) - filepath = cp; + f = fopen(cp, "r"); } #endif - if (filepath != NULL) { - f = fopen(filepath, "r"); - if (f != NULL) return f; - else if (f == NULL && errno != ENOENT) return NULL; - } + return f; } else { f = fopen(filepath, "r"); if (f == NULL) { -- cgit v1.2.3-70-g09d2 From 0ab205f50ae2f82bf495e72b62adbfdc0e82b387 Mon Sep 17 00:00:00 2001 From: Jon Lund Steffensen Date: Thu, 13 Mar 2014 01:38:59 +0100 Subject: config-ini: Simplify file path construction for XDG_CONFIG_DIRS Add comment pointing to the XDG Base Directory Specification. --- src/config-ini.c | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/config-ini.c b/src/config-ini.c index 01519b5..75e7f7e 100644 --- a/src/config-ini.c +++ b/src/config-ini.c @@ -47,6 +47,15 @@ open_config_file(const char *filepath) { FILE *f = NULL; + /* If a path is not specified (filepath is NULL) then + the configuration file is searched for in the directories + specified by the XDG Base Directory Specification + . + + If HOME is not set, getpwuid() is consulted for the home directory. On + windows platforms the %localappdata% is used in place of XDG_CONFIG_HOME. + */ + if (filepath == NULL) { FILE *f = NULL; char cp[MAX_CONFIG_PATH]; @@ -57,6 +66,7 @@ open_config_file(const char *filepath) snprintf(cp, sizeof(cp), "%s/redshift.conf", env); f = fopen(cp, "r"); } + #ifdef _WIN32 if (f == NULL && (env = getenv("localappdata")) != NULL && env[0] != '\0') { @@ -72,6 +82,7 @@ open_config_file(const char *filepath) f = fopen(cp, "r"); } #ifndef _WIN32 + if (f == NULL) { struct passwd *pwd = getpwuid(getuid()); char *home = pwd->pw_dir; @@ -79,27 +90,28 @@ open_config_file(const char *filepath) "%s/.config/redshift.conf", home); f = fopen(cp, "r"); } + if (f == NULL && (env = getenv("XDG_CONFIG_DIRS")) != NULL && env[0] != '\0') { - int conf_dirs_len = strlen(env); - int conf_dir_ptr = 0; - char *conf_dir = alloca((conf_dirs_len + 1) * sizeof(char)); - int i; - for (i = 0; i < conf_dirs_len; i++) { - char c = env[i]; - if (c == ':' || c == '\0') { - conf_dir[conf_dir_ptr] = '\0'; - conf_dir_ptr = 0; - if (conf_dir[0] != '\0') { - snprintf(cp, sizeof(cp), - "%s/redshift.conf", conf_dir); - if ((f = fopen(cp, "r")) != NULL) - break; - } - } else - conf_dir[conf_dir_ptr++] = c; + char *begin = env; + while (1) { + char *end = strchr(begin, ':'); + if (end == NULL) end = strchr(begin, '\0'); + + int len = end - begin; + if (len > 0) { + snprintf(cp, sizeof(cp), + "%.*s/redshift.conf", len, begin); + + f = fopen(cp, "r"); + if (f != NULL) break; + } + + if (end[0] == '\0') break; + begin = end + 1; } } + if (f == NULL) { snprintf(cp, sizeof(cp), "%s/redshift.conf", "/etc"); -- cgit v1.2.3-70-g09d2