aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makemodule.am2
-rw-r--r--lib/util/mkdir_p.c42
2 files changed, 43 insertions, 1 deletions
diff --git a/lib/Makemodule.am b/lib/Makemodule.am
index f23d9b7..691d6df 100644
--- a/lib/Makemodule.am
+++ b/lib/Makemodule.am
@@ -19,7 +19,7 @@ libsquashfs_a_SOURCES += include/frag_reader.h
libutil_a_SOURCES = lib/util/canonicalize_name.c lib/util/write_retry.c
libutil_a_SOURCES += lib/util/read_retry.c include/util.h
-libutil_a_SOURCES += lib/util/print_version.c
+libutil_a_SOURCES += lib/util/print_version.c lib/util/mkdir_p.c
if WITH_ZLIB
libcompress_a_SOURCES += lib/comp/zlib.c
diff --git a/lib/util/mkdir_p.c b/lib/util/mkdir_p.c
new file mode 100644
index 0000000..7d1e052
--- /dev/null
+++ b/lib/util/mkdir_p.c
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <alloca.h>
+#include <stdio.h>
+#include <errno.h>
+
+#include "util.h"
+
+int mkdir_p(const char *path)
+{
+ size_t i, len;
+ char *buffer;
+
+ while (path[0] == '/' && path[1] == '/')
+ ++path;
+
+ if (*path == '\0' || (path[0] == '/' && path[1] == '\0'))
+ return 0;
+
+ len = strlen(path) + 1;
+ buffer = alloca(len);
+
+ for (i = 0; i < len; ++i) {
+ if (i > 0 && (path[i] == '/' || path[i] == '\0')) {
+ buffer[i] = '\0';
+
+ if (mkdir(buffer, 0755) != 0) {
+ if (errno != EEXIST) {
+ fprintf(stderr, "mkdir %s: %s\n",
+ buffer, strerror(errno));
+ return -1;
+ }
+ }
+ }
+
+ buffer[i] = path[i];
+ }
+
+ return 0;
+}