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. */
#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;
}