rm.c revision 1.12 1 /*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)rm.c 4.26 (Berkeley) 3/10/91";*/
42 static char rcsid[] = "$Id: rm.c,v 1.12 1993/11/29 22:39:19 jtc Exp $";
43 #endif /* not lint */
44
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <locale.h>
49 #include <errno.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <unistd.h>
53 #include <fts.h>
54
55 int check __P((char *, char *, struct stat *));
56 void checkdot __P((char **));
57 void error __P((char *, int));
58 void rmtree __P((char **));
59 void rmfile __P((char **));
60 void usage __P((void));
61
62 int dflag, fflag, iflag, retval, stdin_ok;
63
64 /*
65 * rm --
66 * This rm is different from historic rm's, but is expected to match
67 * POSIX 1003.2 behavior. The most visible difference is that -f
68 * has two specific effects now, ignore non-existent files and force
69 * file removal.
70 */
71
72 int
73 main(argc, argv)
74 int argc;
75 char **argv;
76 {
77 int ch, rflag;
78
79 setlocale(LC_ALL, "");
80
81 rflag = 0;
82 while ((ch = getopt(argc, argv, "dfiRr")) != -1)
83 switch(ch) {
84 case 'd':
85 dflag = 1;
86 break;
87 case 'f':
88 fflag = 1;
89 iflag = 0;
90 break;
91 case 'i':
92 fflag = 0;
93 iflag = 1;
94 break;
95 case 'R':
96 case 'r': /* compatibility */
97 rflag = 1;
98 break;
99 case '?':
100 default:
101 usage();
102 }
103 argc -= optind;
104 argv += optind;
105
106 if (argc < 1)
107 usage();
108
109 checkdot(argv);
110
111 if (*argv) {
112 stdin_ok = isatty(STDIN_FILENO);
113
114 if (rflag)
115 rmtree(argv);
116 else
117 rmfile(argv);
118 }
119
120 exit(retval);
121 }
122
123 void
124 rmtree(argv)
125 char **argv;
126 {
127 register FTS *fts;
128 register FTSENT *p;
129 register int needstat;
130
131 /*
132 * Remove a file hierarchy. If forcing removal (-f), or interactive
133 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
134 */
135 needstat = !fflag && !iflag && stdin_ok;
136
137 /*
138 * If the -i option is specified, the user can skip on the pre-order
139 * visit. The fts_number field flags skipped directories.
140 */
141 #define SKIPPED 1
142
143 if (!(fts = fts_open(argv,
144 needstat ? FTS_PHYSICAL : FTS_PHYSICAL|FTS_NOSTAT,
145 (int (*)())NULL))) {
146 if (!fflag)
147 (void)fprintf(stderr, "rm: %s.\n", strerror(errno));
148 exit (1);
149 }
150
151 while ((p = fts_read(fts)) != NULL) {
152 switch(p->fts_info) {
153 case FTS_DNR:
154 case FTS_ERR:
155 error(p->fts_path, errno);
156 continue;
157 /*
158 * FTS_NS: assume that if can't stat the file, it can't be
159 * unlinked.
160 */
161 case FTS_NS:
162 if (!needstat)
163 break;
164 if (!fflag || errno != ENOENT)
165 error(p->fts_path, errno);
166 continue;
167 /* Pre-order: give user chance to skip. */
168 case FTS_D:
169 if (!fflag && !check(p->fts_path, p->fts_accpath,
170 p->fts_statp)) {
171 (void)fts_set(fts, p, FTS_SKIP);
172 p->fts_number = SKIPPED;
173 }
174 continue;
175 /* Post-order: see if user skipped. */
176 case FTS_DP:
177 if (p->fts_number == SKIPPED)
178 continue;
179 break;
180
181 default:
182 if (!fflag && !check(p->fts_path, p->fts_accpath,
183 p->fts_statp))
184 continue;
185 }
186
187 /*
188 * If we can't read or search the directory, may still be
189 * able to remove it. Don't print out the un{read,search}able
190 * message unless the remove fails.
191 */
192 if (p->fts_info == FTS_DP || p->fts_info == FTS_DNR) {
193 if (!rmdir(p->fts_accpath))
194 continue;
195 if (errno == ENOENT) {
196 if (fflag)
197 continue;
198 } else if (p->fts_info != FTS_DP && !fflag)
199 (void)fprintf(stderr,
200 "rm: unable to read %s.\n", p->fts_path);
201 } else if (!unlink(p->fts_accpath) || fflag && errno == ENOENT)
202 continue;
203 error(p->fts_path, errno);
204 }
205 }
206
207 void
208 rmfile(argv)
209 char **argv;
210 {
211 register char *f;
212 struct stat sb;
213
214 /*
215 * Remove a file. POSIX 1003.2 states that, by default, attempting
216 * to remove a directory is an error, so must always stat the file.
217 */
218 while ((f = *argv++) != NULL) {
219 /* If the file does not exist:
220 * If the -f option was not specified, write a diagnostic
221 * to the standard error...
222 */
223 if (lstat(f, &sb)) {
224 if (!fflag || errno != ENOENT) {
225 error(f, errno);
226 }
227 continue;
228 }
229
230 /* If the file is of type directory and neither the -r or -R
231 * (or -d) options are specified, write a diagnostic to the
232 * standard error...
233 */
234 if (S_ISDIR(sb.st_mode) && !dflag) {
235 error (f, EISDIR);
236 continue;
237 }
238
239 if (!fflag && !check(f, f, &sb)) {
240 continue;
241 }
242
243 /*
244 * rmdir() directories, unlink() files...
245 */
246 if ((S_ISDIR(sb.st_mode) ? rmdir(f) : unlink(f))) {
247 error(f, errno);
248 }
249 }
250 }
251
252 int
253 check(path, name, sp)
254 char *path, *name;
255 struct stat *sp;
256 {
257 register int first, ch;
258 char modep[15], *user_from_uid(), *group_from_gid();
259
260 /* Check -i first. */
261 if (iflag)
262 (void)fprintf(stderr, "remove %s? ", path);
263 else {
264 /*
265 * If it's not a symbolic link and it's unwritable and we're
266 * talking to a terminal, ask. Symbolic links are excluded
267 * because their permissions are meaningless.
268 */
269 if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK))
270 return(1);
271 strmode(sp->st_mode, modep);
272 (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
273 modep + 1, modep[9] == ' ' ? "" : " ",
274 user_from_uid(sp->st_uid, 0),
275 group_from_gid(sp->st_gid, 0), path);
276 }
277 (void)fflush(stderr);
278
279 first = ch = getchar();
280 while (ch != '\n' && ch != EOF)
281 ch = getchar();
282 return(first == 'y' || first == 'Y');
283 }
284
285 #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || (a)[1] == '.' && !(a)[2]))
286 void
287 checkdot(argv)
288 char **argv;
289 {
290 register char *p, **t, **save;
291 int complained;
292
293 complained = 0;
294 for (t = argv; *t;) {
295 if ((p = rindex(*t, '/')) != NULL)
296 ++p;
297 else
298 p = *t;
299 if (ISDOT(p)) {
300 if (!complained++)
301 (void)fprintf(stderr,
302 "rm: \".\" and \"..\" may not be removed.\n");
303 retval = 1;
304 for (save = t; t[0] = t[1]; ++t);
305 t = save;
306 } else
307 ++t;
308 }
309 }
310
311 void
312 error(name, val)
313 char *name;
314 int val;
315 {
316 if (!fflag)
317 (void)fprintf(stderr, "rm: %s: %s.\n", name, strerror(val));
318 retval = 1;
319 }
320
321 void
322 usage()
323 {
324 (void)fprintf(stderr, "usage: rm [-dfiRr] file ...\n");
325 exit(1);
326 }
327