sbase

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

commit d9fa4b3ae7af2f5e0a3cc66ba90265afe720139c
parent cb04864692157c23b7d8adc1de82a8c00bcd879a
Author: FRIGN <dev@frign.de>
Date:   Thu,  5 Mar 2015 18:03:08 +0100

Audit mkdir(1)

1) No need for strchr() in mkdirp or a while-loop. Rewrite it in
   a sane and readable way.
2) fix usage according to the manpage.
3) order includes, don't align local variables.
4) argc-style-fix.
5) BUGFIX: Don't try to chmod() *argv when mkdir() / mkdirp() failed.
6) Add newline before return in two places.

Diffstat:
MREADME | 2+-
Mmkdir.c | 48+++++++++++++++++++++---------------------------
2 files changed, 22 insertions(+), 28 deletions(-)

diff --git a/README b/README @@ -44,7 +44,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support, =* logname yes none = ls no (-C), -S, -f, -m, -s, -x =*| md5sum non-posix none -=* mkdir yes none +=*| mkdir yes none =*| mkfifo yes none =* mktemp non-posix none =*| mv yes none (-i) diff --git a/mkdir.c b/mkdir.c @@ -3,7 +3,6 @@ #include <errno.h> #include <stdlib.h> -#include <string.h> #include "util.h" @@ -12,33 +11,31 @@ mkdirp(char *path) { char *p = path; - do { - if (*p && (p = strchr(&p[1], '/'))) - *p = '\0'; - if (mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO) < 0 && errno != EEXIST) { + 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); return -1; } - if (p) - *p = '/'; - } while (p); + *p = '/'; + } + return 0; } static void usage(void) { - eprintf("usage: %s [-p] [-m mode] directory ...\n", argv0); + eprintf("usage: %s [-p] [-m mode] name ...\n", argv0); } int main(int argc, char *argv[]) { - mode_t mode = 0; - mode_t mask; - int pflag = 0; - int mflag = 0; - int ret = 0; + mode_t mode = 0, mask; + int pflag = 0, mflag = 0, ret = 0; ARGBEGIN { case 'p': @@ -53,23 +50,20 @@ main(int argc, char *argv[]) usage(); } ARGEND; - if (argc < 1) + if (!argc) usage(); - for (; argc > 0; argc--, argv++) { - if (pflag) { - if (mkdirp(argv[0]) < 0) - ret = 1; - } else if (mkdir(argv[0], S_IRWXU|S_IRWXG|S_IRWXO) < 0) { - weprintf("mkdir %s:", argv[0]); + for (; *argv; argc--, argv++) { + if (pflag && mkdirp(*argv) < 0) { + ret = 1; + } else if (!pflag && mkdir(*argv, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { + weprintf("mkdir %s:", *argv); + ret = 1; + } else if (mflag && chmod(*argv, mode) < 0) { + weprintf("chmod %s:", *argv); ret = 1; - } - if (mflag) { - if (chmod(argv[0], mode) < 0) { - weprintf("chmod %s:", argv[0]); - ret = 1; - } } } + return ret; }