[hackers] [sbase] Move mkdirp() to libutil || sin

From: <git_AT_suckless.org>
Date: Mon, 20 Apr 2015 19:04:12 +0200 (CEST)

commit b9d60bee879ddf787fe168d19f6ed1952c6edc33
Author: sin <sin_AT_2f30.org>
Date: Mon Apr 20 18:02:11 2015 +0100

    Move mkdirp() to libutil

diff --git a/Makefile b/Makefile
index ffa8678..27ce5ef 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -54,6 +54,7 @@ LIBUTILSRC =\
         libutil/getlines.c\
         libutil/human.c\
         libutil/md5.c\
+ libutil/mkdirp.c\
         libutil/mode.c\
         libutil/putword.c\
         libutil/reallocarray.c\
diff --git a/libutil/mkdirp.c b/libutil/mkdirp.c
new file mode 100644
index 0000000..7796e24
--- /dev/null
+++ b/libutil/mkdirp.c
_AT_@ -0,0 +1,30 @@
+/* See LICENSE file for copyright and license details. */
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <limits.h>
+
+#include "../util.h"
+
+int
+mkdirp(const char *path)
+{
+ char tmp[PATH_MAX], *p;
+
+ estrlcpy(tmp, path, sizeof(tmp));
+ for (p = tmp + (tmp[0] == '/'); *p; p++) {
+ if (*p != '/')
+ continue;
+ *p = '\0';
+ if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) {
+ weprintf("mkdir %s:", tmp);
+ return -1;
+ }
+ *p = '/';
+ }
+ if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) {
+ weprintf("mkdir %s:", tmp);
+ return -1;
+ }
+ return 0;
+}
diff --git a/mkdir.c b/mkdir.c
index 9d4951a..6de0d1a 100644
--- a/mkdir.c
+++ b/mkdir.c
_AT_@ -6,29 +6,6 @@
 
 #include "util.h"
 
-static int
-mkdirp(char *path)
-{
- char *p;
-
- for (p = path + (*path == '/'); *p; p++) {
- if (*p != '/')
- continue;
- *p = '\0';
- if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) {
- weprintf("mkdir %s:", path);
- *p = '/';
- return -1;
- }
- *p = '/';
- }
- if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) {
- weprintf("mkdir %s:", path);
- return -1;
- }
- return 0;
-}
-
 static void
 usage(void)
 {
diff --git a/tar.c b/tar.c
index a115325..06fb654 100644
--- a/tar.c
+++ b/tar.c
_AT_@ -174,29 +174,6 @@ archive(const char *path)
 }
 
 static int
-mkdirp(char *path)
-{
- char *p;
-
- for (p = path; *p; p++) {
- if (*p != '/')
- continue;
- *p = '\0';
- if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) {
- weprintf("mkdir %s:", path);
- *p = '/';
- return -1;
- }
- *p = '/';
- }
- if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) {
- weprintf("mkdir %s:", path);
- return -1;
- }
- return 0;
-}
-
-static int
 unarchive(char *fname, ssize_t l, char b[BLKSIZ])
 {
         FILE *f = NULL;
diff --git a/util.h b/util.h
index a878d73..7dd5713 100644
--- a/util.h
+++ b/util.h
_AT_@ -74,3 +74,4 @@ long long strtonum(const char *, long long, long long, const char **);
 long long enstrtonum(int, const char *, long long, long long);
 long long estrtonum(const char *, long long, long long);
 size_t unescape(char *);
+int mkdirp(const char *);
Received on Mon Apr 20 2015 - 19:04:12 CEST

This archive was generated by hypermail 2.3.0 : Mon Apr 20 2015 - 19:12:22 CEST