src/theme/theme-dir.c: support colon separated XDG_* env vars

This commit is contained in:
Johan Malm 2020-08-07 20:40:46 +01:00
parent ff03f1d6aa
commit fc6fca6945

View file

@ -7,6 +7,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <glib.h>
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;
}