Tizen 2.0 Release
[external/tizen-coreutils.git] / src / rm.c
1 /* `rm' file deletion utility for GNU.
2    Copyright (C) 88, 90, 91, 1994-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 "argmatch.h"
53 #include "error.h"
54 #include "lstat.h"
55 #include "quote.h"
56 #include "quotearg.h"
57 #include "remove.h"
58 #include "root-dev-ino.h"
59 #include "yesno.h"
60
61 /* The official name of this program (e.g., no `g' prefix).  */
62 #define PROGRAM_NAME "rm"
63
64 #define AUTHORS \
65   "Paul Rubin", "David MacKenzie, Richard Stallman", "Jim Meyering"
66
67 /* Name this program was run with.  */
68 char *program_name;
69
70 /* For long options that have no equivalent short option, use a
71    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
72 enum
73 {
74   INTERACTIVE_OPTION = CHAR_MAX + 1,
75   ONE_FILE_SYSTEM,
76   NO_PRESERVE_ROOT,
77   PRESERVE_ROOT,
78   PRESUME_INPUT_TTY_OPTION
79 };
80
81 enum interactive_type
82   {
83     interactive_never,          /* 0: no option or --interactive=never */
84     interactive_once,           /* 1: -I or --interactive=once */
85     interactive_always          /* 2: default, -i or --interactive=always */
86   };
87
88 static struct option const long_opts[] =
89 {
90   {"directory", no_argument, NULL, 'd'},
91   {"force", no_argument, NULL, 'f'},
92   {"interactive", optional_argument, NULL, INTERACTIVE_OPTION},
93
94   {"one-file-system", no_argument, NULL, ONE_FILE_SYSTEM},
95   {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
96   {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
97
98   /* This is solely for testing.  Do not document.  */
99   /* It is relatively difficult to ensure that there is a tty on stdin.
100      Since rm acts differently depending on that, without this option,
101      it'd be harder to test the parts of rm that depend on that setting.  */
102   {"-presume-input-tty", no_argument, NULL, PRESUME_INPUT_TTY_OPTION},
103
104   {"recursive", no_argument, NULL, 'r'},
105   {"verbose", no_argument, NULL, 'v'},
106   {GETOPT_HELP_OPTION_DECL},
107   {GETOPT_VERSION_OPTION_DECL},
108   {NULL, 0, NULL, 0}
109 };
110
111 static char const *const interactive_args[] =
112 {
113   "never", "no", "none",
114   "once",
115   "always", "yes", NULL
116 };
117 static enum interactive_type const interactive_types[] =
118 {
119   interactive_never, interactive_never, interactive_never,
120   interactive_once,
121   interactive_always, interactive_always
122 };
123 ARGMATCH_VERIFY (interactive_args, interactive_types);
124
125 /* Advise the user about invalid usages like "rm -foo" if the file
126    "-foo" exists, assuming ARGC and ARGV are as with `main'.  */
127
128 static void
129 diagnose_leading_hyphen (int argc, char **argv)
130 {
131   /* OPTIND is unreliable, so iterate through the arguments looking
132      for a file name that looks like an option.  */
133   int i;
134
135   for (i = 1; i < argc; i++)
136     {
137       char const *arg = argv[i];
138       struct stat st;
139
140       if (arg[0] == '-' && arg[1] && lstat (arg, &st) == 0)
141         {
142           fprintf (stderr,
143                    _("Try `%s ./%s' to remove the file %s.\n"),
144                    argv[0],
145                    quotearg_n_style (1, shell_quoting_style, arg),
146                    quote (arg));
147           break;
148         }
149     }
150 }
151
152 void
153 usage (int status)
154 {
155   if (status != EXIT_SUCCESS)
156     fprintf (stderr, _("Try `%s --help' for more information.\n"),
157              program_name);
158   else
159     {
160       printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
161       fputs (_("\
162 Remove (unlink) the FILE(s).\n\
163 \n\
164   -f, --force           ignore nonexistent files, never prompt\n\
165   -i                    prompt before every removal\n\
166 "), stdout);
167       fputs (_("\
168   -I                    prompt once before removing more than three files, or\n\
169                           when removing recursively.  Less intrusive than -i,\n\
170                           while still giving protection against most mistakes\n\
171       --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or\n\
172                           always (-i).  Without WHEN, prompt always\n\
173 "), stdout);
174       fputs (_("\
175       --one-file-system  when removing a hierarchy recursively, skip any\n\
176                           directory that is on a file system different from\n\
177                           that of the corresponding command line argument\n\
178 "), stdout);
179       fputs (_("\
180       --no-preserve-root  do not treat `/' specially\n\
181       --preserve-root   do not remove `/' (default)\n\
182   -r, -R, --recursive   remove directories and their contents recursively\n\
183   -v, --verbose         explain what is being done\n\
184 "), stdout);
185       fputs (HELP_OPTION_DESCRIPTION, stdout);
186       fputs (VERSION_OPTION_DESCRIPTION, stdout);
187       fputs (_("\
188 \n\
189 By default, rm does not remove directories.  Use the --recursive (-r or -R)\n\
190 option to remove each listed directory, too, along with all of its contents.\n\
191 "), stdout);
192       printf (_("\
193 \n\
194 To remove a file whose name starts with a `-', for example `-foo',\n\
195 use one of these commands:\n\
196   %s -- -foo\n\
197 \n\
198   %s ./-foo\n\
199 "),
200               program_name, program_name);
201       fputs (_("\
202 \n\
203 Note that if you use rm to remove a file, it is usually possible to recover\n\
204 the contents of that file.  If you want more assurance that the contents are\n\
205 truly unrecoverable, consider using shred.\n\
206 "), stdout);
207       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
208     }
209   exit (status);
210 }
211
212 static void
213 rm_option_init (struct rm_options *x)
214 {
215   x->ignore_missing_files = false;
216   x->interactive = RMI_SOMETIMES;
217   x->one_file_system = false;
218   x->recursive = false;
219   x->root_dev_ino = NULL;
220   x->stdin_tty = isatty (STDIN_FILENO);
221   x->verbose = false;
222
223   /* Since this program exits immediately after calling `rm', rm need not
224      expend unnecessary effort to preserve the initial working directory.  */
225   x->require_restore_cwd = false;
226 }
227
228 int
229 main (int argc, char **argv)
230 {
231   bool preserve_root = true;
232   struct rm_options x;
233   bool prompt_once = false;
234   int c;
235
236   initialize_main (&argc, &argv);
237   program_name = argv[0];
238   setlocale (LC_ALL, "");
239   bindtextdomain (PACKAGE, LOCALEDIR);
240   textdomain (PACKAGE);
241
242   atexit (close_stdout);
243
244   rm_option_init (&x);
245
246   while ((c = getopt_long (argc, argv, "dfirvIR", long_opts, NULL)) != -1)
247     {
248       switch (c)
249         {
250         case 'd':
251           /* Ignore this option, for backward compatibility with
252              coreutils 5.92.  FIXME: Some time after 2005, change this
253              to report an error (or perhaps behave like FreeBSD does)
254              instead of ignoring the option.  */
255           break;
256
257         case 'f':
258           x.interactive = RMI_NEVER;
259           x.ignore_missing_files = true;
260           prompt_once = false;
261           break;
262
263         case 'i':
264           x.interactive = RMI_ALWAYS;
265           x.ignore_missing_files = false;
266           prompt_once = false;
267           break;
268
269         case 'I':
270           x.interactive = RMI_NEVER;
271           x.ignore_missing_files = false;
272           prompt_once = true;
273           break;
274
275         case 'r':
276         case 'R':
277           x.recursive = true;
278           break;
279
280         case INTERACTIVE_OPTION:
281           {
282             int i;
283             if (optarg)
284               i = XARGMATCH ("--interactive", optarg, interactive_args,
285                              interactive_types);
286             else
287               i = interactive_always;
288             switch (i)
289               {
290               case interactive_never:
291                 x.interactive = RMI_NEVER;
292                 prompt_once = false;
293                 break;
294
295               case interactive_once:
296                 x.interactive = RMI_SOMETIMES;
297                 x.ignore_missing_files = false;
298                 prompt_once = true;
299                 break;
300
301               case interactive_always:
302                 x.interactive = RMI_ALWAYS;
303                 x.ignore_missing_files = false;
304                 prompt_once = false;
305                 break;
306               }
307             break;
308           }
309
310         case ONE_FILE_SYSTEM:
311           x.one_file_system = true;
312           break;
313
314         case NO_PRESERVE_ROOT:
315           preserve_root = false;
316           break;
317
318         case PRESERVE_ROOT:
319           preserve_root = true;
320           break;
321
322         case PRESUME_INPUT_TTY_OPTION:
323           x.stdin_tty = true;
324           break;
325
326         case 'v':
327           x.verbose = true;
328           break;
329
330         case_GETOPT_HELP_CHAR;
331         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
332         default:
333           diagnose_leading_hyphen (argc, argv);
334           usage (EXIT_FAILURE);
335         }
336     }
337
338   if (argc <= optind)
339     {
340       if (x.ignore_missing_files)
341         exit (EXIT_SUCCESS);
342       else
343         {
344           error (0, 0, _("missing operand"));
345           usage (EXIT_FAILURE);
346         }
347     }
348
349   if (x.recursive & preserve_root)
350     {
351       static struct dev_ino dev_ino_buf;
352       x.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
353       if (x.root_dev_ino == NULL)
354         error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
355                quote ("/"));
356     }
357
358   size_t n_files = argc - optind;
359   char const *const *file = (char const *const *) argv + optind;
360
361   if (prompt_once && (x.recursive || 3 < n_files))
362     {
363       fprintf (stderr,
364                (x.recursive
365                 ? _("%s: remove all arguments recursively? ")
366                 : _("%s: remove all arguments? ")),
367                program_name);
368       if (!yesno ())
369         exit (EXIT_SUCCESS);
370     }
371   enum RM_status status = rm (n_files, file, &x);
372   assert (VALID_STATUS (status));
373   exit (status == RM_ERROR ? EXIT_FAILURE : EXIT_SUCCESS);
374 }