VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/rm.c@ 1422

Last change on this file since 1422 was 1328, checked in by bird, 17 years ago

include our getopt.h

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

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