tests: remove a non portable localtime test
[coreutils.git] / src / rm.c
blob13a5714c332af8c36425802406d97a3ba86055e3
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 "error.h"
30 #include "remove.h"
31 #include "root-dev-ino.h"
32 #include "yesno.h"
33 #include "priv-set.h"
35 /* The official name of this program (e.g., no 'g' prefix). */
36 #define PROGRAM_NAME "rm"
38 #define AUTHORS \
39 proper_name ("Paul Rubin"), \
40 proper_name ("David MacKenzie"), \
41 proper_name ("Richard M. Stallman"), \
42 proper_name ("Jim Meyering")
44 /* For long options that have no equivalent short option, use a
45 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
46 enum
48 INTERACTIVE_OPTION = CHAR_MAX + 1,
49 ONE_FILE_SYSTEM,
50 NO_PRESERVE_ROOT,
51 PRESERVE_ROOT,
52 PRESUME_INPUT_TTY_OPTION
55 enum interactive_type
57 interactive_never, /* 0: no option or --interactive=never */
58 interactive_once, /* 1: -I or --interactive=once */
59 interactive_always /* 2: default, -i or --interactive=always */
62 static struct option const long_opts[] =
64 {"force", no_argument, NULL, 'f'},
65 {"interactive", optional_argument, NULL, INTERACTIVE_OPTION},
67 {"one-file-system", no_argument, NULL, ONE_FILE_SYSTEM},
68 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
69 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
71 /* This is solely for testing. Do not document. */
72 /* It is relatively difficult to ensure that there is a tty on stdin.
73 Since rm acts differently depending on that, without this option,
74 it'd be harder to test the parts of rm that depend on that setting. */
75 {"-presume-input-tty", no_argument, NULL, PRESUME_INPUT_TTY_OPTION},
77 {"recursive", no_argument, NULL, 'r'},
78 {"dir", no_argument, NULL, 'd'},
79 {"verbose", no_argument, NULL, 'v'},
80 {GETOPT_HELP_OPTION_DECL},
81 {GETOPT_VERSION_OPTION_DECL},
82 {NULL, 0, NULL, 0}
85 static char const *const interactive_args[] =
87 "never", "no", "none",
88 "once",
89 "always", "yes", NULL
91 static enum interactive_type const interactive_types[] =
93 interactive_never, interactive_never, interactive_never,
94 interactive_once,
95 interactive_always, interactive_always
97 ARGMATCH_VERIFY (interactive_args, interactive_types);
99 /* Advise the user about invalid usages like "rm -foo" if the file
100 "-foo" exists, assuming ARGC and ARGV are as with 'main'. */
102 static void
103 diagnose_leading_hyphen (int argc, char **argv)
105 /* OPTIND is unreliable, so iterate through the arguments looking
106 for a file name that looks like an option. */
107 int i;
109 for (i = 1; i < argc; i++)
111 char const *arg = argv[i];
112 struct stat st;
114 if (arg[0] == '-' && arg[1] && lstat (arg, &st) == 0)
116 fprintf (stderr,
117 _("Try '%s ./%s' to remove the file %s.\n"),
118 argv[0],
119 quotearg_n_style (1, shell_escape_quoting_style, arg),
120 quoteaf (arg));
121 break;
126 void
127 usage (int status)
129 if (status != EXIT_SUCCESS)
130 emit_try_help ();
131 else
133 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
134 fputs (_("\
135 Remove (unlink) the FILE(s).\n\
137 -f, --force ignore nonexistent files and arguments, never prompt\n\
138 -i prompt before every removal\n\
139 "), stdout);
140 fputs (_("\
141 -I prompt once before removing more than three files, or\n\
142 when removing recursively; less intrusive than -i,\n\
143 while still giving protection against most mistakes\n\
144 --interactive[=WHEN] prompt according to WHEN: never, once (-I), or\n\
145 always (-i); without WHEN, prompt always\n\
146 "), stdout);
147 fputs (_("\
148 --one-file-system when removing a hierarchy recursively, skip any\n\
149 directory that is on a file system different from\n\
150 that of the corresponding command line argument\n\
151 "), stdout);
152 fputs (_("\
153 --no-preserve-root do not treat '/' specially\n\
154 --preserve-root do not remove '/' (default)\n\
155 -r, -R, --recursive remove directories and their contents recursively\n\
156 -d, --dir remove empty directories\n\
157 -v, --verbose explain what is being done\n\
158 "), stdout);
159 fputs (HELP_OPTION_DESCRIPTION, stdout);
160 fputs (VERSION_OPTION_DESCRIPTION, stdout);
161 fputs (_("\
163 By default, rm does not remove directories. Use the --recursive (-r or -R)\n\
164 option to remove each listed directory, too, along with all of its contents.\n\
165 "), stdout);
166 printf (_("\
168 To remove a file whose name starts with a '-', for example '-foo',\n\
169 use one of these commands:\n\
170 %s -- -foo\n\
172 %s ./-foo\n\
174 program_name, program_name);
175 fputs (_("\
177 Note that if you use rm to remove a file, it might be possible to recover\n\
178 some of its contents, given sufficient expertise and/or time. For greater\n\
179 assurance that the contents are truly unrecoverable, consider using shred.\n\
180 "), stdout);
181 emit_ancillary_info (PROGRAM_NAME);
183 exit (status);
186 static void
187 rm_option_init (struct rm_options *x)
189 x->ignore_missing_files = false;
190 x->interactive = RMI_SOMETIMES;
191 x->one_file_system = false;
192 x->remove_empty_directories = false;
193 x->recursive = false;
194 x->root_dev_ino = NULL;
195 x->stdin_tty = isatty (STDIN_FILENO);
196 x->verbose = false;
198 /* Since this program exits immediately after calling 'rm', rm need not
199 expend unnecessary effort to preserve the initial working directory. */
200 x->require_restore_cwd = false;
204 main (int argc, char **argv)
206 bool preserve_root = true;
207 struct rm_options x;
208 bool prompt_once = false;
209 int c;
211 initialize_main (&argc, &argv);
212 set_program_name (argv[0]);
213 setlocale (LC_ALL, "");
214 bindtextdomain (PACKAGE, LOCALEDIR);
215 textdomain (PACKAGE);
217 atexit (close_stdin);
219 rm_option_init (&x);
221 /* Try to disable the ability to unlink a directory. */
222 priv_set_remove_linkdir ();
224 while ((c = getopt_long (argc, argv, "dfirvIR", long_opts, NULL)) != -1)
226 switch (c)
228 case 'd':
229 x.remove_empty_directories = true;
230 break;
232 case 'f':
233 x.interactive = RMI_NEVER;
234 x.ignore_missing_files = true;
235 prompt_once = false;
236 break;
238 case 'i':
239 x.interactive = RMI_ALWAYS;
240 x.ignore_missing_files = false;
241 prompt_once = false;
242 break;
244 case 'I':
245 x.interactive = RMI_SOMETIMES;
246 x.ignore_missing_files = false;
247 prompt_once = true;
248 break;
250 case 'r':
251 case 'R':
252 x.recursive = true;
253 break;
255 case INTERACTIVE_OPTION:
257 int i;
258 if (optarg)
259 i = XARGMATCH ("--interactive", optarg, interactive_args,
260 interactive_types);
261 else
262 i = interactive_always;
263 switch (i)
265 case interactive_never:
266 x.interactive = RMI_NEVER;
267 prompt_once = false;
268 break;
270 case interactive_once:
271 x.interactive = RMI_SOMETIMES;
272 x.ignore_missing_files = false;
273 prompt_once = true;
274 break;
276 case interactive_always:
277 x.interactive = RMI_ALWAYS;
278 x.ignore_missing_files = false;
279 prompt_once = false;
280 break;
282 break;
285 case ONE_FILE_SYSTEM:
286 x.one_file_system = true;
287 break;
289 case NO_PRESERVE_ROOT:
290 preserve_root = false;
291 break;
293 case PRESERVE_ROOT:
294 preserve_root = true;
295 break;
297 case PRESUME_INPUT_TTY_OPTION:
298 x.stdin_tty = true;
299 break;
301 case 'v':
302 x.verbose = true;
303 break;
305 case_GETOPT_HELP_CHAR;
306 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
307 default:
308 diagnose_leading_hyphen (argc, argv);
309 usage (EXIT_FAILURE);
313 if (argc <= optind)
315 if (x.ignore_missing_files)
316 return EXIT_SUCCESS;
317 else
319 error (0, 0, _("missing operand"));
320 usage (EXIT_FAILURE);
324 if (x.recursive && preserve_root)
326 static struct dev_ino dev_ino_buf;
327 x.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
328 if (x.root_dev_ino == NULL)
329 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
330 quoteaf ("/"));
333 uintmax_t n_files = argc - optind;
334 char **file = argv + optind;
336 if (prompt_once && (x.recursive || 3 < n_files))
338 fprintf (stderr,
339 (x.recursive
340 ? ngettext ("%s: remove %"PRIuMAX" argument recursively? ",
341 "%s: remove %"PRIuMAX" arguments recursively? ",
342 select_plural (n_files))
343 : ngettext ("%s: remove %"PRIuMAX" argument? ",
344 "%s: remove %"PRIuMAX" arguments? ",
345 select_plural (n_files))),
346 program_name, n_files);
347 if (!yesno ())
348 return EXIT_SUCCESS;
351 enum RM_status status = rm (file, &x);
352 assert (VALID_STATUS (status));
353 return status == RM_ERROR ? EXIT_FAILURE : EXIT_SUCCESS;