From 02b0e2c95fc083808af3723b2055ac9fb6968891 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sun, 1 Dec 2019 21:05:46 -0500 Subject: Rename header file to database.h --- Makefile | 2 +- database.h | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ litterbox.c | 2 +- litterbox.h | 72 ------------------------------------------------------------- 4 files changed, 74 insertions(+), 74 deletions(-) create mode 100644 database.h delete mode 100644 litterbox.h diff --git a/Makefile b/Makefile index 573290a..db31ae2 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ dev: tags all all: ${BINS} -${BINS:=.o}: litterbox.h +${BINS:=.o}: database.h tags: *.c *.h ctags -w *.c *.h diff --git a/database.h b/database.h new file mode 100644 index 0000000..5a9a4f6 --- /dev/null +++ b/database.h @@ -0,0 +1,72 @@ +/* Copyright (C) 2019 C. McEnroe + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DATABASE_PATH "litterbox/litterbox.sqlite" + +static inline sqlite3 *dbOpenPath(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 = '/'; + } + sqlite3 *db; + int error = sqlite3_open_v2(path, &db, flags, NULL); + if (!error) return db; + if (error == SQLITE_CANTOPEN) { + sqlite3_close(db); + return NULL; + } + errx(EX_NOINPUT, "%s: %s", path, sqlite3_errmsg(db)); +} + +static inline sqlite3 *dbOpen(int flags) { + const char *home = getenv("HOME"); + const char *dataHome = getenv("XDG_DATA_HOME"); + const char *dataDirs = getenv("XDG_DATA_DIRS"); + + char path[PATH_MAX]; + if (dataHome) { + snprintf(path, sizeof(path), "%s/" DATABASE_PATH, dataHome); + } else { + if (!home) errx(EX_CONFIG, "HOME unset"); + snprintf(path, sizeof(path), "%s/.local/share/" DATABASE_PATH, home); + } + sqlite3 *db = dbOpenPath(path, 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 = dbOpenPath(path, flags); + if (db) return db; + dataDirs += len; + if (*dataDirs) dataDirs++; + } + return NULL; +} diff --git a/litterbox.c b/litterbox.c index bcfdd8f..8264d95 100644 --- a/litterbox.c +++ b/litterbox.c @@ -20,7 +20,7 @@ #include #include -#include "litterbox.h" +#include "database.h" int main(void) { sqlite3 *db = dbOpen(SQLITE_OPEN_READWRITE); diff --git a/litterbox.h b/litterbox.h deleted file mode 100644 index 5a9a4f6..0000000 --- a/litterbox.h +++ /dev/null @@ -1,72 +0,0 @@ -/* Copyright (C) 2019 C. McEnroe - * - * 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define DATABASE_PATH "litterbox/litterbox.sqlite" - -static inline sqlite3 *dbOpenPath(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 = '/'; - } - sqlite3 *db; - int error = sqlite3_open_v2(path, &db, flags, NULL); - if (!error) return db; - if (error == SQLITE_CANTOPEN) { - sqlite3_close(db); - return NULL; - } - errx(EX_NOINPUT, "%s: %s", path, sqlite3_errmsg(db)); -} - -static inline sqlite3 *dbOpen(int flags) { - const char *home = getenv("HOME"); - const char *dataHome = getenv("XDG_DATA_HOME"); - const char *dataDirs = getenv("XDG_DATA_DIRS"); - - char path[PATH_MAX]; - if (dataHome) { - snprintf(path, sizeof(path), "%s/" DATABASE_PATH, dataHome); - } else { - if (!home) errx(EX_CONFIG, "HOME unset"); - snprintf(path, sizeof(path), "%s/.local/share/" DATABASE_PATH, home); - } - sqlite3 *db = dbOpenPath(path, 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 = dbOpenPath(path, flags); - if (db) return db; - dataDirs += len; - if (*dataDirs) dataDirs++; - } - return NULL; -} -- cgit 1.4.1