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