file_cmds-116.9.tar.gz
[apple/file_cmds.git] / rm / rm.c
1 /*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. 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 static const char copyright[] =
36 "@(#) Copyright (c) 1990, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94";
43 #else
44 static const char rcsid[] =
45 "$FreeBSD: src/bin/rm/rm.c,v 1.33 2001/06/13 15:01:25 ru Exp $";
46 #endif
47 #endif /* not lint */
48
49 #include <sys/stat.h>
50 #include <sys/param.h>
51 #include <sys/mount.h>
52
53 #include <err.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <fts.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <sysexits.h>
61 #include <unistd.h>
62
63 #ifdef __APPLE__
64 #include "get_compat.h"
65 #else
66 #define COMPAT_MODE(func, mode) 1
67 #endif
68
69 int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
70 uid_t uid;
71
72 int check __P((char *, char *, struct stat *));
73 int checkdir __P((char *));
74 int yes_or_no __P((void));
75 void checkdot __P((char **));
76 void rm_file __P((char **));
77 void rm_overwrite __P((char *, struct stat *));
78 void rm_tree __P((char **));
79 void usage __P((void));
80
81 /*
82 * rm --
83 * This rm is different from historic rm's, but is expected to match
84 * POSIX 1003.2 behavior. The most visible difference is that -f
85 * has two specific effects now, ignore non-existent files and force
86 * file removal.
87 */
88 int
89 main(argc, argv)
90 int argc;
91 char *argv[];
92 {
93 int ch, rflag;
94 char *p;
95
96 /*
97 * Test for the special case where the utility is called as
98 * "unlink", for which the functionality provided is greatly
99 * simplified.
100 */
101 if ((p = rindex(argv[0], '/')) == NULL)
102 p = argv[0];
103 else
104 ++p;
105 if (strcmp(p, "unlink") == 0) {
106 if (argc == 2) {
107 rm_file(&argv[1]);
108 exit(eval);
109 } else
110 usage();
111 }
112
113 Pflag = rflag = 0;
114 while ((ch = getopt(argc, argv, "dfiPRrvW")) != -1)
115 switch(ch) {
116 case 'd':
117 dflag = 1;
118 break;
119 case 'f':
120 fflag = 1;
121 iflag = 0;
122 break;
123 case 'i':
124 fflag = 0;
125 iflag = 1;
126 break;
127 case 'P':
128 Pflag = 1;
129 break;
130 case 'R':
131 case 'r': /* Compatibility. */
132 rflag = 1;
133 break;
134 case 'v':
135 vflag = 1;
136 break;
137 case 'W':
138 Wflag = 1;
139 break;
140 default:
141 usage();
142 }
143 argc -= optind;
144 argv += optind;
145
146 if (argc < 1) {
147 if (fflag)
148 return 0;
149 usage();
150 }
151
152 checkdot(argv);
153 uid = geteuid();
154
155 if (*argv) {
156 stdin_ok = isatty(STDIN_FILENO);
157
158 if (rflag)
159 rm_tree(argv);
160 else
161 rm_file(argv);
162 }
163
164 exit (eval);
165 }
166
167 void
168 rm_tree(argv)
169 char **argv;
170 {
171 FTS *fts;
172 FTSENT *p;
173 int needstat;
174 int flags;
175 int rval;
176 int wantConformance = COMPAT_MODE("bin/rm", "unix2003");
177 /*
178 * Remove a file hierarchy. If forcing removal (-f), or interactive
179 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
180 */
181 needstat = !uid || (!fflag && !iflag && stdin_ok);
182
183 /*
184 * If the -i option is specified, the user can skip on the pre-order
185 * visit. The fts_number field flags skipped directories.
186 */
187 #define SKIPPED 1
188
189 flags = FTS_PHYSICAL;
190 if (!needstat)
191 flags |= FTS_NOSTAT;
192 if (Wflag)
193 flags |= FTS_WHITEOUT;
194 if (!(fts = fts_open(argv, flags, NULL))) {
195 if (fflag && errno == ENOENT)
196 return;
197 err(1, NULL);
198 }
199 while ((p = fts_read(fts)) != NULL) {
200 switch (p->fts_info) {
201 case FTS_DNR:
202 if (!fflag || p->fts_errno != ENOENT) {
203 warnx("%s: %s",
204 p->fts_path, strerror(p->fts_errno));
205 eval = 1;
206 }
207 continue;
208 case FTS_ERR:
209 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
210 case FTS_NS:
211 /*
212 * FTS_NS: assume that if can't stat the file, it
213 * can't be unlinked.
214 */
215 if (!needstat)
216 break;
217 if (!fflag || p->fts_errno != ENOENT) {
218 warnx("%s: %s",
219 p->fts_path, strerror(p->fts_errno));
220 eval = 1;
221 }
222 continue;
223 case FTS_D:
224 /* Pre-order: give user chance to skip. */
225 /* In conformance mode the user is prompted to skip processing the contents.
226 * Then the option to delete the dir is presented post-order */
227 if (!fflag &&
228 ( (wantConformance && !checkdir(p->fts_path)) ||
229 (!wantConformance && !check(p->fts_path, p->fts_accpath, p->fts_statp))
230 )
231 ){
232 (void)fts_set(fts, p, FTS_SKIP);
233 p->fts_number = SKIPPED;
234 }
235 else if (!uid &&
236 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
237 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
238 chflags(p->fts_accpath,
239 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
240 goto err;
241 continue;
242 case FTS_DP:
243 /* Post-order: see if user skipped. */
244 if(p->fts_number == SKIPPED)/*in legacy mode, the user was prompted pre-order */
245 continue;
246 else if(wantConformance)
247 {
248 /* delete directory if force is on, or if user answers Y to prompt */
249 if(fflag || check(p->fts_path, p->fts_accpath, p->fts_statp))
250 break;
251 else
252 continue;
253 }
254 break;
255 default:
256 if (!fflag &&
257 !check(p->fts_path, p->fts_accpath, p->fts_statp))
258 continue;
259 }
260
261 rval = 0;
262 if (!uid &&
263 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
264 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
265 rval = chflags(p->fts_accpath,
266 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
267 if (rval == 0) {
268 /*
269 * If we can't read or search the directory, may still be
270 * able to remove it. Don't print out the un{read,search}able
271 * message unless the remove fails.
272 */
273 switch (p->fts_info) {
274 case FTS_DP:
275 case FTS_DNR:
276 rval = rmdir(p->fts_accpath);
277 if (rval == 0 || (fflag && errno == ENOENT)) {
278 if (rval == 0 && vflag)
279 (void)printf("%s\n",
280 p->fts_path);
281 continue;
282 }
283 break;
284
285 case FTS_W:
286 rval = undelete(p->fts_accpath);
287 if (rval == 0 && (fflag && errno == ENOENT)) {
288 if (vflag)
289 (void)printf("%s\n",
290 p->fts_path);
291 continue;
292 }
293 break;
294
295 default:
296 if (Pflag)
297 rm_overwrite(p->fts_accpath, NULL);
298 rval = unlink(p->fts_accpath);
299 if (rval == 0 || (fflag && errno == ENOENT)) {
300 if (rval == 0 && vflag)
301 (void)printf("%s\n",
302 p->fts_path);
303 continue;
304 }
305 }
306 }
307 err:
308 warn("%s", p->fts_path);
309 eval = 1;
310 }
311 if (errno)
312 err(1, "fts_read");
313 }
314
315 void
316 rm_file(argv)
317 char **argv;
318 {
319 struct stat sb;
320 int rval;
321 char *f;
322
323 /*
324 * Remove a file. POSIX 1003.2 states that, by default, attempting
325 * to remove a directory is an error, so must always stat the file.
326 */
327 while ((f = *argv++) != NULL) {
328 /* Assume if can't stat the file, can't unlink it. */
329 if (lstat(f, &sb)) {
330 if (Wflag) {
331 sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
332 } else {
333 if (!fflag || errno != ENOENT) {
334 warn("%s", f);
335 eval = 1;
336 }
337 continue;
338 }
339 } else if (Wflag) {
340 warnx("%s: %s", f, strerror(EEXIST));
341 eval = 1;
342 continue;
343 }
344
345 if (S_ISDIR(sb.st_mode) && !dflag) {
346 warnx("%s: is a directory", f);
347 eval = 1;
348 continue;
349 }
350 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
351 continue;
352 rval = 0;
353 if (!uid &&
354 (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
355 !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
356 rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
357 if (rval == 0) {
358 if (S_ISWHT(sb.st_mode))
359 rval = undelete(f);
360 else if (S_ISDIR(sb.st_mode))
361 rval = rmdir(f);
362 else {
363 if (Pflag)
364 rm_overwrite(f, &sb);
365 rval = unlink(f);
366 }
367 }
368 if (rval && (!fflag || errno != ENOENT)) {
369 warn("%s", f);
370 eval = 1;
371 }
372 if (vflag && rval == 0)
373 (void)printf("%s\n", f);
374 }
375 }
376
377 /*
378 * rm_overwrite --
379 * Overwrite the file 3 times with varying bit patterns.
380 *
381 * XXX
382 * This is a cheap way to *really* delete files. Note that only regular
383 * files are deleted, directories (and therefore names) will remain.
384 * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
385 * System V file system). In a logging file system, you'll have to have
386 * kernel support.
387 */
388 void
389 rm_overwrite(file, sbp)
390 char *file;
391 struct stat *sbp;
392 {
393 struct stat sb;
394 struct statfs fsb;
395 off_t len;
396 int bsize, fd, wlen;
397 char *buf = NULL;
398
399 fd = -1;
400 if (sbp == NULL) {
401 if (lstat(file, &sb))
402 goto err;
403 sbp = &sb;
404 }
405 if (!S_ISREG(sbp->st_mode))
406 return;
407 if ((fd = open(file, O_WRONLY, 0)) == -1)
408 goto err;
409 if (fstatfs(fd, &fsb) == -1)
410 goto err;
411 bsize = MAX(fsb.f_iosize, 1024);
412 if ((buf = malloc(bsize)) == NULL)
413 err(1, "malloc");
414
415 #define PASS(byte) { \
416 memset(buf, byte, bsize); \
417 for (len = sbp->st_size; len > 0; len -= wlen) { \
418 wlen = len < bsize ? len : bsize; \
419 if (write(fd, buf, wlen) != wlen) \
420 goto err; \
421 } \
422 }
423 PASS(0xff);
424 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
425 goto err;
426 PASS(0x00);
427 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
428 goto err;
429 PASS(0xff);
430 if (!fsync(fd) && !close(fd)) {
431 free(buf);
432 return;
433 }
434
435 err: eval = 1;
436 if (buf)
437 free(buf);
438 warn("%s", file);
439 }
440
441 int
442 yes_or_no()
443 {
444 int ch, first;
445 (void)fflush(stderr);
446
447 first = ch = getchar();
448 while (ch != '\n' && ch != EOF)
449 ch = getchar();
450 return (first == 'y' || first == 'Y');
451 }
452
453 int
454 checkdir(path)
455 char *path;
456 {
457 if(!iflag)
458 return 1; //if not interactive, process directory's contents
459 (void)fprintf(stderr, "examine files in directory %s? ", path);
460 return yes_or_no();
461 }
462
463 int
464 check(path, name, sp)
465 char *path, *name;
466 struct stat *sp;
467 {
468 char modep[15], *flagsp;
469
470 /* Check -i first. */
471 if (iflag)
472 (void)fprintf(stderr, "remove %s? ", path);
473 else {
474 /*
475 * If it's not a symbolic link and it's unwritable and we're
476 * talking to a terminal, ask. Symbolic links are excluded
477 * because their permissions are meaningless. Check stdin_ok
478 * first because we may not have stat'ed the file.
479 */
480 if (!stdin_ok || S_ISLNK(sp->st_mode) ||
481 (!access(name, W_OK) &&
482 !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
483 (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)))
484 return (1);
485 strmode(sp->st_mode, modep);
486 if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
487 err(1, NULL);
488 (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
489 modep + 1, modep[9] == ' ' ? "" : " ",
490 user_from_uid(sp->st_uid, 0),
491 group_from_gid(sp->st_gid, 0),
492 *flagsp ? flagsp : "", *flagsp ? " " : "",
493 path);
494 free(flagsp);
495 }
496 return yes_or_no();
497 }
498
499
500 #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
501 void
502 checkdot(argv)
503 char **argv;
504 {
505 char *p, **save, **t;
506 int complained;
507
508 complained = 0;
509 for (t = argv; *t;) {
510 if ((p = strrchr(*t, '/')) != NULL)
511 ++p;
512 else
513 p = *t;
514 if (ISDOT(p)) {
515 if (!complained++)
516 warnx("\".\" and \"..\" may not be removed");
517 eval = 1;
518 for (save = t; (t[0] = t[1]) != NULL; ++t)
519 continue;
520 t = save;
521 } else
522 ++t;
523 }
524 }
525
526 void
527 usage()
528 {
529
530 (void)fprintf(stderr, "%s\n%s\n",
531 "usage: rm [-f | -i] [-dPRrvW] file ...",
532 " unlink file");
533 exit(EX_USAGE);
534 }