'sort' race condition fixes.
[coreutils.git] / src / rm.c
blob8d80df86bc2158755f009691c2c7096ed4fda2bf
1 /* `rm' file deletion utility for GNU.
2 Copyright (C) 88, 90, 91, 1994-2000 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 void strip_trailing_slashes ();
65 /* Name this program was run with. */
66 char *program_name;
68 static struct option const long_opts[] =
70 {"directory", no_argument, NULL, 'd'},
71 {"force", no_argument, NULL, 'f'},
72 {"interactive", no_argument, NULL, 'i'},
73 {"recursive", no_argument, NULL, 'r'},
74 {"verbose", no_argument, NULL, 'v'},
75 {GETOPT_HELP_OPTION_DECL},
76 {GETOPT_VERSION_OPTION_DECL},
77 {NULL, 0, NULL, 0}
80 void
81 usage (int status)
83 if (status != 0)
84 fprintf (stderr, _("Try `%s --help' for more information.\n"),
85 program_name);
86 else
88 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
89 printf (_("\
90 Remove (unlink) the FILE(s).\n\
91 \n\
92 -d, --directory unlink directory, even if non-empty (super-user only)\n\
93 -f, --force ignore nonexistent files, never prompt\n\
94 -i, --interactive prompt before any removal\n\
95 -r, -R, --recursive remove the contents of directories recursively\n\
96 -v, --verbose explain what is being done\n\
97 --help display this help and exit\n\
98 --version output version information and exit\n\
99 \n\
100 To remove a file whose name starts with a `-', for example `-foo',\n\
101 use one of these commands:\n\
102 %s -- -foo\n\
103 %s ./-foo\n\
105 program_name, program_name);
106 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
108 exit (status);
111 static void
112 rm_option_init (struct rm_options *x)
114 x->unlink_dirs = 0;
115 x->ignore_missing_files = 0;
116 x->interactive = 0;
117 x->recursive = 0;
118 x->stdin_tty = isatty (STDIN_FILENO);
119 x->verbose = 0;
123 main (int argc, char **argv)
125 struct rm_options x;
126 int fail = 0;
127 int c;
129 program_name = argv[0];
130 setlocale (LC_ALL, "");
131 bindtextdomain (PACKAGE, LOCALEDIR);
132 textdomain (PACKAGE);
134 atexit (close_stdout);
136 rm_option_init (&x);
138 while ((c = getopt_long (argc, argv, "dfirvR", long_opts, NULL)) != -1)
140 switch (c)
142 case 0: /* Long option. */
143 break;
144 case 'd':
145 x.unlink_dirs = 1;
146 break;
147 case 'f':
148 x.interactive = 0;
149 x.ignore_missing_files = 1;
150 break;
151 case 'i':
152 x.interactive = 1;
153 x.ignore_missing_files = 0;
154 break;
155 case 'r':
156 case 'R':
157 x.recursive = 1;
158 break;
159 case 'v':
160 x.verbose = 1;
161 break;
162 case_GETOPT_HELP_CHAR;
163 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
164 default:
165 usage (1);
169 if (optind == argc)
171 if (x.ignore_missing_files)
172 exit (0);
173 else
175 error (0, 0, _("too few arguments"));
176 usage (1);
180 remove_init ();
182 for (; optind < argc; optind++)
184 struct File_spec fs;
185 enum RM_status status;
187 /* Stripping slashes is harmless for rmdir;
188 if the arg is not a directory, it will fail with ENOTDIR. */
189 strip_trailing_slashes (argv[optind]);
190 fspec_init_file (&fs, argv[optind]);
191 status = rm (&fs, 1, &x);
192 assert (VALID_STATUS (status));
193 if (status == RM_ERROR)
194 fail = 1;
197 remove_fini ();
199 exit (fail);