summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorErik K <erikk@previousplan.org>2022-05-26 16:31:27 +0000
committerErik K <erikk@previousplan.org>2022-05-26 16:31:27 +0000
commit61d75021c8a1180d2b591e70c783bd2eb21188bd (patch)
treee5401f7c66da4b41eb1bc7f01a8e98a38f8ceae8 /util.c
parent9643e3cb444ee26088be4636ba2b753ac0166156 (diff)
mkdir parent directories when needed, also fixed bug in list().HEADmaster
Diffstat (limited to 'util.c')
-rw-r--r--util.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/util.c b/util.c
index 3f4f51d..50a7116 100644
--- a/util.c
+++ b/util.c
@@ -1,3 +1,5 @@
+#include <sys/stat.h>
+
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@@ -65,6 +67,28 @@ xrealloc(void *ptr, size_t size)
return n;
}
+int
+mkparentdirs(const char *path)
+{
+ char part[512];
+ int i;
+
+ if (!path[0])
+ return 0;
+ my_strlcpy(part, path, sizeof(part));
+ for (i = 1; part[i]; i++) {
+ if (part[i] == '/' && part[i + 1] != '/' && part[i + 1]) {
+ part[i] = '\0';
+ if (mkdir(part, 0755) < 0 && errno != EEXIST) {
+ fprintf(stderr, "mkdir %s: %s\n", part, strerror(errno));
+ return 0;
+ }
+ part[i] = '/';
+ }
+ }
+ return 1;
+}
+
/* strlcpy is unportable, and strncpy is a mess. So we define our own strlcpy
* instead. */
size_t