1 /* `rm' file deletion utility for GNU.
2 Copyright (C) 88, 90, 91, 1994-2002 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)
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 avoid recursion 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
49 #include <sys/types.h>
57 /* The official name of this program (e.g., no `g' prefix). */
58 #define PROGRAM_NAME "rm"
61 N_ ("Paul Rubin, David MacKenzie, Richard Stallman, and Jim Meyering")
63 /* Name this program was run with. */
66 /* For long options that have no equivalent short option, use a
67 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
70 PRESUME_INPUT_TTY_OPTION
= CHAR_MAX
+ 1
73 static struct option
const long_opts
[] =
75 {"directory", no_argument
, NULL
, 'd'},
76 {"force", no_argument
, NULL
, 'f'},
77 {"interactive", no_argument
, NULL
, 'i'},
79 /* This is solely for testing. Do not document. */
80 /* It is relatively difficult to ensure that there is a tty on stdin.
81 Since rm acts differently depending on that, without this option,
82 it'd be harder to test the parts of rm that depend on that setting. */
83 {"presume-input-tty", no_argument
, NULL
, PRESUME_INPUT_TTY_OPTION
},
85 {"recursive", no_argument
, NULL
, 'r'},
86 {"verbose", no_argument
, NULL
, 'v'},
87 {GETOPT_HELP_OPTION_DECL
},
88 {GETOPT_VERSION_OPTION_DECL
},
96 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
100 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name
);
102 Remove (unlink) the FILE(s).\n\
104 -d, --directory unlink FILE, even if it is a non-empty directory\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\
111 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
112 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
115 To remove a file whose name starts with a `-', for example `-foo',\n\
116 use one of these commands:\n\
121 program_name
, program_name
);
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\
128 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
134 rm_option_init (struct rm_options
*x
)
137 x
->ignore_missing_files
= 0;
140 x
->stdin_tty
= isatty (STDIN_FILENO
);
145 main (int argc
, char **argv
)
151 program_name
= argv
[0];
152 setlocale (LC_ALL
, "");
153 bindtextdomain (PACKAGE
, LOCALEDIR
);
154 textdomain (PACKAGE
);
156 atexit (close_stdout
);
160 while ((c
= getopt_long (argc
, argv
, "dfirvR", long_opts
, NULL
)) != -1)
164 case 0: /* Long option. */
171 x
.ignore_missing_files
= 1;
175 x
.ignore_missing_files
= 0;
181 case PRESUME_INPUT_TTY_OPTION
:
187 case_GETOPT_HELP_CHAR
;
188 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
190 usage (EXIT_FAILURE
);
196 if (x
.ignore_missing_files
)
200 error (0, 0, _("too few arguments"));
201 usage (EXIT_FAILURE
);
206 size_t n_files
= argc
- optind
;
207 char const *const *file
= (char const *const *) argv
+ optind
;
209 enum RM_status status
= rm (n_files
, file
, &x
);
210 assert (VALID_STATUS (status
));
211 if (status
== RM_ERROR
)