commit faf4f4295234ab591507c2782b40efbffb74b5c0
parent 09854b708feef2a61cb9b56197c03d4b4a1e55fc
Author: sin <sin@2f30.org>
Date: Wed, 21 Aug 2013 09:29:53 +0100
Add mkpath() - a recursive mkdir() implementation
The code was taken from ii[1].
[1] http://git.suckless.org/ii
Diffstat:
4 files changed, 34 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
@@ -9,6 +9,7 @@ LIB = \
util/dev.o \
util/eprintf.o \
util/estrtol.o \
+ util/mkpath.o \
util/recurse.o
SRC = smdev.c
diff --git a/mkpath.h b/mkpath.h
@@ -0,0 +1,2 @@
+/* See LICENSE file for copyright and license details. */
+int mkpath(const char *path, mode_t mode);
diff --git a/smdev.c b/smdev.c
@@ -12,6 +12,7 @@
#include <limits.h>
#include <regex.h>
#include "config.h"
+#include "mkpath.h"
#include "util.h"
static int create_dev(const char *path);
@@ -117,7 +118,7 @@ create_dev(const char *path)
case '=':
if (Rules[i].path[strlen(Rules[i].path) - 1] == '/') {
snprintf(devpath, sizeof(devpath), "/dev/%s", &Rules[i].path[1]);
- if (mkdir(devpath, 0755) < 0)
+ if (mkpath(devpath, 0755) < 0)
eprintf("mkdir %s:", devpath);
strcat(devpath, devname);
} else {
diff --git a/util/mkpath.c b/util/mkpath.c
@@ -0,0 +1,29 @@
+#include <sys/stat.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <limits.h>
+
+int
+mkpath(const char *path, mode_t mode)
+{
+ char tmp[PATH_MAX];
+ char *p = NULL;
+ size_t len;
+
+ snprintf(tmp, sizeof(tmp),"%s",path);
+ len = strlen(tmp);
+ if(tmp[len - 1] == '/')
+ tmp[len - 1] = 0;
+ for(p = tmp + 1; *p; p++)
+ if(*p == '/') {
+ *p = 0;
+ if (mkdir(tmp, mode) < 0 && errno != EEXIST)
+ return -1;
+ *p = '/';
+ }
+ if (mkdir(tmp, mode) < 0 && errno != EEXIST)
+ return -1;
+ return 0;
+}
+