rm.c revision 241014
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1990, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 4. Neither the name of the University nor the names of its contributors
141556Srgrimes *    may be used to endorse or promote products derived from this software
151556Srgrimes *    without specific prior written permission.
161556Srgrimes *
171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271556Srgrimes * SUCH DAMAGE.
281556Srgrimes */
291556Srgrimes
30114433Sobrien#if 0
311556Srgrimes#ifndef lint
3227959Sstevestatic const char copyright[] =
331556Srgrimes"@(#) Copyright (c) 1990, 1993, 1994\n\
341556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
3527964Ssteve#endif /* not lint */
3627964Ssteve
3727964Ssteve#ifndef lint
3827964Sstevestatic char sccsid[] = "@(#)rm.c	8.5 (Berkeley) 4/18/94";
39114433Sobrien#endif /* not lint */
4027964Ssteve#endif
4199110Sobrien#include <sys/cdefs.h>
4299110Sobrien__FBSDID("$FreeBSD: head/bin/rm/rm.c 241014 2012-09-27 23:31:12Z mdf $");
431556Srgrimes
441556Srgrimes#include <sys/stat.h>
4547584Skris#include <sys/param.h>
4647584Skris#include <sys/mount.h>
471556Srgrimes
481556Srgrimes#include <err.h>
491556Srgrimes#include <errno.h>
501556Srgrimes#include <fcntl.h>
511556Srgrimes#include <fts.h>
5290644Simp#include <grp.h>
5390644Simp#include <pwd.h>
54241014Smdf#include <stdint.h>
551556Srgrimes#include <stdio.h>
561556Srgrimes#include <stdlib.h>
571556Srgrimes#include <string.h>
5850539Smharo#include <sysexits.h>
591556Srgrimes#include <unistd.h>
601556Srgrimes
61226961Sedstatic int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
62226961Sedstatic int rflag, Iflag;
63226961Sedstatic uid_t uid;
64226961Sedstatic volatile sig_atomic_t info;
651556Srgrimes
6690110Simpint	check(char *, char *, struct stat *);
67137009Sdelphijint	check2(char **);
6890110Simpvoid	checkdot(char **);
69136113Sdesvoid	checkslash(char **);
7090110Simpvoid	rm_file(char **);
71122409Sguidoint	rm_overwrite(char *, struct stat *);
7290110Simpvoid	rm_tree(char **);
73191670Simpstatic void siginfo(int __unused);
7490110Simpvoid	usage(void);
751556Srgrimes
761556Srgrimes/*
771556Srgrimes * rm --
781556Srgrimes *	This rm is different from historic rm's, but is expected to match
79136112Sdes *	POSIX 1003.2 behavior.	The most visible difference is that -f
801556Srgrimes *	has two specific effects now, ignore non-existent files and force
81136112Sdes *	file removal.
821556Srgrimes */
831556Srgrimesint
8490110Simpmain(int argc, char *argv[])
851556Srgrimes{
86137009Sdelphij	int ch;
8754895Ssheldonh	char *p;
881556Srgrimes
8954895Ssheldonh	/*
9054895Ssheldonh	 * Test for the special case where the utility is called as
9154895Ssheldonh	 * "unlink", for which the functionality provided is greatly
9254895Ssheldonh	 * simplified.
9354895Ssheldonh	 */
94219680Sjilles	if ((p = strrchr(argv[0], '/')) == NULL)
9554895Ssheldonh		p = argv[0];
9654895Ssheldonh	else
9754895Ssheldonh		++p;
9854895Ssheldonh	if (strcmp(p, "unlink") == 0) {
9997533Stjr		while (getopt(argc, argv, "") != -1)
10054895Ssheldonh			usage();
10197533Stjr		argc -= optind;
10297533Stjr		argv += optind;
10399858Stjr		if (argc != 1)
10497533Stjr			usage();
10597533Stjr		rm_file(&argv[0]);
10697533Stjr		exit(eval);
10754895Ssheldonh	}
10854895Ssheldonh
10920421Ssteve	Pflag = rflag = 0;
110137009Sdelphij	while ((ch = getopt(argc, argv, "dfiIPRrvW")) != -1)
1111556Srgrimes		switch(ch) {
1121556Srgrimes		case 'd':
1131556Srgrimes			dflag = 1;
1141556Srgrimes			break;
1151556Srgrimes		case 'f':
1161556Srgrimes			fflag = 1;
1171556Srgrimes			iflag = 0;
1181556Srgrimes			break;
1191556Srgrimes		case 'i':
1201556Srgrimes			fflag = 0;
1211556Srgrimes			iflag = 1;
1221556Srgrimes			break;
123137009Sdelphij		case 'I':
124137009Sdelphij			Iflag = 1;
125137009Sdelphij			break;
1261556Srgrimes		case 'P':
1271556Srgrimes			Pflag = 1;
1281556Srgrimes			break;
1291556Srgrimes		case 'R':
1301556Srgrimes		case 'r':			/* Compatibility. */
1311556Srgrimes			rflag = 1;
1321556Srgrimes			break;
13350872Smharo		case 'v':
13450872Smharo			vflag = 1;
13550872Smharo			break;
13620421Ssteve		case 'W':
13720421Ssteve			Wflag = 1;
13820421Ssteve			break;
1391556Srgrimes		default:
1401556Srgrimes			usage();
1411556Srgrimes		}
1421556Srgrimes	argc -= optind;
1431556Srgrimes	argv += optind;
1441556Srgrimes
14544282Sjkh	if (argc < 1) {
14644282Sjkh		if (fflag)
147122409Sguido			return (0);
1481556Srgrimes		usage();
14944282Sjkh	}
1501556Srgrimes
1511556Srgrimes	checkdot(argv);
152136124Sdes	if (getenv("POSIXLY_CORRECT") == NULL)
153136124Sdes		checkslash(argv);
1547798Sache	uid = geteuid();
1551556Srgrimes
156191670Simp	(void)signal(SIGINFO, siginfo);
15720421Ssteve	if (*argv) {
15820421Ssteve		stdin_ok = isatty(STDIN_FILENO);
15920421Ssteve
160137009Sdelphij		if (Iflag) {
161137009Sdelphij			if (check2(argv) == 0)
162137009Sdelphij				exit (1);
163137009Sdelphij		}
16420421Ssteve		if (rflag)
16520421Ssteve			rm_tree(argv);
16620421Ssteve		else
16720421Ssteve			rm_file(argv);
16820421Ssteve	}
16920421Ssteve
1701556Srgrimes	exit (eval);
1711556Srgrimes}
1721556Srgrimes
1731556Srgrimesvoid
17490110Simprm_tree(char **argv)
1751556Srgrimes{
1761556Srgrimes	FTS *fts;
1771556Srgrimes	FTSENT *p;
1781556Srgrimes	int needstat;
17920421Ssteve	int flags;
1807798Sache	int rval;
1811556Srgrimes
1821556Srgrimes	/*
1831556Srgrimes	 * Remove a file hierarchy.  If forcing removal (-f), or interactive
1841556Srgrimes	 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
1851556Srgrimes	 */
18620421Ssteve	needstat = !uid || (!fflag && !iflag && stdin_ok);
1871556Srgrimes
1881556Srgrimes	/*
1891556Srgrimes	 * If the -i option is specified, the user can skip on the pre-order
1901556Srgrimes	 * visit.  The fts_number field flags skipped directories.
1911556Srgrimes	 */
1921556Srgrimes#define	SKIPPED	1
1931556Srgrimes
19451230Sbde	flags = FTS_PHYSICAL;
19520421Ssteve	if (!needstat)
19620421Ssteve		flags |= FTS_NOSTAT;
19720421Ssteve	if (Wflag)
19820421Ssteve		flags |= FTS_WHITEOUT;
199137639Sjkh	if (!(fts = fts_open(argv, flags, NULL))) {
200137639Sjkh		if (fflag && errno == ENOENT)
201137639Sjkh			return;
20299744Sdillon		err(1, "fts_open");
203137639Sjkh	}
2041556Srgrimes	while ((p = fts_read(fts)) != NULL) {
2051556Srgrimes		switch (p->fts_info) {
2061556Srgrimes		case FTS_DNR:
2071556Srgrimes			if (!fflag || p->fts_errno != ENOENT) {
2081556Srgrimes				warnx("%s: %s",
2091556Srgrimes				    p->fts_path, strerror(p->fts_errno));
2101556Srgrimes				eval = 1;
2111556Srgrimes			}
2121556Srgrimes			continue;
2131556Srgrimes		case FTS_ERR:
2141556Srgrimes			errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
2151556Srgrimes		case FTS_NS:
2161556Srgrimes			/*
217124041Skuriyama			 * Assume that since fts_read() couldn't stat the
218124041Skuriyama			 * file, it can't be unlinked.
2191556Srgrimes			 */
2201556Srgrimes			if (!needstat)
2211556Srgrimes				break;
2221556Srgrimes			if (!fflag || p->fts_errno != ENOENT) {
2231556Srgrimes				warnx("%s: %s",
2241556Srgrimes				    p->fts_path, strerror(p->fts_errno));
2251556Srgrimes				eval = 1;
2261556Srgrimes			}
2271556Srgrimes			continue;
2281556Srgrimes		case FTS_D:
2291556Srgrimes			/* Pre-order: give user chance to skip. */
23020421Ssteve			if (!fflag && !check(p->fts_path, p->fts_accpath,
2311556Srgrimes			    p->fts_statp)) {
2321556Srgrimes				(void)fts_set(fts, p, FTS_SKIP);
2331556Srgrimes				p->fts_number = SKIPPED;
2341556Srgrimes			}
2357798Sache			else if (!uid &&
2367798Sache				 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
2377798Sache				 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
238193087Sjilles				 lchflags(p->fts_accpath,
2397798Sache					 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
2407798Sache				goto err;
2411556Srgrimes			continue;
2421556Srgrimes		case FTS_DP:
2431556Srgrimes			/* Post-order: see if user skipped. */
2441556Srgrimes			if (p->fts_number == SKIPPED)
2451556Srgrimes				continue;
2461556Srgrimes			break;
24720421Ssteve		default:
24820421Ssteve			if (!fflag &&
24920421Ssteve			    !check(p->fts_path, p->fts_accpath, p->fts_statp))
25020421Ssteve				continue;
2511556Srgrimes		}
2521556Srgrimes
2537798Sache		rval = 0;
2547798Sache		if (!uid &&
2557798Sache		    (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
2567798Sache		    !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
257193087Sjilles			rval = lchflags(p->fts_accpath,
2587798Sache				       p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
25953819Smharo		if (rval == 0) {
2607798Sache			/*
2617798Sache			 * If we can't read or search the directory, may still be
2627798Sache			 * able to remove it.  Don't print out the un{read,search}able
2637798Sache			 * message unless the remove fails.
2647798Sache			 */
26520421Ssteve			switch (p->fts_info) {
26620421Ssteve			case FTS_DP:
26720421Ssteve			case FTS_DNR:
26853819Smharo				rval = rmdir(p->fts_accpath);
26953819Smharo				if (rval == 0 || (fflag && errno == ENOENT)) {
27053819Smharo					if (rval == 0 && vflag)
27150872Smharo						(void)printf("%s\n",
27270219Sobrien						    p->fts_path);
273191670Simp					if (rval == 0 && info) {
274191670Simp						info = 0;
275191670Simp						(void)printf("%s\n",
276191670Simp						    p->fts_path);
277191670Simp					}
2781556Srgrimes					continue;
27950539Smharo				}
28020421Ssteve				break;
28120421Ssteve
28220421Ssteve			case FTS_W:
28353819Smharo				rval = undelete(p->fts_accpath);
28453819Smharo				if (rval == 0 && (fflag && errno == ENOENT)) {
28553819Smharo					if (vflag)
28650872Smharo						(void)printf("%s\n",
28770219Sobrien						    p->fts_path);
288191670Simp					if (info) {
289191670Simp						info = 0;
290191670Simp						(void)printf("%s\n",
291191670Simp						    p->fts_path);
292191670Simp					}
29320421Ssteve					continue;
29450539Smharo				}
29520421Ssteve				break;
29620421Ssteve
297124041Skuriyama			case FTS_NS:
298124041Skuriyama				/*
299124041Skuriyama				 * Assume that since fts_read() couldn't stat
300124041Skuriyama				 * the file, it can't be unlinked.
301124041Skuriyama				 */
302124041Skuriyama				if (fflag)
303124041Skuriyama					continue;
304124041Skuriyama				/* FALLTHROUGH */
305237339Sdelphij
306237339Sdelphij			case FTS_F:
307237339Sdelphij			case FTS_NSOK:
3087798Sache				if (Pflag)
309237339Sdelphij					if (!rm_overwrite(p->fts_accpath, p->fts_info ==
310237339Sdelphij					    FTS_NSOK ? NULL : p->fts_statp))
311122409Sguido						continue;
312237339Sdelphij				/* FALLTHROUGH */
313237339Sdelphij
314237339Sdelphij			default:
31553819Smharo				rval = unlink(p->fts_accpath);
31653819Smharo				if (rval == 0 || (fflag && errno == ENOENT)) {
31753819Smharo					if (rval == 0 && vflag)
31850872Smharo						(void)printf("%s\n",
31970219Sobrien						    p->fts_path);
320191670Simp					if (rval == 0 && info) {
321191670Simp						info = 0;
322191670Simp						(void)printf("%s\n",
323191670Simp						    p->fts_path);
324191670Simp					}
3257798Sache					continue;
32650539Smharo				}
3277798Sache			}
3281556Srgrimes		}
3297798Sacheerr:
3301556Srgrimes		warn("%s", p->fts_path);
3311556Srgrimes		eval = 1;
3321556Srgrimes	}
3331556Srgrimes	if (errno)
3341556Srgrimes		err(1, "fts_read");
335157770Smaxim	fts_close(fts);
3361556Srgrimes}
3371556Srgrimes
3381556Srgrimesvoid
33990110Simprm_file(char **argv)
3401556Srgrimes{
3411556Srgrimes	struct stat sb;
34220421Ssteve	int rval;
3431556Srgrimes	char *f;
3441556Srgrimes
3451556Srgrimes	/*
3461556Srgrimes	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
3471556Srgrimes	 * to remove a directory is an error, so must always stat the file.
3481556Srgrimes	 */
3491556Srgrimes	while ((f = *argv++) != NULL) {
3501556Srgrimes		/* Assume if can't stat the file, can't unlink it. */
3511556Srgrimes		if (lstat(f, &sb)) {
35220421Ssteve			if (Wflag) {
35320421Ssteve				sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
35420421Ssteve			} else {
35520421Ssteve				if (!fflag || errno != ENOENT) {
35620421Ssteve					warn("%s", f);
35720421Ssteve					eval = 1;
35820421Ssteve				}
35920421Ssteve				continue;
3601556Srgrimes			}
36120421Ssteve		} else if (Wflag) {
36220421Ssteve			warnx("%s: %s", f, strerror(EEXIST));
36320421Ssteve			eval = 1;
3641556Srgrimes			continue;
3651556Srgrimes		}
36620421Ssteve
36720421Ssteve		if (S_ISDIR(sb.st_mode) && !dflag) {
3681556Srgrimes			warnx("%s: is a directory", f);
3691556Srgrimes			eval = 1;
3701556Srgrimes			continue;
3711556Srgrimes		}
37220421Ssteve		if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
3731556Srgrimes			continue;
3747798Sache		rval = 0;
375163485Smaxim		if (!uid && !S_ISWHT(sb.st_mode) &&
3767798Sache		    (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
3777798Sache		    !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
378193087Sjilles			rval = lchflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
37953819Smharo		if (rval == 0) {
38020421Ssteve			if (S_ISWHT(sb.st_mode))
38120421Ssteve				rval = undelete(f);
38220421Ssteve			else if (S_ISDIR(sb.st_mode))
3837798Sache				rval = rmdir(f);
3847798Sache			else {
3857798Sache				if (Pflag)
386122409Sguido					if (!rm_overwrite(f, &sb))
387122409Sguido						continue;
3887798Sache				rval = unlink(f);
3897798Sache			}
3901556Srgrimes		}
3911556Srgrimes		if (rval && (!fflag || errno != ENOENT)) {
3921556Srgrimes			warn("%s", f);
3931556Srgrimes			eval = 1;
3941556Srgrimes		}
39553819Smharo		if (vflag && rval == 0)
39650539Smharo			(void)printf("%s\n", f);
397191670Simp		if (info && rval == 0) {
398191670Simp			info = 0;
399191670Simp			(void)printf("%s\n", f);
400191670Simp		}
4011556Srgrimes	}
4021556Srgrimes}
4031556Srgrimes
4041556Srgrimes/*
4051556Srgrimes * rm_overwrite --
4061556Srgrimes *	Overwrite the file 3 times with varying bit patterns.
4071556Srgrimes *
4081556Srgrimes * XXX
4091556Srgrimes * This is a cheap way to *really* delete files.  Note that only regular
4101556Srgrimes * files are deleted, directories (and therefore names) will remain.
411102230Strhodes * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
412213582Suqs * System V file system).  In a logging or COW file system, you'll have to
413213582Suqs * have kernel support.
4141556Srgrimes */
415122409Sguidoint
41690110Simprm_overwrite(char *file, struct stat *sbp)
4171556Srgrimes{
418237284Skevlo	struct stat sb, sb2;
41947584Skris	struct statfs fsb;
4201556Srgrimes	off_t len;
42147584Skris	int bsize, fd, wlen;
42247584Skris	char *buf = NULL;
4231556Srgrimes
4241556Srgrimes	fd = -1;
4251556Srgrimes	if (sbp == NULL) {
4261556Srgrimes		if (lstat(file, &sb))
4271556Srgrimes			goto err;
4281556Srgrimes		sbp = &sb;
4291556Srgrimes	}
4301556Srgrimes	if (!S_ISREG(sbp->st_mode))
431122409Sguido		return (1);
432163812Sdelphij	if (sbp->st_nlink > 1 && !fflag) {
433241014Smdf		warnx("%s (inode %ju): not overwritten due to multiple links",
434241014Smdf		    file, (uintmax_t)sbp->st_ino);
435163812Sdelphij		return (0);
436163777Sdelphij	}
437237284Skevlo	if ((fd = open(file, O_WRONLY|O_NONBLOCK|O_NOFOLLOW, 0)) == -1)
4381556Srgrimes		goto err;
439237284Skevlo	if (fstat(fd, &sb2))
440237284Skevlo		goto err;
441237284Skevlo	if (sb2.st_dev != sbp->st_dev || sb2.st_ino != sbp->st_ino ||
442237284Skevlo	    !S_ISREG(sb2.st_mode)) {
443237284Skevlo		errno = EPERM;
444237284Skevlo		goto err;
445237284Skevlo	}
44647584Skris	if (fstatfs(fd, &fsb) == -1)
44747584Skris		goto err;
44847584Skris	bsize = MAX(fsb.f_iosize, 1024);
44947584Skris	if ((buf = malloc(bsize)) == NULL)
450122304Sbde		err(1, "%s: malloc", file);
4511556Srgrimes
4521556Srgrimes#define	PASS(byte) {							\
45347584Skris	memset(buf, byte, bsize);					\
4541556Srgrimes	for (len = sbp->st_size; len > 0; len -= wlen) {		\
45547584Skris		wlen = len < bsize ? len : bsize;			\
4561556Srgrimes		if (write(fd, buf, wlen) != wlen)			\
4571556Srgrimes			goto err;					\
4581556Srgrimes	}								\
4591556Srgrimes}
4601556Srgrimes	PASS(0xff);
4611556Srgrimes	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
4621556Srgrimes		goto err;
4631556Srgrimes	PASS(0x00);
4641556Srgrimes	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
4651556Srgrimes		goto err;
4661556Srgrimes	PASS(0xff);
46747584Skris	if (!fsync(fd) && !close(fd)) {
46847584Skris		free(buf);
469122409Sguido		return (1);
47047584Skris	}
4711556Srgrimes
4721556Srgrimeserr:	eval = 1;
47347584Skris	if (buf)
47447584Skris		free(buf);
475122304Sbde	if (fd != -1)
476122304Sbde		close(fd);
4771556Srgrimes	warn("%s", file);
478122409Sguido	return (0);
4791556Srgrimes}
4801556Srgrimes
4811556Srgrimes
4821556Srgrimesint
48390110Simpcheck(char *path, char *name, struct stat *sp)
4841556Srgrimes{
4851556Srgrimes	int ch, first;
48661749Sjoe	char modep[15], *flagsp;
4871556Srgrimes
4881556Srgrimes	/* Check -i first. */
4891556Srgrimes	if (iflag)
4901556Srgrimes		(void)fprintf(stderr, "remove %s? ", path);
4911556Srgrimes	else {
4921556Srgrimes		/*
4931556Srgrimes		 * If it's not a symbolic link and it's unwritable and we're
494136112Sdes		 * talking to a terminal, ask.	Symbolic links are excluded
4951556Srgrimes		 * because their permissions are meaningless.  Check stdin_ok
4961556Srgrimes		 * first because we may not have stat'ed the file.
4971556Srgrimes		 */
498150729Sdougb		if (!stdin_ok || S_ISLNK(sp->st_mode) ||
49920421Ssteve		    (!access(name, W_OK) &&
5007798Sache		    !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
50120421Ssteve		    (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)))
5021556Srgrimes			return (1);
5031556Srgrimes		strmode(sp->st_mode, modep);
50461749Sjoe		if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
50599744Sdillon			err(1, "fflagstostr");
506150729Sdougb		if (Pflag)
507150729Sdougb			errx(1,
508150729Sdougb			    "%s: -P was specified, but file is not writable",
509150729Sdougb			    path);
51061749Sjoe		(void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
5111556Srgrimes		    modep + 1, modep[9] == ' ' ? "" : " ",
5121556Srgrimes		    user_from_uid(sp->st_uid, 0),
5137798Sache		    group_from_gid(sp->st_gid, 0),
514136112Sdes		    *flagsp ? flagsp : "", *flagsp ? " " : "",
5157798Sache		    path);
51661749Sjoe		free(flagsp);
5171556Srgrimes	}
5181556Srgrimes	(void)fflush(stderr);
5191556Srgrimes
5201556Srgrimes	first = ch = getchar();
5211556Srgrimes	while (ch != '\n' && ch != EOF)
5221556Srgrimes		ch = getchar();
52314409Swosch	return (first == 'y' || first == 'Y');
5241556Srgrimes}
5251556Srgrimes
526136113Sdes#define ISSLASH(a)	((a)[0] == '/' && (a)[1] == '\0')
527136113Sdesvoid
528136113Sdescheckslash(char **argv)
529136113Sdes{
530136113Sdes	char **t, **u;
531136113Sdes	int complained;
532136113Sdes
533136113Sdes	complained = 0;
534136113Sdes	for (t = argv; *t;) {
535136113Sdes		if (ISSLASH(*t)) {
536136113Sdes			if (!complained++)
537136113Sdes				warnx("\"/\" may not be removed");
538136113Sdes			eval = 1;
539136113Sdes			for (u = t; u[0] != NULL; ++u)
540136113Sdes				u[0] = u[1];
541136113Sdes		} else {
542136113Sdes			++t;
543136113Sdes		}
544136113Sdes	}
545136113Sdes}
546136113Sdes
547137009Sdelphijint
548137009Sdelphijcheck2(char **argv)
549137009Sdelphij{
550137009Sdelphij	struct stat st;
551137009Sdelphij	int first;
552137009Sdelphij	int ch;
553137009Sdelphij	int fcount = 0;
554137009Sdelphij	int dcount = 0;
555137009Sdelphij	int i;
556137009Sdelphij	const char *dname = NULL;
557137009Sdelphij
558137009Sdelphij	for (i = 0; argv[i]; ++i) {
559137009Sdelphij		if (lstat(argv[i], &st) == 0) {
560137009Sdelphij			if (S_ISDIR(st.st_mode)) {
561137009Sdelphij				++dcount;
562137009Sdelphij				dname = argv[i];    /* only used if 1 dir */
563137009Sdelphij			} else {
564137009Sdelphij				++fcount;
565137009Sdelphij			}
566137009Sdelphij		}
567137009Sdelphij	}
568137009Sdelphij	first = 0;
569137009Sdelphij	while (first != 'n' && first != 'N' && first != 'y' && first != 'Y') {
570137009Sdelphij		if (dcount && rflag) {
571137009Sdelphij			fprintf(stderr, "recursively remove");
572137009Sdelphij			if (dcount == 1)
573137009Sdelphij				fprintf(stderr, " %s", dname);
574137009Sdelphij			else
575137009Sdelphij				fprintf(stderr, " %d dirs", dcount);
576137009Sdelphij			if (fcount == 1)
577137009Sdelphij				fprintf(stderr, " and 1 file");
578137009Sdelphij			else if (fcount > 1)
579137009Sdelphij				fprintf(stderr, " and %d files", fcount);
580137009Sdelphij		} else if (dcount + fcount > 3) {
581137009Sdelphij			fprintf(stderr, "remove %d files", dcount + fcount);
582137009Sdelphij		} else {
583137009Sdelphij			return(1);
584137009Sdelphij		}
585137009Sdelphij		fprintf(stderr, "? ");
586137009Sdelphij		fflush(stderr);
587137009Sdelphij
588137009Sdelphij		first = ch = getchar();
589137009Sdelphij		while (ch != '\n' && ch != EOF)
590137009Sdelphij			ch = getchar();
591137009Sdelphij		if (ch == EOF)
592137009Sdelphij			break;
593137009Sdelphij	}
594137009Sdelphij	return (first == 'y' || first == 'Y');
595137009Sdelphij}
596137009Sdelphij
5972927Sphk#define ISDOT(a)	((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
5981556Srgrimesvoid
59990110Simpcheckdot(char **argv)
6001556Srgrimes{
6011556Srgrimes	char *p, **save, **t;
6021556Srgrimes	int complained;
6031556Srgrimes
6041556Srgrimes	complained = 0;
6051556Srgrimes	for (t = argv; *t;) {
6061556Srgrimes		if ((p = strrchr(*t, '/')) != NULL)
6071556Srgrimes			++p;
6081556Srgrimes		else
6091556Srgrimes			p = *t;
6101556Srgrimes		if (ISDOT(p)) {
6111556Srgrimes			if (!complained++)
6121556Srgrimes				warnx("\".\" and \"..\" may not be removed");
6131556Srgrimes			eval = 1;
61420421Ssteve			for (save = t; (t[0] = t[1]) != NULL; ++t)
61520421Ssteve				continue;
6161556Srgrimes			t = save;
6171556Srgrimes		} else
6181556Srgrimes			++t;
6191556Srgrimes	}
6201556Srgrimes}
6211556Srgrimes
6221556Srgrimesvoid
62390110Simpusage(void)
6241556Srgrimes{
62553819Smharo
62654895Ssheldonh	(void)fprintf(stderr, "%s\n%s\n",
627137009Sdelphij	    "usage: rm [-f | -i] [-dIPRrvW] file ...",
62854895Ssheldonh	    "       unlink file");
62950539Smharo	exit(EX_USAGE);
6301556Srgrimes}
631191670Simp
632191670Simpstatic void
633191670Simpsiginfo(int sig __unused)
634191670Simp{
635191670Simp
636191670Simp	info = 1;
637191670Simp}
638