summaryrefslogtreecommitdiffstats
path: root/bio/vaje/1/programi/mkdirp.c
diff options
context:
space:
mode:
Diffstat (limited to 'bio/vaje/1/programi/mkdirp.c')
-rw-r--r--bio/vaje/1/programi/mkdirp.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/bio/vaje/1/programi/mkdirp.c b/bio/vaje/1/programi/mkdirp.c
new file mode 100644
index 0000000..a6c58e6
--- /dev/null
+++ b/bio/vaje/1/programi/mkdirp.c
@@ -0,0 +1,43 @@
+// borrowed from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
+#pragma once
+#include <string.h>
+#include <limits.h> /* PATH_MAX */
+#include <sys/stat.h> /* mkdir(2) */
+#include <errno.h>
+int mkdir_p(const char *path) {
+ /* Adapted from http://stackoverflow.com/a/2336245/119527 */
+ const size_t len = strlen(path);
+ char _path[PATH_MAX];
+ char *p;
+
+ errno = 0;
+
+ /* Copy string so its mutable */
+ if (len > sizeof(_path)-1) {
+ errno = ENAMETOOLONG;
+ return -1;
+ }
+ strcpy(_path, path);
+
+ /* Iterate the string */
+ for (p = _path + 1; *p; p++) {
+ if (*p == '/') {
+ /* Temporarily truncate */
+ *p = '\0';
+
+ if (mkdir(_path, S_IRWXU) != 0) {
+ if (errno != EEXIST)
+ return -1;
+ }
+
+ *p = '/';
+ }
+ }
+
+ if (mkdir(_path, S_IRWXU) != 0) {
+ if (errno != EEXIST)
+ return -1;
+ }
+
+ return 0;
+}