[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * rmdir implementation for busybox
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
|
---|
| 6 | *
|
---|
[2725] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 8 | */
|
---|
| 9 |
|
---|
| 10 | /* BB_AUDIT SUSv3 compliant */
|
---|
| 11 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
|
---|
| 12 |
|
---|
[3232] | 13 | //usage:#define rmdir_trivial_usage
|
---|
| 14 | //usage: "[OPTIONS] DIRECTORY..."
|
---|
| 15 | //usage:#define rmdir_full_usage "\n\n"
|
---|
| 16 | //usage: "Remove DIRECTORY if it is empty\n"
|
---|
| 17 | //usage: IF_FEATURE_RMDIR_LONG_OPTIONS(
|
---|
| 18 | //usage: "\n -p|--parents Include parents"
|
---|
| 19 | //usage: "\n --ignore-fail-on-non-empty"
|
---|
| 20 | //usage: )
|
---|
| 21 | //usage: IF_NOT_FEATURE_RMDIR_LONG_OPTIONS(
|
---|
| 22 | //usage: "\n -p Include parents"
|
---|
| 23 | //usage: )
|
---|
| 24 | //usage:
|
---|
| 25 | //usage:#define rmdir_example_usage
|
---|
| 26 | //usage: "# rmdir /tmp/foo\n"
|
---|
| 27 |
|
---|
[1765] | 28 | #include "libbb.h"
|
---|
[821] | 29 |
|
---|
[1765] | 30 | /* This is a NOFORK applet. Be very careful! */
|
---|
| 31 |
|
---|
| 32 |
|
---|
[3232] | 33 | #define PARENTS (1 << 0)
|
---|
[3621] | 34 | #define VERBOSE ((1 << 1) * ENABLE_FEATURE_VERBOSE)
|
---|
[3232] | 35 | #define IGNORE_NON_EMPTY (1 << 2)
|
---|
[2725] | 36 |
|
---|
| 37 | int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 38 | int rmdir_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 39 | {
|
---|
| 40 | int status = EXIT_SUCCESS;
|
---|
| 41 | int flags;
|
---|
| 42 | char *path;
|
---|
| 43 |
|
---|
[2725] | 44 | #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
|
---|
| 45 | static const char rmdir_longopts[] ALIGN1 =
|
---|
| 46 | "parents\0" No_argument "p"
|
---|
| 47 | /* Debian etch: many packages fail to be purged or installed
|
---|
| 48 | * because they desperately want this option: */
|
---|
| 49 | "ignore-fail-on-non-empty\0" No_argument "\xff"
|
---|
[3621] | 50 | IF_FEATURE_VERBOSE(
|
---|
| 51 | "verbose\0" No_argument "v"
|
---|
| 52 | )
|
---|
[2725] | 53 | ;
|
---|
| 54 | applet_long_options = rmdir_longopts;
|
---|
| 55 | #endif
|
---|
[3232] | 56 | flags = getopt32(argv, "pv");
|
---|
[821] | 57 | argv += optind;
|
---|
| 58 |
|
---|
| 59 | if (!*argv) {
|
---|
| 60 | bb_show_usage();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | do {
|
---|
| 64 | path = *argv;
|
---|
| 65 |
|
---|
[1765] | 66 | while (1) {
|
---|
[3621] | 67 | if (flags & VERBOSE) {
|
---|
| 68 | printf("rmdir: removing directory, '%s'\n", path);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[821] | 71 | if (rmdir(path) < 0) {
|
---|
[2725] | 72 | #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
|
---|
| 73 | if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
|
---|
| 74 | break;
|
---|
| 75 | #endif
|
---|
| 76 | bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
|
---|
[821] | 77 | status = EXIT_FAILURE;
|
---|
[2725] | 78 | } else if (flags & PARENTS) {
|
---|
| 79 | /* Note: path was not "" since rmdir succeeded. */
|
---|
[821] | 80 | path = dirname(path);
|
---|
[2725] | 81 | /* Path is now just the parent component. Dirname
|
---|
| 82 | * returns "." if there are no parents.
|
---|
[1765] | 83 | */
|
---|
[2725] | 84 | if (NOT_LONE_CHAR(path, '.')) {
|
---|
[821] | 85 | continue;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | break;
|
---|
[1765] | 89 | }
|
---|
[821] | 90 | } while (*++argv);
|
---|
| 91 |
|
---|
| 92 | return status;
|
---|
| 93 | }
|
---|