Add p-flag to rmdir(1)

This commit is contained in:
FRIGN 2015-01-30 12:21:26 +01:00
parent 97ad252d5d
commit ae0df49f24

26
rmdir.c
View File

@ -1,6 +1,8 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <libgen.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
@ -8,13 +10,19 @@
static void static void
usage(void) usage(void)
{ {
eprintf("usage: rmdir dir...\n"); eprintf("usage: rmdir [-p] dir ...\n");
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int pflag = 0;
char *d;
ARGBEGIN { ARGBEGIN {
case 'p':
pflag = 1;
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -22,8 +30,20 @@ main(int argc, char *argv[])
if (argc < 1) if (argc < 1)
usage(); usage();
for (; argc > 0; argc--, argv++) for (; argc > 0; argc--, argv++) {
if (rmdir(argv[0]) < 0) if (rmdir(argv[0]) < 0) {
weprintf("rmdir %s:", argv[0]); weprintf("rmdir %s:", argv[0]);
if (pflag)
return 1;
}
if (pflag) {
d = dirname(argv[0]);
for (; strcmp(d, "/") && strcmp(d, ".") ;) {
if (rmdir(d) < 0)
eprintf("rmdir %s:", d);
d = dirname(d);
}
}
}
return 0; return 0;
} }