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

From: <git_AT_suckless.org>
Date: Tue, 3 Mar 2015 14:32:26 +0100 (CET)

commit da06da56696e5a8e6fddea26449365ced46e8b0c
Author: FRIGN <dev_AT_frign.de>
Date: Mon Mar 2 15:39:39 2015 +0100

    Audit rmdir(1)
    
    1) style fix (don't arrange local variables)
    2) BUGFIX: Previously, if ret was turned 1 for some folder, it
       would disable the p-flag for every following folders, which
       is not desired.
       Instead, the "else if" makes sure that the p-flag-section is
       only entered when the initial rmdir succeeds.
    3) BUGFIX: Previously, the program would cancel with eprintf if
       it failed to remove one folder in the parent-pathname.
       This is not desired, as we have other folders pending.
       Instead, print a warning for the current failing parent-folder,
       set ret to 1 and break off the loop at this point.
       This allows to finish the other pending folders without issues.

diff --git a/README b/README
index ce1e072..266dee3 100644
--- a/README
+++ b/README
_AT_@ -57,7 +57,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
 = readlink non-posix none
 =* renice yes none
 =*| rm yes none (-i)
-=* rmdir yes none
+=*| rmdir yes none
 # sed
     seq non-posix none
 =*| setsid non-posix none
diff --git a/rmdir.c b/rmdir.c
index e5ad2b9..e0936ae 100644
--- a/rmdir.c
+++ b/rmdir.c
_AT_@ -14,7 +14,7 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
- int pflag = 0, ret = 0;
+ int pflag = 0, ret = 0;
         char *d;
 
         ARGBEGIN {
_AT_@ -25,22 +25,25 @@ main(int argc, char *argv[])
                 usage();
         } ARGEND;
 
- if (argc < 1)
+ if (!argc)
                 usage();
 
- for (; argc > 0; argc--, argv++) {
- if (rmdir(argv[0]) < 0) {
- weprintf("rmdir %s:", argv[0]);
+ for (; *argv; argc--, argv++) {
+ if (rmdir(*argv) < 0) {
+ weprintf("rmdir %s:", *argv);
                         ret = 1;
- }
- if (pflag && !ret) {
- d = dirname(argv[0]);
+ } else if (pflag) {
+ d = dirname(*argv);
                         for (; strcmp(d, "/") && strcmp(d, ".") ;) {
- if (rmdir(d) < 0)
- eprintf("rmdir %s:", d);
+ if (rmdir(d) < 0) {
+ weprintf("rmdir %s:", d);
+ ret = 1;
+ break;
+ }
                                 d = dirname(d);
                         }
                 }
         }
+
         return ret;
 }
Received on Tue Mar 03 2015 - 14:32:26 CET

This archive was generated by hypermail 2.3.0 : Tue Mar 03 2015 - 14:36:24 CET