Skip to content

Commit 1d62d6b

Browse files
authored
config: allow whitespaces in config path
1 parent 346f5a9 commit 1d62d6b

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

sway/config.c

+47-22
Original file line numberDiff line numberDiff line change
@@ -338,35 +338,60 @@ static bool file_exists(const char *path) {
338338
return path && access(path, R_OK) != -1;
339339
}
340340

341+
static char *config_path(const char *prefix, const char *config_folder) {
342+
if (!prefix || !prefix[0] || !config_folder || !config_folder[0]) {
343+
return NULL;
344+
}
345+
346+
const char *filename = "config";
347+
348+
size_t size = 3 + strlen(prefix) + strlen(config_folder) + strlen(filename);
349+
char *path = calloc(size, sizeof(char));
350+
snprintf(path, size, "%s/%s/%s", prefix, config_folder, filename);
351+
return path;
352+
}
353+
341354
static char *get_config_path(void) {
342-
static const char *config_paths[] = {
343-
"$HOME/.sway/config",
344-
"$XDG_CONFIG_HOME/sway/config",
345-
"$HOME/.i3/config",
346-
"$XDG_CONFIG_HOME/i3/config",
347-
SYSCONFDIR "/sway/config",
348-
SYSCONFDIR "/i3/config",
355+
char *path = NULL;
356+
const char *home = getenv("HOME");
357+
size_t size_fallback = 1 + strlen(home) + strlen("/.config");
358+
char *config_home_fallback = calloc(size_fallback, sizeof(char));
359+
snprintf(config_home_fallback, size_fallback, "%s/.config", home);
360+
361+
const char *config_home = getenv("XDG_CONFIG_HOME");
362+
if (config_home == NULL || config_home[0] == '\0') {
363+
config_home = config_home_fallback;
364+
}
365+
366+
struct config_path {
367+
const char *prefix;
368+
const char *config_folder;
349369
};
350370

351-
char *config_home = getenv("XDG_CONFIG_HOME");
352-
if (!config_home || !*config_home) {
353-
config_paths[1] = "$HOME/.config/sway/config";
354-
config_paths[3] = "$HOME/.config/i3/config";
355-
}
371+
struct config_path config_paths[] = {
372+
{ .prefix = home, .config_folder = ".sway"},
373+
{ .prefix = config_home, .config_folder = "sway"},
374+
{ .prefix = home, .config_folder = ".i3"},
375+
{ .prefix = config_home, .config_folder = "i3"},
376+
{ .prefix = SYSCONFDIR, .config_folder = "sway"},
377+
{ .prefix = SYSCONFDIR, .config_folder = "i3"}
378+
};
356379

357-
for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) {
358-
wordexp_t p;
359-
if (wordexp(config_paths[i], &p, WRDE_UNDEF) == 0) {
360-
char *path = strdup(p.we_wordv[0]);
361-
wordfree(&p);
362-
if (file_exists(path)) {
363-
return path;
364-
}
365-
free(path);
380+
size_t num_config_paths = sizeof(config_paths)/sizeof(config_paths[0]);
381+
for (size_t i = 0; i < num_config_paths; i++) {
382+
path = config_path(config_paths[i].prefix, config_paths[i].config_folder);
383+
if (!path) {
384+
continue;
366385
}
386+
if (file_exists(path)) {
387+
break;
388+
}
389+
free(path);
390+
path = NULL;
367391
}
368392

369-
return NULL;
393+
free(config_home_fallback);
394+
return path;
370395
}
371396

372397
static bool load_config(const char *path, struct sway_config *config,

0 commit comments

Comments
 (0)