Home | History | Annotate | Line # | Download | only in rm
rm.c revision 1.10
      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.10      jtc static char rcsid[] = "$Id: rm.c,v 1.10 1993/11/16 23:16:49 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.9      jtc 
    120   1.9      jtc 	exit(retval);
    121   1.1      cgd }
    122   1.1      cgd 
    123   1.7      jtc void
    124   1.1      cgd rmtree(argv)
    125   1.1      cgd 	char **argv;
    126   1.1      cgd {
    127   1.1      cgd 	register FTS *fts;
    128   1.1      cgd 	register FTSENT *p;
    129   1.1      cgd 	register int needstat;
    130   1.1      cgd 
    131   1.1      cgd 	/*
    132   1.1      cgd 	 * Remove a file hierarchy.  If forcing removal (-f), or interactive
    133   1.1      cgd 	 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
    134   1.1      cgd 	 */
    135   1.1      cgd 	needstat = !fflag && !iflag && stdin_ok;
    136   1.1      cgd 
    137   1.1      cgd 	/*
    138   1.1      cgd 	 * If the -i option is specified, the user can skip on the pre-order
    139   1.1      cgd 	 * visit.  The fts_number field flags skipped directories.
    140   1.1      cgd 	 */
    141   1.1      cgd #define	SKIPPED	1
    142   1.1      cgd 
    143   1.1      cgd 	if (!(fts = fts_open(argv,
    144   1.1      cgd 	    needstat ? FTS_PHYSICAL : FTS_PHYSICAL|FTS_NOSTAT,
    145   1.9      jtc 		(int (*)())NULL))) {
    146   1.9      jtc 		if (!fflag)
    147   1.9      jtc 			(void)fprintf(stderr, "rm: %s.\n", strerror(errno));
    148   1.9      jtc 		exit (1);
    149   1.1      cgd 	}
    150   1.9      jtc 
    151   1.7      jtc 	while ((p = fts_read(fts)) != NULL) {
    152   1.1      cgd 		switch(p->fts_info) {
    153   1.1      cgd 		case FTS_DNR:
    154   1.1      cgd 		case FTS_ERR:
    155   1.1      cgd 			error(p->fts_path, errno);
    156   1.1      cgd 			exit(1);
    157   1.1      cgd 		/*
    158   1.1      cgd 		 * FTS_NS: assume that if can't stat the file, it can't be
    159   1.1      cgd 		 * unlinked.
    160   1.1      cgd 		 */
    161   1.1      cgd 		case FTS_NS:
    162   1.1      cgd 			if (!needstat)
    163   1.1      cgd 				break;
    164   1.1      cgd 			if (!fflag || errno != ENOENT)
    165   1.1      cgd 				error(p->fts_path, errno);
    166   1.1      cgd 			continue;
    167   1.1      cgd 		/* Pre-order: give user chance to skip. */
    168   1.1      cgd 		case FTS_D:
    169   1.8      jtc 			if (!fflag && !check(p->fts_path, p->fts_accpath,
    170   1.6  deraadt 			    p->fts_statp)) {
    171   1.1      cgd 				(void)fts_set(fts, p, FTS_SKIP);
    172   1.1      cgd 				p->fts_number = SKIPPED;
    173   1.1      cgd 			}
    174   1.1      cgd 			continue;
    175   1.1      cgd 		/* Post-order: see if user skipped. */
    176   1.1      cgd 		case FTS_DP:
    177   1.1      cgd 			if (p->fts_number == SKIPPED)
    178   1.1      cgd 				continue;
    179   1.1      cgd 			break;
    180   1.9      jtc 
    181   1.9      jtc 		default:
    182   1.9      jtc 			if (!fflag && !check(p->fts_path, p->fts_accpath,
    183   1.9      jtc 			    p->fts_statp))
    184   1.9      jtc 				continue;
    185   1.1      cgd 		}
    186   1.1      cgd 
    187   1.1      cgd 		/*
    188   1.1      cgd 		 * If we can't read or search the directory, may still be
    189   1.1      cgd 		 * able to remove it.  Don't print out the un{read,search}able
    190   1.1      cgd 		 * message unless the remove fails.
    191   1.1      cgd 		 */
    192   1.1      cgd 		if (p->fts_info == FTS_DP || p->fts_info == FTS_DNR) {
    193   1.1      cgd 			if (!rmdir(p->fts_accpath))
    194   1.1      cgd 				continue;
    195   1.1      cgd 			if (errno == ENOENT) {
    196   1.1      cgd 				if (fflag)
    197   1.1      cgd 					continue;
    198   1.4  mycroft 			} else if (p->fts_info != FTS_DP && !fflag)
    199   1.1      cgd 				(void)fprintf(stderr,
    200   1.1      cgd 				    "rm: unable to read %s.\n", p->fts_path);
    201   1.1      cgd 		} else if (!unlink(p->fts_accpath) || fflag && errno == ENOENT)
    202   1.1      cgd 			continue;
    203   1.1      cgd 		error(p->fts_path, errno);
    204   1.1      cgd 	}
    205   1.1      cgd }
    206   1.1      cgd 
    207   1.7      jtc void
    208   1.1      cgd rmfile(argv)
    209   1.1      cgd 	char **argv;
    210   1.1      cgd {
    211   1.1      cgd 	register char *f;
    212   1.1      cgd 	struct stat sb;
    213   1.1      cgd 
    214   1.1      cgd 	/*
    215   1.1      cgd 	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
    216   1.1      cgd 	 * to remove a directory is an error, so must always stat the file.
    217   1.1      cgd 	 */
    218   1.7      jtc 	while ((f = *argv++) != NULL) {
    219   1.7      jtc 		/* If the file does not exist:
    220   1.7      jtc 		 *   If the -f option was not specified, write a diagnostic
    221   1.7      jtc 		 *   to the standard error...
    222   1.7      jtc 		 */
    223   1.1      cgd 		if (lstat(f, &sb)) {
    224   1.9      jtc 			if (!fflag || errno != ENOENT) {
    225   1.9      jtc 				error(f, errno);
    226   1.9      jtc 			}
    227   1.1      cgd 			continue;
    228   1.1      cgd 		}
    229   1.7      jtc 
    230   1.7      jtc 		/* If the file is of type directory and neither the -r or -R
    231   1.7      jtc 		 * (or -d) options are specified, write a diagnostic to the
    232   1.7      jtc 		 * standard error...
    233   1.7      jtc 		 */
    234   1.7      jtc 		if (S_ISDIR(sb.st_mode) && !dflag) {
    235   1.7      jtc 			error (f, EISDIR);
    236   1.1      cgd 			continue;
    237   1.1      cgd 		}
    238   1.7      jtc 
    239   1.7      jtc 		if (!fflag && !check(f, f, &sb)) {
    240   1.1      cgd 			continue;
    241   1.7      jtc 		}
    242   1.7      jtc 
    243   1.7      jtc 		/*
    244   1.7      jtc 		 * rmdir() directories, unlink() files...
    245   1.7      jtc 		 */
    246   1.7      jtc 		if ((S_ISDIR(sb.st_mode) ? rmdir(f) : unlink(f))) {
    247   1.1      cgd 			error(f, errno);
    248   1.7      jtc 		}
    249   1.1      cgd 	}
    250   1.1      cgd }
    251   1.1      cgd 
    252   1.7      jtc int
    253   1.1      cgd check(path, name, sp)
    254   1.1      cgd 	char *path, *name;
    255   1.1      cgd 	struct stat *sp;
    256   1.1      cgd {
    257   1.1      cgd 	register int first, ch;
    258   1.1      cgd 	char modep[15], *user_from_uid(), *group_from_gid();
    259   1.1      cgd 
    260   1.1      cgd 	/* Check -i first. */
    261   1.1      cgd 	if (iflag)
    262   1.1      cgd 		(void)fprintf(stderr, "remove %s? ", path);
    263   1.1      cgd 	else {
    264   1.1      cgd 		/*
    265   1.1      cgd 		 * If it's not a symbolic link and it's unwritable and we're
    266   1.1      cgd 		 * talking to a terminal, ask.  Symbolic links are excluded
    267   1.1      cgd 		 * because their permissions are meaningless.
    268   1.1      cgd 		 */
    269   1.1      cgd 		if (S_ISLNK(sp->st_mode) || !stdin_ok || !access(name, W_OK))
    270   1.1      cgd 			return(1);
    271   1.1      cgd 		strmode(sp->st_mode, modep);
    272   1.8      jtc 		(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
    273   1.8      jtc 		    modep + 1, modep[9] == ' ' ? "" : " ",
    274   1.8      jtc 		    user_from_uid(sp->st_uid, 0),
    275   1.8      jtc 		    group_from_gid(sp->st_gid, 0), path);
    276   1.1      cgd 	}
    277   1.1      cgd 	(void)fflush(stderr);
    278   1.1      cgd 
    279   1.1      cgd 	first = ch = getchar();
    280   1.1      cgd 	while (ch != '\n' && ch != EOF)
    281   1.1      cgd 		ch = getchar();
    282  1.10      jtc 	return(first == 'y' || first == 'Y');
    283   1.1      cgd }
    284   1.1      cgd 
    285   1.1      cgd #define ISDOT(a)	((a)[0] == '.' && (!(a)[1] || (a)[1] == '.' && !(a)[2]))
    286   1.7      jtc void
    287   1.1      cgd checkdot(argv)
    288   1.1      cgd 	char **argv;
    289   1.1      cgd {
    290   1.1      cgd 	register char *p, **t, **save;
    291   1.1      cgd 	int complained;
    292   1.1      cgd 
    293   1.1      cgd 	complained = 0;
    294   1.1      cgd 	for (t = argv; *t;) {
    295   1.7      jtc 		if ((p = rindex(*t, '/')) != NULL)
    296   1.1      cgd 			++p;
    297   1.1      cgd 		else
    298   1.1      cgd 			p = *t;
    299   1.1      cgd 		if (ISDOT(p)) {
    300   1.8      jtc 			if (!complained++)
    301   1.1      cgd 			    (void)fprintf(stderr,
    302   1.1      cgd 				"rm: \".\" and \"..\" may not be removed.\n");
    303   1.1      cgd 			retval = 1;
    304   1.1      cgd 			for (save = t; t[0] = t[1]; ++t);
    305   1.1      cgd 			t = save;
    306   1.1      cgd 		} else
    307   1.1      cgd 			++t;
    308   1.1      cgd 	}
    309   1.1      cgd }
    310   1.1      cgd 
    311   1.7      jtc void
    312   1.1      cgd error(name, val)
    313   1.1      cgd 	char *name;
    314   1.1      cgd 	int val;
    315   1.1      cgd {
    316   1.4  mycroft 	if (!fflag)
    317   1.4  mycroft 		(void)fprintf(stderr, "rm: %s: %s.\n", name, strerror(val));
    318   1.1      cgd 	retval = 1;
    319   1.1      cgd }
    320   1.1      cgd 
    321   1.7      jtc void
    322   1.1      cgd usage()
    323   1.1      cgd {
    324   1.8      jtc 	(void)fprintf(stderr, "usage: rm [-dfiRr] file ...\n");
    325   1.1      cgd 	exit(1);
    326   1.1      cgd }
    327