xref: /titanic_44/usr/src/cmd/rm/rm.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  * rm [-fiRr] file ...
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
39*7c478bd9Sstevel@tonic-gate #include <string.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
42*7c478bd9Sstevel@tonic-gate #include <dirent.h>
43*7c478bd9Sstevel@tonic-gate #include <limits.h>
44*7c478bd9Sstevel@tonic-gate #include <locale.h>
45*7c478bd9Sstevel@tonic-gate #include <langinfo.h>
46*7c478bd9Sstevel@tonic-gate #include <unistd.h>
47*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
48*7c478bd9Sstevel@tonic-gate #include <errno.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/resource.h>
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #define	ARGCNT		5		/* Number of arguments */
52*7c478bd9Sstevel@tonic-gate #define	CHILD		0
53*7c478bd9Sstevel@tonic-gate #define	DIRECTORY	((buffer.st_mode&S_IFMT) == S_IFDIR)
54*7c478bd9Sstevel@tonic-gate #define	SYMLINK		((buffer.st_mode&S_IFMT) == S_IFLNK)
55*7c478bd9Sstevel@tonic-gate #define	FAIL		-1
56*7c478bd9Sstevel@tonic-gate #define	MAXFORK		100		/* Maximum number of forking attempts */
57*7c478bd9Sstevel@tonic-gate #define	NAMESIZE	MAXNAMLEN + 1	/* "/" + (file name size) */
58*7c478bd9Sstevel@tonic-gate #define	TRUE		1
59*7c478bd9Sstevel@tonic-gate #define	FALSE		0
60*7c478bd9Sstevel@tonic-gate #define	WRITE		02
61*7c478bd9Sstevel@tonic-gate #define	SEARCH		07
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate static	int	errcode;
64*7c478bd9Sstevel@tonic-gate static	int interactive, recursive, silent; /* flags for command line options */
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate static	void	rm(char *, int);
67*7c478bd9Sstevel@tonic-gate static	void	undir(char *, int, dev_t, ino_t);
68*7c478bd9Sstevel@tonic-gate static	int	yes(void);
69*7c478bd9Sstevel@tonic-gate static	int	mypath(dev_t, ino_t);
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate static	char	yeschr[SCHAR_MAX + 2];
72*7c478bd9Sstevel@tonic-gate static	char	nochr[SCHAR_MAX + 2];
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate static char *fullpath;
75*7c478bd9Sstevel@tonic-gate static int homedirfd;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static void push_name(char *name, int first);
78*7c478bd9Sstevel@tonic-gate static void pop_name(int first);
79*7c478bd9Sstevel@tonic-gate static void force_chdir(char *);
80*7c478bd9Sstevel@tonic-gate static void ch_dir(char *);
81*7c478bd9Sstevel@tonic-gate static char *get_filename(char *name);
82*7c478bd9Sstevel@tonic-gate static void chdir_home(void);
83*7c478bd9Sstevel@tonic-gate static void check_homedir(void);
84*7c478bd9Sstevel@tonic-gate static void cleanup(void);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate static char 	*cwd;		/* pathname of home dir, from getcwd() */
87*7c478bd9Sstevel@tonic-gate static rlim_t	maxfiles;	/* maximum number of open files */
88*7c478bd9Sstevel@tonic-gate static int	first_dir = 1;	/* flag set when first trying to remove a dir */
89*7c478bd9Sstevel@tonic-gate 	/* flag set when can't get dev/inode of a parent dir */
90*7c478bd9Sstevel@tonic-gate static int	parent_err = 0;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate struct dir_id {
93*7c478bd9Sstevel@tonic-gate 	dev_t	dev;
94*7c478bd9Sstevel@tonic-gate 	ino_t	inode;
95*7c478bd9Sstevel@tonic-gate 	struct dir_id *next;
96*7c478bd9Sstevel@tonic-gate };
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	/*
99*7c478bd9Sstevel@tonic-gate 	 * homedir is the first of a linked list of structures
100*7c478bd9Sstevel@tonic-gate 	 * containing unique identifying device and inode numbers for
101*7c478bd9Sstevel@tonic-gate 	 * each directory, from the home dir up to the root.
102*7c478bd9Sstevel@tonic-gate 	 */
103*7c478bd9Sstevel@tonic-gate static struct dir_id homedir;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate int
106*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	extern int	optind;
109*7c478bd9Sstevel@tonic-gate 	int	errflg = 0;
110*7c478bd9Sstevel@tonic-gate 	int	c;
111*7c478bd9Sstevel@tonic-gate 	struct rlimit rl;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
114*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
115*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
116*7c478bd9Sstevel@tonic-gate #endif
117*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	(void) strncpy(yeschr, nl_langinfo(YESSTR), SCHAR_MAX + 1);
120*7c478bd9Sstevel@tonic-gate 	(void) strncpy(nochr, nl_langinfo(NOSTR), SCHAR_MAX + 1);
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "frRi")) != EOF)
123*7c478bd9Sstevel@tonic-gate 		switch (c) {
124*7c478bd9Sstevel@tonic-gate 		case 'f':
125*7c478bd9Sstevel@tonic-gate 			silent = TRUE;
126*7c478bd9Sstevel@tonic-gate #ifdef XPG4
127*7c478bd9Sstevel@tonic-gate 			interactive = FALSE;
128*7c478bd9Sstevel@tonic-gate #endif
129*7c478bd9Sstevel@tonic-gate 			break;
130*7c478bd9Sstevel@tonic-gate 		case 'i':
131*7c478bd9Sstevel@tonic-gate 			interactive = TRUE;
132*7c478bd9Sstevel@tonic-gate #ifdef XPG4
133*7c478bd9Sstevel@tonic-gate 			silent = FALSE;
134*7c478bd9Sstevel@tonic-gate #endif
135*7c478bd9Sstevel@tonic-gate 			break;
136*7c478bd9Sstevel@tonic-gate 		case 'r':
137*7c478bd9Sstevel@tonic-gate 		case 'R':
138*7c478bd9Sstevel@tonic-gate 			recursive = TRUE;
139*7c478bd9Sstevel@tonic-gate 			break;
140*7c478bd9Sstevel@tonic-gate 		case '?':
141*7c478bd9Sstevel@tonic-gate 			errflg = 1;
142*7c478bd9Sstevel@tonic-gate 			break;
143*7c478bd9Sstevel@tonic-gate 		}
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	/*
146*7c478bd9Sstevel@tonic-gate 	 * For BSD compatibility allow '-' to delimit the end
147*7c478bd9Sstevel@tonic-gate 	 * of options.  However, if options were already explicitly
148*7c478bd9Sstevel@tonic-gate 	 * terminated with '--', then treat '-' literally: otherwise,
149*7c478bd9Sstevel@tonic-gate 	 * "rm -- -" won't remove '-'.
150*7c478bd9Sstevel@tonic-gate 	 */
151*7c478bd9Sstevel@tonic-gate 	if (optind < argc &&
152*7c478bd9Sstevel@tonic-gate 	    strcmp(argv[optind], "-") == 0 &&
153*7c478bd9Sstevel@tonic-gate 	    strcmp(argv[optind - 1], "--") != 0)
154*7c478bd9Sstevel@tonic-gate 		optind++;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	argc -= optind;
157*7c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	if ((argc < 1 && !silent) || errflg) {
160*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
161*7c478bd9Sstevel@tonic-gate 			gettext("usage: rm [-fiRr] file ...\n"));
162*7c478bd9Sstevel@tonic-gate 		exit(2);
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl)) {
166*7c478bd9Sstevel@tonic-gate 		perror("getrlimit");
167*7c478bd9Sstevel@tonic-gate 		exit(2);
168*7c478bd9Sstevel@tonic-gate 	} else
169*7c478bd9Sstevel@tonic-gate 		maxfiles = rl.rlim_cur - 2;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
172*7c478bd9Sstevel@tonic-gate 		rm(*argv, 1);
173*7c478bd9Sstevel@tonic-gate 		argv++;
174*7c478bd9Sstevel@tonic-gate 	}
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	cleanup();
177*7c478bd9Sstevel@tonic-gate 	return (errcode ? 2 : 0);
178*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate static void
182*7c478bd9Sstevel@tonic-gate rm(char *path, int first)
183*7c478bd9Sstevel@tonic-gate {
184*7c478bd9Sstevel@tonic-gate 	struct stat buffer;
185*7c478bd9Sstevel@tonic-gate 	char	*filepath;
186*7c478bd9Sstevel@tonic-gate 	char	*p;
187*7c478bd9Sstevel@tonic-gate 	char	resolved_path[PATH_MAX];
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	/*
190*7c478bd9Sstevel@tonic-gate 	 * Check file to see if it exists.
191*7c478bd9Sstevel@tonic-gate 	 */
192*7c478bd9Sstevel@tonic-gate 	if (lstat(path, &buffer) == FAIL) {
193*7c478bd9Sstevel@tonic-gate 		if (!silent) {
194*7c478bd9Sstevel@tonic-gate 			perror(path);
195*7c478bd9Sstevel@tonic-gate 			++errcode;
196*7c478bd9Sstevel@tonic-gate 		}
197*7c478bd9Sstevel@tonic-gate 		return;
198*7c478bd9Sstevel@tonic-gate 	}
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	/* prevent removal of / but allow removal of sym-links */
201*7c478bd9Sstevel@tonic-gate 	if (!S_ISLNK(buffer.st_mode) && realpath(path, resolved_path) != NULL &&
202*7c478bd9Sstevel@tonic-gate 	    strcmp(resolved_path, "/") == 0) {
203*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
204*7c478bd9Sstevel@tonic-gate 		    gettext("rm of %s is not allowed\n"), resolved_path);
205*7c478bd9Sstevel@tonic-gate 		errcode++;
206*7c478bd9Sstevel@tonic-gate 		return;
207*7c478bd9Sstevel@tonic-gate 	}
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	/* prevent removal of . or .. (directly) */
210*7c478bd9Sstevel@tonic-gate 	if (p = strrchr(path, '/'))
211*7c478bd9Sstevel@tonic-gate 		p++;
212*7c478bd9Sstevel@tonic-gate 	else
213*7c478bd9Sstevel@tonic-gate 		p = path;
214*7c478bd9Sstevel@tonic-gate 	if (strcmp(".", p) == 0 || strcmp("..", p) == 0) {
215*7c478bd9Sstevel@tonic-gate 		if (!silent) {
216*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
217*7c478bd9Sstevel@tonic-gate 			    gettext("rm of %s is not allowed\n"), path);
218*7c478bd9Sstevel@tonic-gate 			errcode++;
219*7c478bd9Sstevel@tonic-gate 		}
220*7c478bd9Sstevel@tonic-gate 		return;
221*7c478bd9Sstevel@tonic-gate 	}
222*7c478bd9Sstevel@tonic-gate 	/*
223*7c478bd9Sstevel@tonic-gate 	 * If it's a directory, remove its contents.
224*7c478bd9Sstevel@tonic-gate 	 */
225*7c478bd9Sstevel@tonic-gate 	if (DIRECTORY) {
226*7c478bd9Sstevel@tonic-gate 		/*
227*7c478bd9Sstevel@tonic-gate 		 * If "-r" wasn't specified, trying to remove directories
228*7c478bd9Sstevel@tonic-gate 		 * is an error.
229*7c478bd9Sstevel@tonic-gate 		 */
230*7c478bd9Sstevel@tonic-gate 		if (!recursive) {
231*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
232*7c478bd9Sstevel@tonic-gate 			    gettext("rm: %s is a directory\n"), path);
233*7c478bd9Sstevel@tonic-gate 			++errcode;
234*7c478bd9Sstevel@tonic-gate 			return;
235*7c478bd9Sstevel@tonic-gate 		}
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 		if (first_dir) {
238*7c478bd9Sstevel@tonic-gate 			check_homedir();
239*7c478bd9Sstevel@tonic-gate 			first_dir = 0;
240*7c478bd9Sstevel@tonic-gate 		}
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 		undir(path, first, buffer.st_dev, buffer.st_ino);
243*7c478bd9Sstevel@tonic-gate 		return;
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	filepath = get_filename(path);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	/*
249*7c478bd9Sstevel@tonic-gate 	 * If interactive, ask for acknowledgement.
250*7c478bd9Sstevel@tonic-gate 	 *
251*7c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE - The following message will contain the
252*7c478bd9Sstevel@tonic-gate 	 * first character of the strings for "yes" and "no" defined
253*7c478bd9Sstevel@tonic-gate 	 * in the file "nl_langinfo.po".  After substitution, the
254*7c478bd9Sstevel@tonic-gate 	 * message will appear as follows:
255*7c478bd9Sstevel@tonic-gate 	 *	rm: remove <filename> (y/n)?
256*7c478bd9Sstevel@tonic-gate 	 * For example, in German, this will appear as
257*7c478bd9Sstevel@tonic-gate 	 *	rm: l�schen <filename> (j/n)?
258*7c478bd9Sstevel@tonic-gate 	 * where j=ja, n=nein, <filename>=the file to be removed
259*7c478bd9Sstevel@tonic-gate 	 *
260*7c478bd9Sstevel@tonic-gate 	 */
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	if (interactive) {
264*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rm: remove %s (%s/%s)? "),
265*7c478bd9Sstevel@tonic-gate 			filepath, yeschr, nochr);
266*7c478bd9Sstevel@tonic-gate 		if (!yes()) {
267*7c478bd9Sstevel@tonic-gate 			free(filepath);
268*7c478bd9Sstevel@tonic-gate 			return;
269*7c478bd9Sstevel@tonic-gate 		}
270*7c478bd9Sstevel@tonic-gate 	} else if (!silent) {
271*7c478bd9Sstevel@tonic-gate 		/*
272*7c478bd9Sstevel@tonic-gate 		 * If not silent, and stdin is a terminal, and there's
273*7c478bd9Sstevel@tonic-gate 		 * no write access, and the file isn't a symbolic link,
274*7c478bd9Sstevel@tonic-gate 		 * ask for permission.
275*7c478bd9Sstevel@tonic-gate 		 *
276*7c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE - The following message will contain the
277*7c478bd9Sstevel@tonic-gate 		 * first character of the strings for "yes" and "no" defined
278*7c478bd9Sstevel@tonic-gate 		 * in the file "nl_langinfo.po".  After substitution, the
279*7c478bd9Sstevel@tonic-gate 		 * message will appear as follows:
280*7c478bd9Sstevel@tonic-gate 		 * 	rm: <filename>: override protection XXX (y/n)?
281*7c478bd9Sstevel@tonic-gate 		 * where XXX is the permission mode bits of the file in octal
282*7c478bd9Sstevel@tonic-gate 		 * and <filename> is the file to be removed
283*7c478bd9Sstevel@tonic-gate 		 *
284*7c478bd9Sstevel@tonic-gate 		 */
285*7c478bd9Sstevel@tonic-gate 		if (!SYMLINK && access(path, W_OK) == FAIL &&
286*7c478bd9Sstevel@tonic-gate 		    isatty(fileno(stdin))) {
287*7c478bd9Sstevel@tonic-gate 			(void) printf(
288*7c478bd9Sstevel@tonic-gate 			    gettext("rm: %s: override protection %o (%s/%s)? "),
289*7c478bd9Sstevel@tonic-gate 			    filepath, buffer.st_mode & 0777, yeschr, nochr);
290*7c478bd9Sstevel@tonic-gate 			/*
291*7c478bd9Sstevel@tonic-gate 			 * If permission isn't given, skip the file.
292*7c478bd9Sstevel@tonic-gate 			 */
293*7c478bd9Sstevel@tonic-gate 			if (!yes()) {
294*7c478bd9Sstevel@tonic-gate 				free(filepath);
295*7c478bd9Sstevel@tonic-gate 				return;
296*7c478bd9Sstevel@tonic-gate 			}
297*7c478bd9Sstevel@tonic-gate 		}
298*7c478bd9Sstevel@tonic-gate 	}
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 	/*
301*7c478bd9Sstevel@tonic-gate 	 * If the unlink fails, inform the user. For /usr/bin/rm, only inform
302*7c478bd9Sstevel@tonic-gate 	 * the user if interactive or not silent.
303*7c478bd9Sstevel@tonic-gate 	 * If unlink fails with errno = ENOENT because file was removed
304*7c478bd9Sstevel@tonic-gate 	 * in between the lstat call and unlink don't inform the user and
305*7c478bd9Sstevel@tonic-gate 	 * don't change errcode.
306*7c478bd9Sstevel@tonic-gate 	 */
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 	if (unlink(path) == FAIL) {
309*7c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
310*7c478bd9Sstevel@tonic-gate 			free(filepath);
311*7c478bd9Sstevel@tonic-gate 			return;
312*7c478bd9Sstevel@tonic-gate 		}
313*7c478bd9Sstevel@tonic-gate #ifndef XPG4
314*7c478bd9Sstevel@tonic-gate 		if (!silent || interactive) {
315*7c478bd9Sstevel@tonic-gate #endif
316*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
317*7c478bd9Sstevel@tonic-gate 				    gettext("rm: %s not removed: "), filepath);
318*7c478bd9Sstevel@tonic-gate 				perror("");
319*7c478bd9Sstevel@tonic-gate #ifndef XPG4
320*7c478bd9Sstevel@tonic-gate 		}
321*7c478bd9Sstevel@tonic-gate #endif
322*7c478bd9Sstevel@tonic-gate 		++errcode;
323*7c478bd9Sstevel@tonic-gate 	}
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	free(filepath);
326*7c478bd9Sstevel@tonic-gate }
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate static void
329*7c478bd9Sstevel@tonic-gate undir(char *path, int first, dev_t dev, ino_t ino)
330*7c478bd9Sstevel@tonic-gate {
331*7c478bd9Sstevel@tonic-gate 	char	*newpath;
332*7c478bd9Sstevel@tonic-gate 	DIR	*name;
333*7c478bd9Sstevel@tonic-gate 	struct dirent *direct;
334*7c478bd9Sstevel@tonic-gate 	int	ismypath;
335*7c478bd9Sstevel@tonic-gate 	int	chdir_failed = 0;
336*7c478bd9Sstevel@tonic-gate 	size_t	len;
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	push_name(path, first);
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	/*
341*7c478bd9Sstevel@tonic-gate 	 * If interactive and this file isn't in the path of the
342*7c478bd9Sstevel@tonic-gate 	 * current working directory, ask for acknowledgement.
343*7c478bd9Sstevel@tonic-gate 	 *
344*7c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE - The following message will contain the
345*7c478bd9Sstevel@tonic-gate 	 * first character of the strings for "yes" and "no" defined
346*7c478bd9Sstevel@tonic-gate 	 * in the file "nl_langinfo.po".  After substitution, the
347*7c478bd9Sstevel@tonic-gate 	 * message will appear as follows:
348*7c478bd9Sstevel@tonic-gate 	 *	rm: examine files in directory <directoryname> (y/n)?
349*7c478bd9Sstevel@tonic-gate 	 * where <directoryname> is the directory to be removed
350*7c478bd9Sstevel@tonic-gate 	 *
351*7c478bd9Sstevel@tonic-gate 	 */
352*7c478bd9Sstevel@tonic-gate 	ismypath = mypath(dev, ino);
353*7c478bd9Sstevel@tonic-gate 	if (interactive) {
354*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
355*7c478bd9Sstevel@tonic-gate 		    gettext("rm: examine files in directory %s (%s/%s)? "),
356*7c478bd9Sstevel@tonic-gate 			fullpath, yeschr, nochr);
357*7c478bd9Sstevel@tonic-gate 		/*
358*7c478bd9Sstevel@tonic-gate 		 * If the answer is no, skip the directory.
359*7c478bd9Sstevel@tonic-gate 		 */
360*7c478bd9Sstevel@tonic-gate 		if (!yes()) {
361*7c478bd9Sstevel@tonic-gate 			pop_name(first);
362*7c478bd9Sstevel@tonic-gate 			return;
363*7c478bd9Sstevel@tonic-gate 		}
364*7c478bd9Sstevel@tonic-gate 	}
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate #ifdef XPG4
367*7c478bd9Sstevel@tonic-gate 	/*
368*7c478bd9Sstevel@tonic-gate 	 * XCU4 and POSIX.2: If not interactive and file is not in the
369*7c478bd9Sstevel@tonic-gate 	 * path of the current working directory, check to see whether
370*7c478bd9Sstevel@tonic-gate 	 * or not directory is readable or writable and if not,
371*7c478bd9Sstevel@tonic-gate 	 * prompt user for response.
372*7c478bd9Sstevel@tonic-gate 	 */
373*7c478bd9Sstevel@tonic-gate 	if (!interactive && !ismypath &&
374*7c478bd9Sstevel@tonic-gate 	    (access(path, W_OK|X_OK) == FAIL) && isatty(fileno(stdin))) {
375*7c478bd9Sstevel@tonic-gate 		if (!silent) {
376*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
377*7c478bd9Sstevel@tonic-gate 			    gettext(
378*7c478bd9Sstevel@tonic-gate 				"rm: examine files in directory %s (%s/%s)? "),
379*7c478bd9Sstevel@tonic-gate 			    fullpath, yeschr, nochr);
380*7c478bd9Sstevel@tonic-gate 			/*
381*7c478bd9Sstevel@tonic-gate 			 * If the answer is no, skip the directory.
382*7c478bd9Sstevel@tonic-gate 			 */
383*7c478bd9Sstevel@tonic-gate 			if (!yes()) {
384*7c478bd9Sstevel@tonic-gate 				pop_name(first);
385*7c478bd9Sstevel@tonic-gate 				return;
386*7c478bd9Sstevel@tonic-gate 			}
387*7c478bd9Sstevel@tonic-gate 		}
388*7c478bd9Sstevel@tonic-gate 	}
389*7c478bd9Sstevel@tonic-gate #endif
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate 	/*
392*7c478bd9Sstevel@tonic-gate 	 * Open the directory for reading.
393*7c478bd9Sstevel@tonic-gate 	 */
394*7c478bd9Sstevel@tonic-gate 	if ((name = opendir(path)) == NULL) {
395*7c478bd9Sstevel@tonic-gate 		int	saveerrno = errno;
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 		/*
398*7c478bd9Sstevel@tonic-gate 		 * If interactive, ask for acknowledgement.
399*7c478bd9Sstevel@tonic-gate 		 */
400*7c478bd9Sstevel@tonic-gate 		if (interactive) {
401*7c478bd9Sstevel@tonic-gate 			/*
402*7c478bd9Sstevel@tonic-gate 			 * Print an error message that
403*7c478bd9Sstevel@tonic-gate 			 * we could not read the directory
404*7c478bd9Sstevel@tonic-gate 			 * as the user wanted to examine
405*7c478bd9Sstevel@tonic-gate 			 * files in the directory.  Only
406*7c478bd9Sstevel@tonic-gate 			 * affect the error status if
407*7c478bd9Sstevel@tonic-gate 			 * user doesn't want to remove the
408*7c478bd9Sstevel@tonic-gate 			 * directory as we still may be able
409*7c478bd9Sstevel@tonic-gate 			 * remove the directory successfully.
410*7c478bd9Sstevel@tonic-gate 			 */
411*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
412*7c478bd9Sstevel@tonic-gate 			    "rm: cannot read directory %s: "),
413*7c478bd9Sstevel@tonic-gate 			    fullpath);
414*7c478bd9Sstevel@tonic-gate 			errno = saveerrno;
415*7c478bd9Sstevel@tonic-gate 			perror("");
416*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
417*7c478bd9Sstevel@tonic-gate 			    "rm: remove %s: (%s/%s)? "),
418*7c478bd9Sstevel@tonic-gate 			    fullpath, yeschr, nochr);
419*7c478bd9Sstevel@tonic-gate 			if (!yes()) {
420*7c478bd9Sstevel@tonic-gate 				++errcode;
421*7c478bd9Sstevel@tonic-gate 				pop_name(first);
422*7c478bd9Sstevel@tonic-gate 				return;
423*7c478bd9Sstevel@tonic-gate 			}
424*7c478bd9Sstevel@tonic-gate 		}
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 		/*
427*7c478bd9Sstevel@tonic-gate 		 * If the directory is empty, we may be able to
428*7c478bd9Sstevel@tonic-gate 		 * go ahead and remove it.
429*7c478bd9Sstevel@tonic-gate 		 */
430*7c478bd9Sstevel@tonic-gate 		if (rmdir(path) == FAIL) {
431*7c478bd9Sstevel@tonic-gate 			if (interactive) {
432*7c478bd9Sstevel@tonic-gate 				int	rmdirerr = errno;
433*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
434*7c478bd9Sstevel@tonic-gate 				    "rm: Unable to remove directory %s: "),
435*7c478bd9Sstevel@tonic-gate 				    fullpath);
436*7c478bd9Sstevel@tonic-gate 				errno = rmdirerr;
437*7c478bd9Sstevel@tonic-gate 				perror("");
438*7c478bd9Sstevel@tonic-gate 			} else {
439*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
440*7c478bd9Sstevel@tonic-gate 				    "rm: cannot read directory %s: "),
441*7c478bd9Sstevel@tonic-gate 				    fullpath);
442*7c478bd9Sstevel@tonic-gate 				errno = saveerrno;
443*7c478bd9Sstevel@tonic-gate 				perror("");
444*7c478bd9Sstevel@tonic-gate 			}
445*7c478bd9Sstevel@tonic-gate 			++errcode;
446*7c478bd9Sstevel@tonic-gate 		}
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 		/* Continue to next file/directory rather than exit */
449*7c478bd9Sstevel@tonic-gate 		pop_name(first);
450*7c478bd9Sstevel@tonic-gate 		return;
451*7c478bd9Sstevel@tonic-gate 	}
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate 	/*
454*7c478bd9Sstevel@tonic-gate 	 * XCU4 requires that rm -r descend the directory
455*7c478bd9Sstevel@tonic-gate 	 * hierarchy without regard to PATH_MAX.  If we can't
456*7c478bd9Sstevel@tonic-gate 	 * chdir() do not increment error counter and do not
457*7c478bd9Sstevel@tonic-gate 	 * print message.
458*7c478bd9Sstevel@tonic-gate 	 *
459*7c478bd9Sstevel@tonic-gate 	 * However, if we cannot chdir because someone has taken away
460*7c478bd9Sstevel@tonic-gate 	 * execute access we may still be able to delete the directory
461*7c478bd9Sstevel@tonic-gate 	 * if it's empty. The old rm could do this.
462*7c478bd9Sstevel@tonic-gate 	 */
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	if (chdir(path) == -1) {
465*7c478bd9Sstevel@tonic-gate 		chdir_failed = 1;
466*7c478bd9Sstevel@tonic-gate 	}
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 	/*
469*7c478bd9Sstevel@tonic-gate 	 * Read every directory entry.
470*7c478bd9Sstevel@tonic-gate 	 */
471*7c478bd9Sstevel@tonic-gate 	while ((direct = readdir(name)) != NULL) {
472*7c478bd9Sstevel@tonic-gate 		/*
473*7c478bd9Sstevel@tonic-gate 		 * Ignore "." and ".." entries.
474*7c478bd9Sstevel@tonic-gate 		 */
475*7c478bd9Sstevel@tonic-gate 		if (strcmp(direct->d_name, ".") == 0 ||
476*7c478bd9Sstevel@tonic-gate 		    strcmp(direct->d_name, "..") == 0)
477*7c478bd9Sstevel@tonic-gate 			continue;
478*7c478bd9Sstevel@tonic-gate 		/*
479*7c478bd9Sstevel@tonic-gate 		 * Try to remove the file.
480*7c478bd9Sstevel@tonic-gate 		 */
481*7c478bd9Sstevel@tonic-gate 		len = strlen(direct->d_name) + 1;
482*7c478bd9Sstevel@tonic-gate 		if (chdir_failed) {
483*7c478bd9Sstevel@tonic-gate 			len += strlen(path) + 2;
484*7c478bd9Sstevel@tonic-gate 		}
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 		newpath = malloc(len);
487*7c478bd9Sstevel@tonic-gate 		if (newpath == NULL) {
488*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
489*7c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
490*7c478bd9Sstevel@tonic-gate 			cleanup();
491*7c478bd9Sstevel@tonic-gate 			exit(1);
492*7c478bd9Sstevel@tonic-gate 		}
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate 		if (!chdir_failed) {
495*7c478bd9Sstevel@tonic-gate 			(void) strcpy(newpath, direct->d_name);
496*7c478bd9Sstevel@tonic-gate 		} else {
497*7c478bd9Sstevel@tonic-gate 			(void) snprintf(newpath, len, "%s/%s",
498*7c478bd9Sstevel@tonic-gate 			    path, direct->d_name);
499*7c478bd9Sstevel@tonic-gate 		}
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 		/*
503*7c478bd9Sstevel@tonic-gate 		 * If a spare file descriptor is available, just call the
504*7c478bd9Sstevel@tonic-gate 		 * "rm" function with the file name; otherwise close the
505*7c478bd9Sstevel@tonic-gate 		 * directory and reopen it when the child is removed.
506*7c478bd9Sstevel@tonic-gate 		 */
507*7c478bd9Sstevel@tonic-gate 		if (name->dd_fd >= maxfiles) {
508*7c478bd9Sstevel@tonic-gate 			(void) closedir(name);
509*7c478bd9Sstevel@tonic-gate 			rm(newpath, 0);
510*7c478bd9Sstevel@tonic-gate 			if (!chdir_failed)
511*7c478bd9Sstevel@tonic-gate 				name = opendir(".");
512*7c478bd9Sstevel@tonic-gate 			else
513*7c478bd9Sstevel@tonic-gate 				name = opendir(path);
514*7c478bd9Sstevel@tonic-gate 			if (name == NULL) {
515*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
516*7c478bd9Sstevel@tonic-gate 				    gettext("rm: cannot read directory %s: "),
517*7c478bd9Sstevel@tonic-gate 				    fullpath);
518*7c478bd9Sstevel@tonic-gate 				perror("");
519*7c478bd9Sstevel@tonic-gate 				cleanup();
520*7c478bd9Sstevel@tonic-gate 				exit(2);
521*7c478bd9Sstevel@tonic-gate 			}
522*7c478bd9Sstevel@tonic-gate 		} else
523*7c478bd9Sstevel@tonic-gate 			rm(newpath, 0);
524*7c478bd9Sstevel@tonic-gate 
525*7c478bd9Sstevel@tonic-gate 		free(newpath);
526*7c478bd9Sstevel@tonic-gate 	}
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 	/*
529*7c478bd9Sstevel@tonic-gate 	 * Close the directory we just finished reading.
530*7c478bd9Sstevel@tonic-gate 	 */
531*7c478bd9Sstevel@tonic-gate 	(void) closedir(name);
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate 	/*
534*7c478bd9Sstevel@tonic-gate 	 * The contents of the directory have been removed.  If the
535*7c478bd9Sstevel@tonic-gate 	 * directory itself is in the path of the current working
536*7c478bd9Sstevel@tonic-gate 	 * directory, don't try to remove it.
537*7c478bd9Sstevel@tonic-gate 	 * When the directory itself is the current working directory, mypath()
538*7c478bd9Sstevel@tonic-gate 	 * has a return code == 2.
539*7c478bd9Sstevel@tonic-gate 	 *
540*7c478bd9Sstevel@tonic-gate 	 * XCU4: Because we've descended the directory hierarchy in order
541*7c478bd9Sstevel@tonic-gate 	 * to avoid PATH_MAX limitation, we must now start ascending
542*7c478bd9Sstevel@tonic-gate 	 * one level at a time and remove files/directories.
543*7c478bd9Sstevel@tonic-gate 	 */
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate 	if (!chdir_failed) {
546*7c478bd9Sstevel@tonic-gate 		if (first)
547*7c478bd9Sstevel@tonic-gate 			chdir_home();
548*7c478bd9Sstevel@tonic-gate 		else if (chdir("..") == -1) {
549*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
550*7c478bd9Sstevel@tonic-gate 			    gettext("rm: cannot change to parent of "
551*7c478bd9Sstevel@tonic-gate 				    "directory %s: "),
552*7c478bd9Sstevel@tonic-gate 			    fullpath);
553*7c478bd9Sstevel@tonic-gate 			perror("");
554*7c478bd9Sstevel@tonic-gate 			cleanup();
555*7c478bd9Sstevel@tonic-gate 			exit(2);
556*7c478bd9Sstevel@tonic-gate 		}
557*7c478bd9Sstevel@tonic-gate 	}
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate 	switch (ismypath) {
560*7c478bd9Sstevel@tonic-gate 	case 3:
561*7c478bd9Sstevel@tonic-gate 		pop_name(first);
562*7c478bd9Sstevel@tonic-gate 		return;
563*7c478bd9Sstevel@tonic-gate 	case 2:
564*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
565*7c478bd9Sstevel@tonic-gate 		    gettext("rm: Cannot remove any directory in the path "
566*7c478bd9Sstevel@tonic-gate 			"of the current working directory\n%s\n"), fullpath);
567*7c478bd9Sstevel@tonic-gate 		++errcode;
568*7c478bd9Sstevel@tonic-gate 		pop_name(first);
569*7c478bd9Sstevel@tonic-gate 		return;
570*7c478bd9Sstevel@tonic-gate 	case 1:
571*7c478bd9Sstevel@tonic-gate 		++errcode;
572*7c478bd9Sstevel@tonic-gate 		pop_name(first);
573*7c478bd9Sstevel@tonic-gate 		return;
574*7c478bd9Sstevel@tonic-gate 	case 0:
575*7c478bd9Sstevel@tonic-gate 		break;
576*7c478bd9Sstevel@tonic-gate 	}
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate 	/*
579*7c478bd9Sstevel@tonic-gate 	 * If interactive, ask for acknowledgement.
580*7c478bd9Sstevel@tonic-gate 	 */
581*7c478bd9Sstevel@tonic-gate 	if (interactive) {
582*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rm: remove %s: (%s/%s)? "),
583*7c478bd9Sstevel@tonic-gate 			fullpath, yeschr, nochr);
584*7c478bd9Sstevel@tonic-gate 		if (!yes()) {
585*7c478bd9Sstevel@tonic-gate 			pop_name(first);
586*7c478bd9Sstevel@tonic-gate 			return;
587*7c478bd9Sstevel@tonic-gate 		}
588*7c478bd9Sstevel@tonic-gate 	}
589*7c478bd9Sstevel@tonic-gate 	if (rmdir(path) == FAIL) {
590*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
591*7c478bd9Sstevel@tonic-gate 			gettext("rm: Unable to remove directory %s: "),
592*7c478bd9Sstevel@tonic-gate 			fullpath);
593*7c478bd9Sstevel@tonic-gate 		perror("");
594*7c478bd9Sstevel@tonic-gate 		++errcode;
595*7c478bd9Sstevel@tonic-gate 	}
596*7c478bd9Sstevel@tonic-gate 	pop_name(first);
597*7c478bd9Sstevel@tonic-gate }
598*7c478bd9Sstevel@tonic-gate 
599*7c478bd9Sstevel@tonic-gate 
600*7c478bd9Sstevel@tonic-gate static int
601*7c478bd9Sstevel@tonic-gate yes(void)
602*7c478bd9Sstevel@tonic-gate {
603*7c478bd9Sstevel@tonic-gate 	int	i, b;
604*7c478bd9Sstevel@tonic-gate 	char	ans[SCHAR_MAX + 1];
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate 	for (i = 0; ; i++) {
607*7c478bd9Sstevel@tonic-gate 		b = getchar();
608*7c478bd9Sstevel@tonic-gate 		if (b == '\n' || b == '\0' || b == EOF) {
609*7c478bd9Sstevel@tonic-gate 			ans[i] = 0;
610*7c478bd9Sstevel@tonic-gate 			break;
611*7c478bd9Sstevel@tonic-gate 		}
612*7c478bd9Sstevel@tonic-gate 		if (i < SCHAR_MAX)
613*7c478bd9Sstevel@tonic-gate 			ans[i] = b;
614*7c478bd9Sstevel@tonic-gate 	}
615*7c478bd9Sstevel@tonic-gate 	if (i >= SCHAR_MAX) {
616*7c478bd9Sstevel@tonic-gate 		i = SCHAR_MAX;
617*7c478bd9Sstevel@tonic-gate 		ans[SCHAR_MAX] = 0;
618*7c478bd9Sstevel@tonic-gate 	}
619*7c478bd9Sstevel@tonic-gate 	if ((i == 0) | (strncmp(yeschr, ans, i)))
620*7c478bd9Sstevel@tonic-gate 		return (0);
621*7c478bd9Sstevel@tonic-gate 	return (1);
622*7c478bd9Sstevel@tonic-gate }
623*7c478bd9Sstevel@tonic-gate 
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate static int
626*7c478bd9Sstevel@tonic-gate mypath(dev_t dev, ino_t ino)
627*7c478bd9Sstevel@tonic-gate {
628*7c478bd9Sstevel@tonic-gate 	struct dir_id *curdir;
629*7c478bd9Sstevel@tonic-gate 
630*7c478bd9Sstevel@tonic-gate 	/*
631*7c478bd9Sstevel@tonic-gate 	 * Check to see if this is our current directory
632*7c478bd9Sstevel@tonic-gate 	 * Indicated by return 2;
633*7c478bd9Sstevel@tonic-gate 	 */
634*7c478bd9Sstevel@tonic-gate 	if (dev == homedir.dev && ino == homedir.inode) {
635*7c478bd9Sstevel@tonic-gate 		return (2);
636*7c478bd9Sstevel@tonic-gate 	}
637*7c478bd9Sstevel@tonic-gate 
638*7c478bd9Sstevel@tonic-gate 	curdir = homedir.next;
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate 	while (curdir != NULL) {
641*7c478bd9Sstevel@tonic-gate 		/*
642*7c478bd9Sstevel@tonic-gate 		 * If we find a match, the directory (dev, ino) passed to
643*7c478bd9Sstevel@tonic-gate 		 * mypath() is an ancestor of ours. Indicated by return 3.
644*7c478bd9Sstevel@tonic-gate 		 */
645*7c478bd9Sstevel@tonic-gate 		if (curdir->dev == dev && curdir->inode == ino)
646*7c478bd9Sstevel@tonic-gate 			return (3);
647*7c478bd9Sstevel@tonic-gate 		curdir = curdir->next;
648*7c478bd9Sstevel@tonic-gate 	}
649*7c478bd9Sstevel@tonic-gate 	/*
650*7c478bd9Sstevel@tonic-gate 	 * parent_err indicates we couldn't stat or chdir to
651*7c478bd9Sstevel@tonic-gate 	 * one of our parent dirs, so the linked list of dir_id structs
652*7c478bd9Sstevel@tonic-gate 	 * is incomplete
653*7c478bd9Sstevel@tonic-gate 	 */
654*7c478bd9Sstevel@tonic-gate 	if (parent_err) {
655*7c478bd9Sstevel@tonic-gate #ifndef XPG4
656*7c478bd9Sstevel@tonic-gate 		if (!silent || interactive) {
657*7c478bd9Sstevel@tonic-gate #endif
658*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("rm: cannot determine "
659*7c478bd9Sstevel@tonic-gate 			    "if this is an ancestor of the current "
660*7c478bd9Sstevel@tonic-gate 			    "working directory\n%s\n"), fullpath);
661*7c478bd9Sstevel@tonic-gate #ifndef XPG4
662*7c478bd9Sstevel@tonic-gate 		}
663*7c478bd9Sstevel@tonic-gate #endif
664*7c478bd9Sstevel@tonic-gate 		/* assume it is. least dangerous */
665*7c478bd9Sstevel@tonic-gate 		return (1);
666*7c478bd9Sstevel@tonic-gate 	}
667*7c478bd9Sstevel@tonic-gate 	return (0);
668*7c478bd9Sstevel@tonic-gate }
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate static int maxlen;
671*7c478bd9Sstevel@tonic-gate static int curlen;
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate static char *
674*7c478bd9Sstevel@tonic-gate get_filename(char *name)
675*7c478bd9Sstevel@tonic-gate {
676*7c478bd9Sstevel@tonic-gate 	char *path;
677*7c478bd9Sstevel@tonic-gate 	size_t len;
678*7c478bd9Sstevel@tonic-gate 
679*7c478bd9Sstevel@tonic-gate 	if (fullpath == NULL || *fullpath == '\0') {
680*7c478bd9Sstevel@tonic-gate 		path = strdup(name);
681*7c478bd9Sstevel@tonic-gate 		if (path == NULL) {
682*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
683*7c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
684*7c478bd9Sstevel@tonic-gate 			cleanup();
685*7c478bd9Sstevel@tonic-gate 			exit(1);
686*7c478bd9Sstevel@tonic-gate 		}
687*7c478bd9Sstevel@tonic-gate 	} else {
688*7c478bd9Sstevel@tonic-gate 		len = strlen(fullpath) + strlen(name) + 2;
689*7c478bd9Sstevel@tonic-gate 		path = malloc(len);
690*7c478bd9Sstevel@tonic-gate 		if (path == NULL) {
691*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
692*7c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
693*7c478bd9Sstevel@tonic-gate 			cleanup();
694*7c478bd9Sstevel@tonic-gate 			exit(1);
695*7c478bd9Sstevel@tonic-gate 		}
696*7c478bd9Sstevel@tonic-gate 		(void) snprintf(path, len, "%s/%s", fullpath, name);
697*7c478bd9Sstevel@tonic-gate 	}
698*7c478bd9Sstevel@tonic-gate 	return (path);
699*7c478bd9Sstevel@tonic-gate }
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate static void
702*7c478bd9Sstevel@tonic-gate push_name(char *name, int first)
703*7c478bd9Sstevel@tonic-gate {
704*7c478bd9Sstevel@tonic-gate 	int	namelen;
705*7c478bd9Sstevel@tonic-gate 
706*7c478bd9Sstevel@tonic-gate 	namelen = strlen(name) + 1; /* 1 for "/" */
707*7c478bd9Sstevel@tonic-gate 	if ((curlen + namelen) >= maxlen) {
708*7c478bd9Sstevel@tonic-gate 		maxlen += PATH_MAX;
709*7c478bd9Sstevel@tonic-gate 		fullpath = (char *)realloc(fullpath, (size_t)(maxlen + 1));
710*7c478bd9Sstevel@tonic-gate 	}
711*7c478bd9Sstevel@tonic-gate 	if (first) {
712*7c478bd9Sstevel@tonic-gate 		(void) strcpy(fullpath, name);
713*7c478bd9Sstevel@tonic-gate 	} else {
714*7c478bd9Sstevel@tonic-gate 		(void) strcat(fullpath, "/");
715*7c478bd9Sstevel@tonic-gate 		(void) strcat(fullpath, name);
716*7c478bd9Sstevel@tonic-gate 	}
717*7c478bd9Sstevel@tonic-gate 	curlen = strlen(fullpath);
718*7c478bd9Sstevel@tonic-gate }
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate static void
721*7c478bd9Sstevel@tonic-gate pop_name(int first)
722*7c478bd9Sstevel@tonic-gate {
723*7c478bd9Sstevel@tonic-gate 	char *slash;
724*7c478bd9Sstevel@tonic-gate 
725*7c478bd9Sstevel@tonic-gate 	if (first) {
726*7c478bd9Sstevel@tonic-gate 		*fullpath = '\0';
727*7c478bd9Sstevel@tonic-gate 		return;
728*7c478bd9Sstevel@tonic-gate 	}
729*7c478bd9Sstevel@tonic-gate 	slash = strrchr(fullpath, '/');
730*7c478bd9Sstevel@tonic-gate 	if (slash)
731*7c478bd9Sstevel@tonic-gate 		*slash = '\0';
732*7c478bd9Sstevel@tonic-gate 	else
733*7c478bd9Sstevel@tonic-gate 		*fullpath = '\0';
734*7c478bd9Sstevel@tonic-gate 	curlen = strlen(fullpath);
735*7c478bd9Sstevel@tonic-gate }
736*7c478bd9Sstevel@tonic-gate 
737*7c478bd9Sstevel@tonic-gate static void
738*7c478bd9Sstevel@tonic-gate force_chdir(char *dirname)
739*7c478bd9Sstevel@tonic-gate {
740*7c478bd9Sstevel@tonic-gate 	char 	*pathname, *mp, *tp;
741*7c478bd9Sstevel@tonic-gate 
742*7c478bd9Sstevel@tonic-gate 	/* use pathname instead of dirname, so dirname won't be modified */
743*7c478bd9Sstevel@tonic-gate 	if ((pathname = strdup(dirname)) == NULL) {
744*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rm: strdup: "));
745*7c478bd9Sstevel@tonic-gate 		perror("");
746*7c478bd9Sstevel@tonic-gate 		cleanup();
747*7c478bd9Sstevel@tonic-gate 		exit(2);
748*7c478bd9Sstevel@tonic-gate 	}
749*7c478bd9Sstevel@tonic-gate 
750*7c478bd9Sstevel@tonic-gate 	/* pathname is an absolute full path from getcwd() */
751*7c478bd9Sstevel@tonic-gate 	mp = pathname;
752*7c478bd9Sstevel@tonic-gate 	while (mp) {
753*7c478bd9Sstevel@tonic-gate 		tp = strchr(mp, '/');
754*7c478bd9Sstevel@tonic-gate 		if (strlen(mp) >= PATH_MAX) {
755*7c478bd9Sstevel@tonic-gate 			/*
756*7c478bd9Sstevel@tonic-gate 			 * after the first iteration through this
757*7c478bd9Sstevel@tonic-gate 			 * loop, the below will NULL out the '/'
758*7c478bd9Sstevel@tonic-gate 			 * which follows the first dir on pathname
759*7c478bd9Sstevel@tonic-gate 			 */
760*7c478bd9Sstevel@tonic-gate 			*tp = 0;
761*7c478bd9Sstevel@tonic-gate 			tp++;
762*7c478bd9Sstevel@tonic-gate 			if (*mp == NULL)
763*7c478bd9Sstevel@tonic-gate 				ch_dir("/");
764*7c478bd9Sstevel@tonic-gate 			else
765*7c478bd9Sstevel@tonic-gate 				/*
766*7c478bd9Sstevel@tonic-gate 				 * mp points to the start of a dirname,
767*7c478bd9Sstevel@tonic-gate 				 * terminated by NULL, so ch_dir()
768*7c478bd9Sstevel@tonic-gate 				 * here will move down one directory
769*7c478bd9Sstevel@tonic-gate 				 */
770*7c478bd9Sstevel@tonic-gate 				ch_dir(mp);
771*7c478bd9Sstevel@tonic-gate 			/*
772*7c478bd9Sstevel@tonic-gate 			 * reset mp to the start of the dirname
773*7c478bd9Sstevel@tonic-gate 			 * which follows the one we just chdir'd to
774*7c478bd9Sstevel@tonic-gate 			 */
775*7c478bd9Sstevel@tonic-gate 			mp = tp;
776*7c478bd9Sstevel@tonic-gate 			continue;	/* probably can remove this */
777*7c478bd9Sstevel@tonic-gate 		} else {
778*7c478bd9Sstevel@tonic-gate 			ch_dir(mp);
779*7c478bd9Sstevel@tonic-gate 			break;
780*7c478bd9Sstevel@tonic-gate 		}
781*7c478bd9Sstevel@tonic-gate 	}
782*7c478bd9Sstevel@tonic-gate 	free(pathname);
783*7c478bd9Sstevel@tonic-gate }
784*7c478bd9Sstevel@tonic-gate 
785*7c478bd9Sstevel@tonic-gate static void
786*7c478bd9Sstevel@tonic-gate ch_dir(char *dirname)
787*7c478bd9Sstevel@tonic-gate {
788*7c478bd9Sstevel@tonic-gate 	if (chdir(dirname) == -1) {
789*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
790*7c478bd9Sstevel@tonic-gate 		gettext("rm: cannot change to %s directory: "), dirname);
791*7c478bd9Sstevel@tonic-gate 			perror("");
792*7c478bd9Sstevel@tonic-gate 			cleanup();
793*7c478bd9Sstevel@tonic-gate 			exit(2);
794*7c478bd9Sstevel@tonic-gate 	}
795*7c478bd9Sstevel@tonic-gate }
796*7c478bd9Sstevel@tonic-gate 
797*7c478bd9Sstevel@tonic-gate static void
798*7c478bd9Sstevel@tonic-gate chdir_home(void)
799*7c478bd9Sstevel@tonic-gate {
800*7c478bd9Sstevel@tonic-gate 	/*
801*7c478bd9Sstevel@tonic-gate 	 * Go back to home dir--the dir from where rm was executed--using
802*7c478bd9Sstevel@tonic-gate 	 * one of two methods, depending on which method works
803*7c478bd9Sstevel@tonic-gate 	 * for the given permissions of the home dir and its
804*7c478bd9Sstevel@tonic-gate 	 * parent directories.
805*7c478bd9Sstevel@tonic-gate 	 */
806*7c478bd9Sstevel@tonic-gate 	if (homedirfd != -1) {
807*7c478bd9Sstevel@tonic-gate 		if (fchdir(homedirfd) == -1) {
808*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
809*7c478bd9Sstevel@tonic-gate 			    gettext("rm: cannot change to starting "
810*7c478bd9Sstevel@tonic-gate 			    "directory: "));
811*7c478bd9Sstevel@tonic-gate 			perror("");
812*7c478bd9Sstevel@tonic-gate 			cleanup();
813*7c478bd9Sstevel@tonic-gate 			exit(2);
814*7c478bd9Sstevel@tonic-gate 		}
815*7c478bd9Sstevel@tonic-gate 	} else {
816*7c478bd9Sstevel@tonic-gate 		if (strlen(cwd) < PATH_MAX)
817*7c478bd9Sstevel@tonic-gate 			ch_dir(cwd);
818*7c478bd9Sstevel@tonic-gate 		else
819*7c478bd9Sstevel@tonic-gate 			force_chdir(cwd);
820*7c478bd9Sstevel@tonic-gate 	}
821*7c478bd9Sstevel@tonic-gate }
822*7c478bd9Sstevel@tonic-gate 
823*7c478bd9Sstevel@tonic-gate /*
824*7c478bd9Sstevel@tonic-gate  * check_homedir -
825*7c478bd9Sstevel@tonic-gate  * is only called the first time rm tries to
826*7c478bd9Sstevel@tonic-gate  * remove a directory.  It saves the current directory, i.e.,
827*7c478bd9Sstevel@tonic-gate  * home dir, so we can go back to it after traversing elsewhere.
828*7c478bd9Sstevel@tonic-gate  * It also saves all the device and inode numbers of each
829*7c478bd9Sstevel@tonic-gate  * dir from the home dir back to the root in a linked list, so we
830*7c478bd9Sstevel@tonic-gate  * can later check, via mypath(), if we are trying to remove our current
831*7c478bd9Sstevel@tonic-gate  * dir or an ancestor.
832*7c478bd9Sstevel@tonic-gate  */
833*7c478bd9Sstevel@tonic-gate static void
834*7c478bd9Sstevel@tonic-gate check_homedir(void)
835*7c478bd9Sstevel@tonic-gate {
836*7c478bd9Sstevel@tonic-gate 	int	size;	/* size allocated for pathname of home dir (cwd) */
837*7c478bd9Sstevel@tonic-gate 	struct stat buffer;
838*7c478bd9Sstevel@tonic-gate 	struct dir_id *lastdir, *curdir;
839*7c478bd9Sstevel@tonic-gate 
840*7c478bd9Sstevel@tonic-gate 	/*
841*7c478bd9Sstevel@tonic-gate 	 * We need to save where we currently are (the "home dir") so
842*7c478bd9Sstevel@tonic-gate 	 * we can return after traversing down directories we're
843*7c478bd9Sstevel@tonic-gate 	 * removing.  Two methods are attempted:
844*7c478bd9Sstevel@tonic-gate 	 *
845*7c478bd9Sstevel@tonic-gate 	 * 1) open() the home dir so we can use the fd
846*7c478bd9Sstevel@tonic-gate 	 *    to fchdir() back.  This requires read permission
847*7c478bd9Sstevel@tonic-gate 	 *    on the home dir.
848*7c478bd9Sstevel@tonic-gate 	 *
849*7c478bd9Sstevel@tonic-gate 	 * 2) getcwd() so we can chdir() to go back.  This
850*7c478bd9Sstevel@tonic-gate 	 *    requires search (x) permission on the home dir,
851*7c478bd9Sstevel@tonic-gate 	 *    and read and search permission on all parent dirs.  Also,
852*7c478bd9Sstevel@tonic-gate 	 *    getcwd() will not work if the home dir is > 341
853*7c478bd9Sstevel@tonic-gate 	 *    directories deep (see open bugid 4033182 - getcwd needs
854*7c478bd9Sstevel@tonic-gate 	 *    to work for pathnames of any depth).
855*7c478bd9Sstevel@tonic-gate 	 *
856*7c478bd9Sstevel@tonic-gate 	 * If neither method works, we can't remove any directories
857*7c478bd9Sstevel@tonic-gate 	 * and rm will fail.
858*7c478bd9Sstevel@tonic-gate 	 *
859*7c478bd9Sstevel@tonic-gate 	 * For future enhancement, a possible 3rd option to use
860*7c478bd9Sstevel@tonic-gate 	 * would be to fork a process to remove a directory,
861*7c478bd9Sstevel@tonic-gate 	 * eliminating the need to chdir back to the home directory
862*7c478bd9Sstevel@tonic-gate 	 * and eliminating the permission restrictions on the home dir
863*7c478bd9Sstevel@tonic-gate 	 * or its parent dirs.
864*7c478bd9Sstevel@tonic-gate 	 */
865*7c478bd9Sstevel@tonic-gate 	homedirfd = open(".", O_RDONLY);
866*7c478bd9Sstevel@tonic-gate 	if (homedirfd == -1) {
867*7c478bd9Sstevel@tonic-gate 		size = PATH_MAX;
868*7c478bd9Sstevel@tonic-gate 		while ((cwd = getcwd(NULL, size)) == NULL) {
869*7c478bd9Sstevel@tonic-gate 			if (errno == ERANGE) {
870*7c478bd9Sstevel@tonic-gate 				size = PATH_MAX + size;
871*7c478bd9Sstevel@tonic-gate 				continue;
872*7c478bd9Sstevel@tonic-gate 			} else {
873*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
874*7c478bd9Sstevel@tonic-gate 				    gettext("rm: cannot open starting "
875*7c478bd9Sstevel@tonic-gate 				    "directory: "));
876*7c478bd9Sstevel@tonic-gate 				perror("pwd");
877*7c478bd9Sstevel@tonic-gate 				exit(2);
878*7c478bd9Sstevel@tonic-gate 			}
879*7c478bd9Sstevel@tonic-gate 		}
880*7c478bd9Sstevel@tonic-gate 	}
881*7c478bd9Sstevel@tonic-gate 
882*7c478bd9Sstevel@tonic-gate 	/*
883*7c478bd9Sstevel@tonic-gate 	 * since we exit on error here, we're guaranteed to at least
884*7c478bd9Sstevel@tonic-gate 	 * have info in the first dir_id struct, homedir
885*7c478bd9Sstevel@tonic-gate 	 */
886*7c478bd9Sstevel@tonic-gate 	if (stat(".", &buffer) == -1) {
887*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
888*7c478bd9Sstevel@tonic-gate 		    gettext("rm: cannot stat current directory: "));
889*7c478bd9Sstevel@tonic-gate 		perror("");
890*7c478bd9Sstevel@tonic-gate 		exit(2);
891*7c478bd9Sstevel@tonic-gate 	}
892*7c478bd9Sstevel@tonic-gate 	homedir.dev = buffer.st_dev;
893*7c478bd9Sstevel@tonic-gate 	homedir.inode = buffer.st_ino;
894*7c478bd9Sstevel@tonic-gate 	homedir.next = NULL;
895*7c478bd9Sstevel@tonic-gate 
896*7c478bd9Sstevel@tonic-gate 	lastdir = &homedir;
897*7c478bd9Sstevel@tonic-gate 	/*
898*7c478bd9Sstevel@tonic-gate 	 * Starting from current working directory, walk toward the
899*7c478bd9Sstevel@tonic-gate 	 * root, looking at each directory along the way.
900*7c478bd9Sstevel@tonic-gate 	 */
901*7c478bd9Sstevel@tonic-gate 	for (;;) {
902*7c478bd9Sstevel@tonic-gate 		if (chdir("..") == -1 || lstat(".", &buffer) == -1) {
903*7c478bd9Sstevel@tonic-gate 			parent_err = 1;
904*7c478bd9Sstevel@tonic-gate 			break;
905*7c478bd9Sstevel@tonic-gate 		}
906*7c478bd9Sstevel@tonic-gate 
907*7c478bd9Sstevel@tonic-gate 		if ((lastdir->next = malloc(sizeof (struct dir_id))) ==
908*7c478bd9Sstevel@tonic-gate 		    NULL) {
909*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
910*7c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
911*7c478bd9Sstevel@tonic-gate 			cleanup();
912*7c478bd9Sstevel@tonic-gate 			exit(1);
913*7c478bd9Sstevel@tonic-gate 		}
914*7c478bd9Sstevel@tonic-gate 
915*7c478bd9Sstevel@tonic-gate 		curdir = lastdir->next;
916*7c478bd9Sstevel@tonic-gate 		curdir->dev = buffer.st_dev;
917*7c478bd9Sstevel@tonic-gate 		curdir->inode = buffer.st_ino;
918*7c478bd9Sstevel@tonic-gate 		curdir->next = NULL;
919*7c478bd9Sstevel@tonic-gate 
920*7c478bd9Sstevel@tonic-gate 		/*
921*7c478bd9Sstevel@tonic-gate 		 * Stop when we reach the root; note that chdir("..")
922*7c478bd9Sstevel@tonic-gate 		 * at the root dir will stay in root. Get rid of
923*7c478bd9Sstevel@tonic-gate 		 * the redundant dir_id struct for root.
924*7c478bd9Sstevel@tonic-gate 		 */
925*7c478bd9Sstevel@tonic-gate 		if (curdir->dev == lastdir->dev && curdir->inode ==
926*7c478bd9Sstevel@tonic-gate 		    lastdir->inode) {
927*7c478bd9Sstevel@tonic-gate 			lastdir->next = NULL;
928*7c478bd9Sstevel@tonic-gate 			free(curdir);
929*7c478bd9Sstevel@tonic-gate 			break;
930*7c478bd9Sstevel@tonic-gate 		}
931*7c478bd9Sstevel@tonic-gate 
932*7c478bd9Sstevel@tonic-gate 			/* loop again to go back another level */
933*7c478bd9Sstevel@tonic-gate 		lastdir = curdir;
934*7c478bd9Sstevel@tonic-gate 	}
935*7c478bd9Sstevel@tonic-gate 		/* go back to home directory */
936*7c478bd9Sstevel@tonic-gate 	chdir_home();
937*7c478bd9Sstevel@tonic-gate }
938*7c478bd9Sstevel@tonic-gate 
939*7c478bd9Sstevel@tonic-gate /*
940*7c478bd9Sstevel@tonic-gate  * cleanup the dynamically-allocated list of device numbers and inodes,
941*7c478bd9Sstevel@tonic-gate  * if any.  If homedir was never used, it is external and static so
942*7c478bd9Sstevel@tonic-gate  * it is guaranteed initialized to zero, thus homedir.next would be NULL.
943*7c478bd9Sstevel@tonic-gate  */
944*7c478bd9Sstevel@tonic-gate 
945*7c478bd9Sstevel@tonic-gate static void
946*7c478bd9Sstevel@tonic-gate cleanup(void) {
947*7c478bd9Sstevel@tonic-gate 
948*7c478bd9Sstevel@tonic-gate 	struct dir_id *lastdir, *curdir;
949*7c478bd9Sstevel@tonic-gate 
950*7c478bd9Sstevel@tonic-gate 	curdir = homedir.next;
951*7c478bd9Sstevel@tonic-gate 
952*7c478bd9Sstevel@tonic-gate 	while (curdir != NULL) {
953*7c478bd9Sstevel@tonic-gate 		lastdir = curdir;
954*7c478bd9Sstevel@tonic-gate 		curdir = curdir->next;
955*7c478bd9Sstevel@tonic-gate 		free(lastdir);
956*7c478bd9Sstevel@tonic-gate 	}
957*7c478bd9Sstevel@tonic-gate }
958