xref: /titanic_44/usr/src/cmd/rm/rm.c (revision 12a9e0ef0ee7b7a72de9fba28fed51e452ea43b0)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
579033acbSas145665  * Common Development and Distribution License (the "License").
679033acbSas145665  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
2379033acbSas145665  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27014a7923Sas145665 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28014a7923Sas145665 /*	All Rights Reserved   */
29014a7923Sas145665 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * rm [-fiRr] file ...
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <fcntl.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <dirent.h>
427c478bd9Sstevel@tonic-gate #include <limits.h>
437c478bd9Sstevel@tonic-gate #include <locale.h>
447c478bd9Sstevel@tonic-gate #include <langinfo.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <errno.h>
487c478bd9Sstevel@tonic-gate #include <sys/resource.h>
49014a7923Sas145665 #include <sys/avl.h>
50014a7923Sas145665 #include <libcmdutils.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	ARGCNT		5		/* Number of arguments */
537c478bd9Sstevel@tonic-gate #define	CHILD		0
547c478bd9Sstevel@tonic-gate #define	DIRECTORY	((buffer.st_mode&S_IFMT) == S_IFDIR)
557c478bd9Sstevel@tonic-gate #define	SYMLINK		((buffer.st_mode&S_IFMT) == S_IFLNK)
567c478bd9Sstevel@tonic-gate #define	FAIL		-1
577c478bd9Sstevel@tonic-gate #define	MAXFORK		100		/* Maximum number of forking attempts */
587c478bd9Sstevel@tonic-gate #define	NAMESIZE	MAXNAMLEN + 1	/* "/" + (file name size) */
597c478bd9Sstevel@tonic-gate #define	TRUE		1
607c478bd9Sstevel@tonic-gate #define	FALSE		0
617c478bd9Sstevel@tonic-gate #define	WRITE		02
627c478bd9Sstevel@tonic-gate #define	SEARCH		07
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static	int	errcode;
657c478bd9Sstevel@tonic-gate static	int interactive, recursive, silent; /* flags for command line options */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static	void	rm(char *, int);
687c478bd9Sstevel@tonic-gate static	void	undir(char *, int, dev_t, ino_t);
697c478bd9Sstevel@tonic-gate static	int	yes(void);
707c478bd9Sstevel@tonic-gate static	int	mypath(dev_t, ino_t);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static	char	yeschr[SCHAR_MAX + 2];
737c478bd9Sstevel@tonic-gate static	char	nochr[SCHAR_MAX + 2];
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static char *fullpath;
76*12a9e0efSsn199410 static int initdirfd;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static void push_name(char *name, int first);
797c478bd9Sstevel@tonic-gate static void pop_name(int first);
807c478bd9Sstevel@tonic-gate static void force_chdir(char *);
817c478bd9Sstevel@tonic-gate static void ch_dir(char *);
827c478bd9Sstevel@tonic-gate static char *get_filename(char *name);
83*12a9e0efSsn199410 static void chdir_init(void);
84*12a9e0efSsn199410 static void check_initdir(void);
857c478bd9Sstevel@tonic-gate static void cleanup(void);
867c478bd9Sstevel@tonic-gate 
87*12a9e0efSsn199410 static char 	*cwd;		/* pathname of init dir, from getcwd() */
887c478bd9Sstevel@tonic-gate static rlim_t	maxfiles;	/* maximum number of open files */
897c478bd9Sstevel@tonic-gate static int	first_dir = 1;	/* flag set when first trying to remove a dir */
907c478bd9Sstevel@tonic-gate 	/* flag set when can't get dev/inode of a parent dir */
917c478bd9Sstevel@tonic-gate static int	parent_err = 0;
92014a7923Sas145665 static avl_tree_t *tree;	/* tree to keep track of nodes visited */
93*12a9e0efSsn199410 	/*
94*12a9e0efSsn199410 	 * flag set when an attempt to move subdirectories during execution
95*12a9e0efSsn199410 	 * of rm is discovered
96*12a9e0efSsn199410 	 */
97*12a9e0efSsn199410 static int	bad_chdir = 0;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate struct dir_id {
1007c478bd9Sstevel@tonic-gate 	dev_t	dev;
1017c478bd9Sstevel@tonic-gate 	ino_t	inode;
1027c478bd9Sstevel@tonic-gate 	struct dir_id *next;
1037c478bd9Sstevel@tonic-gate };
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/*
106*12a9e0efSsn199410 	 * initdir is the first of a linked list of structures
1077c478bd9Sstevel@tonic-gate 	 * containing unique identifying device and inode numbers for
108*12a9e0efSsn199410 	 * each directory, from the initial dir up to the root.
109*12a9e0efSsn199410 	 * current_dir is a pointer to the most recent directory pushed
110*12a9e0efSsn199410 	 * on during a recursive rm() call.
1117c478bd9Sstevel@tonic-gate 	 */
112*12a9e0efSsn199410 static struct dir_id initdir, *current_dir;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate int
1157c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	extern int	optind;
1187c478bd9Sstevel@tonic-gate 	int	errflg = 0;
1197c478bd9Sstevel@tonic-gate 	int	c;
1207c478bd9Sstevel@tonic-gate 	struct rlimit rl;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1237c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1247c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1257c478bd9Sstevel@tonic-gate #endif
1267c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	(void) strncpy(yeschr, nl_langinfo(YESSTR), SCHAR_MAX + 1);
1297c478bd9Sstevel@tonic-gate 	(void) strncpy(nochr, nl_langinfo(NOSTR), SCHAR_MAX + 1);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "frRi")) != EOF)
1327c478bd9Sstevel@tonic-gate 		switch (c) {
1337c478bd9Sstevel@tonic-gate 		case 'f':
1347c478bd9Sstevel@tonic-gate 			silent = TRUE;
1357c478bd9Sstevel@tonic-gate #ifdef XPG4
1367c478bd9Sstevel@tonic-gate 			interactive = FALSE;
1377c478bd9Sstevel@tonic-gate #endif
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 		case 'i':
1407c478bd9Sstevel@tonic-gate 			interactive = TRUE;
1417c478bd9Sstevel@tonic-gate #ifdef XPG4
1427c478bd9Sstevel@tonic-gate 			silent = FALSE;
1437c478bd9Sstevel@tonic-gate #endif
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		case 'r':
1467c478bd9Sstevel@tonic-gate 		case 'R':
1477c478bd9Sstevel@tonic-gate 			recursive = TRUE;
1487c478bd9Sstevel@tonic-gate 			break;
1497c478bd9Sstevel@tonic-gate 		case '?':
1507c478bd9Sstevel@tonic-gate 			errflg = 1;
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * For BSD compatibility allow '-' to delimit the end
1567c478bd9Sstevel@tonic-gate 	 * of options.  However, if options were already explicitly
1577c478bd9Sstevel@tonic-gate 	 * terminated with '--', then treat '-' literally: otherwise,
1587c478bd9Sstevel@tonic-gate 	 * "rm -- -" won't remove '-'.
1597c478bd9Sstevel@tonic-gate 	 */
1607c478bd9Sstevel@tonic-gate 	if (optind < argc &&
1617c478bd9Sstevel@tonic-gate 	    strcmp(argv[optind], "-") == 0 &&
1627c478bd9Sstevel@tonic-gate 	    strcmp(argv[optind - 1], "--") != 0)
1637c478bd9Sstevel@tonic-gate 		optind++;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	argc -= optind;
1667c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if ((argc < 1 && !silent) || errflg) {
1697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1707c478bd9Sstevel@tonic-gate 			gettext("usage: rm [-fiRr] file ...\n"));
1717c478bd9Sstevel@tonic-gate 		exit(2);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl)) {
1757c478bd9Sstevel@tonic-gate 		perror("getrlimit");
1767c478bd9Sstevel@tonic-gate 		exit(2);
1777c478bd9Sstevel@tonic-gate 	} else
1787c478bd9Sstevel@tonic-gate 		maxfiles = rl.rlim_cur - 2;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
181014a7923Sas145665 		tree = NULL;
1827c478bd9Sstevel@tonic-gate 		rm(*argv, 1);
183*12a9e0efSsn199410 		while (bad_chdir) {
184*12a9e0efSsn199410 			/*
185*12a9e0efSsn199410 			 * If bad_chdir is set, the argument directory is not
186*12a9e0efSsn199410 			 * deleted since rm does not continue with the recursion
187*12a9e0efSsn199410 			 * Call rm() again on the same argument to remove it.
188*12a9e0efSsn199410 			 */
189*12a9e0efSsn199410 			bad_chdir = 0;
190*12a9e0efSsn199410 			rm(*argv, 1);
191*12a9e0efSsn199410 		}
1927c478bd9Sstevel@tonic-gate 		argv++;
193014a7923Sas145665 		destroy_tree(tree);
1947c478bd9Sstevel@tonic-gate 	}
195*12a9e0efSsn199410 
1967c478bd9Sstevel@tonic-gate 	cleanup();
1977c478bd9Sstevel@tonic-gate 	return (errcode ? 2 : 0);
1987c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static void
2027c478bd9Sstevel@tonic-gate rm(char *path, int first)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	struct stat buffer;
2057c478bd9Sstevel@tonic-gate 	char	*filepath;
2067c478bd9Sstevel@tonic-gate 	char	*p;
2077c478bd9Sstevel@tonic-gate 	char	resolved_path[PATH_MAX];
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * Check file to see if it exists.
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 	if (lstat(path, &buffer) == FAIL) {
2137c478bd9Sstevel@tonic-gate 		if (!silent) {
2147c478bd9Sstevel@tonic-gate 			perror(path);
2157c478bd9Sstevel@tonic-gate 			++errcode;
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 		return;
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	/* prevent removal of / but allow removal of sym-links */
2217c478bd9Sstevel@tonic-gate 	if (!S_ISLNK(buffer.st_mode) && realpath(path, resolved_path) != NULL &&
2227c478bd9Sstevel@tonic-gate 	    strcmp(resolved_path, "/") == 0) {
2237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2247c478bd9Sstevel@tonic-gate 		    gettext("rm of %s is not allowed\n"), resolved_path);
2257c478bd9Sstevel@tonic-gate 		errcode++;
2267c478bd9Sstevel@tonic-gate 		return;
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/* prevent removal of . or .. (directly) */
2307c478bd9Sstevel@tonic-gate 	if (p = strrchr(path, '/'))
2317c478bd9Sstevel@tonic-gate 		p++;
2327c478bd9Sstevel@tonic-gate 	else
2337c478bd9Sstevel@tonic-gate 		p = path;
2347c478bd9Sstevel@tonic-gate 	if (strcmp(".", p) == 0 || strcmp("..", p) == 0) {
2357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2367c478bd9Sstevel@tonic-gate 			gettext("rm of %s is not allowed\n"), path);
2377c478bd9Sstevel@tonic-gate 		errcode++;
2387c478bd9Sstevel@tonic-gate 		return;
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	/*
2417c478bd9Sstevel@tonic-gate 	 * If it's a directory, remove its contents.
2427c478bd9Sstevel@tonic-gate 	 */
2437c478bd9Sstevel@tonic-gate 	if (DIRECTORY) {
2447c478bd9Sstevel@tonic-gate 		/*
2457c478bd9Sstevel@tonic-gate 		 * If "-r" wasn't specified, trying to remove directories
2467c478bd9Sstevel@tonic-gate 		 * is an error.
2477c478bd9Sstevel@tonic-gate 		 */
2487c478bd9Sstevel@tonic-gate 		if (!recursive) {
2497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2507c478bd9Sstevel@tonic-gate 			    gettext("rm: %s is a directory\n"), path);
2517c478bd9Sstevel@tonic-gate 			++errcode;
2527c478bd9Sstevel@tonic-gate 			return;
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		if (first_dir) {
256*12a9e0efSsn199410 			check_initdir();
257*12a9e0efSsn199410 			current_dir = NULL;
2587c478bd9Sstevel@tonic-gate 			first_dir = 0;
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		undir(path, first, buffer.st_dev, buffer.st_ino);
2627c478bd9Sstevel@tonic-gate 		return;
2637c478bd9Sstevel@tonic-gate 	}
264*12a9e0efSsn199410 	/*
265*12a9e0efSsn199410 	 * If 'bad_chdir' is set, rm is in an unintended directory and tries
266*12a9e0efSsn199410 	 * to unlink a file whose name is contained in 'path'. Return to
267*12a9e0efSsn199410 	 * the calling function without proceeding with the argument.
268*12a9e0efSsn199410 	 */
269*12a9e0efSsn199410 	if (bad_chdir)
270*12a9e0efSsn199410 		return;
271*12a9e0efSsn199410 
2727c478bd9Sstevel@tonic-gate 	filepath = get_filename(path);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/*
2757c478bd9Sstevel@tonic-gate 	 * If interactive, ask for acknowledgement.
2767c478bd9Sstevel@tonic-gate 	 *
2777c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE - The following message will contain the
2787c478bd9Sstevel@tonic-gate 	 * first character of the strings for "yes" and "no" defined
2797c478bd9Sstevel@tonic-gate 	 * in the file "nl_langinfo.po".  After substitution, the
2807c478bd9Sstevel@tonic-gate 	 * message will appear as follows:
2817c478bd9Sstevel@tonic-gate 	 *	rm: remove <filename> (y/n)?
2827c478bd9Sstevel@tonic-gate 	 * For example, in German, this will appear as
2837c478bd9Sstevel@tonic-gate 	 *	rm: l�schen <filename> (j/n)?
2847c478bd9Sstevel@tonic-gate 	 * where j=ja, n=nein, <filename>=the file to be removed
2857c478bd9Sstevel@tonic-gate 	 *
2867c478bd9Sstevel@tonic-gate 	 */
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if (interactive) {
2907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rm: remove %s (%s/%s)? "),
2917c478bd9Sstevel@tonic-gate 			filepath, yeschr, nochr);
2927c478bd9Sstevel@tonic-gate 		if (!yes()) {
2937c478bd9Sstevel@tonic-gate 			free(filepath);
2947c478bd9Sstevel@tonic-gate 			return;
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 	} else if (!silent) {
2977c478bd9Sstevel@tonic-gate 		/*
2987c478bd9Sstevel@tonic-gate 		 * If not silent, and stdin is a terminal, and there's
2997c478bd9Sstevel@tonic-gate 		 * no write access, and the file isn't a symbolic link,
3007c478bd9Sstevel@tonic-gate 		 * ask for permission.
3017c478bd9Sstevel@tonic-gate 		 *
3027c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE - The following message will contain the
3037c478bd9Sstevel@tonic-gate 		 * first character of the strings for "yes" and "no" defined
3047c478bd9Sstevel@tonic-gate 		 * in the file "nl_langinfo.po".  After substitution, the
3057c478bd9Sstevel@tonic-gate 		 * message will appear as follows:
3067c478bd9Sstevel@tonic-gate 		 * 	rm: <filename>: override protection XXX (y/n)?
3077c478bd9Sstevel@tonic-gate 		 * where XXX is the permission mode bits of the file in octal
3087c478bd9Sstevel@tonic-gate 		 * and <filename> is the file to be removed
3097c478bd9Sstevel@tonic-gate 		 *
3107c478bd9Sstevel@tonic-gate 		 */
3117c478bd9Sstevel@tonic-gate 		if (!SYMLINK && access(path, W_OK) == FAIL &&
3127c478bd9Sstevel@tonic-gate 		    isatty(fileno(stdin))) {
3137c478bd9Sstevel@tonic-gate 			(void) printf(
3147c478bd9Sstevel@tonic-gate 			    gettext("rm: %s: override protection %o (%s/%s)? "),
3157c478bd9Sstevel@tonic-gate 			    filepath, buffer.st_mode & 0777, yeschr, nochr);
3167c478bd9Sstevel@tonic-gate 			/*
3177c478bd9Sstevel@tonic-gate 			 * If permission isn't given, skip the file.
3187c478bd9Sstevel@tonic-gate 			 */
3197c478bd9Sstevel@tonic-gate 			if (!yes()) {
3207c478bd9Sstevel@tonic-gate 				free(filepath);
3217c478bd9Sstevel@tonic-gate 				return;
3227c478bd9Sstevel@tonic-gate 			}
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * If the unlink fails, inform the user. For /usr/bin/rm, only inform
3287c478bd9Sstevel@tonic-gate 	 * the user if interactive or not silent.
3297c478bd9Sstevel@tonic-gate 	 * If unlink fails with errno = ENOENT because file was removed
3307c478bd9Sstevel@tonic-gate 	 * in between the lstat call and unlink don't inform the user and
3317c478bd9Sstevel@tonic-gate 	 * don't change errcode.
3327c478bd9Sstevel@tonic-gate 	 */
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (unlink(path) == FAIL) {
3357c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
3367c478bd9Sstevel@tonic-gate 			free(filepath);
3377c478bd9Sstevel@tonic-gate 			return;
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate #ifndef XPG4
3407c478bd9Sstevel@tonic-gate 		if (!silent || interactive) {
3417c478bd9Sstevel@tonic-gate #endif
3427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3437c478bd9Sstevel@tonic-gate 				    gettext("rm: %s not removed: "), filepath);
3447c478bd9Sstevel@tonic-gate 				perror("");
3457c478bd9Sstevel@tonic-gate #ifndef XPG4
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate #endif
3487c478bd9Sstevel@tonic-gate 		++errcode;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	free(filepath);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate static void
3557c478bd9Sstevel@tonic-gate undir(char *path, int first, dev_t dev, ino_t ino)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	char	*newpath;
3587c478bd9Sstevel@tonic-gate 	DIR	*name;
3597c478bd9Sstevel@tonic-gate 	struct dirent *direct;
3607c478bd9Sstevel@tonic-gate 	int	ismypath;
361014a7923Sas145665 	int	ret;
3627c478bd9Sstevel@tonic-gate 	int	chdir_failed = 0;
3637c478bd9Sstevel@tonic-gate 	size_t	len;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	push_name(path, first);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * If interactive and this file isn't in the path of the
3697c478bd9Sstevel@tonic-gate 	 * current working directory, ask for acknowledgement.
3707c478bd9Sstevel@tonic-gate 	 *
3717c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE - The following message will contain the
3727c478bd9Sstevel@tonic-gate 	 * first character of the strings for "yes" and "no" defined
3737c478bd9Sstevel@tonic-gate 	 * in the file "nl_langinfo.po".  After substitution, the
3747c478bd9Sstevel@tonic-gate 	 * message will appear as follows:
3757c478bd9Sstevel@tonic-gate 	 *	rm: examine files in directory <directoryname> (y/n)?
3767c478bd9Sstevel@tonic-gate 	 * where <directoryname> is the directory to be removed
3777c478bd9Sstevel@tonic-gate 	 *
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 	ismypath = mypath(dev, ino);
3807c478bd9Sstevel@tonic-gate 	if (interactive) {
3817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3827c478bd9Sstevel@tonic-gate 		    gettext("rm: examine files in directory %s (%s/%s)? "),
3837c478bd9Sstevel@tonic-gate 			fullpath, yeschr, nochr);
3847c478bd9Sstevel@tonic-gate 		/*
3857c478bd9Sstevel@tonic-gate 		 * If the answer is no, skip the directory.
3867c478bd9Sstevel@tonic-gate 		 */
3877c478bd9Sstevel@tonic-gate 		if (!yes()) {
3887c478bd9Sstevel@tonic-gate 			pop_name(first);
3897c478bd9Sstevel@tonic-gate 			return;
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate #ifdef XPG4
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * XCU4 and POSIX.2: If not interactive and file is not in the
3967c478bd9Sstevel@tonic-gate 	 * path of the current working directory, check to see whether
3977c478bd9Sstevel@tonic-gate 	 * or not directory is readable or writable and if not,
3987c478bd9Sstevel@tonic-gate 	 * prompt user for response.
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	if (!interactive && !ismypath &&
4017c478bd9Sstevel@tonic-gate 	    (access(path, W_OK|X_OK) == FAIL) && isatty(fileno(stdin))) {
4027c478bd9Sstevel@tonic-gate 		if (!silent) {
4037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4047c478bd9Sstevel@tonic-gate 			    gettext(
4057c478bd9Sstevel@tonic-gate 				"rm: examine files in directory %s (%s/%s)? "),
4067c478bd9Sstevel@tonic-gate 			    fullpath, yeschr, nochr);
4077c478bd9Sstevel@tonic-gate 			/*
4087c478bd9Sstevel@tonic-gate 			 * If the answer is no, skip the directory.
4097c478bd9Sstevel@tonic-gate 			 */
4107c478bd9Sstevel@tonic-gate 			if (!yes()) {
4117c478bd9Sstevel@tonic-gate 				pop_name(first);
4127c478bd9Sstevel@tonic-gate 				return;
4137c478bd9Sstevel@tonic-gate 			}
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate #endif
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
419014a7923Sas145665 	 * Add this node to the search tree so we don't
420014a7923Sas145665 	 * get into a endless loop. If the add fails then
421014a7923Sas145665 	 * we have visited this node before.
422014a7923Sas145665 	 */
423014a7923Sas145665 	ret = add_tnode(&tree, dev, ino);
424014a7923Sas145665 	if (ret != 1) {
425014a7923Sas145665 		if (ret == 0) {
426014a7923Sas145665 			(void) fprintf(stderr,
427014a7923Sas145665 			    gettext("rm: cycle detected for %s\n"),
428014a7923Sas145665 			    fullpath);
429014a7923Sas145665 		} else if (ret == -1) {
430014a7923Sas145665 			perror("rm");
431014a7923Sas145665 		}
432014a7923Sas145665 		errcode++;
433014a7923Sas145665 		pop_name(first);
434014a7923Sas145665 		return;
435014a7923Sas145665 	}
436014a7923Sas145665 
437014a7923Sas145665 	/*
4387c478bd9Sstevel@tonic-gate 	 * Open the directory for reading.
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 	if ((name = opendir(path)) == NULL) {
4417c478bd9Sstevel@tonic-gate 		int	saveerrno = errno;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		/*
4447c478bd9Sstevel@tonic-gate 		 * If interactive, ask for acknowledgement.
4457c478bd9Sstevel@tonic-gate 		 */
4467c478bd9Sstevel@tonic-gate 		if (interactive) {
4477c478bd9Sstevel@tonic-gate 			/*
4487c478bd9Sstevel@tonic-gate 			 * Print an error message that
4497c478bd9Sstevel@tonic-gate 			 * we could not read the directory
4507c478bd9Sstevel@tonic-gate 			 * as the user wanted to examine
4517c478bd9Sstevel@tonic-gate 			 * files in the directory.  Only
4527c478bd9Sstevel@tonic-gate 			 * affect the error status if
4537c478bd9Sstevel@tonic-gate 			 * user doesn't want to remove the
4547c478bd9Sstevel@tonic-gate 			 * directory as we still may be able
4557c478bd9Sstevel@tonic-gate 			 * remove the directory successfully.
4567c478bd9Sstevel@tonic-gate 			 */
4577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4587c478bd9Sstevel@tonic-gate 			    "rm: cannot read directory %s: "),
4597c478bd9Sstevel@tonic-gate 			    fullpath);
4607c478bd9Sstevel@tonic-gate 			errno = saveerrno;
4617c478bd9Sstevel@tonic-gate 			perror("");
4627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4637c478bd9Sstevel@tonic-gate 			    "rm: remove %s: (%s/%s)? "),
4647c478bd9Sstevel@tonic-gate 			    fullpath, yeschr, nochr);
4657c478bd9Sstevel@tonic-gate 			if (!yes()) {
4667c478bd9Sstevel@tonic-gate 				++errcode;
4677c478bd9Sstevel@tonic-gate 				pop_name(first);
4687c478bd9Sstevel@tonic-gate 				return;
4697c478bd9Sstevel@tonic-gate 			}
4707c478bd9Sstevel@tonic-gate 		}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		/*
4737c478bd9Sstevel@tonic-gate 		 * If the directory is empty, we may be able to
4747c478bd9Sstevel@tonic-gate 		 * go ahead and remove it.
4757c478bd9Sstevel@tonic-gate 		 */
4767c478bd9Sstevel@tonic-gate 		if (rmdir(path) == FAIL) {
4777c478bd9Sstevel@tonic-gate 			if (interactive) {
4787c478bd9Sstevel@tonic-gate 				int	rmdirerr = errno;
4797c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4807c478bd9Sstevel@tonic-gate 				    "rm: Unable to remove directory %s: "),
4817c478bd9Sstevel@tonic-gate 				    fullpath);
4827c478bd9Sstevel@tonic-gate 				errno = rmdirerr;
4837c478bd9Sstevel@tonic-gate 				perror("");
4847c478bd9Sstevel@tonic-gate 			} else {
4857c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4867c478bd9Sstevel@tonic-gate 				    "rm: cannot read directory %s: "),
4877c478bd9Sstevel@tonic-gate 				    fullpath);
4887c478bd9Sstevel@tonic-gate 				errno = saveerrno;
4897c478bd9Sstevel@tonic-gate 				perror("");
4907c478bd9Sstevel@tonic-gate 			}
4917c478bd9Sstevel@tonic-gate 			++errcode;
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		/* Continue to next file/directory rather than exit */
4957c478bd9Sstevel@tonic-gate 		pop_name(first);
4967c478bd9Sstevel@tonic-gate 		return;
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	/*
5007c478bd9Sstevel@tonic-gate 	 * XCU4 requires that rm -r descend the directory
5017c478bd9Sstevel@tonic-gate 	 * hierarchy without regard to PATH_MAX.  If we can't
5027c478bd9Sstevel@tonic-gate 	 * chdir() do not increment error counter and do not
5037c478bd9Sstevel@tonic-gate 	 * print message.
5047c478bd9Sstevel@tonic-gate 	 *
5057c478bd9Sstevel@tonic-gate 	 * However, if we cannot chdir because someone has taken away
5067c478bd9Sstevel@tonic-gate 	 * execute access we may still be able to delete the directory
5077c478bd9Sstevel@tonic-gate 	 * if it's empty. The old rm could do this.
5087c478bd9Sstevel@tonic-gate 	 */
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if (chdir(path) == -1) {
5117c478bd9Sstevel@tonic-gate 		chdir_failed = 1;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	/*
5157c478bd9Sstevel@tonic-gate 	 * Read every directory entry.
5167c478bd9Sstevel@tonic-gate 	 */
5177c478bd9Sstevel@tonic-gate 	while ((direct = readdir(name)) != NULL) {
5187c478bd9Sstevel@tonic-gate 		/*
5197c478bd9Sstevel@tonic-gate 		 * Ignore "." and ".." entries.
5207c478bd9Sstevel@tonic-gate 		 */
5217c478bd9Sstevel@tonic-gate 		if (strcmp(direct->d_name, ".") == 0 ||
5227c478bd9Sstevel@tonic-gate 		    strcmp(direct->d_name, "..") == 0)
5237c478bd9Sstevel@tonic-gate 			continue;
5247c478bd9Sstevel@tonic-gate 		/*
5257c478bd9Sstevel@tonic-gate 		 * Try to remove the file.
5267c478bd9Sstevel@tonic-gate 		 */
5277c478bd9Sstevel@tonic-gate 		len = strlen(direct->d_name) + 1;
5287c478bd9Sstevel@tonic-gate 		if (chdir_failed) {
5297c478bd9Sstevel@tonic-gate 			len += strlen(path) + 2;
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		newpath = malloc(len);
5337c478bd9Sstevel@tonic-gate 		if (newpath == NULL) {
5347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5357c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
5367c478bd9Sstevel@tonic-gate 			cleanup();
5377c478bd9Sstevel@tonic-gate 			exit(1);
5387c478bd9Sstevel@tonic-gate 		}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 		if (!chdir_failed) {
5417c478bd9Sstevel@tonic-gate 			(void) strcpy(newpath, direct->d_name);
5427c478bd9Sstevel@tonic-gate 		} else {
5437c478bd9Sstevel@tonic-gate 			(void) snprintf(newpath, len, "%s/%s",
5447c478bd9Sstevel@tonic-gate 			    path, direct->d_name);
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		/*
5497c478bd9Sstevel@tonic-gate 		 * If a spare file descriptor is available, just call the
5507c478bd9Sstevel@tonic-gate 		 * "rm" function with the file name; otherwise close the
5517c478bd9Sstevel@tonic-gate 		 * directory and reopen it when the child is removed.
5527c478bd9Sstevel@tonic-gate 		 */
5537c478bd9Sstevel@tonic-gate 		if (name->dd_fd >= maxfiles) {
5547c478bd9Sstevel@tonic-gate 			(void) closedir(name);
5557c478bd9Sstevel@tonic-gate 			rm(newpath, 0);
5567c478bd9Sstevel@tonic-gate 			if (!chdir_failed)
5577c478bd9Sstevel@tonic-gate 				name = opendir(".");
5587c478bd9Sstevel@tonic-gate 			else
5597c478bd9Sstevel@tonic-gate 				name = opendir(path);
5607c478bd9Sstevel@tonic-gate 			if (name == NULL) {
5617c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5627c478bd9Sstevel@tonic-gate 				    gettext("rm: cannot read directory %s: "),
5637c478bd9Sstevel@tonic-gate 				    fullpath);
5647c478bd9Sstevel@tonic-gate 				perror("");
5657c478bd9Sstevel@tonic-gate 				cleanup();
5667c478bd9Sstevel@tonic-gate 				exit(2);
5677c478bd9Sstevel@tonic-gate 			}
5687c478bd9Sstevel@tonic-gate 		} else
5697c478bd9Sstevel@tonic-gate 			rm(newpath, 0);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		free(newpath);
572*12a9e0efSsn199410 
573*12a9e0efSsn199410 		/*
574*12a9e0efSsn199410 		 * If an attempt to move files/directories during the execution
575*12a9e0efSsn199410 		 * of rm is detected DO NOT proceed with the argument directory.
576*12a9e0efSsn199410 		 * rm, in such a case, may have chdir() into a directory outside
577*12a9e0efSsn199410 		 * the hierarchy of argument directory.
578*12a9e0efSsn199410 		 */
579*12a9e0efSsn199410 		if (bad_chdir) {
580*12a9e0efSsn199410 			pop_name(first);
581*12a9e0efSsn199410 			(void) closedir(name);
582*12a9e0efSsn199410 			return;
583*12a9e0efSsn199410 		}
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/*
5877c478bd9Sstevel@tonic-gate 	 * Close the directory we just finished reading.
5887c478bd9Sstevel@tonic-gate 	 */
5897c478bd9Sstevel@tonic-gate 	(void) closedir(name);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	/*
5927c478bd9Sstevel@tonic-gate 	 * The contents of the directory have been removed.  If the
5937c478bd9Sstevel@tonic-gate 	 * directory itself is in the path of the current working
5947c478bd9Sstevel@tonic-gate 	 * directory, don't try to remove it.
5957c478bd9Sstevel@tonic-gate 	 * When the directory itself is the current working directory, mypath()
5967c478bd9Sstevel@tonic-gate 	 * has a return code == 2.
5977c478bd9Sstevel@tonic-gate 	 *
5987c478bd9Sstevel@tonic-gate 	 * XCU4: Because we've descended the directory hierarchy in order
5997c478bd9Sstevel@tonic-gate 	 * to avoid PATH_MAX limitation, we must now start ascending
6007c478bd9Sstevel@tonic-gate 	 * one level at a time and remove files/directories.
6017c478bd9Sstevel@tonic-gate 	 */
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	if (!chdir_failed) {
6047c478bd9Sstevel@tonic-gate 		if (first)
605*12a9e0efSsn199410 			chdir_init();
6067c478bd9Sstevel@tonic-gate 		else if (chdir("..") == -1) {
6077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6087c478bd9Sstevel@tonic-gate 			    gettext("rm: cannot change to parent of "
6097c478bd9Sstevel@tonic-gate 				    "directory %s: "),
6107c478bd9Sstevel@tonic-gate 			    fullpath);
6117c478bd9Sstevel@tonic-gate 			perror("");
6127c478bd9Sstevel@tonic-gate 			cleanup();
6137c478bd9Sstevel@tonic-gate 			exit(2);
6147c478bd9Sstevel@tonic-gate 		}
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	switch (ismypath) {
6187c478bd9Sstevel@tonic-gate 	case 3:
6197c478bd9Sstevel@tonic-gate 		pop_name(first);
6207c478bd9Sstevel@tonic-gate 		return;
6217c478bd9Sstevel@tonic-gate 	case 2:
6227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6237c478bd9Sstevel@tonic-gate 		    gettext("rm: Cannot remove any directory in the path "
6247c478bd9Sstevel@tonic-gate 			"of the current working directory\n%s\n"), fullpath);
6257c478bd9Sstevel@tonic-gate 		++errcode;
6267c478bd9Sstevel@tonic-gate 		pop_name(first);
6277c478bd9Sstevel@tonic-gate 		return;
6287c478bd9Sstevel@tonic-gate 	case 1:
6297c478bd9Sstevel@tonic-gate 		++errcode;
6307c478bd9Sstevel@tonic-gate 		pop_name(first);
6317c478bd9Sstevel@tonic-gate 		return;
6327c478bd9Sstevel@tonic-gate 	case 0:
6337c478bd9Sstevel@tonic-gate 		break;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/*
6377c478bd9Sstevel@tonic-gate 	 * If interactive, ask for acknowledgement.
6387c478bd9Sstevel@tonic-gate 	 */
6397c478bd9Sstevel@tonic-gate 	if (interactive) {
6407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rm: remove %s: (%s/%s)? "),
6417c478bd9Sstevel@tonic-gate 			fullpath, yeschr, nochr);
6427c478bd9Sstevel@tonic-gate 		if (!yes()) {
6437c478bd9Sstevel@tonic-gate 			pop_name(first);
6447c478bd9Sstevel@tonic-gate 			return;
6457c478bd9Sstevel@tonic-gate 		}
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate 	if (rmdir(path) == FAIL) {
6487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6497c478bd9Sstevel@tonic-gate 			gettext("rm: Unable to remove directory %s: "),
6507c478bd9Sstevel@tonic-gate 			fullpath);
6517c478bd9Sstevel@tonic-gate 		perror("");
6527c478bd9Sstevel@tonic-gate 		++errcode;
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	pop_name(first);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate static int
6597c478bd9Sstevel@tonic-gate yes(void)
6607c478bd9Sstevel@tonic-gate {
6617c478bd9Sstevel@tonic-gate 	int	i, b;
6627c478bd9Sstevel@tonic-gate 	char	ans[SCHAR_MAX + 1];
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	for (i = 0; ; i++) {
6657c478bd9Sstevel@tonic-gate 		b = getchar();
6667c478bd9Sstevel@tonic-gate 		if (b == '\n' || b == '\0' || b == EOF) {
6677c478bd9Sstevel@tonic-gate 			ans[i] = 0;
6687c478bd9Sstevel@tonic-gate 			break;
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 		if (i < SCHAR_MAX)
6717c478bd9Sstevel@tonic-gate 			ans[i] = b;
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 	if (i >= SCHAR_MAX) {
6747c478bd9Sstevel@tonic-gate 		i = SCHAR_MAX;
6757c478bd9Sstevel@tonic-gate 		ans[SCHAR_MAX] = 0;
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 	if ((i == 0) | (strncmp(yeschr, ans, i)))
6787c478bd9Sstevel@tonic-gate 		return (0);
6797c478bd9Sstevel@tonic-gate 	return (1);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate static int
6847c478bd9Sstevel@tonic-gate mypath(dev_t dev, ino_t ino)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	struct dir_id *curdir;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/*
6897c478bd9Sstevel@tonic-gate 	 * Check to see if this is our current directory
6907c478bd9Sstevel@tonic-gate 	 * Indicated by return 2;
6917c478bd9Sstevel@tonic-gate 	 */
692*12a9e0efSsn199410 	if (dev == initdir.dev && ino == initdir.inode) {
6937c478bd9Sstevel@tonic-gate 		return (2);
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
696*12a9e0efSsn199410 	curdir = initdir.next;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	while (curdir != NULL) {
6997c478bd9Sstevel@tonic-gate 		/*
7007c478bd9Sstevel@tonic-gate 		 * If we find a match, the directory (dev, ino) passed to
7017c478bd9Sstevel@tonic-gate 		 * mypath() is an ancestor of ours. Indicated by return 3.
7027c478bd9Sstevel@tonic-gate 		 */
7037c478bd9Sstevel@tonic-gate 		if (curdir->dev == dev && curdir->inode == ino)
7047c478bd9Sstevel@tonic-gate 			return (3);
7057c478bd9Sstevel@tonic-gate 		curdir = curdir->next;
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 	/*
7087c478bd9Sstevel@tonic-gate 	 * parent_err indicates we couldn't stat or chdir to
7097c478bd9Sstevel@tonic-gate 	 * one of our parent dirs, so the linked list of dir_id structs
7107c478bd9Sstevel@tonic-gate 	 * is incomplete
7117c478bd9Sstevel@tonic-gate 	 */
7127c478bd9Sstevel@tonic-gate 	if (parent_err) {
7137c478bd9Sstevel@tonic-gate #ifndef XPG4
7147c478bd9Sstevel@tonic-gate 		if (!silent || interactive) {
7157c478bd9Sstevel@tonic-gate #endif
7167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("rm: cannot determine "
7177c478bd9Sstevel@tonic-gate 			    "if this is an ancestor of the current "
7187c478bd9Sstevel@tonic-gate 			    "working directory\n%s\n"), fullpath);
7197c478bd9Sstevel@tonic-gate #ifndef XPG4
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate #endif
7227c478bd9Sstevel@tonic-gate 		/* assume it is. least dangerous */
7237c478bd9Sstevel@tonic-gate 		return (1);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 	return (0);
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate static int maxlen;
7297c478bd9Sstevel@tonic-gate static int curlen;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate static char *
7327c478bd9Sstevel@tonic-gate get_filename(char *name)
7337c478bd9Sstevel@tonic-gate {
7347c478bd9Sstevel@tonic-gate 	char *path;
7357c478bd9Sstevel@tonic-gate 	size_t len;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	if (fullpath == NULL || *fullpath == '\0') {
7387c478bd9Sstevel@tonic-gate 		path = strdup(name);
7397c478bd9Sstevel@tonic-gate 		if (path == NULL) {
7407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7417c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
7427c478bd9Sstevel@tonic-gate 			cleanup();
7437c478bd9Sstevel@tonic-gate 			exit(1);
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 	} else {
7467c478bd9Sstevel@tonic-gate 		len = strlen(fullpath) + strlen(name) + 2;
7477c478bd9Sstevel@tonic-gate 		path = malloc(len);
7487c478bd9Sstevel@tonic-gate 		if (path == NULL) {
7497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7507c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
7517c478bd9Sstevel@tonic-gate 			cleanup();
7527c478bd9Sstevel@tonic-gate 			exit(1);
7537c478bd9Sstevel@tonic-gate 		}
7547c478bd9Sstevel@tonic-gate 		(void) snprintf(path, len, "%s/%s", fullpath, name);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 	return (path);
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate static void
7607c478bd9Sstevel@tonic-gate push_name(char *name, int first)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	int	namelen;
763*12a9e0efSsn199410 	struct	stat buffer;
764*12a9e0efSsn199410 	struct	dir_id *newdir;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	namelen = strlen(name) + 1; /* 1 for "/" */
7677c478bd9Sstevel@tonic-gate 	if ((curlen + namelen) >= maxlen) {
7687c478bd9Sstevel@tonic-gate 		maxlen += PATH_MAX;
7697c478bd9Sstevel@tonic-gate 		fullpath = (char *)realloc(fullpath, (size_t)(maxlen + 1));
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 	if (first) {
7727c478bd9Sstevel@tonic-gate 		(void) strcpy(fullpath, name);
7737c478bd9Sstevel@tonic-gate 	} else {
7747c478bd9Sstevel@tonic-gate 		(void) strcat(fullpath, "/");
7757c478bd9Sstevel@tonic-gate 		(void) strcat(fullpath, name);
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 	curlen = strlen(fullpath);
778*12a9e0efSsn199410 
779*12a9e0efSsn199410 	if (stat(".", &buffer) == -1) {
780*12a9e0efSsn199410 		(void) fprintf(stderr,
781*12a9e0efSsn199410 		    gettext("rm: cannot stat current directory: "));
782*12a9e0efSsn199410 		perror("");
783*12a9e0efSsn199410 		exit(2);
784*12a9e0efSsn199410 	}
785*12a9e0efSsn199410 	if ((newdir = malloc(sizeof (struct dir_id))) == NULL) {
786*12a9e0efSsn199410 		(void) fprintf(stderr,
787*12a9e0efSsn199410 		    gettext("rm: Insufficient memory.\n"));
788*12a9e0efSsn199410 		cleanup();
789*12a9e0efSsn199410 		exit(1);
790*12a9e0efSsn199410 	}
791*12a9e0efSsn199410 
792*12a9e0efSsn199410 	newdir->dev = buffer.st_dev;
793*12a9e0efSsn199410 	newdir->inode = buffer.st_ino;
794*12a9e0efSsn199410 	newdir->next = current_dir;
795*12a9e0efSsn199410 	current_dir = newdir;
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate static void
7997c478bd9Sstevel@tonic-gate pop_name(int first)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 	char *slash;
802*12a9e0efSsn199410 	struct	stat buffer;
803*12a9e0efSsn199410 	struct	dir_id *remove_dir;
804*12a9e0efSsn199410 
8057c478bd9Sstevel@tonic-gate 	if (first) {
8067c478bd9Sstevel@tonic-gate 		*fullpath = '\0';
8077c478bd9Sstevel@tonic-gate 		return;
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 	slash = strrchr(fullpath, '/');
8107c478bd9Sstevel@tonic-gate 	if (slash)
8117c478bd9Sstevel@tonic-gate 		*slash = '\0';
8127c478bd9Sstevel@tonic-gate 	else
8137c478bd9Sstevel@tonic-gate 		*fullpath = '\0';
8147c478bd9Sstevel@tonic-gate 	curlen = strlen(fullpath);
815*12a9e0efSsn199410 
816*12a9e0efSsn199410 	if (stat(".", &buffer) == -1) {
817*12a9e0efSsn199410 		(void) fprintf(stderr,
818*12a9e0efSsn199410 		    gettext("rm: cannot stat current directory: "));
819*12a9e0efSsn199410 		perror("");
820*12a9e0efSsn199410 		exit(2);
821*12a9e0efSsn199410 	}
822*12a9e0efSsn199410 
823*12a9e0efSsn199410 	/*
824*12a9e0efSsn199410 	 * For each pop operation, verify that the device and inode numbers
825*12a9e0efSsn199410 	 * of "." match the numbers recorded before the chdir was done into
826*12a9e0efSsn199410 	 * the directory. If they do not match, it is an indication of
827*12a9e0efSsn199410 	 * possible malicious activity and rm has chdir to an unintended
828*12a9e0efSsn199410 	 * directory
829*12a9e0efSsn199410 	 */
830*12a9e0efSsn199410 	if ((current_dir->inode != buffer.st_ino) || (current_dir->dev !=
831*12a9e0efSsn199410 	    buffer.st_dev)) {
832*12a9e0efSsn199410 		if (!bad_chdir) {
833*12a9e0efSsn199410 			(void) fprintf(stderr, gettext("rm: WARNING: "
834*12a9e0efSsn199410 			    "A subdirectory of %s was moved or linked to "
835*12a9e0efSsn199410 			    "another directory during the execution of rm\n"),
836*12a9e0efSsn199410 			    fullpath);
837*12a9e0efSsn199410 		}
838*12a9e0efSsn199410 		bad_chdir = 1;
839*12a9e0efSsn199410 	}
840*12a9e0efSsn199410 	remove_dir = current_dir;
841*12a9e0efSsn199410 	current_dir = current_dir->next;
842*12a9e0efSsn199410 	free(remove_dir);
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate static void
8467c478bd9Sstevel@tonic-gate force_chdir(char *dirname)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	char 	*pathname, *mp, *tp;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	/* use pathname instead of dirname, so dirname won't be modified */
8517c478bd9Sstevel@tonic-gate 	if ((pathname = strdup(dirname)) == NULL) {
8527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rm: strdup: "));
8537c478bd9Sstevel@tonic-gate 		perror("");
8547c478bd9Sstevel@tonic-gate 		cleanup();
8557c478bd9Sstevel@tonic-gate 		exit(2);
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/* pathname is an absolute full path from getcwd() */
8597c478bd9Sstevel@tonic-gate 	mp = pathname;
8607c478bd9Sstevel@tonic-gate 	while (mp) {
8617c478bd9Sstevel@tonic-gate 		tp = strchr(mp, '/');
8627c478bd9Sstevel@tonic-gate 		if (strlen(mp) >= PATH_MAX) {
8637c478bd9Sstevel@tonic-gate 			/*
8647c478bd9Sstevel@tonic-gate 			 * after the first iteration through this
8657c478bd9Sstevel@tonic-gate 			 * loop, the below will NULL out the '/'
8667c478bd9Sstevel@tonic-gate 			 * which follows the first dir on pathname
8677c478bd9Sstevel@tonic-gate 			 */
8687c478bd9Sstevel@tonic-gate 			*tp = 0;
8697c478bd9Sstevel@tonic-gate 			tp++;
8707c478bd9Sstevel@tonic-gate 			if (*mp == NULL)
8717c478bd9Sstevel@tonic-gate 				ch_dir("/");
8727c478bd9Sstevel@tonic-gate 			else
8737c478bd9Sstevel@tonic-gate 				/*
8747c478bd9Sstevel@tonic-gate 				 * mp points to the start of a dirname,
8757c478bd9Sstevel@tonic-gate 				 * terminated by NULL, so ch_dir()
8767c478bd9Sstevel@tonic-gate 				 * here will move down one directory
8777c478bd9Sstevel@tonic-gate 				 */
8787c478bd9Sstevel@tonic-gate 				ch_dir(mp);
8797c478bd9Sstevel@tonic-gate 			/*
8807c478bd9Sstevel@tonic-gate 			 * reset mp to the start of the dirname
8817c478bd9Sstevel@tonic-gate 			 * which follows the one we just chdir'd to
8827c478bd9Sstevel@tonic-gate 			 */
8837c478bd9Sstevel@tonic-gate 			mp = tp;
8847c478bd9Sstevel@tonic-gate 			continue;	/* probably can remove this */
8857c478bd9Sstevel@tonic-gate 		} else {
8867c478bd9Sstevel@tonic-gate 			ch_dir(mp);
8877c478bd9Sstevel@tonic-gate 			break;
8887c478bd9Sstevel@tonic-gate 		}
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 	free(pathname);
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate static void
8947c478bd9Sstevel@tonic-gate ch_dir(char *dirname)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	if (chdir(dirname) == -1) {
8977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8987c478bd9Sstevel@tonic-gate 		gettext("rm: cannot change to %s directory: "), dirname);
8997c478bd9Sstevel@tonic-gate 			perror("");
9007c478bd9Sstevel@tonic-gate 			cleanup();
9017c478bd9Sstevel@tonic-gate 			exit(2);
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate static void
906*12a9e0efSsn199410 chdir_init(void)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate 	/*
909*12a9e0efSsn199410 	 * Go back to init dir--the dir from where rm was executed--using
9107c478bd9Sstevel@tonic-gate 	 * one of two methods, depending on which method works
911*12a9e0efSsn199410 	 * for the given permissions of the init dir and its
9127c478bd9Sstevel@tonic-gate 	 * parent directories.
9137c478bd9Sstevel@tonic-gate 	 */
914*12a9e0efSsn199410 	if (initdirfd != -1) {
915*12a9e0efSsn199410 		if (fchdir(initdirfd) == -1) {
9167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9177c478bd9Sstevel@tonic-gate 			    gettext("rm: cannot change to starting "
9187c478bd9Sstevel@tonic-gate 			    "directory: "));
9197c478bd9Sstevel@tonic-gate 			perror("");
9207c478bd9Sstevel@tonic-gate 			cleanup();
9217c478bd9Sstevel@tonic-gate 			exit(2);
9227c478bd9Sstevel@tonic-gate 		}
9237c478bd9Sstevel@tonic-gate 	} else {
9247c478bd9Sstevel@tonic-gate 		if (strlen(cwd) < PATH_MAX)
9257c478bd9Sstevel@tonic-gate 			ch_dir(cwd);
9267c478bd9Sstevel@tonic-gate 		else
9277c478bd9Sstevel@tonic-gate 			force_chdir(cwd);
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate }
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate /*
932*12a9e0efSsn199410  * check_initdir -
9337c478bd9Sstevel@tonic-gate  * is only called the first time rm tries to
9347c478bd9Sstevel@tonic-gate  * remove a directory.  It saves the current directory, i.e.,
935*12a9e0efSsn199410  * init dir, so we can go back to it after traversing elsewhere.
9367c478bd9Sstevel@tonic-gate  * It also saves all the device and inode numbers of each
937*12a9e0efSsn199410  * dir from the initial dir back to the root in a linked list, so we
9387c478bd9Sstevel@tonic-gate  * can later check, via mypath(), if we are trying to remove our current
9397c478bd9Sstevel@tonic-gate  * dir or an ancestor.
9407c478bd9Sstevel@tonic-gate  */
9417c478bd9Sstevel@tonic-gate static void
942*12a9e0efSsn199410 check_initdir(void)
9437c478bd9Sstevel@tonic-gate {
944*12a9e0efSsn199410 	int	size;	/* size allocated for pathname of init dir (cwd) */
9457c478bd9Sstevel@tonic-gate 	struct stat buffer;
9467c478bd9Sstevel@tonic-gate 	struct dir_id *lastdir, *curdir;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	/*
949*12a9e0efSsn199410 	 * We need to save where we currently are (the "init dir") so
9507c478bd9Sstevel@tonic-gate 	 * we can return after traversing down directories we're
9517c478bd9Sstevel@tonic-gate 	 * removing.  Two methods are attempted:
9527c478bd9Sstevel@tonic-gate 	 *
953*12a9e0efSsn199410 	 * 1) open() the initial dir so we can use the fd
9547c478bd9Sstevel@tonic-gate 	 *    to fchdir() back.  This requires read permission
955*12a9e0efSsn199410 	 *    on the initial dir.
9567c478bd9Sstevel@tonic-gate 	 *
9577c478bd9Sstevel@tonic-gate 	 * 2) getcwd() so we can chdir() to go back.  This
958*12a9e0efSsn199410 	 *    requires search (x) permission on the init dir,
9597c478bd9Sstevel@tonic-gate 	 *    and read and search permission on all parent dirs.  Also,
960*12a9e0efSsn199410 	 *    getcwd() will not work if the init dir is > 341
9617c478bd9Sstevel@tonic-gate 	 *    directories deep (see open bugid 4033182 - getcwd needs
9627c478bd9Sstevel@tonic-gate 	 *    to work for pathnames of any depth).
9637c478bd9Sstevel@tonic-gate 	 *
9647c478bd9Sstevel@tonic-gate 	 * If neither method works, we can't remove any directories
9657c478bd9Sstevel@tonic-gate 	 * and rm will fail.
9667c478bd9Sstevel@tonic-gate 	 *
9677c478bd9Sstevel@tonic-gate 	 * For future enhancement, a possible 3rd option to use
9687c478bd9Sstevel@tonic-gate 	 * would be to fork a process to remove a directory,
969*12a9e0efSsn199410 	 * eliminating the need to chdir back to the initial directory
970*12a9e0efSsn199410 	 * and eliminating the permission restrictions on the initial dir
9717c478bd9Sstevel@tonic-gate 	 * or its parent dirs.
9727c478bd9Sstevel@tonic-gate 	 */
973*12a9e0efSsn199410 	initdirfd = open(".", O_RDONLY);
974*12a9e0efSsn199410 	if (initdirfd == -1) {
9757c478bd9Sstevel@tonic-gate 		size = PATH_MAX;
9767c478bd9Sstevel@tonic-gate 		while ((cwd = getcwd(NULL, size)) == NULL) {
9777c478bd9Sstevel@tonic-gate 			if (errno == ERANGE) {
9787c478bd9Sstevel@tonic-gate 				size = PATH_MAX + size;
9797c478bd9Sstevel@tonic-gate 				continue;
9807c478bd9Sstevel@tonic-gate 			} else {
9817c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
9827c478bd9Sstevel@tonic-gate 				    gettext("rm: cannot open starting "
9837c478bd9Sstevel@tonic-gate 				    "directory: "));
9847c478bd9Sstevel@tonic-gate 				perror("pwd");
9857c478bd9Sstevel@tonic-gate 				exit(2);
9867c478bd9Sstevel@tonic-gate 			}
9877c478bd9Sstevel@tonic-gate 		}
9887c478bd9Sstevel@tonic-gate 	}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	/*
9917c478bd9Sstevel@tonic-gate 	 * since we exit on error here, we're guaranteed to at least
992*12a9e0efSsn199410 	 * have info in the first dir_id struct, initdir
9937c478bd9Sstevel@tonic-gate 	 */
9947c478bd9Sstevel@tonic-gate 	if (stat(".", &buffer) == -1) {
9957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9967c478bd9Sstevel@tonic-gate 		    gettext("rm: cannot stat current directory: "));
9977c478bd9Sstevel@tonic-gate 		perror("");
9987c478bd9Sstevel@tonic-gate 		exit(2);
9997c478bd9Sstevel@tonic-gate 	}
1000*12a9e0efSsn199410 	initdir.dev = buffer.st_dev;
1001*12a9e0efSsn199410 	initdir.inode = buffer.st_ino;
1002*12a9e0efSsn199410 	initdir.next = NULL;
10037c478bd9Sstevel@tonic-gate 
1004*12a9e0efSsn199410 	lastdir = &initdir;
10057c478bd9Sstevel@tonic-gate 	/*
10067c478bd9Sstevel@tonic-gate 	 * Starting from current working directory, walk toward the
10077c478bd9Sstevel@tonic-gate 	 * root, looking at each directory along the way.
10087c478bd9Sstevel@tonic-gate 	 */
10097c478bd9Sstevel@tonic-gate 	for (;;) {
10107c478bd9Sstevel@tonic-gate 		if (chdir("..") == -1 || lstat(".", &buffer) == -1) {
10117c478bd9Sstevel@tonic-gate 			parent_err = 1;
10127c478bd9Sstevel@tonic-gate 			break;
10137c478bd9Sstevel@tonic-gate 		}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 		if ((lastdir->next = malloc(sizeof (struct dir_id))) ==
10167c478bd9Sstevel@tonic-gate 		    NULL) {
10177c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10187c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
10197c478bd9Sstevel@tonic-gate 			cleanup();
10207c478bd9Sstevel@tonic-gate 			exit(1);
10217c478bd9Sstevel@tonic-gate 		}
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 		curdir = lastdir->next;
10247c478bd9Sstevel@tonic-gate 		curdir->dev = buffer.st_dev;
10257c478bd9Sstevel@tonic-gate 		curdir->inode = buffer.st_ino;
10267c478bd9Sstevel@tonic-gate 		curdir->next = NULL;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 		/*
10297c478bd9Sstevel@tonic-gate 		 * Stop when we reach the root; note that chdir("..")
10307c478bd9Sstevel@tonic-gate 		 * at the root dir will stay in root. Get rid of
10317c478bd9Sstevel@tonic-gate 		 * the redundant dir_id struct for root.
10327c478bd9Sstevel@tonic-gate 		 */
10337c478bd9Sstevel@tonic-gate 		if (curdir->dev == lastdir->dev && curdir->inode ==
10347c478bd9Sstevel@tonic-gate 		    lastdir->inode) {
10357c478bd9Sstevel@tonic-gate 			lastdir->next = NULL;
10367c478bd9Sstevel@tonic-gate 			free(curdir);
10377c478bd9Sstevel@tonic-gate 			break;
10387c478bd9Sstevel@tonic-gate 		}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 			/* loop again to go back another level */
10417c478bd9Sstevel@tonic-gate 		lastdir = curdir;
10427c478bd9Sstevel@tonic-gate 	}
1043*12a9e0efSsn199410 		/* go back to init directory */
1044*12a9e0efSsn199410 	chdir_init();
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate /*
10487c478bd9Sstevel@tonic-gate  * cleanup the dynamically-allocated list of device numbers and inodes,
1049*12a9e0efSsn199410  * if any.  If initdir was never used, it is external and static so
1050*12a9e0efSsn199410  * it is guaranteed initialized to zero, thus initdir.next would be NULL.
10517c478bd9Sstevel@tonic-gate  */
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate static void
10547c478bd9Sstevel@tonic-gate cleanup(void) {
1055*12a9e0efSsn199410 
10567c478bd9Sstevel@tonic-gate 	struct dir_id *lastdir, *curdir;
10577c478bd9Sstevel@tonic-gate 
1058*12a9e0efSsn199410 	curdir = initdir.next;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	while (curdir != NULL) {
10617c478bd9Sstevel@tonic-gate 		lastdir = curdir;
10627c478bd9Sstevel@tonic-gate 		curdir = curdir->next;
10637c478bd9Sstevel@tonic-gate 		free(lastdir);
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate }
1066