(main): Standardize on the diagnostics given when someone gives
[platform/upstream/coreutils.git] / src / rm.c
1 /* `rm' file deletion utility for GNU.
2    Copyright (C) 88, 90, 91, 1994-2004 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 /* Written by Paul Rubin, David MacKenzie, and Richard Stallman.
19    Reworked to use chdir and avoid recursion by Jim Meyering.  */
20
21 /* Implementation overview:
22
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.
28
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.
33
34    RM detects directory cycles lazily.  See lib/cycle-check.c.
35
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.
39
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.  */
44
45 #include <config.h>
46 #include <stdio.h>
47 #include <getopt.h>
48 #include <sys/types.h>
49 #include <assert.h>
50
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
58 /* The official name of this program (e.g., no `g' prefix).  */
59 #define PROGRAM_NAME "rm"
60
61 #define AUTHORS \
62   "Paul Rubin", "David MacKenzie, Richard Stallman", "Jim Meyering"
63
64 /* Name this program was run with.  */
65 char *program_name;
66
67 /* For long options that have no equivalent short option, use a
68    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
69 enum
70 {
71   NO_PRESERVE_ROOT = CHAR_MAX + 1,
72   PRESERVE_ROOT,
73   PRESUME_INPUT_TTY_OPTION
74 };
75
76 static struct option const long_opts[] =
77 {
78   {"directory", no_argument, NULL, 'd'},
79   {"force", no_argument, NULL, 'f'},
80   {"interactive", no_argument, NULL, 'i'},
81
82   {"no-preserve-root", no_argument, 0, NO_PRESERVE_ROOT},
83   {"preserve-root", no_argument, 0, PRESERVE_ROOT},
84
85   /* This is solely for testing.  Do not document.  */
86   /* It is relatively difficult to ensure that there is a tty on stdin.
87      Since rm acts differently depending on that, without this option,
88      it'd be harder to test the parts of rm that depend on that setting.  */
89   {"presume-input-tty", no_argument, NULL, PRESUME_INPUT_TTY_OPTION},
90
91   {"recursive", no_argument, NULL, 'r'},
92   {"verbose", no_argument, NULL, 'v'},
93   {GETOPT_HELP_OPTION_DECL},
94   {GETOPT_VERSION_OPTION_DECL},
95   {NULL, 0, NULL, 0}
96 };
97
98 void
99 usage (int status)
100 {
101   if (status != EXIT_SUCCESS)
102     fprintf (stderr, _("Try `%s --help' for more information.\n"),
103              program_name);
104   else
105     {
106       char *base = base_name (program_name);
107       printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
108       fputs (_("\
109 Remove (unlink) the FILE(s).\n\
110 \n\
111   -d, --directory       unlink FILE, even if it is a non-empty directory\n\
112                           (super-user only; this works only if your system\n\
113                            supports `unlink' for nonempty directories)\n\
114   -f, --force           ignore nonexistent files, never prompt\n\
115   -i, --interactive     prompt before any removal\n\
116 "), stdout);
117       fputs (_("\
118       --no-preserve-root do not treat `/' specially (the default)\n\
119       --preserve-root   fail to operate recursively on `/'\n\
120   -r, -R, --recursive   remove the contents of directories recursively\n\
121   -v, --verbose         explain what is being done\n\
122 "), stdout);
123       fputs (HELP_OPTION_DESCRIPTION, stdout);
124       fputs (VERSION_OPTION_DESCRIPTION, stdout);
125       printf (_("\
126 \n\
127 To remove a file whose name starts with a `-', for example `-foo',\n\
128 use one of these commands:\n\
129   %s -- -foo\n\
130 \n\
131   %s ./-foo\n\
132 "),
133               base, base);
134       fputs (_("\
135 \n\
136 Note that if you use rm to remove a file, it is usually possible to recover\n\
137 the contents of that file.  If you want more assurance that the contents are\n\
138 truly unrecoverable, consider using shred.\n\
139 "), stdout);
140       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
141     }
142   exit (status);
143 }
144
145 static void
146 rm_option_init (struct rm_options *x)
147 {
148   x->unlink_dirs = 0;
149   x->ignore_missing_files = 0;
150   x->interactive = 0;
151   x->recursive = 0;
152   x->root_dev_ino = NULL;
153   x->stdin_tty = isatty (STDIN_FILENO);
154   x->verbose = 0;
155
156   /* Since this program exits immediately after calling `rm', rm need not
157      expend unnecessary effort to preserve the initial working directory.  */
158   x->require_restore_cwd = false;
159 }
160
161 int
162 main (int argc, char **argv)
163 {
164   bool preserve_root = false;
165   struct rm_options x;
166   int fail = 0;
167   int c;
168
169   initialize_main (&argc, &argv);
170   program_name = argv[0];
171   setlocale (LC_ALL, "");
172   bindtextdomain (PACKAGE, LOCALEDIR);
173   textdomain (PACKAGE);
174
175   atexit (close_stdout);
176
177   rm_option_init (&x);
178
179   while ((c = getopt_long (argc, argv, "dfirvR", long_opts, NULL)) != -1)
180     {
181       switch (c)
182         {
183         case 0:         /* Long option.  */
184           break;
185
186         case 'd':
187           x.unlink_dirs = 1;
188           break;
189
190         case 'f':
191           x.interactive = 0;
192           x.ignore_missing_files = 1;
193           break;
194
195         case 'i':
196           x.interactive = 1;
197           x.ignore_missing_files = 0;
198           break;
199
200         case 'r':
201         case 'R':
202           x.recursive = 1;
203           break;
204
205         case NO_PRESERVE_ROOT:
206           preserve_root = false;
207           break;
208
209         case PRESERVE_ROOT:
210           preserve_root = true;
211           break;
212
213         case PRESUME_INPUT_TTY_OPTION:
214           x.stdin_tty = 1;
215           break;
216
217         case 'v':
218           x.verbose = 1;
219           break;
220
221         case_GETOPT_HELP_CHAR;
222         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
223         default:
224           usage (EXIT_FAILURE);
225         }
226     }
227
228   if (argc <= optind)
229     {
230       if (x.ignore_missing_files)
231         exit (EXIT_SUCCESS);
232       else
233         {
234           error (0, 0, _("missing operand"));
235           usage (EXIT_FAILURE);
236         }
237     }
238
239   if (x.recursive && preserve_root)
240     {
241       static struct dev_ino dev_ino_buf;
242       x.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
243       if (x.root_dev_ino == NULL)
244         error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
245                quote ("/"));
246     }
247
248   {
249     size_t n_files = argc - optind;
250     char const *const *file = (char const *const *) argv + optind;
251
252     enum RM_status status = rm (n_files, file, &x);
253     assert (VALID_STATUS (status));
254     if (status == RM_ERROR)
255       fail = 1;
256   }
257
258   exit (fail);
259 }