doc: clarify the operation of wc -L
[coreutils.git] / src / rmdir.c
blob457e855e234844609e5cad6d79b336a5cb38e439
1 /* rmdir -- remove directories
3 Copyright (C) 1990-2015 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Options:
19 -p, --parent Remove any parent dirs that are explicitly mentioned
20 in an argument, if they become empty after the
21 argument file is removed.
23 David MacKenzie <djm@ai.mit.edu> */
25 #include <config.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <sys/types.h>
30 #include "system.h"
31 #include "error.h"
32 #include "prog-fprintf.h"
33 #include "quote.h"
35 /* The official name of this program (e.g., no 'g' prefix). */
36 #define PROGRAM_NAME "rmdir"
38 #define AUTHORS proper_name ("David MacKenzie")
40 /* If true, remove empty parent directories. */
41 static bool remove_empty_parents;
43 /* If true, don't treat failure to remove a nonempty directory
44 as an error. */
45 static bool ignore_fail_on_non_empty;
47 /* If true, output a diagnostic for every directory processed. */
48 static bool verbose;
50 /* For long options that have no equivalent short option, use a
51 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
52 enum
54 IGNORE_FAIL_ON_NON_EMPTY_OPTION = CHAR_MAX + 1
57 static struct option const longopts[] =
59 /* Don't name this '--force' because it's not close enough in meaning
60 to e.g. rm's -f option. */
61 {"ignore-fail-on-non-empty", no_argument, NULL,
62 IGNORE_FAIL_ON_NON_EMPTY_OPTION},
64 {"path", no_argument, NULL, 'p'}, /* Deprecated. */
65 {"parents", no_argument, NULL, 'p'},
66 {"verbose", no_argument, NULL, 'v'},
67 {GETOPT_HELP_OPTION_DECL},
68 {GETOPT_VERSION_OPTION_DECL},
69 {NULL, 0, NULL, 0}
72 /* Return true if ERROR_NUMBER is one of the values associated
73 with a failed rmdir due to non-empty target directory. */
74 static bool
75 errno_rmdir_non_empty (int error_number)
77 return error_number == ENOTEMPTY || error_number == EEXIST;
80 /* Return true if when rmdir fails with errno == ERROR_NUMBER
81 the directory may be empty. */
82 static bool
83 errno_may_be_empty (int error_number)
85 switch (error_number)
87 case EACCES:
88 case EPERM:
89 case EROFS:
90 case EEXIST:
91 case EBUSY:
92 return true;
93 default:
94 return false;
98 /* Return true if an rmdir failure with errno == error_number
99 for DIR is ignorable. */
100 static bool
101 ignorable_failure (int error_number, char const *dir)
103 return (ignore_fail_on_non_empty
104 && (errno_rmdir_non_empty (error_number)
105 || (errno_may_be_empty (error_number)
106 && is_empty_dir (AT_FDCWD, dir))));
109 /* Remove any empty parent directories of DIR.
110 If DIR contains slash characters, at least one of them
111 (beginning with the rightmost) is replaced with a NUL byte.
112 Return true if successful. */
114 static bool
115 remove_parents (char *dir)
117 char *slash;
118 bool ok = true;
120 strip_trailing_slashes (dir);
121 while (1)
123 slash = strrchr (dir, '/');
124 if (slash == NULL)
125 break;
126 /* Remove any characters after the slash, skipping any extra
127 slashes in a row. */
128 while (slash > dir && *slash == '/')
129 --slash;
130 slash[1] = 0;
132 /* Give a diagnostic for each attempted removal if --verbose. */
133 if (verbose)
134 prog_fprintf (stdout, _("removing directory, %s"), quote (dir));
136 ok = (rmdir (dir) == 0);
138 if (!ok)
140 /* Stop quietly if --ignore-fail-on-non-empty. */
141 if (ignorable_failure (errno, dir))
143 ok = true;
145 else
147 /* Barring race conditions, DIR is expected to be a directory. */
148 error (0, errno, _("failed to remove directory %s"),
149 quote (dir));
151 break;
154 return ok;
157 void
158 usage (int status)
160 if (status != EXIT_SUCCESS)
161 emit_try_help ();
162 else
164 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
165 fputs (_("\
166 Remove the DIRECTORY(ies), if they are empty.\n\
168 --ignore-fail-on-non-empty\n\
169 ignore each failure that is solely because a directory\n\
170 is non-empty\n\
171 "), stdout);
172 fputs (_("\
173 -p, --parents remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is\
175 similar to 'rmdir a/b/c a/b a'\n\
176 -v, --verbose output a diagnostic for every directory processed\n\
177 "), stdout);
178 fputs (HELP_OPTION_DESCRIPTION, stdout);
179 fputs (VERSION_OPTION_DESCRIPTION, stdout);
180 emit_ancillary_info (PROGRAM_NAME);
182 exit (status);
186 main (int argc, char **argv)
188 bool ok = true;
189 int optc;
191 initialize_main (&argc, &argv);
192 set_program_name (argv[0]);
193 setlocale (LC_ALL, "");
194 bindtextdomain (PACKAGE, LOCALEDIR);
195 textdomain (PACKAGE);
197 atexit (close_stdout);
199 remove_empty_parents = false;
201 while ((optc = getopt_long (argc, argv, "pv", longopts, NULL)) != -1)
203 switch (optc)
205 case 'p':
206 remove_empty_parents = true;
207 break;
208 case IGNORE_FAIL_ON_NON_EMPTY_OPTION:
209 ignore_fail_on_non_empty = true;
210 break;
211 case 'v':
212 verbose = true;
213 break;
214 case_GETOPT_HELP_CHAR;
215 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
216 default:
217 usage (EXIT_FAILURE);
221 if (optind == argc)
223 error (0, 0, _("missing operand"));
224 usage (EXIT_FAILURE);
227 for (; optind < argc; ++optind)
229 char *dir = argv[optind];
231 /* Give a diagnostic for each attempted removal if --verbose. */
232 if (verbose)
233 prog_fprintf (stdout, _("removing directory, %s"), quote (dir));
235 if (rmdir (dir) != 0)
237 if (ignorable_failure (errno, dir))
238 continue;
240 /* Here, the diagnostic is less precise, since we have no idea
241 whether DIR is a directory. */
242 error (0, errno, _("failed to remove %s"), quote (dir));
243 ok = false;
245 else if (remove_empty_parents)
247 ok &= remove_parents (dir);
251 return ok ? EXIT_SUCCESS : EXIT_FAILURE;