config: allow whitespaces in config path

(cherry picked from commit 1d62d6bfa0)
This commit is contained in:
columbarius 2021-03-25 17:22:26 +01:00 committed by Simon Ser
parent a9563a3710
commit 8f5c546fca

View file

@ -338,35 +338,60 @@ static bool file_exists(const char *path) {
return path && access(path, R_OK) != -1; return path && access(path, R_OK) != -1;
} }
static char *get_config_path(void) { static char *config_path(const char *prefix, const char *config_folder) {
static const char *config_paths[] = { if (!prefix || !prefix[0] || !config_folder || !config_folder[0]) {
"$HOME/.sway/config", return NULL;
"$XDG_CONFIG_HOME/sway/config",
"$HOME/.i3/config",
"$XDG_CONFIG_HOME/i3/config",
SYSCONFDIR "/sway/config",
SYSCONFDIR "/i3/config",
};
char *config_home = getenv("XDG_CONFIG_HOME");
if (!config_home || !*config_home) {
config_paths[1] = "$HOME/.config/sway/config";
config_paths[3] = "$HOME/.config/i3/config";
} }
for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) { const char *filename = "config";
wordexp_t p;
if (wordexp(config_paths[i], &p, WRDE_UNDEF) == 0) { size_t size = 3 + strlen(prefix) + strlen(config_folder) + strlen(filename);
char *path = strdup(p.we_wordv[0]); char *path = calloc(size, sizeof(char));
wordfree(&p); snprintf(path, size, "%s/%s/%s", prefix, config_folder, filename);
if (file_exists(path)) {
return path; return path;
} }
free(path);
} static char *get_config_path(void) {
char *path = NULL;
const char *home = getenv("HOME");
size_t size_fallback = 1 + strlen(home) + strlen("/.config");
char *config_home_fallback = calloc(size_fallback, sizeof(char));
snprintf(config_home_fallback, size_fallback, "%s/.config", home);
const char *config_home = getenv("XDG_CONFIG_HOME");
if (config_home == NULL || config_home[0] == '\0') {
config_home = config_home_fallback;
} }
return NULL; struct config_path {
const char *prefix;
const char *config_folder;
};
struct config_path config_paths[] = {
{ .prefix = home, .config_folder = ".sway"},
{ .prefix = config_home, .config_folder = "sway"},
{ .prefix = home, .config_folder = ".i3"},
{ .prefix = config_home, .config_folder = "i3"},
{ .prefix = SYSCONFDIR, .config_folder = "sway"},
{ .prefix = SYSCONFDIR, .config_folder = "i3"}
};
size_t num_config_paths = sizeof(config_paths)/sizeof(config_paths[0]);
for (size_t i = 0; i < num_config_paths; i++) {
path = config_path(config_paths[i].prefix, config_paths[i].config_folder);
if (!path) {
continue;
}
if (file_exists(path)) {
break;
}
free(path);
path = NULL;
}
free(config_home_fallback);
return path;
} }
static bool load_config(const char *path, struct sway_config *config, static bool load_config(const char *path, struct sway_config *config,