* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / rm.c
blob28e09ce1253a500d987ac273d36ef9f4c25f4d3f
1 /* `rm' file deletion utility for GNU.
2 Copyright (C) 88, 90, 91, 1994-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Paul Rubin, David MacKenzie, and Richard Stallman.
19 Reworked to use chdir and avoid recursion by Jim Meyering. */
21 /* Implementation overview:
23 In the `usual' case, RM saves no state for directories it is processing.
24 When a removal fails (either due to an error or to an interactive `no'
25 reply), the failure is noted (see description of `ht' in remove.c's
26 remove_cwd_entries function) so that when/if the containing directory
27 is reopened, RM doesn't try to remove the entry again.
29 RM may delete arbitrarily deep hierarchies -- even ones in which file
30 names (from root to leaf) are longer than the system-imposed maximum.
31 It does this by using chdir to change to each directory in turn before
32 removing the entries in that directory.
34 RM detects directory cycles lazily. See lib/cycle-check.c.
36 RM is careful to avoid forming full file names whenever possible.
37 A full file name is formed only when it is about to be used -- e.g.
38 in a diagnostic or in an interactive-mode prompt.
40 RM minimizes the number of lstat system calls it makes. On systems
41 that have valid d_type data in directory entries, RM makes only one
42 lstat call per command line argument -- regardless of the depth of
43 the hierarchy. */
45 #include <config.h>
46 #include <stdio.h>
47 #include <getopt.h>
48 #include <sys/types.h>
49 #include <assert.h>
51 #include "system.h"
52 #include "argmatch.h"
53 #include "error.h"
54 #include "lstat.h"
55 #include "quote.h"
56 #include "quotearg.h"
57 #include "remove.h"
58 #include "root-dev-ino.h"
59 #include "yesno.h"
61 /* The official name of this program (e.g., no `g' prefix). */
62 #define PROGRAM_NAME "rm"
64 #define AUTHORS \
65 "Paul Rubin", "David MacKenzie, Richard Stallman", "Jim Meyering"
67 /* Name this program was run with. */
68 char *program_name;
70 /* For long options that have no equivalent short option, use a
71 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
72 enum
74 INTERACTIVE_OPTION = CHAR_MAX + 1,
75 NO_PRESERVE_ROOT,
76 PRESERVE_ROOT,
77 PRESUME_INPUT_TTY_OPTION
80 enum interactive_type
82 interactive_never, /* 0: no option or --interactive=never */
83 interactive_once, /* 1: -I or --interactive=once */
84 interactive_always /* 2: default, -i or --interactive=always */
87 static struct option const long_opts[] =
89 {"directory", no_argument, NULL, 'd'},
90 {"force", no_argument, NULL, 'f'},
91 {"interactive", optional_argument, NULL, INTERACTIVE_OPTION},
93 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
94 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
96 /* This is solely for testing. Do not document. */
97 /* It is relatively difficult to ensure that there is a tty on stdin.
98 Since rm acts differently depending on that, without this option,
99 it'd be harder to test the parts of rm that depend on that setting. */
100 {"-presume-input-tty", no_argument, NULL, PRESUME_INPUT_TTY_OPTION},
102 {"recursive", no_argument, NULL, 'r'},
103 {"verbose", no_argument, NULL, 'v'},
104 {GETOPT_HELP_OPTION_DECL},
105 {GETOPT_VERSION_OPTION_DECL},
106 {NULL, 0, NULL, 0}
109 static char const *const interactive_args[] =
111 "never", "no", "none",
112 "once",
113 "always", "yes", NULL
115 static enum interactive_type const interactive_types[] =
117 interactive_never, interactive_never, interactive_never,
118 interactive_once,
119 interactive_always, interactive_always
121 ARGMATCH_VERIFY (interactive_args, interactive_types);
123 /* Advise the user about invalid usages like "rm -foo" if the file
124 "-foo" exists, assuming ARGC and ARGV are as with `main'. */
126 static void
127 diagnose_leading_hyphen (int argc, char **argv)
129 /* OPTIND is unreliable, so iterate through the arguments looking
130 for a file name that looks like an option. */
131 int i;
133 for (i = 1; i < argc; i++)
135 char const *arg = argv[i];
136 struct stat st;
138 if (arg[0] == '-' && arg[1] && lstat (arg, &st) == 0)
140 fprintf (stderr,
141 _("Try `%s ./%s' to remove the file %s.\n"),
142 argv[0],
143 quotearg_n_style (1, shell_quoting_style, arg),
144 quote (arg));
145 break;
150 void
151 usage (int status)
153 if (status != EXIT_SUCCESS)
154 fprintf (stderr, _("Try `%s --help' for more information.\n"),
155 program_name);
156 else
158 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
159 fputs (_("\
160 Remove (unlink) the FILE(s).\n\
162 -f, --force ignore nonexistent files, never prompt\n\
163 -i prompt before every removal\n\
164 "), stdout);
165 fputs (_("\
166 -I prompt once before removing more than three files, or\n\
167 when removing recursively. Less intrusive than -i,\n\
168 while still giving protection against most mistakes\n\
169 --interactive[=WHEN] prompt according to WHEN: never, once (-I), or\n\
170 always (-i). Without WHEN, prompt always\n\
171 "), stdout);
172 fputs (_("\
173 --no-preserve-root do not treat `/' specially\n\
174 --preserve-root do not remove `/' (default)\n\
175 -r, -R, --recursive remove directories and their contents recursively\n\
176 -v, --verbose explain what is being done\n\
177 "), stdout);
178 fputs (HELP_OPTION_DESCRIPTION, stdout);
179 fputs (VERSION_OPTION_DESCRIPTION, stdout);
180 fputs (_("\
182 By default, rm does not remove directories. Use the --recursive (-r or -R)\n\
183 option to remove each listed directory, too, along with all of its contents.\n\
184 "), stdout);
185 printf (_("\
187 To remove a file whose name starts with a `-', for example `-foo',\n\
188 use one of these commands:\n\
189 %s -- -foo\n\
191 %s ./-foo\n\
193 program_name, program_name);
194 fputs (_("\
196 Note that if you use rm to remove a file, it is usually possible to recover\n\
197 the contents of that file. If you want more assurance that the contents are\n\
198 truly unrecoverable, consider using shred.\n\
199 "), stdout);
200 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
202 exit (status);
205 static void
206 rm_option_init (struct rm_options *x)
208 x->ignore_missing_files = false;
209 x->interactive = false;
210 x->recursive = false;
211 x->root_dev_ino = NULL;
212 x->stdin_tty = isatty (STDIN_FILENO);
213 x->verbose = false;
215 /* Since this program exits immediately after calling `rm', rm need not
216 expend unnecessary effort to preserve the initial working directory. */
217 x->require_restore_cwd = false;
221 main (int argc, char **argv)
223 bool preserve_root = true;
224 struct rm_options x;
225 bool prompt_once = false;
226 int c;
228 initialize_main (&argc, &argv);
229 program_name = argv[0];
230 setlocale (LC_ALL, "");
231 bindtextdomain (PACKAGE, LOCALEDIR);
232 textdomain (PACKAGE);
234 atexit (close_stdout);
236 rm_option_init (&x);
238 while ((c = getopt_long (argc, argv, "dfirvIR", long_opts, NULL)) != -1)
240 switch (c)
242 case 'd':
243 /* Ignore this option, for backward compatibility with
244 coreutils 5.92. Some time after 2005, we'll change this
245 to report an error (or perhaps behave like FreeBSD does)
246 instead of ignoring the option. */
247 break;
249 case 'f':
250 x.interactive = false;
251 x.ignore_missing_files = true;
252 prompt_once = false;
253 break;
255 case 'i':
256 x.interactive = true;
257 x.ignore_missing_files = false;
258 prompt_once = false;
259 break;
261 case 'I':
262 x.interactive = false;
263 x.ignore_missing_files = false;
264 prompt_once = true;
265 break;
267 case 'r':
268 case 'R':
269 x.recursive = true;
270 break;
272 case INTERACTIVE_OPTION:
274 int i;
275 if (optarg)
276 i = XARGMATCH ("--interactive", optarg, interactive_args,
277 interactive_types);
278 else
279 i = interactive_always;
280 switch (i)
282 case interactive_never:
283 x.interactive = false;
284 prompt_once = false;
285 break;
287 case interactive_once:
288 x.interactive = false;
289 x.ignore_missing_files = false;
290 prompt_once = true;
291 break;
293 case interactive_always:
294 x.interactive = true;
295 x.ignore_missing_files = false;
296 prompt_once = false;
297 break;
299 break;
302 case NO_PRESERVE_ROOT:
303 preserve_root = false;
304 break;
306 case PRESERVE_ROOT:
307 preserve_root = true;
308 break;
310 case PRESUME_INPUT_TTY_OPTION:
311 x.stdin_tty = true;
312 break;
314 case 'v':
315 x.verbose = true;
316 break;
318 case_GETOPT_HELP_CHAR;
319 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
320 default:
321 diagnose_leading_hyphen (argc, argv);
322 usage (EXIT_FAILURE);
326 if (argc <= optind)
328 if (x.ignore_missing_files)
329 exit (EXIT_SUCCESS);
330 else
332 error (0, 0, _("missing operand"));
333 usage (EXIT_FAILURE);
337 if (x.recursive & preserve_root)
339 static struct dev_ino dev_ino_buf;
340 x.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
341 if (x.root_dev_ino == NULL)
342 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
343 quote ("/"));
347 size_t n_files = argc - optind;
348 char const *const *file = (char const *const *) argv + optind;
350 if (prompt_once && (x.recursive || 3 < n_files))
352 fprintf (stderr,
353 (x.recursive
354 ? _("%s: remove all arguments recursively? ")
355 : _("%s: remove all arguments? ")),
356 program_name);
357 if (!yesno ())
358 exit (EXIT_SUCCESS);
360 enum RM_status status = rm (n_files, file, &x);
361 assert (VALID_STATUS (status));
362 exit (status == RM_ERROR ? EXIT_FAILURE : EXIT_SUCCESS);