fix config search paths

This commit is contained in:
taiyu 2015-09-20 10:56:22 -07:00
parent 9fd2d635a1
commit 72aaffcf5b

View File

@ -110,41 +110,42 @@ static void config_defaults(struct sway_config *config) {
static char *get_config_path(void) { static char *get_config_path(void) {
char *config_path = NULL; char *config_path = NULL;
char *paths[3] = {getenv("HOME"), getenv("XDG_CONFIG_HOME"), ""}; char *paths[3] = { getenv("HOME"), getenv("XDG_CONFIG_HOME"), "" };
int pathlen[3] = {0, 0, 0}; int pathlen[3] = { 0, 0, 0 };
int i; int i;
#define home paths[0] #define home paths[0]
#define conf paths[1] #define conf paths[1]
// Get home and config directories // Get home and config directories
conf = conf ? strdup(conf) : NULL;
home = home ? strdup(home) : NULL; home = home ? strdup(home) : NULL;
if (conf) { // If config folder is unset, set it to $HOME/.config
conf = strdup(conf); if (!conf && home) {
} else if (home) {
const char *def = "/.config"; const char *def = "/.config";
conf = malloc(strlen(home) + strlen(def) + 1); conf = malloc(strlen(home) + strlen(def) + 1);
strcpy(conf, home); strcpy(conf, home);
strcat(conf, def); strcat(conf, def);
} else {
home = strdup("");
conf = strdup("");
} }
pathlen[0] = strlen(home); // Get path lengths
pathlen[1] = strlen(conf); pathlen[0] = home ? strlen(home) : 0;
pathlen[1] = conf ? strlen(conf) : 0;
#undef home #undef home
#undef conf #undef conf
// Search for config file from search paths // Search for config file from search paths
static const char *search_paths[] = { static const char *search_paths[] = {
"/.sway/config", // Prepend with $home "/.sway/config", // Prepend with $home
"/sway/config", // Prepend with $config "/sway/config", // Prepend with $config
"/etc/sway/config", "/etc/sway/config",
"/.i3/config", // $home "/.i3/config", // $home
"/.i3/config", // $config "/i3/config", // $config
"/etc/i3/config" "/etc/i3/config"
}; };
for (i = 0; i < (int)(sizeof(search_paths) / sizeof(char *)); ++i) { for (i = 0; i < (int)(sizeof(search_paths) / sizeof(char *)); ++i) {
// Only try path if it is set by enviroment variables
if (paths[i%3]) {
char *test = malloc(pathlen[i%3] + strlen(search_paths[i]) + 1); char *test = malloc(pathlen[i%3] + strlen(search_paths[i]) + 1);
strcpy(test, paths[i%3]); strcpy(test, paths[i%3]);
strcat(test, search_paths[i]); strcpy(test + pathlen[i%3], search_paths[i]);
sway_log(L_DEBUG, "Checking for config at %s", test); sway_log(L_DEBUG, "Checking for config at %s", test);
if (file_exists(test)) { if (file_exists(test)) {
config_path = test; config_path = test;
@ -152,6 +153,7 @@ static char *get_config_path(void) {
} }
free(test); free(test);
} }
}
sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS");
char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS"); char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");