xref: /titanic_44/usr/src/cmd/rm/rm.c (revision 014a7923f9b48f6ab3ba1a38049a3dacddc587cb)
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 
27*014a7923Sas145665 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*014a7923Sas145665 /*	All Rights Reserved   */
29*014a7923Sas145665 
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>
49*014a7923Sas145665 #include <sys/avl.h>
50*014a7923Sas145665 #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;
767c478bd9Sstevel@tonic-gate static int homedirfd;
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);
837c478bd9Sstevel@tonic-gate static void chdir_home(void);
847c478bd9Sstevel@tonic-gate static void check_homedir(void);
857c478bd9Sstevel@tonic-gate static void cleanup(void);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static char 	*cwd;		/* pathname of home 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;
92*014a7923Sas145665 static avl_tree_t *tree;	/* tree to keep track of nodes visited */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate struct dir_id {
957c478bd9Sstevel@tonic-gate 	dev_t	dev;
967c478bd9Sstevel@tonic-gate 	ino_t	inode;
977c478bd9Sstevel@tonic-gate 	struct dir_id *next;
987c478bd9Sstevel@tonic-gate };
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * homedir is the first of a linked list of structures
1027c478bd9Sstevel@tonic-gate 	 * containing unique identifying device and inode numbers for
1037c478bd9Sstevel@tonic-gate 	 * each directory, from the home dir up to the root.
1047c478bd9Sstevel@tonic-gate 	 */
1057c478bd9Sstevel@tonic-gate static struct dir_id homedir;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate int
1087c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	extern int	optind;
1117c478bd9Sstevel@tonic-gate 	int	errflg = 0;
1127c478bd9Sstevel@tonic-gate 	int	c;
1137c478bd9Sstevel@tonic-gate 	struct rlimit rl;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1167c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1177c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1187c478bd9Sstevel@tonic-gate #endif
1197c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	(void) strncpy(yeschr, nl_langinfo(YESSTR), SCHAR_MAX + 1);
1227c478bd9Sstevel@tonic-gate 	(void) strncpy(nochr, nl_langinfo(NOSTR), SCHAR_MAX + 1);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "frRi")) != EOF)
1257c478bd9Sstevel@tonic-gate 		switch (c) {
1267c478bd9Sstevel@tonic-gate 		case 'f':
1277c478bd9Sstevel@tonic-gate 			silent = TRUE;
1287c478bd9Sstevel@tonic-gate #ifdef XPG4
1297c478bd9Sstevel@tonic-gate 			interactive = FALSE;
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate 			break;
1327c478bd9Sstevel@tonic-gate 		case 'i':
1337c478bd9Sstevel@tonic-gate 			interactive = TRUE;
1347c478bd9Sstevel@tonic-gate #ifdef XPG4
1357c478bd9Sstevel@tonic-gate 			silent = FALSE;
1367c478bd9Sstevel@tonic-gate #endif
1377c478bd9Sstevel@tonic-gate 			break;
1387c478bd9Sstevel@tonic-gate 		case 'r':
1397c478bd9Sstevel@tonic-gate 		case 'R':
1407c478bd9Sstevel@tonic-gate 			recursive = TRUE;
1417c478bd9Sstevel@tonic-gate 			break;
1427c478bd9Sstevel@tonic-gate 		case '?':
1437c478bd9Sstevel@tonic-gate 			errflg = 1;
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/*
1487c478bd9Sstevel@tonic-gate 	 * For BSD compatibility allow '-' to delimit the end
1497c478bd9Sstevel@tonic-gate 	 * of options.  However, if options were already explicitly
1507c478bd9Sstevel@tonic-gate 	 * terminated with '--', then treat '-' literally: otherwise,
1517c478bd9Sstevel@tonic-gate 	 * "rm -- -" won't remove '-'.
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 	if (optind < argc &&
1547c478bd9Sstevel@tonic-gate 	    strcmp(argv[optind], "-") == 0 &&
1557c478bd9Sstevel@tonic-gate 	    strcmp(argv[optind - 1], "--") != 0)
1567c478bd9Sstevel@tonic-gate 		optind++;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	argc -= optind;
1597c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if ((argc < 1 && !silent) || errflg) {
1627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1637c478bd9Sstevel@tonic-gate 			gettext("usage: rm [-fiRr] file ...\n"));
1647c478bd9Sstevel@tonic-gate 		exit(2);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl)) {
1687c478bd9Sstevel@tonic-gate 		perror("getrlimit");
1697c478bd9Sstevel@tonic-gate 		exit(2);
1707c478bd9Sstevel@tonic-gate 	} else
1717c478bd9Sstevel@tonic-gate 		maxfiles = rl.rlim_cur - 2;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
174*014a7923Sas145665 		tree = NULL;
1757c478bd9Sstevel@tonic-gate 		rm(*argv, 1);
1767c478bd9Sstevel@tonic-gate 		argv++;
177*014a7923Sas145665 		destroy_tree(tree);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 	cleanup();
1807c478bd9Sstevel@tonic-gate 	return (errcode ? 2 : 0);
1817c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate static void
1857c478bd9Sstevel@tonic-gate rm(char *path, int first)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	struct stat buffer;
1887c478bd9Sstevel@tonic-gate 	char	*filepath;
1897c478bd9Sstevel@tonic-gate 	char	*p;
1907c478bd9Sstevel@tonic-gate 	char	resolved_path[PATH_MAX];
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/*
1937c478bd9Sstevel@tonic-gate 	 * Check file to see if it exists.
1947c478bd9Sstevel@tonic-gate 	 */
1957c478bd9Sstevel@tonic-gate 	if (lstat(path, &buffer) == FAIL) {
1967c478bd9Sstevel@tonic-gate 		if (!silent) {
1977c478bd9Sstevel@tonic-gate 			perror(path);
1987c478bd9Sstevel@tonic-gate 			++errcode;
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 		return;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* prevent removal of / but allow removal of sym-links */
2047c478bd9Sstevel@tonic-gate 	if (!S_ISLNK(buffer.st_mode) && realpath(path, resolved_path) != NULL &&
2057c478bd9Sstevel@tonic-gate 	    strcmp(resolved_path, "/") == 0) {
2067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2077c478bd9Sstevel@tonic-gate 		    gettext("rm of %s is not allowed\n"), resolved_path);
2087c478bd9Sstevel@tonic-gate 		errcode++;
2097c478bd9Sstevel@tonic-gate 		return;
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/* prevent removal of . or .. (directly) */
2137c478bd9Sstevel@tonic-gate 	if (p = strrchr(path, '/'))
2147c478bd9Sstevel@tonic-gate 		p++;
2157c478bd9Sstevel@tonic-gate 	else
2167c478bd9Sstevel@tonic-gate 		p = path;
2177c478bd9Sstevel@tonic-gate 	if (strcmp(".", p) == 0 || strcmp("..", p) == 0) {
2187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2197c478bd9Sstevel@tonic-gate 			gettext("rm of %s is not allowed\n"), path);
2207c478bd9Sstevel@tonic-gate 		errcode++;
2217c478bd9Sstevel@tonic-gate 		return;
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 	/*
2247c478bd9Sstevel@tonic-gate 	 * If it's a directory, remove its contents.
2257c478bd9Sstevel@tonic-gate 	 */
2267c478bd9Sstevel@tonic-gate 	if (DIRECTORY) {
2277c478bd9Sstevel@tonic-gate 		/*
2287c478bd9Sstevel@tonic-gate 		 * If "-r" wasn't specified, trying to remove directories
2297c478bd9Sstevel@tonic-gate 		 * is an error.
2307c478bd9Sstevel@tonic-gate 		 */
2317c478bd9Sstevel@tonic-gate 		if (!recursive) {
2327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2337c478bd9Sstevel@tonic-gate 			    gettext("rm: %s is a directory\n"), path);
2347c478bd9Sstevel@tonic-gate 			++errcode;
2357c478bd9Sstevel@tonic-gate 			return;
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		if (first_dir) {
2397c478bd9Sstevel@tonic-gate 			check_homedir();
2407c478bd9Sstevel@tonic-gate 			first_dir = 0;
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		undir(path, first, buffer.st_dev, buffer.st_ino);
2447c478bd9Sstevel@tonic-gate 		return;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	filepath = get_filename(path);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*
2497c478bd9Sstevel@tonic-gate 	 * If interactive, ask for acknowledgement.
2507c478bd9Sstevel@tonic-gate 	 *
2517c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE - The following message will contain the
2527c478bd9Sstevel@tonic-gate 	 * first character of the strings for "yes" and "no" defined
2537c478bd9Sstevel@tonic-gate 	 * in the file "nl_langinfo.po".  After substitution, the
2547c478bd9Sstevel@tonic-gate 	 * message will appear as follows:
2557c478bd9Sstevel@tonic-gate 	 *	rm: remove <filename> (y/n)?
2567c478bd9Sstevel@tonic-gate 	 * For example, in German, this will appear as
2577c478bd9Sstevel@tonic-gate 	 *	rm: l�schen <filename> (j/n)?
2587c478bd9Sstevel@tonic-gate 	 * where j=ja, n=nein, <filename>=the file to be removed
2597c478bd9Sstevel@tonic-gate 	 *
2607c478bd9Sstevel@tonic-gate 	 */
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (interactive) {
2647c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rm: remove %s (%s/%s)? "),
2657c478bd9Sstevel@tonic-gate 			filepath, yeschr, nochr);
2667c478bd9Sstevel@tonic-gate 		if (!yes()) {
2677c478bd9Sstevel@tonic-gate 			free(filepath);
2687c478bd9Sstevel@tonic-gate 			return;
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 	} else if (!silent) {
2717c478bd9Sstevel@tonic-gate 		/*
2727c478bd9Sstevel@tonic-gate 		 * If not silent, and stdin is a terminal, and there's
2737c478bd9Sstevel@tonic-gate 		 * no write access, and the file isn't a symbolic link,
2747c478bd9Sstevel@tonic-gate 		 * ask for permission.
2757c478bd9Sstevel@tonic-gate 		 *
2767c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE - The following message will contain the
2777c478bd9Sstevel@tonic-gate 		 * first character of the strings for "yes" and "no" defined
2787c478bd9Sstevel@tonic-gate 		 * in the file "nl_langinfo.po".  After substitution, the
2797c478bd9Sstevel@tonic-gate 		 * message will appear as follows:
2807c478bd9Sstevel@tonic-gate 		 * 	rm: <filename>: override protection XXX (y/n)?
2817c478bd9Sstevel@tonic-gate 		 * where XXX is the permission mode bits of the file in octal
2827c478bd9Sstevel@tonic-gate 		 * and <filename> is the file to be removed
2837c478bd9Sstevel@tonic-gate 		 *
2847c478bd9Sstevel@tonic-gate 		 */
2857c478bd9Sstevel@tonic-gate 		if (!SYMLINK && access(path, W_OK) == FAIL &&
2867c478bd9Sstevel@tonic-gate 		    isatty(fileno(stdin))) {
2877c478bd9Sstevel@tonic-gate 			(void) printf(
2887c478bd9Sstevel@tonic-gate 			    gettext("rm: %s: override protection %o (%s/%s)? "),
2897c478bd9Sstevel@tonic-gate 			    filepath, buffer.st_mode & 0777, yeschr, nochr);
2907c478bd9Sstevel@tonic-gate 			/*
2917c478bd9Sstevel@tonic-gate 			 * If permission isn't given, skip the file.
2927c478bd9Sstevel@tonic-gate 			 */
2937c478bd9Sstevel@tonic-gate 			if (!yes()) {
2947c478bd9Sstevel@tonic-gate 				free(filepath);
2957c478bd9Sstevel@tonic-gate 				return;
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/*
3017c478bd9Sstevel@tonic-gate 	 * If the unlink fails, inform the user. For /usr/bin/rm, only inform
3027c478bd9Sstevel@tonic-gate 	 * the user if interactive or not silent.
3037c478bd9Sstevel@tonic-gate 	 * If unlink fails with errno = ENOENT because file was removed
3047c478bd9Sstevel@tonic-gate 	 * in between the lstat call and unlink don't inform the user and
3057c478bd9Sstevel@tonic-gate 	 * don't change errcode.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (unlink(path) == FAIL) {
3097c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
3107c478bd9Sstevel@tonic-gate 			free(filepath);
3117c478bd9Sstevel@tonic-gate 			return;
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate #ifndef XPG4
3147c478bd9Sstevel@tonic-gate 		if (!silent || interactive) {
3157c478bd9Sstevel@tonic-gate #endif
3167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3177c478bd9Sstevel@tonic-gate 				    gettext("rm: %s not removed: "), filepath);
3187c478bd9Sstevel@tonic-gate 				perror("");
3197c478bd9Sstevel@tonic-gate #ifndef XPG4
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate #endif
3227c478bd9Sstevel@tonic-gate 		++errcode;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	free(filepath);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate static void
3297c478bd9Sstevel@tonic-gate undir(char *path, int first, dev_t dev, ino_t ino)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	char	*newpath;
3327c478bd9Sstevel@tonic-gate 	DIR	*name;
3337c478bd9Sstevel@tonic-gate 	struct dirent *direct;
3347c478bd9Sstevel@tonic-gate 	int	ismypath;
335*014a7923Sas145665 	int	ret;
3367c478bd9Sstevel@tonic-gate 	int	chdir_failed = 0;
3377c478bd9Sstevel@tonic-gate 	size_t	len;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	push_name(path, first);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * If interactive and this file isn't in the path of the
3437c478bd9Sstevel@tonic-gate 	 * current working directory, ask for acknowledgement.
3447c478bd9Sstevel@tonic-gate 	 *
3457c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE - The following message will contain the
3467c478bd9Sstevel@tonic-gate 	 * first character of the strings for "yes" and "no" defined
3477c478bd9Sstevel@tonic-gate 	 * in the file "nl_langinfo.po".  After substitution, the
3487c478bd9Sstevel@tonic-gate 	 * message will appear as follows:
3497c478bd9Sstevel@tonic-gate 	 *	rm: examine files in directory <directoryname> (y/n)?
3507c478bd9Sstevel@tonic-gate 	 * where <directoryname> is the directory to be removed
3517c478bd9Sstevel@tonic-gate 	 *
3527c478bd9Sstevel@tonic-gate 	 */
3537c478bd9Sstevel@tonic-gate 	ismypath = mypath(dev, ino);
3547c478bd9Sstevel@tonic-gate 	if (interactive) {
3557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3567c478bd9Sstevel@tonic-gate 		    gettext("rm: examine files in directory %s (%s/%s)? "),
3577c478bd9Sstevel@tonic-gate 			fullpath, yeschr, nochr);
3587c478bd9Sstevel@tonic-gate 		/*
3597c478bd9Sstevel@tonic-gate 		 * If the answer is no, skip the directory.
3607c478bd9Sstevel@tonic-gate 		 */
3617c478bd9Sstevel@tonic-gate 		if (!yes()) {
3627c478bd9Sstevel@tonic-gate 			pop_name(first);
3637c478bd9Sstevel@tonic-gate 			return;
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate #ifdef XPG4
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * XCU4 and POSIX.2: If not interactive and file is not in the
3707c478bd9Sstevel@tonic-gate 	 * path of the current working directory, check to see whether
3717c478bd9Sstevel@tonic-gate 	 * or not directory is readable or writable and if not,
3727c478bd9Sstevel@tonic-gate 	 * prompt user for response.
3737c478bd9Sstevel@tonic-gate 	 */
3747c478bd9Sstevel@tonic-gate 	if (!interactive && !ismypath &&
3757c478bd9Sstevel@tonic-gate 	    (access(path, W_OK|X_OK) == FAIL) && isatty(fileno(stdin))) {
3767c478bd9Sstevel@tonic-gate 		if (!silent) {
3777c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3787c478bd9Sstevel@tonic-gate 			    gettext(
3797c478bd9Sstevel@tonic-gate 				"rm: examine files in directory %s (%s/%s)? "),
3807c478bd9Sstevel@tonic-gate 			    fullpath, yeschr, nochr);
3817c478bd9Sstevel@tonic-gate 			/*
3827c478bd9Sstevel@tonic-gate 			 * If the answer is no, skip the directory.
3837c478bd9Sstevel@tonic-gate 			 */
3847c478bd9Sstevel@tonic-gate 			if (!yes()) {
3857c478bd9Sstevel@tonic-gate 				pop_name(first);
3867c478bd9Sstevel@tonic-gate 				return;
3877c478bd9Sstevel@tonic-gate 			}
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate #endif
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/*
393*014a7923Sas145665 	 * Add this node to the search tree so we don't
394*014a7923Sas145665 	 * get into a endless loop. If the add fails then
395*014a7923Sas145665 	 * we have visited this node before.
396*014a7923Sas145665 	 */
397*014a7923Sas145665 	ret = add_tnode(&tree, dev, ino);
398*014a7923Sas145665 	if (ret != 1) {
399*014a7923Sas145665 		if (ret == 0) {
400*014a7923Sas145665 			(void) fprintf(stderr,
401*014a7923Sas145665 			    gettext("rm: cycle detected for %s\n"),
402*014a7923Sas145665 			    fullpath);
403*014a7923Sas145665 		} else if (ret == -1) {
404*014a7923Sas145665 			perror("rm");
405*014a7923Sas145665 		}
406*014a7923Sas145665 		errcode++;
407*014a7923Sas145665 		pop_name(first);
408*014a7923Sas145665 		return;
409*014a7923Sas145665 	}
410*014a7923Sas145665 
411*014a7923Sas145665 	/*
4127c478bd9Sstevel@tonic-gate 	 * Open the directory for reading.
4137c478bd9Sstevel@tonic-gate 	 */
4147c478bd9Sstevel@tonic-gate 	if ((name = opendir(path)) == NULL) {
4157c478bd9Sstevel@tonic-gate 		int	saveerrno = errno;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		/*
4187c478bd9Sstevel@tonic-gate 		 * If interactive, ask for acknowledgement.
4197c478bd9Sstevel@tonic-gate 		 */
4207c478bd9Sstevel@tonic-gate 		if (interactive) {
4217c478bd9Sstevel@tonic-gate 			/*
4227c478bd9Sstevel@tonic-gate 			 * Print an error message that
4237c478bd9Sstevel@tonic-gate 			 * we could not read the directory
4247c478bd9Sstevel@tonic-gate 			 * as the user wanted to examine
4257c478bd9Sstevel@tonic-gate 			 * files in the directory.  Only
4267c478bd9Sstevel@tonic-gate 			 * affect the error status if
4277c478bd9Sstevel@tonic-gate 			 * user doesn't want to remove the
4287c478bd9Sstevel@tonic-gate 			 * directory as we still may be able
4297c478bd9Sstevel@tonic-gate 			 * remove the directory successfully.
4307c478bd9Sstevel@tonic-gate 			 */
4317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4327c478bd9Sstevel@tonic-gate 			    "rm: cannot read directory %s: "),
4337c478bd9Sstevel@tonic-gate 			    fullpath);
4347c478bd9Sstevel@tonic-gate 			errno = saveerrno;
4357c478bd9Sstevel@tonic-gate 			perror("");
4367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4377c478bd9Sstevel@tonic-gate 			    "rm: remove %s: (%s/%s)? "),
4387c478bd9Sstevel@tonic-gate 			    fullpath, yeschr, nochr);
4397c478bd9Sstevel@tonic-gate 			if (!yes()) {
4407c478bd9Sstevel@tonic-gate 				++errcode;
4417c478bd9Sstevel@tonic-gate 				pop_name(first);
4427c478bd9Sstevel@tonic-gate 				return;
4437c478bd9Sstevel@tonic-gate 			}
4447c478bd9Sstevel@tonic-gate 		}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		/*
4477c478bd9Sstevel@tonic-gate 		 * If the directory is empty, we may be able to
4487c478bd9Sstevel@tonic-gate 		 * go ahead and remove it.
4497c478bd9Sstevel@tonic-gate 		 */
4507c478bd9Sstevel@tonic-gate 		if (rmdir(path) == FAIL) {
4517c478bd9Sstevel@tonic-gate 			if (interactive) {
4527c478bd9Sstevel@tonic-gate 				int	rmdirerr = errno;
4537c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4547c478bd9Sstevel@tonic-gate 				    "rm: Unable to remove directory %s: "),
4557c478bd9Sstevel@tonic-gate 				    fullpath);
4567c478bd9Sstevel@tonic-gate 				errno = rmdirerr;
4577c478bd9Sstevel@tonic-gate 				perror("");
4587c478bd9Sstevel@tonic-gate 			} else {
4597c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4607c478bd9Sstevel@tonic-gate 				    "rm: cannot read directory %s: "),
4617c478bd9Sstevel@tonic-gate 				    fullpath);
4627c478bd9Sstevel@tonic-gate 				errno = saveerrno;
4637c478bd9Sstevel@tonic-gate 				perror("");
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate 			++errcode;
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		/* Continue to next file/directory rather than exit */
4697c478bd9Sstevel@tonic-gate 		pop_name(first);
4707c478bd9Sstevel@tonic-gate 		return;
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	/*
4747c478bd9Sstevel@tonic-gate 	 * XCU4 requires that rm -r descend the directory
4757c478bd9Sstevel@tonic-gate 	 * hierarchy without regard to PATH_MAX.  If we can't
4767c478bd9Sstevel@tonic-gate 	 * chdir() do not increment error counter and do not
4777c478bd9Sstevel@tonic-gate 	 * print message.
4787c478bd9Sstevel@tonic-gate 	 *
4797c478bd9Sstevel@tonic-gate 	 * However, if we cannot chdir because someone has taken away
4807c478bd9Sstevel@tonic-gate 	 * execute access we may still be able to delete the directory
4817c478bd9Sstevel@tonic-gate 	 * if it's empty. The old rm could do this.
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	if (chdir(path) == -1) {
4857c478bd9Sstevel@tonic-gate 		chdir_failed = 1;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * Read every directory entry.
4907c478bd9Sstevel@tonic-gate 	 */
4917c478bd9Sstevel@tonic-gate 	while ((direct = readdir(name)) != NULL) {
4927c478bd9Sstevel@tonic-gate 		/*
4937c478bd9Sstevel@tonic-gate 		 * Ignore "." and ".." entries.
4947c478bd9Sstevel@tonic-gate 		 */
4957c478bd9Sstevel@tonic-gate 		if (strcmp(direct->d_name, ".") == 0 ||
4967c478bd9Sstevel@tonic-gate 		    strcmp(direct->d_name, "..") == 0)
4977c478bd9Sstevel@tonic-gate 			continue;
4987c478bd9Sstevel@tonic-gate 		/*
4997c478bd9Sstevel@tonic-gate 		 * Try to remove the file.
5007c478bd9Sstevel@tonic-gate 		 */
5017c478bd9Sstevel@tonic-gate 		len = strlen(direct->d_name) + 1;
5027c478bd9Sstevel@tonic-gate 		if (chdir_failed) {
5037c478bd9Sstevel@tonic-gate 			len += strlen(path) + 2;
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		newpath = malloc(len);
5077c478bd9Sstevel@tonic-gate 		if (newpath == NULL) {
5087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5097c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
5107c478bd9Sstevel@tonic-gate 			cleanup();
5117c478bd9Sstevel@tonic-gate 			exit(1);
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		if (!chdir_failed) {
5157c478bd9Sstevel@tonic-gate 			(void) strcpy(newpath, direct->d_name);
5167c478bd9Sstevel@tonic-gate 		} else {
5177c478bd9Sstevel@tonic-gate 			(void) snprintf(newpath, len, "%s/%s",
5187c478bd9Sstevel@tonic-gate 			    path, direct->d_name);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 		/*
5237c478bd9Sstevel@tonic-gate 		 * If a spare file descriptor is available, just call the
5247c478bd9Sstevel@tonic-gate 		 * "rm" function with the file name; otherwise close the
5257c478bd9Sstevel@tonic-gate 		 * directory and reopen it when the child is removed.
5267c478bd9Sstevel@tonic-gate 		 */
5277c478bd9Sstevel@tonic-gate 		if (name->dd_fd >= maxfiles) {
5287c478bd9Sstevel@tonic-gate 			(void) closedir(name);
5297c478bd9Sstevel@tonic-gate 			rm(newpath, 0);
5307c478bd9Sstevel@tonic-gate 			if (!chdir_failed)
5317c478bd9Sstevel@tonic-gate 				name = opendir(".");
5327c478bd9Sstevel@tonic-gate 			else
5337c478bd9Sstevel@tonic-gate 				name = opendir(path);
5347c478bd9Sstevel@tonic-gate 			if (name == NULL) {
5357c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5367c478bd9Sstevel@tonic-gate 				    gettext("rm: cannot read directory %s: "),
5377c478bd9Sstevel@tonic-gate 				    fullpath);
5387c478bd9Sstevel@tonic-gate 				perror("");
5397c478bd9Sstevel@tonic-gate 				cleanup();
5407c478bd9Sstevel@tonic-gate 				exit(2);
5417c478bd9Sstevel@tonic-gate 			}
5427c478bd9Sstevel@tonic-gate 		} else
5437c478bd9Sstevel@tonic-gate 			rm(newpath, 0);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 		free(newpath);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * Close the directory we just finished reading.
5507c478bd9Sstevel@tonic-gate 	 */
5517c478bd9Sstevel@tonic-gate 	(void) closedir(name);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/*
5547c478bd9Sstevel@tonic-gate 	 * The contents of the directory have been removed.  If the
5557c478bd9Sstevel@tonic-gate 	 * directory itself is in the path of the current working
5567c478bd9Sstevel@tonic-gate 	 * directory, don't try to remove it.
5577c478bd9Sstevel@tonic-gate 	 * When the directory itself is the current working directory, mypath()
5587c478bd9Sstevel@tonic-gate 	 * has a return code == 2.
5597c478bd9Sstevel@tonic-gate 	 *
5607c478bd9Sstevel@tonic-gate 	 * XCU4: Because we've descended the directory hierarchy in order
5617c478bd9Sstevel@tonic-gate 	 * to avoid PATH_MAX limitation, we must now start ascending
5627c478bd9Sstevel@tonic-gate 	 * one level at a time and remove files/directories.
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	if (!chdir_failed) {
5667c478bd9Sstevel@tonic-gate 		if (first)
5677c478bd9Sstevel@tonic-gate 			chdir_home();
5687c478bd9Sstevel@tonic-gate 		else if (chdir("..") == -1) {
5697c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5707c478bd9Sstevel@tonic-gate 			    gettext("rm: cannot change to parent of "
5717c478bd9Sstevel@tonic-gate 				    "directory %s: "),
5727c478bd9Sstevel@tonic-gate 			    fullpath);
5737c478bd9Sstevel@tonic-gate 			perror("");
5747c478bd9Sstevel@tonic-gate 			cleanup();
5757c478bd9Sstevel@tonic-gate 			exit(2);
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	switch (ismypath) {
5807c478bd9Sstevel@tonic-gate 	case 3:
5817c478bd9Sstevel@tonic-gate 		pop_name(first);
5827c478bd9Sstevel@tonic-gate 		return;
5837c478bd9Sstevel@tonic-gate 	case 2:
5847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5857c478bd9Sstevel@tonic-gate 		    gettext("rm: Cannot remove any directory in the path "
5867c478bd9Sstevel@tonic-gate 			"of the current working directory\n%s\n"), fullpath);
5877c478bd9Sstevel@tonic-gate 		++errcode;
5887c478bd9Sstevel@tonic-gate 		pop_name(first);
5897c478bd9Sstevel@tonic-gate 		return;
5907c478bd9Sstevel@tonic-gate 	case 1:
5917c478bd9Sstevel@tonic-gate 		++errcode;
5927c478bd9Sstevel@tonic-gate 		pop_name(first);
5937c478bd9Sstevel@tonic-gate 		return;
5947c478bd9Sstevel@tonic-gate 	case 0:
5957c478bd9Sstevel@tonic-gate 		break;
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	/*
5997c478bd9Sstevel@tonic-gate 	 * If interactive, ask for acknowledgement.
6007c478bd9Sstevel@tonic-gate 	 */
6017c478bd9Sstevel@tonic-gate 	if (interactive) {
6027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rm: remove %s: (%s/%s)? "),
6037c478bd9Sstevel@tonic-gate 			fullpath, yeschr, nochr);
6047c478bd9Sstevel@tonic-gate 		if (!yes()) {
6057c478bd9Sstevel@tonic-gate 			pop_name(first);
6067c478bd9Sstevel@tonic-gate 			return;
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 	if (rmdir(path) == FAIL) {
6107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6117c478bd9Sstevel@tonic-gate 			gettext("rm: Unable to remove directory %s: "),
6127c478bd9Sstevel@tonic-gate 			fullpath);
6137c478bd9Sstevel@tonic-gate 		perror("");
6147c478bd9Sstevel@tonic-gate 		++errcode;
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 	pop_name(first);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate static int
6217c478bd9Sstevel@tonic-gate yes(void)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	int	i, b;
6247c478bd9Sstevel@tonic-gate 	char	ans[SCHAR_MAX + 1];
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	for (i = 0; ; i++) {
6277c478bd9Sstevel@tonic-gate 		b = getchar();
6287c478bd9Sstevel@tonic-gate 		if (b == '\n' || b == '\0' || b == EOF) {
6297c478bd9Sstevel@tonic-gate 			ans[i] = 0;
6307c478bd9Sstevel@tonic-gate 			break;
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate 		if (i < SCHAR_MAX)
6337c478bd9Sstevel@tonic-gate 			ans[i] = b;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	if (i >= SCHAR_MAX) {
6367c478bd9Sstevel@tonic-gate 		i = SCHAR_MAX;
6377c478bd9Sstevel@tonic-gate 		ans[SCHAR_MAX] = 0;
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 	if ((i == 0) | (strncmp(yeschr, ans, i)))
6407c478bd9Sstevel@tonic-gate 		return (0);
6417c478bd9Sstevel@tonic-gate 	return (1);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate static int
6467c478bd9Sstevel@tonic-gate mypath(dev_t dev, ino_t ino)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	struct dir_id *curdir;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/*
6517c478bd9Sstevel@tonic-gate 	 * Check to see if this is our current directory
6527c478bd9Sstevel@tonic-gate 	 * Indicated by return 2;
6537c478bd9Sstevel@tonic-gate 	 */
6547c478bd9Sstevel@tonic-gate 	if (dev == homedir.dev && ino == homedir.inode) {
6557c478bd9Sstevel@tonic-gate 		return (2);
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	curdir = homedir.next;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	while (curdir != NULL) {
6617c478bd9Sstevel@tonic-gate 		/*
6627c478bd9Sstevel@tonic-gate 		 * If we find a match, the directory (dev, ino) passed to
6637c478bd9Sstevel@tonic-gate 		 * mypath() is an ancestor of ours. Indicated by return 3.
6647c478bd9Sstevel@tonic-gate 		 */
6657c478bd9Sstevel@tonic-gate 		if (curdir->dev == dev && curdir->inode == ino)
6667c478bd9Sstevel@tonic-gate 			return (3);
6677c478bd9Sstevel@tonic-gate 		curdir = curdir->next;
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 	/*
6707c478bd9Sstevel@tonic-gate 	 * parent_err indicates we couldn't stat or chdir to
6717c478bd9Sstevel@tonic-gate 	 * one of our parent dirs, so the linked list of dir_id structs
6727c478bd9Sstevel@tonic-gate 	 * is incomplete
6737c478bd9Sstevel@tonic-gate 	 */
6747c478bd9Sstevel@tonic-gate 	if (parent_err) {
6757c478bd9Sstevel@tonic-gate #ifndef XPG4
6767c478bd9Sstevel@tonic-gate 		if (!silent || interactive) {
6777c478bd9Sstevel@tonic-gate #endif
6787c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("rm: cannot determine "
6797c478bd9Sstevel@tonic-gate 			    "if this is an ancestor of the current "
6807c478bd9Sstevel@tonic-gate 			    "working directory\n%s\n"), fullpath);
6817c478bd9Sstevel@tonic-gate #ifndef XPG4
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate #endif
6847c478bd9Sstevel@tonic-gate 		/* assume it is. least dangerous */
6857c478bd9Sstevel@tonic-gate 		return (1);
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	return (0);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate static int maxlen;
6917c478bd9Sstevel@tonic-gate static int curlen;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate static char *
6947c478bd9Sstevel@tonic-gate get_filename(char *name)
6957c478bd9Sstevel@tonic-gate {
6967c478bd9Sstevel@tonic-gate 	char *path;
6977c478bd9Sstevel@tonic-gate 	size_t len;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (fullpath == NULL || *fullpath == '\0') {
7007c478bd9Sstevel@tonic-gate 		path = strdup(name);
7017c478bd9Sstevel@tonic-gate 		if (path == NULL) {
7027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7037c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
7047c478bd9Sstevel@tonic-gate 			cleanup();
7057c478bd9Sstevel@tonic-gate 			exit(1);
7067c478bd9Sstevel@tonic-gate 		}
7077c478bd9Sstevel@tonic-gate 	} else {
7087c478bd9Sstevel@tonic-gate 		len = strlen(fullpath) + strlen(name) + 2;
7097c478bd9Sstevel@tonic-gate 		path = malloc(len);
7107c478bd9Sstevel@tonic-gate 		if (path == NULL) {
7117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7127c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
7137c478bd9Sstevel@tonic-gate 			cleanup();
7147c478bd9Sstevel@tonic-gate 			exit(1);
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 		(void) snprintf(path, len, "%s/%s", fullpath, name);
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 	return (path);
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate static void
7227c478bd9Sstevel@tonic-gate push_name(char *name, int first)
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	int	namelen;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	namelen = strlen(name) + 1; /* 1 for "/" */
7277c478bd9Sstevel@tonic-gate 	if ((curlen + namelen) >= maxlen) {
7287c478bd9Sstevel@tonic-gate 		maxlen += PATH_MAX;
7297c478bd9Sstevel@tonic-gate 		fullpath = (char *)realloc(fullpath, (size_t)(maxlen + 1));
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 	if (first) {
7327c478bd9Sstevel@tonic-gate 		(void) strcpy(fullpath, name);
7337c478bd9Sstevel@tonic-gate 	} else {
7347c478bd9Sstevel@tonic-gate 		(void) strcat(fullpath, "/");
7357c478bd9Sstevel@tonic-gate 		(void) strcat(fullpath, name);
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 	curlen = strlen(fullpath);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate static void
7417c478bd9Sstevel@tonic-gate pop_name(int first)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate 	char *slash;
7447c478bd9Sstevel@tonic-gate 	if (first) {
7457c478bd9Sstevel@tonic-gate 		*fullpath = '\0';
7467c478bd9Sstevel@tonic-gate 		return;
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	slash = strrchr(fullpath, '/');
7497c478bd9Sstevel@tonic-gate 	if (slash)
7507c478bd9Sstevel@tonic-gate 		*slash = '\0';
7517c478bd9Sstevel@tonic-gate 	else
7527c478bd9Sstevel@tonic-gate 		*fullpath = '\0';
7537c478bd9Sstevel@tonic-gate 	curlen = strlen(fullpath);
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate static void
7577c478bd9Sstevel@tonic-gate force_chdir(char *dirname)
7587c478bd9Sstevel@tonic-gate {
7597c478bd9Sstevel@tonic-gate 	char 	*pathname, *mp, *tp;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	/* use pathname instead of dirname, so dirname won't be modified */
7627c478bd9Sstevel@tonic-gate 	if ((pathname = strdup(dirname)) == NULL) {
7637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rm: strdup: "));
7647c478bd9Sstevel@tonic-gate 		perror("");
7657c478bd9Sstevel@tonic-gate 		cleanup();
7667c478bd9Sstevel@tonic-gate 		exit(2);
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	/* pathname is an absolute full path from getcwd() */
7707c478bd9Sstevel@tonic-gate 	mp = pathname;
7717c478bd9Sstevel@tonic-gate 	while (mp) {
7727c478bd9Sstevel@tonic-gate 		tp = strchr(mp, '/');
7737c478bd9Sstevel@tonic-gate 		if (strlen(mp) >= PATH_MAX) {
7747c478bd9Sstevel@tonic-gate 			/*
7757c478bd9Sstevel@tonic-gate 			 * after the first iteration through this
7767c478bd9Sstevel@tonic-gate 			 * loop, the below will NULL out the '/'
7777c478bd9Sstevel@tonic-gate 			 * which follows the first dir on pathname
7787c478bd9Sstevel@tonic-gate 			 */
7797c478bd9Sstevel@tonic-gate 			*tp = 0;
7807c478bd9Sstevel@tonic-gate 			tp++;
7817c478bd9Sstevel@tonic-gate 			if (*mp == NULL)
7827c478bd9Sstevel@tonic-gate 				ch_dir("/");
7837c478bd9Sstevel@tonic-gate 			else
7847c478bd9Sstevel@tonic-gate 				/*
7857c478bd9Sstevel@tonic-gate 				 * mp points to the start of a dirname,
7867c478bd9Sstevel@tonic-gate 				 * terminated by NULL, so ch_dir()
7877c478bd9Sstevel@tonic-gate 				 * here will move down one directory
7887c478bd9Sstevel@tonic-gate 				 */
7897c478bd9Sstevel@tonic-gate 				ch_dir(mp);
7907c478bd9Sstevel@tonic-gate 			/*
7917c478bd9Sstevel@tonic-gate 			 * reset mp to the start of the dirname
7927c478bd9Sstevel@tonic-gate 			 * which follows the one we just chdir'd to
7937c478bd9Sstevel@tonic-gate 			 */
7947c478bd9Sstevel@tonic-gate 			mp = tp;
7957c478bd9Sstevel@tonic-gate 			continue;	/* probably can remove this */
7967c478bd9Sstevel@tonic-gate 		} else {
7977c478bd9Sstevel@tonic-gate 			ch_dir(mp);
7987c478bd9Sstevel@tonic-gate 			break;
7997c478bd9Sstevel@tonic-gate 		}
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 	free(pathname);
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate static void
8057c478bd9Sstevel@tonic-gate ch_dir(char *dirname)
8067c478bd9Sstevel@tonic-gate {
8077c478bd9Sstevel@tonic-gate 	if (chdir(dirname) == -1) {
8087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8097c478bd9Sstevel@tonic-gate 		gettext("rm: cannot change to %s directory: "), dirname);
8107c478bd9Sstevel@tonic-gate 			perror("");
8117c478bd9Sstevel@tonic-gate 			cleanup();
8127c478bd9Sstevel@tonic-gate 			exit(2);
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate static void
8177c478bd9Sstevel@tonic-gate chdir_home(void)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	/*
8207c478bd9Sstevel@tonic-gate 	 * Go back to home dir--the dir from where rm was executed--using
8217c478bd9Sstevel@tonic-gate 	 * one of two methods, depending on which method works
8227c478bd9Sstevel@tonic-gate 	 * for the given permissions of the home dir and its
8237c478bd9Sstevel@tonic-gate 	 * parent directories.
8247c478bd9Sstevel@tonic-gate 	 */
8257c478bd9Sstevel@tonic-gate 	if (homedirfd != -1) {
8267c478bd9Sstevel@tonic-gate 		if (fchdir(homedirfd) == -1) {
8277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8287c478bd9Sstevel@tonic-gate 			    gettext("rm: cannot change to starting "
8297c478bd9Sstevel@tonic-gate 			    "directory: "));
8307c478bd9Sstevel@tonic-gate 			perror("");
8317c478bd9Sstevel@tonic-gate 			cleanup();
8327c478bd9Sstevel@tonic-gate 			exit(2);
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 	} else {
8357c478bd9Sstevel@tonic-gate 		if (strlen(cwd) < PATH_MAX)
8367c478bd9Sstevel@tonic-gate 			ch_dir(cwd);
8377c478bd9Sstevel@tonic-gate 		else
8387c478bd9Sstevel@tonic-gate 			force_chdir(cwd);
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate /*
8437c478bd9Sstevel@tonic-gate  * check_homedir -
8447c478bd9Sstevel@tonic-gate  * is only called the first time rm tries to
8457c478bd9Sstevel@tonic-gate  * remove a directory.  It saves the current directory, i.e.,
8467c478bd9Sstevel@tonic-gate  * home dir, so we can go back to it after traversing elsewhere.
8477c478bd9Sstevel@tonic-gate  * It also saves all the device and inode numbers of each
8487c478bd9Sstevel@tonic-gate  * dir from the home dir back to the root in a linked list, so we
8497c478bd9Sstevel@tonic-gate  * can later check, via mypath(), if we are trying to remove our current
8507c478bd9Sstevel@tonic-gate  * dir or an ancestor.
8517c478bd9Sstevel@tonic-gate  */
8527c478bd9Sstevel@tonic-gate static void
8537c478bd9Sstevel@tonic-gate check_homedir(void)
8547c478bd9Sstevel@tonic-gate {
8557c478bd9Sstevel@tonic-gate 	int	size;	/* size allocated for pathname of home dir (cwd) */
8567c478bd9Sstevel@tonic-gate 	struct stat buffer;
8577c478bd9Sstevel@tonic-gate 	struct dir_id *lastdir, *curdir;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	/*
8607c478bd9Sstevel@tonic-gate 	 * We need to save where we currently are (the "home dir") so
8617c478bd9Sstevel@tonic-gate 	 * we can return after traversing down directories we're
8627c478bd9Sstevel@tonic-gate 	 * removing.  Two methods are attempted:
8637c478bd9Sstevel@tonic-gate 	 *
8647c478bd9Sstevel@tonic-gate 	 * 1) open() the home dir so we can use the fd
8657c478bd9Sstevel@tonic-gate 	 *    to fchdir() back.  This requires read permission
8667c478bd9Sstevel@tonic-gate 	 *    on the home dir.
8677c478bd9Sstevel@tonic-gate 	 *
8687c478bd9Sstevel@tonic-gate 	 * 2) getcwd() so we can chdir() to go back.  This
8697c478bd9Sstevel@tonic-gate 	 *    requires search (x) permission on the home dir,
8707c478bd9Sstevel@tonic-gate 	 *    and read and search permission on all parent dirs.  Also,
8717c478bd9Sstevel@tonic-gate 	 *    getcwd() will not work if the home dir is > 341
8727c478bd9Sstevel@tonic-gate 	 *    directories deep (see open bugid 4033182 - getcwd needs
8737c478bd9Sstevel@tonic-gate 	 *    to work for pathnames of any depth).
8747c478bd9Sstevel@tonic-gate 	 *
8757c478bd9Sstevel@tonic-gate 	 * If neither method works, we can't remove any directories
8767c478bd9Sstevel@tonic-gate 	 * and rm will fail.
8777c478bd9Sstevel@tonic-gate 	 *
8787c478bd9Sstevel@tonic-gate 	 * For future enhancement, a possible 3rd option to use
8797c478bd9Sstevel@tonic-gate 	 * would be to fork a process to remove a directory,
8807c478bd9Sstevel@tonic-gate 	 * eliminating the need to chdir back to the home directory
8817c478bd9Sstevel@tonic-gate 	 * and eliminating the permission restrictions on the home dir
8827c478bd9Sstevel@tonic-gate 	 * or its parent dirs.
8837c478bd9Sstevel@tonic-gate 	 */
8847c478bd9Sstevel@tonic-gate 	homedirfd = open(".", O_RDONLY);
8857c478bd9Sstevel@tonic-gate 	if (homedirfd == -1) {
8867c478bd9Sstevel@tonic-gate 		size = PATH_MAX;
8877c478bd9Sstevel@tonic-gate 		while ((cwd = getcwd(NULL, size)) == NULL) {
8887c478bd9Sstevel@tonic-gate 			if (errno == ERANGE) {
8897c478bd9Sstevel@tonic-gate 				size = PATH_MAX + size;
8907c478bd9Sstevel@tonic-gate 				continue;
8917c478bd9Sstevel@tonic-gate 			} else {
8927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
8937c478bd9Sstevel@tonic-gate 				    gettext("rm: cannot open starting "
8947c478bd9Sstevel@tonic-gate 				    "directory: "));
8957c478bd9Sstevel@tonic-gate 				perror("pwd");
8967c478bd9Sstevel@tonic-gate 				exit(2);
8977c478bd9Sstevel@tonic-gate 			}
8987c478bd9Sstevel@tonic-gate 		}
8997c478bd9Sstevel@tonic-gate 	}
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	/*
9027c478bd9Sstevel@tonic-gate 	 * since we exit on error here, we're guaranteed to at least
9037c478bd9Sstevel@tonic-gate 	 * have info in the first dir_id struct, homedir
9047c478bd9Sstevel@tonic-gate 	 */
9057c478bd9Sstevel@tonic-gate 	if (stat(".", &buffer) == -1) {
9067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9077c478bd9Sstevel@tonic-gate 		    gettext("rm: cannot stat current directory: "));
9087c478bd9Sstevel@tonic-gate 		perror("");
9097c478bd9Sstevel@tonic-gate 		exit(2);
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 	homedir.dev = buffer.st_dev;
9127c478bd9Sstevel@tonic-gate 	homedir.inode = buffer.st_ino;
9137c478bd9Sstevel@tonic-gate 	homedir.next = NULL;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	lastdir = &homedir;
9167c478bd9Sstevel@tonic-gate 	/*
9177c478bd9Sstevel@tonic-gate 	 * Starting from current working directory, walk toward the
9187c478bd9Sstevel@tonic-gate 	 * root, looking at each directory along the way.
9197c478bd9Sstevel@tonic-gate 	 */
9207c478bd9Sstevel@tonic-gate 	for (;;) {
9217c478bd9Sstevel@tonic-gate 		if (chdir("..") == -1 || lstat(".", &buffer) == -1) {
9227c478bd9Sstevel@tonic-gate 			parent_err = 1;
9237c478bd9Sstevel@tonic-gate 			break;
9247c478bd9Sstevel@tonic-gate 		}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 		if ((lastdir->next = malloc(sizeof (struct dir_id))) ==
9277c478bd9Sstevel@tonic-gate 		    NULL) {
9287c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9297c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
9307c478bd9Sstevel@tonic-gate 			cleanup();
9317c478bd9Sstevel@tonic-gate 			exit(1);
9327c478bd9Sstevel@tonic-gate 		}
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 		curdir = lastdir->next;
9357c478bd9Sstevel@tonic-gate 		curdir->dev = buffer.st_dev;
9367c478bd9Sstevel@tonic-gate 		curdir->inode = buffer.st_ino;
9377c478bd9Sstevel@tonic-gate 		curdir->next = NULL;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		/*
9407c478bd9Sstevel@tonic-gate 		 * Stop when we reach the root; note that chdir("..")
9417c478bd9Sstevel@tonic-gate 		 * at the root dir will stay in root. Get rid of
9427c478bd9Sstevel@tonic-gate 		 * the redundant dir_id struct for root.
9437c478bd9Sstevel@tonic-gate 		 */
9447c478bd9Sstevel@tonic-gate 		if (curdir->dev == lastdir->dev && curdir->inode ==
9457c478bd9Sstevel@tonic-gate 		    lastdir->inode) {
9467c478bd9Sstevel@tonic-gate 			lastdir->next = NULL;
9477c478bd9Sstevel@tonic-gate 			free(curdir);
9487c478bd9Sstevel@tonic-gate 			break;
9497c478bd9Sstevel@tonic-gate 		}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 			/* loop again to go back another level */
9527c478bd9Sstevel@tonic-gate 		lastdir = curdir;
9537c478bd9Sstevel@tonic-gate 	}
9547c478bd9Sstevel@tonic-gate 		/* go back to home directory */
9557c478bd9Sstevel@tonic-gate 	chdir_home();
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate /*
9597c478bd9Sstevel@tonic-gate  * cleanup the dynamically-allocated list of device numbers and inodes,
9607c478bd9Sstevel@tonic-gate  * if any.  If homedir was never used, it is external and static so
9617c478bd9Sstevel@tonic-gate  * it is guaranteed initialized to zero, thus homedir.next would be NULL.
9627c478bd9Sstevel@tonic-gate  */
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate static void
9657c478bd9Sstevel@tonic-gate cleanup(void) {
9667c478bd9Sstevel@tonic-gate 	struct dir_id *lastdir, *curdir;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	curdir = homedir.next;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	while (curdir != NULL) {
9717c478bd9Sstevel@tonic-gate 		lastdir = curdir;
9727c478bd9Sstevel@tonic-gate 		curdir = curdir->next;
9737c478bd9Sstevel@tonic-gate 		free(lastdir);
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate }
976