Home | History | Annotate | Line # | Download | only in rm
rm.c revision 1.16
      1   1.1      cgd /*-
      2  1.15  mycroft  * Copyright (c) 1990, 1993, 1994
      3  1.15  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.15  mycroft static char copyright[] =
     36  1.15  mycroft "@(#) Copyright (c) 1990, 1993, 1994\n\
     37  1.15  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.15  mycroft /*static char sccsid[] = "from: @(#)rm.c	8.5 (Berkeley) 4/18/94";*/
     42  1.16      jtc static char *rcsid = "$Id: rm.c,v 1.16 1994/11/02 16:17:14 jtc Exp $";
     43   1.1      cgd #endif /* not lint */
     44   1.1      cgd 
     45  1.15  mycroft #include <sys/types.h>
     46  1.15  mycroft #include <sys/stat.h>
     47  1.15  mycroft 
     48  1.15  mycroft #include <locale.h>
     49  1.15  mycroft #include <err.h>
     50  1.15  mycroft #include <errno.h>
     51  1.15  mycroft #include <fcntl.h>
     52  1.15  mycroft #include <fts.h>
     53   1.7      jtc #include <stdio.h>
     54  1.15  mycroft #include <stdlib.h>
     55   1.7      jtc #include <string.h>
     56   1.7      jtc #include <unistd.h>
     57   1.7      jtc 
     58  1.15  mycroft int dflag, eval, fflag, iflag, Pflag, stdin_ok;
     59   1.1      cgd 
     60  1.15  mycroft int	check __P((char *, char *, struct stat *));
     61  1.15  mycroft void	checkdot __P((char **));
     62  1.15  mycroft void	rm_file __P((char **));
     63  1.15  mycroft void	rm_overwrite __P((char *, struct stat *));
     64  1.15  mycroft void	rm_tree __P((char **));
     65  1.15  mycroft void	usage __P((void));
     66   1.1      cgd 
     67   1.1      cgd /*
     68   1.1      cgd  * rm --
     69   1.1      cgd  *	This rm is different from historic rm's, but is expected to match
     70   1.1      cgd  *	POSIX 1003.2 behavior.  The most visible difference is that -f
     71   1.1      cgd  *	has two specific effects now, ignore non-existent files and force
     72   1.1      cgd  * 	file removal.
     73   1.1      cgd  */
     74   1.7      jtc int
     75   1.1      cgd main(argc, argv)
     76   1.1      cgd 	int argc;
     77  1.15  mycroft 	char *argv[];
     78   1.1      cgd {
     79   1.1      cgd 	int ch, rflag;
     80   1.1      cgd 
     81   1.7      jtc 	setlocale(LC_ALL, "");
     82   1.7      jtc 
     83  1.15  mycroft 	Pflag = rflag = 0;
     84  1.15  mycroft 	while ((ch = getopt(argc, argv, "dfiPRr")) != -1)
     85   1.1      cgd 		switch(ch) {
     86   1.1      cgd 		case 'd':
     87   1.1      cgd 			dflag = 1;
     88   1.1      cgd 			break;
     89   1.1      cgd 		case 'f':
     90   1.1      cgd 			fflag = 1;
     91   1.1      cgd 			iflag = 0;
     92   1.1      cgd 			break;
     93   1.1      cgd 		case 'i':
     94   1.1      cgd 			fflag = 0;
     95   1.1      cgd 			iflag = 1;
     96   1.1      cgd 			break;
     97  1.15  mycroft 		case 'P':
     98  1.15  mycroft 			Pflag = 1;
     99  1.15  mycroft 			break;
    100   1.1      cgd 		case 'R':
    101  1.15  mycroft 		case 'r':			/* Compatibility. */
    102   1.1      cgd 			rflag = 1;
    103   1.1      cgd 			break;
    104   1.1      cgd 		case '?':
    105   1.1      cgd 		default:
    106   1.1      cgd 			usage();
    107   1.1      cgd 		}
    108   1.1      cgd 	argc -= optind;
    109   1.1      cgd 	argv += optind;
    110   1.1      cgd 
    111   1.1      cgd 	if (argc < 1)
    112   1.1      cgd 		usage();
    113   1.1      cgd 
    114   1.1      cgd 	checkdot(argv);
    115   1.1      cgd 
    116  1.11      jtc 	if (*argv) {
    117  1.11      jtc 		stdin_ok = isatty(STDIN_FILENO);
    118   1.1      cgd 
    119  1.11      jtc 		if (rflag)
    120  1.15  mycroft 			rm_tree(argv);
    121  1.11      jtc 		else
    122  1.15  mycroft 			rm_file(argv);
    123  1.11      jtc 	}
    124   1.9      jtc 
    125  1.15  mycroft 	exit (eval);
    126   1.1      cgd }
    127   1.1      cgd 
    128   1.7      jtc void
    129  1.15  mycroft rm_tree(argv)
    130   1.1      cgd 	char **argv;
    131   1.1      cgd {
    132  1.15  mycroft 	FTS *fts;
    133  1.15  mycroft 	FTSENT *p;
    134  1.15  mycroft 	int needstat;
    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      cgd 	if (!(fts = fts_open(argv,
    149   1.1      cgd 	    needstat ? FTS_PHYSICAL : FTS_PHYSICAL|FTS_NOSTAT,
    150  1.15  mycroft 	    (int (*)())NULL)))
    151  1.15  mycroft 		err(1, NULL);
    152   1.7      jtc 	while ((p = fts_read(fts)) != NULL) {
    153  1.15  mycroft 		switch (p->fts_info) {
    154   1.1      cgd 		case FTS_DNR:
    155  1.15  mycroft 			if (!fflag || p->fts_errno != ENOENT) {
    156  1.15  mycroft 				warnx("%s: %s",
    157  1.15  mycroft 				    p->fts_path, strerror(p->fts_errno));
    158  1.15  mycroft 				eval = 1;
    159  1.15  mycroft 			}
    160  1.15  mycroft 			continue;
    161   1.1      cgd 		case FTS_ERR:
    162  1.15  mycroft 			errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
    163   1.1      cgd 		case FTS_NS:
    164  1.15  mycroft 			/*
    165  1.15  mycroft 			 * FTS_NS: assume that if can't stat the file, it
    166  1.15  mycroft 			 * can't be unlinked.
    167  1.15  mycroft 			 */
    168   1.1      cgd 			if (!needstat)
    169   1.1      cgd 				break;
    170  1.15  mycroft 			if (!fflag || p->fts_errno != ENOENT) {
    171  1.15  mycroft 				warnx("%s: %s",
    172  1.15  mycroft 				    p->fts_path, strerror(p->fts_errno));
    173  1.15  mycroft 				eval = 1;
    174  1.15  mycroft 			}
    175   1.1      cgd 			continue;
    176   1.1      cgd 		case FTS_D:
    177  1.15  mycroft 			/* Pre-order: give user chance to skip. */
    178   1.8      jtc 			if (!fflag && !check(p->fts_path, p->fts_accpath,
    179   1.6  deraadt 			    p->fts_statp)) {
    180   1.1      cgd 				(void)fts_set(fts, p, FTS_SKIP);
    181   1.1      cgd 				p->fts_number = SKIPPED;
    182   1.1      cgd 			}
    183   1.1      cgd 			continue;
    184   1.1      cgd 		case FTS_DP:
    185  1.15  mycroft 			/* Post-order: see if user skipped. */
    186   1.1      cgd 			if (p->fts_number == SKIPPED)
    187   1.1      cgd 				continue;
    188   1.1      cgd 			break;
    189   1.9      jtc 		default:
    190  1.15  mycroft 			if (!fflag &&
    191  1.15  mycroft 			    !check(p->fts_path, p->fts_accpath, p->fts_statp))
    192   1.9      jtc 				continue;
    193   1.1      cgd 		}
    194   1.1      cgd 
    195   1.1      cgd 		/*
    196   1.1      cgd 		 * If we can't read or search the directory, may still be
    197   1.1      cgd 		 * able to remove it.  Don't print out the un{read,search}able
    198   1.1      cgd 		 * message unless the remove fails.
    199   1.1      cgd 		 */
    200   1.1      cgd 		if (p->fts_info == FTS_DP || p->fts_info == FTS_DNR) {
    201  1.15  mycroft 			if (!rmdir(p->fts_accpath) || fflag && errno == ENOENT)
    202   1.1      cgd 				continue;
    203  1.14      jtc 		} else {
    204  1.15  mycroft 			if (Pflag)
    205  1.15  mycroft 				rm_overwrite(p->fts_accpath, NULL);
    206  1.14      jtc 			if (!unlink(p->fts_accpath) || fflag && errno == ENOENT)
    207  1.14      jtc 				continue;
    208  1.14      jtc 		}
    209  1.15  mycroft 		warn("%s", p->fts_path);
    210  1.15  mycroft 		eval = 1;
    211   1.1      cgd 	}
    212  1.15  mycroft 	if (errno)
    213  1.15  mycroft 		err(1, "fts_read");
    214   1.1      cgd }
    215   1.1      cgd 
    216   1.7      jtc void
    217  1.15  mycroft rm_file(argv)
    218   1.1      cgd 	char **argv;
    219   1.1      cgd {
    220   1.1      cgd 	struct stat sb;
    221  1.15  mycroft 	int rval;
    222  1.15  mycroft 	char *f;
    223   1.1      cgd 
    224   1.1      cgd 	/*
    225   1.1      cgd 	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
    226   1.1      cgd 	 * to remove a directory is an error, so must always stat the file.
    227   1.1      cgd 	 */
    228   1.7      jtc 	while ((f = *argv++) != NULL) {
    229  1.15  mycroft 		/* Assume if can't stat the file, can't unlink it. */
    230   1.1      cgd 		if (lstat(f, &sb)) {
    231   1.9      jtc 			if (!fflag || errno != ENOENT) {
    232  1.15  mycroft 				warn("%s", f);
    233  1.15  mycroft 				eval = 1;
    234   1.9      jtc 			}
    235   1.1      cgd 			continue;
    236   1.1      cgd 		}
    237   1.7      jtc 		if (S_ISDIR(sb.st_mode) && !dflag) {
    238  1.15  mycroft 			warnx("%s: is a directory", f);
    239  1.15  mycroft 			eval = 1;
    240   1.1      cgd 			continue;
    241   1.1      cgd 		}
    242  1.15  mycroft 		if (!fflag && !check(f, f, &sb))
    243   1.1      cgd 			continue;
    244  1.15  mycroft 		if (S_ISDIR(sb.st_mode))
    245  1.15  mycroft 			rval = rmdir(f);
    246  1.15  mycroft 		else {
    247  1.15  mycroft 			if (Pflag)
    248  1.15  mycroft 				rm_overwrite(f, &sb);
    249  1.15  mycroft 			rval = unlink(f);
    250  1.15  mycroft 		}
    251  1.15  mycroft 		if (rval && (!fflag || errno != ENOENT)) {
    252  1.15  mycroft 			warn("%s", f);
    253  1.15  mycroft 			eval = 1;
    254   1.7      jtc 		}
    255  1.15  mycroft 	}
    256  1.15  mycroft }
    257   1.7      jtc 
    258  1.15  mycroft /*
    259  1.15  mycroft  * rm_overwrite --
    260  1.15  mycroft  *	Overwrite the file 3 times with varying bit patterns.
    261  1.15  mycroft  *
    262  1.15  mycroft  * XXX
    263  1.15  mycroft  * This is a cheap way to *really* delete files.  Note that only regular
    264  1.15  mycroft  * files are deleted, directories (and therefore names) will remain.
    265  1.15  mycroft  * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
    266  1.15  mycroft  * System V file system).  In a logging file system, you'll have to have
    267  1.15  mycroft  * kernel support.
    268  1.15  mycroft  */
    269  1.15  mycroft void
    270  1.15  mycroft rm_overwrite(file, sbp)
    271  1.15  mycroft 	char *file;
    272  1.15  mycroft 	struct stat *sbp;
    273  1.15  mycroft {
    274  1.15  mycroft 	struct stat sb;
    275  1.15  mycroft 	off_t len;
    276  1.15  mycroft 	int fd, wlen;
    277  1.15  mycroft 	char buf[8 * 1024];
    278  1.15  mycroft 
    279  1.15  mycroft 	fd = -1;
    280  1.15  mycroft 	if (sbp == NULL) {
    281  1.15  mycroft 		if (lstat(file, &sb))
    282  1.15  mycroft 			goto err;
    283  1.15  mycroft 		sbp = &sb;
    284   1.1      cgd 	}
    285  1.15  mycroft 	if (!S_ISREG(sbp->st_mode))
    286  1.15  mycroft 		return;
    287  1.15  mycroft 	if ((fd = open(file, O_WRONLY, 0)) == -1)
    288  1.15  mycroft 		goto err;
    289  1.15  mycroft 
    290  1.15  mycroft #define	PASS(byte) {							\
    291  1.15  mycroft 	memset(buf, byte, sizeof(buf));					\
    292  1.15  mycroft 	for (len = sbp->st_size; len > 0; len -= wlen) {		\
    293  1.15  mycroft 		wlen = len < sizeof(buf) ? len : sizeof(buf);		\
    294  1.15  mycroft 		if (write(fd, buf, wlen) != wlen)			\
    295  1.15  mycroft 			goto err;					\
    296  1.15  mycroft 	}								\
    297  1.15  mycroft }
    298  1.15  mycroft 	PASS(0xff);
    299  1.15  mycroft 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
    300  1.15  mycroft 		goto err;
    301  1.15  mycroft 	PASS(0x00);
    302  1.15  mycroft 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
    303  1.15  mycroft 		goto err;
    304  1.15  mycroft 	PASS(0xff);
    305  1.15  mycroft 	if (!fsync(fd) && !close(fd))
    306  1.15  mycroft 		return;
    307  1.15  mycroft 
    308  1.15  mycroft err:	eval = 1;
    309  1.15  mycroft 	warn("%s", file);
    310   1.1      cgd }
    311   1.1      cgd 
    312  1.15  mycroft 
    313   1.7      jtc int
    314   1.1      cgd check(path, name, sp)
    315   1.1      cgd 	char *path, *name;
    316   1.1      cgd 	struct stat *sp;
    317   1.1      cgd {
    318  1.15  mycroft 	int ch, first;
    319  1.15  mycroft 	char modep[15];
    320   1.1      cgd 
    321   1.1      cgd 	/* Check -i first. */
    322   1.1      cgd 	if (iflag)
    323   1.1      cgd 		(void)fprintf(stderr, "remove %s? ", path);
    324   1.1      cgd 	else {
    325   1.1      cgd 		/*
    326   1.1      cgd 		 * If it's not a symbolic link and it's unwritable and we're
    327   1.1      cgd 		 * talking to a terminal, ask.  Symbolic links are excluded
    328  1.15  mycroft 		 * because their permissions are meaningless.  Check stdin_ok
    329  1.15  mycroft 		 * first because we may not have stat'ed the file.
    330   1.1      cgd 		 */
    331  1.12      jtc 		if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK))
    332  1.15  mycroft 			return (1);
    333   1.1      cgd 		strmode(sp->st_mode, modep);
    334   1.8      jtc 		(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
    335   1.8      jtc 		    modep + 1, modep[9] == ' ' ? "" : " ",
    336   1.8      jtc 		    user_from_uid(sp->st_uid, 0),
    337   1.8      jtc 		    group_from_gid(sp->st_gid, 0), path);
    338   1.1      cgd 	}
    339   1.1      cgd 	(void)fflush(stderr);
    340   1.1      cgd 
    341   1.1      cgd 	first = ch = getchar();
    342   1.1      cgd 	while (ch != '\n' && ch != EOF)
    343   1.1      cgd 		ch = getchar();
    344  1.15  mycroft 	return (first == 'y' || first == 'Y');
    345   1.1      cgd }
    346   1.1      cgd 
    347  1.16      jtc 
    348  1.16      jtc /*
    349  1.16      jtc  * POSIX.2 requires that if "." or ".." are specified as the basename
    350  1.16      jtc  * portion of an operand, a diagnostic message be written to standard
    351  1.16      jtc  * error and nothing more be done with such operands.
    352  1.16      jtc  *
    353  1.16      jtc  * Since POSIX.2 defines basename as the final portion of a path after
    354  1.16      jtc  * trailing slashes have been removed, we'll remove them here.
    355  1.16      jtc  */
    356  1.16      jtc 
    357   1.1      cgd #define ISDOT(a)	((a)[0] == '.' && (!(a)[1] || (a)[1] == '.' && !(a)[2]))
    358   1.7      jtc void
    359   1.1      cgd checkdot(argv)
    360   1.1      cgd 	char **argv;
    361   1.1      cgd {
    362  1.15  mycroft 	char *p, **save, **t;
    363   1.1      cgd 	int complained;
    364   1.1      cgd 
    365   1.1      cgd 	complained = 0;
    366   1.1      cgd 	for (t = argv; *t;) {
    367  1.16      jtc 		/* strip trailing slashes */
    368  1.16      jtc 		p = strrchr (*t, '\0');
    369  1.16      jtc 		while (--p > *t && *p == '/')
    370  1.16      jtc 			*p = '\0';
    371  1.16      jtc 
    372  1.16      jtc 		/* extract basename */
    373  1.15  mycroft 		if ((p = strrchr(*t, '/')) != NULL)
    374   1.1      cgd 			++p;
    375   1.1      cgd 		else
    376   1.1      cgd 			p = *t;
    377  1.16      jtc 
    378   1.1      cgd 		if (ISDOT(p)) {
    379   1.8      jtc 			if (!complained++)
    380  1.15  mycroft 				warnx("\".\" and \"..\" may not be removed");
    381  1.15  mycroft 			eval = 1;
    382  1.15  mycroft 			for (save = t; (t[0] = t[1]) != NULL; ++t)
    383  1.13      jtc 				continue;
    384   1.1      cgd 			t = save;
    385   1.1      cgd 		} else
    386   1.1      cgd 			++t;
    387   1.1      cgd 	}
    388   1.1      cgd }
    389   1.1      cgd 
    390   1.7      jtc void
    391  1.15  mycroft usage()
    392   1.1      cgd {
    393   1.1      cgd 
    394  1.15  mycroft 	(void)fprintf(stderr, "usage: rm [-dfiPRr] file ...\n");
    395   1.1      cgd 	exit(1);
    396   1.1      cgd }
    397