6c3ba4c4c7
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.
50 lines
713 B
C
50 lines
713 B
C
/* See LICENSE file for copyright and license details. */
|
|
#include <libgen.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "util.h"
|
|
|
|
static void
|
|
usage(void)
|
|
{
|
|
eprintf("usage: rmdir [-p] dir ...\n");
|
|
}
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
int pflag = 0, ret = 0;
|
|
char *d;
|
|
|
|
ARGBEGIN {
|
|
case 'p':
|
|
pflag = 1;
|
|
break;
|
|
default:
|
|
usage();
|
|
} ARGEND;
|
|
|
|
if (!argc)
|
|
usage();
|
|
|
|
for (; *argv; argc--, argv++) {
|
|
if (rmdir(*argv) < 0) {
|
|
weprintf("rmdir %s:", *argv);
|
|
ret = 1;
|
|
} else if (pflag) {
|
|
d = dirname(*argv);
|
|
for (; strcmp(d, "/") && strcmp(d, ".") ;) {
|
|
if (rmdir(d) < 0) {
|
|
weprintf("rmdir %s:", d);
|
|
ret = 1;
|
|
break;
|
|
}
|
|
d = dirname(d);
|
|
}
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|