Home | History | Annotate | Line # | Download | only in rm
rm.c revision 1.8
      1  1.1      cgd /*-
      2  1.1      cgd  * Copyright (c) 1990 The Regents of the University of California.
      3  1.1      cgd  * 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      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.5  mycroft /*static char sccsid[] = "from: @(#)rm.c	4.26 (Berkeley) 3/10/91";*/
     42  1.8      jtc static char rcsid[] = "$Id: rm.c,v 1.8 1993/10/25 19:28:19 jtc Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.7      jtc #include <stdio.h>
     46  1.7      jtc #include <string.h>
     47  1.7      jtc #include <stdlib.h>
     48  1.7      jtc #include <locale.h>
     49  1.7      jtc #include <errno.h>
     50  1.1      cgd #include <sys/types.h>
     51  1.1      cgd #include <sys/stat.h>
     52  1.7      jtc #include <unistd.h>
     53  1.1      cgd #include <fts.h>
     54  1.7      jtc 
     55  1.7      jtc int check	__P((char *, char *, struct stat *));
     56  1.7      jtc void checkdot	__P((char **));
     57  1.7      jtc void error	__P((char *, int));
     58  1.7      jtc void rmtree	__P((char **));
     59  1.7      jtc void rmfile	__P((char **));
     60  1.7      jtc void usage	__P((void));
     61  1.1      cgd 
     62  1.1      cgd int dflag, fflag, iflag, retval, stdin_ok;
     63  1.1      cgd 
     64  1.1      cgd /*
     65  1.1      cgd  * rm --
     66  1.1      cgd  *	This rm is different from historic rm's, but is expected to match
     67  1.1      cgd  *	POSIX 1003.2 behavior.  The most visible difference is that -f
     68  1.1      cgd  *	has two specific effects now, ignore non-existent files and force
     69  1.1      cgd  * 	file removal.
     70  1.1      cgd  */
     71  1.1      cgd 
     72  1.7      jtc int
     73  1.1      cgd main(argc, argv)
     74  1.1      cgd 	int argc;
     75  1.1      cgd 	char **argv;
     76  1.1      cgd {
     77  1.1      cgd 	int ch, rflag;
     78  1.1      cgd 
     79  1.7      jtc 	setlocale(LC_ALL, "");
     80  1.7      jtc 
     81  1.1      cgd 	rflag = 0;
     82  1.1      cgd 	while ((ch = getopt(argc, argv, "dfiRr")) != EOF)
     83  1.1      cgd 		switch(ch) {
     84  1.1      cgd 		case 'd':
     85  1.1      cgd 			dflag = 1;
     86  1.1      cgd 			break;
     87  1.1      cgd 		case 'f':
     88  1.1      cgd 			fflag = 1;
     89  1.1      cgd 			iflag = 0;
     90  1.1      cgd 			break;
     91  1.1      cgd 		case 'i':
     92  1.1      cgd 			fflag = 0;
     93  1.1      cgd 			iflag = 1;
     94  1.1      cgd 			break;
     95  1.1      cgd 		case 'R':
     96  1.1      cgd 		case 'r':			/* compatibility */
     97  1.1      cgd 			rflag = 1;
     98  1.1      cgd 			break;
     99  1.1      cgd 		case '?':
    100  1.1      cgd 		default:
    101  1.1      cgd 			usage();
    102  1.1      cgd 		}
    103  1.1      cgd 	argc -= optind;
    104  1.1      cgd 	argv += optind;
    105  1.1      cgd 
    106  1.1      cgd 	if (argc < 1)
    107  1.1      cgd 		usage();
    108  1.1      cgd 
    109  1.1      cgd 	checkdot(argv);
    110  1.1      cgd 	if (!*argv)
    111  1.4  mycroft 		exit(fflag ? 0 : retval);
    112  1.1      cgd 
    113  1.1      cgd 	stdin_ok = isatty(STDIN_FILENO);
    114  1.1      cgd 
    115  1.1      cgd 	if (rflag)
    116  1.1      cgd 		rmtree(argv);
    117  1.1      cgd 	else
    118  1.1      cgd 		rmfile(argv);
    119  1.4  mycroft 	exit(fflag ? 0 : retval);
    120  1.1      cgd }
    121  1.1      cgd 
    122  1.7      jtc void
    123  1.1      cgd rmtree(argv)
    124  1.1      cgd 	char **argv;
    125  1.1      cgd {
    126  1.1      cgd 	register FTS *fts;
    127  1.1      cgd 	register FTSENT *p;
    128  1.1      cgd 	register int needstat;
    129  1.1      cgd 
    130  1.1      cgd 	/*
    131  1.1      cgd 	 * Remove a file hierarchy.  If forcing removal (-f), or interactive
    132  1.1      cgd 	 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
    133  1.1      cgd 	 */
    134  1.1      cgd 	needstat = !fflag && !iflag && stdin_ok;
    135  1.1      cgd 
    136  1.1      cgd 	/*
    137  1.1      cgd 	 * If the -i option is specified, the user can skip on the pre-order
    138  1.1      cgd 	 * visit.  The fts_number field flags skipped directories.
    139  1.1      cgd 	 */
    140  1.1      cgd #define	SKIPPED	1
    141  1.1      cgd 
    142  1.1      cgd 	if (!(fts = fts_open(argv,
    143  1.1      cgd 	    needstat ? FTS_PHYSICAL : FTS_PHYSICAL|FTS_NOSTAT,
    144  1.4  mycroft 	    (int (*)())NULL)) && !fflag) {
    145  1.1      cgd 		(void)fprintf(stderr, "rm: %s.\n", strerror(errno));
    146  1.1      cgd 		exit(1);
    147  1.1      cgd 	}
    148  1.7      jtc 	while ((p = fts_read(fts)) != NULL) {
    149  1.1      cgd 		switch(p->fts_info) {
    150  1.1      cgd 		case FTS_DNR:
    151  1.1      cgd 		case FTS_ERR:
    152  1.1      cgd 			error(p->fts_path, errno);
    153  1.1      cgd 			exit(1);
    154  1.1      cgd 		/*
    155  1.1      cgd 		 * FTS_NS: assume that if can't stat the file, it can't be
    156  1.1      cgd 		 * unlinked.
    157  1.1      cgd 		 */
    158  1.1      cgd 		case FTS_NS:
    159  1.1      cgd 			if (!needstat)
    160  1.1      cgd 				break;
    161  1.1      cgd 			if (!fflag || errno != ENOENT)
    162  1.1      cgd 				error(p->fts_path, errno);
    163  1.1      cgd 			continue;
    164  1.1      cgd 		/* Pre-order: give user chance to skip. */
    165  1.1      cgd 		case FTS_D:
    166  1.8      jtc 			if (!fflag && !check(p->fts_path, p->fts_accpath,
    167  1.6  deraadt 			    p->fts_statp)) {
    168  1.1      cgd 				(void)fts_set(fts, p, FTS_SKIP);
    169  1.1      cgd 				p->fts_number = SKIPPED;
    170  1.1      cgd 			}
    171  1.1      cgd 			continue;
    172  1.1      cgd 		/* Post-order: see if user skipped. */
    173  1.1      cgd 		case FTS_DP:
    174  1.1      cgd 			if (p->fts_number == SKIPPED)
    175  1.1      cgd 				continue;
    176  1.1      cgd 			break;
    177  1.1      cgd 		}
    178  1.1      cgd 
    179  1.1      cgd 		if (!fflag &&
    180  1.6  deraadt 		    !check(p->fts_path, p->fts_accpath, p->fts_statp))
    181  1.1      cgd 			continue;
    182  1.1      cgd 
    183  1.1      cgd 		/*
    184  1.1      cgd 		 * If we can't read or search the directory, may still be
    185  1.1      cgd 		 * able to remove it.  Don't print out the un{read,search}able
    186  1.1      cgd 		 * message unless the remove fails.
    187  1.1      cgd 		 */
    188  1.1      cgd 		if (p->fts_info == FTS_DP || p->fts_info == FTS_DNR) {
    189  1.1      cgd 			if (!rmdir(p->fts_accpath))
    190  1.1      cgd 				continue;
    191  1.1      cgd 			if (errno == ENOENT) {
    192  1.1      cgd 				if (fflag)
    193  1.1      cgd 					continue;
    194  1.4  mycroft 			} else if (p->fts_info != FTS_DP && !fflag)
    195  1.1      cgd 				(void)fprintf(stderr,
    196  1.1      cgd 				    "rm: unable to read %s.\n", p->fts_path);
    197  1.1      cgd 		} else if (!unlink(p->fts_accpath) || fflag && errno == ENOENT)
    198  1.1      cgd 			continue;
    199  1.1      cgd 		error(p->fts_path, errno);
    200  1.1      cgd 	}
    201  1.1      cgd }
    202  1.1      cgd 
    203  1.7      jtc void
    204  1.1      cgd rmfile(argv)
    205  1.1      cgd 	char **argv;
    206  1.1      cgd {
    207  1.1      cgd 	register char *f;
    208  1.1      cgd 	struct stat sb;
    209  1.1      cgd 
    210  1.1      cgd 	/*
    211  1.1      cgd 	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
    212  1.1      cgd 	 * to remove a directory is an error, so must always stat the file.
    213  1.1      cgd 	 */
    214  1.7      jtc 	while ((f = *argv++) != NULL) {
    215  1.7      jtc 		/* If the file does not exist:
    216  1.7      jtc 		 *   If the -f option was not specified, write a diagnostic
    217  1.7      jtc 		 *   to the standard error...
    218  1.7      jtc 		 */
    219  1.1      cgd 		if (lstat(f, &sb)) {
    220  1.7      jtc 			error(f, errno);
    221  1.1      cgd 			continue;
    222  1.1      cgd 		}
    223  1.7      jtc 
    224  1.7      jtc 		/* If the file is of type directory and neither the -r or -R
    225  1.7      jtc 		 * (or -d) options are specified, write a diagnostic to the
    226  1.7      jtc 		 * standard error...
    227  1.7      jtc 		 */
    228  1.7      jtc 		if (S_ISDIR(sb.st_mode) && !dflag) {
    229  1.7      jtc 			error (f, EISDIR);
    230  1.1      cgd 			continue;
    231  1.1      cgd 		}
    232  1.7      jtc 
    233  1.7      jtc 		if (!fflag && !check(f, f, &sb)) {
    234  1.1      cgd 			continue;
    235  1.7      jtc 		}
    236  1.7      jtc 
    237  1.7      jtc 		/*
    238  1.7      jtc 		 * rmdir() directories, unlink() files...
    239  1.7      jtc 		 */
    240  1.7      jtc 		if ((S_ISDIR(sb.st_mode) ? rmdir(f) : unlink(f))) {
    241  1.1      cgd 			error(f, errno);
    242  1.7      jtc 		}
    243  1.1      cgd 	}
    244  1.1      cgd }
    245  1.1      cgd 
    246  1.7      jtc int
    247  1.1      cgd check(path, name, sp)
    248  1.1      cgd 	char *path, *name;
    249  1.1      cgd 	struct stat *sp;
    250  1.1      cgd {
    251  1.1      cgd 	register int first, ch;
    252  1.1      cgd 	char modep[15], *user_from_uid(), *group_from_gid();
    253  1.1      cgd 
    254  1.1      cgd 	/* Check -i first. */
    255  1.1      cgd 	if (iflag)
    256  1.1      cgd 		(void)fprintf(stderr, "remove %s? ", path);
    257  1.1      cgd 	else {
    258  1.1      cgd 		/*
    259  1.1      cgd 		 * If it's not a symbolic link and it's unwritable and we're
    260  1.1      cgd 		 * talking to a terminal, ask.  Symbolic links are excluded
    261  1.1      cgd 		 * because their permissions are meaningless.
    262  1.1      cgd 		 */
    263  1.1      cgd 		if (S_ISLNK(sp->st_mode) || !stdin_ok || !access(name, W_OK))
    264  1.1      cgd 			return(1);
    265  1.1      cgd 		strmode(sp->st_mode, modep);
    266  1.8      jtc 		(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
    267  1.8      jtc 		    modep + 1, modep[9] == ' ' ? "" : " ",
    268  1.8      jtc 		    user_from_uid(sp->st_uid, 0),
    269  1.8      jtc 		    group_from_gid(sp->st_gid, 0), path);
    270  1.1      cgd 	}
    271  1.1      cgd 	(void)fflush(stderr);
    272  1.1      cgd 
    273  1.1      cgd 	first = ch = getchar();
    274  1.1      cgd 	while (ch != '\n' && ch != EOF)
    275  1.1      cgd 		ch = getchar();
    276  1.1      cgd 	return(first == 'y');
    277  1.1      cgd }
    278  1.1      cgd 
    279  1.1      cgd #define ISDOT(a)	((a)[0] == '.' && (!(a)[1] || (a)[1] == '.' && !(a)[2]))
    280  1.7      jtc void
    281  1.1      cgd checkdot(argv)
    282  1.1      cgd 	char **argv;
    283  1.1      cgd {
    284  1.1      cgd 	register char *p, **t, **save;
    285  1.1      cgd 	int complained;
    286  1.1      cgd 
    287  1.1      cgd 	complained = 0;
    288  1.1      cgd 	for (t = argv; *t;) {
    289  1.7      jtc 		if ((p = rindex(*t, '/')) != NULL)
    290  1.1      cgd 			++p;
    291  1.1      cgd 		else
    292  1.1      cgd 			p = *t;
    293  1.1      cgd 		if (ISDOT(p)) {
    294  1.8      jtc 			if (!complained++)
    295  1.1      cgd 			    (void)fprintf(stderr,
    296  1.1      cgd 				"rm: \".\" and \"..\" may not be removed.\n");
    297  1.1      cgd 			retval = 1;
    298  1.1      cgd 			for (save = t; t[0] = t[1]; ++t);
    299  1.1      cgd 			t = save;
    300  1.1      cgd 		} else
    301  1.1      cgd 			++t;
    302  1.1      cgd 	}
    303  1.1      cgd }
    304  1.1      cgd 
    305  1.7      jtc void
    306  1.1      cgd error(name, val)
    307  1.1      cgd 	char *name;
    308  1.1      cgd 	int val;
    309  1.1      cgd {
    310  1.4  mycroft 	if (!fflag)
    311  1.4  mycroft 		(void)fprintf(stderr, "rm: %s: %s.\n", name, strerror(val));
    312  1.1      cgd 	retval = 1;
    313  1.1      cgd }
    314  1.1      cgd 
    315  1.7      jtc void
    316  1.1      cgd usage()
    317  1.1      cgd {
    318  1.8      jtc 	(void)fprintf(stderr, "usage: rm [-dfiRr] file ...\n");
    319  1.1      cgd 	exit(1);
    320  1.1      cgd }
    321