dcb3a19fefdfc890ef50279f9e708a6fef1280a4
[platform/upstream/coreutils.git] / src / rm.c
1 /* `rm' file deletion utility for GNU.
2    Copyright (C) 88, 90, 91, 1994-2003 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 "remove.h"
55 #include "save-cwd.h"
56
57 /* The official name of this program (e.g., no `g' prefix).  */
58 #define PROGRAM_NAME "rm"
59
60 #define WRITTEN_BY \
61   _("Written by Paul Rubin, David MacKenzie, Richard Stallman, and Jim Meyering.")
62
63 /* Name this program was run with.  */
64 char *program_name;
65
66 /* For long options that have no equivalent short option, use a
67    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
68 enum
69 {
70   PRESUME_INPUT_TTY_OPTION = CHAR_MAX + 1
71 };
72
73 static struct option const long_opts[] =
74 {
75   {"directory", no_argument, NULL, 'd'},
76   {"force", no_argument, NULL, 'f'},
77   {"interactive", no_argument, NULL, 'i'},
78
79   /* This is solely for testing.  Do not document.  */
80   /* It is relatively difficult to ensure that there is a tty on stdin.
81      Since rm acts differently depending on that, without this option,
82      it'd be harder to test the parts of rm that depend on that setting.  */
83   {"presume-input-tty", no_argument, NULL, PRESUME_INPUT_TTY_OPTION},
84
85   {"recursive", no_argument, NULL, 'r'},
86   {"verbose", no_argument, NULL, 'v'},
87   {GETOPT_HELP_OPTION_DECL},
88   {GETOPT_VERSION_OPTION_DECL},
89   {NULL, 0, NULL, 0}
90 };
91
92 void
93 usage (int status)
94 {
95   if (status != 0)
96     fprintf (stderr, _("Try `%s --help' for more information.\n"),
97              program_name);
98   else
99     {
100       char *base = base_name (program_name);
101       printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
102       fputs (_("\
103 Remove (unlink) the FILE(s).\n\
104 \n\
105   -d, --directory       unlink FILE, even if it is a non-empty directory\n\
106                           (super-user only; this works only if your system\n\
107                            supports `unlink' for nonempty directories)\n\
108   -f, --force           ignore nonexistent files, never prompt\n\
109   -i, --interactive     prompt before any removal\n\
110   -r, -R, --recursive   remove the contents of directories recursively\n\
111   -v, --verbose         explain what is being done\n\
112 "), stdout);
113       fputs (HELP_OPTION_DESCRIPTION, stdout);
114       fputs (VERSION_OPTION_DESCRIPTION, stdout);
115       printf (_("\
116 \n\
117 To remove a file whose name starts with a `-', for example `-foo',\n\
118 use one of these commands:\n\
119   %s -- -foo\n\
120 \n\
121   %s ./-foo\n\
122 "),
123               base, base);
124       fputs (_("\
125 \n\
126 Note that if you use rm to remove a file, it is usually possible to recover\n\
127 the contents of that file.  If you want more assurance that the contents are\n\
128 truly unrecoverable, consider using shred.\n\
129 "), stdout);
130       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
131     }
132   exit (status);
133 }
134
135 static void
136 rm_option_init (struct rm_options *x)
137 {
138   x->unlink_dirs = 0;
139   x->ignore_missing_files = 0;
140   x->interactive = 0;
141   x->recursive = 0;
142   x->stdin_tty = isatty (STDIN_FILENO);
143   x->verbose = 0;
144 }
145
146 int
147 main (int argc, char **argv)
148 {
149   struct rm_options x;
150   int fail = 0;
151   int c;
152
153   initialize_main (&argc, &argv);
154   program_name = argv[0];
155   setlocale (LC_ALL, "");
156   bindtextdomain (PACKAGE, LOCALEDIR);
157   textdomain (PACKAGE);
158
159   atexit (close_stdout);
160
161   rm_option_init (&x);
162
163   while ((c = getopt_long (argc, argv, "dfirvR", long_opts, NULL)) != -1)
164     {
165       switch (c)
166         {
167         case 0:         /* Long option.  */
168           break;
169         case 'd':
170           x.unlink_dirs = 1;
171           break;
172         case 'f':
173           x.interactive = 0;
174           x.ignore_missing_files = 1;
175           break;
176         case 'i':
177           x.interactive = 1;
178           x.ignore_missing_files = 0;
179           break;
180         case 'r':
181         case 'R':
182           x.recursive = 1;
183           break;
184         case PRESUME_INPUT_TTY_OPTION:
185           x.stdin_tty = 1;
186           break;
187         case 'v':
188           x.verbose = 1;
189           break;
190         case_GETOPT_HELP_CHAR;
191         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
192         default:
193           usage (EXIT_FAILURE);
194         }
195     }
196
197   if (argc <= optind)
198     {
199       if (x.ignore_missing_files)
200         exit (EXIT_SUCCESS);
201       else
202         {
203           error (0, 0, _("too few arguments"));
204           usage (EXIT_FAILURE);
205         }
206     }
207
208   {
209     size_t n_files = argc - optind;
210     char const *const *file = (char const *const *) argv + optind;
211
212     enum RM_status status = rm (n_files, file, &x);
213     assert (VALID_STATUS (status));
214     if (status == RM_ERROR)
215       fail = 1;
216   }
217
218   exit (fail);
219 }