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