summaryrefslogtreecommitdiff
path: root/src/rm.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rm.c')
-rw-r--r--src/rm.c324
1 files changed, 152 insertions, 172 deletions
diff --git a/src/rm.c b/src/rm.c
index 81f81ec..13a5714 100644
--- a/src/rm.c
+++ b/src/rm.c
@@ -1,10 +1,10 @@
-/* `rm' file deletion utility for GNU.
- Copyright (C) 88, 90, 91, 1994-2007 Free Software Foundation, Inc.
+/* 'rm' file deletion utility for GNU.
+ Copyright (C) 1988-2016 Free Software Foundation, Inc.
- This program is free software; you can redistribute it and/or modify
+ This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
- any later version.
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -12,35 +12,11 @@
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software Foundation,
- Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
-/* Written by Paul Rubin, David MacKenzie, and Richard Stallman.
- Reworked to use chdir and avoid recursion by Jim Meyering. */
-
-/* Implementation overview:
-
- In the `usual' case, RM saves no state for directories it is processing.
- When a removal fails (either due to an error or to an interactive `no'
- reply), the failure is noted (see description of `ht' in remove.c's
- remove_cwd_entries function) so that when/if the containing directory
- is reopened, RM doesn't try to remove the entry again.
-
- RM may delete arbitrarily deep hierarchies -- even ones in which file
- names (from root to leaf) are longer than the system-imposed maximum.
- It does this by using chdir to change to each directory in turn before
- removing the entries in that directory.
-
- RM detects directory cycles lazily. See lib/cycle-check.c.
-
- RM is careful to avoid forming full file names whenever possible.
- A full file name is formed only when it is about to be used -- e.g.
- in a diagnostic or in an interactive-mode prompt.
-
- RM minimizes the number of lstat system calls it makes. On systems
- that have valid d_type data in directory entries, RM makes only one
- lstat call per command line argument -- regardless of the depth of
- the hierarchy. */
+/* Initially written by Paul Rubin, David MacKenzie, and Richard Stallman.
+ Reworked to use chdir and avoid recursion, and later, rewritten
+ once again, to use fts, by Jim Meyering. */
#include <config.h>
#include <stdio.h>
@@ -51,21 +27,19 @@
#include "system.h"
#include "argmatch.h"
#include "error.h"
-#include "lstat.h"
-#include "quote.h"
-#include "quotearg.h"
#include "remove.h"
#include "root-dev-ino.h"
#include "yesno.h"
+#include "priv-set.h"
-/* The official name of this program (e.g., no `g' prefix). */
+/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "rm"
#define AUTHORS \
- "Paul Rubin", "David MacKenzie, Richard Stallman", "Jim Meyering"
-
-/* Name this program was run with. */
-char *program_name;
+ proper_name ("Paul Rubin"), \
+ proper_name ("David MacKenzie"), \
+ proper_name ("Richard M. Stallman"), \
+ proper_name ("Jim Meyering")
/* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
@@ -87,7 +61,6 @@ enum interactive_type
static struct option const long_opts[] =
{
- {"directory", no_argument, NULL, 'd'},
{"force", no_argument, NULL, 'f'},
{"interactive", optional_argument, NULL, INTERACTIVE_OPTION},
@@ -102,6 +75,7 @@ static struct option const long_opts[] =
{"-presume-input-tty", no_argument, NULL, PRESUME_INPUT_TTY_OPTION},
{"recursive", no_argument, NULL, 'r'},
+ {"dir", no_argument, NULL, 'd'},
{"verbose", no_argument, NULL, 'v'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
@@ -123,7 +97,7 @@ static enum interactive_type const interactive_types[] =
ARGMATCH_VERIFY (interactive_args, interactive_types);
/* Advise the user about invalid usages like "rm -foo" if the file
- "-foo" exists, assuming ARGC and ARGV are as with `main'. */
+ "-foo" exists, assuming ARGC and ARGV are as with 'main'. */
static void
diagnose_leading_hyphen (int argc, char **argv)
@@ -138,14 +112,14 @@ diagnose_leading_hyphen (int argc, char **argv)
struct stat st;
if (arg[0] == '-' && arg[1] && lstat (arg, &st) == 0)
- {
- fprintf (stderr,
- _("Try `%s ./%s' to remove the file %s.\n"),
- argv[0],
- quotearg_n_style (1, shell_quoting_style, arg),
- quote (arg));
- break;
- }
+ {
+ fprintf (stderr,
+ _("Try '%s ./%s' to remove the file %s.\n"),
+ argv[0],
+ quotearg_n_style (1, shell_escape_quoting_style, arg),
+ quoteaf (arg));
+ break;
+ }
}
}
@@ -153,23 +127,22 @@ void
usage (int status)
{
if (status != EXIT_SUCCESS)
- fprintf (stderr, _("Try `%s --help' for more information.\n"),
- program_name);
+ emit_try_help ();
else
{
- printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
+ printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
fputs (_("\
Remove (unlink) the FILE(s).\n\
\n\
- -f, --force ignore nonexistent files, never prompt\n\
+ -f, --force ignore nonexistent files and arguments, never prompt\n\
-i prompt before every removal\n\
"), stdout);
fputs (_("\
-I prompt once before removing more than three files, or\n\
- when removing recursively. Less intrusive than -i,\n\
+ when removing recursively; less intrusive than -i,\n\
while still giving protection against most mistakes\n\
--interactive[=WHEN] prompt according to WHEN: never, once (-I), or\n\
- always (-i). Without WHEN, prompt always\n\
+ always (-i); without WHEN, prompt always\n\
"), stdout);
fputs (_("\
--one-file-system when removing a hierarchy recursively, skip any\n\
@@ -177,9 +150,10 @@ Remove (unlink) the FILE(s).\n\
that of the corresponding command line argument\n\
"), stdout);
fputs (_("\
- --no-preserve-root do not treat `/' specially\n\
- --preserve-root do not remove `/' (default)\n\
+ --no-preserve-root do not treat '/' specially\n\
+ --preserve-root do not remove '/' (default)\n\
-r, -R, --recursive remove directories and their contents recursively\n\
+ -d, --dir remove empty directories\n\
-v, --verbose explain what is being done\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
@@ -191,20 +165,20 @@ option to remove each listed directory, too, along with all of its contents.\n\
"), stdout);
printf (_("\
\n\
-To remove a file whose name starts with a `-', for example `-foo',\n\
+To remove a file whose name starts with a '-', for example '-foo',\n\
use one of these commands:\n\
%s -- -foo\n\
\n\
%s ./-foo\n\
"),
- program_name, program_name);
+ program_name, program_name);
fputs (_("\
\n\
-Note that if you use rm to remove a file, it is usually possible to recover\n\
-the contents of that file. If you want more assurance that the contents are\n\
-truly unrecoverable, consider using shred.\n\
+Note that if you use rm to remove a file, it might be possible to recover\n\
+some of its contents, given sufficient expertise and/or time. For greater\n\
+assurance that the contents are truly unrecoverable, consider using shred.\n\
"), stdout);
- printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
+ emit_ancillary_info (PROGRAM_NAME);
}
exit (status);
}
@@ -215,12 +189,13 @@ rm_option_init (struct rm_options *x)
x->ignore_missing_files = false;
x->interactive = RMI_SOMETIMES;
x->one_file_system = false;
+ x->remove_empty_directories = false;
x->recursive = false;
x->root_dev_ino = NULL;
x->stdin_tty = isatty (STDIN_FILENO);
x->verbose = false;
- /* Since this program exits immediately after calling `rm', rm need not
+ /* Since this program exits immediately after calling 'rm', rm need not
expend unnecessary effort to preserve the initial working directory. */
x->require_restore_cwd = false;
}
@@ -234,141 +209,146 @@ main (int argc, char **argv)
int c;
initialize_main (&argc, &argv);
- program_name = argv[0];
+ set_program_name (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
- atexit (close_stdout);
+ atexit (close_stdin);
rm_option_init (&x);
+ /* Try to disable the ability to unlink a directory. */
+ priv_set_remove_linkdir ();
+
while ((c = getopt_long (argc, argv, "dfirvIR", long_opts, NULL)) != -1)
{
switch (c)
- {
- case 'd':
- /* Ignore this option, for backward compatibility with
- coreutils 5.92. FIXME: Some time after 2005, change this
- to report an error (or perhaps behave like FreeBSD does)
- instead of ignoring the option. */
- break;
-
- case 'f':
- x.interactive = RMI_NEVER;
- x.ignore_missing_files = true;
- prompt_once = false;
- break;
-
- case 'i':
- x.interactive = RMI_ALWAYS;
- x.ignore_missing_files = false;
- prompt_once = false;
- break;
-
- case 'I':
- x.interactive = RMI_NEVER;
- x.ignore_missing_files = false;
- prompt_once = true;
- break;
-
- case 'r':
- case 'R':
- x.recursive = true;
- break;
-
- case INTERACTIVE_OPTION:
- {
- int i;
- if (optarg)
- i = XARGMATCH ("--interactive", optarg, interactive_args,
- interactive_types);
- else
- i = interactive_always;
- switch (i)
- {
- case interactive_never:
- x.interactive = RMI_NEVER;
- prompt_once = false;
- break;
-
- case interactive_once:
- x.interactive = RMI_SOMETIMES;
- x.ignore_missing_files = false;
- prompt_once = true;
- break;
-
- case interactive_always:
- x.interactive = RMI_ALWAYS;
- x.ignore_missing_files = false;
- prompt_once = false;
- break;
- }
- break;
- }
-
- case ONE_FILE_SYSTEM:
- x.one_file_system = true;
- break;
-
- case NO_PRESERVE_ROOT:
- preserve_root = false;
- break;
-
- case PRESERVE_ROOT:
- preserve_root = true;
- break;
-
- case PRESUME_INPUT_TTY_OPTION:
- x.stdin_tty = true;
- break;
-
- case 'v':
- x.verbose = true;
- break;
-
- case_GETOPT_HELP_CHAR;
- case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
- default:
- diagnose_leading_hyphen (argc, argv);
- usage (EXIT_FAILURE);
- }
+ {
+ case 'd':
+ x.remove_empty_directories = true;
+ break;
+
+ case 'f':
+ x.interactive = RMI_NEVER;
+ x.ignore_missing_files = true;
+ prompt_once = false;
+ break;
+
+ case 'i':
+ x.interactive = RMI_ALWAYS;
+ x.ignore_missing_files = false;
+ prompt_once = false;
+ break;
+
+ case 'I':
+ x.interactive = RMI_SOMETIMES;
+ x.ignore_missing_files = false;
+ prompt_once = true;
+ break;
+
+ case 'r':
+ case 'R':
+ x.recursive = true;
+ break;
+
+ case INTERACTIVE_OPTION:
+ {
+ int i;
+ if (optarg)
+ i = XARGMATCH ("--interactive", optarg, interactive_args,
+ interactive_types);
+ else
+ i = interactive_always;
+ switch (i)
+ {
+ case interactive_never:
+ x.interactive = RMI_NEVER;
+ prompt_once = false;
+ break;
+
+ case interactive_once:
+ x.interactive = RMI_SOMETIMES;
+ x.ignore_missing_files = false;
+ prompt_once = true;
+ break;
+
+ case interactive_always:
+ x.interactive = RMI_ALWAYS;
+ x.ignore_missing_files = false;
+ prompt_once = false;
+ break;
+ }
+ break;
+ }
+
+ case ONE_FILE_SYSTEM:
+ x.one_file_system = true;
+ break;
+
+ case NO_PRESERVE_ROOT:
+ preserve_root = false;
+ break;
+
+ case PRESERVE_ROOT:
+ preserve_root = true;
+ break;
+
+ case PRESUME_INPUT_TTY_OPTION:
+ x.stdin_tty = true;
+ break;
+
+ case 'v':
+ x.verbose = true;
+ break;
+
+ case_GETOPT_HELP_CHAR;
+ case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
+ default:
+ diagnose_leading_hyphen (argc, argv);
+ usage (EXIT_FAILURE);
+ }
}
if (argc <= optind)
{
if (x.ignore_missing_files)
- exit (EXIT_SUCCESS);
+ return EXIT_SUCCESS;
else
- {
- error (0, 0, _("missing operand"));
- usage (EXIT_FAILURE);
- }
+ {
+ error (0, 0, _("missing operand"));
+ usage (EXIT_FAILURE);
+ }
}
- if (x.recursive & preserve_root)
+ if (x.recursive && preserve_root)
{
static struct dev_ino dev_ino_buf;
x.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
if (x.root_dev_ino == NULL)
- error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
- quote ("/"));
+ error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
+ quoteaf ("/"));
}
- size_t n_files = argc - optind;
- char const *const *file = (char const *const *) argv + optind;
+ uintmax_t n_files = argc - optind;
+ char **file = argv + optind;
if (prompt_once && (x.recursive || 3 < n_files))
{
fprintf (stderr,
- (x.recursive
- ? _("%s: remove all arguments recursively? ")
- : _("%s: remove all arguments? ")),
- program_name);
+ (x.recursive
+ ? ngettext ("%s: remove %"PRIuMAX" argument recursively? ",
+ "%s: remove %"PRIuMAX" arguments recursively? ",
+ select_plural (n_files))
+ : ngettext ("%s: remove %"PRIuMAX" argument? ",
+ "%s: remove %"PRIuMAX" arguments? ",
+ select_plural (n_files))),
+ program_name, n_files);
if (!yesno ())
- exit (EXIT_SUCCESS);
+ return EXIT_SUCCESS;
}
- enum RM_status status = rm (n_files, file, &x);
+
+ enum RM_status status = rm (file, &x);
assert (VALID_STATUS (status));
- exit (status == RM_ERROR ? EXIT_FAILURE : EXIT_SUCCESS);
+ return status == RM_ERROR ? EXIT_FAILURE : EXIT_SUCCESS;
}