(do_link): Produce the same sort of one-line output for
[coreutils.git] / src / rmdir.c
blobb193a08519cdde6161bda8929fad43df6adeacde
1 /* rmdir -- remove directories
2 Copyright (C) 90, 91, 1995-1999 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "rmdir"
36 #define AUTHORS "David MacKenzie"
38 #ifndef EEXIST
39 # define EEXIST 0
40 #endif
42 #ifndef ENOTEMPTY
43 # define ENOTEMPTY 0
44 #endif
46 void strip_trailing_slashes ();
48 /* The name this program was run with. */
49 char *program_name;
51 /* If nonzero, remove empty parent directories. */
52 static int empty_paths;
54 /* If nonzero, don't treat failure to remove a nonempty directory
55 as an error. */
56 static int ignore_fail_on_non_empty;
58 /* If nonzero, output a diagnostic for every directory processed. */
59 static int verbose;
61 /* For long options that have no equivalent short option, use a
62 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
63 enum
65 IGNORE_FAIL_ON_NON_EMPTY_OPTION = CHAR_MAX + 1
68 static struct option const longopts[] =
70 /* Don't name this `--force' because it's not close enough in meaning
71 to e.g. rm's -f option. */
72 {"ignore-fail-on-non-empty", no_argument, NULL,
73 IGNORE_FAIL_ON_NON_EMPTY_OPTION},
75 {"path", no_argument, NULL, 'p'},
76 {"parents", no_argument, NULL, 'p'},
77 {"verbose", no_argument, NULL, 14},
78 {GETOPT_HELP_OPTION_DECL},
79 {GETOPT_VERSION_OPTION_DECL},
80 {NULL, 0, NULL, 0}
83 /* Return nonzero if ERROR_NUMBER is one of the values associated
84 with a failed rmdir due to non-empty target directory. */
86 static int
87 errno_rmdir_non_empty (int error_number)
89 return (error_number == ENOTEMPTY
90 || error_number == EEXIST);
93 /* Remove any empty parent directories of PATH.
94 If PATH contains slash characters, at least one of them
95 (beginning with the rightmost) is replaced with a NUL byte. */
97 static int
98 remove_parents (char *path)
100 char *slash;
101 int fail = 0;
103 while (1)
105 slash = strrchr (path, '/');
106 if (slash == NULL)
107 break;
108 /* Remove any characters after the slash, skipping any extra
109 slashes in a row. */
110 while (slash > path && *slash == '/')
111 --slash;
112 slash[1] = 0;
114 /* Give a diagnostic for each attempted removal if --verbose. */
115 if (verbose)
116 error (0, 0, _("removing directory, %s"), path);
118 fail = rmdir (path);
120 if (fail)
122 /* Stop quietly if --ignore-fail-on-non-empty. */
123 if (ignore_fail_on_non_empty
124 && errno_rmdir_non_empty (errno))
126 fail = 0;
128 else
130 error (0, errno, "%s", path);
132 break;
135 return fail;
138 void
139 usage (int status)
141 if (status != 0)
142 fprintf (stderr, _("Try `%s --help' for more information.\n"),
143 program_name);
144 else
146 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
147 printf (_("\
148 Remove the DIRECTORY(ies), if they are empty.\n\
150 --ignore-fail-on-non-empty\n\
151 ignore each failure that is solely because a directory\n\
152 is non-empty\n\
153 -p, --parents remove DIRECTORY, then try to remove each directory\n\
154 component of that path name. E.g., `rmdir -p a/b/c' is\n\
155 similar to `rmdir a/b/c a/b a'.\n\
156 --verbose output a diagnostic for every directory processed\n\
157 --help display this help and exit\n\
158 --version output version information and exit\n\
159 "));
160 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
161 close_stdout ();
163 exit (status);
167 main (int argc, char **argv)
169 int errors = 0;
170 int optc;
172 program_name = argv[0];
173 setlocale (LC_ALL, "");
174 bindtextdomain (PACKAGE, LOCALEDIR);
175 textdomain (PACKAGE);
177 empty_paths = 0;
179 while ((optc = getopt_long (argc, argv, "p", longopts, NULL)) != -1)
181 switch (optc)
183 case 0: /* Long option. */
184 break;
185 case 'p':
186 empty_paths = 1;
187 break;
188 case IGNORE_FAIL_ON_NON_EMPTY_OPTION:
189 ignore_fail_on_non_empty = 1;
190 break;
191 case 14:
192 verbose = 1;
193 break;
194 case_GETOPT_HELP_CHAR;
195 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
196 default:
197 usage (1);
201 if (optind == argc)
203 error (0, 0, _("too few arguments"));
204 usage (1);
207 for (; optind < argc; ++optind)
209 int fail;
210 char *dir = argv[optind];
212 /* Stripping slashes is harmless for rmdir;
213 if the arg is not a directory, it will fail with ENOTDIR. */
214 strip_trailing_slashes (dir);
216 /* Give a diagnostic for each attempted removal if --verbose. */
217 if (verbose)
218 error (0, 0, _("removing directory, %s"), dir);
220 fail = rmdir (dir);
222 if (fail)
224 if (ignore_fail_on_non_empty
225 && errno_rmdir_non_empty (errno))
226 continue;
228 error (0, errno, "%s", dir);
229 errors = 1;
231 else if (empty_paths)
233 errors += remove_parents (dir);
237 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);