summaryrefslogtreecommitdiff
path: root/coreutils/rmdir.c
diff options
context:
space:
mode:
authorMatt Kraai2001-08-29 21:18:47 +0000
committerMatt Kraai2001-08-29 21:18:47 +0000
commit1350666498752664a5e772ed99de3e541c04c477 (patch)
tree8cd1e8ef9238cef1cf0ba71c9778237b554d02c9 /coreutils/rmdir.c
parent5171bf6d40b6b6c0b31e068aec15d14071e3e176 (diff)
downloadbusybox-1350666498752664a5e772ed99de3e541c04c477.zip
busybox-1350666498752664a5e772ed99de3e541c04c477.tar.gz
Add -p support.
Diffstat (limited to 'coreutils/rmdir.c')
-rw-r--r--coreutils/rmdir.c70
1 files changed, 61 insertions, 9 deletions
diff --git a/coreutils/rmdir.c b/coreutils/rmdir.c
index 2c28037..cac27ca 100644
--- a/coreutils/rmdir.c
+++ b/coreutils/rmdir.c
@@ -22,24 +22,76 @@
*
*/
-#include <stdio.h>
-#include <errno.h>
+#include <getopt.h>
#include <unistd.h>
#include <stdlib.h>
+
#include "busybox.h"
-extern int rmdir_main(int argc, char **argv)
+
+/* Return true if a path is composed of multiple components. */
+
+static int
+multiple_components_p (const char *path)
+{
+ const char *s = path;
+
+ while (s[0] != '\0' && s[0] != '/')
+ s++;
+
+ while (s[0] == '/')
+ s++;
+
+ return (s[0] != '\0');
+}
+
+
+/* Remove a directory. Returns 0 if successful, -1 on error. */
+
+static int
+remove_directory (char *path, int flags)
+{
+ if (!(flags & FILEUTILS_RECUR)) {
+ if (rmdir (path) < 0) {
+ perror_msg ("unable to remove `%s'", path);
+ return -1;
+ }
+ } else {
+ if (remove_directory (path, 0) < 0)
+ return -1;
+
+ if (multiple_components_p (path))
+ if (remove_directory (dirname (path), flags) < 0)
+ return -1;
+ }
+
+ return 0;
+}
+
+
+extern int
+rmdir_main (int argc, char **argv)
{
int status = EXIT_SUCCESS;
+ int flags = 0;
+ int i, opt;
- if (argc == 1 || **(argv + 1) == '-')
+ while ((opt = getopt (argc, argv, "p")) != -1)
+ switch (opt) {
+ case 'p':
+ flags |= FILEUTILS_RECUR;
+ break;
+
+ default:
+ show_usage ();
+ }
+
+ if (optind == argc)
show_usage();
- while (--argc > 0) {
- if (rmdir(*(++argv)) == -1) {
- perror_msg("%s", *argv);
+ for (i = optind; i < argc; i++)
+ if (remove_directory (argv[i], flags) < 0)
status = EXIT_FAILURE;
- }
- }
+
return status;
}