diff options
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 |