Home | History | Annotate | Line # | Download | only in rm
rm.c revision 1.1.1.3
      1      1.1      cgd /*-
      2  1.1.1.2  mycroft  * Copyright (c) 1990, 1993, 1994
      3  1.1.1.2  mycroft  *	The Regents of the University of California.  All rights reserved.
      4      1.1      cgd  *
      5      1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6      1.1      cgd  * modification, are permitted provided that the following conditions
      7      1.1      cgd  * are met:
      8      1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9      1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10      1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12      1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13      1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14      1.1      cgd  *    must display the following acknowledgement:
     15      1.1      cgd  *	This product includes software developed by the University of
     16      1.1      cgd  *	California, Berkeley and its contributors.
     17      1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18      1.1      cgd  *    may be used to endorse or promote products derived from this software
     19      1.1      cgd  *    without specific prior written permission.
     20      1.1      cgd  *
     21      1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1      cgd  * SUCH DAMAGE.
     32      1.1      cgd  */
     33      1.1      cgd 
     34      1.1      cgd #ifndef lint
     35  1.1.1.2  mycroft static char copyright[] =
     36  1.1.1.2  mycroft "@(#) Copyright (c) 1990, 1993, 1994\n\
     37  1.1.1.2  mycroft 	The Regents of the University of California.  All rights reserved.\n";
     38      1.1      cgd #endif /* not lint */
     39      1.1      cgd 
     40      1.1      cgd #ifndef lint
     41  1.1.1.3      jtc static char sccsid[] = "@(#)rm.c	8.8 (Berkeley) 4/27/95";
     42      1.1      cgd #endif /* not lint */
     43      1.1      cgd 
     44      1.1      cgd #include <sys/types.h>
     45      1.1      cgd #include <sys/stat.h>
     46  1.1.1.2  mycroft 
     47  1.1.1.2  mycroft #include <err.h>
     48  1.1.1.2  mycroft #include <errno.h>
     49  1.1.1.2  mycroft #include <fcntl.h>
     50      1.1      cgd #include <fts.h>
     51      1.1      cgd #include <stdio.h>
     52      1.1      cgd #include <stdlib.h>
     53  1.1.1.2  mycroft #include <string.h>
     54  1.1.1.2  mycroft #include <unistd.h>
     55      1.1      cgd 
     56  1.1.1.3      jtc int dflag, eval, fflag, iflag, Pflag, Wflag, stdin_ok;
     57  1.1.1.2  mycroft 
     58  1.1.1.2  mycroft int	check __P((char *, char *, struct stat *));
     59  1.1.1.2  mycroft void	checkdot __P((char **));
     60  1.1.1.2  mycroft void	rm_file __P((char **));
     61  1.1.1.2  mycroft void	rm_overwrite __P((char *, struct stat *));
     62  1.1.1.2  mycroft void	rm_tree __P((char **));
     63  1.1.1.2  mycroft void	usage __P((void));
     64      1.1      cgd 
     65      1.1      cgd /*
     66      1.1      cgd  * rm --
     67      1.1      cgd  *	This rm is different from historic rm's, but is expected to match
     68      1.1      cgd  *	POSIX 1003.2 behavior.  The most visible difference is that -f
     69      1.1      cgd  *	has two specific effects now, ignore non-existent files and force
     70      1.1      cgd  * 	file removal.
     71      1.1      cgd  */
     72  1.1.1.2  mycroft int
     73      1.1      cgd main(argc, argv)
     74      1.1      cgd 	int argc;
     75  1.1.1.2  mycroft 	char *argv[];
     76      1.1      cgd {
     77      1.1      cgd 	int ch, rflag;
     78      1.1      cgd 
     79  1.1.1.2  mycroft 	Pflag = rflag = 0;
     80  1.1.1.3      jtc 	while ((ch = getopt(argc, argv, "dfiPRrW")) != -1)
     81      1.1      cgd 		switch(ch) {
     82      1.1      cgd 		case 'd':
     83      1.1      cgd 			dflag = 1;
     84      1.1      cgd 			break;
     85      1.1      cgd 		case 'f':
     86      1.1      cgd 			fflag = 1;
     87      1.1      cgd 			iflag = 0;
     88      1.1      cgd 			break;
     89      1.1      cgd 		case 'i':
     90      1.1      cgd 			fflag = 0;
     91      1.1      cgd 			iflag = 1;
     92      1.1      cgd 			break;
     93  1.1.1.2  mycroft 		case 'P':
     94  1.1.1.2  mycroft 			Pflag = 1;
     95  1.1.1.2  mycroft 			break;
     96      1.1      cgd 		case 'R':
     97  1.1.1.2  mycroft 		case 'r':			/* Compatibility. */
     98      1.1      cgd 			rflag = 1;
     99      1.1      cgd 			break;
    100  1.1.1.3      jtc 		case 'W':
    101  1.1.1.3      jtc 			Wflag = 1;
    102  1.1.1.3      jtc 			break;
    103      1.1      cgd 		case '?':
    104      1.1      cgd 		default:
    105      1.1      cgd 			usage();
    106      1.1      cgd 		}
    107      1.1      cgd 	argc -= optind;
    108      1.1      cgd 	argv += optind;
    109      1.1      cgd 
    110      1.1      cgd 	if (argc < 1)
    111      1.1      cgd 		usage();
    112      1.1      cgd 
    113      1.1      cgd 	checkdot(argv);
    114      1.1      cgd 
    115  1.1.1.3      jtc 	if (*argv) {
    116  1.1.1.3      jtc 		stdin_ok = isatty(STDIN_FILENO);
    117  1.1.1.3      jtc 
    118  1.1.1.3      jtc 		if (rflag)
    119  1.1.1.3      jtc 			rm_tree(argv);
    120  1.1.1.3      jtc 		else
    121  1.1.1.3      jtc 			rm_file(argv);
    122  1.1.1.3      jtc 	}
    123      1.1      cgd 
    124  1.1.1.2  mycroft 	exit (eval);
    125      1.1      cgd }
    126      1.1      cgd 
    127  1.1.1.2  mycroft void
    128  1.1.1.2  mycroft rm_tree(argv)
    129      1.1      cgd 	char **argv;
    130      1.1      cgd {
    131  1.1.1.2  mycroft 	FTS *fts;
    132  1.1.1.2  mycroft 	FTSENT *p;
    133  1.1.1.2  mycroft 	int needstat;
    134  1.1.1.3      jtc 	int flags;
    135      1.1      cgd 
    136      1.1      cgd 	/*
    137      1.1      cgd 	 * Remove a file hierarchy.  If forcing removal (-f), or interactive
    138      1.1      cgd 	 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
    139      1.1      cgd 	 */
    140      1.1      cgd 	needstat = !fflag && !iflag && stdin_ok;
    141      1.1      cgd 
    142      1.1      cgd 	/*
    143      1.1      cgd 	 * If the -i option is specified, the user can skip on the pre-order
    144      1.1      cgd 	 * visit.  The fts_number field flags skipped directories.
    145      1.1      cgd 	 */
    146      1.1      cgd #define	SKIPPED	1
    147      1.1      cgd 
    148  1.1.1.3      jtc 	flags = FTS_PHYSICAL;
    149  1.1.1.3      jtc 	if (!needstat)
    150  1.1.1.3      jtc 		flags |= FTS_NOSTAT;
    151  1.1.1.3      jtc 	if (Wflag)
    152  1.1.1.3      jtc 		flags |= FTS_WHITEOUT;
    153  1.1.1.3      jtc 	if (!(fts = fts_open(argv, flags, (int (*)())NULL)))
    154  1.1.1.2  mycroft 		err(1, NULL);
    155  1.1.1.2  mycroft 	while ((p = fts_read(fts)) != NULL) {
    156  1.1.1.2  mycroft 		switch (p->fts_info) {
    157      1.1      cgd 		case FTS_DNR:
    158  1.1.1.2  mycroft 			if (!fflag || p->fts_errno != ENOENT) {
    159  1.1.1.2  mycroft 				warnx("%s: %s",
    160  1.1.1.2  mycroft 				    p->fts_path, strerror(p->fts_errno));
    161  1.1.1.2  mycroft 				eval = 1;
    162  1.1.1.2  mycroft 			}
    163  1.1.1.2  mycroft 			continue;
    164      1.1      cgd 		case FTS_ERR:
    165  1.1.1.2  mycroft 			errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
    166      1.1      cgd 		case FTS_NS:
    167  1.1.1.2  mycroft 			/*
    168  1.1.1.2  mycroft 			 * FTS_NS: assume that if can't stat the file, it
    169  1.1.1.2  mycroft 			 * can't be unlinked.
    170  1.1.1.2  mycroft 			 */
    171      1.1      cgd 			if (!needstat)
    172      1.1      cgd 				break;
    173  1.1.1.2  mycroft 			if (!fflag || p->fts_errno != ENOENT) {
    174  1.1.1.2  mycroft 				warnx("%s: %s",
    175  1.1.1.2  mycroft 				    p->fts_path, strerror(p->fts_errno));
    176  1.1.1.2  mycroft 				eval = 1;
    177  1.1.1.2  mycroft 			}
    178      1.1      cgd 			continue;
    179      1.1      cgd 		case FTS_D:
    180  1.1.1.2  mycroft 			/* Pre-order: give user chance to skip. */
    181  1.1.1.3      jtc 			if (!fflag && !check(p->fts_path, p->fts_accpath,
    182  1.1.1.2  mycroft 			    p->fts_statp)) {
    183      1.1      cgd 				(void)fts_set(fts, p, FTS_SKIP);
    184      1.1      cgd 				p->fts_number = SKIPPED;
    185      1.1      cgd 			}
    186      1.1      cgd 			continue;
    187      1.1      cgd 		case FTS_DP:
    188  1.1.1.2  mycroft 			/* Post-order: see if user skipped. */
    189      1.1      cgd 			if (p->fts_number == SKIPPED)
    190      1.1      cgd 				continue;
    191      1.1      cgd 			break;
    192  1.1.1.3      jtc 		default:
    193  1.1.1.3      jtc 			if (!fflag &&
    194  1.1.1.3      jtc 			    !check(p->fts_path, p->fts_accpath, p->fts_statp))
    195  1.1.1.3      jtc 				continue;
    196      1.1      cgd 		}
    197      1.1      cgd 
    198      1.1      cgd 		/*
    199      1.1      cgd 		 * If we can't read or search the directory, may still be
    200      1.1      cgd 		 * able to remove it.  Don't print out the un{read,search}able
    201      1.1      cgd 		 * message unless the remove fails.
    202      1.1      cgd 		 */
    203  1.1.1.3      jtc 		switch (p->fts_info) {
    204  1.1.1.3      jtc 		case FTS_DP:
    205  1.1.1.3      jtc 		case FTS_DNR:
    206  1.1.1.3      jtc 			if (!rmdir(p->fts_accpath) || fflag && errno == ENOENT)
    207      1.1      cgd 				continue;
    208  1.1.1.3      jtc 			break;
    209  1.1.1.3      jtc 
    210  1.1.1.3      jtc 		case FTS_W:
    211  1.1.1.3      jtc 			if (!undelete(p->fts_accpath) ||
    212  1.1.1.3      jtc 			    fflag && errno == ENOENT)
    213  1.1.1.3      jtc 				continue;
    214  1.1.1.3      jtc 			break;
    215  1.1.1.3      jtc 
    216  1.1.1.3      jtc 		default:
    217  1.1.1.2  mycroft 			if (Pflag)
    218  1.1.1.2  mycroft 				rm_overwrite(p->fts_accpath, NULL);
    219  1.1.1.2  mycroft 			if (!unlink(p->fts_accpath) || fflag && errno == ENOENT)
    220  1.1.1.2  mycroft 				continue;
    221  1.1.1.2  mycroft 		}
    222  1.1.1.2  mycroft 		warn("%s", p->fts_path);
    223  1.1.1.2  mycroft 		eval = 1;
    224      1.1      cgd 	}
    225  1.1.1.2  mycroft 	if (errno)
    226  1.1.1.2  mycroft 		err(1, "fts_read");
    227      1.1      cgd }
    228      1.1      cgd 
    229  1.1.1.2  mycroft void
    230  1.1.1.2  mycroft rm_file(argv)
    231      1.1      cgd 	char **argv;
    232      1.1      cgd {
    233      1.1      cgd 	struct stat sb;
    234  1.1.1.3      jtc 	int rval;
    235  1.1.1.2  mycroft 	char *f;
    236      1.1      cgd 
    237      1.1      cgd 	/*
    238      1.1      cgd 	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
    239      1.1      cgd 	 * to remove a directory is an error, so must always stat the file.
    240      1.1      cgd 	 */
    241  1.1.1.2  mycroft 	while ((f = *argv++) != NULL) {
    242      1.1      cgd 		/* Assume if can't stat the file, can't unlink it. */
    243      1.1      cgd 		if (lstat(f, &sb)) {
    244  1.1.1.3      jtc 			if (Wflag) {
    245  1.1.1.3      jtc 				sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
    246  1.1.1.3      jtc 			} else {
    247  1.1.1.3      jtc 				if (!fflag || errno != ENOENT) {
    248  1.1.1.3      jtc 					warn("%s", f);
    249  1.1.1.3      jtc 					eval = 1;
    250  1.1.1.3      jtc 				}
    251  1.1.1.3      jtc 				continue;
    252  1.1.1.2  mycroft 			}
    253  1.1.1.3      jtc 		} else if (Wflag) {
    254  1.1.1.3      jtc 			warnx("%s: %s", f, strerror(EEXIST));
    255  1.1.1.3      jtc 			eval = 1;
    256      1.1      cgd 			continue;
    257      1.1      cgd 		}
    258  1.1.1.3      jtc 
    259  1.1.1.3      jtc 		if (S_ISDIR(sb.st_mode) && !dflag) {
    260  1.1.1.2  mycroft 			warnx("%s: is a directory", f);
    261  1.1.1.2  mycroft 			eval = 1;
    262      1.1      cgd 			continue;
    263      1.1      cgd 		}
    264  1.1.1.3      jtc 		if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
    265      1.1      cgd 			continue;
    266  1.1.1.3      jtc 		if (S_ISWHT(sb.st_mode))
    267  1.1.1.3      jtc 			rval = undelete(f);
    268  1.1.1.3      jtc 		else if (S_ISDIR(sb.st_mode))
    269  1.1.1.2  mycroft 			rval = rmdir(f);
    270  1.1.1.2  mycroft 		else {
    271  1.1.1.2  mycroft 			if (Pflag)
    272  1.1.1.2  mycroft 				rm_overwrite(f, &sb);
    273  1.1.1.2  mycroft 			rval = unlink(f);
    274  1.1.1.2  mycroft 		}
    275  1.1.1.2  mycroft 		if (rval && (!fflag || errno != ENOENT)) {
    276  1.1.1.2  mycroft 			warn("%s", f);
    277  1.1.1.2  mycroft 			eval = 1;
    278  1.1.1.2  mycroft 		}
    279      1.1      cgd 	}
    280      1.1      cgd }
    281      1.1      cgd 
    282  1.1.1.2  mycroft /*
    283  1.1.1.2  mycroft  * rm_overwrite --
    284  1.1.1.2  mycroft  *	Overwrite the file 3 times with varying bit patterns.
    285  1.1.1.2  mycroft  *
    286  1.1.1.2  mycroft  * XXX
    287  1.1.1.2  mycroft  * This is a cheap way to *really* delete files.  Note that only regular
    288  1.1.1.2  mycroft  * files are deleted, directories (and therefore names) will remain.
    289  1.1.1.2  mycroft  * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
    290  1.1.1.2  mycroft  * System V file system).  In a logging file system, you'll have to have
    291  1.1.1.2  mycroft  * kernel support.
    292  1.1.1.2  mycroft  */
    293  1.1.1.2  mycroft void
    294  1.1.1.2  mycroft rm_overwrite(file, sbp)
    295  1.1.1.2  mycroft 	char *file;
    296  1.1.1.2  mycroft 	struct stat *sbp;
    297  1.1.1.2  mycroft {
    298  1.1.1.2  mycroft 	struct stat sb;
    299  1.1.1.2  mycroft 	off_t len;
    300  1.1.1.2  mycroft 	int fd, wlen;
    301  1.1.1.2  mycroft 	char buf[8 * 1024];
    302  1.1.1.2  mycroft 
    303  1.1.1.2  mycroft 	fd = -1;
    304  1.1.1.2  mycroft 	if (sbp == NULL) {
    305  1.1.1.2  mycroft 		if (lstat(file, &sb))
    306  1.1.1.2  mycroft 			goto err;
    307  1.1.1.2  mycroft 		sbp = &sb;
    308  1.1.1.2  mycroft 	}
    309  1.1.1.2  mycroft 	if (!S_ISREG(sbp->st_mode))
    310  1.1.1.2  mycroft 		return;
    311  1.1.1.2  mycroft 	if ((fd = open(file, O_WRONLY, 0)) == -1)
    312  1.1.1.2  mycroft 		goto err;
    313  1.1.1.2  mycroft 
    314  1.1.1.2  mycroft #define	PASS(byte) {							\
    315  1.1.1.2  mycroft 	memset(buf, byte, sizeof(buf));					\
    316  1.1.1.2  mycroft 	for (len = sbp->st_size; len > 0; len -= wlen) {		\
    317  1.1.1.2  mycroft 		wlen = len < sizeof(buf) ? len : sizeof(buf);		\
    318  1.1.1.2  mycroft 		if (write(fd, buf, wlen) != wlen)			\
    319  1.1.1.2  mycroft 			goto err;					\
    320  1.1.1.2  mycroft 	}								\
    321  1.1.1.2  mycroft }
    322  1.1.1.2  mycroft 	PASS(0xff);
    323  1.1.1.2  mycroft 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
    324  1.1.1.2  mycroft 		goto err;
    325  1.1.1.2  mycroft 	PASS(0x00);
    326  1.1.1.2  mycroft 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
    327  1.1.1.2  mycroft 		goto err;
    328  1.1.1.2  mycroft 	PASS(0xff);
    329  1.1.1.2  mycroft 	if (!fsync(fd) && !close(fd))
    330  1.1.1.2  mycroft 		return;
    331  1.1.1.2  mycroft 
    332  1.1.1.2  mycroft err:	eval = 1;
    333  1.1.1.2  mycroft 	warn("%s", file);
    334  1.1.1.2  mycroft }
    335  1.1.1.2  mycroft 
    336  1.1.1.2  mycroft 
    337  1.1.1.2  mycroft int
    338      1.1      cgd check(path, name, sp)
    339      1.1      cgd 	char *path, *name;
    340      1.1      cgd 	struct stat *sp;
    341      1.1      cgd {
    342  1.1.1.2  mycroft 	int ch, first;
    343  1.1.1.2  mycroft 	char modep[15];
    344      1.1      cgd 
    345      1.1      cgd 	/* Check -i first. */
    346      1.1      cgd 	if (iflag)
    347      1.1      cgd 		(void)fprintf(stderr, "remove %s? ", path);
    348      1.1      cgd 	else {
    349      1.1      cgd 		/*
    350      1.1      cgd 		 * If it's not a symbolic link and it's unwritable and we're
    351      1.1      cgd 		 * talking to a terminal, ask.  Symbolic links are excluded
    352  1.1.1.2  mycroft 		 * because their permissions are meaningless.  Check stdin_ok
    353  1.1.1.2  mycroft 		 * first because we may not have stat'ed the file.
    354      1.1      cgd 		 */
    355  1.1.1.2  mycroft 		if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK))
    356  1.1.1.2  mycroft 			return (1);
    357      1.1      cgd 		strmode(sp->st_mode, modep);
    358      1.1      cgd 		(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
    359      1.1      cgd 		    modep + 1, modep[9] == ' ' ? "" : " ",
    360      1.1      cgd 		    user_from_uid(sp->st_uid, 0),
    361      1.1      cgd 		    group_from_gid(sp->st_gid, 0), path);
    362      1.1      cgd 	}
    363      1.1      cgd 	(void)fflush(stderr);
    364      1.1      cgd 
    365      1.1      cgd 	first = ch = getchar();
    366      1.1      cgd 	while (ch != '\n' && ch != EOF)
    367      1.1      cgd 		ch = getchar();
    368  1.1.1.2  mycroft 	return (first == 'y');
    369      1.1      cgd }
    370      1.1      cgd 
    371      1.1      cgd #define ISDOT(a)	((a)[0] == '.' && (!(a)[1] || (a)[1] == '.' && !(a)[2]))
    372  1.1.1.2  mycroft void
    373      1.1      cgd checkdot(argv)
    374      1.1      cgd 	char **argv;
    375      1.1      cgd {
    376  1.1.1.2  mycroft 	char *p, **save, **t;
    377      1.1      cgd 	int complained;
    378      1.1      cgd 
    379      1.1      cgd 	complained = 0;
    380      1.1      cgd 	for (t = argv; *t;) {
    381  1.1.1.2  mycroft 		if ((p = strrchr(*t, '/')) != NULL)
    382      1.1      cgd 			++p;
    383      1.1      cgd 		else
    384      1.1      cgd 			p = *t;
    385      1.1      cgd 		if (ISDOT(p)) {
    386      1.1      cgd 			if (!complained++)
    387  1.1.1.2  mycroft 				warnx("\".\" and \"..\" may not be removed");
    388  1.1.1.2  mycroft 			eval = 1;
    389  1.1.1.3      jtc 			for (save = t; (t[0] = t[1]) != NULL; ++t)
    390  1.1.1.3      jtc 				continue;
    391      1.1      cgd 			t = save;
    392      1.1      cgd 		} else
    393      1.1      cgd 			++t;
    394      1.1      cgd 	}
    395      1.1      cgd }
    396      1.1      cgd 
    397  1.1.1.2  mycroft void
    398      1.1      cgd usage()
    399      1.1      cgd {
    400  1.1.1.2  mycroft 
    401  1.1.1.3      jtc 	(void)fprintf(stderr, "usage: rm [-dfiPRrW] file ...\n");
    402      1.1      cgd 	exit(1);
    403      1.1      cgd }
    404