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. */
24 #include <sys/types.h>
32 #include "root-dev-ino.h"
36 /* The official name of this program (e.g., no 'g' prefix). */
37 #define PROGRAM_NAME "rm"
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. */
49 INTERACTIVE_OPTION
= CHAR_MAX
+ 1,
53 PRESUME_INPUT_TTY_OPTION
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
},
86 static char const *const interactive_args
[] =
88 "never", "no", "none",
92 static enum interactive_type
const interactive_types
[] =
94 interactive_never
, interactive_never
, interactive_never
,
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'. */
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. */
110 for (i
= 1; i
< argc
; i
++)
112 char const *arg
= argv
[i
];
115 if (arg
[0] == '-' && arg
[1] && lstat (arg
, &st
) == 0)
118 _("Try '%s ./%s' to remove the file %s.\n"),
120 quotearg_n_style (1, shell_escape_quoting_style
, arg
),
130 if (status
!= EXIT_SUCCESS
)
134 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name
);
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\
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\
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\
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\
160 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
161 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
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\
169 To remove a file whose name starts with a '-', for example '-foo',\n\
170 use one of these commands:\n\
175 program_name
, program_name
);
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\
182 emit_ancillary_info (PROGRAM_NAME
);
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
);
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;
209 bool prompt_once
= false;
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
);
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)
230 x
.remove_empty_directories
= true;
234 x
.interactive
= RMI_NEVER
;
235 x
.ignore_missing_files
= true;
240 x
.interactive
= RMI_ALWAYS
;
241 x
.ignore_missing_files
= false;
246 x
.interactive
= RMI_SOMETIMES
;
247 x
.ignore_missing_files
= false;
256 case INTERACTIVE_OPTION
:
260 i
= XARGMATCH ("--interactive", optarg
, interactive_args
,
263 i
= interactive_always
;
266 case interactive_never
:
267 x
.interactive
= RMI_NEVER
;
271 case interactive_once
:
272 x
.interactive
= RMI_SOMETIMES
;
273 x
.ignore_missing_files
= false;
277 case interactive_always
:
278 x
.interactive
= RMI_ALWAYS
;
279 x
.ignore_missing_files
= false;
286 case ONE_FILE_SYSTEM
:
287 x
.one_file_system
= true;
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;
298 preserve_root
= true;
301 case PRESUME_INPUT_TTY_OPTION
:
309 case_GETOPT_HELP_CHAR
;
310 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
312 diagnose_leading_hyphen (argc
, argv
);
313 usage (EXIT_FAILURE
);
319 if (x
.ignore_missing_files
)
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"),
337 uintmax_t n_files
= argc
- optind
;
338 char **file
= argv
+ optind
;
340 if (prompt_once
&& (x
.recursive
|| 3 < n_files
))
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
);
355 enum RM_status status
= rm (file
, &x
);
356 assert (VALID_STATUS (status
));
357 return status
== RM_ERROR
? EXIT_FAILURE
: EXIT_SUCCESS
;