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