Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=fw32.git;a=commitdiff;h=d3d2cc8b07036985021ea55812a45d7f306cff01

commit d3d2cc8b07036985021ea55812a45d7f306cff01
Author: James Buren <r...@frugalware.org>
Date:   Wed Nov 2 03:35:04 2011 -0500

fw32.c: add mkdir_parents

diff --git a/fw32.c b/fw32.c
index 7a2e9b2..0fb53fd 100644
--- a/fw32.c
+++ b/fw32.c
@@ -1,18 +1,68 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <limits.h>
+#include <assert.h>

static void
error(const char *fmt,...)
{
-       va_list args;
+  va_list args;

-       va_start(args,fmt);
+  assert(fmt);

-       vfprintf(stderr,fmt,args);
+  va_start(args,fmt);

-       va_end(args);
+  vfprintf(stderr,fmt,args);

-       exit(EXIT_FAILURE);
+  va_end(args);
+
+  exit(EXIT_FAILURE);
}

+static void
+mkdir_parents(const char *s)
+{
+  char path[PATH_MAX], *p;
+  struct stat st;
+
+  assert(s && *s == '/');
+
+  snprintf(path,sizeof path,"%s",s);
+
+  for( p = strchr(path + 1,'/') ; p && *p ; p = strchr(p + 1,'/') )
+  {
+    *p = 0;
+
+    if(!stat(path,&st))
+    {
+      if(S_ISDIR(st.st_mode))
+      {
+        *p = '/';
+
+        continue;
+      }
+
+      error("Parent directory exists and is not a directory: %s\n",path);
+    }
+
+    if(mkdir(path,0755))
+      error("Failed to create parent directory: %s\n",path);
+
+    *p = '/';
+  }
+
+  if(!stat(path,&st))
+  {
+    if(S_ISDIR(st.st_mode))
+      return;
+
+    error("Directory exists and is not a directory: %s\n",path);
+  }
+
+  if(mkdir(path,0755))
+    error("Failed to create directory: %s\n",path);
+}
_______________________________________________
Frugalware-git mailing list
Frugalware-git@frugalware.org
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to