summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--database.h11
-rw-r--r--litterbox.c10
-rw-r--r--xdg.c57
3 files changed, 37 insertions, 41 deletions
diff --git a/database.h b/database.h
index 9fc1fc8..15704f0 100644
--- a/database.h
+++ b/database.h
@@ -41,12 +41,8 @@
 #define SQL(...) #__VA_ARGS__
 #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
 
-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
-);
+const char *configPath(const char **dirs, const char *path);
+const char *dataPath(const char **dirs, const char *path);
 FILE *configOpen(const char *path, const char *mode);
 FILE *dataOpen(const char *path, const char *mode);
 void dataMkdir(const char *path);
@@ -122,9 +118,8 @@ static inline void dbFind(const char *path, int flags) {
 		dataMkdir("");
 	}
 
-	char buf[PATH_MAX];
 	const char *dirs = NULL;
-	while (NULL != (path = dataPath(buf, sizeof(buf), &dirs, DatabasePath))) {
+	while (NULL != (path = dataPath(&dirs, DatabasePath))) {
 		dbOpen(path, flags);
 		if (db) return;
 	}
diff --git a/litterbox.c b/litterbox.c
index 72df0c4..5a88132 100644
--- a/litterbox.c
+++ b/litterbox.c
@@ -858,11 +858,9 @@ int main(int argc, char *argv[]) {
 		tls_config_insecure_noverifyname(config);
 	}
 
-	const char *dirs;
-	char pbuf[PATH_MAX];
 	if (cert) {
-		dirs = NULL;
-		while (NULL != (path = configPath(pbuf, sizeof(pbuf), &dirs, cert))) {
+		const char *dirs = NULL;
+		while (NULL != (path = configPath(&dirs, cert))) {
 			if (priv) {
 				error = tls_config_set_cert_file(config, path);
 			} else {
@@ -873,8 +871,8 @@ int main(int argc, char *argv[]) {
 		if (error) errx(EX_NOINPUT, "%s: %s", cert, tls_config_error(config));
 	}
 	if (priv) {
-		dirs = NULL;
-		while (NULL != (path = configPath(pbuf, sizeof(pbuf), &dirs, priv))) {
+		const char *dirs = NULL;
+		while (NULL != (path = configPath(&dirs, priv))) {
 			error = tls_config_set_key_file(config, path);
 			if (!error) break;
 		}
diff --git a/xdg.c b/xdg.c
index ded02af..00cd021 100644
--- a/xdg.c
+++ b/xdg.c
@@ -33,6 +33,8 @@
 #include <string.h>
 #include <sys/stat.h>
 
+#include "database.h"
+
 #define SUBDIR "litterbox"
 
 struct Base {
@@ -56,14 +58,14 @@ static const struct Base Data = {
 	.defDirs = "/usr/local/share:/usr/share",
 };
 
-static const char *basePath(
-	struct Base base,
-	char *buf, size_t cap, const char **dirs, const char *path
-) {
+static const char *
+basePath(struct Base base, const char **dirs, const char *path) {
+	static char buf[PATH_MAX];
+
 	if (*dirs) {
 		if (!**dirs) return NULL;
 		size_t len = strcspn(*dirs, ":");
-		snprintf(buf, cap, "%.*s/" SUBDIR "/%s", (int)len, *dirs, path);
+		snprintf(buf, sizeof(buf), "%.*s/" SUBDIR "/%s", (int)len, *dirs, path);
 		*dirs += len;
 		if (**dirs) *dirs += 1;
 		return buf;
@@ -80,29 +82,30 @@ static const char *basePath(
 	const char *home = getenv("HOME");
 	const char *baseHome = getenv(base.envHome);
 	if (baseHome) {
-		snprintf(buf, cap, "%s/" SUBDIR "/%s", baseHome, path);
+		snprintf(buf, sizeof(buf), "%s/" SUBDIR "/%s", baseHome, path);
+	} else if (home) {
+		snprintf(
+			buf, sizeof(buf), "%s/%s/" SUBDIR "/%s",
+			home, base.defHome, path
+		);
 	} else {
-		if (!home) return NULL;
-		snprintf(buf, cap, "%s/%s/" SUBDIR "/%s", home, base.defHome, path);
+		return NULL;
 	}
 	return buf;
 }
 
-const char *
-configPath(char *buf, size_t cap, const char **dirs, const char *path) {
-	return basePath(Config, buf, cap, dirs, path);
+const char *configPath(const char **dirs, const char *path) {
+	return basePath(Config, dirs, path);
 }
 
 const char *
-dataPath(char *buf, size_t cap, const char **dirs, const char *path) {
-	return basePath(Data, buf, cap, dirs, path);
+dataPath(const char **dirs, const char *path) {
+	return basePath(Data, dirs, path);
 }
 
 FILE *configOpen(const char *path, const char *mode) {
-	const char *abs;
-	char buf[PATH_MAX];
 	const char *dirs = NULL;
-	while (NULL != (abs = configPath(buf, sizeof(buf), &dirs, path))) {
+	for (const char *abs; NULL != (abs = configPath(&dirs, path));) {
 		FILE *file = fopen(abs, mode);
 		if (file) return file;
 		if (errno != ENOENT) warn("%s", abs);
@@ -113,31 +116,31 @@ FILE *configOpen(const char *path, const char *mode) {
 }
 
 void dataMkdir(const char *path) {
-	char buf[PATH_MAX];
 	const char *dirs = NULL;
-	const char *abs = dataPath(buf, sizeof(buf), &dirs, path);
+	const char *abs = dataPath(&dirs, path);
+	if (!abs) return;
 	int error = mkdir(abs, S_IRWXU);
 	if (error && errno != EEXIST) warn("%s", abs);
 }
 
 FILE *dataOpen(const char *path, const char *mode) {
-	const char *abs;
-	char buf[PATH_MAX];
 	const char *dirs = NULL;
-	while (NULL != (abs = dataPath(buf, sizeof(buf), &dirs, path))) {
+	for (const char *abs; NULL != (abs = dataPath(&dirs, path));) {
 		FILE *file = fopen(abs, mode);
 		if (file) return file;
 		if (errno != ENOENT) warn("%s", abs);
 	}
 
 	if (mode[0] != 'r') {
-		dirs = NULL;
-		abs = dataPath(buf, sizeof(buf), &dirs, path);
-		if (!abs) return NULL;
-
 		dataMkdir("");
-		FILE *file = fopen(abs, mode);
-		if (!file) warn("%s", abs);
+		dirs = NULL;
+		path = dataPath(&dirs, path);
+		if (!path) {
+			warn("HOME unset");
+			return NULL;
+		}
+		FILE *file = fopen(path, mode);
+		if (!file) warn("%s", path);
 		return file;
 	}