sbase

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

commit 58ed21f49d777924be808f8f53d70415060fa14a
parent 655df20ebeda0a0131c932d9a1c8d48569e97c69
Author: sin <sin@2f30.org>
Date:   Mon Apr 20 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:
mkdir.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; }