xref: /freebsd/bin/rm/rm.c (revision 9d6d3f96d03feee62481767f309774b669fd185d)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1990, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
64b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
74b88c807SRodney W. Grimes  * are met:
84b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
94b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
104b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
114b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
124b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
134b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
144b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
154b88c807SRodney W. Grimes  *    without specific prior written permission.
164b88c807SRodney W. Grimes  *
174b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
184b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
194b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
204b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
214b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
224b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
234b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
244b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
254b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
274b88c807SRodney W. Grimes  * SUCH DAMAGE.
284b88c807SRodney W. Grimes  */
294b88c807SRodney W. Grimes 
3009a80d48SDavid E. O'Brien #if 0
314b88c807SRodney W. Grimes #ifndef lint
3241cc862cSSteve Price static const char copyright[] =
334b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\
344b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
35395f4bf0SSteve Price #endif /* not lint */
36395f4bf0SSteve Price 
37395f4bf0SSteve Price #ifndef lint
38395f4bf0SSteve Price static char sccsid[] = "@(#)rm.c	8.5 (Berkeley) 4/18/94";
394b88c807SRodney W. Grimes #endif /* not lint */
4009a80d48SDavid E. O'Brien #endif
412749b141SDavid E. O'Brien #include <sys/cdefs.h>
422749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
434b88c807SRodney W. Grimes 
444b88c807SRodney W. Grimes #include <sys/stat.h>
45f80db2b8SKris Kennaway #include <sys/param.h>
46f80db2b8SKris Kennaway #include <sys/mount.h>
474b88c807SRodney W. Grimes 
484b88c807SRodney W. Grimes #include <err.h>
494b88c807SRodney W. Grimes #include <errno.h>
504b88c807SRodney W. Grimes #include <fcntl.h>
514b88c807SRodney W. Grimes #include <fts.h>
52fc69394fSWarner Losh #include <grp.h>
53fc69394fSWarner Losh #include <pwd.h>
546db1a7f1SMatthew D Fleming #include <stdint.h>
554b88c807SRodney W. Grimes #include <stdio.h>
564b88c807SRodney W. Grimes #include <stdlib.h>
574b88c807SRodney W. Grimes #include <string.h>
58bfbdd545SMichael Haro #include <sysexits.h>
594b88c807SRodney W. Grimes #include <unistd.h>
604b88c807SRodney W. Grimes 
61f9d4afb4SEd Schouten static int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
62d4319e74SEitan Adler static int rflag, Iflag, xflag;
63f9d4afb4SEd Schouten static uid_t uid;
64f9d4afb4SEd Schouten static volatile sig_atomic_t info;
654b88c807SRodney W. Grimes 
66*9d6d3f96SEitan Adler static int	check(const char *, const char *, struct stat *);
67*9d6d3f96SEitan Adler static int	check2(char **);
68*9d6d3f96SEitan Adler static void	checkdot(char **);
69*9d6d3f96SEitan Adler static void	checkslash(char **);
70*9d6d3f96SEitan Adler static void	rm_file(char **);
71*9d6d3f96SEitan Adler static int	rm_overwrite(const char *, struct stat *);
72*9d6d3f96SEitan Adler static void	rm_tree(char **);
73b5260db6SWarner Losh static void siginfo(int __unused);
74*9d6d3f96SEitan Adler static void	usage(void);
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes /*
774b88c807SRodney W. Grimes  * rm --
784b88c807SRodney W. Grimes  *	This rm is different from historic rm's, but is expected to match
794b88c807SRodney W. Grimes  *	POSIX 1003.2 behavior.	The most visible difference is that -f
804b88c807SRodney W. Grimes  *	has two specific effects now, ignore non-existent files and force
814b88c807SRodney W. Grimes  *	file removal.
824b88c807SRodney W. Grimes  */
834b88c807SRodney W. Grimes int
8446251ddeSWarner Losh main(int argc, char *argv[])
854b88c807SRodney W. Grimes {
8624c0f738SXin LI 	int ch;
87d71e172aSSheldon Hearn 	char *p;
88d71e172aSSheldon Hearn 
89d71e172aSSheldon Hearn 	/*
90d71e172aSSheldon Hearn 	 * Test for the special case where the utility is called as
91d71e172aSSheldon Hearn 	 * "unlink", for which the functionality provided is greatly
92d71e172aSSheldon Hearn 	 * simplified.
93d71e172aSSheldon Hearn 	 */
940cf90cd1SJilles Tjoelker 	if ((p = strrchr(argv[0], '/')) == NULL)
95d71e172aSSheldon Hearn 		p = argv[0];
96d71e172aSSheldon Hearn 	else
97d71e172aSSheldon Hearn 		++p;
98d71e172aSSheldon Hearn 	if (strcmp(p, "unlink") == 0) {
99e9393a92STim J. Robbins 		while (getopt(argc, argv, "") != -1)
100d71e172aSSheldon Hearn 			usage();
101e9393a92STim J. Robbins 		argc -= optind;
102e9393a92STim J. Robbins 		argv += optind;
10390833c99STim J. Robbins 		if (argc != 1)
104e9393a92STim J. Robbins 			usage();
105e9393a92STim J. Robbins 		rm_file(&argv[0]);
106e9393a92STim J. Robbins 		exit(eval);
107d71e172aSSheldon Hearn 	}
1084b88c807SRodney W. Grimes 
109d4319e74SEitan Adler 	Pflag = rflag = xflag = 0;
110d4319e74SEitan Adler 	while ((ch = getopt(argc, argv, "dfiIPRrvWx")) != -1)
1114b88c807SRodney W. Grimes 		switch(ch) {
1124b88c807SRodney W. Grimes 		case 'd':
1134b88c807SRodney W. Grimes 			dflag = 1;
1144b88c807SRodney W. Grimes 			break;
1154b88c807SRodney W. Grimes 		case 'f':
1164b88c807SRodney W. Grimes 			fflag = 1;
1174b88c807SRodney W. Grimes 			iflag = 0;
1184b88c807SRodney W. Grimes 			break;
1194b88c807SRodney W. Grimes 		case 'i':
1204b88c807SRodney W. Grimes 			fflag = 0;
1214b88c807SRodney W. Grimes 			iflag = 1;
1224b88c807SRodney W. Grimes 			break;
12324c0f738SXin LI 		case 'I':
12424c0f738SXin LI 			Iflag = 1;
12524c0f738SXin LI 			break;
1264b88c807SRodney W. Grimes 		case 'P':
1274b88c807SRodney W. Grimes 			Pflag = 1;
1284b88c807SRodney W. Grimes 			break;
1294b88c807SRodney W. Grimes 		case 'R':
1304b88c807SRodney W. Grimes 		case 'r':			/* Compatibility. */
1314b88c807SRodney W. Grimes 			rflag = 1;
1324b88c807SRodney W. Grimes 			break;
133bfbdd545SMichael Haro 		case 'v':
134bfbdd545SMichael Haro 			vflag = 1;
135bfbdd545SMichael Haro 			break;
136777d1f82SMichael Haro 		case 'W':
137777d1f82SMichael Haro 			Wflag = 1;
138777d1f82SMichael Haro 			break;
139d4319e74SEitan Adler 		case 'x':
140d4319e74SEitan Adler 			xflag = 1;
141d4319e74SEitan Adler 			break;
1424b88c807SRodney W. Grimes 		default:
1434b88c807SRodney W. Grimes 			usage();
1444b88c807SRodney W. Grimes 		}
1454b88c807SRodney W. Grimes 	argc -= optind;
1464b88c807SRodney W. Grimes 	argv += optind;
1474b88c807SRodney W. Grimes 
1480e8f2d6cSJordan K. Hubbard 	if (argc < 1) {
1490e8f2d6cSJordan K. Hubbard 		if (fflag)
150f3761deeSGuido van Rooij 			return (0);
1514b88c807SRodney W. Grimes 		usage();
1520e8f2d6cSJordan K. Hubbard 	}
1534b88c807SRodney W. Grimes 
1544b88c807SRodney W. Grimes 	checkdot(argv);
1553f91ab92SDag-Erling Smørgrav 	if (getenv("POSIXLY_CORRECT") == NULL)
15668ef5f71SDag-Erling Smørgrav 		checkslash(argv);
1576b419813SAndrey A. Chernov 	uid = geteuid();
1584b88c807SRodney W. Grimes 
159b5260db6SWarner Losh 	(void)signal(SIGINFO, siginfo);
1601f64b5c9SSteve Price 	if (*argv) {
1611f64b5c9SSteve Price 		stdin_ok = isatty(STDIN_FILENO);
1621f64b5c9SSteve Price 
16324c0f738SXin LI 		if (Iflag) {
16424c0f738SXin LI 			if (check2(argv) == 0)
16524c0f738SXin LI 				exit (1);
16624c0f738SXin LI 		}
1674b88c807SRodney W. Grimes 		if (rflag)
1684b88c807SRodney W. Grimes 			rm_tree(argv);
1694b88c807SRodney W. Grimes 		else
1704b88c807SRodney W. Grimes 			rm_file(argv);
1711f64b5c9SSteve Price 	}
1721f64b5c9SSteve Price 
1734b88c807SRodney W. Grimes 	exit (eval);
1744b88c807SRodney W. Grimes }
1754b88c807SRodney W. Grimes 
176*9d6d3f96SEitan Adler static void
17746251ddeSWarner Losh rm_tree(char **argv)
1784b88c807SRodney W. Grimes {
1794b88c807SRodney W. Grimes 	FTS *fts;
1804b88c807SRodney W. Grimes 	FTSENT *p;
1814b88c807SRodney W. Grimes 	int needstat;
1821f64b5c9SSteve Price 	int flags;
1836b419813SAndrey A. Chernov 	int rval;
1844b88c807SRodney W. Grimes 
1854b88c807SRodney W. Grimes 	/*
1864b88c807SRodney W. Grimes 	 * Remove a file hierarchy.  If forcing removal (-f), or interactive
1874b88c807SRodney W. Grimes 	 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
1884b88c807SRodney W. Grimes 	 */
1891f64b5c9SSteve Price 	needstat = !uid || (!fflag && !iflag && stdin_ok);
1904b88c807SRodney W. Grimes 
1914b88c807SRodney W. Grimes 	/*
1924b88c807SRodney W. Grimes 	 * If the -i option is specified, the user can skip on the pre-order
1934b88c807SRodney W. Grimes 	 * visit.  The fts_number field flags skipped directories.
1944b88c807SRodney W. Grimes 	 */
1954b88c807SRodney W. Grimes #define	SKIPPED	1
1964b88c807SRodney W. Grimes 
1974a086b52SBruce Evans 	flags = FTS_PHYSICAL;
1981f64b5c9SSteve Price 	if (!needstat)
1991f64b5c9SSteve Price 		flags |= FTS_NOSTAT;
2001f64b5c9SSteve Price 	if (Wflag)
2011f64b5c9SSteve Price 		flags |= FTS_WHITEOUT;
202d4319e74SEitan Adler 	if (xflag)
203d4319e74SEitan Adler 		flags |= FTS_XDEV;
204de3abdfaSJordan K. Hubbard 	if (!(fts = fts_open(argv, flags, NULL))) {
205de3abdfaSJordan K. Hubbard 		if (fflag && errno == ENOENT)
206de3abdfaSJordan K. Hubbard 			return;
2075ad9e45fSMatthew Dillon 		err(1, "fts_open");
208de3abdfaSJordan K. Hubbard 	}
2094b88c807SRodney W. Grimes 	while ((p = fts_read(fts)) != NULL) {
2104b88c807SRodney W. Grimes 		switch (p->fts_info) {
2114b88c807SRodney W. Grimes 		case FTS_DNR:
2124b88c807SRodney W. Grimes 			if (!fflag || p->fts_errno != ENOENT) {
2134b88c807SRodney W. Grimes 				warnx("%s: %s",
2144b88c807SRodney W. Grimes 				    p->fts_path, strerror(p->fts_errno));
2154b88c807SRodney W. Grimes 				eval = 1;
2164b88c807SRodney W. Grimes 			}
2174b88c807SRodney W. Grimes 			continue;
2184b88c807SRodney W. Grimes 		case FTS_ERR:
2194b88c807SRodney W. Grimes 			errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
2204b88c807SRodney W. Grimes 		case FTS_NS:
2214b88c807SRodney W. Grimes 			/*
222b800f53dSJun Kuriyama 			 * Assume that since fts_read() couldn't stat the
223b800f53dSJun Kuriyama 			 * file, it can't be unlinked.
2244b88c807SRodney W. Grimes 			 */
2254b88c807SRodney W. Grimes 			if (!needstat)
2264b88c807SRodney W. Grimes 				break;
2274b88c807SRodney W. Grimes 			if (!fflag || p->fts_errno != ENOENT) {
2284b88c807SRodney W. Grimes 				warnx("%s: %s",
2294b88c807SRodney W. Grimes 				    p->fts_path, strerror(p->fts_errno));
2304b88c807SRodney W. Grimes 				eval = 1;
2314b88c807SRodney W. Grimes 			}
2324b88c807SRodney W. Grimes 			continue;
2334b88c807SRodney W. Grimes 		case FTS_D:
2344b88c807SRodney W. Grimes 			/* Pre-order: give user chance to skip. */
2351f64b5c9SSteve Price 			if (!fflag && !check(p->fts_path, p->fts_accpath,
2364b88c807SRodney W. Grimes 			    p->fts_statp)) {
2374b88c807SRodney W. Grimes 				(void)fts_set(fts, p, FTS_SKIP);
2384b88c807SRodney W. Grimes 				p->fts_number = SKIPPED;
2394b88c807SRodney W. Grimes 			}
2406b419813SAndrey A. Chernov 			else if (!uid &&
2416b419813SAndrey A. Chernov 				 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
2426b419813SAndrey A. Chernov 				 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
2436911f596SJilles Tjoelker 				 lchflags(p->fts_accpath,
2446b419813SAndrey A. Chernov 					 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
2456b419813SAndrey A. Chernov 				goto err;
2464b88c807SRodney W. Grimes 			continue;
2474b88c807SRodney W. Grimes 		case FTS_DP:
2484b88c807SRodney W. Grimes 			/* Post-order: see if user skipped. */
2494b88c807SRodney W. Grimes 			if (p->fts_number == SKIPPED)
2504b88c807SRodney W. Grimes 				continue;
2514b88c807SRodney W. Grimes 			break;
2521f64b5c9SSteve Price 		default:
2534b88c807SRodney W. Grimes 			if (!fflag &&
2544b88c807SRodney W. Grimes 			    !check(p->fts_path, p->fts_accpath, p->fts_statp))
2554b88c807SRodney W. Grimes 				continue;
2561f64b5c9SSteve Price 		}
2574b88c807SRodney W. Grimes 
2586b419813SAndrey A. Chernov 		rval = 0;
2596b419813SAndrey A. Chernov 		if (!uid &&
2606b419813SAndrey A. Chernov 		    (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
2616b419813SAndrey A. Chernov 		    !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
2626911f596SJilles Tjoelker 			rval = lchflags(p->fts_accpath,
2636b419813SAndrey A. Chernov 				       p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
2640efa2040SMichael Haro 		if (rval == 0) {
2654b88c807SRodney W. Grimes 			/*
2664b88c807SRodney W. Grimes 			 * If we can't read or search the directory, may still be
2674b88c807SRodney W. Grimes 			 * able to remove it.  Don't print out the un{read,search}able
2684b88c807SRodney W. Grimes 			 * message unless the remove fails.
2694b88c807SRodney W. Grimes 			 */
2701f64b5c9SSteve Price 			switch (p->fts_info) {
2711f64b5c9SSteve Price 			case FTS_DP:
2721f64b5c9SSteve Price 			case FTS_DNR:
2730efa2040SMichael Haro 				rval = rmdir(p->fts_accpath);
2740efa2040SMichael Haro 				if (rval == 0 || (fflag && errno == ENOENT)) {
2750efa2040SMichael Haro 					if (rval == 0 && vflag)
276777d1f82SMichael Haro 						(void)printf("%s\n",
277b6f80a8eSDavid E. O'Brien 						    p->fts_path);
278b5260db6SWarner Losh 					if (rval == 0 && info) {
279b5260db6SWarner Losh 						info = 0;
280b5260db6SWarner Losh 						(void)printf("%s\n",
281b5260db6SWarner Losh 						    p->fts_path);
282b5260db6SWarner Losh 					}
2834b88c807SRodney W. Grimes 					continue;
284bfbdd545SMichael Haro 				}
2851f64b5c9SSteve Price 				break;
2861f64b5c9SSteve Price 
2871f64b5c9SSteve Price 			case FTS_W:
2880efa2040SMichael Haro 				rval = undelete(p->fts_accpath);
2890efa2040SMichael Haro 				if (rval == 0 && (fflag && errno == ENOENT)) {
2900efa2040SMichael Haro 					if (vflag)
291777d1f82SMichael Haro 						(void)printf("%s\n",
292b6f80a8eSDavid E. O'Brien 						    p->fts_path);
293b5260db6SWarner Losh 					if (info) {
294b5260db6SWarner Losh 						info = 0;
295b5260db6SWarner Losh 						(void)printf("%s\n",
296b5260db6SWarner Losh 						    p->fts_path);
297b5260db6SWarner Losh 					}
2984b88c807SRodney W. Grimes 					continue;
299bfbdd545SMichael Haro 				}
3001f64b5c9SSteve Price 				break;
3011f64b5c9SSteve Price 
302b800f53dSJun Kuriyama 			case FTS_NS:
303b800f53dSJun Kuriyama 				/*
304b800f53dSJun Kuriyama 				 * Assume that since fts_read() couldn't stat
305b800f53dSJun Kuriyama 				 * the file, it can't be unlinked.
306b800f53dSJun Kuriyama 				 */
307b800f53dSJun Kuriyama 				if (fflag)
308b800f53dSJun Kuriyama 					continue;
309b800f53dSJun Kuriyama 				/* FALLTHROUGH */
310930e3238SXin LI 
311930e3238SXin LI 			case FTS_F:
312930e3238SXin LI 			case FTS_NSOK:
3134b88c807SRodney W. Grimes 				if (Pflag)
314930e3238SXin LI 					if (!rm_overwrite(p->fts_accpath, p->fts_info ==
315930e3238SXin LI 					    FTS_NSOK ? NULL : p->fts_statp))
316f3761deeSGuido van Rooij 						continue;
317930e3238SXin LI 				/* FALLTHROUGH */
318930e3238SXin LI 
319930e3238SXin LI 			default:
3200efa2040SMichael Haro 				rval = unlink(p->fts_accpath);
3210efa2040SMichael Haro 				if (rval == 0 || (fflag && errno == ENOENT)) {
3220efa2040SMichael Haro 					if (rval == 0 && vflag)
323777d1f82SMichael Haro 						(void)printf("%s\n",
324b6f80a8eSDavid E. O'Brien 						    p->fts_path);
325b5260db6SWarner Losh 					if (rval == 0 && info) {
326b5260db6SWarner Losh 						info = 0;
327b5260db6SWarner Losh 						(void)printf("%s\n",
328b5260db6SWarner Losh 						    p->fts_path);
329b5260db6SWarner Losh 					}
3304b88c807SRodney W. Grimes 					continue;
3314b88c807SRodney W. Grimes 				}
3326b419813SAndrey A. Chernov 			}
333bfbdd545SMichael Haro 		}
3346b419813SAndrey A. Chernov err:
3354b88c807SRodney W. Grimes 		warn("%s", p->fts_path);
3364b88c807SRodney W. Grimes 		eval = 1;
3374b88c807SRodney W. Grimes 	}
3384b88c807SRodney W. Grimes 	if (errno)
3394b88c807SRodney W. Grimes 		err(1, "fts_read");
34008941824SMaxim Konovalov 	fts_close(fts);
3414b88c807SRodney W. Grimes }
3424b88c807SRodney W. Grimes 
3434b88c807SRodney W. Grimes void
34446251ddeSWarner Losh rm_file(char **argv)
3454b88c807SRodney W. Grimes {
3464b88c807SRodney W. Grimes 	struct stat sb;
3471f64b5c9SSteve Price 	int rval;
3484b88c807SRodney W. Grimes 	char *f;
3494b88c807SRodney W. Grimes 
3504b88c807SRodney W. Grimes 	/*
3514b88c807SRodney W. Grimes 	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
3524b88c807SRodney W. Grimes 	 * to remove a directory is an error, so must always stat the file.
3534b88c807SRodney W. Grimes 	 */
3544b88c807SRodney W. Grimes 	while ((f = *argv++) != NULL) {
3554b88c807SRodney W. Grimes 		/* Assume if can't stat the file, can't unlink it. */
3564b88c807SRodney W. Grimes 		if (lstat(f, &sb)) {
3571f64b5c9SSteve Price 			if (Wflag) {
3581f64b5c9SSteve Price 				sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
3591f64b5c9SSteve Price 			} else {
3604b88c807SRodney W. Grimes 				if (!fflag || errno != ENOENT) {
3614b88c807SRodney W. Grimes 					warn("%s", f);
3624b88c807SRodney W. Grimes 					eval = 1;
3634b88c807SRodney W. Grimes 				}
3644b88c807SRodney W. Grimes 				continue;
3654b88c807SRodney W. Grimes 			}
3661f64b5c9SSteve Price 		} else if (Wflag) {
3671f64b5c9SSteve Price 			warnx("%s: %s", f, strerror(EEXIST));
3681f64b5c9SSteve Price 			eval = 1;
3691f64b5c9SSteve Price 			continue;
3701f64b5c9SSteve Price 		}
3711f64b5c9SSteve Price 
3721f64b5c9SSteve Price 		if (S_ISDIR(sb.st_mode) && !dflag) {
3734b88c807SRodney W. Grimes 			warnx("%s: is a directory", f);
3744b88c807SRodney W. Grimes 			eval = 1;
3754b88c807SRodney W. Grimes 			continue;
3764b88c807SRodney W. Grimes 		}
3771f64b5c9SSteve Price 		if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
3784b88c807SRodney W. Grimes 			continue;
3796b419813SAndrey A. Chernov 		rval = 0;
380da29c656SMaxim Konovalov 		if (!uid && !S_ISWHT(sb.st_mode) &&
3816b419813SAndrey A. Chernov 		    (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
3826b419813SAndrey A. Chernov 		    !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
3836911f596SJilles Tjoelker 			rval = lchflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
3840efa2040SMichael Haro 		if (rval == 0) {
3851f64b5c9SSteve Price 			if (S_ISWHT(sb.st_mode))
3861f64b5c9SSteve Price 				rval = undelete(f);
3871f64b5c9SSteve Price 			else if (S_ISDIR(sb.st_mode))
3884b88c807SRodney W. Grimes 				rval = rmdir(f);
3894b88c807SRodney W. Grimes 			else {
3904b88c807SRodney W. Grimes 				if (Pflag)
391f3761deeSGuido van Rooij 					if (!rm_overwrite(f, &sb))
392f3761deeSGuido van Rooij 						continue;
3934b88c807SRodney W. Grimes 				rval = unlink(f);
3944b88c807SRodney W. Grimes 			}
3956b419813SAndrey A. Chernov 		}
3964b88c807SRodney W. Grimes 		if (rval && (!fflag || errno != ENOENT)) {
3974b88c807SRodney W. Grimes 			warn("%s", f);
3984b88c807SRodney W. Grimes 			eval = 1;
3994b88c807SRodney W. Grimes 		}
4000efa2040SMichael Haro 		if (vflag && rval == 0)
401bfbdd545SMichael Haro 			(void)printf("%s\n", f);
402b5260db6SWarner Losh 		if (info && rval == 0) {
403b5260db6SWarner Losh 			info = 0;
404b5260db6SWarner Losh 			(void)printf("%s\n", f);
405b5260db6SWarner Losh 		}
4064b88c807SRodney W. Grimes 	}
4074b88c807SRodney W. Grimes }
4084b88c807SRodney W. Grimes 
4094b88c807SRodney W. Grimes /*
4104b88c807SRodney W. Grimes  * rm_overwrite --
4114b88c807SRodney W. Grimes  *	Overwrite the file 3 times with varying bit patterns.
4124b88c807SRodney W. Grimes  *
4134b88c807SRodney W. Grimes  * XXX
4144b88c807SRodney W. Grimes  * This is a cheap way to *really* delete files.  Note that only regular
4154b88c807SRodney W. Grimes  * files are deleted, directories (and therefore names) will remain.
4164b88c807SRodney W. Grimes  * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
417a3800f8fSUlrich Spörlein  * System V file system).  In a logging or COW file system, you'll have to
418a3800f8fSUlrich Spörlein  * have kernel support.
4194b88c807SRodney W. Grimes  */
420f3761deeSGuido van Rooij int
421*9d6d3f96SEitan Adler rm_overwrite(const char *file, struct stat *sbp)
4224b88c807SRodney W. Grimes {
4234ec1405bSKevin Lo 	struct stat sb, sb2;
424f80db2b8SKris Kennaway 	struct statfs fsb;
4254b88c807SRodney W. Grimes 	off_t len;
426f80db2b8SKris Kennaway 	int bsize, fd, wlen;
427f80db2b8SKris Kennaway 	char *buf = NULL;
4284b88c807SRodney W. Grimes 
4294b88c807SRodney W. Grimes 	fd = -1;
4304b88c807SRodney W. Grimes 	if (sbp == NULL) {
4314b88c807SRodney W. Grimes 		if (lstat(file, &sb))
4324b88c807SRodney W. Grimes 			goto err;
4334b88c807SRodney W. Grimes 		sbp = &sb;
4344b88c807SRodney W. Grimes 	}
4354b88c807SRodney W. Grimes 	if (!S_ISREG(sbp->st_mode))
436f3761deeSGuido van Rooij 		return (1);
43786da4a5eSXin LI 	if (sbp->st_nlink > 1 && !fflag) {
4386db1a7f1SMatthew D Fleming 		warnx("%s (inode %ju): not overwritten due to multiple links",
4396db1a7f1SMatthew D Fleming 		    file, (uintmax_t)sbp->st_ino);
44086da4a5eSXin LI 		return (0);
4410b6f55b7SXin LI 	}
4424ec1405bSKevin Lo 	if ((fd = open(file, O_WRONLY|O_NONBLOCK|O_NOFOLLOW, 0)) == -1)
4434b88c807SRodney W. Grimes 		goto err;
4444ec1405bSKevin Lo 	if (fstat(fd, &sb2))
4454ec1405bSKevin Lo 		goto err;
4464ec1405bSKevin Lo 	if (sb2.st_dev != sbp->st_dev || sb2.st_ino != sbp->st_ino ||
4474ec1405bSKevin Lo 	    !S_ISREG(sb2.st_mode)) {
4484ec1405bSKevin Lo 		errno = EPERM;
4494ec1405bSKevin Lo 		goto err;
4504ec1405bSKevin Lo 	}
451f80db2b8SKris Kennaway 	if (fstatfs(fd, &fsb) == -1)
452f80db2b8SKris Kennaway 		goto err;
453f80db2b8SKris Kennaway 	bsize = MAX(fsb.f_iosize, 1024);
454f80db2b8SKris Kennaway 	if ((buf = malloc(bsize)) == NULL)
4558f1f4338SBruce Evans 		err(1, "%s: malloc", file);
4564b88c807SRodney W. Grimes 
4574b88c807SRodney W. Grimes #define	PASS(byte) {							\
458f80db2b8SKris Kennaway 	memset(buf, byte, bsize);					\
4594b88c807SRodney W. Grimes 	for (len = sbp->st_size; len > 0; len -= wlen) {		\
460f80db2b8SKris Kennaway 		wlen = len < bsize ? len : bsize;			\
4614b88c807SRodney W. Grimes 		if (write(fd, buf, wlen) != wlen)			\
4624b88c807SRodney W. Grimes 			goto err;					\
4634b88c807SRodney W. Grimes 	}								\
4644b88c807SRodney W. Grimes }
4654b88c807SRodney W. Grimes 	PASS(0xff);
4664b88c807SRodney W. Grimes 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
4674b88c807SRodney W. Grimes 		goto err;
4684b88c807SRodney W. Grimes 	PASS(0x00);
4694b88c807SRodney W. Grimes 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
4704b88c807SRodney W. Grimes 		goto err;
4714b88c807SRodney W. Grimes 	PASS(0xff);
472f80db2b8SKris Kennaway 	if (!fsync(fd) && !close(fd)) {
473f80db2b8SKris Kennaway 		free(buf);
474f3761deeSGuido van Rooij 		return (1);
475f80db2b8SKris Kennaway 	}
4764b88c807SRodney W. Grimes 
4774b88c807SRodney W. Grimes err:	eval = 1;
478f80db2b8SKris Kennaway 	if (buf)
479f80db2b8SKris Kennaway 		free(buf);
4808f1f4338SBruce Evans 	if (fd != -1)
4818f1f4338SBruce Evans 		close(fd);
4824b88c807SRodney W. Grimes 	warn("%s", file);
483f3761deeSGuido van Rooij 	return (0);
4844b88c807SRodney W. Grimes }
4854b88c807SRodney W. Grimes 
4864b88c807SRodney W. Grimes 
487*9d6d3f96SEitan Adler static int
488*9d6d3f96SEitan Adler check(const char *path, const char *name, struct stat *sp)
4894b88c807SRodney W. Grimes {
4904b88c807SRodney W. Grimes 	int ch, first;
491141d77b8SJosef Karthauser 	char modep[15], *flagsp;
4924b88c807SRodney W. Grimes 
4934b88c807SRodney W. Grimes 	/* Check -i first. */
4944b88c807SRodney W. Grimes 	if (iflag)
4954b88c807SRodney W. Grimes 		(void)fprintf(stderr, "remove %s? ", path);
4964b88c807SRodney W. Grimes 	else {
4974b88c807SRodney W. Grimes 		/*
4984b88c807SRodney W. Grimes 		 * If it's not a symbolic link and it's unwritable and we're
4994b88c807SRodney W. Grimes 		 * talking to a terminal, ask.  Symbolic links are excluded
5004b88c807SRodney W. Grimes 		 * because their permissions are meaningless.  Check stdin_ok
5014b88c807SRodney W. Grimes 		 * first because we may not have stat'ed the file.
5024b88c807SRodney W. Grimes 		 */
503a5f62950SDoug Barton 		if (!stdin_ok || S_ISLNK(sp->st_mode) ||
5041f64b5c9SSteve Price 		    (!access(name, W_OK) &&
5056b419813SAndrey A. Chernov 		    !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
5061f64b5c9SSteve Price 		    (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)))
5074b88c807SRodney W. Grimes 			return (1);
5084b88c807SRodney W. Grimes 		strmode(sp->st_mode, modep);
509141d77b8SJosef Karthauser 		if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
5105ad9e45fSMatthew Dillon 			err(1, "fflagstostr");
511a5f62950SDoug Barton 		if (Pflag)
512a5f62950SDoug Barton 			errx(1,
513a5f62950SDoug Barton 			    "%s: -P was specified, but file is not writable",
514a5f62950SDoug Barton 			    path);
515141d77b8SJosef Karthauser 		(void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
5164b88c807SRodney W. Grimes 		    modep + 1, modep[9] == ' ' ? "" : " ",
5174b88c807SRodney W. Grimes 		    user_from_uid(sp->st_uid, 0),
5186b419813SAndrey A. Chernov 		    group_from_gid(sp->st_gid, 0),
519141d77b8SJosef Karthauser 		    *flagsp ? flagsp : "", *flagsp ? " " : "",
5206b419813SAndrey A. Chernov 		    path);
521141d77b8SJosef Karthauser 		free(flagsp);
5224b88c807SRodney W. Grimes 	}
5234b88c807SRodney W. Grimes 	(void)fflush(stderr);
5244b88c807SRodney W. Grimes 
5254b88c807SRodney W. Grimes 	first = ch = getchar();
5264b88c807SRodney W. Grimes 	while (ch != '\n' && ch != EOF)
5274b88c807SRodney W. Grimes 		ch = getchar();
528b0205affSWolfram Schneider 	return (first == 'y' || first == 'Y');
5294b88c807SRodney W. Grimes }
5304b88c807SRodney W. Grimes 
53168ef5f71SDag-Erling Smørgrav #define ISSLASH(a)	((a)[0] == '/' && (a)[1] == '\0')
532*9d6d3f96SEitan Adler static void
53368ef5f71SDag-Erling Smørgrav checkslash(char **argv)
53468ef5f71SDag-Erling Smørgrav {
53568ef5f71SDag-Erling Smørgrav 	char **t, **u;
53668ef5f71SDag-Erling Smørgrav 	int complained;
53768ef5f71SDag-Erling Smørgrav 
53868ef5f71SDag-Erling Smørgrav 	complained = 0;
53968ef5f71SDag-Erling Smørgrav 	for (t = argv; *t;) {
54068ef5f71SDag-Erling Smørgrav 		if (ISSLASH(*t)) {
54168ef5f71SDag-Erling Smørgrav 			if (!complained++)
54268ef5f71SDag-Erling Smørgrav 				warnx("\"/\" may not be removed");
54368ef5f71SDag-Erling Smørgrav 			eval = 1;
54468ef5f71SDag-Erling Smørgrav 			for (u = t; u[0] != NULL; ++u)
54568ef5f71SDag-Erling Smørgrav 				u[0] = u[1];
54668ef5f71SDag-Erling Smørgrav 		} else {
54768ef5f71SDag-Erling Smørgrav 			++t;
54868ef5f71SDag-Erling Smørgrav 		}
54968ef5f71SDag-Erling Smørgrav 	}
55068ef5f71SDag-Erling Smørgrav }
55168ef5f71SDag-Erling Smørgrav 
552*9d6d3f96SEitan Adler static int
55324c0f738SXin LI check2(char **argv)
55424c0f738SXin LI {
55524c0f738SXin LI 	struct stat st;
55624c0f738SXin LI 	int first;
55724c0f738SXin LI 	int ch;
55824c0f738SXin LI 	int fcount = 0;
55924c0f738SXin LI 	int dcount = 0;
56024c0f738SXin LI 	int i;
56124c0f738SXin LI 	const char *dname = NULL;
56224c0f738SXin LI 
56324c0f738SXin LI 	for (i = 0; argv[i]; ++i) {
56424c0f738SXin LI 		if (lstat(argv[i], &st) == 0) {
56524c0f738SXin LI 			if (S_ISDIR(st.st_mode)) {
56624c0f738SXin LI 				++dcount;
56724c0f738SXin LI 				dname = argv[i];    /* only used if 1 dir */
56824c0f738SXin LI 			} else {
56924c0f738SXin LI 				++fcount;
57024c0f738SXin LI 			}
57124c0f738SXin LI 		}
57224c0f738SXin LI 	}
57324c0f738SXin LI 	first = 0;
57424c0f738SXin LI 	while (first != 'n' && first != 'N' && first != 'y' && first != 'Y') {
57524c0f738SXin LI 		if (dcount && rflag) {
57624c0f738SXin LI 			fprintf(stderr, "recursively remove");
57724c0f738SXin LI 			if (dcount == 1)
57824c0f738SXin LI 				fprintf(stderr, " %s", dname);
57924c0f738SXin LI 			else
58024c0f738SXin LI 				fprintf(stderr, " %d dirs", dcount);
58124c0f738SXin LI 			if (fcount == 1)
58224c0f738SXin LI 				fprintf(stderr, " and 1 file");
58324c0f738SXin LI 			else if (fcount > 1)
58424c0f738SXin LI 				fprintf(stderr, " and %d files", fcount);
58524c0f738SXin LI 		} else if (dcount + fcount > 3) {
58624c0f738SXin LI 			fprintf(stderr, "remove %d files", dcount + fcount);
58724c0f738SXin LI 		} else {
58824c0f738SXin LI 			return(1);
58924c0f738SXin LI 		}
59024c0f738SXin LI 		fprintf(stderr, "? ");
59124c0f738SXin LI 		fflush(stderr);
59224c0f738SXin LI 
59324c0f738SXin LI 		first = ch = getchar();
59424c0f738SXin LI 		while (ch != '\n' && ch != EOF)
59524c0f738SXin LI 			ch = getchar();
59624c0f738SXin LI 		if (ch == EOF)
59724c0f738SXin LI 			break;
59824c0f738SXin LI 	}
59924c0f738SXin LI 	return (first == 'y' || first == 'Y');
60024c0f738SXin LI }
60124c0f738SXin LI 
60267a3d3a8SPoul-Henning Kamp #define ISDOT(a)	((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
603*9d6d3f96SEitan Adler static void
60446251ddeSWarner Losh checkdot(char **argv)
6054b88c807SRodney W. Grimes {
6064b88c807SRodney W. Grimes 	char *p, **save, **t;
6074b88c807SRodney W. Grimes 	int complained;
6084b88c807SRodney W. Grimes 
6094b88c807SRodney W. Grimes 	complained = 0;
6104b88c807SRodney W. Grimes 	for (t = argv; *t;) {
6114b88c807SRodney W. Grimes 		if ((p = strrchr(*t, '/')) != NULL)
6124b88c807SRodney W. Grimes 			++p;
6134b88c807SRodney W. Grimes 		else
6144b88c807SRodney W. Grimes 			p = *t;
6154b88c807SRodney W. Grimes 		if (ISDOT(p)) {
6164b88c807SRodney W. Grimes 			if (!complained++)
6174b88c807SRodney W. Grimes 				warnx("\".\" and \"..\" may not be removed");
6184b88c807SRodney W. Grimes 			eval = 1;
6191f64b5c9SSteve Price 			for (save = t; (t[0] = t[1]) != NULL; ++t)
6201f64b5c9SSteve Price 				continue;
6214b88c807SRodney W. Grimes 			t = save;
6224b88c807SRodney W. Grimes 		} else
6234b88c807SRodney W. Grimes 			++t;
6244b88c807SRodney W. Grimes 	}
6254b88c807SRodney W. Grimes }
6264b88c807SRodney W. Grimes 
627*9d6d3f96SEitan Adler static void
62846251ddeSWarner Losh usage(void)
6294b88c807SRodney W. Grimes {
6300efa2040SMichael Haro 
631d71e172aSSheldon Hearn 	(void)fprintf(stderr, "%s\n%s\n",
632d4319e74SEitan Adler 	    "usage: rm [-f | -i] [-dIPRrvWx] file ...",
633d71e172aSSheldon Hearn 	    "       unlink file");
634bfbdd545SMichael Haro 	exit(EX_USAGE);
6354b88c807SRodney W. Grimes }
636b5260db6SWarner Losh 
637b5260db6SWarner Losh static void
638b5260db6SWarner Losh siginfo(int sig __unused)
639b5260db6SWarner Losh {
640b5260db6SWarner Losh 
641b5260db6SWarner Losh 	info = 1;
642b5260db6SWarner Losh }
643