summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-08-20 21:52:16 -0400
committerJune McEnroe <june@causal.agency>2020-08-20 22:20:30 -0400
commit7ee5fbd0ab8993b2e569909ff2edbaff21910d4a (patch)
tree4b5ac7ef287feba8e155d44b092e5996fece8671
parentImport xdg.c from catgirl (diff)
downloadlitterbox-7ee5fbd0ab8993b2e569909ff2edbaff21910d4a.tar.gz
litterbox-7ee5fbd0ab8993b2e569909ff2edbaff21910d4a.zip
Use dataPath for dbFind
-rw-r--r--Makefile14
-rw-r--r--database.h39
-rw-r--r--litterbox.c2
-rw-r--r--scoop.c2
-rw-r--r--unscoop.c2
5 files changed, 22 insertions, 37 deletions
diff --git a/Makefile b/Makefile
index 46796b7..e85d6c2 100644
--- a/Makefile
+++ b/Makefile
@@ -11,18 +11,24 @@ RCS  = rc.d/litterbox
 
 -include config.mk
 
-FORMATS = generic catgirl irc textual
 OBJS.litterbox = litterbox.o config.o xdg.o
+OBJS.scoop = scoop.o xdg.o
+OBJS.unscoop = unscoop.o xdg.o
+
+FORMATS = generic catgirl irc textual
 
 dev: tags all test
 
 all: ${BINS}
 
 litterbox: ${OBJS.litterbox}
-	${CC} ${LDFLAGS} ${OBJS.$@} ${LDLIBS} -o $@
+
+scoop: ${OBJS.scoop}
+
+unscoop: ${OBJS.unscoop}
 
 .o:
-	${CC} ${LDFLAGS} $< ${LDLIBS} -o $@
+	${CC} ${LDFLAGS} ${OBJS.$@} ${LDLIBS} -o $@
 
 ${BINS:=.o}: database.h
 
@@ -41,7 +47,7 @@ tags: *.c *.h
 	ctags -w *.c *.h
 
 clean:
-	rm -f .test tags ${BINS} ${RCS} ${OBJS.litterbox} ${BINS:=.o}
+	rm -f .test tags ${BINS} ${RCS} ${BINS:=.o} config.o xdg.o
 
 install: ${BINS} ${MANS} ${INSTALLS}
 	install -d ${DESTDIR}${PREFIX}/bin ${DESTDIR}${MANDIR}/man1
diff --git a/database.h b/database.h
index d2db1f0..9fc1fc8 100644
--- a/database.h
+++ b/database.h
@@ -55,7 +55,7 @@ int getopt_config(
 	const struct option *longopts, int *longindex
 );
 
-#define DATABASE_PATH "litterbox/litterbox.sqlite"
+static const char *DatabasePath = "litterbox.sqlite";
 
 enum { DatabaseVersion = 5 };
 
@@ -97,15 +97,7 @@ static inline void dbExec(const char *sql) {
 	if (error) errx(EX_SOFTWARE, "%s: %s", sqlite3_errmsg(db), sql);
 }
 
-static inline void dbOpen(char *path, int flags) {
-	char *base = strrchr(path, '/');
-	if (flags & SQLITE_OPEN_CREATE && base) {
-		*base = '\0';
-		int error = mkdir(path, 0700);
-		if (error && errno != EEXIST) err(EX_CANTCREAT, "%s", path);
-		*base = '/';
-	}
-
+static inline void dbOpen(const char *path, int flags) {
 	int error = sqlite3_open_v2(path, &db, flags, NULL);
 	if (error == SQLITE_CANTOPEN) {
 		sqlite3_close(db);
@@ -119,35 +111,22 @@ static inline void dbOpen(char *path, int flags) {
 	dbExec(SQL(PRAGMA foreign_keys = true;));
 }
 
-static inline void dbFind(char *path, int flags) {
+static inline void dbFind(const char *path, int flags) {
 	if (path) {
 		dbOpen(path, flags);
 		if (db) return;
 		errx(EX_NOINPUT, "%s: database not found", path);
 	}
 
-	const char *home = getenv("HOME");
-	const char *dataHome = getenv("XDG_DATA_HOME");
-	const char *dataDirs = getenv("XDG_DATA_DIRS");
+	if (flags & SQLITE_OPEN_CREATE) {
+		dataMkdir("");
+	}
 
 	char buf[PATH_MAX];
-	if (dataHome) {
-		snprintf(buf, sizeof(buf), "%s/" DATABASE_PATH, dataHome);
-	} else {
-		if (!home) errx(EX_CONFIG, "HOME unset");
-		snprintf(buf, sizeof(buf), "%s/.local/share/" DATABASE_PATH, home);
-	}
-	dbOpen(buf, flags);
-	if (db) return;
-
-	if (!dataDirs) dataDirs = "/usr/local/share:/usr/share";
-	while (*dataDirs) {
-		size_t len = strcspn(dataDirs, ":");
-		snprintf(buf, sizeof(buf), "%.*s/" DATABASE_PATH, (int)len, dataDirs);
-		dbOpen(buf, flags);
+	const char *dirs = NULL;
+	while (NULL != (path = dataPath(buf, sizeof(buf), &dirs, DatabasePath))) {
+		dbOpen(path, flags);
 		if (db) return;
-		dataDirs += len;
-		if (*dataDirs) dataDirs++;
 	}
 	errx(EX_NOINPUT, "database not found; initialize it with litterbox -i");
 }
diff --git a/litterbox.c b/litterbox.c
index ffd838c..1546cd8 100644
--- a/litterbox.c
+++ b/litterbox.c
@@ -743,9 +743,9 @@ static void quit(int sig) {
 }
 
 int main(int argc, char *argv[]) {
-	char *path = NULL;
 	bool init = false;
 	bool migrate = false;
+	const char *path = NULL;
 	const char *backup = NULL;
 
 	bool insecure = false;
diff --git a/scoop.c b/scoop.c
index 5f15ed6..89f0d84 100644
--- a/scoop.c
+++ b/scoop.c
@@ -387,8 +387,8 @@ static enum Type parseType(const char *input) {
 int main(int argc, char *argv[]) {
 	bool tty = isatty(STDOUT_FILENO);
 
-	char *path = NULL;
 	bool shell = false;
+	const char *path = NULL;
 	Format *format = (tty ? formatColor : formatPlain);
 
 	bool sort = false;
diff --git a/unscoop.c b/unscoop.c
index c9e68f3..6638774 100644
--- a/unscoop.c
+++ b/unscoop.c
@@ -329,8 +329,8 @@ static void dedupEvents(sqlite3 *db) {
 
 int main(int argc, char *argv[]) {
 	bool test = false;
-	char *path = NULL;
 	bool dedup = false;
+	const char *path = NULL;
 	const char *network = NULL;
 	const char *context = NULL;
 	const struct Format *format = &Formats[0];