(strip_trailing_slashes): Remove declaration; now in dirname.h.
[platform/upstream/coreutils.git] / src / rm.c
1 /* `rm' file deletion utility for GNU.
2    Copyright (C) 88, 90, 91, 1994-2001 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 hash tables 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 by maintaining a table of the currently
35    active directories.  See the description of active_dir_map in remove.c.
36
37    RM is careful to avoid forming full file names whenever possible.
38    A full file name is formed only when it is about to be used -- e.g.
39    in a diagnostic or in an interactive-mode prompt.
40
41    RM minimizes the number of lstat system calls it makes.  On systems
42    that have valid d_type data in directory entries, RM makes only one
43    lstat call per command line argument -- regardless of the depth of
44    the hierarchy.  */
45
46 #include <config.h>
47 #include <stdio.h>
48 #include <getopt.h>
49 #include <sys/types.h>
50 #include <assert.h>
51
52 #include "system.h"
53 #include "dirname.h"
54 #include "error.h"
55 #include "remove.h"
56 #include "save-cwd.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, and Jim Meyering"
63
64 /* Name this program was run with.  */
65 char *program_name;
66
67 static struct option const long_opts[] =
68 {
69   {"directory", no_argument, NULL, 'd'},
70   {"force", no_argument, NULL, 'f'},
71   {"interactive", no_argument, NULL, 'i'},
72   {"recursive", no_argument, NULL, 'r'},
73   {"verbose", no_argument, NULL, 'v'},
74   {GETOPT_HELP_OPTION_DECL},
75   {GETOPT_VERSION_OPTION_DECL},
76   {NULL, 0, NULL, 0}
77 };
78
79 void
80 usage (int status)
81 {
82   if (status != 0)
83     fprintf (stderr, _("Try `%s --help' for more information.\n"),
84              program_name);
85   else
86     {
87       printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
88       printf (_("\
89 Remove (unlink) the FILE(s).\n\
90 \n\
91   -d, --directory       unlink directory, even if non-empty (super-user only)\n\
92   -f, --force           ignore nonexistent files, never prompt\n\
93   -i, --interactive     prompt before any removal\n\
94   -r, -R, --recursive   remove the contents of directories recursively\n\
95   -v, --verbose         explain what is being done\n\
96       --help            display this help and exit\n\
97       --version         output version information and exit\n\
98 \n\
99 To remove a file whose name starts with a `-', for example `-foo',\n\
100 use one of these commands:\n\
101   %s -- -foo\n\
102 \n\
103   %s ./-foo\n\
104 \n\
105 Note that if you use rm to remove a file, it is usually possible to recover\n\
106 the contents of that file.  If you want more assurance that the contents are\n\
107 truly unrecoverable, consider using shred.\n\
108 "),
109               program_name, program_name);
110       puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
111     }
112   exit (status);
113 }
114
115 static void
116 rm_option_init (struct rm_options *x)
117 {
118   x->unlink_dirs = 0;
119   x->ignore_missing_files = 0;
120   x->interactive = 0;
121   x->recursive = 0;
122   x->stdin_tty = isatty (STDIN_FILENO);
123   x->verbose = 0;
124 }
125
126 int
127 main (int argc, char **argv)
128 {
129   struct rm_options x;
130   int fail = 0;
131   int c;
132
133   program_name = argv[0];
134   setlocale (LC_ALL, "");
135   bindtextdomain (PACKAGE, LOCALEDIR);
136   textdomain (PACKAGE);
137
138   atexit (close_stdout);
139
140   rm_option_init (&x);
141
142   while ((c = getopt_long (argc, argv, "dfirvR", long_opts, NULL)) != -1)
143     {
144       switch (c)
145         {
146         case 0:         /* Long option.  */
147           break;
148         case 'd':
149           x.unlink_dirs = 1;
150           break;
151         case 'f':
152           x.interactive = 0;
153           x.ignore_missing_files = 1;
154           break;
155         case 'i':
156           x.interactive = 1;
157           x.ignore_missing_files = 0;
158           break;
159         case 'r':
160         case 'R':
161           x.recursive = 1;
162           break;
163         case 'v':
164           x.verbose = 1;
165           break;
166         case_GETOPT_HELP_CHAR;
167         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
168         default:
169           usage (1);
170         }
171     }
172
173   if (optind == argc)
174     {
175       if (x.ignore_missing_files)
176         exit (0);
177       else
178         {
179           error (0, 0, _("too few arguments"));
180           usage (1);
181         }
182     }
183
184   remove_init ();
185
186   for (; optind < argc; optind++)
187     {
188       struct File_spec fs;
189       enum RM_status status;
190
191       /* Stripping slashes is harmless for rmdir;
192          if the arg is not a directory, it will fail with ENOTDIR.  */
193       strip_trailing_slashes (argv[optind]);
194       fspec_init_file (&fs, argv[optind]);
195       status = rm (&fs, 1, &x);
196       assert (VALID_STATUS (status));
197       if (status == RM_ERROR)
198         fail = 1;
199     }
200
201   remove_fini ();
202
203   exit (fail);
204 }