*** empty log message ***
[coreutils.git] / src / rmdir.c
blob01727b8b4d247013346c750c584a31651328f27b
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 static struct option const longopts[] =
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, CHAR_MAX + 1},
67 {"path", no_argument, NULL, 'p'},
68 {"parents", no_argument, NULL, 'p'},
69 {"verbose", no_argument, NULL, 14},
70 {GETOPT_HELP_OPTION_DECL},
71 {GETOPT_VERSION_OPTION_DECL},
72 {NULL, 0, NULL, 0}
75 /* Remove any empty parent directories of PATH.
76 If PATH contains slash characters, at least one of them
77 (beginning with the rightmost) is replaced with a NUL byte. */
79 static int
80 remove_parents (char *path)
82 char *slash;
83 int fail = 0;
85 while (1)
87 slash = strrchr (path, '/');
88 if (slash == NULL)
89 break;
90 /* Remove any characters after the slash, skipping any extra
91 slashes in a row. */
92 while (slash > path && *slash == '/')
93 --slash;
94 slash[1] = 0;
96 /* Give a diagnostic for each attempted removal if --verbose. */
97 if (verbose)
98 error (0, errno, _("removing directory, %s"), path);
100 fail = rmdir (path);
102 if (fail)
104 /* Give a diagnostic and set fail if not --ignore. */
105 if (!ignore_fail_on_non_empty || errno != ENOTEMPTY)
107 error (0, errno, "%s", path);
108 fail = 1;
110 break;
113 return fail;
116 void
117 usage (int status)
119 if (status != 0)
120 fprintf (stderr, _("Try `%s --help' for more information.\n"),
121 program_name);
122 else
124 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
125 printf (_("\
126 Remove the DIRECTORY(ies), if they are empty.\n\
128 --ignore-fail-on-non-empty\n\
129 ignore each failure that is solely because the\n\
130 directory is non-empty\n\
131 -p, --parents remove explicit parent directories if being emptied\n\
132 --verbose output a diagnostic for every directory processed\n\
133 --help display this help and exit\n\
134 --version output version information and exit\n\
135 "));
136 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
137 close_stdout ();
139 exit (status);
143 main (int argc, char **argv)
145 int errors = 0;
146 int optc;
148 program_name = argv[0];
149 setlocale (LC_ALL, "");
150 bindtextdomain (PACKAGE, LOCALEDIR);
151 textdomain (PACKAGE);
153 empty_paths = 0;
155 while ((optc = getopt_long (argc, argv, "p", longopts, NULL)) != -1)
157 switch (optc)
159 case 0: /* Long option. */
160 break;
161 case 'p':
162 empty_paths = 1;
163 break;
164 case CHAR_MAX + 1:
165 ignore_fail_on_non_empty = 1;
166 break;
167 case 14:
168 verbose = 1;
169 break;
170 case_GETOPT_HELP_CHAR;
171 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
172 default:
173 usage (1);
177 if (optind == argc)
179 error (0, 0, _("too few arguments"));
180 usage (1);
183 for (; optind < argc; ++optind)
185 int fail;
186 char *dir = argv[optind];
188 /* Stripping slashes is harmless for rmdir;
189 if the arg is not a directory, it will fail with ENOTDIR. */
190 strip_trailing_slashes (dir);
192 /* Give a diagnostic for each attempted removal if --verbose. */
193 if (verbose)
194 error (0, errno, _("removing directory, %s"), dir);
196 fail = rmdir (dir);
198 if (fail)
200 if (ignore_fail_on_non_empty
201 && (errno == ENOTEMPTY || errno == EEXIST))
202 continue;
204 error (0, errno, "%s", dir);
205 errors = 1;
207 else if (empty_paths)
209 errors += remove_parents (dir);
213 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);