1 /* 'rm' file deletion utility for GNU.
2 Copyright (C) 1988-2022 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 <https://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", optional_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. */
109 for (int i
= 1; i
< argc
; i
++)
111 char const *arg
= argv
[i
];
114 if (arg
[0] == '-' && arg
[1] && lstat (arg
, &st
) == 0)
117 _("Try '%s ./%s' to remove the file %s.\n"),
119 quotearg_n_style (1, shell_escape_quoting_style
, arg
),
129 if (status
!= EXIT_SUCCESS
)
133 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name
);
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\
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\
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\
153 --no-preserve-root do not treat '/' specially\n\
154 --preserve-root[=all] do not remove '/' (default);\n\
155 with 'all', reject any command line argument\n\
156 on a separate device from its parent\n\
159 -r, -R, --recursive remove directories and their contents recursively\n\
160 -d, --dir remove empty directories\n\
161 -v, --verbose explain what is being done\n\
163 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
164 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
167 By default, rm does not remove directories. Use the --recursive (-r or -R)\n\
168 option to remove each listed directory, too, along with all of its contents.\n\
172 To remove a file whose name starts with a '-', for example '-foo',\n\
173 use one of these commands:\n\
178 program_name
, program_name
);
181 Note that if you use rm to remove a file, it might be possible to recover\n\
182 some of its contents, given sufficient expertise and/or time. For greater\n\
183 assurance that the contents are truly unrecoverable, consider using shred(1).\n\
185 emit_ancillary_info (PROGRAM_NAME
);
191 rm_option_init (struct rm_options
*x
)
193 x
->ignore_missing_files
= false;
194 x
->interactive
= RMI_SOMETIMES
;
195 x
->one_file_system
= false;
196 x
->remove_empty_directories
= false;
197 x
->recursive
= false;
198 x
->root_dev_ino
= NULL
;
199 x
->preserve_all_root
= false;
200 x
->stdin_tty
= isatty (STDIN_FILENO
);
203 /* Since this program exits immediately after calling 'rm', rm need not
204 expend unnecessary effort to preserve the initial working directory. */
205 x
->require_restore_cwd
= false;
209 main (int argc
, char **argv
)
211 bool preserve_root
= true;
213 bool prompt_once
= false;
216 initialize_main (&argc
, &argv
);
217 set_program_name (argv
[0]);
218 setlocale (LC_ALL
, "");
219 bindtextdomain (PACKAGE
, LOCALEDIR
);
220 textdomain (PACKAGE
);
222 atexit (close_stdin
);
226 /* Try to disable the ability to unlink a directory. */
227 priv_set_remove_linkdir ();
229 while ((c
= getopt_long (argc
, argv
, "dfirvIR", long_opts
, NULL
)) != -1)
234 x
.remove_empty_directories
= true;
238 x
.interactive
= RMI_NEVER
;
239 x
.ignore_missing_files
= true;
244 x
.interactive
= RMI_ALWAYS
;
245 x
.ignore_missing_files
= false;
250 x
.interactive
= RMI_SOMETIMES
;
251 x
.ignore_missing_files
= false;
260 case INTERACTIVE_OPTION
:
264 i
= XARGMATCH ("--interactive", optarg
, interactive_args
,
267 i
= interactive_always
;
270 case interactive_never
:
271 x
.interactive
= RMI_NEVER
;
275 case interactive_once
:
276 x
.interactive
= RMI_SOMETIMES
;
277 x
.ignore_missing_files
= false;
281 case interactive_always
:
282 x
.interactive
= RMI_ALWAYS
;
283 x
.ignore_missing_files
= false;
290 case ONE_FILE_SYSTEM
:
291 x
.one_file_system
= true;
294 case NO_PRESERVE_ROOT
:
295 if (! STREQ (argv
[optind
- 1], "--no-preserve-root"))
296 die (EXIT_FAILURE
, 0,
297 _("you may not abbreviate the --no-preserve-root option"));
298 preserve_root
= false;
304 if STREQ (optarg
, "all")
305 x
.preserve_all_root
= true;
308 die (EXIT_FAILURE
, 0,
309 _("unrecognized --preserve-root argument: %s"),
313 preserve_root
= true;
316 case PRESUME_INPUT_TTY_OPTION
:
324 case_GETOPT_HELP_CHAR
;
325 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
327 diagnose_leading_hyphen (argc
, argv
);
328 usage (EXIT_FAILURE
);
334 if (x
.ignore_missing_files
)
338 error (0, 0, _("missing operand"));
339 usage (EXIT_FAILURE
);
343 if (x
.recursive
&& preserve_root
)
345 static struct dev_ino dev_ino_buf
;
346 x
.root_dev_ino
= get_root_dev_ino (&dev_ino_buf
);
347 if (x
.root_dev_ino
== NULL
)
348 die (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
352 uintmax_t n_files
= argc
- optind
;
353 char **file
= argv
+ optind
;
355 if (prompt_once
&& (x
.recursive
|| 3 < n_files
))
359 ? ngettext ("%s: remove %"PRIuMAX
" argument recursively? ",
360 "%s: remove %"PRIuMAX
" arguments recursively? ",
361 select_plural (n_files
))
362 : ngettext ("%s: remove %"PRIuMAX
" argument? ",
363 "%s: remove %"PRIuMAX
" arguments? ",
364 select_plural (n_files
))),
365 program_name
, n_files
);
370 enum RM_status status
= rm (file
, &x
);
371 assert (VALID_STATUS (status
));
372 return status
== RM_ERROR
? EXIT_FAILURE
: EXIT_SUCCESS
;