summary refs log tree commit diff
path: root/config.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-08-20 21:33:12 -0400
committerJune McEnroe <june@causal.agency>2020-08-20 21:42:31 -0400
commitdd3b702fd104353970e8a047ab9b4382c935e5d2 (patch)
treeff1d4a3eea4943f59439bb0cc66efd90e021bb9e /config.c
parentSay "OpenSSL" in additional permission notices (diff)
downloadlitterbox-dd3b702fd104353970e8a047ab9b4382c935e5d2.tar.gz
litterbox-dd3b702fd104353970e8a047ab9b4382c935e5d2.zip
Import xdg.c from catgirl
Diffstat (limited to '')
-rw-r--r--config.c45
1 files changed, 3 insertions, 42 deletions
diff --git a/config.c b/config.c
index 60359d8..b8b4efa 100644
--- a/config.c
+++ b/config.c
@@ -33,43 +33,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#define CONFIG_DIR "litterbox"
-
-static FILE *find(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/" CONFIG_DIR "/%s", configHome, path);
-	} else {
-		if (!home) goto local;
-		snprintf(buf, sizeof(buf), "%s/.config/" CONFIG_DIR "/%s", home, path);
-	}
-	FILE *file = fopen(buf, mode);
-	if (file) return file;
-	if (errno != ENOENT) return NULL;
-
-	if (!configDirs) configDirs = "/etc/xdg";
-	while (*configDirs) {
-		size_t len = strcspn(configDirs, ":");
-		snprintf(
-			buf, sizeof(buf), "%.*s/" CONFIG_DIR "/%s",
-			(int)len, configDirs, path
-		);
-		file = fopen(buf, mode);
-		if (file) return file;
-		if (errno != ENOENT) return NULL;
-		configDirs += len;
-		if (*configDirs) configDirs++;
-	}
-
-local:
-	return fopen(path, mode);
-}
+#include "database.h"
 
 #define WS "\t "
 
@@ -102,11 +66,8 @@ int getopt_config(
 			if (optind < argc) {
 				num = 0;
 				path = argv[optind++];
-				file = find(path, "r");
-				if (!file) {
-					warn("%s", path);
-					return clean('?');
-				}
+				file = configOpen(path, "r");
+				if (!file) return clean('?');
 			} else {
 				return clean(-1);
 			}