sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit b9d60bee879ddf787fe168d19f6ed1952c6edc33
parent 31af8555a70b54395565487019d3985b796e3f05
Author: sin <sin@2f30.org>
Date:   Mon, 20 Apr 2015 18:02:11 +0100

Move mkdirp() to libutil

Diffstat:
MMakefile | 1+
Alibutil/mkdirp.c | 30++++++++++++++++++++++++++++++
Mmkdir.c | 23-----------------------
Mtar.c | 23-----------------------
Mutil.h | 1+
5 files changed, 32 insertions(+), 46 deletions(-)

diff --git a/Makefile b/Makefile @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 *);