From fc6fca69455b1f653c9dab5350d2c0678d9ca883 Mon Sep 17 00:00:00 2001 From: Johan Malm Date: Fri, 7 Aug 2020 20:40:46 +0100 Subject: [PATCH] src/theme/theme-dir.c: support colon separated XDG_* env vars --- src/theme/theme-dir.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/theme/theme-dir.c b/src/theme/theme-dir.c index a78c5699..ee8b5842 100644 --- a/src/theme/theme-dir.c +++ b/src/theme/theme-dir.c @@ -7,6 +7,8 @@ #include #include #include +#include +#include struct dir { const char *prefix; @@ -26,29 +28,41 @@ static struct dir theme_dirs[] = { }; /* clang-format on */ +static bool isdir(const char *path) +{ + struct stat st; + return (!stat(path, &st) && S_ISDIR(st.st_mode)); +} + char *theme_dir(const char *theme_name) { static char buf[4096] = { 0 }; if (buf[0] != '\0') return buf; - struct stat st; for (int i = 0; theme_dirs[i].path; i++) { - char *prefix = NULL; struct dir d = theme_dirs[i]; - if (d.prefix) { - prefix = getenv(d.prefix); - if (!prefix) - continue; - snprintf(buf, sizeof(buf), "%s/%s/%s/openbox-3", prefix, - d.path, theme_name); - } else { + if (!d.prefix) { snprintf(buf, sizeof(buf), "%s/%s/openbox-3", d.path, theme_name); + if (isdir(buf)) + return buf; + } else { + char *prefix = getenv(d.prefix); + if (!prefix) + continue; + gchar **prefixes = g_strsplit(prefix, ":", -1); + for (gchar **p = prefixes; *p; p++) { + snprintf(buf, sizeof(buf), + "%s/%s/%s/openbox-3", *p, d.path, + theme_name); + if (isdir(buf)) + return buf; + } } - if (!stat(buf, &st) && S_ISDIR(st.st_mode)) - return buf; } - buf[0] = '\0'; + /* no config directory was found */ + buf[0] = '.'; + buf[1] = '\0'; return buf; }