diff options
author | June McEnroe <june@causal.agency> | 2020-02-10 19:57:10 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-02-10 19:57:10 -0500 |
commit | 99480a42e56e70707822934ffeb56f0454afc127 (patch) | |
tree | 798c7d1bc8193dccc745682cb4e76d60610fb2ed /ui.c | |
parent | Leave a blank line after loaded buffer (diff) | |
download | catgirl-99480a42e56e70707822934ffeb56f0454afc127.tar.gz catgirl-99480a42e56e70707822934ffeb56f0454afc127.zip |
Factor out XDG base directory code
And add warnings to configOpen, since that's the only way to be accurate if a weird error occurs.
Diffstat (limited to '')
-rw-r--r-- | ui.c | 67 |
1 files changed, 0 insertions, 67 deletions
diff --git a/ui.c b/ui.c index ecf7e60..9601aaa 100644 --- a/ui.c +++ b/ui.c @@ -21,13 +21,11 @@ #include <curses.h> #include <err.h> #include <errno.h> -#include <limits.h> #include <stdarg.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <sys/stat.h> #include <sysexits.h> #include <term.h> #include <termios.h> @@ -858,71 +856,6 @@ void uiRead(void) { inputUpdate(); } -static FILE *dataOpen(const char *path, const char *mode) { - if (path[0] == '/' || path[0] == '.') goto local; - - const char *home = getenv("HOME"); - const char *dataHome = getenv("XDG_DATA_HOME"); - const char *dataDirs = getenv("XDG_DATA_DIRS"); - - char homePath[PATH_MAX]; - if (dataHome) { - snprintf( - homePath, sizeof(homePath), - "%s/" XDG_SUBDIR "/%s", dataHome, path - ); - } else { - if (!home) goto local; - snprintf( - homePath, sizeof(homePath), - "%s/.local/share/" XDG_SUBDIR "/%s", home, path - ); - } - FILE *file = fopen(homePath, mode); - if (file) return file; - if (errno != ENOENT) { - warn("%s", homePath); - return NULL; - } - - char buf[PATH_MAX]; - if (!dataDirs) dataDirs = "/usr/local/share:/usr/share"; - while (*dataDirs) { - size_t len = strcspn(dataDirs, ":"); - snprintf( - buf, sizeof(buf), "%.*s/" XDG_SUBDIR "/%s", - (int)len, dataDirs, path - ); - file = fopen(buf, mode); - if (file) return file; - if (errno != ENOENT) { - warn("%s", buf); - return NULL; - } - dataDirs += len; - if (*dataDirs) dataDirs++; - } - - if (mode[0] != 'r') { - char *base = strrchr(homePath, '/'); - *base = '\0'; - int error = mkdir(homePath, S_IRWXU); - if (error && errno != EEXIST) { - warn("%s", homePath); - return NULL; - } - *base = '/'; - file = fopen(homePath, mode); - if (!file) warn("%s", homePath); - return file; - } - -local: - file = fopen(path, mode); - if (!file) warn("%s", path); - return file; -} - static const size_t Signatures[] = { 0x6C72696774616301, }; |