.
[coreutils.git] / src / rm.c
blob61791b51552f5a1003e12bbf33741e2e8473205e
1 /* `rm' file deletion utility for GNU.
2 Copyright (C) 88, 90, 91, 1994-2003 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 2, or (at your option)
7 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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Rubin, David MacKenzie, and Richard Stallman.
19 Reworked to use chdir and avoid recursion by Jim Meyering. */
21 /* Implementation overview:
23 In the `usual' case, RM saves no state for directories it is processing.
24 When a removal fails (either due to an error or to an interactive `no'
25 reply), the failure is noted (see description of `ht' in remove.c's
26 remove_cwd_entries function) so that when/if the containing directory
27 is reopened, RM doesn't try to remove the entry again.
29 RM may delete arbitrarily deep hierarchies -- even ones in which file
30 names (from root to leaf) are longer than the system-imposed maximum.
31 It does this by using chdir to change to each directory in turn before
32 removing the entries in that directory.
34 RM detects directory cycles lazily. See lib/cycle-check.c.
36 RM is careful to avoid forming full file names whenever possible.
37 A full file name is formed only when it is about to be used -- e.g.
38 in a diagnostic or in an interactive-mode prompt.
40 RM minimizes the number of lstat system calls it makes. On systems
41 that have valid d_type data in directory entries, RM makes only one
42 lstat call per command line argument -- regardless of the depth of
43 the hierarchy. */
45 #include <config.h>
46 #include <stdio.h>
47 #include <getopt.h>
48 #include <sys/types.h>
49 #include <assert.h>
51 #include "system.h"
52 #include "dirname.h"
53 #include "error.h"
54 #include "quote.h"
55 #include "remove.h"
56 #include "root-dev-ino.h"
57 #include "save-cwd.h"
59 /* The official name of this program (e.g., no `g' prefix). */
60 #define PROGRAM_NAME "rm"
62 #define AUTHORS \
63 "Paul Rubin", "David MacKenzie, Richard Stallman", "Jim Meyering"
65 /* Name this program was run with. */
66 char *program_name;
68 /* For long options that have no equivalent short option, use a
69 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
70 enum
72 NO_PRESERVE_ROOT = CHAR_MAX + 1,
73 PRESERVE_ROOT,
74 PRESUME_INPUT_TTY_OPTION
77 static struct option const long_opts[] =
79 {"directory", no_argument, NULL, 'd'},
80 {"force", no_argument, NULL, 'f'},
81 {"interactive", no_argument, NULL, 'i'},
83 {"no-preserve-root", no_argument, 0, NO_PRESERVE_ROOT},
84 {"preserve-root", no_argument, 0, PRESERVE_ROOT},
86 /* This is solely for testing. Do not document. */
87 /* It is relatively difficult to ensure that there is a tty on stdin.
88 Since rm acts differently depending on that, without this option,
89 it'd be harder to test the parts of rm that depend on that setting. */
90 {"presume-input-tty", no_argument, NULL, PRESUME_INPUT_TTY_OPTION},
92 {"recursive", no_argument, NULL, 'r'},
93 {"verbose", no_argument, NULL, 'v'},
94 {GETOPT_HELP_OPTION_DECL},
95 {GETOPT_VERSION_OPTION_DECL},
96 {NULL, 0, NULL, 0}
99 void
100 usage (int status)
102 if (status != 0)
103 fprintf (stderr, _("Try `%s --help' for more information.\n"),
104 program_name);
105 else
107 char *base = base_name (program_name);
108 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
109 fputs (_("\
110 Remove (unlink) the FILE(s).\n\
112 -d, --directory unlink FILE, even if it is a non-empty directory\n\
113 (super-user only; this works only if your system\n\
114 supports `unlink' for nonempty directories)\n\
115 -f, --force ignore nonexistent files, never prompt\n\
116 -i, --interactive prompt before any removal\n\
117 "), stdout);
118 fputs (_("\
119 --no-preserve-root do not treat `/' specially (the default)\n\
120 --preserve-root fail to operate recursively on `/'\n\
121 -r, -R, --recursive remove the contents of directories recursively\n\
122 -v, --verbose explain what is being done\n\
123 "), stdout);
124 fputs (HELP_OPTION_DESCRIPTION, stdout);
125 fputs (VERSION_OPTION_DESCRIPTION, stdout);
126 printf (_("\
128 To remove a file whose name starts with a `-', for example `-foo',\n\
129 use one of these commands:\n\
130 %s -- -foo\n\
132 %s ./-foo\n\
134 base, base);
135 fputs (_("\
137 Note that if you use rm to remove a file, it is usually possible to recover\n\
138 the contents of that file. If you want more assurance that the contents are\n\
139 truly unrecoverable, consider using shred.\n\
140 "), stdout);
141 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
143 exit (status);
146 static void
147 rm_option_init (struct rm_options *x)
149 x->unlink_dirs = 0;
150 x->ignore_missing_files = 0;
151 x->interactive = 0;
152 x->recursive = 0;
153 x->root_dev_ino = NULL;
154 x->stdin_tty = isatty (STDIN_FILENO);
155 x->verbose = 0;
159 main (int argc, char **argv)
161 bool preserve_root = false;
162 struct rm_options x;
163 int fail = 0;
164 int c;
166 initialize_main (&argc, &argv);
167 program_name = argv[0];
168 setlocale (LC_ALL, "");
169 bindtextdomain (PACKAGE, LOCALEDIR);
170 textdomain (PACKAGE);
172 atexit (close_stdout);
174 rm_option_init (&x);
176 while ((c = getopt_long (argc, argv, "dfirvR", long_opts, NULL)) != -1)
178 switch (c)
180 case 0: /* Long option. */
181 break;
183 case 'd':
184 x.unlink_dirs = 1;
185 break;
187 case 'f':
188 x.interactive = 0;
189 x.ignore_missing_files = 1;
190 break;
192 case 'i':
193 x.interactive = 1;
194 x.ignore_missing_files = 0;
195 break;
197 case 'r':
198 case 'R':
199 x.recursive = 1;
200 break;
202 case NO_PRESERVE_ROOT:
203 preserve_root = false;
204 break;
206 case PRESERVE_ROOT:
207 preserve_root = true;
208 break;
210 case PRESUME_INPUT_TTY_OPTION:
211 x.stdin_tty = 1;
212 break;
214 case 'v':
215 x.verbose = 1;
216 break;
218 case_GETOPT_HELP_CHAR;
219 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
220 default:
221 usage (EXIT_FAILURE);
225 if (argc <= optind)
227 if (x.ignore_missing_files)
228 exit (EXIT_SUCCESS);
229 else
231 error (0, 0, _("too few arguments"));
232 usage (EXIT_FAILURE);
236 if (x.recursive && preserve_root)
238 static struct dev_ino dev_ino_buf;
239 x.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
240 if (x.root_dev_ino == NULL)
241 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
242 quote ("/"));
246 size_t n_files = argc - optind;
247 char const *const *file = (char const *const *) argv + optind;
249 enum RM_status status = rm (n_files, file, &x);
250 assert (VALID_STATUS (status));
251 if (status == RM_ERROR)
252 fail = 1;
255 exit (fail);