ls: --color now highlights hard linked files, too
[coreutils/bo.git] / src / rm.c
blob8fecfdd5f29683a27d730e56499f626863a1b741
1 /* `rm' file deletion utility for GNU.
2 Copyright (C) 88, 90, 91, 1994-2008 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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>. */
17 /* Written by Paul Rubin, David MacKenzie, and Richard Stallman.
18 Reworked to use chdir and avoid recursion by Jim Meyering. */
20 /* Implementation overview:
22 In the `usual' case, RM saves no state for directories it is processing.
23 When a removal fails (either due to an error or to an interactive `no'
24 reply), the failure is noted (see description of `ht' in remove.c's
25 remove_cwd_entries function) so that when/if the containing directory
26 is reopened, RM doesn't try to remove the entry again.
28 RM may delete arbitrarily deep hierarchies -- even ones in which file
29 names (from root to leaf) are longer than the system-imposed maximum.
30 It does this by using chdir to change to each directory in turn before
31 removing the entries in that directory.
33 RM detects directory cycles lazily. See lib/cycle-check.c.
35 RM is careful to avoid forming full file names whenever possible.
36 A full file name is formed only when it is about to be used -- e.g.
37 in a diagnostic or in an interactive-mode prompt.
39 RM minimizes the number of lstat system calls it makes. On systems
40 that have valid d_type data in directory entries, RM makes only one
41 lstat call per command line argument -- regardless of the depth of
42 the hierarchy. */
44 #include <config.h>
45 #include <stdio.h>
46 #include <getopt.h>
47 #include <sys/types.h>
48 #include <assert.h>
50 #include "system.h"
51 #include "argmatch.h"
52 #include "error.h"
53 #include "quote.h"
54 #include "quotearg.h"
55 #include "remove.h"
56 #include "root-dev-ino.h"
57 #include "yesno.h"
59 /* The official name of this program (e.g., no `g' prefix). */
60 #define PROGRAM_NAME "rm"
62 #define AUTHORS \
63 proper_name ("Paul Rubin"), \
64 proper_name ("David MacKenzie"), \
65 proper_name ("Richard M. Stallman"), \
66 proper_name ("Jim Meyering")
68 /* For long options that have no equivalent short option, use a
69 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
70 enum
72 INTERACTIVE_OPTION = CHAR_MAX + 1,
73 ONE_FILE_SYSTEM,
74 NO_PRESERVE_ROOT,
75 PRESERVE_ROOT,
76 PRESUME_INPUT_TTY_OPTION
79 enum interactive_type
81 interactive_never, /* 0: no option or --interactive=never */
82 interactive_once, /* 1: -I or --interactive=once */
83 interactive_always /* 2: default, -i or --interactive=always */
86 static struct option const long_opts[] =
88 {"directory", no_argument, NULL, 'd'},
89 {"force", no_argument, NULL, 'f'},
90 {"interactive", optional_argument, NULL, INTERACTIVE_OPTION},
92 {"one-file-system", no_argument, NULL, ONE_FILE_SYSTEM},
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 --one-file-system when removing a hierarchy recursively, skip any\n\
174 directory that is on a file system different from\n\
175 that of the corresponding command line argument\n\
176 "), stdout);
177 fputs (_("\
178 --no-preserve-root do not treat `/' specially\n\
179 --preserve-root do not remove `/' (default)\n\
180 -r, -R, --recursive remove directories and their contents recursively\n\
181 -v, --verbose explain what is being done\n\
182 "), stdout);
183 fputs (HELP_OPTION_DESCRIPTION, stdout);
184 fputs (VERSION_OPTION_DESCRIPTION, stdout);
185 fputs (_("\
187 By default, rm does not remove directories. Use the --recursive (-r or -R)\n\
188 option to remove each listed directory, too, along with all of its contents.\n\
189 "), stdout);
190 printf (_("\
192 To remove a file whose name starts with a `-', for example `-foo',\n\
193 use one of these commands:\n\
194 %s -- -foo\n\
196 %s ./-foo\n\
198 program_name, program_name);
199 fputs (_("\
201 Note that if you use rm to remove a file, it is usually possible to recover\n\
202 the contents of that file. If you want more assurance that the contents are\n\
203 truly unrecoverable, consider using shred.\n\
204 "), stdout);
205 emit_bug_reporting_address ();
207 exit (status);
210 static void
211 rm_option_init (struct rm_options *x)
213 x->ignore_missing_files = false;
214 x->interactive = RMI_SOMETIMES;
215 x->one_file_system = false;
216 x->recursive = false;
217 x->root_dev_ino = NULL;
218 x->stdin_tty = isatty (STDIN_FILENO);
219 x->verbose = false;
221 /* Since this program exits immediately after calling `rm', rm need not
222 expend unnecessary effort to preserve the initial working directory. */
223 x->require_restore_cwd = false;
227 main (int argc, char **argv)
229 bool preserve_root = true;
230 struct rm_options x;
231 bool prompt_once = false;
232 int c;
234 initialize_main (&argc, &argv);
235 set_program_name (argv[0]);
236 setlocale (LC_ALL, "");
237 bindtextdomain (PACKAGE, LOCALEDIR);
238 textdomain (PACKAGE);
240 atexit (close_stdin);
242 rm_option_init (&x);
244 while ((c = getopt_long (argc, argv, "dfirvIR", long_opts, NULL)) != -1)
246 switch (c)
248 case 'd':
249 /* Ignore this option, for backward compatibility with
250 coreutils 5.92. FIXME: Some time after 2005, change this
251 to report an error (or perhaps behave like FreeBSD does)
252 instead of ignoring the option. */
253 break;
255 case 'f':
256 x.interactive = RMI_NEVER;
257 x.ignore_missing_files = true;
258 prompt_once = false;
259 break;
261 case 'i':
262 x.interactive = RMI_ALWAYS;
263 x.ignore_missing_files = false;
264 prompt_once = false;
265 break;
267 case 'I':
268 x.interactive = RMI_NEVER;
269 x.ignore_missing_files = false;
270 prompt_once = true;
271 break;
273 case 'r':
274 case 'R':
275 x.recursive = true;
276 break;
278 case INTERACTIVE_OPTION:
280 int i;
281 if (optarg)
282 i = XARGMATCH ("--interactive", optarg, interactive_args,
283 interactive_types);
284 else
285 i = interactive_always;
286 switch (i)
288 case interactive_never:
289 x.interactive = RMI_NEVER;
290 prompt_once = false;
291 break;
293 case interactive_once:
294 x.interactive = RMI_SOMETIMES;
295 x.ignore_missing_files = false;
296 prompt_once = true;
297 break;
299 case interactive_always:
300 x.interactive = RMI_ALWAYS;
301 x.ignore_missing_files = false;
302 prompt_once = false;
303 break;
305 break;
308 case ONE_FILE_SYSTEM:
309 x.one_file_system = true;
310 break;
312 case NO_PRESERVE_ROOT:
313 preserve_root = false;
314 break;
316 case PRESERVE_ROOT:
317 preserve_root = true;
318 break;
320 case PRESUME_INPUT_TTY_OPTION:
321 x.stdin_tty = true;
322 break;
324 case 'v':
325 x.verbose = true;
326 break;
328 case_GETOPT_HELP_CHAR;
329 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
330 default:
331 diagnose_leading_hyphen (argc, argv);
332 usage (EXIT_FAILURE);
336 if (argc <= optind)
338 if (x.ignore_missing_files)
339 exit (EXIT_SUCCESS);
340 else
342 error (0, 0, _("missing operand"));
343 usage (EXIT_FAILURE);
347 if (x.recursive & preserve_root)
349 static struct dev_ino dev_ino_buf;
350 x.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
351 if (x.root_dev_ino == NULL)
352 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
353 quote ("/"));
356 size_t n_files = argc - optind;
357 char const *const *file = (char const *const *) argv + optind;
359 if (prompt_once && (x.recursive || 3 < n_files))
361 fprintf (stderr,
362 (x.recursive
363 ? _("%s: remove all arguments recursively? ")
364 : _("%s: remove all arguments? ")),
365 program_name);
366 if (!yesno ())
367 exit (EXIT_SUCCESS);
369 enum RM_status status = rm (n_files, file, &x);
370 assert (VALID_STATUS (status));
371 exit (status == RM_ERROR ? EXIT_FAILURE : EXIT_SUCCESS);