commit ae0df49f243b4103477863dc7e54afab2a521491
parent 97ad252d5dfd10323871f7a7cc9a004a05609846
Author: FRIGN <dev@frign.de>
Date: Fri, 30 Jan 2015 12:21:26 +0100
Add p-flag to rmdir(1)
Diffstat:
M | rmdir.c | | | 26 | +++++++++++++++++++++++--- |
1 file changed, 23 insertions(+), 3 deletions(-)
diff --git a/rmdir.c b/rmdir.c
@@ -1,6 +1,8 @@
/* See LICENSE file for copyright and license details. */
+#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include "util.h"
@@ -8,13 +10,19 @@
static void
usage(void)
{
- eprintf("usage: rmdir dir...\n");
+ eprintf("usage: rmdir [-p] dir ...\n");
}
int
main(int argc, char *argv[])
{
+ int pflag = 0;
+ char *d;
+
ARGBEGIN {
+ case 'p':
+ pflag = 1;
+ break;
default:
usage();
} ARGEND;
@@ -22,8 +30,20 @@ main(int argc, char *argv[])
if (argc < 1)
usage();
- for (; argc > 0; argc--, argv++)
- if (rmdir(argv[0]) < 0)
+ for (; argc > 0; argc--, argv++) {
+ if (rmdir(argv[0]) < 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;
}