[hackers] [sbase] Audit mkdir(1) || FRIGN

From: <git_AT_suckless.org>
Date: Thu, 5 Mar 2015 19:26:37 +0100 (CET)

commit 62947055b9da0bb4c53500d6891edc678c323510
Author: FRIGN <dev_AT_frign.de>
Date: Thu Mar 5 18:03:08 2015 +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.

diff --git a/README b/README
index 524c369..d6d6b3f 100644
--- a/README
+++ b/README
_AT_@ -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
index d1f43be..b21b59a 100644
--- a/mkdir.c
+++ b/mkdir.c
_AT_@ -3,7 +3,6 @@
 
 #include <errno.h>
 #include <stdlib.h>
-#include <string.h>
 
 #include "util.h"
 
_AT_@ -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':
_AT_@ -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;
 }
Received on Thu Mar 05 2015 - 19:26:37 CET

This archive was generated by hypermail 2.3.0 : Thu Mar 05 2015 - 19:36:10 CET