b2sum: a new checksum utility with md5sum like interface
[coreutils.git] / src / rm.c
blob102bfe69e4528c540e5915de57ede2e288e50560
1 /* 'rm' file deletion utility for GNU.
2 Copyright (C) 1988-2016 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 /* Initially written by Paul Rubin, David MacKenzie, and Richard Stallman.
18 Reworked to use chdir and avoid recursion, and later, rewritten
19 once again, to use fts, by Jim Meyering. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <sys/types.h>
25 #include <assert.h>
27 #include "system.h"
28 #include "argmatch.h"
29 #include "die.h"
30 #include "error.h"
31 #include "remove.h"
32 #include "root-dev-ino.h"
33 #include "yesno.h"
34 #include "priv-set.h"
36 /* The official name of this program (e.g., no 'g' prefix). */
37 #define PROGRAM_NAME "rm"
39 #define AUTHORS \
40 proper_name ("Paul Rubin"), \
41 proper_name ("David MacKenzie"), \
42 proper_name ("Richard M. Stallman"), \
43 proper_name ("Jim Meyering")
45 /* For long options that have no equivalent short option, use a
46 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
47 enum
49 INTERACTIVE_OPTION = CHAR_MAX + 1,
50 ONE_FILE_SYSTEM,
51 NO_PRESERVE_ROOT,
52 PRESERVE_ROOT,
53 PRESUME_INPUT_TTY_OPTION
56 enum interactive_type
58 interactive_never, /* 0: no option or --interactive=never */
59 interactive_once, /* 1: -I or --interactive=once */
60 interactive_always /* 2: default, -i or --interactive=always */
63 static struct option const long_opts[] =
65 {"force", no_argument, NULL, 'f'},
66 {"interactive", optional_argument, NULL, INTERACTIVE_OPTION},
68 {"one-file-system", no_argument, NULL, ONE_FILE_SYSTEM},
69 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
70 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
72 /* This is solely for testing. Do not document. */
73 /* It is relatively difficult to ensure that there is a tty on stdin.
74 Since rm acts differently depending on that, without this option,
75 it'd be harder to test the parts of rm that depend on that setting. */
76 {"-presume-input-tty", no_argument, NULL, PRESUME_INPUT_TTY_OPTION},
78 {"recursive", no_argument, NULL, 'r'},
79 {"dir", no_argument, NULL, 'd'},
80 {"verbose", no_argument, NULL, 'v'},
81 {GETOPT_HELP_OPTION_DECL},
82 {GETOPT_VERSION_OPTION_DECL},
83 {NULL, 0, NULL, 0}
86 static char const *const interactive_args[] =
88 "never", "no", "none",
89 "once",
90 "always", "yes", NULL
92 static enum interactive_type const interactive_types[] =
94 interactive_never, interactive_never, interactive_never,
95 interactive_once,
96 interactive_always, interactive_always
98 ARGMATCH_VERIFY (interactive_args, interactive_types);
100 /* Advise the user about invalid usages like "rm -foo" if the file
101 "-foo" exists, assuming ARGC and ARGV are as with 'main'. */
103 static void
104 diagnose_leading_hyphen (int argc, char **argv)
106 /* OPTIND is unreliable, so iterate through the arguments looking
107 for a file name that looks like an option. */
108 int i;
110 for (i = 1; i < argc; i++)
112 char const *arg = argv[i];
113 struct stat st;
115 if (arg[0] == '-' && arg[1] && lstat (arg, &st) == 0)
117 fprintf (stderr,
118 _("Try '%s ./%s' to remove the file %s.\n"),
119 argv[0],
120 quotearg_n_style (1, shell_escape_quoting_style, arg),
121 quoteaf (arg));
122 break;
127 void
128 usage (int status)
130 if (status != EXIT_SUCCESS)
131 emit_try_help ();
132 else
134 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
135 fputs (_("\
136 Remove (unlink) the FILE(s).\n\
138 -f, --force ignore nonexistent files and arguments, never prompt\n\
139 -i prompt before every removal\n\
140 "), stdout);
141 fputs (_("\
142 -I prompt once before removing more than three files, or\n\
143 when removing recursively; less intrusive than -i,\n\
144 while still giving protection against most mistakes\n\
145 --interactive[=WHEN] prompt according to WHEN: never, once (-I), or\n\
146 always (-i); without WHEN, prompt always\n\
147 "), stdout);
148 fputs (_("\
149 --one-file-system when removing a hierarchy recursively, skip any\n\
150 directory that is on a file system different from\n\
151 that of the corresponding command line argument\n\
152 "), stdout);
153 fputs (_("\
154 --no-preserve-root do not treat '/' specially\n\
155 --preserve-root do not remove '/' (default)\n\
156 -r, -R, --recursive remove directories and their contents recursively\n\
157 -d, --dir remove empty directories\n\
158 -v, --verbose explain what is being done\n\
159 "), stdout);
160 fputs (HELP_OPTION_DESCRIPTION, stdout);
161 fputs (VERSION_OPTION_DESCRIPTION, stdout);
162 fputs (_("\
164 By default, rm does not remove directories. Use the --recursive (-r or -R)\n\
165 option to remove each listed directory, too, along with all of its contents.\n\
166 "), stdout);
167 printf (_("\
169 To remove a file whose name starts with a '-', for example '-foo',\n\
170 use one of these commands:\n\
171 %s -- -foo\n\
173 %s ./-foo\n\
175 program_name, program_name);
176 fputs (_("\
178 Note that if you use rm to remove a file, it might be possible to recover\n\
179 some of its contents, given sufficient expertise and/or time. For greater\n\
180 assurance that the contents are truly unrecoverable, consider using shred.\n\
181 "), stdout);
182 emit_ancillary_info (PROGRAM_NAME);
184 exit (status);
187 static void
188 rm_option_init (struct rm_options *x)
190 x->ignore_missing_files = false;
191 x->interactive = RMI_SOMETIMES;
192 x->one_file_system = false;
193 x->remove_empty_directories = false;
194 x->recursive = false;
195 x->root_dev_ino = NULL;
196 x->stdin_tty = isatty (STDIN_FILENO);
197 x->verbose = false;
199 /* Since this program exits immediately after calling 'rm', rm need not
200 expend unnecessary effort to preserve the initial working directory. */
201 x->require_restore_cwd = false;
205 main (int argc, char **argv)
207 bool preserve_root = true;
208 struct rm_options x;
209 bool prompt_once = false;
210 int c;
212 initialize_main (&argc, &argv);
213 set_program_name (argv[0]);
214 setlocale (LC_ALL, "");
215 bindtextdomain (PACKAGE, LOCALEDIR);
216 textdomain (PACKAGE);
218 atexit (close_stdin);
220 rm_option_init (&x);
222 /* Try to disable the ability to unlink a directory. */
223 priv_set_remove_linkdir ();
225 while ((c = getopt_long (argc, argv, "dfirvIR", long_opts, NULL)) != -1)
227 switch (c)
229 case 'd':
230 x.remove_empty_directories = true;
231 break;
233 case 'f':
234 x.interactive = RMI_NEVER;
235 x.ignore_missing_files = true;
236 prompt_once = false;
237 break;
239 case 'i':
240 x.interactive = RMI_ALWAYS;
241 x.ignore_missing_files = false;
242 prompt_once = false;
243 break;
245 case 'I':
246 x.interactive = RMI_SOMETIMES;
247 x.ignore_missing_files = false;
248 prompt_once = true;
249 break;
251 case 'r':
252 case 'R':
253 x.recursive = true;
254 break;
256 case INTERACTIVE_OPTION:
258 int i;
259 if (optarg)
260 i = XARGMATCH ("--interactive", optarg, interactive_args,
261 interactive_types);
262 else
263 i = interactive_always;
264 switch (i)
266 case interactive_never:
267 x.interactive = RMI_NEVER;
268 prompt_once = false;
269 break;
271 case interactive_once:
272 x.interactive = RMI_SOMETIMES;
273 x.ignore_missing_files = false;
274 prompt_once = true;
275 break;
277 case interactive_always:
278 x.interactive = RMI_ALWAYS;
279 x.ignore_missing_files = false;
280 prompt_once = false;
281 break;
283 break;
286 case ONE_FILE_SYSTEM:
287 x.one_file_system = true;
288 break;
290 case NO_PRESERVE_ROOT:
291 if (! STREQ (argv[optind - 1], "--no-preserve-root"))
292 die (EXIT_FAILURE, 0,
293 _("you may not abbreviate the --no-preserve-root option"));
294 preserve_root = false;
295 break;
297 case PRESERVE_ROOT:
298 preserve_root = true;
299 break;
301 case PRESUME_INPUT_TTY_OPTION:
302 x.stdin_tty = true;
303 break;
305 case 'v':
306 x.verbose = true;
307 break;
309 case_GETOPT_HELP_CHAR;
310 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
311 default:
312 diagnose_leading_hyphen (argc, argv);
313 usage (EXIT_FAILURE);
317 if (argc <= optind)
319 if (x.ignore_missing_files)
320 return EXIT_SUCCESS;
321 else
323 error (0, 0, _("missing operand"));
324 usage (EXIT_FAILURE);
328 if (x.recursive && preserve_root)
330 static struct dev_ino dev_ino_buf;
331 x.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
332 if (x.root_dev_ino == NULL)
333 die (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
334 quoteaf ("/"));
337 uintmax_t n_files = argc - optind;
338 char **file = argv + optind;
340 if (prompt_once && (x.recursive || 3 < n_files))
342 fprintf (stderr,
343 (x.recursive
344 ? ngettext ("%s: remove %"PRIuMAX" argument recursively? ",
345 "%s: remove %"PRIuMAX" arguments recursively? ",
346 select_plural (n_files))
347 : ngettext ("%s: remove %"PRIuMAX" argument? ",
348 "%s: remove %"PRIuMAX" arguments? ",
349 select_plural (n_files))),
350 program_name, n_files);
351 if (!yesno ())
352 return EXIT_SUCCESS;
355 enum RM_status status = rm (file, &x);
356 assert (VALID_STATUS (status));
357 return status == RM_ERROR ? EXIT_FAILURE : EXIT_SUCCESS;