source: trunk/src/gmake/kmkbuiltin/rm.c@ 382

Last change on this file since 382 was 375, checked in by bird, 20 years ago

Re-initialize globals.

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