sbase

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

commit d6885987e5f8fabd486d94d649206d0c34fc5ef0
parent 684b6fe8f25b86d8a8e7d208b639385c6fa5639b
Author: sin <sin@2f30.org>
Date:   Mon, 20 Apr 2015 16:12:36 +0100

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.

Diffstat:
Mmkdir.c | 21+++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/mkdir.c b/mkdir.c @@ -11,17 +11,21 @@ mkdirp(char *path) { char *p; - for (p = path + (*path == '/'); *p; 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; } @@ -54,14 +58,15 @@ main(int argc, char *argv[]) usage(); for (; *argv; argc--, argv++) { - if (pflag && mkdirp(*argv) < 0) { - ret = 1; - } else if (mkdir(*argv, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { - if (!(pflag && errno == EEXIST)) { + 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; - } - } else if (mflag && chmod(*argv, mode) < 0) { + } + if (mflag && chmod(*argv, mode) < 0) { weprintf("chmod %s:", *argv); ret = 1; }