1 /* 'rm' file deletion utility for GNU.
2 Copyright (C) 1988-2024 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>
30 #include "root-dev-ino.h"
34 /* The official name of this program (e.g., no 'g' prefix). */
35 #define PROGRAM_NAME "rm"
38 proper_name ("Paul Rubin"), \
39 proper_name ("David MacKenzie"), \
40 proper_name ("Richard M. Stallman"), \
41 proper_name ("Jim Meyering")
43 /* For long options that have no equivalent short option, use a
44 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
47 INTERACTIVE_OPTION
= CHAR_MAX
+ 1,
51 PRESUME_INPUT_TTY_OPTION
56 interactive_never
, /* 0: no option or --interactive=never */
57 interactive_once
, /* 1: -I or --interactive=once */
58 interactive_always
/* 2: default, -i or --interactive=always */
61 static struct option
const long_opts
[] =
63 {"force", no_argument
, nullptr, 'f'},
64 {"interactive", optional_argument
, nullptr, INTERACTIVE_OPTION
},
66 {"one-file-system", no_argument
, nullptr, ONE_FILE_SYSTEM
},
67 {"no-preserve-root", no_argument
, nullptr, NO_PRESERVE_ROOT
},
68 {"preserve-root", optional_argument
, nullptr, PRESERVE_ROOT
},
70 /* This is solely for testing. Do not document. */
71 /* It is relatively difficult to ensure that there is a tty on stdin.
72 Since rm acts differently depending on that, without this option,
73 it'd be harder to test the parts of rm that depend on that setting. */
74 {"-presume-input-tty", no_argument
, nullptr, PRESUME_INPUT_TTY_OPTION
},
76 {"recursive", no_argument
, nullptr, 'r'},
77 {"dir", no_argument
, nullptr, 'd'},
78 {"verbose", no_argument
, nullptr, 'v'},
79 {GETOPT_HELP_OPTION_DECL
},
80 {GETOPT_VERSION_OPTION_DECL
},
81 {nullptr, 0, nullptr, 0}
84 static char const *const interactive_args
[] =
86 "never", "no", "none",
88 "always", "yes", nullptr
90 static enum interactive_type
const interactive_types
[] =
92 interactive_never
, interactive_never
, interactive_never
,
94 interactive_always
, interactive_always
96 ARGMATCH_VERIFY (interactive_args
, interactive_types
);
98 /* Advise the user about invalid usages like "rm -foo" if the file
99 "-foo" exists, assuming ARGC and ARGV are as with 'main'. */
102 diagnose_leading_hyphen (int argc
, char **argv
)
104 /* OPTIND is unreliable, so iterate through the arguments looking
105 for a file name that looks like an option. */
107 for (int i
= 1; i
< argc
; i
++)
109 char const *arg
= argv
[i
];
112 if (arg
[0] == '-' && arg
[1] && lstat (arg
, &st
) == 0)
115 _("Try '%s ./%s' to remove the file %s.\n"),
117 quotearg_n_style (1, shell_escape_quoting_style
, arg
),
127 if (status
!= EXIT_SUCCESS
)
131 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name
);
133 Remove (unlink) the FILE(s).\n\
135 -f, --force ignore nonexistent files and arguments, never prompt\n\
136 -i prompt before every removal\n\
139 -I prompt once before removing more than three files, or\n\
140 when removing recursively; less intrusive than -i,\n\
141 while still giving protection against most mistakes\n\
142 --interactive[=WHEN] prompt according to WHEN: never, once (-I), or\n\
143 always (-i); without WHEN, prompt always\n\
146 --one-file-system when removing a hierarchy recursively, skip any\n\
147 directory that is on a file system different from\n\
148 that of the corresponding command line argument\n\
151 --no-preserve-root do not treat '/' specially\n\
152 --preserve-root[=all] do not remove '/' (default);\n\
153 with 'all', reject any command line argument\n\
154 on a separate device from its parent\n\
157 -r, -R, --recursive remove directories and their contents recursively\n\
158 -d, --dir remove empty directories\n\
159 -v, --verbose explain what is being done\n\
161 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
162 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
165 By default, rm does not remove directories. Use the --recursive (-r or -R)\n\
166 option to remove each listed directory, too, along with all of its contents.\n\
170 Any attempt to remove a file whose last file name component is '.' or '..'\n\
171 is rejected with a diagnostic.\n\
175 To remove a file whose name starts with a '-', for example '-foo',\n\
176 use one of these commands:\n\
181 program_name
, program_name
);
184 If you use rm to remove a file, it might be possible to recover\n\
185 some of its contents, given sufficient expertise and/or time. For greater\n\
186 assurance that the contents are unrecoverable, consider using shred(1).\n\
188 emit_ancillary_info (PROGRAM_NAME
);
194 rm_option_init (struct rm_options
*x
)
196 x
->ignore_missing_files
= false;
197 x
->interactive
= RMI_SOMETIMES
;
198 x
->one_file_system
= false;
199 x
->remove_empty_directories
= false;
200 x
->recursive
= false;
201 x
->root_dev_ino
= nullptr;
202 x
->preserve_all_root
= false;
203 x
->stdin_tty
= isatty (STDIN_FILENO
);
206 /* Since this program exits immediately after calling 'rm', rm need not
207 expend unnecessary effort to preserve the initial working directory. */
208 x
->require_restore_cwd
= false;
212 main (int argc
, char **argv
)
214 bool preserve_root
= true;
216 bool prompt_once
= false;
219 initialize_main (&argc
, &argv
);
220 set_program_name (argv
[0]);
221 setlocale (LC_ALL
, "");
222 bindtextdomain (PACKAGE
, LOCALEDIR
);
223 textdomain (PACKAGE
);
225 atexit (close_stdin
);
229 /* Try to disable the ability to unlink a directory. */
230 priv_set_remove_linkdir ();
232 while ((c
= getopt_long (argc
, argv
, "dfirvIR", long_opts
, nullptr)) != -1)
237 x
.remove_empty_directories
= true;
241 x
.interactive
= RMI_NEVER
;
242 x
.ignore_missing_files
= true;
247 x
.interactive
= RMI_ALWAYS
;
248 x
.ignore_missing_files
= false;
253 x
.interactive
= RMI_SOMETIMES
;
254 x
.ignore_missing_files
= false;
263 case INTERACTIVE_OPTION
:
267 i
= XARGMATCH ("--interactive", optarg
, interactive_args
,
270 i
= interactive_always
;
273 case interactive_never
:
274 x
.interactive
= RMI_NEVER
;
278 case interactive_once
:
279 x
.interactive
= RMI_SOMETIMES
;
280 x
.ignore_missing_files
= false;
284 case interactive_always
:
285 x
.interactive
= RMI_ALWAYS
;
286 x
.ignore_missing_files
= false;
293 case ONE_FILE_SYSTEM
:
294 x
.one_file_system
= true;
297 case NO_PRESERVE_ROOT
:
298 if (! STREQ (argv
[optind
- 1], "--no-preserve-root"))
299 error (EXIT_FAILURE
, 0,
300 _("you may not abbreviate the --no-preserve-root option"));
301 preserve_root
= false;
307 if STREQ (optarg
, "all")
308 x
.preserve_all_root
= true;
310 error (EXIT_FAILURE
, 0,
311 _("unrecognized --preserve-root argument: %s"),
314 preserve_root
= true;
317 case PRESUME_INPUT_TTY_OPTION
:
325 case_GETOPT_HELP_CHAR
;
326 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
328 diagnose_leading_hyphen (argc
, argv
);
329 usage (EXIT_FAILURE
);
335 if (x
.ignore_missing_files
)
339 error (0, 0, _("missing operand"));
340 usage (EXIT_FAILURE
);
344 if (x
.recursive
&& preserve_root
)
346 static struct dev_ino dev_ino_buf
;
347 x
.root_dev_ino
= get_root_dev_ino (&dev_ino_buf
);
348 if (x
.root_dev_ino
== nullptr)
349 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
353 uintmax_t n_files
= argc
- optind
;
354 char **file
= argv
+ optind
;
356 if (prompt_once
&& (x
.recursive
|| 3 < n_files
))
360 ? ngettext ("%s: remove %ju argument recursively? ",
361 "%s: remove %ju arguments recursively? ",
362 select_plural (n_files
))
363 : ngettext ("%s: remove %ju argument? ",
364 "%s: remove %ju arguments? ",
365 select_plural (n_files
))),
366 program_name
, n_files
);
371 enum RM_status status
= rm (file
, &x
);
372 affirm (VALID_STATUS (status
));
373 return status
== RM_ERROR
? EXIT_FAILURE
: EXIT_SUCCESS
;