Fixes #38: Merge branch 'xdg-config-dirs'

This commit is contained in:
Jon Lund Steffensen
2014-03-13 01:50:43 +01:00
2 changed files with 64 additions and 11 deletions
+1
View File
@@ -39,6 +39,7 @@ stamp-h1
src/redshift-gtk/defs.py
src/redshift-gtk/redshift-gtk
contrib/redshift.spec
src/redshift
# gettext
/po/POTFILES
+63 -11
View File
@@ -22,6 +22,12 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#ifndef _WIN32
# include <pwd.h>
#endif
#include "config-ini.h"
@@ -41,33 +47,79 @@ 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
<http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>.
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];
char *env;
if ((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);
filepath = cp;
f = fopen(cp, "r");
}
#ifdef _WIN32
} else if ((env = getenv("localappdata")) != NULL && env[0] != '\0') {
if (f == NULL && (env = getenv("localappdata")) != NULL &&
env[0] != '\0') {
snprintf(cp, sizeof(cp),
"%s\\redshift.conf", env);
filepath = cp;
f = fopen(cp, "r");
}
#endif
} else if ((env = getenv("HOME")) != NULL && env[0] != '\0') {
if (f == NULL && (env = getenv("HOME")) != NULL &&
env[0] != '\0') {
snprintf(cp, sizeof(cp),
"%s/.config/redshift.conf", env);
filepath = cp;
f = fopen(cp, "r");
}
#ifndef _WIN32
if (f == NULL) {
struct passwd *pwd = getpwuid(getuid());
char *home = pwd->pw_dir;
snprintf(cp, sizeof(cp),
"%s/.config/redshift.conf", home);
f = fopen(cp, "r");
}
if (filepath != NULL) {
f = fopen(filepath, "r");
if (f != NULL) return f;
else if (f == NULL && errno != ENOENT) return NULL;
if (f == NULL && (env = getenv("XDG_CONFIG_DIRS")) != NULL &&
env[0] != '\0') {
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;
}
}
/* TODO look in getenv("XDG_CONFIG_DIRS") */
if (f == NULL) {
snprintf(cp, sizeof(cp),
"%s/redshift.conf", "/etc");
f = fopen(cp, "r");
}
#endif
return f;
} else {
f = fopen(filepath, "r");
if (f == NULL) {