sbase/mkdir.c
sin d6885987e5 Fix mkdir -p and allow -m to be specified along with -p
The mkdirp() function would not create the entire path but it just
happened to work because of the extra mkdir in the main loop.
2015-04-20 16:36:02 +01:00

77 lines
1.2 KiB
C

/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include "util.h"
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 void
usage(void)
{
eprintf("usage: %s [-p] [-m mode] name ...\n", argv0);
}
int
main(int argc, char *argv[])
{
mode_t mode = 0, mask;
int pflag = 0, mflag = 0, ret = 0;
ARGBEGIN {
case 'p':
pflag = 1;
break;
case 'm':
mflag = 1;
mask = getumask();
mode = parsemode(EARGF(usage()), mode, mask);
break;
default:
usage();
} ARGEND;
if (!argc)
usage();
for (; *argv; argc--, argv++) {
if (pflag) {
if (mkdirp(*argv) < 0)
ret = 1;
} else if (mkdir(*argv, S_IRWXU | S_IRWXG | S_IRWXO) < 0 &&
errno != EEXIST) {
weprintf("mkdir %s:", *argv);
ret = 1;
}
if (mflag && chmod(*argv, mode) < 0) {
weprintf("chmod %s:", *argv);
ret = 1;
}
}
return ret;
}