1 /* rmdir -- remove directories
3 Copyright (C) 1990-2019 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 <https://www.gnu.org/licenses/>. */
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> */
28 #include <sys/types.h>
32 #include "prog-fprintf.h"
34 /* The official name of this program (e.g., no 'g' prefix). */
35 #define PROGRAM_NAME "rmdir"
37 #define AUTHORS proper_name ("David MacKenzie")
39 /* If true, remove empty parent directories. */
40 static bool remove_empty_parents
;
42 /* If true, don't treat failure to remove a nonempty directory
44 static bool ignore_fail_on_non_empty
;
46 /* If true, output a diagnostic for every directory processed. */
49 /* For long options that have no equivalent short option, use a
50 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
53 IGNORE_FAIL_ON_NON_EMPTY_OPTION
= CHAR_MAX
+ 1
56 static struct option
const longopts
[] =
58 /* Don't name this '--force' because it's not close enough in meaning
59 to e.g. rm's -f option. */
60 {"ignore-fail-on-non-empty", no_argument
, NULL
,
61 IGNORE_FAIL_ON_NON_EMPTY_OPTION
},
63 {"path", no_argument
, NULL
, 'p'}, /* Deprecated. */
64 {"parents", no_argument
, NULL
, 'p'},
65 {"verbose", no_argument
, NULL
, 'v'},
66 {GETOPT_HELP_OPTION_DECL
},
67 {GETOPT_VERSION_OPTION_DECL
},
71 /* Return true if ERROR_NUMBER is one of the values associated
72 with a failed rmdir due to non-empty target directory. */
74 errno_rmdir_non_empty (int error_number
)
76 return error_number
== ENOTEMPTY
|| error_number
== EEXIST
;
79 /* Return true if when rmdir fails with errno == ERROR_NUMBER
80 the directory may be empty. */
82 errno_may_be_empty (int error_number
)
97 /* Return true if an rmdir failure with errno == error_number
98 for DIR is ignorable. */
100 ignorable_failure (int error_number
, char const *dir
)
102 return (ignore_fail_on_non_empty
103 && (errno_rmdir_non_empty (error_number
)
104 || (errno_may_be_empty (error_number
)
105 && is_empty_dir (AT_FDCWD
, dir
))));
108 /* Remove any empty parent directories of DIR.
109 If DIR contains slash characters, at least one of them
110 (beginning with the rightmost) is replaced with a NUL byte.
111 Return true if successful. */
114 remove_parents (char *dir
)
119 strip_trailing_slashes (dir
);
122 slash
= strrchr (dir
, '/');
125 /* Remove any characters after the slash, skipping any extra
127 while (slash
> dir
&& *slash
== '/')
131 /* Give a diagnostic for each attempted removal if --verbose. */
133 prog_fprintf (stdout
, _("removing directory, %s"), quoteaf (dir
));
135 ok
= (rmdir (dir
) == 0);
139 /* Stop quietly if --ignore-fail-on-non-empty. */
140 if (ignorable_failure (errno
, dir
))
146 /* Barring race conditions, DIR is expected to be a directory. */
147 error (0, errno
, _("failed to remove directory %s"),
159 if (status
!= EXIT_SUCCESS
)
163 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name
);
165 Remove the DIRECTORY(ies), if they are empty.\n\
167 --ignore-fail-on-non-empty\n\
168 ignore each failure that is solely because a directory\n\
172 -p, --parents remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is\
174 similar to 'rmdir a/b/c a/b a'\n\
175 -v, --verbose output a diagnostic for every directory processed\n\
177 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
178 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
179 emit_ancillary_info (PROGRAM_NAME
);
185 main (int argc
, char **argv
)
190 initialize_main (&argc
, &argv
);
191 set_program_name (argv
[0]);
192 setlocale (LC_ALL
, "");
193 bindtextdomain (PACKAGE
, LOCALEDIR
);
194 textdomain (PACKAGE
);
196 atexit (close_stdout
);
198 remove_empty_parents
= false;
200 while ((optc
= getopt_long (argc
, argv
, "pv", longopts
, NULL
)) != -1)
205 remove_empty_parents
= true;
207 case IGNORE_FAIL_ON_NON_EMPTY_OPTION
:
208 ignore_fail_on_non_empty
= true;
213 case_GETOPT_HELP_CHAR
;
214 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
216 usage (EXIT_FAILURE
);
222 error (0, 0, _("missing operand"));
223 usage (EXIT_FAILURE
);
226 for (; optind
< argc
; ++optind
)
228 char *dir
= argv
[optind
];
230 /* Give a diagnostic for each attempted removal if --verbose. */
232 prog_fprintf (stdout
, _("removing directory, %s"), quoteaf (dir
));
234 if (rmdir (dir
) != 0)
236 if (ignorable_failure (errno
, dir
))
239 /* Here, the diagnostic is less precise, since we have no idea
240 whether DIR is a directory. */
241 error (0, errno
, _("failed to remove %s"), quoteaf (dir
));
244 else if (remove_empty_parents
)
246 ok
&= remove_parents (dir
);
250 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;