xref: /freebsd/bin/rm/rm.c (revision 2dfa4b66b3d0caaaae6ce2df476b5615f8415a19)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1990, 1993, 1994
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
84b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
94b88c807SRodney W. Grimes  * are met:
104b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
114b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
124b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
134b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
144b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
164b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
174b88c807SRodney W. Grimes  *    without specific prior written permission.
184b88c807SRodney W. Grimes  *
194b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
204b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
234b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294b88c807SRodney W. Grimes  * SUCH DAMAGE.
304b88c807SRodney W. Grimes  */
314b88c807SRodney W. Grimes 
3209a80d48SDavid E. O'Brien #if 0
334b88c807SRodney W. Grimes #ifndef lint
3441cc862cSSteve Price static const char copyright[] =
354b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\
364b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
37395f4bf0SSteve Price #endif /* not lint */
38395f4bf0SSteve Price 
39395f4bf0SSteve Price #ifndef lint
40395f4bf0SSteve Price static char sccsid[] = "@(#)rm.c	8.5 (Berkeley) 4/18/94";
414b88c807SRodney W. Grimes #endif /* not lint */
4209a80d48SDavid E. O'Brien #endif
432749b141SDavid E. O'Brien #include <sys/cdefs.h>
442749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
454b88c807SRodney W. Grimes 
464b88c807SRodney W. Grimes #include <sys/stat.h>
47f80db2b8SKris Kennaway #include <sys/param.h>
48f80db2b8SKris Kennaway #include <sys/mount.h>
494b88c807SRodney W. Grimes 
504b88c807SRodney W. Grimes #include <err.h>
514b88c807SRodney W. Grimes #include <errno.h>
524b88c807SRodney W. Grimes #include <fcntl.h>
534b88c807SRodney W. Grimes #include <fts.h>
54fc69394fSWarner Losh #include <grp.h>
55b81cd107SXin LI #include <locale.h>
56fc69394fSWarner Losh #include <pwd.h>
574b88c807SRodney W. Grimes #include <stdio.h>
584b88c807SRodney W. Grimes #include <stdlib.h>
594b88c807SRodney W. Grimes #include <string.h>
60bfbdd545SMichael Haro #include <sysexits.h>
614b88c807SRodney W. Grimes #include <unistd.h>
624b88c807SRodney W. Grimes 
63900c4ed3SConrad Meyer static int dflag, eval, fflag, iflag, vflag, Wflag, stdin_ok;
64d4319e74SEitan Adler static int rflag, Iflag, xflag;
65f9d4afb4SEd Schouten static uid_t uid;
66f9d4afb4SEd Schouten static volatile sig_atomic_t info;
674b88c807SRodney W. Grimes 
689d6d3f96SEitan Adler static int	check(const char *, const char *, struct stat *);
699d6d3f96SEitan Adler static int	check2(char **);
709d6d3f96SEitan Adler static void	checkdot(char **);
719d6d3f96SEitan Adler static void	checkslash(char **);
729d6d3f96SEitan Adler static void	rm_file(char **);
739d6d3f96SEitan Adler static void	rm_tree(char **);
74b5260db6SWarner Losh static void siginfo(int __unused);
759d6d3f96SEitan Adler static void	usage(void);
764b88c807SRodney W. Grimes 
774b88c807SRodney W. Grimes /*
784b88c807SRodney W. Grimes  * rm --
794b88c807SRodney W. Grimes  *	This rm is different from historic rm's, but is expected to match
804b88c807SRodney W. Grimes  *	POSIX 1003.2 behavior.	The most visible difference is that -f
814b88c807SRodney W. Grimes  *	has two specific effects now, ignore non-existent files and force
824b88c807SRodney W. Grimes  *	file removal.
834b88c807SRodney W. Grimes  */
844b88c807SRodney W. Grimes int
8546251ddeSWarner Losh main(int argc, char *argv[])
864b88c807SRodney W. Grimes {
8724c0f738SXin LI 	int ch;
88d71e172aSSheldon Hearn 	char *p;
89d71e172aSSheldon Hearn 
90b81cd107SXin LI 	(void)setlocale(LC_ALL, "");
91b81cd107SXin LI 
92d71e172aSSheldon Hearn 	/*
93d71e172aSSheldon Hearn 	 * Test for the special case where the utility is called as
94d71e172aSSheldon Hearn 	 * "unlink", for which the functionality provided is greatly
95d71e172aSSheldon Hearn 	 * simplified.
96d71e172aSSheldon Hearn 	 */
970cf90cd1SJilles Tjoelker 	if ((p = strrchr(argv[0], '/')) == NULL)
98d71e172aSSheldon Hearn 		p = argv[0];
99d71e172aSSheldon Hearn 	else
100d71e172aSSheldon Hearn 		++p;
101d71e172aSSheldon Hearn 	if (strcmp(p, "unlink") == 0) {
10219b4f0dcSEd Maste 		if (argc == 2)
10319b4f0dcSEd Maste 			rm_file(&argv[1]);
10419b4f0dcSEd Maste 		else if (argc == 3 && strcmp(argv[1], "--") == 0)
10519b4f0dcSEd Maste 			rm_file(&argv[2]);
10619b4f0dcSEd Maste 		else
107d71e172aSSheldon Hearn 			usage();
108e9393a92STim J. Robbins 		exit(eval);
109d71e172aSSheldon Hearn 	}
1104b88c807SRodney W. Grimes 
111900c4ed3SConrad Meyer 	rflag = xflag = 0;
112d4319e74SEitan Adler 	while ((ch = getopt(argc, argv, "dfiIPRrvWx")) != -1)
1134b88c807SRodney W. Grimes 		switch(ch) {
1144b88c807SRodney W. Grimes 		case 'd':
1154b88c807SRodney W. Grimes 			dflag = 1;
1164b88c807SRodney W. Grimes 			break;
1174b88c807SRodney W. Grimes 		case 'f':
1184b88c807SRodney W. Grimes 			fflag = 1;
1194b88c807SRodney W. Grimes 			iflag = 0;
1204b88c807SRodney W. Grimes 			break;
1214b88c807SRodney W. Grimes 		case 'i':
1224b88c807SRodney W. Grimes 			fflag = 0;
1234b88c807SRodney W. Grimes 			iflag = 1;
1244b88c807SRodney W. Grimes 			break;
12524c0f738SXin LI 		case 'I':
12624c0f738SXin LI 			Iflag = 1;
12724c0f738SXin LI 			break;
1284b88c807SRodney W. Grimes 		case 'P':
129900c4ed3SConrad Meyer 			/* Compatibility no-op. */
1304b88c807SRodney W. Grimes 			break;
1314b88c807SRodney W. Grimes 		case 'R':
1324b88c807SRodney W. Grimes 		case 'r':			/* Compatibility. */
1334b88c807SRodney W. Grimes 			rflag = 1;
1344b88c807SRodney W. Grimes 			break;
135bfbdd545SMichael Haro 		case 'v':
136bfbdd545SMichael Haro 			vflag = 1;
137bfbdd545SMichael Haro 			break;
138777d1f82SMichael Haro 		case 'W':
139777d1f82SMichael Haro 			Wflag = 1;
140777d1f82SMichael Haro 			break;
141d4319e74SEitan Adler 		case 'x':
142d4319e74SEitan Adler 			xflag = 1;
143d4319e74SEitan Adler 			break;
1444b88c807SRodney W. Grimes 		default:
1454b88c807SRodney W. Grimes 			usage();
1464b88c807SRodney W. Grimes 		}
1474b88c807SRodney W. Grimes 	argc -= optind;
1484b88c807SRodney W. Grimes 	argv += optind;
1494b88c807SRodney W. Grimes 
1500e8f2d6cSJordan K. Hubbard 	if (argc < 1) {
1510e8f2d6cSJordan K. Hubbard 		if (fflag)
152f3761deeSGuido van Rooij 			return (0);
1534b88c807SRodney W. Grimes 		usage();
1540e8f2d6cSJordan K. Hubbard 	}
1554b88c807SRodney W. Grimes 
1564b88c807SRodney W. Grimes 	checkdot(argv);
15768ef5f71SDag-Erling Smørgrav 	checkslash(argv);
1586b419813SAndrey A. Chernov 	uid = geteuid();
1594b88c807SRodney W. Grimes 
160b5260db6SWarner Losh 	(void)signal(SIGINFO, siginfo);
1611f64b5c9SSteve Price 	if (*argv) {
1621f64b5c9SSteve Price 		stdin_ok = isatty(STDIN_FILENO);
1631f64b5c9SSteve Price 
16424c0f738SXin LI 		if (Iflag) {
16524c0f738SXin LI 			if (check2(argv) == 0)
16624c0f738SXin LI 				exit (1);
16724c0f738SXin LI 		}
1684b88c807SRodney W. Grimes 		if (rflag)
1694b88c807SRodney W. Grimes 			rm_tree(argv);
1704b88c807SRodney W. Grimes 		else
1714b88c807SRodney W. Grimes 			rm_file(argv);
1721f64b5c9SSteve Price 	}
1731f64b5c9SSteve Price 
1744b88c807SRodney W. Grimes 	exit (eval);
1754b88c807SRodney W. Grimes }
1764b88c807SRodney W. Grimes 
1779d6d3f96SEitan Adler static void
17846251ddeSWarner Losh rm_tree(char **argv)
1794b88c807SRodney W. Grimes {
1804b88c807SRodney W. Grimes 	FTS *fts;
1814b88c807SRodney W. Grimes 	FTSENT *p;
1824b88c807SRodney W. Grimes 	int needstat;
1831f64b5c9SSteve Price 	int flags;
1846b419813SAndrey A. Chernov 	int rval;
1854b88c807SRodney W. Grimes 
1864b88c807SRodney W. Grimes 	/*
1874b88c807SRodney W. Grimes 	 * Remove a file hierarchy.  If forcing removal (-f), or interactive
1884b88c807SRodney W. Grimes 	 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
1894b88c807SRodney W. Grimes 	 */
1901f64b5c9SSteve Price 	needstat = !uid || (!fflag && !iflag && stdin_ok);
1914b88c807SRodney W. Grimes 
1924b88c807SRodney W. Grimes 	/*
1934b88c807SRodney W. Grimes 	 * If the -i option is specified, the user can skip on the pre-order
1944b88c807SRodney W. Grimes 	 * visit.  The fts_number field flags skipped directories.
1954b88c807SRodney W. Grimes 	 */
1964b88c807SRodney W. Grimes #define	SKIPPED	1
1974b88c807SRodney W. Grimes 
1984a086b52SBruce Evans 	flags = FTS_PHYSICAL;
1991f64b5c9SSteve Price 	if (!needstat)
2001f64b5c9SSteve Price 		flags |= FTS_NOSTAT;
2011f64b5c9SSteve Price 	if (Wflag)
2021f64b5c9SSteve Price 		flags |= FTS_WHITEOUT;
203d4319e74SEitan Adler 	if (xflag)
204d4319e74SEitan Adler 		flags |= FTS_XDEV;
205de3abdfaSJordan K. Hubbard 	if (!(fts = fts_open(argv, flags, NULL))) {
206de3abdfaSJordan K. Hubbard 		if (fflag && errno == ENOENT)
207de3abdfaSJordan K. Hubbard 			return;
2085ad9e45fSMatthew Dillon 		err(1, "fts_open");
209de3abdfaSJordan K. Hubbard 	}
210*2dfa4b66SBryan Drewery 	while (errno = 0, (p = fts_read(fts)) != NULL) {
2114b88c807SRodney W. Grimes 		switch (p->fts_info) {
2124b88c807SRodney W. Grimes 		case FTS_DNR:
2134b88c807SRodney W. Grimes 			if (!fflag || p->fts_errno != ENOENT) {
2144b88c807SRodney W. Grimes 				warnx("%s: %s",
2154b88c807SRodney W. Grimes 				    p->fts_path, strerror(p->fts_errno));
2164b88c807SRodney W. Grimes 				eval = 1;
2174b88c807SRodney W. Grimes 			}
2184b88c807SRodney W. Grimes 			continue;
2194b88c807SRodney W. Grimes 		case FTS_ERR:
2204b88c807SRodney W. Grimes 			errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
2214b88c807SRodney W. Grimes 		case FTS_NS:
2224b88c807SRodney W. Grimes 			/*
223b800f53dSJun Kuriyama 			 * Assume that since fts_read() couldn't stat the
224b800f53dSJun Kuriyama 			 * file, it can't be unlinked.
2254b88c807SRodney W. Grimes 			 */
2264b88c807SRodney W. Grimes 			if (!needstat)
2274b88c807SRodney W. Grimes 				break;
2284b88c807SRodney W. Grimes 			if (!fflag || p->fts_errno != ENOENT) {
2294b88c807SRodney W. Grimes 				warnx("%s: %s",
2304b88c807SRodney W. Grimes 				    p->fts_path, strerror(p->fts_errno));
2314b88c807SRodney W. Grimes 				eval = 1;
2324b88c807SRodney W. Grimes 			}
2334b88c807SRodney W. Grimes 			continue;
2344b88c807SRodney W. Grimes 		case FTS_D:
2354b88c807SRodney W. Grimes 			/* Pre-order: give user chance to skip. */
2361f64b5c9SSteve Price 			if (!fflag && !check(p->fts_path, p->fts_accpath,
2374b88c807SRodney W. Grimes 			    p->fts_statp)) {
2384b88c807SRodney W. Grimes 				(void)fts_set(fts, p, FTS_SKIP);
2394b88c807SRodney W. Grimes 				p->fts_number = SKIPPED;
2404b88c807SRodney W. Grimes 			}
2416b419813SAndrey A. Chernov 			else if (!uid &&
2426b419813SAndrey A. Chernov 				 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
2436b419813SAndrey A. Chernov 				 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
2446911f596SJilles Tjoelker 				 lchflags(p->fts_accpath,
2456b419813SAndrey A. Chernov 					 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
2466b419813SAndrey A. Chernov 				goto err;
2474b88c807SRodney W. Grimes 			continue;
2484b88c807SRodney W. Grimes 		case FTS_DP:
2494b88c807SRodney W. Grimes 			/* Post-order: see if user skipped. */
2504b88c807SRodney W. Grimes 			if (p->fts_number == SKIPPED)
2514b88c807SRodney W. Grimes 				continue;
2524b88c807SRodney W. Grimes 			break;
2531f64b5c9SSteve Price 		default:
2544b88c807SRodney W. Grimes 			if (!fflag &&
2554b88c807SRodney W. Grimes 			    !check(p->fts_path, p->fts_accpath, p->fts_statp))
2564b88c807SRodney W. Grimes 				continue;
2571f64b5c9SSteve Price 		}
2584b88c807SRodney W. Grimes 
2596b419813SAndrey A. Chernov 		rval = 0;
2606b419813SAndrey A. Chernov 		if (!uid &&
2616b419813SAndrey A. Chernov 		    (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
2626b419813SAndrey A. Chernov 		    !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
2636911f596SJilles Tjoelker 			rval = lchflags(p->fts_accpath,
2646b419813SAndrey A. Chernov 				       p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
2650efa2040SMichael Haro 		if (rval == 0) {
2664b88c807SRodney W. Grimes 			/*
2674b88c807SRodney W. Grimes 			 * If we can't read or search the directory, may still be
2684b88c807SRodney W. Grimes 			 * able to remove it.  Don't print out the un{read,search}able
2694b88c807SRodney W. Grimes 			 * message unless the remove fails.
2704b88c807SRodney W. Grimes 			 */
2711f64b5c9SSteve Price 			switch (p->fts_info) {
2721f64b5c9SSteve Price 			case FTS_DP:
2731f64b5c9SSteve Price 			case FTS_DNR:
2740efa2040SMichael Haro 				rval = rmdir(p->fts_accpath);
2750efa2040SMichael Haro 				if (rval == 0 || (fflag && errno == ENOENT)) {
2760efa2040SMichael Haro 					if (rval == 0 && vflag)
277777d1f82SMichael Haro 						(void)printf("%s\n",
278b6f80a8eSDavid E. O'Brien 						    p->fts_path);
279b5260db6SWarner Losh 					if (rval == 0 && info) {
280b5260db6SWarner Losh 						info = 0;
281b5260db6SWarner Losh 						(void)printf("%s\n",
282b5260db6SWarner Losh 						    p->fts_path);
283b5260db6SWarner Losh 					}
2844b88c807SRodney W. Grimes 					continue;
285bfbdd545SMichael Haro 				}
2861f64b5c9SSteve Price 				break;
2871f64b5c9SSteve Price 
2881f64b5c9SSteve Price 			case FTS_W:
2890efa2040SMichael Haro 				rval = undelete(p->fts_accpath);
2900efa2040SMichael Haro 				if (rval == 0 && (fflag && errno == ENOENT)) {
2910efa2040SMichael Haro 					if (vflag)
292777d1f82SMichael Haro 						(void)printf("%s\n",
293b6f80a8eSDavid E. O'Brien 						    p->fts_path);
294b5260db6SWarner Losh 					if (info) {
295b5260db6SWarner Losh 						info = 0;
296b5260db6SWarner Losh 						(void)printf("%s\n",
297b5260db6SWarner Losh 						    p->fts_path);
298b5260db6SWarner Losh 					}
2994b88c807SRodney W. Grimes 					continue;
300bfbdd545SMichael Haro 				}
3011f64b5c9SSteve Price 				break;
3021f64b5c9SSteve Price 
303b800f53dSJun Kuriyama 			case FTS_NS:
304b800f53dSJun Kuriyama 				/*
305b800f53dSJun Kuriyama 				 * Assume that since fts_read() couldn't stat
306b800f53dSJun Kuriyama 				 * the file, it can't be unlinked.
307b800f53dSJun Kuriyama 				 */
308b800f53dSJun Kuriyama 				if (fflag)
309b800f53dSJun Kuriyama 					continue;
310b800f53dSJun Kuriyama 				/* FALLTHROUGH */
311930e3238SXin LI 
312930e3238SXin LI 			case FTS_F:
313930e3238SXin LI 			case FTS_NSOK:
314930e3238SXin LI 			default:
3150efa2040SMichael Haro 				rval = unlink(p->fts_accpath);
3160efa2040SMichael Haro 				if (rval == 0 || (fflag && errno == ENOENT)) {
3170efa2040SMichael Haro 					if (rval == 0 && vflag)
318777d1f82SMichael Haro 						(void)printf("%s\n",
319b6f80a8eSDavid E. O'Brien 						    p->fts_path);
320b5260db6SWarner Losh 					if (rval == 0 && info) {
321b5260db6SWarner Losh 						info = 0;
322b5260db6SWarner Losh 						(void)printf("%s\n",
323b5260db6SWarner Losh 						    p->fts_path);
324b5260db6SWarner Losh 					}
3254b88c807SRodney W. Grimes 					continue;
3264b88c807SRodney W. Grimes 				}
3276b419813SAndrey A. Chernov 			}
328bfbdd545SMichael Haro 		}
3296b419813SAndrey A. Chernov err:
3304b88c807SRodney W. Grimes 		warn("%s", p->fts_path);
3314b88c807SRodney W. Grimes 		eval = 1;
3324b88c807SRodney W. Grimes 	}
333413a368cSWarner Losh 	if (!fflag && errno)
3344b88c807SRodney W. Grimes 		err(1, "fts_read");
33508941824SMaxim Konovalov 	fts_close(fts);
3364b88c807SRodney W. Grimes }
3374b88c807SRodney W. Grimes 
3384c99904bSEitan Adler static void
33946251ddeSWarner Losh rm_file(char **argv)
3404b88c807SRodney W. Grimes {
3414b88c807SRodney W. Grimes 	struct stat sb;
3421f64b5c9SSteve Price 	int rval;
3434b88c807SRodney W. Grimes 	char *f;
3444b88c807SRodney W. Grimes 
3454b88c807SRodney W. Grimes 	/*
3464b88c807SRodney W. Grimes 	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
3474b88c807SRodney W. Grimes 	 * to remove a directory is an error, so must always stat the file.
3484b88c807SRodney W. Grimes 	 */
3494b88c807SRodney W. Grimes 	while ((f = *argv++) != NULL) {
3504b88c807SRodney W. Grimes 		/* Assume if can't stat the file, can't unlink it. */
3514b88c807SRodney W. Grimes 		if (lstat(f, &sb)) {
3521f64b5c9SSteve Price 			if (Wflag) {
3531f64b5c9SSteve Price 				sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
3541f64b5c9SSteve Price 			} else {
3554b88c807SRodney W. Grimes 				if (!fflag || errno != ENOENT) {
3564b88c807SRodney W. Grimes 					warn("%s", f);
3574b88c807SRodney W. Grimes 					eval = 1;
3584b88c807SRodney W. Grimes 				}
3594b88c807SRodney W. Grimes 				continue;
3604b88c807SRodney W. Grimes 			}
3611f64b5c9SSteve Price 		} else if (Wflag) {
3621f64b5c9SSteve Price 			warnx("%s: %s", f, strerror(EEXIST));
3631f64b5c9SSteve Price 			eval = 1;
3641f64b5c9SSteve Price 			continue;
3651f64b5c9SSteve Price 		}
3661f64b5c9SSteve Price 
3671f64b5c9SSteve Price 		if (S_ISDIR(sb.st_mode) && !dflag) {
3684b88c807SRodney W. Grimes 			warnx("%s: is a directory", f);
3694b88c807SRodney W. Grimes 			eval = 1;
3704b88c807SRodney W. Grimes 			continue;
3714b88c807SRodney W. Grimes 		}
3721f64b5c9SSteve Price 		if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
3734b88c807SRodney W. Grimes 			continue;
3746b419813SAndrey A. Chernov 		rval = 0;
375da29c656SMaxim Konovalov 		if (!uid && !S_ISWHT(sb.st_mode) &&
3766b419813SAndrey A. Chernov 		    (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
3776b419813SAndrey A. Chernov 		    !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
3786911f596SJilles Tjoelker 			rval = lchflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
3790efa2040SMichael Haro 		if (rval == 0) {
3801f64b5c9SSteve Price 			if (S_ISWHT(sb.st_mode))
3811f64b5c9SSteve Price 				rval = undelete(f);
3821f64b5c9SSteve Price 			else if (S_ISDIR(sb.st_mode))
3834b88c807SRodney W. Grimes 				rval = rmdir(f);
384900c4ed3SConrad Meyer 			else
3854b88c807SRodney W. Grimes 				rval = unlink(f);
3864b88c807SRodney W. Grimes 		}
3874b88c807SRodney W. Grimes 		if (rval && (!fflag || errno != ENOENT)) {
3884b88c807SRodney W. Grimes 			warn("%s", f);
3894b88c807SRodney W. Grimes 			eval = 1;
3904b88c807SRodney W. Grimes 		}
3910efa2040SMichael Haro 		if (vflag && rval == 0)
392bfbdd545SMichael Haro 			(void)printf("%s\n", f);
393b5260db6SWarner Losh 		if (info && rval == 0) {
394b5260db6SWarner Losh 			info = 0;
395b5260db6SWarner Losh 			(void)printf("%s\n", f);
396b5260db6SWarner Losh 		}
3974b88c807SRodney W. Grimes 	}
3984b88c807SRodney W. Grimes }
3994b88c807SRodney W. Grimes 
4009d6d3f96SEitan Adler static int
4019d6d3f96SEitan Adler check(const char *path, const char *name, struct stat *sp)
4024b88c807SRodney W. Grimes {
4034b88c807SRodney W. Grimes 	int ch, first;
404141d77b8SJosef Karthauser 	char modep[15], *flagsp;
4054b88c807SRodney W. Grimes 
4064b88c807SRodney W. Grimes 	/* Check -i first. */
4074b88c807SRodney W. Grimes 	if (iflag)
4084b88c807SRodney W. Grimes 		(void)fprintf(stderr, "remove %s? ", path);
4094b88c807SRodney W. Grimes 	else {
4104b88c807SRodney W. Grimes 		/*
4114b88c807SRodney W. Grimes 		 * If it's not a symbolic link and it's unwritable and we're
4124b88c807SRodney W. Grimes 		 * talking to a terminal, ask.  Symbolic links are excluded
4134b88c807SRodney W. Grimes 		 * because their permissions are meaningless.  Check stdin_ok
4144b88c807SRodney W. Grimes 		 * first because we may not have stat'ed the file.
4154b88c807SRodney W. Grimes 		 */
416a5f62950SDoug Barton 		if (!stdin_ok || S_ISLNK(sp->st_mode) ||
4171f64b5c9SSteve Price 		    (!access(name, W_OK) &&
4186b419813SAndrey A. Chernov 		    !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
4191f64b5c9SSteve Price 		    (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)))
4204b88c807SRodney W. Grimes 			return (1);
4214b88c807SRodney W. Grimes 		strmode(sp->st_mode, modep);
422141d77b8SJosef Karthauser 		if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
4235ad9e45fSMatthew Dillon 			err(1, "fflagstostr");
424141d77b8SJosef Karthauser 		(void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
4256f74a1c9SEnji Cooper 		    modep + 1, modep[10] == ' ' ? "" : " ",
4264b88c807SRodney W. Grimes 		    user_from_uid(sp->st_uid, 0),
4276b419813SAndrey A. Chernov 		    group_from_gid(sp->st_gid, 0),
428141d77b8SJosef Karthauser 		    *flagsp ? flagsp : "", *flagsp ? " " : "",
4296b419813SAndrey A. Chernov 		    path);
430141d77b8SJosef Karthauser 		free(flagsp);
4314b88c807SRodney W. Grimes 	}
4324b88c807SRodney W. Grimes 	(void)fflush(stderr);
4334b88c807SRodney W. Grimes 
4344b88c807SRodney W. Grimes 	first = ch = getchar();
4354b88c807SRodney W. Grimes 	while (ch != '\n' && ch != EOF)
4364b88c807SRodney W. Grimes 		ch = getchar();
437b0205affSWolfram Schneider 	return (first == 'y' || first == 'Y');
4384b88c807SRodney W. Grimes }
4394b88c807SRodney W. Grimes 
44068ef5f71SDag-Erling Smørgrav #define ISSLASH(a)	((a)[0] == '/' && (a)[1] == '\0')
4419d6d3f96SEitan Adler static void
44268ef5f71SDag-Erling Smørgrav checkslash(char **argv)
44368ef5f71SDag-Erling Smørgrav {
44468ef5f71SDag-Erling Smørgrav 	char **t, **u;
44568ef5f71SDag-Erling Smørgrav 	int complained;
44668ef5f71SDag-Erling Smørgrav 
44768ef5f71SDag-Erling Smørgrav 	complained = 0;
44868ef5f71SDag-Erling Smørgrav 	for (t = argv; *t;) {
44968ef5f71SDag-Erling Smørgrav 		if (ISSLASH(*t)) {
45068ef5f71SDag-Erling Smørgrav 			if (!complained++)
45168ef5f71SDag-Erling Smørgrav 				warnx("\"/\" may not be removed");
45268ef5f71SDag-Erling Smørgrav 			eval = 1;
45368ef5f71SDag-Erling Smørgrav 			for (u = t; u[0] != NULL; ++u)
45468ef5f71SDag-Erling Smørgrav 				u[0] = u[1];
45568ef5f71SDag-Erling Smørgrav 		} else {
45668ef5f71SDag-Erling Smørgrav 			++t;
45768ef5f71SDag-Erling Smørgrav 		}
45868ef5f71SDag-Erling Smørgrav 	}
45968ef5f71SDag-Erling Smørgrav }
46068ef5f71SDag-Erling Smørgrav 
4619d6d3f96SEitan Adler static int
46224c0f738SXin LI check2(char **argv)
46324c0f738SXin LI {
46424c0f738SXin LI 	struct stat st;
46524c0f738SXin LI 	int first;
46624c0f738SXin LI 	int ch;
46724c0f738SXin LI 	int fcount = 0;
46824c0f738SXin LI 	int dcount = 0;
46924c0f738SXin LI 	int i;
47024c0f738SXin LI 	const char *dname = NULL;
47124c0f738SXin LI 
47224c0f738SXin LI 	for (i = 0; argv[i]; ++i) {
47324c0f738SXin LI 		if (lstat(argv[i], &st) == 0) {
47424c0f738SXin LI 			if (S_ISDIR(st.st_mode)) {
47524c0f738SXin LI 				++dcount;
47624c0f738SXin LI 				dname = argv[i];    /* only used if 1 dir */
47724c0f738SXin LI 			} else {
47824c0f738SXin LI 				++fcount;
47924c0f738SXin LI 			}
48024c0f738SXin LI 		}
48124c0f738SXin LI 	}
48224c0f738SXin LI 	first = 0;
48324c0f738SXin LI 	while (first != 'n' && first != 'N' && first != 'y' && first != 'Y') {
48424c0f738SXin LI 		if (dcount && rflag) {
48524c0f738SXin LI 			fprintf(stderr, "recursively remove");
48624c0f738SXin LI 			if (dcount == 1)
48724c0f738SXin LI 				fprintf(stderr, " %s", dname);
48824c0f738SXin LI 			else
48924c0f738SXin LI 				fprintf(stderr, " %d dirs", dcount);
49024c0f738SXin LI 			if (fcount == 1)
49124c0f738SXin LI 				fprintf(stderr, " and 1 file");
49224c0f738SXin LI 			else if (fcount > 1)
49324c0f738SXin LI 				fprintf(stderr, " and %d files", fcount);
49424c0f738SXin LI 		} else if (dcount + fcount > 3) {
49524c0f738SXin LI 			fprintf(stderr, "remove %d files", dcount + fcount);
49624c0f738SXin LI 		} else {
49724c0f738SXin LI 			return(1);
49824c0f738SXin LI 		}
49924c0f738SXin LI 		fprintf(stderr, "? ");
50024c0f738SXin LI 		fflush(stderr);
50124c0f738SXin LI 
50224c0f738SXin LI 		first = ch = getchar();
50324c0f738SXin LI 		while (ch != '\n' && ch != EOF)
50424c0f738SXin LI 			ch = getchar();
50524c0f738SXin LI 		if (ch == EOF)
50624c0f738SXin LI 			break;
50724c0f738SXin LI 	}
50824c0f738SXin LI 	return (first == 'y' || first == 'Y');
50924c0f738SXin LI }
51024c0f738SXin LI 
51167a3d3a8SPoul-Henning Kamp #define ISDOT(a)	((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
5129d6d3f96SEitan Adler static void
51346251ddeSWarner Losh checkdot(char **argv)
5144b88c807SRodney W. Grimes {
5154b88c807SRodney W. Grimes 	char *p, **save, **t;
5164b88c807SRodney W. Grimes 	int complained;
5174b88c807SRodney W. Grimes 
5184b88c807SRodney W. Grimes 	complained = 0;
5194b88c807SRodney W. Grimes 	for (t = argv; *t;) {
5204b88c807SRodney W. Grimes 		if ((p = strrchr(*t, '/')) != NULL)
5214b88c807SRodney W. Grimes 			++p;
5224b88c807SRodney W. Grimes 		else
5234b88c807SRodney W. Grimes 			p = *t;
5244b88c807SRodney W. Grimes 		if (ISDOT(p)) {
5254b88c807SRodney W. Grimes 			if (!complained++)
5264b88c807SRodney W. Grimes 				warnx("\".\" and \"..\" may not be removed");
5274b88c807SRodney W. Grimes 			eval = 1;
5281f64b5c9SSteve Price 			for (save = t; (t[0] = t[1]) != NULL; ++t)
5291f64b5c9SSteve Price 				continue;
5304b88c807SRodney W. Grimes 			t = save;
5314b88c807SRodney W. Grimes 		} else
5324b88c807SRodney W. Grimes 			++t;
5334b88c807SRodney W. Grimes 	}
5344b88c807SRodney W. Grimes }
5354b88c807SRodney W. Grimes 
5369d6d3f96SEitan Adler static void
53746251ddeSWarner Losh usage(void)
5384b88c807SRodney W. Grimes {
5390efa2040SMichael Haro 
540d71e172aSSheldon Hearn 	(void)fprintf(stderr, "%s\n%s\n",
541d4319e74SEitan Adler 	    "usage: rm [-f | -i] [-dIPRrvWx] file ...",
54219b4f0dcSEd Maste 	    "       unlink [--] file");
543bfbdd545SMichael Haro 	exit(EX_USAGE);
5444b88c807SRodney W. Grimes }
545b5260db6SWarner Losh 
546b5260db6SWarner Losh static void
547b5260db6SWarner Losh siginfo(int sig __unused)
548b5260db6SWarner Losh {
549b5260db6SWarner Losh 
550b5260db6SWarner Losh 	info = 1;
551b5260db6SWarner Losh }
552