about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-23 21:53:24 -0500
committerJune McEnroe <june@causal.agency>2019-12-23 21:53:24 -0500
commit7fd758a4a9b99d79a4c490257f16b2fce506b21c (patch)
treee79c8d658771001e6d81c1283a0874287575d179
parentFactour out verbose, dbStep+reset (diff)
downloadlitterbox-7fd758a4a9b99d79a4c490257f16b2fce506b21c.tar.gz
litterbox-7fd758a4a9b99d79a4c490257f16b2fce506b21c.zip
Take optional explicit path in dbFind
-rw-r--r--database.h16
-rw-r--r--litterbox.c2
-rw-r--r--unscoop.c4
3 files changed, 11 insertions, 11 deletions
diff --git a/database.h b/database.h
index 1dcc71b..1d5ef1c 100644
--- a/database.h
+++ b/database.h
@@ -74,26 +74,28 @@ static inline sqlite3 *dbOpen(char *path, int flags) {
 	return db;
 }
 
-static inline sqlite3 *dbFind(int flags) {
+static inline sqlite3 *dbFind(char *path, int flags) {
+	if (path) return dbOpen(path, flags);
+
 	const char *home = getenv("HOME");
 	const char *dataHome = getenv("XDG_DATA_HOME");
 	const char *dataDirs = getenv("XDG_DATA_DIRS");
 
-	char path[PATH_MAX];
+	char buf[PATH_MAX];
 	if (dataHome) {
-		snprintf(path, sizeof(path), "%s/" DATABASE_PATH, dataHome);
+		snprintf(buf, sizeof(buf), "%s/" DATABASE_PATH, dataHome);
 	} else {
 		if (!home) errx(EX_CONFIG, "HOME unset");
-		snprintf(path, sizeof(path), "%s/.local/share/" DATABASE_PATH, home);
+		snprintf(buf, sizeof(buf), "%s/.local/share/" DATABASE_PATH, home);
 	}
-	sqlite3 *db = dbOpen(path, flags);
+	sqlite3 *db = dbOpen(buf, flags);
 	if (db) return db;
 
 	if (!dataDirs) dataDirs = "/usr/local/share:/usr/share";
 	while (*dataDirs) {
 		size_t len = strcspn(dataDirs, ":");
-		snprintf(path, sizeof(path), "%.*s/" DATABASE_PATH, (int)len, dataDirs);
-		db = dbOpen(path, flags);
+		snprintf(buf, sizeof(buf), "%.*s/" DATABASE_PATH, (int)len, dataDirs);
+		db = dbOpen(buf, flags);
 		if (db) return db;
 		dataDirs += len;
 		if (*dataDirs) dataDirs++;
diff --git a/litterbox.c b/litterbox.c
index 178f790..8015cec 100644
--- a/litterbox.c
+++ b/litterbox.c
@@ -410,7 +410,7 @@ int main(int argc, char *argv[]) {
 	int flags = SQLITE_OPEN_READWRITE;
 	if (init) flags |= SQLITE_OPEN_CREATE;
 
-	db = (path ? dbOpen(path, flags) : dbFind(flags));
+	db = dbFind(path, flags);
 	if (!db) errx(EX_NOINPUT, "database not found");
 
 	if (init) {
diff --git a/unscoop.c b/unscoop.c
index 996b929..0b15430 100644
--- a/unscoop.c
+++ b/unscoop.c
@@ -317,10 +317,8 @@ int main(int argc, char *argv[]) {
 		}
 	}
 
-	int flags = SQLITE_OPEN_READWRITE;
-	sqlite3 *db = (path ? dbOpen(path, flags) : dbFind(flags));
+	sqlite3 *db = dbFind(path, SQLITE_OPEN_READWRITE);
 	if (!db) errx(EX_NOINPUT, "database not found");
-
 	if (dbVersion(db) != DatabaseVersion) {
 		errx(EX_CONFIG, "database out of date; migrate with litterbox -m");
 	}