about summary refs log tree commit diff
path: root/xdg.c
diff options
context:
space:
mode:
Diffstat (limited to 'xdg.c')
-rw-r--r--xdg.c172
1 files changed, 73 insertions, 99 deletions
diff --git a/xdg.c b/xdg.c
index 2500bac..75ee871 100644
--- a/xdg.c
+++ b/xdg.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 2019, 2020  C. McEnroe <june@causal.agency>
+/* Copyright (C) 2019, 2020  June McEnroe <june@causal.agency>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -32,126 +32,100 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/stat.h>
+#include <sysexits.h>
 
 #include "chat.h"
 
 #define SUBDIR "catgirl"
 
-FILE *configOpen(const char *path, const char *mode) {
-	if (path[0] == '/' || path[0] == '.') goto local;
-
-	const char *home = getenv("HOME");
-	const char *configHome = getenv("XDG_CONFIG_HOME");
-	const char *configDirs = getenv("XDG_CONFIG_DIRS");
-
-	char buf[PATH_MAX];
-	if (configHome) {
-		snprintf(buf, sizeof(buf), "%s/" SUBDIR "/%s", configHome, path);
-	} else {
-		if (!home) goto local;
-		snprintf(buf, sizeof(buf), "%s/.config/" SUBDIR "/%s", home, path);
+struct Base {
+	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 char *basePath(
+	struct Base base, char *buf, size_t cap, const char *path, int i
+) {
+	if (path[strspn(path, ".")] == '/') {
+		if (i > 0) return NULL;
+		snprintf(buf, cap, "%s", path);
+		return buf;
 	}
-	FILE *file = fopen(buf, mode);
-	if (file) return file;
-	if (errno != ENOENT) warn("%s", buf);
 
-	if (!configDirs) configDirs = "/etc/xdg";
-	while (*configDirs) {
-		size_t len = strcspn(configDirs, ":");
+	if (i > 0) {
+		const char *dirs = getenv(base.envDirs);
+		if (!dirs) dirs = base.defDirs;
+		for (; i > 1; --i) {
+			dirs += strcspn(dirs, ":");
+			dirs += (*dirs == ':');
+		}
+		if (!*dirs) return NULL;
 		snprintf(
-			buf, sizeof(buf), "%.*s/" SUBDIR "/%s",
-			(int)len, configDirs, path
+			buf, cap, "%.*s/" SUBDIR "/%s",
+			(int)strcspn(dirs, ":"), dirs, path
 		);
-		file = fopen(buf, mode);
-		if (file) return file;
-		if (errno != ENOENT) warn("%s", buf);
-		configDirs += len;
-		if (*configDirs) configDirs++;
+		return buf;
 	}
 
-local:
-	file = fopen(path, mode);
-	if (!file) warn("%s", path);
-	return file;
+	const char *home = getenv("HOME");
+	const char *baseHome = getenv(base.envHome);
+	if (baseHome) {
+		snprintf(buf, cap, "%s/" SUBDIR "/%s", baseHome, path);
+	} else if (home) {
+		snprintf(buf, cap, "%s/%s/" SUBDIR "/%s", home, base.defHome, path);
+	} else {
+		errx(EX_USAGE, "HOME unset");
+	}
+	return buf;
 }
 
-FILE *dataOpen(const char *path, const char *mode) {
-	if (path[0] == '/' || path[0] == '.') goto local;
+char *configPath(char *buf, size_t cap, const char *path, int i) {
+	return basePath(Config, buf, cap, path, i);
+}
 
-	const char *home = getenv("HOME");
-	const char *dataHome = getenv("XDG_DATA_HOME");
-	const char *dataDirs = getenv("XDG_DATA_DIRS");
+char *dataPath(char *buf, size_t cap, const char *path, int i) {
+	return basePath(Data, buf, cap, path, i);
+}
 
-	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 *configOpen(const char *path, const char *mode) {
+	char buf[PATH_MAX];
+	for (int i = 0; configPath(buf, sizeof(buf), path, i); ++i) {
+		FILE *file = fopen(buf, mode);
+		if (file) return file;
+		if (errno != ENOENT) warn("%s", buf);
 	}
-	FILE *file = fopen(homePath, mode);
-	if (file) return file;
-	if (errno != ENOENT) warn("%s", homePath);
+	warn("%s", configPath(buf, sizeof(buf), path, 0));
+	return NULL;
+}
 
+FILE *dataOpen(const char *path, const char *mode) {
 	char buf[PATH_MAX];
-	if (!dataDirs) dataDirs = "/usr/local/share:/usr/share";
-	while (*dataDirs) {
-		size_t len = strcspn(dataDirs, ":");
-		snprintf(
-			buf, sizeof(buf), "%.*s/" SUBDIR "/%s",
-			(int)len, dataDirs, path
-		);
-		file = fopen(buf, mode);
+	for (int i = 0; dataPath(buf, sizeof(buf), path, i); ++i) {
+		FILE *file = fopen(buf, mode);
 		if (file) return file;
 		if (errno != ENOENT) warn("%s", buf);
-		dataDirs += len;
-		if (*dataDirs) dataDirs++;
 	}
-
 	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;
+		int error = mkdir(dataPath(buf, sizeof(buf), "", 0), S_IRWXU);
+		if (error && errno != EEXIST) warn("%s", buf);
 	}
-
-local:
-	file = fopen(path, mode);
-	if (!file) warn("%s", path);
+	FILE *file = fopen(dataPath(buf, sizeof(buf), path, 0), mode);
+	if (!file) warn("%s", buf);
 	return file;
 }
-
-void dataMkdir(const char *path) {
-	const char *home = getenv("HOME");
-	const char *dataHome = getenv("XDG_DATA_HOME");
-
-	char homePath[PATH_MAX];
-	if (dataHome) {
-		snprintf(
-			homePath, sizeof(homePath),
-			"%s/" SUBDIR "/%s", dataHome, path
-		);
-	} else {
-		if (!home) return;
-		snprintf(
-			homePath, sizeof(homePath),
-			"%s/.local/share/" SUBDIR "/%s", home, path
-		);
-	}
-
-	int error = mkdir(homePath, S_IRWXU);
-	if (error && errno != EEXIST) warn("%s", homePath);
-}