(main): Stat all non-`-' input file files (and fail if a
[coreutils.git] / src / rm.c
bloba6c1d2ec113851d90f222e2255e04b6b8094338c
1 /* `rm' file deletion utility for GNU.
2 Copyright (C) 88, 90, 91, 94, 95, 96, 97, 1998 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_cwd_entries)
26 so that when/if the containing directory is reopened, RM doesn't try to
27 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 below.
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 "save-cwd.h"
53 #include "system.h"
54 #include "closeout.h"
55 #include "error.h"
56 #include "remove.h"
58 void strip_trailing_slashes ();
60 /* Name this program was run with. */
61 char *program_name;
63 /* If nonzero, display usage information and exit. */
64 static int show_help;
66 /* If nonzero, print the version on standard output and exit. */
67 static int show_version;
69 static struct option const long_opts[] =
71 {"directory", no_argument, NULL, 'd'},
72 {"force", no_argument, NULL, 'f'},
73 {"interactive", no_argument, NULL, 'i'},
74 {"recursive", no_argument, NULL, 'r'},
75 {"verbose", no_argument, NULL, 'v'},
76 {"help", no_argument, &show_help, 1},
77 {"version", no_argument, &show_version, 1},
78 {NULL, 0, NULL, 0}
81 static void
82 usage (int status)
84 if (status != 0)
85 fprintf (stderr, _("Try `%s --help' for more information.\n"),
86 program_name);
87 else
89 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
90 printf (_("\
91 Remove (unlink) the FILE(s).\n\
92 \n\
93 -d, --directory unlink directory, even if non-empty (super-user only)\n\
94 -f, --force ignore nonexistent files, never prompt\n\
95 -i, --interactive prompt before any removal\n\
96 -r, -R, --recursive remove the contents of directories recursively\n\
97 -v, --verbose explain what is being done\n\
98 --help display this help and exit\n\
99 --version output version information and exit\n\
100 "));
101 puts (_("\nReport bugs to <fileutils-bugs@gnu.org>."));
102 close_stdout ();
104 exit (status);
107 static void
108 rm_option_init (struct rm_options *x)
110 x->unlink_dirs = 0;
111 x->ignore_missing_files = 0;
112 x->interactive = 0;
113 x->recursive = 0;
114 x->stdin_tty = isatty (STDIN_FILENO);
115 x->verbose = 0;
119 main (int argc, char **argv)
121 struct rm_options x;
122 int fail = 0;
123 int c;
125 program_name = argv[0];
126 setlocale (LC_ALL, "");
127 bindtextdomain (PACKAGE, LOCALEDIR);
128 textdomain (PACKAGE);
130 rm_option_init (&x);
132 while ((c = getopt_long (argc, argv, "dfirvR", long_opts, NULL)) != -1)
134 switch (c)
136 case 0: /* Long option. */
137 break;
138 case 'd':
139 x.unlink_dirs = 1;
140 break;
141 case 'f':
142 x.interactive = 0;
143 x.ignore_missing_files = 1;
144 break;
145 case 'i':
146 x.interactive = 1;
147 x.ignore_missing_files = 0;
148 break;
149 case 'r':
150 case 'R':
151 x.recursive = 1;
152 break;
153 case 'v':
154 x.verbose = 1;
155 break;
156 default:
157 usage (1);
161 if (show_version)
163 printf ("rm (%s) %s\n", GNU_PACKAGE, VERSION);
164 close_stdout ();
165 exit (0);
168 if (show_help)
169 usage (0);
171 if (optind == argc)
173 if (x.ignore_missing_files)
174 exit (0);
175 else
177 error (0, 0, _("too few arguments"));
178 usage (1);
182 remove_init ();
184 for (; optind < argc; optind++)
186 struct File_spec fs;
187 enum RM_status status;
189 /* Stripping slashes is harmless for rmdir;
190 if the arg is not a directory, it will fail with ENOTDIR. */
191 strip_trailing_slashes (argv[optind]);
192 fspec_init_file (&fs, argv[optind]);
193 status = rm (&fs, 1, &x);
194 assert (VALID_STATUS (status));
195 if (status == RM_ERROR)
196 fail = 1;
199 remove_fini ();
201 if (x.verbose)
202 close_stdout ();
203 exit (fail);