Convert "`%s'" in format strings to "%s", and wrap each
[platform/upstream/coreutils.git] / src / rmdir.c
1 /* rmdir -- remove directories
2    Copyright (C) 90, 91, 1995-2000 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
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.
22
23    David MacKenzie <djm@ai.mit.edu>  */
24
25 #include <config.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <sys/types.h>
29
30 #include "system.h"
31 #include "error.h"
32 #include "quote.h"
33
34 /* The official name of this program (e.g., no `g' prefix).  */
35 #define PROGRAM_NAME "rmdir"
36
37 #define AUTHORS "David MacKenzie"
38
39 #ifndef EEXIST
40 # define EEXIST 0
41 #endif
42
43 #ifndef ENOTEMPTY
44 # define ENOTEMPTY 0
45 #endif
46
47 void strip_trailing_slashes ();
48
49 /* The name this program was run with. */
50 char *program_name;
51
52 /* If nonzero, remove empty parent directories. */
53 static int empty_paths;
54
55 /* If nonzero, don't treat failure to remove a nonempty directory
56    as an error.  */
57 static int ignore_fail_on_non_empty;
58
59 /* If nonzero, output a diagnostic for every directory processed.  */
60 static int verbose;
61
62 /* For long options that have no equivalent short option, use a
63    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
64 enum
65 {
66   IGNORE_FAIL_ON_NON_EMPTY_OPTION = CHAR_MAX + 1
67 };
68
69 static struct option const longopts[] =
70 {
71   /* Don't name this `--force' because it's not close enough in meaning
72      to e.g. rm's -f option.  */
73   {"ignore-fail-on-non-empty", no_argument, NULL,
74    IGNORE_FAIL_ON_NON_EMPTY_OPTION},
75
76   {"path", no_argument, NULL, 'p'},
77   {"parents", no_argument, NULL, 'p'},
78   {"verbose", no_argument, NULL, 'v'},
79   {GETOPT_HELP_OPTION_DECL},
80   {GETOPT_VERSION_OPTION_DECL},
81   {NULL, 0, NULL, 0}
82 };
83
84 /* Return nonzero if ERROR_NUMBER is one of the values associated
85    with a failed rmdir due to non-empty target directory.  */
86
87 static int
88 errno_rmdir_non_empty (int error_number)
89 {
90   return (error_number == RMDIR_ERRNO_NOT_EMPTY);
91 }
92
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.  */
96
97 static int
98 remove_parents (char *path)
99 {
100   char *slash;
101   int fail = 0;
102
103   while (1)
104     {
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;
113
114       /* Give a diagnostic for each attempted removal if --verbose.  */
115       if (verbose)
116         error (0, 0, _("removing directory, %s"), path);
117
118       fail = rmdir (path);
119
120       if (fail)
121         {
122           /* Stop quietly if --ignore-fail-on-non-empty. */
123           if (ignore_fail_on_non_empty
124               && errno_rmdir_non_empty (errno))
125             {
126               fail = 0;
127             }
128           else
129             {
130               error (0, errno, "%s", quote (path));
131             }
132           break;
133         }
134     }
135   return fail;
136 }
137
138 void
139 usage (int status)
140 {
141   if (status != 0)
142     fprintf (stderr, _("Try `%s --help' for more information.\n"),
143              program_name);
144   else
145     {
146       printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
147       printf (_("\
148 Remove the DIRECTORY(ies), if they are empty.\n\
149 \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   -v, --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     }
162   exit (status);
163 }
164
165 int
166 main (int argc, char **argv)
167 {
168   int errors = 0;
169   int optc;
170
171   program_name = argv[0];
172   setlocale (LC_ALL, "");
173   bindtextdomain (PACKAGE, LOCALEDIR);
174   textdomain (PACKAGE);
175
176   atexit (close_stdout);
177
178   empty_paths = 0;
179
180   while ((optc = getopt_long (argc, argv, "pv", longopts, NULL)) != -1)
181     {
182       switch (optc)
183         {
184         case 0:                 /* Long option. */
185           break;
186         case 'p':
187           empty_paths = 1;
188           break;
189         case IGNORE_FAIL_ON_NON_EMPTY_OPTION:
190           ignore_fail_on_non_empty = 1;
191           break;
192         case 'v':
193           verbose = 1;
194           break;
195         case_GETOPT_HELP_CHAR;
196         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
197         default:
198           usage (1);
199         }
200     }
201
202   if (optind == argc)
203     {
204       error (0, 0, _("too few arguments"));
205       usage (1);
206     }
207
208   for (; optind < argc; ++optind)
209     {
210       int fail;
211       char *dir = argv[optind];
212
213       /* Stripping slashes is harmless for rmdir;
214          if the arg is not a directory, it will fail with ENOTDIR.  */
215       strip_trailing_slashes (dir);
216
217       /* Give a diagnostic for each attempted removal if --verbose.  */
218       if (verbose)
219         error (0, 0, _("removing directory, %s"), dir);
220
221       fail = rmdir (dir);
222
223       if (fail)
224         {
225           if (ignore_fail_on_non_empty
226               && errno_rmdir_non_empty (errno))
227             continue;
228
229           error (0, errno, "%s", quote (dir));
230           errors = 1;
231         }
232       else if (empty_paths)
233         {
234           errors += remove_parents (dir);
235         }
236     }
237
238   exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
239 }