Refactor base dir functions to iterate over paths
parent
b06470294d
commit
f432bd72fa
6
chat.h
6
chat.h
|
@ -294,6 +294,12 @@ void logFormat(uint id, const time_t *time, const char *format, ...)
|
||||||
__attribute__((format(printf, 3, 4)));
|
__attribute__((format(printf, 3, 4)));
|
||||||
void logClose(void);
|
void logClose(void);
|
||||||
|
|
||||||
|
const char *configPath(
|
||||||
|
char *buf, size_t cap, const char **dirs, const char *path
|
||||||
|
);
|
||||||
|
const char *dataPath(
|
||||||
|
char *buf, size_t cap, const char **dirs, const char *path
|
||||||
|
);
|
||||||
FILE *configOpen(const char *path, const char *mode);
|
FILE *configOpen(const char *path, const char *mode);
|
||||||
FILE *dataOpen(const char *path, const char *mode);
|
FILE *dataOpen(const char *path, const char *mode);
|
||||||
void dataMkdir(const char *path);
|
void dataMkdir(const char *path);
|
||||||
|
|
188
xdg.c
188
xdg.c
|
@ -37,121 +37,113 @@
|
||||||
|
|
||||||
#define SUBDIR "catgirl"
|
#define SUBDIR "catgirl"
|
||||||
|
|
||||||
FILE *configOpen(const char *path, const char *mode) {
|
struct Base {
|
||||||
if (path[0] == '/' || path[0] == '.') goto local;
|
const char *envHome;
|
||||||
|
const char *envDirs;
|
||||||
|
const char *defHome;
|
||||||
|
const char *defDirs;
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct Base Config = {
|
||||||
|
.envHome = "XDG_CONFIG_HOME",
|
||||||
|
.envDirs = "XDG_CONFIG_DIRS",
|
||||||
|
.defHome = ".config",
|
||||||
|
.defDirs = "/etc/xdg",
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct Base Data = {
|
||||||
|
.envHome = "XDG_DATA_HOME",
|
||||||
|
.envDirs = "XDG_DATA_DIRS",
|
||||||
|
.defHome = ".local/share",
|
||||||
|
.defDirs = "/usr/local/share:/usr/share",
|
||||||
|
};
|
||||||
|
|
||||||
|
static const char *basePath(
|
||||||
|
struct Base base,
|
||||||
|
char *buf, size_t cap, const char **dirs, const char *path
|
||||||
|
) {
|
||||||
|
if (*dirs) {
|
||||||
|
if (!**dirs) return NULL;
|
||||||
|
size_t len = strcspn(*dirs, ":");
|
||||||
|
snprintf(buf, cap, "%.*s/" SUBDIR "/%s", (int)len, *dirs, path);
|
||||||
|
*dirs += len;
|
||||||
|
if (**dirs) *dirs += 1;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path[0] == '/' || path[0] == '.') {
|
||||||
|
*dirs = "";
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
*dirs = getenv(base.envDirs);
|
||||||
|
if (!*dirs) *dirs = base.defDirs;
|
||||||
|
|
||||||
const char *home = getenv("HOME");
|
const char *home = getenv("HOME");
|
||||||
const char *configHome = getenv("XDG_CONFIG_HOME");
|
const char *baseHome = getenv(base.envHome);
|
||||||
const char *configDirs = getenv("XDG_CONFIG_DIRS");
|
if (baseHome) {
|
||||||
|
snprintf(buf, cap, "%s/" SUBDIR "/%s", baseHome, path);
|
||||||
char buf[PATH_MAX];
|
|
||||||
if (configHome) {
|
|
||||||
snprintf(buf, sizeof(buf), "%s/" SUBDIR "/%s", configHome, path);
|
|
||||||
} else {
|
} else {
|
||||||
if (!home) goto local;
|
if (!home) return NULL;
|
||||||
snprintf(buf, sizeof(buf), "%s/.config/" SUBDIR "/%s", home, path);
|
snprintf(buf, cap, "%s/%s/" SUBDIR "/%s", home, base.defHome, path);
|
||||||
}
|
}
|
||||||
FILE *file = fopen(buf, mode);
|
return buf;
|
||||||
if (file) return file;
|
|
||||||
if (errno != ENOENT) warn("%s", buf);
|
|
||||||
|
|
||||||
if (!configDirs) configDirs = "/etc/xdg";
|
|
||||||
while (*configDirs) {
|
|
||||||
size_t len = strcspn(configDirs, ":");
|
|
||||||
snprintf(
|
|
||||||
buf, sizeof(buf), "%.*s/" SUBDIR "/%s",
|
|
||||||
(int)len, configDirs, path
|
|
||||||
);
|
|
||||||
file = fopen(buf, mode);
|
|
||||||
if (file) return file;
|
|
||||||
if (errno != ENOENT) warn("%s", buf);
|
|
||||||
configDirs += len;
|
|
||||||
if (*configDirs) configDirs++;
|
|
||||||
}
|
|
||||||
|
|
||||||
local:
|
|
||||||
file = fopen(path, mode);
|
|
||||||
if (!file) warn("%s", path);
|
|
||||||
return file;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *dataOpen(const char *path, const char *mode) {
|
const char *
|
||||||
if (path[0] == '/' || path[0] == '.') goto local;
|
configPath(char *buf, size_t cap, const char **dirs, const char *path) {
|
||||||
|
return basePath(Config, buf, cap, dirs, path);
|
||||||
|
}
|
||||||
|
|
||||||
const char *home = getenv("HOME");
|
const char *
|
||||||
const char *dataHome = getenv("XDG_DATA_HOME");
|
dataPath(char *buf, size_t cap, const char **dirs, const char *path) {
|
||||||
const char *dataDirs = getenv("XDG_DATA_DIRS");
|
return basePath(Data, buf, cap, dirs, path);
|
||||||
|
}
|
||||||
char homePath[PATH_MAX];
|
|
||||||
if (dataHome) {
|
|
||||||
snprintf(
|
|
||||||
homePath, sizeof(homePath),
|
|
||||||
"%s/" SUBDIR "/%s", dataHome, path
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
if (!home) goto local;
|
|
||||||
snprintf(
|
|
||||||
homePath, sizeof(homePath),
|
|
||||||
"%s/.local/share/" SUBDIR "/%s", home, path
|
|
||||||
);
|
|
||||||
}
|
|
||||||
FILE *file = fopen(homePath, mode);
|
|
||||||
if (file) return file;
|
|
||||||
if (errno != ENOENT) warn("%s", homePath);
|
|
||||||
|
|
||||||
|
FILE *configOpen(const char *path, const char *mode) {
|
||||||
|
const char *abs;
|
||||||
char buf[PATH_MAX];
|
char buf[PATH_MAX];
|
||||||
if (!dataDirs) dataDirs = "/usr/local/share:/usr/share";
|
const char *dirs = NULL;
|
||||||
while (*dataDirs) {
|
while (NULL != (abs = configPath(buf, sizeof(buf), &dirs, path))) {
|
||||||
size_t len = strcspn(dataDirs, ":");
|
FILE *file = fopen(abs, mode);
|
||||||
snprintf(
|
|
||||||
buf, sizeof(buf), "%.*s/" SUBDIR "/%s",
|
|
||||||
(int)len, dataDirs, path
|
|
||||||
);
|
|
||||||
file = fopen(buf, mode);
|
|
||||||
if (file) return file;
|
if (file) return file;
|
||||||
if (errno != ENOENT) warn("%s", buf);
|
if (errno != ENOENT) warn("%s", abs);
|
||||||
dataDirs += len;
|
|
||||||
if (*dataDirs) dataDirs++;
|
|
||||||
}
|
}
|
||||||
|
FILE *file = fopen(path, mode);
|
||||||
if (mode[0] != 'r') {
|
|
||||||
char *base = strrchr(homePath, '/');
|
|
||||||
*base = '\0';
|
|
||||||
int error = mkdir(homePath, S_IRWXU);
|
|
||||||
if (error && errno != EEXIST) {
|
|
||||||
warn("%s", homePath);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
*base = '/';
|
|
||||||
file = fopen(homePath, mode);
|
|
||||||
if (!file) warn("%s", homePath);
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
local:
|
|
||||||
file = fopen(path, mode);
|
|
||||||
if (!file) warn("%s", path);
|
if (!file) warn("%s", path);
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dataMkdir(const char *path) {
|
void dataMkdir(const char *path) {
|
||||||
const char *home = getenv("HOME");
|
char buf[PATH_MAX];
|
||||||
const char *dataHome = getenv("XDG_DATA_HOME");
|
const char *dirs = NULL;
|
||||||
|
const char *abs = dataPath(buf, sizeof(buf), &dirs, path);
|
||||||
|
int error = mkdir(abs, S_IRWXU);
|
||||||
|
if (error && errno != EEXIST) warn("%s", abs);
|
||||||
|
}
|
||||||
|
|
||||||
char homePath[PATH_MAX];
|
FILE *dataOpen(const char *path, const char *mode) {
|
||||||
if (dataHome) {
|
const char *abs;
|
||||||
snprintf(
|
char buf[PATH_MAX];
|
||||||
homePath, sizeof(homePath),
|
const char *dirs = NULL;
|
||||||
"%s/" SUBDIR "/%s", dataHome, path
|
while (NULL != (abs = dataPath(buf, sizeof(buf), &dirs, path))) {
|
||||||
);
|
FILE *file = fopen(abs, mode);
|
||||||
} else {
|
if (file) return file;
|
||||||
if (!home) return;
|
if (errno != ENOENT) warn("%s", abs);
|
||||||
snprintf(
|
|
||||||
homePath, sizeof(homePath),
|
|
||||||
"%s/.local/share/" SUBDIR "/%s", home, path
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int error = mkdir(homePath, S_IRWXU);
|
if (mode[0] != 'r') {
|
||||||
if (error && errno != EEXIST) warn("%s", homePath);
|
dirs = NULL;
|
||||||
|
abs = dataPath(buf, sizeof(buf), &dirs, path);
|
||||||
|
if (!abs) return NULL;
|
||||||
|
|
||||||
|
dataMkdir("");
|
||||||
|
FILE *file = fopen(abs, mode);
|
||||||
|
if (!file) warn("%s", abs);
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *file = fopen(path, mode);
|
||||||
|
if (!file) warn("%s", path);
|
||||||
|
return file;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue