From 1350666498752664a5e772ed99de3e541c04c477 Mon Sep 17 00:00:00 2001 From: Matt Kraai Date: Wed, 29 Aug 2001 21:18:47 +0000 Subject: [PATCH] Add -p support. --- coreutils/rmdir.c | 70 +++++++++++++++++++++++++++++++++++++++++------ rmdir.c | 70 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 122 insertions(+), 18 deletions(-) diff --git a/coreutils/rmdir.c b/coreutils/rmdir.c index 2c280376f..cac27cac9 100644 --- a/coreutils/rmdir.c +++ b/coreutils/rmdir.c @@ -22,24 +22,76 @@ * */ -#include -#include +#include #include #include + #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; } diff --git a/rmdir.c b/rmdir.c index 2c280376f..cac27cac9 100644 --- a/rmdir.c +++ b/rmdir.c @@ -22,24 +22,76 @@ * */ -#include -#include +#include #include #include + #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; } -- 2.25.1