diff options
author | Erik K <erikk@previousplan.org> | 2022-05-26 16:31:27 +0000 |
---|---|---|
committer | Erik K <erikk@previousplan.org> | 2022-05-26 16:31:27 +0000 |
commit | 61d75021c8a1180d2b591e70c783bd2eb21188bd (patch) | |
tree | e5401f7c66da4b41eb1bc7f01a8e98a38f8ceae8 /util.c | |
parent | 9643e3cb444ee26088be4636ba2b753ac0166156 (diff) |
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -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 |