summaryrefslogtreecommitdiff
path: root/coreutils/rmdir.c (plain)
blob: 8979941ce4561c7eff7975634690ecbcb863e6a1
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 *
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
9//config:config RMDIR
10//config: bool "rmdir"
11//config: default y
12//config: help
13//config: rmdir is used to remove empty directories.
14//config:
15//config:config FEATURE_RMDIR_LONG_OPTIONS
16//config: bool "Enable long options"
17//config: default y
18//config: depends on RMDIR && LONG_OPTS
19//config: help
20//config: Support long options for the rmdir applet, including
21//config: --ignore-fail-on-non-empty for compatibility with GNU rmdir.
22
23//applet:IF_RMDIR(APPLET_NOFORK(rmdir, rmdir, BB_DIR_BIN, BB_SUID_DROP, rmdir))
24
25//kbuild:lib-$(CONFIG_RMDIR) += rmdir.o
26
27/* BB_AUDIT SUSv3 compliant */
28/* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
29
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"
37//usage: )
38//usage: IF_NOT_FEATURE_RMDIR_LONG_OPTIONS(
39//usage: "\n -p Include parents"
40//usage: )
41//usage:
42//usage:#define rmdir_example_usage
43//usage: "# rmdir /tmp/foo\n"
44
45#include "libbb.h"
46
47/* This is a NOFORK applet. Be very careful! */
48
49
50#define PARENTS (1 << 0)
51#define VERBOSE ((1 << 1) * ENABLE_FEATURE_VERBOSE)
52#define IGNORE_NON_EMPTY (1 << 2)
53
54int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
55int rmdir_main(int argc UNUSED_PARAM, char **argv)
56{
57 int status = EXIT_SUCCESS;
58 int flags;
59 char *path;
60
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"
67 IF_FEATURE_VERBOSE(
68 "verbose\0" No_argument "v"
69 )
70 ;
71 applet_long_options = rmdir_longopts;
72#endif
73 flags = getopt32(argv, "pv");
74 argv += optind;
75
76 if (!*argv) {
77 bb_show_usage();
78 }
79
80 do {
81 path = *argv;
82
83 while (1) {
84 if (flags & VERBOSE) {
85 printf("rmdir: removing directory, '%s'\n", path);
86 }
87
88 if (rmdir(path) < 0) {
89#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
90 if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
91 break;
92#endif
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. */
97 path = dirname(path);
98 /* Path is now just the parent component. Dirname
99 * returns "." if there are no parents.
100 */
101 if (NOT_LONE_CHAR(path, '.')) {
102 continue;
103 }
104 }
105 break;
106 }
107 } while (*++argv);
108
109 return status;
110}
111