xref: /freebsd/bin/rm/rm.c (revision 68ef5f71b09d46f3815c7dd4b70df4fbda89b9fc)
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>
544b88c807SRodney W. Grimes #include <stdio.h>
554b88c807SRodney W. Grimes #include <stdlib.h>
564b88c807SRodney W. Grimes #include <string.h>
57bfbdd545SMichael Haro #include <sysexits.h>
584b88c807SRodney W. Grimes #include <unistd.h>
594b88c807SRodney W. Grimes 
60777d1f82SMichael Haro int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
616b419813SAndrey A. Chernov uid_t uid;
624b88c807SRodney W. Grimes 
6346251ddeSWarner Losh int	check(char *, char *, struct stat *);
6446251ddeSWarner Losh void	checkdot(char **);
6568ef5f71SDag-Erling Smørgrav void	checkslash(char **);
6646251ddeSWarner Losh void	rm_file(char **);
67f3761deeSGuido van Rooij int	rm_overwrite(char *, struct stat *);
6846251ddeSWarner Losh void	rm_tree(char **);
6946251ddeSWarner Losh void	usage(void);
704b88c807SRodney W. Grimes 
714b88c807SRodney W. Grimes /*
724b88c807SRodney W. Grimes  * rm --
734b88c807SRodney W. Grimes  *	This rm is different from historic rm's, but is expected to match
744b88c807SRodney W. Grimes  *	POSIX 1003.2 behavior.	The most visible difference is that -f
754b88c807SRodney W. Grimes  *	has two specific effects now, ignore non-existent files and force
764b88c807SRodney W. Grimes  *	file removal.
774b88c807SRodney W. Grimes  */
784b88c807SRodney W. Grimes int
7946251ddeSWarner Losh main(int argc, char *argv[])
804b88c807SRodney W. Grimes {
814b88c807SRodney W. Grimes 	int ch, rflag;
82d71e172aSSheldon Hearn 	char *p;
83d71e172aSSheldon Hearn 
84d71e172aSSheldon Hearn 	/*
85d71e172aSSheldon Hearn 	 * Test for the special case where the utility is called as
86d71e172aSSheldon Hearn 	 * "unlink", for which the functionality provided is greatly
87d71e172aSSheldon Hearn 	 * simplified.
88d71e172aSSheldon Hearn 	 */
89d71e172aSSheldon Hearn 	if ((p = rindex(argv[0], '/')) == NULL)
90d71e172aSSheldon Hearn 		p = argv[0];
91d71e172aSSheldon Hearn 	else
92d71e172aSSheldon Hearn 		++p;
93d71e172aSSheldon Hearn 	if (strcmp(p, "unlink") == 0) {
94e9393a92STim J. Robbins 		while (getopt(argc, argv, "") != -1)
95d71e172aSSheldon Hearn 			usage();
96e9393a92STim J. Robbins 		argc -= optind;
97e9393a92STim J. Robbins 		argv += optind;
9890833c99STim J. Robbins 		if (argc != 1)
99e9393a92STim J. Robbins 			usage();
100e9393a92STim J. Robbins 		rm_file(&argv[0]);
101e9393a92STim J. Robbins 		exit(eval);
102d71e172aSSheldon Hearn 	}
1034b88c807SRodney W. Grimes 
1041f64b5c9SSteve Price 	Pflag = rflag = 0;
105777d1f82SMichael Haro 	while ((ch = getopt(argc, argv, "dfiPRrvW")) != -1)
1064b88c807SRodney W. Grimes 		switch(ch) {
1074b88c807SRodney W. Grimes 		case 'd':
1084b88c807SRodney W. Grimes 			dflag = 1;
1094b88c807SRodney W. Grimes 			break;
1104b88c807SRodney W. Grimes 		case 'f':
1114b88c807SRodney W. Grimes 			fflag = 1;
1124b88c807SRodney W. Grimes 			iflag = 0;
1134b88c807SRodney W. Grimes 			break;
1144b88c807SRodney W. Grimes 		case 'i':
1154b88c807SRodney W. Grimes 			fflag = 0;
1164b88c807SRodney W. Grimes 			iflag = 1;
1174b88c807SRodney W. Grimes 			break;
1184b88c807SRodney W. Grimes 		case 'P':
1194b88c807SRodney W. Grimes 			Pflag = 1;
1204b88c807SRodney W. Grimes 			break;
1214b88c807SRodney W. Grimes 		case 'R':
1224b88c807SRodney W. Grimes 		case 'r':			/* Compatibility. */
1234b88c807SRodney W. Grimes 			rflag = 1;
1244b88c807SRodney W. Grimes 			break;
125bfbdd545SMichael Haro 		case 'v':
126bfbdd545SMichael Haro 			vflag = 1;
127bfbdd545SMichael Haro 			break;
128777d1f82SMichael Haro 		case 'W':
129777d1f82SMichael Haro 			Wflag = 1;
130777d1f82SMichael Haro 			break;
1314b88c807SRodney W. Grimes 		default:
1324b88c807SRodney W. Grimes 			usage();
1334b88c807SRodney W. Grimes 		}
1344b88c807SRodney W. Grimes 	argc -= optind;
1354b88c807SRodney W. Grimes 	argv += optind;
1364b88c807SRodney W. Grimes 
1370e8f2d6cSJordan K. Hubbard 	if (argc < 1) {
1380e8f2d6cSJordan K. Hubbard 		if (fflag)
139f3761deeSGuido van Rooij 			return (0);
1404b88c807SRodney W. Grimes 		usage();
1410e8f2d6cSJordan K. Hubbard 	}
1424b88c807SRodney W. Grimes 
1434b88c807SRodney W. Grimes 	checkdot(argv);
14468ef5f71SDag-Erling Smørgrav 	checkslash(argv);
1456b419813SAndrey A. Chernov 	uid = geteuid();
1464b88c807SRodney W. Grimes 
1471f64b5c9SSteve Price 	if (*argv) {
1481f64b5c9SSteve Price 		stdin_ok = isatty(STDIN_FILENO);
1491f64b5c9SSteve Price 
1504b88c807SRodney W. Grimes 		if (rflag)
1514b88c807SRodney W. Grimes 			rm_tree(argv);
1524b88c807SRodney W. Grimes 		else
1534b88c807SRodney W. Grimes 			rm_file(argv);
1541f64b5c9SSteve Price 	}
1551f64b5c9SSteve Price 
1564b88c807SRodney W. Grimes 	exit (eval);
1574b88c807SRodney W. Grimes }
1584b88c807SRodney W. Grimes 
1594b88c807SRodney W. Grimes void
16046251ddeSWarner Losh rm_tree(char **argv)
1614b88c807SRodney W. Grimes {
1624b88c807SRodney W. Grimes 	FTS *fts;
1634b88c807SRodney W. Grimes 	FTSENT *p;
1644b88c807SRodney W. Grimes 	int needstat;
1651f64b5c9SSteve Price 	int flags;
1666b419813SAndrey A. Chernov 	int rval;
1674b88c807SRodney W. Grimes 
1684b88c807SRodney W. Grimes 	/*
1694b88c807SRodney W. Grimes 	 * Remove a file hierarchy.  If forcing removal (-f), or interactive
1704b88c807SRodney W. Grimes 	 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
1714b88c807SRodney W. Grimes 	 */
1721f64b5c9SSteve Price 	needstat = !uid || (!fflag && !iflag && stdin_ok);
1734b88c807SRodney W. Grimes 
1744b88c807SRodney W. Grimes 	/*
1754b88c807SRodney W. Grimes 	 * If the -i option is specified, the user can skip on the pre-order
1764b88c807SRodney W. Grimes 	 * visit.  The fts_number field flags skipped directories.
1774b88c807SRodney W. Grimes 	 */
1784b88c807SRodney W. Grimes #define	SKIPPED	1
1794b88c807SRodney W. Grimes 
1804a086b52SBruce Evans 	flags = FTS_PHYSICAL;
1811f64b5c9SSteve Price 	if (!needstat)
1821f64b5c9SSteve Price 		flags |= FTS_NOSTAT;
1831f64b5c9SSteve Price 	if (Wflag)
1841f64b5c9SSteve Price 		flags |= FTS_WHITEOUT;
185754e501cSRuslan Ermilov 	if (!(fts = fts_open(argv, flags, NULL)))
1865ad9e45fSMatthew Dillon 		err(1, "fts_open");
1874b88c807SRodney W. Grimes 	while ((p = fts_read(fts)) != NULL) {
1884b88c807SRodney W. Grimes 		switch (p->fts_info) {
1894b88c807SRodney W. Grimes 		case FTS_DNR:
1904b88c807SRodney W. Grimes 			if (!fflag || p->fts_errno != ENOENT) {
1914b88c807SRodney W. Grimes 				warnx("%s: %s",
1924b88c807SRodney W. Grimes 				    p->fts_path, strerror(p->fts_errno));
1934b88c807SRodney W. Grimes 				eval = 1;
1944b88c807SRodney W. Grimes 			}
1954b88c807SRodney W. Grimes 			continue;
1964b88c807SRodney W. Grimes 		case FTS_ERR:
1974b88c807SRodney W. Grimes 			errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
1984b88c807SRodney W. Grimes 		case FTS_NS:
1994b88c807SRodney W. Grimes 			/*
200b800f53dSJun Kuriyama 			 * Assume that since fts_read() couldn't stat the
201b800f53dSJun Kuriyama 			 * file, it can't be unlinked.
2024b88c807SRodney W. Grimes 			 */
2034b88c807SRodney W. Grimes 			if (!needstat)
2044b88c807SRodney W. Grimes 				break;
2054b88c807SRodney W. Grimes 			if (!fflag || p->fts_errno != ENOENT) {
2064b88c807SRodney W. Grimes 				warnx("%s: %s",
2074b88c807SRodney W. Grimes 				    p->fts_path, strerror(p->fts_errno));
2084b88c807SRodney W. Grimes 				eval = 1;
2094b88c807SRodney W. Grimes 			}
2104b88c807SRodney W. Grimes 			continue;
2114b88c807SRodney W. Grimes 		case FTS_D:
2124b88c807SRodney W. Grimes 			/* Pre-order: give user chance to skip. */
2131f64b5c9SSteve Price 			if (!fflag && !check(p->fts_path, p->fts_accpath,
2144b88c807SRodney W. Grimes 			    p->fts_statp)) {
2154b88c807SRodney W. Grimes 				(void)fts_set(fts, p, FTS_SKIP);
2164b88c807SRodney W. Grimes 				p->fts_number = SKIPPED;
2174b88c807SRodney W. Grimes 			}
2186b419813SAndrey A. Chernov 			else if (!uid &&
2196b419813SAndrey A. Chernov 				 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
2206b419813SAndrey A. Chernov 				 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
2216b419813SAndrey A. Chernov 				 chflags(p->fts_accpath,
2226b419813SAndrey A. Chernov 					 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
2236b419813SAndrey A. Chernov 				goto err;
2244b88c807SRodney W. Grimes 			continue;
2254b88c807SRodney W. Grimes 		case FTS_DP:
2264b88c807SRodney W. Grimes 			/* Post-order: see if user skipped. */
2274b88c807SRodney W. Grimes 			if (p->fts_number == SKIPPED)
2284b88c807SRodney W. Grimes 				continue;
2294b88c807SRodney W. Grimes 			break;
2301f64b5c9SSteve Price 		default:
2314b88c807SRodney W. Grimes 			if (!fflag &&
2324b88c807SRodney W. Grimes 			    !check(p->fts_path, p->fts_accpath, p->fts_statp))
2334b88c807SRodney W. Grimes 				continue;
2341f64b5c9SSteve Price 		}
2354b88c807SRodney W. Grimes 
2366b419813SAndrey A. Chernov 		rval = 0;
2376b419813SAndrey A. Chernov 		if (!uid &&
2386b419813SAndrey A. Chernov 		    (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
2396b419813SAndrey A. Chernov 		    !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
2406b419813SAndrey A. Chernov 			rval = chflags(p->fts_accpath,
2416b419813SAndrey A. Chernov 				       p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
2420efa2040SMichael Haro 		if (rval == 0) {
2434b88c807SRodney W. Grimes 			/*
2444b88c807SRodney W. Grimes 			 * If we can't read or search the directory, may still be
2454b88c807SRodney W. Grimes 			 * able to remove it.  Don't print out the un{read,search}able
2464b88c807SRodney W. Grimes 			 * message unless the remove fails.
2474b88c807SRodney W. Grimes 			 */
2481f64b5c9SSteve Price 			switch (p->fts_info) {
2491f64b5c9SSteve Price 			case FTS_DP:
2501f64b5c9SSteve Price 			case FTS_DNR:
2510efa2040SMichael Haro 				rval = rmdir(p->fts_accpath);
2520efa2040SMichael Haro 				if (rval == 0 || (fflag && errno == ENOENT)) {
2530efa2040SMichael Haro 					if (rval == 0 && vflag)
254777d1f82SMichael Haro 						(void)printf("%s\n",
255b6f80a8eSDavid E. O'Brien 						    p->fts_path);
2564b88c807SRodney W. Grimes 					continue;
257bfbdd545SMichael Haro 				}
2581f64b5c9SSteve Price 				break;
2591f64b5c9SSteve Price 
2601f64b5c9SSteve Price 			case FTS_W:
2610efa2040SMichael Haro 				rval = undelete(p->fts_accpath);
2620efa2040SMichael Haro 				if (rval == 0 && (fflag && errno == ENOENT)) {
2630efa2040SMichael Haro 					if (vflag)
264777d1f82SMichael Haro 						(void)printf("%s\n",
265b6f80a8eSDavid E. O'Brien 						    p->fts_path);
2664b88c807SRodney W. Grimes 					continue;
267bfbdd545SMichael Haro 				}
2681f64b5c9SSteve Price 				break;
2691f64b5c9SSteve Price 
270b800f53dSJun Kuriyama 			case FTS_NS:
271b800f53dSJun Kuriyama 				/*
272b800f53dSJun Kuriyama 				 * Assume that since fts_read() couldn't stat
273b800f53dSJun Kuriyama 				 * the file, it can't be unlinked.
274b800f53dSJun Kuriyama 				 */
275b800f53dSJun Kuriyama 				if (fflag)
276b800f53dSJun Kuriyama 					continue;
277b800f53dSJun Kuriyama 				/* FALLTHROUGH */
2781f64b5c9SSteve Price 			default:
2794b88c807SRodney W. Grimes 				if (Pflag)
280f3761deeSGuido van Rooij 					if (!rm_overwrite(p->fts_accpath, NULL))
281f3761deeSGuido van Rooij 						continue;
2820efa2040SMichael Haro 				rval = unlink(p->fts_accpath);
2830efa2040SMichael Haro 				if (rval == 0 || (fflag && errno == ENOENT)) {
2840efa2040SMichael Haro 					if (rval == 0 && vflag)
285777d1f82SMichael Haro 						(void)printf("%s\n",
286b6f80a8eSDavid E. O'Brien 						    p->fts_path);
2874b88c807SRodney W. Grimes 					continue;
2884b88c807SRodney W. Grimes 				}
2896b419813SAndrey A. Chernov 			}
290bfbdd545SMichael Haro 		}
2916b419813SAndrey A. Chernov err:
2924b88c807SRodney W. Grimes 		warn("%s", p->fts_path);
2934b88c807SRodney W. Grimes 		eval = 1;
2944b88c807SRodney W. Grimes 	}
2954b88c807SRodney W. Grimes 	if (errno)
2964b88c807SRodney W. Grimes 		err(1, "fts_read");
2974b88c807SRodney W. Grimes }
2984b88c807SRodney W. Grimes 
2994b88c807SRodney W. Grimes void
30046251ddeSWarner Losh rm_file(char **argv)
3014b88c807SRodney W. Grimes {
3024b88c807SRodney W. Grimes 	struct stat sb;
3031f64b5c9SSteve Price 	int rval;
3044b88c807SRodney W. Grimes 	char *f;
3054b88c807SRodney W. Grimes 
3064b88c807SRodney W. Grimes 	/*
3074b88c807SRodney W. Grimes 	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
3084b88c807SRodney W. Grimes 	 * to remove a directory is an error, so must always stat the file.
3094b88c807SRodney W. Grimes 	 */
3104b88c807SRodney W. Grimes 	while ((f = *argv++) != NULL) {
3114b88c807SRodney W. Grimes 		/* Assume if can't stat the file, can't unlink it. */
3124b88c807SRodney W. Grimes 		if (lstat(f, &sb)) {
3131f64b5c9SSteve Price 			if (Wflag) {
3141f64b5c9SSteve Price 				sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
3151f64b5c9SSteve Price 			} else {
3164b88c807SRodney W. Grimes 				if (!fflag || errno != ENOENT) {
3174b88c807SRodney W. Grimes 					warn("%s", f);
3184b88c807SRodney W. Grimes 					eval = 1;
3194b88c807SRodney W. Grimes 				}
3204b88c807SRodney W. Grimes 				continue;
3214b88c807SRodney W. Grimes 			}
3221f64b5c9SSteve Price 		} else if (Wflag) {
3231f64b5c9SSteve Price 			warnx("%s: %s", f, strerror(EEXIST));
3241f64b5c9SSteve Price 			eval = 1;
3251f64b5c9SSteve Price 			continue;
3261f64b5c9SSteve Price 		}
3271f64b5c9SSteve Price 
3281f64b5c9SSteve Price 		if (S_ISDIR(sb.st_mode) && !dflag) {
3294b88c807SRodney W. Grimes 			warnx("%s: is a directory", f);
3304b88c807SRodney W. Grimes 			eval = 1;
3314b88c807SRodney W. Grimes 			continue;
3324b88c807SRodney W. Grimes 		}
3331f64b5c9SSteve Price 		if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
3344b88c807SRodney W. Grimes 			continue;
3356b419813SAndrey A. Chernov 		rval = 0;
3366b419813SAndrey A. Chernov 		if (!uid &&
3376b419813SAndrey A. Chernov 		    (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
3386b419813SAndrey A. Chernov 		    !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
3396b419813SAndrey A. Chernov 			rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
3400efa2040SMichael Haro 		if (rval == 0) {
3411f64b5c9SSteve Price 			if (S_ISWHT(sb.st_mode))
3421f64b5c9SSteve Price 				rval = undelete(f);
3431f64b5c9SSteve Price 			else if (S_ISDIR(sb.st_mode))
3444b88c807SRodney W. Grimes 				rval = rmdir(f);
3454b88c807SRodney W. Grimes 			else {
3464b88c807SRodney W. Grimes 				if (Pflag)
347f3761deeSGuido van Rooij 					if (!rm_overwrite(f, &sb))
348f3761deeSGuido van Rooij 						continue;
3494b88c807SRodney W. Grimes 				rval = unlink(f);
3504b88c807SRodney W. Grimes 			}
3516b419813SAndrey A. Chernov 		}
3524b88c807SRodney W. Grimes 		if (rval && (!fflag || errno != ENOENT)) {
3534b88c807SRodney W. Grimes 			warn("%s", f);
3544b88c807SRodney W. Grimes 			eval = 1;
3554b88c807SRodney W. Grimes 		}
3560efa2040SMichael Haro 		if (vflag && rval == 0)
357bfbdd545SMichael Haro 			(void)printf("%s\n", f);
3584b88c807SRodney W. Grimes 	}
3594b88c807SRodney W. Grimes }
3604b88c807SRodney W. Grimes 
3614b88c807SRodney W. Grimes /*
3624b88c807SRodney W. Grimes  * rm_overwrite --
3634b88c807SRodney W. Grimes  *	Overwrite the file 3 times with varying bit patterns.
3644b88c807SRodney W. Grimes  *
3654b88c807SRodney W. Grimes  * XXX
3664b88c807SRodney W. Grimes  * This is a cheap way to *really* delete files.  Note that only regular
3674b88c807SRodney W. Grimes  * files are deleted, directories (and therefore names) will remain.
3684b88c807SRodney W. Grimes  * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
3694b88c807SRodney W. Grimes  * System V file system).  In a logging file system, you'll have to have
3704b88c807SRodney W. Grimes  * kernel support.
3714b88c807SRodney W. Grimes  */
372f3761deeSGuido van Rooij int
37346251ddeSWarner Losh rm_overwrite(char *file, struct stat *sbp)
3744b88c807SRodney W. Grimes {
3754b88c807SRodney W. Grimes 	struct stat sb;
376f80db2b8SKris Kennaway 	struct statfs fsb;
3774b88c807SRodney W. Grimes 	off_t len;
378f80db2b8SKris Kennaway 	int bsize, fd, wlen;
379f80db2b8SKris Kennaway 	char *buf = NULL;
3804b88c807SRodney W. Grimes 
3814b88c807SRodney W. Grimes 	fd = -1;
3824b88c807SRodney W. Grimes 	if (sbp == NULL) {
3834b88c807SRodney W. Grimes 		if (lstat(file, &sb))
3844b88c807SRodney W. Grimes 			goto err;
3854b88c807SRodney W. Grimes 		sbp = &sb;
3864b88c807SRodney W. Grimes 	}
3874b88c807SRodney W. Grimes 	if (!S_ISREG(sbp->st_mode))
388f3761deeSGuido van Rooij 		return (1);
3894b88c807SRodney W. Grimes 	if ((fd = open(file, O_WRONLY, 0)) == -1)
3904b88c807SRodney W. Grimes 		goto err;
391f80db2b8SKris Kennaway 	if (fstatfs(fd, &fsb) == -1)
392f80db2b8SKris Kennaway 		goto err;
393f80db2b8SKris Kennaway 	bsize = MAX(fsb.f_iosize, 1024);
394f80db2b8SKris Kennaway 	if ((buf = malloc(bsize)) == NULL)
3958f1f4338SBruce Evans 		err(1, "%s: malloc", file);
3964b88c807SRodney W. Grimes 
3974b88c807SRodney W. Grimes #define	PASS(byte) {							\
398f80db2b8SKris Kennaway 	memset(buf, byte, bsize);					\
3994b88c807SRodney W. Grimes 	for (len = sbp->st_size; len > 0; len -= wlen) {		\
400f80db2b8SKris Kennaway 		wlen = len < bsize ? len : bsize;			\
4014b88c807SRodney W. Grimes 		if (write(fd, buf, wlen) != wlen)			\
4024b88c807SRodney W. Grimes 			goto err;					\
4034b88c807SRodney W. Grimes 	}								\
4044b88c807SRodney W. Grimes }
4054b88c807SRodney W. Grimes 	PASS(0xff);
4064b88c807SRodney W. Grimes 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
4074b88c807SRodney W. Grimes 		goto err;
4084b88c807SRodney W. Grimes 	PASS(0x00);
4094b88c807SRodney W. Grimes 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
4104b88c807SRodney W. Grimes 		goto err;
4114b88c807SRodney W. Grimes 	PASS(0xff);
412f80db2b8SKris Kennaway 	if (!fsync(fd) && !close(fd)) {
413f80db2b8SKris Kennaway 		free(buf);
414f3761deeSGuido van Rooij 		return (1);
415f80db2b8SKris Kennaway 	}
4164b88c807SRodney W. Grimes 
4174b88c807SRodney W. Grimes err:	eval = 1;
418f80db2b8SKris Kennaway 	if (buf)
419f80db2b8SKris Kennaway 		free(buf);
4208f1f4338SBruce Evans 	if (fd != -1)
4218f1f4338SBruce Evans 		close(fd);
4224b88c807SRodney W. Grimes 	warn("%s", file);
423f3761deeSGuido van Rooij 	return (0);
4244b88c807SRodney W. Grimes }
4254b88c807SRodney W. Grimes 
4264b88c807SRodney W. Grimes 
4274b88c807SRodney W. Grimes int
42846251ddeSWarner Losh check(char *path, char *name, struct stat *sp)
4294b88c807SRodney W. Grimes {
4304b88c807SRodney W. Grimes 	int ch, first;
431141d77b8SJosef Karthauser 	char modep[15], *flagsp;
4324b88c807SRodney W. Grimes 
4334b88c807SRodney W. Grimes 	/* Check -i first. */
4344b88c807SRodney W. Grimes 	if (iflag)
4354b88c807SRodney W. Grimes 		(void)fprintf(stderr, "remove %s? ", path);
4364b88c807SRodney W. Grimes 	else {
4374b88c807SRodney W. Grimes 		/*
4384b88c807SRodney W. Grimes 		 * If it's not a symbolic link and it's unwritable and we're
4394b88c807SRodney W. Grimes 		 * talking to a terminal, ask.	Symbolic links are excluded
4404b88c807SRodney W. Grimes 		 * because their permissions are meaningless.  Check stdin_ok
4414b88c807SRodney W. Grimes 		 * first because we may not have stat'ed the file.
442f3761deeSGuido van Rooij 		 * Also skip this check if the -P option was specified because
443f3761deeSGuido van Rooij 		 * we will not be able to overwrite file contents and will
444f3761deeSGuido van Rooij 		 * barf later.
4454b88c807SRodney W. Grimes 		 */
446f3761deeSGuido van Rooij 		if (!stdin_ok || S_ISLNK(sp->st_mode) || Pflag ||
4471f64b5c9SSteve Price 		    (!access(name, W_OK) &&
4486b419813SAndrey A. Chernov 		    !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
4491f64b5c9SSteve Price 		    (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)))
4504b88c807SRodney W. Grimes 			return (1);
4514b88c807SRodney W. Grimes 		strmode(sp->st_mode, modep);
452141d77b8SJosef Karthauser 		if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
4535ad9e45fSMatthew Dillon 			err(1, "fflagstostr");
454141d77b8SJosef Karthauser 		(void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
4554b88c807SRodney W. Grimes 		    modep + 1, modep[9] == ' ' ? "" : " ",
4564b88c807SRodney W. Grimes 		    user_from_uid(sp->st_uid, 0),
4576b419813SAndrey A. Chernov 		    group_from_gid(sp->st_gid, 0),
458141d77b8SJosef Karthauser 		    *flagsp ? flagsp : "", *flagsp ? " " : "",
4596b419813SAndrey A. Chernov 		    path);
460141d77b8SJosef Karthauser 		free(flagsp);
4614b88c807SRodney W. Grimes 	}
4624b88c807SRodney W. Grimes 	(void)fflush(stderr);
4634b88c807SRodney W. Grimes 
4644b88c807SRodney W. Grimes 	first = ch = getchar();
4654b88c807SRodney W. Grimes 	while (ch != '\n' && ch != EOF)
4664b88c807SRodney W. Grimes 		ch = getchar();
467b0205affSWolfram Schneider 	return (first == 'y' || first == 'Y');
4684b88c807SRodney W. Grimes }
4694b88c807SRodney W. Grimes 
47068ef5f71SDag-Erling Smørgrav #define ISSLASH(a)	((a)[0] == '/' && (a)[1] == '\0')
47168ef5f71SDag-Erling Smørgrav void
47268ef5f71SDag-Erling Smørgrav checkslash(char **argv)
47368ef5f71SDag-Erling Smørgrav {
47468ef5f71SDag-Erling Smørgrav 	char **t, **u;
47568ef5f71SDag-Erling Smørgrav 	int complained;
47668ef5f71SDag-Erling Smørgrav 
47768ef5f71SDag-Erling Smørgrav 	complained = 0;
47868ef5f71SDag-Erling Smørgrav 	for (t = argv; *t;) {
47968ef5f71SDag-Erling Smørgrav 		if (ISSLASH(*t)) {
48068ef5f71SDag-Erling Smørgrav 			if (!complained++)
48168ef5f71SDag-Erling Smørgrav 				warnx("\"/\" may not be removed");
48268ef5f71SDag-Erling Smørgrav 			eval = 1;
48368ef5f71SDag-Erling Smørgrav 			for (u = t; u[0] != NULL; ++u)
48468ef5f71SDag-Erling Smørgrav 				u[0] = u[1];
48568ef5f71SDag-Erling Smørgrav 		} else {
48668ef5f71SDag-Erling Smørgrav 			++t;
48768ef5f71SDag-Erling Smørgrav 		}
48868ef5f71SDag-Erling Smørgrav 	}
48968ef5f71SDag-Erling Smørgrav }
49068ef5f71SDag-Erling Smørgrav 
49167a3d3a8SPoul-Henning Kamp #define ISDOT(a)	((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
4924b88c807SRodney W. Grimes void
49346251ddeSWarner Losh checkdot(char **argv)
4944b88c807SRodney W. Grimes {
4954b88c807SRodney W. Grimes 	char *p, **save, **t;
4964b88c807SRodney W. Grimes 	int complained;
4974b88c807SRodney W. Grimes 
4984b88c807SRodney W. Grimes 	complained = 0;
4994b88c807SRodney W. Grimes 	for (t = argv; *t;) {
5004b88c807SRodney W. Grimes 		if ((p = strrchr(*t, '/')) != NULL)
5014b88c807SRodney W. Grimes 			++p;
5024b88c807SRodney W. Grimes 		else
5034b88c807SRodney W. Grimes 			p = *t;
5044b88c807SRodney W. Grimes 		if (ISDOT(p)) {
5054b88c807SRodney W. Grimes 			if (!complained++)
5064b88c807SRodney W. Grimes 				warnx("\".\" and \"..\" may not be removed");
5074b88c807SRodney W. Grimes 			eval = 1;
5081f64b5c9SSteve Price 			for (save = t; (t[0] = t[1]) != NULL; ++t)
5091f64b5c9SSteve Price 				continue;
5104b88c807SRodney W. Grimes 			t = save;
5114b88c807SRodney W. Grimes 		} else
5124b88c807SRodney W. Grimes 			++t;
5134b88c807SRodney W. Grimes 	}
5144b88c807SRodney W. Grimes }
5154b88c807SRodney W. Grimes 
5164b88c807SRodney W. Grimes void
51746251ddeSWarner Losh usage(void)
5184b88c807SRodney W. Grimes {
5190efa2040SMichael Haro 
520d71e172aSSheldon Hearn 	(void)fprintf(stderr, "%s\n%s\n",
521d71e172aSSheldon Hearn 	    "usage: rm [-f | -i] [-dPRrvW] file ...",
522d71e172aSSheldon Hearn 	    "       unlink file");
523bfbdd545SMichael Haro 	exit(EX_USAGE);
5244b88c807SRodney W. Grimes }
525