Skip to content

Commit

Permalink
config-ini: Properly respect XDG_CONFIG_HOME base directory specifica…
Browse files Browse the repository at this point in the history
…tion

If redshift.conf is not found, fall back to formerly used path.
  • Loading branch information
kakurasan committed May 4, 2018
1 parent 36e7877 commit 9ff0501
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/config-ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,15 @@ open_config_file(const char *filepath)

if (f == NULL && (env = getenv("XDG_CONFIG_HOME")) != NULL &&
env[0] != '\0') {
snprintf(cp, sizeof(cp), "%s/redshift.conf", env);
snprintf(cp, sizeof(cp),
"%s/redshift/redshift.conf", env);
f = fopen(cp, "r");
if (f == NULL) {
/* Fall back to formerly used path. */
snprintf(cp, sizeof(cp),
"%s/redshift.conf", env);
f = fopen(cp, "r");
}
}

#ifdef _WIN32
Expand All @@ -78,17 +85,29 @@ open_config_file(const char *filepath)
if (f == NULL && (env = getenv("HOME")) != NULL &&
env[0] != '\0') {
snprintf(cp, sizeof(cp),
"%s/.config/redshift.conf", env);
"%s/.config/redshift/redshift.conf", env);
f = fopen(cp, "r");
if (f == NULL) {
/* Fall back to formerly used path. */
snprintf(cp, sizeof(cp),
"%s/.config/redshift.conf", env);
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);
"%s/.config/redshift/redshift.conf", home);
f = fopen(cp, "r");
if (f == NULL) {
/* Fall back to formerly used path. */
snprintf(cp, sizeof(cp),
"%s/.config/redshift.conf", home);
f = fopen(cp, "r");
}
}

if (f == NULL && (env = getenv("XDG_CONFIG_DIRS")) != NULL &&
Expand All @@ -101,9 +120,14 @@ open_config_file(const char *filepath)
int len = end - begin;
if (len > 0) {
snprintf(cp, sizeof(cp),
"%.*s/redshift.conf", len, begin);

"%.*s/redshift/redshift.conf", len, begin);
f = fopen(cp, "r");
if (f != NULL) {
/* Fall back to formerly used path. */
snprintf(cp, sizeof(cp),
"%.*s/redshift.conf", len, begin);
f = fopen(cp, "r");
}
if (f != NULL) break;
}

Expand Down

0 comments on commit 9ff0501

Please sign in to comment.