VirtualBox

source: kBuild/trunk/src/gmakenew/kmkbuiltin/rm.c@ 902

Last change on this file since 902 was 809, checked in by bird, 18 years ago

Solaris + cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 KB
Line 
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static const char copyright[] =
33"@(#) Copyright (c) 1990, 1993, 1994\n\
34 The Regents of the University of California. All rights reserved.\n";
35#endif /* not lint */
36
37#ifndef lint
38static char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94";
39#endif /* not lint */
40#include <sys/cdefs.h>
41//__FBSDID("$FreeBSD: src/bin/rm/rm.c,v 1.47 2004/04/06 20:06:50 markm Exp $");
42#endif
43#ifdef __EMX__
44# define DO_RMTREE
45#endif
46
47#include <sys/stat.h>
48#ifndef _MSC_VER
49# include <sys/param.h>
50# include <sys/mount.h>
51#endif
52
53#include "err.h"
54#include <errno.h>
55#include <fcntl.h>
56#ifdef DO_RMTREE
57# include <fts.h>
58#endif
59#include <grp.h>
60#include <pwd.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <sysexits.h>
65#include <unistd.h>
66
67#ifdef __sun__
68# include "getopt.h"
69#endif
70#ifdef _MSC_VER
71# include "mscfakes.h"
72#endif
73
74#ifdef __EMX__
75#undef S_IFWHT
76#undef S_ISWHT
77#endif
78#ifndef S_IFWHT
79#define S_IFWHT 0
80#define S_ISWHT(s) 0
81#define undelete(s) (-1)
82#endif
83
84#if !defined(__FreeBSD__) && !defined(__APPLE__)
85extern void strmode(mode_t mode, char *p);
86#endif
87
88static int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
89static uid_t uid;
90
91static char *argv0;
92
93static int check(char *, char *, struct stat *);
94static void checkdot(char **);
95static void rm_file(char **);
96static int rm_overwrite(char *, struct stat *);
97#ifdef DO_RMTREE
98static void rm_tree(char **);
99#endif
100static int usage(void);
101
102/*
103 * rm --
104 * This rm is different from historic rm's, but is expected to match
105 * POSIX 1003.2 behavior. The most visible difference is that -f
106 * has two specific effects now, ignore non-existent files and force
107 * file removal.
108 */
109int
110kmk_builtin_rm(int argc, char *argv[])
111{
112 int ch, rflag;
113 char *p;
114
115 /* reinitialize globals */
116 argv0 = argv[0];
117 dflag = eval = fflag = iflag = Pflag = vflag = Wflag = stdin_ok = 0;
118 uid = 0;
119
120 /* kmk: reset getopt and set program name. */
121 g_progname = argv[0];
122 opterr = 1;
123 optarg = NULL;
124 optopt = 0;
125#if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__)
126 optreset = 1;
127 optind = 1;
128#else
129 optind = 0; /* init */
130#endif
131
132#if 0 /* kmk: we don't need this */
133 /*
134 * Test for the special case where the utility is called as
135 * "unlink", for which the functionality provided is greatly
136 * simplified.
137 */
138 if ((p = rindex(argv[0], '/')) == NULL)
139 p = argv[0];
140 else
141 ++p;
142 if (strcmp(p, "unlink") == 0) {
143 while (getopt(argc, argv, "") != -1)
144 return usage();
145 argc -= optind;
146 argv += optind;
147 if (argc != 1)
148 return usage();
149 rm_file(&argv[0]);
150 return eval;
151 }
152#else
153 (void)p;
154#endif
155 Pflag = rflag = 0;
156 while ((ch = getopt(argc, argv, "dfiPRrvW")) != -1)
157 switch(ch) {
158 case 'd':
159 dflag = 1;
160 break;
161 case 'f':
162 fflag = 1;
163 iflag = 0;
164 break;
165 case 'i':
166 fflag = 0;
167 iflag = 1;
168 break;
169 case 'P':
170 Pflag = 1;
171 break;
172 case 'R':
173 case 'r': /* Compatibility. */
174#ifdef DO_RMTREE
175 rflag = 1;
176 break;
177#else
178 errno = EINVAL;
179 return err(1, "Recursion is not supported!");
180#endif
181 case 'v':
182 vflag = 1;
183 break;
184#ifdef FTS_WHITEOUT
185 case 'W':
186 Wflag = 1;
187 break;
188#endif
189 default:
190 return usage();
191 }
192 argc -= optind;
193 argv += optind;
194
195 if (argc < 1) {
196 if (fflag)
197 return (0);
198 return usage();
199 }
200
201 checkdot(argv);
202 uid = geteuid();
203
204 if (*argv) {
205 stdin_ok = isatty(STDIN_FILENO);
206#ifdef DO_RMTREE
207 if (rflag)
208 rm_tree(argv);
209 else
210#endif
211 rm_file(argv);
212 }
213
214 return eval;
215}
216
217#ifdef DO_RMTREE
218static void
219rm_tree(char **argv)
220{
221 FTS *fts;
222 FTSENT *p;
223 int needstat;
224 int flags;
225 int rval;
226
227 /*
228 * Remove a file hierarchy. If forcing removal (-f), or interactive
229 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
230 */
231 needstat = !uid || (!fflag && !iflag && stdin_ok);
232
233 /*
234 * If the -i option is specified, the user can skip on the pre-order
235 * visit. The fts_number field flags skipped directories.
236 */
237#define SKIPPED 1
238
239 flags = FTS_PHYSICAL;
240 if (!needstat)
241 flags |= FTS_NOSTAT;
242#ifdef FTS_WHITEOUT
243 if (Wflag)
244 flags |= FTS_WHITEOUT;
245#endif
246 if (!(fts = fts_open(argv, flags, NULL))) {
247 eval = err(1, "fts_open");
248 return;
249 }
250 while ((p = fts_read(fts)) != NULL) {
251 switch (p->fts_info) {
252 case FTS_DNR:
253 if (!fflag || p->fts_errno != ENOENT) {
254 fprintf(stderr, "%s: %s: %s\n",
255 argv0, p->fts_path, strerror(p->fts_errno));
256 eval = 1;
257 }
258 continue;
259 case FTS_ERR:
260 eval = errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
261 return;
262 case FTS_NS:
263 /*
264 * Assume that since fts_read() couldn't stat the
265 * file, it can't be unlinked.
266 */
267 if (!needstat)
268 break;
269 if (!fflag || p->fts_errno != ENOENT) {
270 fprintf(stderr, "%s: %s: %s\n",
271 argv0, p->fts_path, strerror(p->fts_errno));
272 eval = 1;
273 }
274 continue;
275 case FTS_D:
276 /* Pre-order: give user chance to skip. */
277 if (!fflag && !check(p->fts_path, p->fts_accpath,
278 p->fts_statp)) {
279 (void)fts_set(fts, p, FTS_SKIP);
280 p->fts_number = SKIPPED;
281 }
282#ifdef UF_APPEND
283 else if (!uid &&
284 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
285 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
286 chflags(p->fts_accpath,
287 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
288 goto err;
289#endif
290 continue;
291 case FTS_DP:
292 /* Post-order: see if user skipped. */
293 if (p->fts_number == SKIPPED)
294 continue;
295 break;
296 default:
297 if (!fflag &&
298 !check(p->fts_path, p->fts_accpath, p->fts_statp))
299 continue;
300 }
301
302 rval = 0;
303#ifdef UF_APPEND
304 if (!uid &&
305 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
306 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
307 rval = chflags(p->fts_accpath,
308 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
309#endif
310 if (rval == 0) {
311 /*
312 * If we can't read or search the directory, may still be
313 * able to remove it. Don't print out the un{read,search}able
314 * message unless the remove fails.
315 */
316 switch (p->fts_info) {
317 case FTS_DP:
318 case FTS_DNR:
319 rval = rmdir(p->fts_accpath);
320 if (rval == 0 || (fflag && errno == ENOENT)) {
321 if (rval == 0 && vflag)
322 (void)printf("%s\n",
323 p->fts_path);
324 continue;
325 }
326 break;
327
328#ifdef FTS_W
329 case FTS_W:
330 rval = undelete(p->fts_accpath);
331 if (rval == 0 && (fflag && errno == ENOENT)) {
332 if (vflag)
333 (void)printf("%s\n",
334 p->fts_path);
335 continue;
336 }
337 break;
338#endif
339
340 case FTS_NS:
341 /*
342 * Assume that since fts_read() couldn't stat
343 * the file, it can't be unlinked.
344 */
345 if (fflag)
346 continue;
347 /* FALLTHROUGH */
348 default:
349 if (Pflag)
350 if (!rm_overwrite(p->fts_accpath, NULL))
351 continue;
352 rval = unlink(p->fts_accpath);
353#ifdef _MSC_VER
354 if (rval != 0) {
355 chmod(p->fts_accpath, 0777);
356 rval = unlink(p->fts_accpath);
357 }
358#endif
359
360 if (rval == 0 || (fflag && errno == ENOENT)) {
361 if (rval == 0 && vflag)
362 (void)printf("%s\n",
363 p->fts_path);
364 continue;
365 }
366 }
367 }
368err:
369 fprintf(stderr, "%s: %s: %s\n", argv0, p->fts_path, strerror(errno));
370 eval = 1;
371 }
372 if (errno) {
373 fprintf(stderr, "%s: fts_read: %s\n", argv0, strerror(errno));
374 eval = 1;
375 }
376}
377#endif /* DO_RMTREE */
378
379static void
380rm_file(char **argv)
381{
382 struct stat sb;
383 int rval;
384 char *f;
385
386 /*
387 * Remove a file. POSIX 1003.2 states that, by default, attempting
388 * to remove a directory is an error, so must always stat the file.
389 */
390 while ((f = *argv++) != NULL) {
391 /* Assume if can't stat the file, can't unlink it. */
392 if (lstat(f, &sb)) {
393#ifdef FTS_WHITEOUT
394 if (Wflag) {
395 sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
396 } else {
397#else
398 {
399#endif
400 if (!fflag || errno != ENOENT) {
401 fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(errno));
402 eval = 1;
403 }
404 continue;
405 }
406#ifdef FTS_WHITEOUT
407 } else if (Wflag) {
408 fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(EEXIST));
409 eval = 1;
410 continue;
411#endif
412 }
413
414 if (S_ISDIR(sb.st_mode) && !dflag) {
415 fprintf(stderr, "%s: %s: is a directory\n", argv0, f);
416 eval = 1;
417 continue;
418 }
419 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
420 continue;
421 rval = 0;
422#ifdef UF_APPEND
423 if (!uid &&
424 (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
425 !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
426 rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
427#endif
428 if (rval == 0) {
429 if (S_ISWHT(sb.st_mode))
430 rval = undelete(f);
431 else if (S_ISDIR(sb.st_mode))
432 rval = rmdir(f);
433 else {
434 if (Pflag)
435 if (!rm_overwrite(f, &sb))
436 continue;
437 rval = unlink(f);
438#ifdef _MSC_VER
439 if (rval != 0) {
440 chmod(f, 0777);
441 rval = unlink(f);
442 }
443#endif
444 }
445 }
446 if (rval && (!fflag || errno != ENOENT)) {
447 fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(errno));
448 eval = 1;
449 }
450 if (vflag && rval == 0)
451 (void)printf("%s\n", f);
452 }
453}
454
455/*
456 * rm_overwrite --
457 * Overwrite the file 3 times with varying bit patterns.
458 *
459 * XXX
460 * This is a cheap way to *really* delete files. Note that only regular
461 * files are deleted, directories (and therefore names) will remain.
462 * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
463 * System V file system). In a logging file system, you'll have to have
464 * kernel support.
465 */
466static int
467rm_overwrite(char *file, struct stat *sbp)
468{
469 struct stat sb;
470#ifdef HAVE_FSTATFS
471 struct statfs fsb;
472#endif
473 off_t len;
474 int bsize, fd, wlen;
475 char *buf = NULL;
476
477 fd = -1;
478 if (sbp == NULL) {
479 if (lstat(file, &sb))
480 goto err;
481 sbp = &sb;
482 }
483 if (!S_ISREG(sbp->st_mode))
484 return (1);
485 if ((fd = open(file, O_WRONLY, 0)) == -1)
486 goto err;
487#ifdef HAVE_FSTATFS
488 if (fstatfs(fd, &fsb) == -1)
489 goto err;
490 bsize = MAX(fsb.f_iosize, 1024);
491#elif defined(HAVE_ST_BLKSIZE)
492 bsize = MAX(sb.st_blksize, 1024);
493#else
494 bsize = 1024;
495#endif
496 if ((buf = malloc(bsize)) == NULL)
497 exit(err(1, "%s: malloc", file));
498
499#define PASS(byte) { \
500 memset(buf, byte, bsize); \
501 for (len = sbp->st_size; len > 0; len -= wlen) { \
502 wlen = len < bsize ? len : bsize; \
503 if (write(fd, buf, wlen) != wlen) \
504 goto err; \
505 } \
506}
507 PASS(0xff);
508 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
509 goto err;
510 PASS(0x00);
511 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
512 goto err;
513 PASS(0xff);
514 if (!fsync(fd) && !close(fd)) {
515 free(buf);
516 return (1);
517 }
518
519err: eval = 1;
520 if (buf)
521 free(buf);
522 if (fd != -1)
523 close(fd);
524 fprintf(stderr, "%s: %s: %s\n", argv0, file, strerror(errno));
525 return (0);
526}
527
528
529static int
530check(char *path, char *name, struct stat *sp)
531{
532 int ch, first;
533 char modep[15], *flagsp;
534
535 /* Check -i first. */
536 if (iflag)
537 (void)fprintf(stderr, "remove %s? ", path);
538 else {
539 /*
540 * If it's not a symbolic link and it's unwritable and we're
541 * talking to a terminal, ask. Symbolic links are excluded
542 * because their permissions are meaningless. Check stdin_ok
543 * first because we may not have stat'ed the file.
544 * Also skip this check if the -P option was specified because
545 * we will not be able to overwrite file contents and will
546 * barf later.
547 */
548 if (!stdin_ok || S_ISLNK(sp->st_mode) || Pflag ||
549 (!access(name, W_OK) &&
550#ifdef SF_APPEND
551 !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
552 (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))
553#else
554 1)
555#endif
556 )
557 return (1);
558 strmode(sp->st_mode, modep);
559#ifdef SF_APPEND
560 if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
561 exit(err(1, "fflagstostr"));
562 (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
563 modep + 1, modep[9] == ' ' ? "" : " ",
564 user_from_uid(sp->st_uid, 0),
565 group_from_gid(sp->st_gid, 0),
566 *flagsp ? flagsp : "", *flagsp ? " " : "",
567 path);
568 free(flagsp);
569#else
570 (void)flagsp;
571 (void)fprintf(stderr, "override %s%s %d/%d for %s? ",
572 modep + 1, modep[9] == ' ' ? "" : " ",
573 sp->st_uid, sp->st_gid, path);
574#endif
575 }
576 (void)fflush(stderr);
577
578 first = ch = getchar();
579 while (ch != '\n' && ch != EOF)
580 ch = getchar();
581 return (first == 'y' || first == 'Y');
582}
583
584#define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
585static void
586checkdot(char **argv)
587{
588 char *p, **save, **t;
589 int complained;
590
591 complained = 0;
592 for (t = argv; *t;) {
593 if ((p = strrchr(*t, '/')) != NULL)
594 ++p;
595 else
596 p = *t;
597 if (ISDOT(p)) {
598 if (!complained++)
599 fprintf(stderr, "%s: \".\" and \"..\" may not be removed\n", argv0);
600 eval = 1;
601 for (save = t; (t[0] = t[1]) != NULL; ++t)
602 continue;
603 t = save;
604 } else
605 ++t;
606 }
607}
608
609static int
610usage(void)
611{
612
613 (void)fprintf(stderr, "%s\n%s\n",
614 "usage: rm [-f | -i] [-dPRrvW] file ...\n",
615 " unlink file");
616 return EX_USAGE;
617}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette