Move mkdirp() to libutil

This commit is contained in:
sin 2015-04-20 18:02:11 +01:00
parent 31af8555a7
commit b9d60bee87
5 changed files with 32 additions and 46 deletions

View File

@ -54,6 +54,7 @@ LIBUTILSRC =\
libutil/getlines.c\ libutil/getlines.c\
libutil/human.c\ libutil/human.c\
libutil/md5.c\ libutil/md5.c\
libutil/mkdirp.c\
libutil/mode.c\ libutil/mode.c\
libutil/putword.c\ libutil/putword.c\
libutil/reallocarray.c\ libutil/reallocarray.c\

30
libutil/mkdirp.c Normal file
View File

@ -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;
}

23
mkdir.c
View File

@ -6,29 +6,6 @@
#include "util.h" #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 static void
usage(void) usage(void)
{ {

23
tar.c
View File

@ -173,29 +173,6 @@ archive(const char *path)
return 0; return 0;
} }
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 static int
unarchive(char *fname, ssize_t l, char b[BLKSIZ]) unarchive(char *fname, ssize_t l, char b[BLKSIZ])
{ {

1
util.h
View File

@ -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 enstrtonum(int, const char *, long long, long long);
long long estrtonum(const char *, long long, long long); long long estrtonum(const char *, long long, long long);
size_t unescape(char *); size_t unescape(char *);
int mkdirp(const char *);