summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorBert <ber.t@gmx.com>2011-04-07 17:29:59 +0200
committerBert <ber.t@gmx.com>2011-04-07 17:29:59 +0200
commit92709b2b2f579300b1c007e5a3ba869e78fcc922 (patch)
tree52b7f876bb19d342eeee01d0a1fb3dcb22742005 /util.c
parentf52a99db6c1f0a659934a50ae4a4ef8085ee3752 (diff)
downloadnsxiv-92709b2b2f579300b1c007e5a3ba869e78fcc922.tar.zst
Use directory structure in cache dir
Diffstat (limited to 'util.c')
-rw-r--r--util.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/util.c b/util.c
index 517b4ba..42d1384 100644
--- a/util.c
+++ b/util.c
@@ -18,6 +18,8 @@
#include <stdlib.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
@@ -156,6 +158,47 @@ end:
return path;
}
+int create_dir_rec(const char *path) {
+ char *dir, *d;
+ struct stat stats;
+ int err = 0;
+
+ if (!path || !*path)
+ return -1;
+
+ if (!stat(path, &stats)) {
+ if (S_ISDIR(stats.st_mode)) {
+ return 0;
+ } else {
+ warn("not a directory: %s", path);
+ return -1;
+ }
+ }
+
+ d = dir = (char*) s_malloc(strlen(path) + 1);
+ strcpy(dir, path);
+
+ while (d != NULL && !err) {
+ d = strchr(d + 1, '/');
+ if (d != NULL)
+ *d = '\0';
+ if (access(dir, F_OK) && errno == ENOENT) {
+ if (mkdir(dir, 0755)) {
+ warn("could not create directory: %s", dir);
+ err = -1;
+ }
+ } else if (stat(dir, &stats) || !S_ISDIR(stats.st_mode)) {
+ warn("not a directory: %s", dir);
+ err = -1;
+ }
+ if (d != NULL)
+ *d = '/';
+ }
+ free(dir);
+
+ return err;
+}
+
char* readline(FILE *stream) {
size_t len;
char *buf, *s, *end;