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

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

o Ported all kmk builtins to win32.
o Fixed serveral bugs in kmk builtins.
o Probably broke both linux, bsd and OS/2. :-)

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