*** empty log message ***
[coreutils.git] / src / rmdir.c
blobbacc911f0312ec95c3d7ce5b24e37ff07b48557f
1 /* rmdir -- remove directories
2 Copyright (C) 90, 91, 1995-2001 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"
32 #include "quote.h"
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "rmdir"
37 #define AUTHORS "David MacKenzie"
39 #ifndef EEXIST
40 # define EEXIST 0
41 #endif
43 #ifndef ENOTEMPTY
44 # define ENOTEMPTY 0
45 #endif
47 /* The name this program was run with. */
48 char *program_name;
50 /* If nonzero, remove empty parent directories. */
51 static int empty_paths;
53 /* If nonzero, don't treat failure to remove a nonempty directory
54 as an error. */
55 static int ignore_fail_on_non_empty;
57 /* If nonzero, output a diagnostic for every directory processed. */
58 static int verbose;
60 /* For long options that have no equivalent short option, use a
61 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
62 enum
64 IGNORE_FAIL_ON_NON_EMPTY_OPTION = CHAR_MAX + 1
67 static struct option const longopts[] =
69 /* Don't name this `--force' because it's not close enough in meaning
70 to e.g. rm's -f option. */
71 {"ignore-fail-on-non-empty", no_argument, NULL,
72 IGNORE_FAIL_ON_NON_EMPTY_OPTION},
74 {"path", no_argument, NULL, 'p'},
75 {"parents", no_argument, NULL, 'p'},
76 {"verbose", no_argument, NULL, 'v'},
77 {GETOPT_HELP_OPTION_DECL},
78 {GETOPT_VERSION_OPTION_DECL},
79 {NULL, 0, NULL, 0}
82 /* Return nonzero if ERROR_NUMBER is one of the values associated
83 with a failed rmdir due to non-empty target directory. */
85 static int
86 errno_rmdir_non_empty (int error_number)
88 return (error_number == RMDIR_ERRNO_NOT_EMPTY);
91 /* Remove any empty parent directories of PATH.
92 If PATH contains slash characters, at least one of them
93 (beginning with the rightmost) is replaced with a NUL byte. */
95 static int
96 remove_parents (char *path)
98 char *slash;
99 int fail = 0;
101 while (1)
103 slash = strrchr (path, '/');
104 if (slash == NULL)
105 break;
106 /* Remove any characters after the slash, skipping any extra
107 slashes in a row. */
108 while (slash > path && *slash == '/')
109 --slash;
110 slash[1] = 0;
112 /* Give a diagnostic for each attempted removal if --verbose. */
113 if (verbose)
114 error (0, 0, _("removing directory, %s"), path);
116 fail = rmdir (path);
118 if (fail)
120 /* Stop quietly if --ignore-fail-on-non-empty. */
121 if (ignore_fail_on_non_empty
122 && errno_rmdir_non_empty (errno))
124 fail = 0;
126 else
128 error (0, errno, "%s", quote (path));
130 break;
133 return fail;
136 void
137 usage (int status)
139 if (status != 0)
140 fprintf (stderr, _("Try `%s --help' for more information.\n"),
141 program_name);
142 else
144 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
145 fputs (_("\
146 Remove the DIRECTORY(ies), if they are empty.\n\
148 --ignore-fail-on-non-empty\n\
149 ignore each failure that is solely because a directory\n\
150 is non-empty\n\
151 "), stdout);
152 fputs (_("\
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 "), stdout);
158 fputs (HELP_OPTION_DESCRIPTION, stdout);
159 fputs (VERSION_OPTION_DESCRIPTION, stdout);
160 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
162 exit (status);
166 main (int argc, char **argv)
168 int errors = 0;
169 int optc;
171 program_name = argv[0];
172 setlocale (LC_ALL, "");
173 bindtextdomain (PACKAGE, LOCALEDIR);
174 textdomain (PACKAGE);
176 atexit (close_stdout);
178 empty_paths = 0;
180 while ((optc = getopt_long (argc, argv, "pv", longopts, NULL)) != -1)
182 switch (optc)
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);
202 if (optind == argc)
204 error (0, 0, _("too few arguments"));
205 usage (1);
208 for (; optind < argc; ++optind)
210 int fail;
211 char *dir = argv[optind];
213 /* Give a diagnostic for each attempted removal if --verbose. */
214 if (verbose)
215 error (0, 0, _("removing directory, %s"), dir);
217 fail = rmdir (dir);
219 if (fail)
221 if (ignore_fail_on_non_empty
222 && errno_rmdir_non_empty (errno))
223 continue;
225 error (0, errno, "%s", quote (dir));
226 errors = 1;
228 else if (empty_paths)
230 errors += remove_parents (dir);
234 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);