1 /* vi: set sw=4 ts=4: */
3 * rmdir implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config: bool "rmdir"
13 //config: rmdir is used to remove empty directories.
15 //config:config FEATURE_RMDIR_LONG_OPTIONS
16 //config: bool "Enable long options"
18 //config: depends on RMDIR && LONG_OPTS
20 //config: Support long options for the rmdir applet, including
21 //config: --ignore-fail-on-non-empty for compatibility with GNU rmdir.
23 //applet:IF_RMDIR(APPLET_NOFORK(rmdir, rmdir, BB_DIR_BIN, BB_SUID_DROP, rmdir))
25 //kbuild:lib-$(CONFIG_RMDIR) += rmdir.o
27 /* BB_AUDIT SUSv3 compliant */
28 /* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
30 //usage:#define rmdir_trivial_usage
31 //usage: "[OPTIONS] DIRECTORY..."
32 //usage:#define rmdir_full_usage "\n\n"
33 //usage: "Remove DIRECTORY if it is empty\n"
34 //usage: IF_FEATURE_RMDIR_LONG_OPTIONS(
35 //usage: "\n -p|--parents Include parents"
36 //usage: "\n --ignore-fail-on-non-empty"
38 //usage: IF_NOT_FEATURE_RMDIR_LONG_OPTIONS(
39 //usage: "\n -p Include parents"
42 //usage:#define rmdir_example_usage
43 //usage: "# rmdir /tmp/foo\n"
47 /* This is a NOFORK applet. Be very careful! */
50 #define PARENTS (1 << 0)
51 #define VERBOSE ((1 << 1) * ENABLE_FEATURE_VERBOSE)
52 #define IGNORE_NON_EMPTY (1 << 2)
54 int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
55 int rmdir_main(int argc UNUSED_PARAM, char **argv)
57 int status = EXIT_SUCCESS;
61 #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
62 static const char rmdir_longopts[] ALIGN1 =
63 "parents\0" No_argument "p"
64 /* Debian etch: many packages fail to be purged or installed
65 * because they desperately want this option: */
66 "ignore-fail-on-non-empty\0" No_argument "\xff"
68 "verbose\0" No_argument "v"
71 applet_long_options = rmdir_longopts;
73 flags = getopt32(argv, "pv");
84 if (flags & VERBOSE) {
85 printf("rmdir: removing directory, '%s'\n", path);
88 if (rmdir(path) < 0) {
89 #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
90 if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
93 bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
94 status = EXIT_FAILURE;
95 } else if (flags & PARENTS) {
96 /* Note: path was not "" since rmdir succeeded. */
98 /* Path is now just the parent component. Dirname
99 * returns "." if there are no parents.
101 if (NOT_LONE_CHAR(path, '.')) {