[Frugalware-git] fwsetup-ng: add function for recursing through like mkdir -p

James Buren ryuo at frugalware.org
Wed Aug 29 07:29:58 CEST 2012


Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=fwsetup-ng.git;a=commitdiff;h=4c45029937e0b0aac205675b697e85cd3731157d

commit 4c45029937e0b0aac205675b697e85cd3731157d
Author: James Buren <ryuo at frugalware.org>
Date:   Wed Aug 29 00:29:48 2012 -0500

add function for recursing through like mkdir -p

diff --git a/src/local.h b/src/local.h
index a8564d5..23174fb 100644
--- a/src/local.h
+++ b/src/local.h
@@ -5,11 +5,15 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <limits.h>

#define _(S) S
#define NEWT_WIDTH  70
#define NEWT_HEIGHT 21

+extern bool mkdir_recurse(const char *path);
extern int ui_main(int argc,char **argv);
extern FILE *logfile;
extern int main(int argc,char **argv);
diff --git a/src/utility.c b/src/utility.c
new file mode 100644
index 0000000..20a7dd8
--- /dev/null
+++ b/src/utility.c
@@ -0,0 +1,39 @@
+#include "local.h"
+
+extern bool mkdir_recurse(const char *path)
+{
+  char buf[PATH_MAX] = {0};
+  char *s = buf;
+
+  if(path == 0)
+  {
+    errno = EINVAL;
+    fprintf(logfile,"%s: %s\n",__func__,strerror(errno));
+    return false;
+  }
+
+  snprintf(buf,PATH_MAX,"%s",path);
+
+  while((s = strchr(s,'/')) != 0)
+  {
+    *s = 0;
+
+    if(mkdir(buf,0755) == -1 && errno != EEXIST)
+    {
+      fprintf(logfile,"%s: %s\n",__func__,strerror(errno));
+      return false;
+    }
+
+    *s = '/';
+
+    ++s;
+  }
+
+  if(mkdir(buf,0755) == -1 && errno != EEXIST)
+  {
+    fprintf(logfile,"%s: %s\n",__func__,strerror(errno));
+    return false;
+  }
+
+  return true;
+}


More information about the Frugalware-git mailing list