*** empty log message ***
[coreutils.git] / src / rm.c
blob9c105efe2ae6163c761e0b1a63e0f139c3deace5
1 /* `rm' file deletion utility for GNU.
2 Copyright (C) 88, 90, 91, 1994-2001 Free Software Foundation, Inc.
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.
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.
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. */
18 /* Written by Paul Rubin, David MacKenzie, and Richard Stallman.
19 Reworked to use chdir and hash tables by Jim Meyering. */
21 /* Implementation overview:
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.
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.
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.
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.
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. */
46 #include <config.h>
47 #include <stdio.h>
48 #include <getopt.h>
49 #include <sys/types.h>
50 #include <assert.h>
52 #include "system.h"
53 #include "error.h"
54 #include "remove.h"
55 #include "save-cwd.h"
57 /* The official name of this program (e.g., no `g' prefix). */
58 #define PROGRAM_NAME "rm"
60 #define AUTHORS \
61 "Paul Rubin, David MacKenzie, Richard Stallman, and Jim Meyering"
63 /* Name this program was run with. */
64 char *program_name;
66 static struct option const long_opts[] =
68 {"directory", no_argument, NULL, 'd'},
69 {"force", no_argument, NULL, 'f'},
70 {"interactive", no_argument, NULL, 'i'},
71 {"recursive", no_argument, NULL, 'r'},
72 {"verbose", no_argument, NULL, 'v'},
73 {GETOPT_HELP_OPTION_DECL},
74 {GETOPT_VERSION_OPTION_DECL},
75 {NULL, 0, NULL, 0}
78 void
79 usage (int status)
81 if (status != 0)
82 fprintf (stderr, _("Try `%s --help' for more information.\n"),
83 program_name);
84 else
86 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
87 fputs (_("\
88 Remove (unlink) the FILE(s).\n\
89 \n\
90 -d, --directory unlink FILE, even if it is a non-empty directory\n\
91 (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 "), stdout);
97 fputs (HELP_OPTION_DESCRIPTION, stdout);
98 fputs (VERSION_OPTION_DESCRIPTION, stdout);
99 printf (_("\
101 To remove a file whose name starts with a `-', for example `-foo',\n\
102 use one of these commands:\n\
103 %s -- -foo\n\
105 %s ./-foo\n\
107 program_name, program_name);
108 fputs (_("\
110 Note that if you use rm to remove a file, it is usually possible to recover\n\
111 the contents of that file. If you want more assurance that the contents are\n\
112 truly unrecoverable, consider using shred.\n\
113 "), stdout);
114 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
116 exit (status);
119 static void
120 rm_option_init (struct rm_options *x)
122 x->unlink_dirs = 0;
123 x->ignore_missing_files = 0;
124 x->interactive = 0;
125 x->recursive = 0;
126 x->stdin_tty = isatty (STDIN_FILENO);
127 x->verbose = 0;
131 main (int argc, char **argv)
133 struct rm_options x;
134 int fail = 0;
135 int c;
137 program_name = argv[0];
138 setlocale (LC_ALL, "");
139 bindtextdomain (PACKAGE, LOCALEDIR);
140 textdomain (PACKAGE);
142 atexit (close_stdout);
144 rm_option_init (&x);
146 while ((c = getopt_long (argc, argv, "dfirvR", long_opts, NULL)) != -1)
148 switch (c)
150 case 0: /* Long option. */
151 break;
152 case 'd':
153 x.unlink_dirs = 1;
154 break;
155 case 'f':
156 x.interactive = 0;
157 x.ignore_missing_files = 1;
158 break;
159 case 'i':
160 x.interactive = 1;
161 x.ignore_missing_files = 0;
162 break;
163 case 'r':
164 case 'R':
165 x.recursive = 1;
166 break;
167 case 'v':
168 x.verbose = 1;
169 break;
170 case_GETOPT_HELP_CHAR;
171 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
172 default:
173 usage (1);
177 if (optind == argc)
179 if (x.ignore_missing_files)
180 exit (0);
181 else
183 error (0, 0, _("too few arguments"));
184 usage (1);
188 remove_init ();
190 for (; optind < argc; optind++)
192 struct File_spec fs;
193 enum RM_status status;
195 fspec_init_file (&fs, argv[optind]);
196 status = rm (&fs, 1, &x);
197 assert (VALID_STATUS (status));
198 if (status == RM_ERROR)
199 fail = 1;
202 remove_fini ();
204 exit (fail);