diff src/mkpath.c @ 124:9c7bc6c0327e

Add DLNA server function test. (from uShare project)
author naoyan@johnstown.minaminoshima.org
date Wed, 29 Sep 2010 23:18:55 +0900
parents recpt1/mkpath.c@f1553492e8bb
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/mkpath.c	Wed Sep 29 23:18:55 2010 +0900
@@ -0,0 +1,54 @@
+/* mkpath is originally written by Jonathan Leffler.
+ * see "http://stackoverflow.com/questions/675039/how-can-i-create-directory-tree-in-c-linux" for detail.
+ * copyright: (C) JLSS 1990-91,1997-98,2001,2005,2008
+ */
+
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+static int
+do_mkdir(const char *path, mode_t mode)
+{
+    struct stat st;
+    int status = 0;
+
+    if (stat(path, &st) != 0) {
+        /* Directory does not exist */
+        if (mkdir(path, mode) != 0)
+            status = -1;
+    }
+    else if (!S_ISDIR(st.st_mode)) {
+        errno = ENOTDIR;
+        status = -1;
+    }
+
+    return(status);
+}
+
+int
+mkpath(const char *path, mode_t mode)
+{
+    char *pp;
+    char *sp;
+    int status;
+    char *copypath = strdup(path);
+
+    status = 0;
+    pp = copypath;
+    while (status == 0 && (sp = strchr(pp, '/')) != 0) {
+        if (sp != pp) {
+            /* Neither root nor double slash in path */
+            *sp = '\0';
+            status = do_mkdir(copypath, mode);
+            *sp = '/';
+        }
+        pp = sp + 1;
+    }
+    if (status == 0)
+        status = do_mkdir(path, mode);
+    free(copypath);
+    return (status);
+}