Tizen 2.0 Release
[external/tizen-coreutils.git] / src / rmdir.c
1 /* rmdir -- remove directories
2
3    Copyright (C) 90, 91, 1995-2002, 2004, 2005, 2006 Free Software
4    Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Options:
21    -p, --parent         Remove any parent dirs that are explicitly mentioned
22                         in an argument, if they become empty after the
23                         argument file is removed.
24
25    David MacKenzie <djm@ai.mit.edu>  */
26
27 #include <config.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <sys/types.h>
31
32 #include "system.h"
33 #include "error.h"
34 #include "quotearg.h"
35
36 /* The official name of this program (e.g., no `g' prefix).  */
37 #define PROGRAM_NAME "rmdir"
38
39 #define AUTHORS "David MacKenzie"
40
41 /* The name this program was run with. */
42 char *program_name;
43
44 /* If true, remove empty parent directories.  */
45 static bool remove_empty_parents;
46
47 /* If true, don't treat failure to remove a nonempty directory
48    as an error.  */
49 static bool ignore_fail_on_non_empty;
50
51 /* If true, output a diagnostic for every directory processed.  */
52 static bool verbose;
53
54 /* For long options that have no equivalent short option, use a
55    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
56 enum
57 {
58   IGNORE_FAIL_ON_NON_EMPTY_OPTION = CHAR_MAX + 1
59 };
60
61 static struct option const longopts[] =
62 {
63   /* Don't name this `--force' because it's not close enough in meaning
64      to e.g. rm's -f option.  */
65   {"ignore-fail-on-non-empty", no_argument, NULL,
66    IGNORE_FAIL_ON_NON_EMPTY_OPTION},
67
68   {"path", no_argument, NULL, 'p'},  /* Deprecated.  */
69   {"parents", no_argument, NULL, 'p'},
70   {"verbose", no_argument, NULL, 'v'},
71   {GETOPT_HELP_OPTION_DECL},
72   {GETOPT_VERSION_OPTION_DECL},
73   {NULL, 0, NULL, 0}
74 };
75
76 /* Return true if ERROR_NUMBER is one of the values associated
77    with a failed rmdir due to non-empty target directory.  */
78
79 static bool
80 errno_rmdir_non_empty (int error_number)
81 {
82   return (error_number == RMDIR_ERRNO_NOT_EMPTY);
83 }
84
85 /* Remove any empty parent directories of DIR.
86    If DIR contains slash characters, at least one of them
87    (beginning with the rightmost) is replaced with a NUL byte.
88    Return true if successful.  */
89
90 static bool
91 remove_parents (char *dir)
92 {
93   char *slash;
94   bool ok = true;
95
96   strip_trailing_slashes (dir);
97   while (1)
98     {
99       slash = strrchr (dir, '/');
100       if (slash == NULL)
101         break;
102       /* Remove any characters after the slash, skipping any extra
103          slashes in a row. */
104       while (slash > dir && *slash == '/')
105         --slash;
106       slash[1] = 0;
107
108       /* Give a diagnostic for each attempted removal if --verbose.  */
109       if (verbose)
110         error (0, 0, _("removing directory, %s"), dir);
111
112       ok = (rmdir (dir) == 0);
113
114       if (!ok)
115         {
116           /* Stop quietly if --ignore-fail-on-non-empty. */
117           if (ignore_fail_on_non_empty
118               && errno_rmdir_non_empty (errno))
119             {
120               ok = true;
121             }
122           else
123             {
124               error (0, errno, "%s", quotearg_colon (dir));
125             }
126           break;
127         }
128     }
129   return ok;
130 }
131
132 void
133 usage (int status)
134 {
135   if (status != EXIT_SUCCESS)
136     fprintf (stderr, _("Try `%s --help' for more information.\n"),
137              program_name);
138   else
139     {
140       printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
141       fputs (_("\
142 Remove the DIRECTORY(ies), if they are empty.\n\
143 \n\
144       --ignore-fail-on-non-empty\n\
145                   ignore each failure that is solely because a directory\n\
146                   is non-empty\n\
147 "), stdout);
148       fputs (_("\
149   -p, --parents   Remove DIRECTORY and its ancestors.  E.g., `rmdir -p a/b/c' is\n\
150                   similar to `rmdir a/b/c a/b a'.\n\
151   -v, --verbose   output a diagnostic for every directory processed\n\
152 "), stdout);
153       fputs (HELP_OPTION_DESCRIPTION, stdout);
154       fputs (VERSION_OPTION_DESCRIPTION, stdout);
155       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
156     }
157   exit (status);
158 }
159
160 int
161 main (int argc, char **argv)
162 {
163   bool ok = true;
164   int optc;
165
166   initialize_main (&argc, &argv);
167   program_name = argv[0];
168   setlocale (LC_ALL, "");
169   bindtextdomain (PACKAGE, LOCALEDIR);
170   textdomain (PACKAGE);
171
172   atexit (close_stdout);
173
174   remove_empty_parents = false;
175
176   while ((optc = getopt_long (argc, argv, "pv", longopts, NULL)) != -1)
177     {
178       switch (optc)
179         {
180         case 'p':
181           remove_empty_parents = true;
182           break;
183         case IGNORE_FAIL_ON_NON_EMPTY_OPTION:
184           ignore_fail_on_non_empty = true;
185           break;
186         case 'v':
187           verbose = true;
188           break;
189         case_GETOPT_HELP_CHAR;
190         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
191         default:
192           usage (EXIT_FAILURE);
193         }
194     }
195
196   if (optind == argc)
197     {
198       error (0, 0, _("missing operand"));
199       usage (EXIT_FAILURE);
200     }
201
202   for (; optind < argc; ++optind)
203     {
204       char *dir = argv[optind];
205
206       /* Give a diagnostic for each attempted removal if --verbose.  */
207       if (verbose)
208         error (0, 0, _("removing directory, %s"), dir);
209
210       if (rmdir (dir) != 0)
211         {
212           if (ignore_fail_on_non_empty
213               && errno_rmdir_non_empty (errno))
214             continue;
215
216           error (0, errno, "%s", quotearg_colon (dir));
217           ok = false;
218         }
219       else if (remove_empty_parents)
220         {
221           ok &= remove_parents (dir);
222         }
223     }
224
225   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
226 }