Most .c files (AUTHORS): Revert the WRITTEN_BY/AUTHORS change
[platform/upstream/coreutils.git] / src / rmdir.c
1 /* rmdir -- remove directories
2    Copyright (C) 90, 91, 1995-2002 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 "dirname.h"
32 #include "error.h"
33 #include "quote.h"
34
35 /* The official name of this program (e.g., no `g' prefix).  */
36 #define PROGRAM_NAME "rmdir"
37
38 #define AUTHORS "David MacKenzie"
39
40 #ifndef EEXIST
41 # define EEXIST 0
42 #endif
43
44 #ifndef ENOTEMPTY
45 # define ENOTEMPTY 0
46 #endif
47
48 /* The name this program was run with. */
49 char *program_name;
50
51 /* If nonzero, remove empty parent directories. */
52 static int empty_paths;
53
54 /* If nonzero, don't treat failure to remove a nonempty directory
55    as an error.  */
56 static int ignore_fail_on_non_empty;
57
58 /* If nonzero, output a diagnostic for every directory processed.  */
59 static int verbose;
60
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
64 {
65   IGNORE_FAIL_ON_NON_EMPTY_OPTION = CHAR_MAX + 1
66 };
67
68 static struct option const longopts[] =
69 {
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},
74
75   {"path", no_argument, NULL, 'p'},
76   {"parents", no_argument, NULL, 'p'},
77   {"verbose", no_argument, NULL, 'v'},
78   {GETOPT_HELP_OPTION_DECL},
79   {GETOPT_VERSION_OPTION_DECL},
80   {NULL, 0, NULL, 0}
81 };
82
83 /* Return nonzero if ERROR_NUMBER is one of the values associated
84    with a failed rmdir due to non-empty target directory.  */
85
86 static int
87 errno_rmdir_non_empty (int error_number)
88 {
89   return (error_number == RMDIR_ERRNO_NOT_EMPTY);
90 }
91
92 /* Remove any empty parent directories of PATH.
93    If PATH contains slash characters, at least one of them
94    (beginning with the rightmost) is replaced with a NUL byte.  */
95
96 static int
97 remove_parents (char *path)
98 {
99   char *slash;
100   int fail = 0;
101
102   strip_trailing_slashes (path);
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       fputs (_("\
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 "), stdout);
154       fputs (_("\
155   -p, --parents   remove DIRECTORY, then try to remove each directory\n\
156                   component of that path name.  E.g., `rmdir -p a/b/c' is\n\
157                   similar to `rmdir a/b/c a/b a'.\n\
158   -v, --verbose   output a diagnostic for every directory processed\n\
159 "), stdout);
160       fputs (HELP_OPTION_DESCRIPTION, stdout);
161       fputs (VERSION_OPTION_DESCRIPTION, stdout);
162       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
163     }
164   exit (status);
165 }
166
167 int
168 main (int argc, char **argv)
169 {
170   int errors = 0;
171   int optc;
172
173   initialize_main (&argc, &argv);
174   program_name = argv[0];
175   setlocale (LC_ALL, "");
176   bindtextdomain (PACKAGE, LOCALEDIR);
177   textdomain (PACKAGE);
178
179   atexit (close_stdout);
180
181   empty_paths = 0;
182
183   while ((optc = getopt_long (argc, argv, "pv", longopts, NULL)) != -1)
184     {
185       switch (optc)
186         {
187         case 0:                 /* Long option. */
188           break;
189         case 'p':
190           empty_paths = 1;
191           break;
192         case IGNORE_FAIL_ON_NON_EMPTY_OPTION:
193           ignore_fail_on_non_empty = 1;
194           break;
195         case 'v':
196           verbose = 1;
197           break;
198         case_GETOPT_HELP_CHAR;
199         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
200         default:
201           usage (EXIT_FAILURE);
202         }
203     }
204
205   if (optind == argc)
206     {
207       error (0, 0, _("too few arguments"));
208       usage (EXIT_FAILURE);
209     }
210
211   for (; optind < argc; ++optind)
212     {
213       int fail;
214       char *dir = argv[optind];
215
216       /* Give a diagnostic for each attempted removal if --verbose.  */
217       if (verbose)
218         error (0, 0, _("removing directory, %s"), dir);
219
220       fail = rmdir (dir);
221
222       if (fail)
223         {
224           if (ignore_fail_on_non_empty
225               && errno_rmdir_non_empty (errno))
226             continue;
227
228           error (0, errno, "%s", quote (dir));
229           errors = 1;
230         }
231       else if (empty_paths)
232         {
233           errors += remove_parents (dir);
234         }
235     }
236
237   exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
238 }