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