Sanitize environment.
[coreutils.git] / src / rmdir.c
blob8facbddf8ee2a156e5b4c9d1965a351f5d3ecad8
1 /* rmdir -- remove directories
2 Copyright (C) 90, 91, 1995-2002, 2004 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 "dirname.h"
32 #include "error.h"
33 #include "quote.h"
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME "rmdir"
38 #define AUTHORS "David MacKenzie"
40 #ifndef EEXIST
41 # define EEXIST 0
42 #endif
44 #ifndef ENOTEMPTY
45 # define ENOTEMPTY 0
46 #endif
48 /* The name this program was run with. */
49 char *program_name;
51 /* If true, remove empty parent directories. */
52 static bool empty_paths;
54 /* If true, don't treat failure to remove a nonempty directory
55 as an error. */
56 static bool ignore_fail_on_non_empty;
58 /* If true, output a diagnostic for every directory processed. */
59 static bool verbose;
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
65 IGNORE_FAIL_ON_NON_EMPTY_OPTION = CHAR_MAX + 1
68 static struct option const longopts[] =
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},
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}
83 /* Return true if ERROR_NUMBER is one of the values associated
84 with a failed rmdir due to non-empty target directory. */
86 static bool
87 errno_rmdir_non_empty (int error_number)
89 return (error_number == RMDIR_ERRNO_NOT_EMPTY);
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 Return true if successful. */
97 static bool
98 remove_parents (char *path)
100 char *slash;
101 bool ok = true;
103 strip_trailing_slashes (path);
104 while (1)
106 slash = strrchr (path, '/');
107 if (slash == NULL)
108 break;
109 /* Remove any characters after the slash, skipping any extra
110 slashes in a row. */
111 while (slash > path && *slash == '/')
112 --slash;
113 slash[1] = 0;
115 /* Give a diagnostic for each attempted removal if --verbose. */
116 if (verbose)
117 error (0, 0, _("removing directory, %s"), path);
119 ok = (rmdir (path) == 0);
121 if (!ok)
123 /* Stop quietly if --ignore-fail-on-non-empty. */
124 if (ignore_fail_on_non_empty
125 && errno_rmdir_non_empty (errno))
127 ok = true;
129 else
131 error (0, errno, "%s", quote (path));
133 break;
136 return ok;
139 void
140 usage (int status)
142 if (status != EXIT_SUCCESS)
143 fprintf (stderr, _("Try `%s --help' for more information.\n"),
144 program_name);
145 else
147 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
148 fputs (_("\
149 Remove the DIRECTORY(ies), if they are empty.\n\
151 --ignore-fail-on-non-empty\n\
152 ignore each failure that is solely because a directory\n\
153 is non-empty\n\
154 "), stdout);
155 fputs (_("\
156 -p, --parents remove DIRECTORY, then try to remove each directory\n\
157 component of that path name. E.g., `rmdir -p a/b/c' is\n\
158 similar to `rmdir a/b/c a/b a'.\n\
159 -v, --verbose output a diagnostic for every directory processed\n\
160 "), stdout);
161 fputs (HELP_OPTION_DESCRIPTION, stdout);
162 fputs (VERSION_OPTION_DESCRIPTION, stdout);
163 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
165 exit (status);
169 main (int argc, char **argv)
171 bool ok = true;
172 int optc;
174 initialize_main (&argc, &argv);
175 program_name = argv[0];
176 setlocale (LC_ALL, "");
177 bindtextdomain (PACKAGE, LOCALEDIR);
178 textdomain (PACKAGE);
180 atexit (close_stdout);
182 empty_paths = false;
184 while ((optc = getopt_long (argc, argv, "pv", longopts, NULL)) != -1)
186 switch (optc)
188 case 'p':
189 empty_paths = true;
190 break;
191 case IGNORE_FAIL_ON_NON_EMPTY_OPTION:
192 ignore_fail_on_non_empty = true;
193 break;
194 case 'v':
195 verbose = true;
196 break;
197 case_GETOPT_HELP_CHAR;
198 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
199 default:
200 usage (EXIT_FAILURE);
204 if (optind == argc)
206 error (0, 0, _("missing operand"));
207 usage (EXIT_FAILURE);
210 for (; optind < argc; ++optind)
212 char *dir = argv[optind];
214 /* Give a diagnostic for each attempted removal if --verbose. */
215 if (verbose)
216 error (0, 0, _("removing directory, %s"), dir);
218 if (rmdir (dir) != 0)
220 if (ignore_fail_on_non_empty
221 && errno_rmdir_non_empty (errno))
222 continue;
224 error (0, errno, "%s", quote (dir));
225 ok = false;
227 else if (empty_paths)
229 ok &= remove_parents (dir);
233 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);