1 /* rmdir -- remove directories
3 Copyright (C) 90, 91, 1995-2002, 2004-2008 Free Software
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 -p, --parent Remove any parent dirs that are explicitly mentioned
21 in an argument, if they become empty after the
22 argument file is removed.
24 David MacKenzie <djm@ai.mit.edu> */
29 #include <sys/types.h>
33 #include "prog-fprintf.h"
36 /* The official name of this program (e.g., no `g' prefix). */
37 #define PROGRAM_NAME "rmdir"
39 #define AUTHORS proper_name ("David MacKenzie")
41 /* The name this program was run with. */
44 /* If true, remove empty parent directories. */
45 static bool remove_empty_parents;
47 /* If true, don't treat failure to remove a nonempty directory
49 static bool ignore_fail_on_non_empty;
51 /* If true, output a diagnostic for every directory processed. */
54 /* For long options that have no equivalent short option, use a
55 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
58 IGNORE_FAIL_ON_NON_EMPTY_OPTION = CHAR_MAX + 1
61 static struct option const longopts[] =
63 /* Don't name this `--force' because it's not close enough in meaning
64 to e.g. rm's -f option. */
65 {"ignore-fail-on-non-empty", no_argument, NULL,
66 IGNORE_FAIL_ON_NON_EMPTY_OPTION},
68 {"path", no_argument, NULL, 'p'}, /* Deprecated. */
69 {"parents", no_argument, NULL, 'p'},
70 {"verbose", no_argument, NULL, 'v'},
71 {GETOPT_HELP_OPTION_DECL},
72 {GETOPT_VERSION_OPTION_DECL},
76 /* Return true if ERROR_NUMBER is one of the values associated
77 with a failed rmdir due to non-empty target directory. */
79 errno_rmdir_non_empty (int error_number)
81 return (error_number == RMDIR_ERRNO_NOT_EMPTY);
84 /* Return true if when rmdir fails with errno == ERROR_NUMBER
85 the directory may be empty. */
87 errno_may_be_empty (int error_number)
102 /* Return true if an rmdir failure with errno == error_number
103 for DIR is ignorable. */
105 ignorable_failure (int error_number, char const *dir)
107 return (ignore_fail_on_non_empty
108 && (errno_rmdir_non_empty (error_number)
109 || (errno_may_be_empty (error_number)
110 && is_empty_dir (AT_FDCWD, dir))));
113 /* Remove any empty parent directories of DIR.
114 If DIR contains slash characters, at least one of them
115 (beginning with the rightmost) is replaced with a NUL byte.
116 Return true if successful. */
119 remove_parents (char *dir)
124 strip_trailing_slashes (dir);
127 slash = strrchr (dir, '/');
130 /* Remove any characters after the slash, skipping any extra
132 while (slash > dir && *slash == '/')
136 /* Give a diagnostic for each attempted removal if --verbose. */
138 prog_fprintf (stdout, _("removing directory, %s"), quote (dir));
140 ok = (rmdir (dir) == 0);
144 /* Stop quietly if --ignore-fail-on-non-empty. */
145 if (ignorable_failure (errno, dir))
151 /* Barring race conditions, DIR is expected to be a directory. */
152 error (0, errno, _("failed to remove directory %s"),
164 if (status != EXIT_SUCCESS)
165 fprintf (stderr, _("Try `%s --help' for more information.\n"),
169 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
171 Remove the DIRECTORY(ies), if they are empty.\n\
173 --ignore-fail-on-non-empty\n\
174 ignore each failure that is solely because a directory\n\
178 -p, --parents Remove DIRECTORY and its ancestors. E.g., `rmdir -p a/b/c' is\n\
179 similar to `rmdir a/b/c a/b a'.\n\
180 -v, --verbose output a diagnostic for every directory processed\n\
182 fputs (HELP_OPTION_DESCRIPTION, stdout);
183 fputs (VERSION_OPTION_DESCRIPTION, stdout);
184 emit_bug_reporting_address ();
190 main (int argc, char **argv)
195 initialize_main (&argc, &argv);
196 program_name = argv[0];
197 setlocale (LC_ALL, "");
198 bindtextdomain (PACKAGE, LOCALEDIR);
199 textdomain (PACKAGE);
201 atexit (close_stdout);
203 remove_empty_parents = false;
205 while ((optc = getopt_long (argc, argv, "pv", longopts, NULL)) != -1)
210 remove_empty_parents = true;
212 case IGNORE_FAIL_ON_NON_EMPTY_OPTION:
213 ignore_fail_on_non_empty = true;
218 case_GETOPT_HELP_CHAR;
219 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
221 usage (EXIT_FAILURE);
227 error (0, 0, _("missing operand"));
228 usage (EXIT_FAILURE);
231 for (; optind < argc; ++optind)
233 char *dir = argv[optind];
235 /* Give a diagnostic for each attempted removal if --verbose. */
237 prog_fprintf (stdout, _("removing directory, %s"), quote (dir));
239 if (rmdir (dir) != 0)
241 if (ignorable_failure (errno, dir))
244 /* Here, the diagnostic is less precise, since we have no idea
245 whether DIR is a directory. */
246 error (0, errno, _("failed to remove %s"), quote (dir));
249 else if (remove_empty_parents)
251 ok &= remove_parents (dir);
255 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);