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.
This commit is contained in:
FRIGN 2015-03-02 15:39:39 +01:00
parent e50ee15a9c
commit 6c3ba4c4c7
2 changed files with 14 additions and 11 deletions

2
README
View File

@ -57,7 +57,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
= readlink non-posix none = readlink non-posix none
=* renice yes none =* renice yes none
=*| rm yes none (-i) =*| rm yes none (-i)
=* rmdir yes none =*| rmdir yes none
# sed # sed
seq non-posix none seq non-posix none
=*| setsid non-posix none =*| setsid non-posix none

23
rmdir.c
View File

@ -14,7 +14,7 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int pflag = 0, ret = 0; int pflag = 0, ret = 0;
char *d; char *d;
ARGBEGIN { ARGBEGIN {
@ -25,22 +25,25 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if (argc < 1) if (!argc)
usage(); usage();
for (; argc > 0; argc--, argv++) { for (; *argv; argc--, argv++) {
if (rmdir(argv[0]) < 0) { if (rmdir(*argv) < 0) {
weprintf("rmdir %s:", argv[0]); weprintf("rmdir %s:", *argv);
ret = 1; ret = 1;
} } else if (pflag) {
if (pflag && !ret) { d = dirname(*argv);
d = dirname(argv[0]);
for (; strcmp(d, "/") && strcmp(d, ".") ;) { for (; strcmp(d, "/") && strcmp(d, ".") ;) {
if (rmdir(d) < 0) if (rmdir(d) < 0) {
eprintf("rmdir %s:", d); weprintf("rmdir %s:", d);
ret = 1;
break;
}
d = dirname(d); d = dirname(d);
} }
} }
} }
return ret; return ret;
} }