xref: /freebsd/bin/rm/rm.c (revision 1f64b5c98e27bd980f07679712f9db3f5c04845c)
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  * 3. All advertising materials mentioning features or use of this software
144b88c807SRodney W. Grimes  *    must display the following acknowledgement:
154b88c807SRodney W. Grimes  *	This product includes software developed by the University of
164b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
174b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
184b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
194b88c807SRodney W. Grimes  *    without specific prior written permission.
204b88c807SRodney W. Grimes  *
214b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314b88c807SRodney W. Grimes  * SUCH DAMAGE.
3289730b29SDavid Greenman  *
331f64b5c9SSteve Price  *	$Id: rm.c,v 1.11 1996/03/07 23:26:59 wosch Exp $
344b88c807SRodney W. Grimes  */
354b88c807SRodney W. Grimes 
364b88c807SRodney W. Grimes #ifndef lint
371f64b5c9SSteve Price static char const copyright[] =
384b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\
394b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
404b88c807SRodney W. Grimes #endif /* not lint */
414b88c807SRodney W. Grimes 
424b88c807SRodney W. Grimes #ifndef lint
431f64b5c9SSteve Price static char const sccsid[] = "@(#)rm.c	8.5 (Berkeley) 4/18/94";
444b88c807SRodney W. Grimes #endif /* not lint */
454b88c807SRodney W. Grimes 
464b88c807SRodney W. Grimes #include <sys/types.h>
474b88c807SRodney W. Grimes #include <sys/stat.h>
484b88c807SRodney W. Grimes 
494b88c807SRodney W. Grimes #include <err.h>
504b88c807SRodney W. Grimes #include <errno.h>
514b88c807SRodney W. Grimes #include <fcntl.h>
524b88c807SRodney W. Grimes #include <fts.h>
534b88c807SRodney W. Grimes #include <stdio.h>
544b88c807SRodney W. Grimes #include <stdlib.h>
554b88c807SRodney W. Grimes #include <string.h>
564b88c807SRodney W. Grimes #include <unistd.h>
574b88c807SRodney W. Grimes 
581f64b5c9SSteve Price /*
591f64b5c9SSteve Price  * XXX Until we get kernel support for the undelete(2) system call,
601f64b5c9SSteve Price  * this define *must* remain in place.
611f64b5c9SSteve Price  */
621f64b5c9SSteve Price #define BSD4_4_LITE
631f64b5c9SSteve Price 
646b419813SAndrey A. Chernov extern char *flags_to_string __P((u_long, char *));
656b419813SAndrey A. Chernov 
661f64b5c9SSteve Price #ifdef BSD4_4_LITE
674b88c807SRodney W. Grimes int dflag, eval, fflag, iflag, Pflag, stdin_ok;
681f64b5c9SSteve Price #else
691f64b5c9SSteve Price int dflag, eval, fflag, iflag, Pflag, Wflag, stdin_ok;
701f64b5c9SSteve Price #endif
716b419813SAndrey A. Chernov uid_t uid;
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes int	check __P((char *, char *, struct stat *));
744b88c807SRodney W. Grimes void	checkdot __P((char **));
754b88c807SRodney W. Grimes void	rm_file __P((char **));
764b88c807SRodney W. Grimes void	rm_overwrite __P((char *, struct stat *));
774b88c807SRodney W. Grimes void	rm_tree __P((char **));
784b88c807SRodney W. Grimes void	usage __P((void));
794b88c807SRodney W. Grimes 
804b88c807SRodney W. Grimes /*
814b88c807SRodney W. Grimes  * rm --
824b88c807SRodney W. Grimes  *	This rm is different from historic rm's, but is expected to match
834b88c807SRodney W. Grimes  *	POSIX 1003.2 behavior.  The most visible difference is that -f
844b88c807SRodney W. Grimes  *	has two specific effects now, ignore non-existent files and force
854b88c807SRodney W. Grimes  * 	file removal.
864b88c807SRodney W. Grimes  */
874b88c807SRodney W. Grimes int
884b88c807SRodney W. Grimes main(argc, argv)
894b88c807SRodney W. Grimes 	int argc;
904b88c807SRodney W. Grimes 	char *argv[];
914b88c807SRodney W. Grimes {
924b88c807SRodney W. Grimes 	int ch, rflag;
934b88c807SRodney W. Grimes 
941f64b5c9SSteve Price 	Pflag = rflag = 0;
951f64b5c9SSteve Price #ifdef BSD4_4_LITE
96daf6f1ebSPaul Traina 	while ((ch = getopt(argc, argv, "dfiPRr")) != EOF)
971f64b5c9SSteve Price #else
981f64b5c9SSteve Price 	while ((ch = getopt(argc, argv, "dfiPRrW")) != EOF)
991f64b5c9SSteve Price #endif
1004b88c807SRodney W. Grimes 		switch(ch) {
1014b88c807SRodney W. Grimes 		case 'd':
1024b88c807SRodney W. Grimes 			dflag = 1;
1034b88c807SRodney W. Grimes 			break;
1044b88c807SRodney W. Grimes 		case 'f':
1054b88c807SRodney W. Grimes 			fflag = 1;
1064b88c807SRodney W. Grimes 			iflag = 0;
1074b88c807SRodney W. Grimes 			break;
1084b88c807SRodney W. Grimes 		case 'i':
1094b88c807SRodney W. Grimes 			fflag = 0;
1104b88c807SRodney W. Grimes 			iflag = 1;
1114b88c807SRodney W. Grimes 			break;
1124b88c807SRodney W. Grimes 		case 'P':
1134b88c807SRodney W. Grimes 			Pflag = 1;
1144b88c807SRodney W. Grimes 			break;
1154b88c807SRodney W. Grimes 		case 'R':
1164b88c807SRodney W. Grimes 		case 'r':			/* Compatibility. */
1174b88c807SRodney W. Grimes 			rflag = 1;
1184b88c807SRodney W. Grimes 			break;
1191f64b5c9SSteve Price #ifndef BSD4_4_LITE
1201f64b5c9SSteve Price 		case 'W':
1211f64b5c9SSteve Price 			Wflag = 1;
1221f64b5c9SSteve Price 			break;
1231f64b5c9SSteve Price #endif
1244b88c807SRodney W. Grimes 		default:
1254b88c807SRodney W. Grimes 			usage();
1264b88c807SRodney W. Grimes 		}
1274b88c807SRodney W. Grimes 	argc -= optind;
1284b88c807SRodney W. Grimes 	argv += optind;
1294b88c807SRodney W. Grimes 
1304b88c807SRodney W. Grimes 	if (argc < 1)
1314b88c807SRodney W. Grimes 		usage();
1324b88c807SRodney W. Grimes 
1334b88c807SRodney W. Grimes 	checkdot(argv);
1346b419813SAndrey A. Chernov 	uid = geteuid();
1354b88c807SRodney W. Grimes 
1361f64b5c9SSteve Price 	if (*argv) {
1371f64b5c9SSteve Price 		stdin_ok = isatty(STDIN_FILENO);
1381f64b5c9SSteve Price 
1394b88c807SRodney W. Grimes 		if (rflag)
1404b88c807SRodney W. Grimes 			rm_tree(argv);
1414b88c807SRodney W. Grimes 		else
1424b88c807SRodney W. Grimes 			rm_file(argv);
1431f64b5c9SSteve Price 	}
1441f64b5c9SSteve Price 
1454b88c807SRodney W. Grimes 	exit (eval);
1464b88c807SRodney W. Grimes }
1474b88c807SRodney W. Grimes 
1484b88c807SRodney W. Grimes void
1494b88c807SRodney W. Grimes rm_tree(argv)
1504b88c807SRodney W. Grimes 	char **argv;
1514b88c807SRodney W. Grimes {
1524b88c807SRodney W. Grimes 	FTS *fts;
1534b88c807SRodney W. Grimes 	FTSENT *p;
1544b88c807SRodney W. Grimes 	int needstat;
1551f64b5c9SSteve Price 	int flags;
1566b419813SAndrey A. Chernov 	int rval;
1574b88c807SRodney W. Grimes 
1584b88c807SRodney W. Grimes 	/*
1594b88c807SRodney W. Grimes 	 * Remove a file hierarchy.  If forcing removal (-f), or interactive
1604b88c807SRodney W. Grimes 	 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
1614b88c807SRodney W. Grimes 	 */
1621f64b5c9SSteve Price 	needstat = !uid || (!fflag && !iflag && stdin_ok);
1634b88c807SRodney W. Grimes 
1644b88c807SRodney W. Grimes 	/*
1654b88c807SRodney W. Grimes 	 * If the -i option is specified, the user can skip on the pre-order
1664b88c807SRodney W. Grimes 	 * visit.  The fts_number field flags skipped directories.
1674b88c807SRodney W. Grimes 	 */
1684b88c807SRodney W. Grimes #define	SKIPPED	1
1694b88c807SRodney W. Grimes 
1701f64b5c9SSteve Price 	flags = FTS_PHYSICAL | FTS_NOCHDIR;
1711f64b5c9SSteve Price 	if (!needstat)
1721f64b5c9SSteve Price 		flags |= FTS_NOSTAT;
1731f64b5c9SSteve Price #ifndef BSD4_4_LITE
1741f64b5c9SSteve Price 	if (Wflag)
1751f64b5c9SSteve Price 		flags |= FTS_WHITEOUT;
1761f64b5c9SSteve Price #endif
1771f64b5c9SSteve Price 	if (!(fts = fts_open(argv, flags, (int (*)())NULL)))
1784b88c807SRodney W. Grimes 		err(1, NULL);
1794b88c807SRodney W. Grimes 	while ((p = fts_read(fts)) != NULL) {
1804b88c807SRodney W. Grimes 		switch (p->fts_info) {
1814b88c807SRodney W. Grimes 		case FTS_DNR:
1824b88c807SRodney W. Grimes 			if (!fflag || p->fts_errno != ENOENT) {
1834b88c807SRodney W. Grimes 				warnx("%s: %s",
1844b88c807SRodney W. Grimes 				    p->fts_path, strerror(p->fts_errno));
1854b88c807SRodney W. Grimes 				eval = 1;
1864b88c807SRodney W. Grimes 			}
1874b88c807SRodney W. Grimes 			continue;
1884b88c807SRodney W. Grimes 		case FTS_ERR:
1894b88c807SRodney W. Grimes 			errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
1904b88c807SRodney W. Grimes 		case FTS_NS:
1914b88c807SRodney W. Grimes 			/*
1924b88c807SRodney W. Grimes 			 * FTS_NS: assume that if can't stat the file, it
1934b88c807SRodney W. Grimes 			 * can't be unlinked.
1944b88c807SRodney W. Grimes 			 */
1954b88c807SRodney W. Grimes 			if (!needstat)
1964b88c807SRodney W. Grimes 				break;
1974b88c807SRodney W. Grimes 			if (!fflag || p->fts_errno != ENOENT) {
1984b88c807SRodney W. Grimes 				warnx("%s: %s",
1994b88c807SRodney W. Grimes 				    p->fts_path, strerror(p->fts_errno));
2004b88c807SRodney W. Grimes 				eval = 1;
2014b88c807SRodney W. Grimes 			}
2024b88c807SRodney W. Grimes 			continue;
2034b88c807SRodney W. Grimes 		case FTS_D:
2044b88c807SRodney W. Grimes 			/* Pre-order: give user chance to skip. */
2051f64b5c9SSteve Price 			if (!fflag && !check(p->fts_path, p->fts_accpath,
2064b88c807SRodney W. Grimes 			    p->fts_statp)) {
2074b88c807SRodney W. Grimes 				(void)fts_set(fts, p, FTS_SKIP);
2084b88c807SRodney W. Grimes 				p->fts_number = SKIPPED;
2094b88c807SRodney W. Grimes 			}
2106b419813SAndrey A. Chernov 			else if (!uid &&
2116b419813SAndrey A. Chernov 				 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
2126b419813SAndrey A. Chernov 				 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
2136b419813SAndrey A. Chernov 				 chflags(p->fts_accpath,
2146b419813SAndrey A. Chernov 					 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
2156b419813SAndrey A. Chernov 				goto err;
2164b88c807SRodney W. Grimes 			continue;
2174b88c807SRodney W. Grimes 		case FTS_DP:
2184b88c807SRodney W. Grimes 			/* Post-order: see if user skipped. */
2194b88c807SRodney W. Grimes 			if (p->fts_number == SKIPPED)
2204b88c807SRodney W. Grimes 				continue;
2214b88c807SRodney W. Grimes 			break;
2221f64b5c9SSteve Price 		default:
2234b88c807SRodney W. Grimes 			if (!fflag &&
2244b88c807SRodney W. Grimes 			    !check(p->fts_path, p->fts_accpath, p->fts_statp))
2254b88c807SRodney W. Grimes 				continue;
2261f64b5c9SSteve Price 		}
2274b88c807SRodney W. Grimes 
2286b419813SAndrey A. Chernov 		rval = 0;
2296b419813SAndrey A. Chernov 		if (!uid &&
2306b419813SAndrey A. Chernov 		    (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
2316b419813SAndrey A. Chernov 		    !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
2326b419813SAndrey A. Chernov 			rval = chflags(p->fts_accpath,
2336b419813SAndrey A. Chernov 				       p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
2346b419813SAndrey A. Chernov 		if (!rval) {
2354b88c807SRodney W. Grimes 			/*
2364b88c807SRodney W. Grimes 			 * If we can't read or search the directory, may still be
2374b88c807SRodney W. Grimes 			 * able to remove it.  Don't print out the un{read,search}able
2384b88c807SRodney W. Grimes 			 * message unless the remove fails.
2394b88c807SRodney W. Grimes 			 */
2401f64b5c9SSteve Price 			switch (p->fts_info) {
2411f64b5c9SSteve Price 			case FTS_DP:
2421f64b5c9SSteve Price 			case FTS_DNR:
2431f64b5c9SSteve Price 				if (!rmdir(p->fts_accpath) || (fflag && errno == ENOENT))
2444b88c807SRodney W. Grimes 					continue;
2451f64b5c9SSteve Price 				break;
2461f64b5c9SSteve Price 
2471f64b5c9SSteve Price #ifndef BSD4_4_LITE
2481f64b5c9SSteve Price 			case FTS_W:
2491f64b5c9SSteve Price 				if (!undelete(p->fts_accpath) ||
2501f64b5c9SSteve Price 			    	fflag && errno == ENOENT)
2514b88c807SRodney W. Grimes 					continue;
2521f64b5c9SSteve Price 				break;
2531f64b5c9SSteve Price #endif
2541f64b5c9SSteve Price 
2551f64b5c9SSteve Price 			default:
2564b88c807SRodney W. Grimes 				if (Pflag)
2574b88c807SRodney W. Grimes 					rm_overwrite(p->fts_accpath, NULL);
25867a3d3a8SPoul-Henning Kamp 				if (!unlink(p->fts_accpath) || (fflag && errno == ENOENT))
2594b88c807SRodney W. Grimes 					continue;
2604b88c807SRodney W. Grimes 			}
2616b419813SAndrey A. Chernov 		}
2626b419813SAndrey A. Chernov err:
2634b88c807SRodney W. Grimes 		warn("%s", p->fts_path);
2644b88c807SRodney W. Grimes 		eval = 1;
2654b88c807SRodney W. Grimes 	}
2664b88c807SRodney W. Grimes 	if (errno)
2674b88c807SRodney W. Grimes 		err(1, "fts_read");
2684b88c807SRodney W. Grimes }
2694b88c807SRodney W. Grimes 
2704b88c807SRodney W. Grimes void
2714b88c807SRodney W. Grimes rm_file(argv)
2724b88c807SRodney W. Grimes 	char **argv;
2734b88c807SRodney W. Grimes {
2744b88c807SRodney W. Grimes 	struct stat sb;
2751f64b5c9SSteve Price 	int rval;
2764b88c807SRodney W. Grimes 	char *f;
2774b88c807SRodney W. Grimes 
2784b88c807SRodney W. Grimes 	/*
2794b88c807SRodney W. Grimes 	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
2804b88c807SRodney W. Grimes 	 * to remove a directory is an error, so must always stat the file.
2814b88c807SRodney W. Grimes 	 */
2824b88c807SRodney W. Grimes 	while ((f = *argv++) != NULL) {
2834b88c807SRodney W. Grimes 		/* Assume if can't stat the file, can't unlink it. */
2844b88c807SRodney W. Grimes 		if (lstat(f, &sb)) {
2851f64b5c9SSteve Price #ifdef BSD4_4_LITE
2861f64b5c9SSteve Price 			if (!fflag || errno != ENOENT) {
2871f64b5c9SSteve Price 				warn("%s", f);
2881f64b5c9SSteve Price 				eval = 1;
2891f64b5c9SSteve Price  			}
2901f64b5c9SSteve Price #else
2911f64b5c9SSteve Price 			if (Wflag) {
2921f64b5c9SSteve Price 				sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
2931f64b5c9SSteve Price 			} else {
2944b88c807SRodney W. Grimes 				if (!fflag || errno != ENOENT) {
2954b88c807SRodney W. Grimes 					warn("%s", f);
2964b88c807SRodney W. Grimes 					eval = 1;
2974b88c807SRodney W. Grimes 				}
2984b88c807SRodney W. Grimes 				continue;
2994b88c807SRodney W. Grimes 			}
3001f64b5c9SSteve Price 		} else if (Wflag) {
3011f64b5c9SSteve Price 			warnx("%s: %s", f, strerror(EEXIST));
3021f64b5c9SSteve Price 			eval = 1;
3031f64b5c9SSteve Price #endif
3041f64b5c9SSteve Price 			continue;
3051f64b5c9SSteve Price 		}
3061f64b5c9SSteve Price 
3071f64b5c9SSteve Price 		if (S_ISDIR(sb.st_mode) && !dflag) {
3084b88c807SRodney W. Grimes 			warnx("%s: is a directory", f);
3094b88c807SRodney W. Grimes 			eval = 1;
3104b88c807SRodney W. Grimes 			continue;
3114b88c807SRodney W. Grimes 		}
3121f64b5c9SSteve Price #ifdef BSD4_4_LITE
3134b88c807SRodney W. Grimes 		if (!fflag && !check(f, f, &sb))
3141f64b5c9SSteve Price #else
3151f64b5c9SSteve Price 		if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
3161f64b5c9SSteve Price #endif
3174b88c807SRodney W. Grimes 			continue;
3186b419813SAndrey A. Chernov 		rval = 0;
3196b419813SAndrey A. Chernov 		if (!uid &&
3206b419813SAndrey A. Chernov 		    (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
3216b419813SAndrey A. Chernov 		    !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
3226b419813SAndrey A. Chernov 			rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
3236b419813SAndrey A. Chernov 		if (!rval) {
3241f64b5c9SSteve Price #ifdef BSD4_4_LITE
3254b88c807SRodney W. Grimes 			if (S_ISDIR(sb.st_mode))
3261f64b5c9SSteve Price #else
3271f64b5c9SSteve Price 			if (S_ISWHT(sb.st_mode))
3281f64b5c9SSteve Price 				rval = undelete(f);
3291f64b5c9SSteve Price 			else if (S_ISDIR(sb.st_mode))
3301f64b5c9SSteve Price #endif
3314b88c807SRodney W. Grimes 				rval = rmdir(f);
3324b88c807SRodney W. Grimes 			else {
3334b88c807SRodney W. Grimes 				if (Pflag)
3344b88c807SRodney W. Grimes 					rm_overwrite(f, &sb);
3354b88c807SRodney W. Grimes 				rval = unlink(f);
3364b88c807SRodney W. Grimes 			}
3376b419813SAndrey A. Chernov 		}
3384b88c807SRodney W. Grimes 		if (rval && (!fflag || errno != ENOENT)) {
3394b88c807SRodney W. Grimes 			warn("%s", f);
3404b88c807SRodney W. Grimes 			eval = 1;
3414b88c807SRodney W. Grimes 		}
3424b88c807SRodney W. Grimes 	}
3434b88c807SRodney W. Grimes }
3444b88c807SRodney W. Grimes 
3454b88c807SRodney W. Grimes /*
3464b88c807SRodney W. Grimes  * rm_overwrite --
3474b88c807SRodney W. Grimes  *	Overwrite the file 3 times with varying bit patterns.
3484b88c807SRodney W. Grimes  *
3494b88c807SRodney W. Grimes  * XXX
3504b88c807SRodney W. Grimes  * This is a cheap way to *really* delete files.  Note that only regular
3514b88c807SRodney W. Grimes  * files are deleted, directories (and therefore names) will remain.
3524b88c807SRodney W. Grimes  * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
3534b88c807SRodney W. Grimes  * System V file system).  In a logging file system, you'll have to have
3544b88c807SRodney W. Grimes  * kernel support.
3554b88c807SRodney W. Grimes  */
3564b88c807SRodney W. Grimes void
3574b88c807SRodney W. Grimes rm_overwrite(file, sbp)
3584b88c807SRodney W. Grimes 	char *file;
3594b88c807SRodney W. Grimes 	struct stat *sbp;
3604b88c807SRodney W. Grimes {
3614b88c807SRodney W. Grimes 	struct stat sb;
3624b88c807SRodney W. Grimes 	off_t len;
3634b88c807SRodney W. Grimes 	int fd, wlen;
3644b88c807SRodney W. Grimes 	char buf[8 * 1024];
3654b88c807SRodney W. Grimes 
3664b88c807SRodney W. Grimes 	fd = -1;
3674b88c807SRodney W. Grimes 	if (sbp == NULL) {
3684b88c807SRodney W. Grimes 		if (lstat(file, &sb))
3694b88c807SRodney W. Grimes 			goto err;
3704b88c807SRodney W. Grimes 		sbp = &sb;
3714b88c807SRodney W. Grimes 	}
3724b88c807SRodney W. Grimes 	if (!S_ISREG(sbp->st_mode))
3734b88c807SRodney W. Grimes 		return;
3744b88c807SRodney W. Grimes 	if ((fd = open(file, O_WRONLY, 0)) == -1)
3754b88c807SRodney W. Grimes 		goto err;
3764b88c807SRodney W. Grimes 
3774b88c807SRodney W. Grimes #define	PASS(byte) {							\
3784b88c807SRodney W. Grimes 	memset(buf, byte, sizeof(buf));					\
3794b88c807SRodney W. Grimes 	for (len = sbp->st_size; len > 0; len -= wlen) {		\
3804b88c807SRodney W. Grimes 		wlen = len < sizeof(buf) ? len : sizeof(buf);		\
3814b88c807SRodney W. Grimes 		if (write(fd, buf, wlen) != wlen)			\
3824b88c807SRodney W. Grimes 			goto err;					\
3834b88c807SRodney W. Grimes 	}								\
3844b88c807SRodney W. Grimes }
3854b88c807SRodney W. Grimes 	PASS(0xff);
3864b88c807SRodney W. Grimes 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
3874b88c807SRodney W. Grimes 		goto err;
3884b88c807SRodney W. Grimes 	PASS(0x00);
3894b88c807SRodney W. Grimes 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
3904b88c807SRodney W. Grimes 		goto err;
3914b88c807SRodney W. Grimes 	PASS(0xff);
3924b88c807SRodney W. Grimes 	if (!fsync(fd) && !close(fd))
3934b88c807SRodney W. Grimes 		return;
3944b88c807SRodney W. Grimes 
3954b88c807SRodney W. Grimes err:	eval = 1;
3964b88c807SRodney W. Grimes 	warn("%s", file);
3974b88c807SRodney W. Grimes }
3984b88c807SRodney W. Grimes 
3994b88c807SRodney W. Grimes 
4004b88c807SRodney W. Grimes int
4014b88c807SRodney W. Grimes check(path, name, sp)
4024b88c807SRodney W. Grimes 	char *path, *name;
4034b88c807SRodney W. Grimes 	struct stat *sp;
4044b88c807SRodney W. Grimes {
4054b88c807SRodney W. Grimes 	int ch, first;
4066b419813SAndrey A. Chernov 	char modep[15], flagsp[128];
4074b88c807SRodney W. Grimes 
4084b88c807SRodney W. Grimes 	/* Check -i first. */
4094b88c807SRodney W. Grimes 	if (iflag)
4104b88c807SRodney W. Grimes 		(void)fprintf(stderr, "remove %s? ", path);
4114b88c807SRodney W. Grimes 	else {
4124b88c807SRodney W. Grimes 		/*
4134b88c807SRodney W. Grimes 		 * If it's not a symbolic link and it's unwritable and we're
4144b88c807SRodney W. Grimes 		 * talking to a terminal, ask.  Symbolic links are excluded
4154b88c807SRodney W. Grimes 		 * because their permissions are meaningless.  Check stdin_ok
4164b88c807SRodney W. Grimes 		 * first because we may not have stat'ed the file.
4174b88c807SRodney W. Grimes 		 */
4186b419813SAndrey A. Chernov 		if (!stdin_ok || S_ISLNK(sp->st_mode) ||
4191f64b5c9SSteve Price 		    (!access(name, W_OK) &&
4206b419813SAndrey A. Chernov 		    !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
4211f64b5c9SSteve Price 		    (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)))
4224b88c807SRodney W. Grimes 			return (1);
4234b88c807SRodney W. Grimes 		strmode(sp->st_mode, modep);
4246b419813SAndrey A. Chernov 		strcpy(flagsp, flags_to_string(sp->st_flags, NULL));
4256b419813SAndrey A. Chernov 		if (*flagsp)
4266b419813SAndrey A. Chernov 			strcat(flagsp, " ");
4276b419813SAndrey A. Chernov 		(void)fprintf(stderr, "override %s%s%s/%s %sfor %s? ",
4284b88c807SRodney W. Grimes 		    modep + 1, modep[9] == ' ' ? "" : " ",
4294b88c807SRodney W. Grimes 		    user_from_uid(sp->st_uid, 0),
4306b419813SAndrey A. Chernov 		    group_from_gid(sp->st_gid, 0),
4316b419813SAndrey A. Chernov 		    *flagsp ? flagsp : "",
4326b419813SAndrey A. Chernov 		    path);
4334b88c807SRodney W. Grimes 	}
4344b88c807SRodney W. Grimes 	(void)fflush(stderr);
4354b88c807SRodney W. Grimes 
4364b88c807SRodney W. Grimes 	first = ch = getchar();
4374b88c807SRodney W. Grimes 	while (ch != '\n' && ch != EOF)
4384b88c807SRodney W. Grimes 		ch = getchar();
439b0205affSWolfram Schneider 	return (first == 'y' || first == 'Y');
4404b88c807SRodney W. Grimes }
4414b88c807SRodney W. Grimes 
44267a3d3a8SPoul-Henning Kamp #define ISDOT(a)	((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
4434b88c807SRodney W. Grimes void
4444b88c807SRodney W. Grimes checkdot(argv)
4454b88c807SRodney W. Grimes 	char **argv;
4464b88c807SRodney W. Grimes {
4474b88c807SRodney W. Grimes 	char *p, **save, **t;
4484b88c807SRodney W. Grimes 	int complained;
4494b88c807SRodney W. Grimes 
4504b88c807SRodney W. Grimes 	complained = 0;
4514b88c807SRodney W. Grimes 	for (t = argv; *t;) {
4524b88c807SRodney W. Grimes 		if ((p = strrchr(*t, '/')) != NULL)
4534b88c807SRodney W. Grimes 			++p;
4544b88c807SRodney W. Grimes 		else
4554b88c807SRodney W. Grimes 			p = *t;
4564b88c807SRodney W. Grimes 		if (ISDOT(p)) {
4574b88c807SRodney W. Grimes 			if (!complained++)
4584b88c807SRodney W. Grimes 				warnx("\".\" and \"..\" may not be removed");
4594b88c807SRodney W. Grimes 			eval = 1;
4601f64b5c9SSteve Price 			for (save = t; (t[0] = t[1]) != NULL; ++t)
4611f64b5c9SSteve Price 				continue;
4624b88c807SRodney W. Grimes 			t = save;
4634b88c807SRodney W. Grimes 		} else
4644b88c807SRodney W. Grimes 			++t;
4654b88c807SRodney W. Grimes 	}
4664b88c807SRodney W. Grimes }
4674b88c807SRodney W. Grimes 
4684b88c807SRodney W. Grimes void
4694b88c807SRodney W. Grimes usage()
4704b88c807SRodney W. Grimes {
4714b88c807SRodney W. Grimes 
4721f64b5c9SSteve Price #ifdef BSD4_4_LITE
473b0205affSWolfram Schneider 	(void)fprintf(stderr, "usage: rm [-f | -i] [-dPRr] file ...\n");
4741f64b5c9SSteve Price #else
4751f64b5c9SSteve Price 	(void)fprintf(stderr, "usage: rm [-f | -i] [-dPRrW] file ...\n");
4761f64b5c9SSteve Price #endif
4774b88c807SRodney W. Grimes 	exit(1);
4784b88c807SRodney W. Grimes }
479