From bb9dfd3d7ed3648090f28310f4f0051379c8ba79 Mon Sep 17 00:00:00 2001 From: bluemonk Date: Mon, 6 Feb 2023 11:14:28 +0100 Subject: [PATCH] Add some additional fallbacks when looking for the configuration file --- main.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 7aab81a..38fedb9 100644 --- a/main.c +++ b/main.c @@ -39,7 +39,8 @@ get_config_path_user_config(void) { struct passwd *passwd = getpwuid(getuid()); if (passwd == NULL) { - LOG_ERRNO("failed to lookup user"); + LOG_ERRNO("User entry not found in /etc/passwd. Are you using systemd-homed?"); + LOG_ERRNO("Please set $XDG_CONFIG_HOME, $HOME or $USER, or use the -c command line option"); return NULL; } @@ -56,8 +57,10 @@ static char * get_config_path_xdg(void) { const char *xdg_config_home = getenv("XDG_CONFIG_HOME"); - if (xdg_config_home == NULL) + if (xdg_config_home == NULL) { + LOG_ERRNO("$XDG_CONFIG_HOME environment variable not set, falling back to $HOME"); return NULL; + } int len = snprintf(NULL, 0, "%s/yambar/config.yml", xdg_config_home); char *path = malloc(len + 1); @@ -65,6 +68,36 @@ get_config_path_xdg(void) return path; } +static char * +get_config_path_user_home(void) +{ + const char *user_home = getenv("HOME"); + if (user_home == NULL) { + LOG_ERRNO("$HOME environment variable not set, falling back to /home/$USER"); + return NULL; + } + + int len = snprintf(NULL, 0, "%s/.config/yambar/config.yml", user_home); + char *path = malloc(len + 1); + snprintf(path, len + 1, "%s/.config/yambar/config.yml", user_home); + return path; +} + +static char * +get_config_path_user(void) +{ + const char *user = getenv("USER"); + if (user == NULL) { + LOG_ERRNO("$USER environment variable not set, falling back to search /etc/passwd"); + return NULL; + } + + int len = snprintf(NULL, 0, "/home/%s/.config/yambar/config.yml", user); + char *path = malloc(len + 1); + snprintf(path, len + 1, "/home/%s/.config/yambar/config.yml", user); + return path; +} + static char * get_config_path(void) { @@ -75,6 +108,16 @@ get_config_path(void) return config; free(config); + config = get_config_path_user_home(); + if (config != NULL && stat(config, &st) == 0 && S_ISREG(st.st_mode)) + return config; + free(config); + + config = get_config_path_user(); + if (config != NULL && stat(config, &st) == 0 && S_ISREG(st.st_mode)) + return config; + free(config); + /* 'Default' XDG_CONFIG_HOME */ config = get_config_path_user_config(); if (config != NULL && stat(config, &st) == 0 && S_ISREG(st.st_mode))