xref: /titanic_53/usr/src/cmd/find/find.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23da1a9cbeSjonb  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27da1a9cbeSjonb #pragma ident	"%Z%%M%	%I%	%E% SMI"
28da1a9cbeSjonb 
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
307c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*	Parts of this product may be derived from		*/
347c478bd9Sstevel@tonic-gate /*	Mortice Kern Systems Inc. and Berkeley 4.3 BSD systems.	*/
357c478bd9Sstevel@tonic-gate /*	licensed from  Mortice Kern Systems Inc. and 		*/
367c478bd9Sstevel@tonic-gate /*	the University of California.				*/
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * Copyright 1985, 1990 by Mortice Kern Systems Inc.  All rights reserved.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <pwd.h>
457c478bd9Sstevel@tonic-gate #include <grp.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/stat.h>
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include <sys/acl.h>
507c478bd9Sstevel@tonic-gate #include <limits.h>
517c478bd9Sstevel@tonic-gate #include <unistd.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
537c478bd9Sstevel@tonic-gate #include <locale.h>
547c478bd9Sstevel@tonic-gate #include <string.h>
557c478bd9Sstevel@tonic-gate #include <strings.h>
567c478bd9Sstevel@tonic-gate #include <libgen.h>
577c478bd9Sstevel@tonic-gate #include <ctype.h>
587c478bd9Sstevel@tonic-gate #include <wait.h>
597c478bd9Sstevel@tonic-gate #include <fnmatch.h>
607c478bd9Sstevel@tonic-gate #include <langinfo.h>
617c478bd9Sstevel@tonic-gate #include <ftw.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	A_DAY		(long)(60*60*24)	/* a day full of seconds */
64da1a9cbeSjonb #define	A_MIN		(long)(60)
657c478bd9Sstevel@tonic-gate #define	BLKSIZ		512
667c478bd9Sstevel@tonic-gate #define	round(x, s)	(((x)+(s)-1)&~((s)-1))
677c478bd9Sstevel@tonic-gate #ifndef FTW_SLN
687c478bd9Sstevel@tonic-gate #define	FTW_SLN		7
697c478bd9Sstevel@tonic-gate #endif
707c478bd9Sstevel@tonic-gate #define	LINEBUF_SIZE		LINE_MAX	/* input or output lines */
717c478bd9Sstevel@tonic-gate #define	REMOTE_FS		"/etc/dfs/fstypes"
727c478bd9Sstevel@tonic-gate #define	N_FSTYPES		20
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * This is the list of operations
767c478bd9Sstevel@tonic-gate  * F_USER and F_GROUP are named to avoid conflict with USER and GROUP defined
777c478bd9Sstevel@tonic-gate  * in sys/acl.h
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate enum Command
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	PRINT, DEPTH, LOCAL, MOUNT, ATIME, MTIME, CTIME, NEWER,
837c478bd9Sstevel@tonic-gate 	NAME, F_USER, F_GROUP, INUM, SIZE, LINKS, PERM, EXEC, OK, CPIO, NCPIO,
847c478bd9Sstevel@tonic-gate 	TYPE, AND, OR, NOT, LPAREN, RPAREN, CSIZE, VARARGS, FOLLOW,
85da1a9cbeSjonb 	PRUNE, NOUSER, NOGRP, FSTYPE, LS, XATTR, ACL, MMIN, AMIN, CMIN
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate enum Type
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	Unary, Id, Num, Str, Exec, Cpio, Op
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate struct Args
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	char		name[10];
967c478bd9Sstevel@tonic-gate 	enum Command	action;
977c478bd9Sstevel@tonic-gate 	enum Type	type;
987c478bd9Sstevel@tonic-gate };
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * Except for pathnames, these are the only legal arguments
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate static struct Args commands[] =
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	"!",		NOT,	Op,
1067c478bd9Sstevel@tonic-gate 	"(",		LPAREN,	Unary,
1077c478bd9Sstevel@tonic-gate 	")",		RPAREN,	Unary,
1087c478bd9Sstevel@tonic-gate 	"-a",		AND,	Op,
109da1a9cbeSjonb 	"-amin",	AMIN,	Num,
1107c478bd9Sstevel@tonic-gate 	"-atime",	ATIME,	Num,
1117c478bd9Sstevel@tonic-gate 	"-cpio",	CPIO,	Cpio,
112da1a9cbeSjonb 	"-cmin",	CMIN,	Num,
1137c478bd9Sstevel@tonic-gate 	"-ctime",	CTIME,	Num,
1147c478bd9Sstevel@tonic-gate 	"-depth",	DEPTH,	Unary,
1157c478bd9Sstevel@tonic-gate 	"-exec",	EXEC,	Exec,
1167c478bd9Sstevel@tonic-gate 	"-follow",	FOLLOW, Unary,
1177c478bd9Sstevel@tonic-gate 	"-group",	F_GROUP,	Num,
1187c478bd9Sstevel@tonic-gate 	"-inum",	INUM,	Num,
1197c478bd9Sstevel@tonic-gate 	"-links",	LINKS,	Num,
1207c478bd9Sstevel@tonic-gate 	"-local",	LOCAL,	Unary,
1217c478bd9Sstevel@tonic-gate 	"-mount",	MOUNT,	Unary,
122da1a9cbeSjonb 	"-mmin",	MMIN,	Num,
1237c478bd9Sstevel@tonic-gate 	"-mtime",	MTIME,	Num,
1247c478bd9Sstevel@tonic-gate 	"-name",	NAME,	Str,
1257c478bd9Sstevel@tonic-gate 	"-ncpio",	NCPIO,  Cpio,
1267c478bd9Sstevel@tonic-gate 	"-newer",	NEWER,	Str,
1277c478bd9Sstevel@tonic-gate 	"-o",		OR,	Op,
1287c478bd9Sstevel@tonic-gate 	"-ok",		OK,	Exec,
1297c478bd9Sstevel@tonic-gate 	"-perm",	PERM,	Num,
1307c478bd9Sstevel@tonic-gate 	"-print",	PRINT,	Unary,
1317c478bd9Sstevel@tonic-gate 	"-size",	SIZE,	Num,
1327c478bd9Sstevel@tonic-gate 	"-type",	TYPE,	Num,
1337c478bd9Sstevel@tonic-gate 	"-xdev",	MOUNT,	Unary,
1347c478bd9Sstevel@tonic-gate 	"-user",	F_USER,	Num,
1357c478bd9Sstevel@tonic-gate 	"-prune",	PRUNE,	Unary,
1367c478bd9Sstevel@tonic-gate 	"-nouser",	NOUSER,	Unary,
1377c478bd9Sstevel@tonic-gate 	"-nogroup",	NOGRP,	Unary,
1387c478bd9Sstevel@tonic-gate 	"-fstype",	FSTYPE,	Str,
1397c478bd9Sstevel@tonic-gate 	"-ls",		LS,	Unary,
1407c478bd9Sstevel@tonic-gate 	"-xattr",	XATTR,	Unary,
1417c478bd9Sstevel@tonic-gate 	"-acl",		ACL,	Unary,
1427c478bd9Sstevel@tonic-gate 	NULL,		0,	0
1437c478bd9Sstevel@tonic-gate };
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate union Item
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	struct Node	*np;
1487c478bd9Sstevel@tonic-gate 	struct Arglist	*vp;
1497c478bd9Sstevel@tonic-gate 	time_t		t;
1507c478bd9Sstevel@tonic-gate 	char		*cp;
1517c478bd9Sstevel@tonic-gate 	char		**ap;
1527c478bd9Sstevel@tonic-gate 	long		l;
1537c478bd9Sstevel@tonic-gate 	int		i;
1547c478bd9Sstevel@tonic-gate 	long long	ll;
1557c478bd9Sstevel@tonic-gate };
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate struct Node
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	struct Node	*next;
1607c478bd9Sstevel@tonic-gate 	enum Command	action;
1617c478bd9Sstevel@tonic-gate 	enum Type	type;
1627c478bd9Sstevel@tonic-gate 	union Item	first;
1637c478bd9Sstevel@tonic-gate 	union Item	second;
1647c478bd9Sstevel@tonic-gate };
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /* if no -print, -exec or -ok replace "expression" with "(expression) -print" */
1677c478bd9Sstevel@tonic-gate static	struct	Node PRINT_NODE = { 0, PRINT, 0, 0};
1687c478bd9Sstevel@tonic-gate static	struct	Node LPAREN_NODE = { 0, LPAREN, 0, 0};
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * Prototype variable size arglist buffer
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate struct Arglist
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	struct Arglist	*next;
1787c478bd9Sstevel@tonic-gate 	char		*end;
1797c478bd9Sstevel@tonic-gate 	char		*nextstr;
1807c478bd9Sstevel@tonic-gate 	char		**firstvar;
1817c478bd9Sstevel@tonic-gate 	char		**nextvar;
1827c478bd9Sstevel@tonic-gate 	char		*arglist[1];
1837c478bd9Sstevel@tonic-gate };
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static int		compile();
1877c478bd9Sstevel@tonic-gate static int		execute();
1887c478bd9Sstevel@tonic-gate static int		doexec();
1897c478bd9Sstevel@tonic-gate static struct Args	*lookup();
1907c478bd9Sstevel@tonic-gate static int		ok();
1916c83d09fSrobbin static void		usage(void)	__NORETURN;
1927c478bd9Sstevel@tonic-gate static struct Arglist	*varargs();
1937c478bd9Sstevel@tonic-gate static int		list();
1947c478bd9Sstevel@tonic-gate static char		*getgroup();
1957c478bd9Sstevel@tonic-gate static FILE		*cmdopen();
1967c478bd9Sstevel@tonic-gate static int		cmdclose();
1977c478bd9Sstevel@tonic-gate static char		*getshell();
1987c478bd9Sstevel@tonic-gate static void 		init_remote_fs();
1997c478bd9Sstevel@tonic-gate static char		*getname();
2007c478bd9Sstevel@tonic-gate static int		readmode();
2017c478bd9Sstevel@tonic-gate static mode_t		getmode();
2027c478bd9Sstevel@tonic-gate static char		*gettail();
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static int		walkflags = FTW_CHDIR|FTW_PHYS|FTW_ANYERR;
2067c478bd9Sstevel@tonic-gate static struct Node	*savetnode;
2077c478bd9Sstevel@tonic-gate static struct Node	*topnode;
2087c478bd9Sstevel@tonic-gate static struct Node	*freenode;	/* next free node we may use later */
2097c478bd9Sstevel@tonic-gate static char		*cpio[] = { "cpio", "-o", 0 };
2107c478bd9Sstevel@tonic-gate static char		*ncpio[] = { "cpio", "-oc", 0 };
2117c478bd9Sstevel@tonic-gate static char		*cpiol[] = { "cpio", "-oL", 0 };
2127c478bd9Sstevel@tonic-gate static char		*ncpiol[] = { "cpio", "-ocL", 0 };
2137c478bd9Sstevel@tonic-gate static time_t		now;
2147c478bd9Sstevel@tonic-gate static FILE		*output;
2157c478bd9Sstevel@tonic-gate static char		*dummyarg = (char *)-1;
2167c478bd9Sstevel@tonic-gate static int		lastval;
2177c478bd9Sstevel@tonic-gate static int		varsize;
2187c478bd9Sstevel@tonic-gate static struct Arglist	*lastlist;
2197c478bd9Sstevel@tonic-gate static char		*cmdname;
2207c478bd9Sstevel@tonic-gate static char		*remote_fstypes[N_FSTYPES+1];
2217c478bd9Sstevel@tonic-gate static int		fstype_index = 0;
2227c478bd9Sstevel@tonic-gate static int		action_expression = 0;	/* -print, -exec, or -ok */
2237c478bd9Sstevel@tonic-gate static int		error = 0;
2247c478bd9Sstevel@tonic-gate static int		paren_cnt = 0;	/* keeps track of parentheses */
2257c478bd9Sstevel@tonic-gate static int		hflag = 0;
2267c478bd9Sstevel@tonic-gate static int		lflag = 0;
2277c478bd9Sstevel@tonic-gate static int		varexecrc = 0;
2287c478bd9Sstevel@tonic-gate extern char		**environ;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate int
2317c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	char *cp;
2347c478bd9Sstevel@tonic-gate 	int c;
2357c478bd9Sstevel@tonic-gate 	int paths;
2367c478bd9Sstevel@tonic-gate 	char *cwdpath;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2397c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2407c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2417c478bd9Sstevel@tonic-gate #endif
2427c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	cmdname = argv[0];
2457c478bd9Sstevel@tonic-gate 	if (time(&now) == (time_t)(-1)) {
2467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: time() %s\n"),
2477c478bd9Sstevel@tonic-gate 			cmdname, strerror(errno));
2487c478bd9Sstevel@tonic-gate 		exit(1);
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "HL")) != -1) {
2517c478bd9Sstevel@tonic-gate 		switch (c) {
2527c478bd9Sstevel@tonic-gate 		case 'H':
2537c478bd9Sstevel@tonic-gate 			hflag = 1;
2547c478bd9Sstevel@tonic-gate 			lflag = 0;
2557c478bd9Sstevel@tonic-gate 			break;
2567c478bd9Sstevel@tonic-gate 		case 'L':
2577c478bd9Sstevel@tonic-gate 			hflag = 0;
2587c478bd9Sstevel@tonic-gate 			lflag = 1;
2597c478bd9Sstevel@tonic-gate 			break;
2607c478bd9Sstevel@tonic-gate 		case '?':
2617c478bd9Sstevel@tonic-gate 			usage();
2627c478bd9Sstevel@tonic-gate 			break;
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	argc -= optind;
2677c478bd9Sstevel@tonic-gate 	argv += optind;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	if (argc < 1) {
2707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2717c478bd9Sstevel@tonic-gate 		    gettext("%s: insufficient number of arguments\n"), cmdname);
2727c478bd9Sstevel@tonic-gate 		usage();
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	for (paths = 0; (cp = argv[paths]) != 0; ++paths) {
2767c478bd9Sstevel@tonic-gate 		if (*cp == '-')
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 		else if ((*cp == '!' || *cp == '(') && *(cp+1) == 0)
2797c478bd9Sstevel@tonic-gate 			break;
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if (paths == 0) /* no path-list */
2837c478bd9Sstevel@tonic-gate 		usage();
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	output = stdout;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/* lflag is the same as -follow */
2887c478bd9Sstevel@tonic-gate 	if (lflag)
2897c478bd9Sstevel@tonic-gate 		walkflags &= ~FTW_PHYS;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/* allocate enough space for the compiler */
2927c478bd9Sstevel@tonic-gate 	topnode = malloc((argc + 1) * sizeof (struct Node));
2937c478bd9Sstevel@tonic-gate 	savetnode = malloc((argc + 1) * sizeof (struct Node));
2947c478bd9Sstevel@tonic-gate 	(void) memset(topnode, 0, (argc + 1) * sizeof (struct Node));
2957c478bd9Sstevel@tonic-gate 	(void) memset(savetnode, 0, (argc + 1) * sizeof (struct Node));
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (compile(argv + paths, topnode, &action_expression) == 0) {
2987c478bd9Sstevel@tonic-gate 		/* no expression, default to -print */
2997c478bd9Sstevel@tonic-gate 		(void) memcpy(topnode, &PRINT_NODE, sizeof (struct Node));
3007c478bd9Sstevel@tonic-gate 	} else if (!action_expression) {
3017c478bd9Sstevel@tonic-gate 		/*
3027c478bd9Sstevel@tonic-gate 		 * if no action expression, insert an LPAREN node above topnode,
3037c478bd9Sstevel@tonic-gate 		 * with a PRINT node as its next node
3047c478bd9Sstevel@tonic-gate 		 */
3057c478bd9Sstevel@tonic-gate 		struct Node *savenode;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		if (freenode == NULL) {
3087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: can't append -print"
3097c478bd9Sstevel@tonic-gate 				" implicitly; try explicit -print option\n"),
3107c478bd9Sstevel@tonic-gate 				cmdname);
3117c478bd9Sstevel@tonic-gate 			exit(1);
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 		savenode = topnode;
3147c478bd9Sstevel@tonic-gate 		topnode = freenode++;
3157c478bd9Sstevel@tonic-gate 		(void) memcpy(topnode, &LPAREN_NODE, sizeof (struct Node));
3167c478bd9Sstevel@tonic-gate 		topnode->next = freenode;
3177c478bd9Sstevel@tonic-gate 		topnode->first.np = savenode;
3187c478bd9Sstevel@tonic-gate 		(void) memcpy(topnode->next, &PRINT_NODE, sizeof (struct Node));
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 	(void) memcpy(savetnode, topnode, ((argc + 1) * sizeof (struct Node)));
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	while (paths--) {
3237c478bd9Sstevel@tonic-gate 		char *curpath;
3247c478bd9Sstevel@tonic-gate 		struct stat sb;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		curpath = *(argv++);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		/*
3297c478bd9Sstevel@tonic-gate 		 * If -H is specified, it means we walk the first
3307c478bd9Sstevel@tonic-gate 		 * level (pathname on command line) logically, following
3317c478bd9Sstevel@tonic-gate 		 * symlinks, but lower levels are walked physically.
3327c478bd9Sstevel@tonic-gate 		 * We use our own secret interface to nftw() to change
3337c478bd9Sstevel@tonic-gate 		 * the from stat to lstat after the top level is walked.
3347c478bd9Sstevel@tonic-gate 		 */
3357c478bd9Sstevel@tonic-gate 		if (hflag) {
3367c478bd9Sstevel@tonic-gate 			if (stat(curpath, &sb) < 0 && errno == ENOENT)
3377c478bd9Sstevel@tonic-gate 				walkflags &= ~FTW_HOPTION;
3387c478bd9Sstevel@tonic-gate 			else
3397c478bd9Sstevel@tonic-gate 				walkflags |= FTW_HOPTION;
3407c478bd9Sstevel@tonic-gate 		}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		/*
3437c478bd9Sstevel@tonic-gate 		 * We need this check as nftw needs a CWD and we have no
3447c478bd9Sstevel@tonic-gate 		 * way of returning back from that code with a meaningful
3457c478bd9Sstevel@tonic-gate 		 * error related to this
3467c478bd9Sstevel@tonic-gate 		 */
3477c478bd9Sstevel@tonic-gate 		if ((cwdpath = getcwd(NULL, PATH_MAX)) == NULL) {
3487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3497c478bd9Sstevel@tonic-gate 				gettext("%s : cannot get the current working "
3507c478bd9Sstevel@tonic-gate 					"directory\n"), cmdname);
3517c478bd9Sstevel@tonic-gate 			exit(1);
3527c478bd9Sstevel@tonic-gate 		} else
3537c478bd9Sstevel@tonic-gate 			free(cwdpath);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		if (nftw(curpath, execute, 1000, walkflags)) {
3577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3587c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot open %s: %s\n"),
3597c478bd9Sstevel@tonic-gate 			    cmdname, curpath, strerror(errno));
3607c478bd9Sstevel@tonic-gate 			error = 1;
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		if (paths > 1)
3647c478bd9Sstevel@tonic-gate 			(void) memcpy(topnode, savetnode,
3657c478bd9Sstevel@tonic-gate 			    ((argc + 1) * sizeof (struct Node)));
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/* execute any remaining variable length lists */
3697c478bd9Sstevel@tonic-gate 	while (lastlist) {
3707c478bd9Sstevel@tonic-gate 		if (lastlist->end != lastlist->nextstr) {
3717c478bd9Sstevel@tonic-gate 			*lastlist->nextvar = 0;
3727c478bd9Sstevel@tonic-gate 			(void) doexec((char *)0, lastlist->arglist);
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 		lastlist = lastlist->next;
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 	if (output != stdout)
3777c478bd9Sstevel@tonic-gate 		return (cmdclose(output));
3787c478bd9Sstevel@tonic-gate 	return ((varexecrc != 0) ? varexecrc : error);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * compile the arguments
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate static int
3867c478bd9Sstevel@tonic-gate compile(argv, np, actionp)
3877c478bd9Sstevel@tonic-gate char **argv;
3887c478bd9Sstevel@tonic-gate struct Node *np;
3897c478bd9Sstevel@tonic-gate int *actionp;
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	char *b;
3927c478bd9Sstevel@tonic-gate 	char **av;
3937c478bd9Sstevel@tonic-gate 	struct Node *oldnp = topnode;
3947c478bd9Sstevel@tonic-gate 	struct Args *argp;
3957c478bd9Sstevel@tonic-gate 	char **com;
3967c478bd9Sstevel@tonic-gate 	int i;
3977c478bd9Sstevel@tonic-gate 	enum Command wasop = PRINT;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	for (av = argv; *av && (argp = lookup(*av)); av++) {
4007c478bd9Sstevel@tonic-gate 		np->next = 0;
4017c478bd9Sstevel@tonic-gate 		np->action = argp->action;
4027c478bd9Sstevel@tonic-gate 		np->type = argp->type;
4037c478bd9Sstevel@tonic-gate 		np->second.i = 0;
4047c478bd9Sstevel@tonic-gate 		if (argp->type == Op) {
4057c478bd9Sstevel@tonic-gate 			if (wasop == NOT || (wasop && np->action != NOT)) {
4067c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4077c478bd9Sstevel@tonic-gate 				gettext("%s: operand follows operand\n"),
4087c478bd9Sstevel@tonic-gate 						cmdname);
4097c478bd9Sstevel@tonic-gate 				exit(1);
4107c478bd9Sstevel@tonic-gate 			}
4117c478bd9Sstevel@tonic-gate 			if (np->action != NOT && oldnp == 0)
4127c478bd9Sstevel@tonic-gate 				goto err;
4137c478bd9Sstevel@tonic-gate 			wasop = argp->action;
4147c478bd9Sstevel@tonic-gate 		} else {
4157c478bd9Sstevel@tonic-gate 			wasop = PRINT;
4167c478bd9Sstevel@tonic-gate 			if (argp->type != Unary) {
4177c478bd9Sstevel@tonic-gate 				if (!(b = *++av)) {
4187c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
4197c478bd9Sstevel@tonic-gate 					gettext("%s: incomplete statement\n"),
4207c478bd9Sstevel@tonic-gate 							cmdname);
4217c478bd9Sstevel@tonic-gate 					exit(1);
4227c478bd9Sstevel@tonic-gate 				}
4237c478bd9Sstevel@tonic-gate 				if (argp->type == Num) {
4247c478bd9Sstevel@tonic-gate 					if ((argp->action != PERM) ||
4257c478bd9Sstevel@tonic-gate 					    (*b != '+')) {
4267c478bd9Sstevel@tonic-gate 						if (*b == '+' || *b == '-') {
4277c478bd9Sstevel@tonic-gate 							np->second.i = *b;
4287c478bd9Sstevel@tonic-gate 							b++;
4297c478bd9Sstevel@tonic-gate 						}
4307c478bd9Sstevel@tonic-gate 					}
4317c478bd9Sstevel@tonic-gate 				}
4327c478bd9Sstevel@tonic-gate 			}
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 		switch (argp->action) {
4357c478bd9Sstevel@tonic-gate 		case AND:
4367c478bd9Sstevel@tonic-gate 			break;
4377c478bd9Sstevel@tonic-gate 		case NOT:
4387c478bd9Sstevel@tonic-gate 			break;
4397c478bd9Sstevel@tonic-gate 		case OR:
4407c478bd9Sstevel@tonic-gate 			np->first.np = topnode;
4417c478bd9Sstevel@tonic-gate 			topnode = np;
4427c478bd9Sstevel@tonic-gate 			oldnp->next = 0;
4437c478bd9Sstevel@tonic-gate 			break;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		case LPAREN: {
4467c478bd9Sstevel@tonic-gate 			struct Node *save = topnode;
4477c478bd9Sstevel@tonic-gate 			topnode = np+1;
4487c478bd9Sstevel@tonic-gate 			paren_cnt++;
4497c478bd9Sstevel@tonic-gate 			i = compile(++av, topnode, actionp);
4507c478bd9Sstevel@tonic-gate 			np->first.np = topnode;
4517c478bd9Sstevel@tonic-gate 			topnode = save;
4527c478bd9Sstevel@tonic-gate 			av += i;
4537c478bd9Sstevel@tonic-gate 			oldnp = np;
4547c478bd9Sstevel@tonic-gate 			np += i + 1;
4557c478bd9Sstevel@tonic-gate 			oldnp->next = np;
4567c478bd9Sstevel@tonic-gate 			continue;
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		case RPAREN:
4607c478bd9Sstevel@tonic-gate 			if (paren_cnt <= 0) {
4617c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4627c478bd9Sstevel@tonic-gate 				    gettext("%s: unmatched ')'\n"),
4637c478bd9Sstevel@tonic-gate 				    cmdname);
4647c478bd9Sstevel@tonic-gate 				exit(1);
4657c478bd9Sstevel@tonic-gate 			}
4667c478bd9Sstevel@tonic-gate 			paren_cnt--;
4677c478bd9Sstevel@tonic-gate 			if (oldnp == 0)
4687c478bd9Sstevel@tonic-gate 				goto err;
4697c478bd9Sstevel@tonic-gate 			if (oldnp->type == Op) {
4707c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4717c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot immediately"
4727c478bd9Sstevel@tonic-gate 				    " follow an operand with ')'\n"),
4737c478bd9Sstevel@tonic-gate 				    cmdname);
4747c478bd9Sstevel@tonic-gate 				exit(1);
4757c478bd9Sstevel@tonic-gate 			}
4767c478bd9Sstevel@tonic-gate 			oldnp->next = 0;
4777c478bd9Sstevel@tonic-gate 			return (av-argv);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		case FOLLOW:
4807c478bd9Sstevel@tonic-gate 			walkflags &= ~FTW_PHYS;
4817c478bd9Sstevel@tonic-gate 			break;
4827c478bd9Sstevel@tonic-gate 		case MOUNT:
4837c478bd9Sstevel@tonic-gate 			walkflags |= FTW_MOUNT;
4847c478bd9Sstevel@tonic-gate 			break;
4857c478bd9Sstevel@tonic-gate 		case DEPTH:
4867c478bd9Sstevel@tonic-gate 			walkflags |= FTW_DEPTH;
4877c478bd9Sstevel@tonic-gate 			break;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 		case LOCAL:
4907c478bd9Sstevel@tonic-gate 			np->first.l = 0L;
4917c478bd9Sstevel@tonic-gate 			np->first.ll = 0LL;
4927c478bd9Sstevel@tonic-gate 			np->second.i = '+';
4937c478bd9Sstevel@tonic-gate 			/*
4947c478bd9Sstevel@tonic-gate 			 * Make it compatible to df -l for
4957c478bd9Sstevel@tonic-gate 			 * future enhancement. So, anything
4967c478bd9Sstevel@tonic-gate 			 * that is not remote, then it is
4977c478bd9Sstevel@tonic-gate 			 * local.
4987c478bd9Sstevel@tonic-gate 			 */
4997c478bd9Sstevel@tonic-gate 			init_remote_fs();
5007c478bd9Sstevel@tonic-gate 			break;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		case SIZE:
5037c478bd9Sstevel@tonic-gate 			if (b[strlen(b)-1] == 'c')
5047c478bd9Sstevel@tonic-gate 				np->action = CSIZE;
5057c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
5067c478bd9Sstevel@tonic-gate 		case INUM:
5077c478bd9Sstevel@tonic-gate 			np->first.ll = atoll(b);
5087c478bd9Sstevel@tonic-gate 			break;
5097c478bd9Sstevel@tonic-gate 
510da1a9cbeSjonb 		case CMIN:
5117c478bd9Sstevel@tonic-gate 		case CTIME:
512da1a9cbeSjonb 		case MMIN:
5137c478bd9Sstevel@tonic-gate 		case MTIME:
514da1a9cbeSjonb 		case AMIN:
5157c478bd9Sstevel@tonic-gate 		case ATIME:
5167c478bd9Sstevel@tonic-gate 		case LINKS:
5177c478bd9Sstevel@tonic-gate 			np->first.l = atol(b);
5187c478bd9Sstevel@tonic-gate 			break;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 		case F_USER:
5217c478bd9Sstevel@tonic-gate 		case F_GROUP: {
5227c478bd9Sstevel@tonic-gate 			struct	passwd	*pw;
5237c478bd9Sstevel@tonic-gate 			struct	group *gr;
5247c478bd9Sstevel@tonic-gate 			i = -1;
5257c478bd9Sstevel@tonic-gate 			if (argp->action == F_USER) {
5267c478bd9Sstevel@tonic-gate 				if ((pw = getpwnam(b)) != 0)
5277c478bd9Sstevel@tonic-gate 					i = (int)pw->pw_uid;
5287c478bd9Sstevel@tonic-gate 			} else {
5297c478bd9Sstevel@tonic-gate 				if ((gr = getgrnam(b)) != 0)
5307c478bd9Sstevel@tonic-gate 					i = (int)gr->gr_gid;
5317c478bd9Sstevel@tonic-gate 			}
5327c478bd9Sstevel@tonic-gate 			if (i == -1) {
5337c478bd9Sstevel@tonic-gate 				if (fnmatch("[0-9][0-9][0-9]*", b, 0) &&
5347c478bd9Sstevel@tonic-gate 						fnmatch("[0-9][0-9]", b, 0) &&
5357c478bd9Sstevel@tonic-gate 						fnmatch("[0-9]", b, 0)) {
5367c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
5377c478bd9Sstevel@tonic-gate 					    "%s: cannot find %s name\n"),
5387c478bd9Sstevel@tonic-gate 						cmdname, *av);
5397c478bd9Sstevel@tonic-gate 					exit(1);
5407c478bd9Sstevel@tonic-gate 				}
5417c478bd9Sstevel@tonic-gate 				i = atoi(b);
5427c478bd9Sstevel@tonic-gate 			}
5437c478bd9Sstevel@tonic-gate 			np->first.l = i;
5447c478bd9Sstevel@tonic-gate 			break;
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		case EXEC:
5487c478bd9Sstevel@tonic-gate 		case OK:
5497c478bd9Sstevel@tonic-gate 			walkflags &= ~FTW_CHDIR;
5507c478bd9Sstevel@tonic-gate 			np->first.ap = av;
5517c478bd9Sstevel@tonic-gate 			(*actionp)++;
5527c478bd9Sstevel@tonic-gate 			for (;;) {
5537c478bd9Sstevel@tonic-gate 				if ((b = *av) == 0) {
5547c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
5557c478bd9Sstevel@tonic-gate 					gettext("%s: incomplete statement\n"),
5567c478bd9Sstevel@tonic-gate 						cmdname);
5577c478bd9Sstevel@tonic-gate 					exit(1);
5587c478bd9Sstevel@tonic-gate 				}
5597c478bd9Sstevel@tonic-gate 				if (strcmp(b, ";") == 0) {
5607c478bd9Sstevel@tonic-gate 					*av = 0;
5617c478bd9Sstevel@tonic-gate 					break;
5627c478bd9Sstevel@tonic-gate 				} else if (strcmp(b, "{}") == 0)
5637c478bd9Sstevel@tonic-gate 					*av = dummyarg;
5647c478bd9Sstevel@tonic-gate 				else if (strcmp(b, "+") == 0 &&
5657c478bd9Sstevel@tonic-gate 					av[-1] == dummyarg &&
5667c478bd9Sstevel@tonic-gate 					np->action == EXEC) {
5677c478bd9Sstevel@tonic-gate 					av[-1] = 0;
5687c478bd9Sstevel@tonic-gate 					np->first.vp = varargs(np->first.ap);
5697c478bd9Sstevel@tonic-gate 					np->action = VARARGS;
5707c478bd9Sstevel@tonic-gate 					break;
5717c478bd9Sstevel@tonic-gate 				}
5727c478bd9Sstevel@tonic-gate 				av++;
5737c478bd9Sstevel@tonic-gate 			}
5747c478bd9Sstevel@tonic-gate 			break;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 		case NAME:
5777c478bd9Sstevel@tonic-gate 			np->first.cp = b;
5787c478bd9Sstevel@tonic-gate 			break;
5797c478bd9Sstevel@tonic-gate 		case PERM:
5807c478bd9Sstevel@tonic-gate 			if (*b == '-')
5817c478bd9Sstevel@tonic-gate 				++b;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 			if (readmode(b) != NULL) {
5847c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
5857c478bd9Sstevel@tonic-gate 				    "find: -perm: Bad permission string\n"));
5867c478bd9Sstevel@tonic-gate 				usage();
5877c478bd9Sstevel@tonic-gate 			}
5887c478bd9Sstevel@tonic-gate 			np->first.l = (long)getmode((mode_t)0);
5897c478bd9Sstevel@tonic-gate 			break;
5907c478bd9Sstevel@tonic-gate 		case TYPE:
5917c478bd9Sstevel@tonic-gate 			i = *b;
5927c478bd9Sstevel@tonic-gate 			np->first.l =
5937c478bd9Sstevel@tonic-gate 			    i == 'd' ? S_IFDIR :
5947c478bd9Sstevel@tonic-gate 			    i == 'b' ? S_IFBLK :
5957c478bd9Sstevel@tonic-gate 			    i == 'c' ? S_IFCHR :
5967c478bd9Sstevel@tonic-gate #ifdef S_IFIFO
5977c478bd9Sstevel@tonic-gate 			    i == 'p' ? S_IFIFO :
5987c478bd9Sstevel@tonic-gate #endif
5997c478bd9Sstevel@tonic-gate 			    i == 'f' ? S_IFREG :
6007c478bd9Sstevel@tonic-gate #ifdef S_IFLNK
6017c478bd9Sstevel@tonic-gate 			    i == 'l' ? S_IFLNK :
6027c478bd9Sstevel@tonic-gate #endif
6037c478bd9Sstevel@tonic-gate #ifdef S_IFSOCK
6047c478bd9Sstevel@tonic-gate 			    i == 's' ? S_IFSOCK :
6057c478bd9Sstevel@tonic-gate #endif
6067c478bd9Sstevel@tonic-gate #ifdef S_IFDOOR
6077c478bd9Sstevel@tonic-gate 			    i == 'D' ? S_IFDOOR :
6087c478bd9Sstevel@tonic-gate #endif
6097c478bd9Sstevel@tonic-gate 			    0;
6107c478bd9Sstevel@tonic-gate 			break;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		case CPIO:
6137c478bd9Sstevel@tonic-gate 			if (walkflags & FTW_PHYS)
6147c478bd9Sstevel@tonic-gate 				com = cpio;
6157c478bd9Sstevel@tonic-gate 			else
6167c478bd9Sstevel@tonic-gate 				com = cpiol;
6177c478bd9Sstevel@tonic-gate 			goto common;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		case NCPIO: {
6207c478bd9Sstevel@tonic-gate 			FILE *fd;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 			if (walkflags & FTW_PHYS)
6237c478bd9Sstevel@tonic-gate 				com = ncpio;
6247c478bd9Sstevel@tonic-gate 			else
6257c478bd9Sstevel@tonic-gate 				com = ncpiol;
6267c478bd9Sstevel@tonic-gate 		common:
6277c478bd9Sstevel@tonic-gate 			/* set up cpio */
6287c478bd9Sstevel@tonic-gate 			if ((fd = fopen(b, "w")) == NULL) {
6297c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6307c478bd9Sstevel@tonic-gate 					gettext("%s: cannot create %s\n"),
6317c478bd9Sstevel@tonic-gate 					cmdname, b);
6327c478bd9Sstevel@tonic-gate 				exit(1);
6337c478bd9Sstevel@tonic-gate 			}
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 			np->first.l = (long)cmdopen("cpio", com, "w", fd);
6367c478bd9Sstevel@tonic-gate 			(void) fclose(fd);
6377c478bd9Sstevel@tonic-gate 			walkflags |= FTW_DEPTH;
6387c478bd9Sstevel@tonic-gate 			np->action = CPIO;
6397c478bd9Sstevel@tonic-gate 		}
6407c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
6417c478bd9Sstevel@tonic-gate 		case PRINT:
6427c478bd9Sstevel@tonic-gate 			(*actionp)++;
6437c478bd9Sstevel@tonic-gate 			break;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		case NEWER: {
6467c478bd9Sstevel@tonic-gate 			struct stat statb;
6477c478bd9Sstevel@tonic-gate 			if (stat(b, &statb) < 0) {
6487c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6497c478bd9Sstevel@tonic-gate 					gettext("%s: cannot access %s\n"),
6507c478bd9Sstevel@tonic-gate 					cmdname, b);
6517c478bd9Sstevel@tonic-gate 				exit(1);
6527c478bd9Sstevel@tonic-gate 			}
6537c478bd9Sstevel@tonic-gate 			np->first.l = statb.st_mtime;
6547c478bd9Sstevel@tonic-gate 			np->second.i = '+';
6557c478bd9Sstevel@tonic-gate 			break;
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 		case PRUNE:
6597c478bd9Sstevel@tonic-gate 		case NOUSER:
6607c478bd9Sstevel@tonic-gate 		case NOGRP:
6617c478bd9Sstevel@tonic-gate 			break;
6627c478bd9Sstevel@tonic-gate 		case FSTYPE:
6637c478bd9Sstevel@tonic-gate 			np->first.cp = b;
6647c478bd9Sstevel@tonic-gate 			break;
6657c478bd9Sstevel@tonic-gate 		case LS:
6667c478bd9Sstevel@tonic-gate 			(*actionp)++;
6677c478bd9Sstevel@tonic-gate 			break;
6687c478bd9Sstevel@tonic-gate 		case XATTR:
6697c478bd9Sstevel@tonic-gate 			break;
6707c478bd9Sstevel@tonic-gate 		case ACL:
6717c478bd9Sstevel@tonic-gate 			break;
6727c478bd9Sstevel@tonic-gate 		}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 		oldnp = np++;
6757c478bd9Sstevel@tonic-gate 		oldnp->next = np;
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	if ((*av) || (wasop))
6797c478bd9Sstevel@tonic-gate 		goto err;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	if (paren_cnt != 0) {
6827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: unmatched '('\n"),
6837c478bd9Sstevel@tonic-gate 		cmdname);
6847c478bd9Sstevel@tonic-gate 		exit(1);
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	/* just before returning, save next free node from the list */
6887c478bd9Sstevel@tonic-gate 	freenode = oldnp->next;
6897c478bd9Sstevel@tonic-gate 	oldnp->next = 0;
6907c478bd9Sstevel@tonic-gate 	return (av-argv);
6917c478bd9Sstevel@tonic-gate err:
6927c478bd9Sstevel@tonic-gate 	if (*av)
6937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6947c478bd9Sstevel@tonic-gate 		    gettext("%s: bad option %s\n"), cmdname, *av);
6957c478bd9Sstevel@tonic-gate 	else
6967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: bad option\n"), cmdname);
6977c478bd9Sstevel@tonic-gate 	usage();
6987c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  * print out a usage message
7037c478bd9Sstevel@tonic-gate  */
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate static void
7066c83d09fSrobbin usage(void)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
7097c478bd9Sstevel@tonic-gate 	    gettext("%s: [-H | -L] path-list predicate-list\n"), cmdname);
7107c478bd9Sstevel@tonic-gate 	exit(1);
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate /*
7147c478bd9Sstevel@tonic-gate  * This is the function that gets executed at each node
7157c478bd9Sstevel@tonic-gate  */
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate static int
7187c478bd9Sstevel@tonic-gate execute(name, statb, type, state)
7197c478bd9Sstevel@tonic-gate char *name;
7207c478bd9Sstevel@tonic-gate struct stat *statb;
7217c478bd9Sstevel@tonic-gate int type;
7227c478bd9Sstevel@tonic-gate struct FTW *state;
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	struct Node *np = topnode;
7257c478bd9Sstevel@tonic-gate 	int val;
7267c478bd9Sstevel@tonic-gate 	time_t t;
7277c478bd9Sstevel@tonic-gate 	long l;
7287c478bd9Sstevel@tonic-gate 	long long ll;
7297c478bd9Sstevel@tonic-gate 	int not = 1;
7307c478bd9Sstevel@tonic-gate 	char *filename;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	if (type == FTW_NS) {
7337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: stat() error %s: %s\n"),
7347c478bd9Sstevel@tonic-gate 			cmdname, name, strerror(errno));
7357c478bd9Sstevel@tonic-gate 		error = 1;
7367c478bd9Sstevel@tonic-gate 		return (0);
7377c478bd9Sstevel@tonic-gate 	} else if (type == FTW_DNR) {
7387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: cannot read dir %s: %s\n"),
7397c478bd9Sstevel@tonic-gate 			cmdname, name, strerror(errno));
7407c478bd9Sstevel@tonic-gate 		error = 1;
7417c478bd9Sstevel@tonic-gate 		return (0);
7427c478bd9Sstevel@tonic-gate 	} else if (type == FTW_SLN && lflag == 0) {
7437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7447c478bd9Sstevel@tonic-gate 			gettext("%s: cannot follow symbolic link %s: %s\n"),
7457c478bd9Sstevel@tonic-gate 			cmdname, name, strerror(errno));
7467c478bd9Sstevel@tonic-gate 		error = 1;
7477c478bd9Sstevel@tonic-gate 		return (0);
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	while (np) {
7517c478bd9Sstevel@tonic-gate 		switch (np->action) {
7527c478bd9Sstevel@tonic-gate 		case NOT:
7537c478bd9Sstevel@tonic-gate 			not = !not;
7547c478bd9Sstevel@tonic-gate 			np = np->next;
7557c478bd9Sstevel@tonic-gate 			continue;
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 		case AND:
7587c478bd9Sstevel@tonic-gate 			np = np->next;
7597c478bd9Sstevel@tonic-gate 			continue;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		case OR:
7627c478bd9Sstevel@tonic-gate 			if (np->first.np == np) {
7637c478bd9Sstevel@tonic-gate 				/*
7647c478bd9Sstevel@tonic-gate 				 * handle naked OR (no term on left hand side)
7657c478bd9Sstevel@tonic-gate 				 */
7667c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
7677c478bd9Sstevel@tonic-gate 				    gettext("%s: invalid -o construction\n"),
7687c478bd9Sstevel@tonic-gate 				    cmdname);
7697c478bd9Sstevel@tonic-gate 				exit(2);
7707c478bd9Sstevel@tonic-gate 			}
7717c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
7727c478bd9Sstevel@tonic-gate 		case LPAREN: {
7737c478bd9Sstevel@tonic-gate 			struct Node *save = topnode;
7747c478bd9Sstevel@tonic-gate 			topnode = np->first.np;
7757c478bd9Sstevel@tonic-gate 			(void) execute(name, statb, type, state);
7767c478bd9Sstevel@tonic-gate 			val = lastval;
7777c478bd9Sstevel@tonic-gate 			topnode = save;
7787c478bd9Sstevel@tonic-gate 			if (np->action == OR) {
7797c478bd9Sstevel@tonic-gate 				if (val)
7807c478bd9Sstevel@tonic-gate 					return (0);
7817c478bd9Sstevel@tonic-gate 				val = 1;
7827c478bd9Sstevel@tonic-gate 			}
7837c478bd9Sstevel@tonic-gate 			break;
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		case LOCAL: {
7877c478bd9Sstevel@tonic-gate 			int	nremfs;
7887c478bd9Sstevel@tonic-gate 			val = 1;
7897c478bd9Sstevel@tonic-gate 			/*
7907c478bd9Sstevel@tonic-gate 			 * If file system type matches the remote
7917c478bd9Sstevel@tonic-gate 			 * file system type, then it is not local.
7927c478bd9Sstevel@tonic-gate 			 */
7937c478bd9Sstevel@tonic-gate 			for (nremfs = 0; nremfs < fstype_index; nremfs++) {
7947c478bd9Sstevel@tonic-gate 				if (strcmp(remote_fstypes[nremfs],
7957c478bd9Sstevel@tonic-gate 						statb->st_fstype) == 0) {
7967c478bd9Sstevel@tonic-gate 					val = 0;
7977c478bd9Sstevel@tonic-gate 					break;
7987c478bd9Sstevel@tonic-gate 				}
7997c478bd9Sstevel@tonic-gate 			}
8007c478bd9Sstevel@tonic-gate 			break;
8017c478bd9Sstevel@tonic-gate 		}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 		case TYPE:
8047c478bd9Sstevel@tonic-gate 			l = (long)statb->st_mode&S_IFMT;
8057c478bd9Sstevel@tonic-gate 			goto num;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		case PERM:
8087c478bd9Sstevel@tonic-gate 			l = (long)statb->st_mode&07777;
8097c478bd9Sstevel@tonic-gate 			if (np->second.i == '-')
8107c478bd9Sstevel@tonic-gate 				val = ((l&np->first.l) == np->first.l);
8117c478bd9Sstevel@tonic-gate 			else
8127c478bd9Sstevel@tonic-gate 				val = (l == np->first.l);
8137c478bd9Sstevel@tonic-gate 			break;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 		case INUM:
8167c478bd9Sstevel@tonic-gate 			ll = (long long)statb->st_ino;
8177c478bd9Sstevel@tonic-gate 			goto llnum;
8187c478bd9Sstevel@tonic-gate 		case NEWER:
8197c478bd9Sstevel@tonic-gate 			l = statb->st_mtime;
8207c478bd9Sstevel@tonic-gate 			goto num;
8217c478bd9Sstevel@tonic-gate 		case ATIME:
8227c478bd9Sstevel@tonic-gate 			t = statb->st_atime;
8237c478bd9Sstevel@tonic-gate 			goto days;
8247c478bd9Sstevel@tonic-gate 		case CTIME:
8257c478bd9Sstevel@tonic-gate 			t = statb->st_ctime;
8267c478bd9Sstevel@tonic-gate 			goto days;
8277c478bd9Sstevel@tonic-gate 		case MTIME:
8287c478bd9Sstevel@tonic-gate 			t = statb->st_mtime;
8297c478bd9Sstevel@tonic-gate 		days:
8307c478bd9Sstevel@tonic-gate 			l = (now-t)/A_DAY;
8317c478bd9Sstevel@tonic-gate 			goto num;
832da1a9cbeSjonb 		case MMIN:
833da1a9cbeSjonb 			t = statb->st_mtime;
834da1a9cbeSjonb 			goto mins;
835da1a9cbeSjonb 		case AMIN:
836da1a9cbeSjonb 			t = statb->st_atime;
837da1a9cbeSjonb 			goto mins;
838da1a9cbeSjonb 		case CMIN:
839da1a9cbeSjonb 			t = statb->st_ctime;
840da1a9cbeSjonb 			goto mins;
841da1a9cbeSjonb 		mins:
842da1a9cbeSjonb 			l = (now-t)/A_MIN;
843da1a9cbeSjonb 			goto num;
8447c478bd9Sstevel@tonic-gate 		case CSIZE:
8457c478bd9Sstevel@tonic-gate 			ll = (long long)statb->st_size;
8467c478bd9Sstevel@tonic-gate 			goto llnum;
8477c478bd9Sstevel@tonic-gate 		case SIZE:
8487c478bd9Sstevel@tonic-gate 			ll = (long long)round(statb->st_size, BLKSIZ)/BLKSIZ;
8497c478bd9Sstevel@tonic-gate 			goto llnum;
8507c478bd9Sstevel@tonic-gate 		case F_USER:
8517c478bd9Sstevel@tonic-gate 			l = (long)statb->st_uid;
8527c478bd9Sstevel@tonic-gate 			goto num;
8537c478bd9Sstevel@tonic-gate 		case F_GROUP:
8547c478bd9Sstevel@tonic-gate 			l = (long)statb->st_gid;
8557c478bd9Sstevel@tonic-gate 			goto num;
8567c478bd9Sstevel@tonic-gate 		case LINKS:
8577c478bd9Sstevel@tonic-gate 			l = (long)statb->st_nlink;
8587c478bd9Sstevel@tonic-gate 			goto num;
8597c478bd9Sstevel@tonic-gate 		llnum:
8607c478bd9Sstevel@tonic-gate 			if (np->second.i == '+')
8617c478bd9Sstevel@tonic-gate 				val = (ll > np->first.ll);
8627c478bd9Sstevel@tonic-gate 			else if (np->second.i == '-')
8637c478bd9Sstevel@tonic-gate 				val = (ll < np->first.ll);
8647c478bd9Sstevel@tonic-gate 			else
8657c478bd9Sstevel@tonic-gate 				val = (ll == np->first.ll);
8667c478bd9Sstevel@tonic-gate 			break;
8677c478bd9Sstevel@tonic-gate 		num:
8687c478bd9Sstevel@tonic-gate 			if (np->second.i == '+')
8697c478bd9Sstevel@tonic-gate 				val = (l > np->first.l);
8707c478bd9Sstevel@tonic-gate 			else if (np->second.i == '-')
8717c478bd9Sstevel@tonic-gate 				val = (l < np->first.l);
8727c478bd9Sstevel@tonic-gate 			else
8737c478bd9Sstevel@tonic-gate 				val = (l == np->first.l);
8747c478bd9Sstevel@tonic-gate 			break;
8757c478bd9Sstevel@tonic-gate 		case OK:
8767c478bd9Sstevel@tonic-gate 			val = ok(name, np->first.ap);
8777c478bd9Sstevel@tonic-gate 			break;
8787c478bd9Sstevel@tonic-gate 		case EXEC:
8797c478bd9Sstevel@tonic-gate 			val = doexec(name, np->first.ap);
8807c478bd9Sstevel@tonic-gate 			break;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		case VARARGS: {
8837c478bd9Sstevel@tonic-gate 			struct Arglist *ap = np->first.vp;
8847c478bd9Sstevel@tonic-gate 			char *cp;
8857c478bd9Sstevel@tonic-gate 			cp = ap->nextstr - (strlen(name)+1);
8867c478bd9Sstevel@tonic-gate 			if (cp >= (char *)(ap->nextvar+3)) {
8877c478bd9Sstevel@tonic-gate 				/* there is room just copy the name */
8887c478bd9Sstevel@tonic-gate 				val = 1;
8897c478bd9Sstevel@tonic-gate 				(void) strcpy(cp, name);
8907c478bd9Sstevel@tonic-gate 				*ap->nextvar++ = cp;
8917c478bd9Sstevel@tonic-gate 				ap->nextstr = cp;
8927c478bd9Sstevel@tonic-gate 			} else {
8937c478bd9Sstevel@tonic-gate 				/* no more room, exec command */
8947c478bd9Sstevel@tonic-gate 				*ap->nextvar++ = name;
8957c478bd9Sstevel@tonic-gate 				*ap->nextvar = 0;
8967c478bd9Sstevel@tonic-gate 				val = 1;
8977c478bd9Sstevel@tonic-gate 				varexecrc = doexec((char *)0, ap->arglist);
8987c478bd9Sstevel@tonic-gate 				ap->nextstr = ap->end;
8997c478bd9Sstevel@tonic-gate 				ap->nextvar = ap->firstvar;
9007c478bd9Sstevel@tonic-gate 			}
9017c478bd9Sstevel@tonic-gate 			break;
9027c478bd9Sstevel@tonic-gate 		}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 		case DEPTH:
9057c478bd9Sstevel@tonic-gate 		case MOUNT:
9067c478bd9Sstevel@tonic-gate 		case FOLLOW:
9077c478bd9Sstevel@tonic-gate 			val = 1;
9087c478bd9Sstevel@tonic-gate 			break;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 		case NAME: {
9117c478bd9Sstevel@tonic-gate 			/*
9127c478bd9Sstevel@tonic-gate 			 * XPG4 find should not treat a leading '.' in a
9137c478bd9Sstevel@tonic-gate 			 * filename specially for pattern matching.
9147c478bd9Sstevel@tonic-gate 			 * /usr/bin/find  will not pattern match a leading
9157c478bd9Sstevel@tonic-gate 			 * '.' in a filename, unless '.' is explicitly
9167c478bd9Sstevel@tonic-gate 			 * specified.
9177c478bd9Sstevel@tonic-gate 			 */
9187c478bd9Sstevel@tonic-gate #ifdef XPG4
9197c478bd9Sstevel@tonic-gate 			val = !fnmatch(np->first.cp,
9207c478bd9Sstevel@tonic-gate 			    name+state->base, 0);
9217c478bd9Sstevel@tonic-gate #else
9227c478bd9Sstevel@tonic-gate 			val = !fnmatch(np->first.cp,
9237c478bd9Sstevel@tonic-gate 			    name+state->base, FNM_PERIOD);
9247c478bd9Sstevel@tonic-gate #endif
9257c478bd9Sstevel@tonic-gate 			break;
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 		case PRUNE:
9297c478bd9Sstevel@tonic-gate 			if (type == FTW_D)
9307c478bd9Sstevel@tonic-gate 				state->quit = FTW_PRUNE;
9317c478bd9Sstevel@tonic-gate 			val = 1;
9327c478bd9Sstevel@tonic-gate 			break;
9337c478bd9Sstevel@tonic-gate 		case NOUSER:
9347c478bd9Sstevel@tonic-gate 			val = ((getpwuid(statb->st_uid)) == 0);
9357c478bd9Sstevel@tonic-gate 			break;
9367c478bd9Sstevel@tonic-gate 		case NOGRP:
9377c478bd9Sstevel@tonic-gate 			val = ((getgrgid(statb->st_gid)) == 0);
9387c478bd9Sstevel@tonic-gate 			break;
9397c478bd9Sstevel@tonic-gate 		case FSTYPE:
9407c478bd9Sstevel@tonic-gate 			val = (strcmp(np->first.cp, statb->st_fstype) == 0);
9417c478bd9Sstevel@tonic-gate 			break;
9427c478bd9Sstevel@tonic-gate 		case CPIO:
9437c478bd9Sstevel@tonic-gate 			output = (FILE *)np->first.l;
9447c478bd9Sstevel@tonic-gate 			(void) fprintf(output, "%s\n", name);
9457c478bd9Sstevel@tonic-gate 			val = 1;
9467c478bd9Sstevel@tonic-gate 			break;
9477c478bd9Sstevel@tonic-gate 		case PRINT:
9487c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s\n", name);
9497c478bd9Sstevel@tonic-gate 			val = 1;
9507c478bd9Sstevel@tonic-gate 			break;
9517c478bd9Sstevel@tonic-gate 		case LS:
9527c478bd9Sstevel@tonic-gate 			(void) list(name, statb);
9537c478bd9Sstevel@tonic-gate 			val = 1;
9547c478bd9Sstevel@tonic-gate 			break;
9557c478bd9Sstevel@tonic-gate 		case XATTR:
9567c478bd9Sstevel@tonic-gate 			filename = gettail(name);
9577c478bd9Sstevel@tonic-gate 			val = (pathconf(filename, _PC_XATTR_EXISTS) == 1);
9587c478bd9Sstevel@tonic-gate 			break;
9597c478bd9Sstevel@tonic-gate 		case ACL:
9607c478bd9Sstevel@tonic-gate 			/*
9617c478bd9Sstevel@tonic-gate 			 * Need to get the tail of the file name, since we have
9627c478bd9Sstevel@tonic-gate 			 * already chdir()ed into the directory (performed in
9637c478bd9Sstevel@tonic-gate 			 * nftw()) of the file
9647c478bd9Sstevel@tonic-gate 			 */
9657c478bd9Sstevel@tonic-gate 			filename = gettail(name);
966*fa9e4066Sahrens 			val = acl_trivial(name);
9677c478bd9Sstevel@tonic-gate 			break;
9687c478bd9Sstevel@tonic-gate 		}
9697c478bd9Sstevel@tonic-gate 		/*
9707c478bd9Sstevel@tonic-gate 		 * evaluate 'val' and 'not' (exclusive-or)
9717c478bd9Sstevel@tonic-gate 		 * if no inversion (not == 1), return only when val == 0
9727c478bd9Sstevel@tonic-gate 		 * (primary not true). Otherwise, invert the primary
9737c478bd9Sstevel@tonic-gate 		 * and return when the primary is true.
9747c478bd9Sstevel@tonic-gate 		 * 'Lastval' saves the last result (fail or pass) when
9757c478bd9Sstevel@tonic-gate 		 * returning back to the calling routine.
9767c478bd9Sstevel@tonic-gate 		 */
9777c478bd9Sstevel@tonic-gate 		if (val^not) {
9787c478bd9Sstevel@tonic-gate 			lastval = 0;
9797c478bd9Sstevel@tonic-gate 			return (0);
9807c478bd9Sstevel@tonic-gate 		}
9817c478bd9Sstevel@tonic-gate 		lastval = 1;
9827c478bd9Sstevel@tonic-gate 		not = 1;
9837c478bd9Sstevel@tonic-gate 		np = np->next;
9847c478bd9Sstevel@tonic-gate 	}
9857c478bd9Sstevel@tonic-gate 	return (0);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate /*
9897c478bd9Sstevel@tonic-gate  * code for the -ok option
9907c478bd9Sstevel@tonic-gate  */
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate static int
9937c478bd9Sstevel@tonic-gate ok(name, argv)
9947c478bd9Sstevel@tonic-gate char *name;
9957c478bd9Sstevel@tonic-gate char *argv[];
9967c478bd9Sstevel@tonic-gate {
9977c478bd9Sstevel@tonic-gate 	int c, yes = 0;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	(void) fflush(stdout); 	/* to flush possible `-print' */
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if ((*argv != dummyarg) && (strcmp(*argv, name)))
10027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "< %s ... %s >?   ", *argv, name);
10037c478bd9Sstevel@tonic-gate 	else
10047c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "< {} ... %s >?   ", name);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	(void) fflush(stderr);
10077c478bd9Sstevel@tonic-gate 	if ((c = tolower(getchar())) == *nl_langinfo(YESSTR))
10087c478bd9Sstevel@tonic-gate 		yes = 1;
10097c478bd9Sstevel@tonic-gate 	while (c != '\n')
10107c478bd9Sstevel@tonic-gate 		if (c == EOF)
10117c478bd9Sstevel@tonic-gate 			exit(2);
10127c478bd9Sstevel@tonic-gate 		else
10137c478bd9Sstevel@tonic-gate 			c = getchar();
10147c478bd9Sstevel@tonic-gate 	return (yes? doexec(name, argv): 0);
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate /*
10187c478bd9Sstevel@tonic-gate  * execute argv with {} replaced by name
10197c478bd9Sstevel@tonic-gate  */
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate static int
10227c478bd9Sstevel@tonic-gate doexec(name, argv)
10237c478bd9Sstevel@tonic-gate char *name;
10247c478bd9Sstevel@tonic-gate char *argv[];
10257c478bd9Sstevel@tonic-gate {
10267c478bd9Sstevel@tonic-gate 	char *cp;
10277c478bd9Sstevel@tonic-gate 	char **av = argv;
10287c478bd9Sstevel@tonic-gate 	int dummyseen = 0;
10297c478bd9Sstevel@tonic-gate 	int r = 0;
10307c478bd9Sstevel@tonic-gate 	pid_t pid;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);	  /* to flush possible `-print' */
10337c478bd9Sstevel@tonic-gate 	if (name) {
10347c478bd9Sstevel@tonic-gate 		while (cp = *av++) {
10357c478bd9Sstevel@tonic-gate 			if (cp == dummyarg) {
10367c478bd9Sstevel@tonic-gate 				dummyseen = 1;
10377c478bd9Sstevel@tonic-gate 				av[-1] = name;
10387c478bd9Sstevel@tonic-gate 			}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 		}
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 	if (argv[0] == NULL)    /* null command line */
10437c478bd9Sstevel@tonic-gate 		return (r);
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	if (pid = fork()) {
10467c478bd9Sstevel@tonic-gate 		while (wait(&r) != pid);
10477c478bd9Sstevel@tonic-gate 	} else /* child */ {
10487c478bd9Sstevel@tonic-gate 		(void) execvp(argv[0], argv);
10497c478bd9Sstevel@tonic-gate 		exit(1);
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 	if (name && dummyseen) {
10527c478bd9Sstevel@tonic-gate 		for (av = argv; cp = *av++; ) {
10537c478bd9Sstevel@tonic-gate 			if (cp == name)
10547c478bd9Sstevel@tonic-gate 				av[-1] = dummyarg;
10557c478bd9Sstevel@tonic-gate 		}
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	return (!r);
10597c478bd9Sstevel@tonic-gate }
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate /*
10637c478bd9Sstevel@tonic-gate  *  Table lookup routine
10647c478bd9Sstevel@tonic-gate  */
10657c478bd9Sstevel@tonic-gate static struct Args *
10667c478bd9Sstevel@tonic-gate lookup(word)
10677c478bd9Sstevel@tonic-gate char *word;
10687c478bd9Sstevel@tonic-gate {
10697c478bd9Sstevel@tonic-gate 	struct Args *argp = commands;
10707c478bd9Sstevel@tonic-gate 	int second;
10717c478bd9Sstevel@tonic-gate 	if (word == 0 || *word == 0)
10727c478bd9Sstevel@tonic-gate 		return (0);
10737c478bd9Sstevel@tonic-gate 	second = word[1];
10747c478bd9Sstevel@tonic-gate 	while (*argp->name) {
10757c478bd9Sstevel@tonic-gate 		if (second == argp->name[1] && strcmp(word, argp->name) == 0)
10767c478bd9Sstevel@tonic-gate 			return (argp);
10777c478bd9Sstevel@tonic-gate 		argp++;
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 	return (0);
10807c478bd9Sstevel@tonic-gate }
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate /*
10847c478bd9Sstevel@tonic-gate  * Get space for variable length argument list
10857c478bd9Sstevel@tonic-gate  */
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate static struct Arglist *
10887c478bd9Sstevel@tonic-gate varargs(com)
10897c478bd9Sstevel@tonic-gate char **com;
10907c478bd9Sstevel@tonic-gate {
10917c478bd9Sstevel@tonic-gate 	struct Arglist *ap;
10927c478bd9Sstevel@tonic-gate 	int n;
10937c478bd9Sstevel@tonic-gate 	char **ep;
10947c478bd9Sstevel@tonic-gate 	if (varsize == 0) {
10957c478bd9Sstevel@tonic-gate 		n = 2*sizeof (char **);
10967c478bd9Sstevel@tonic-gate 		for (ep = environ; *ep; ep++)
10977c478bd9Sstevel@tonic-gate 			n += (strlen(*ep)+sizeof (ep) + 1);
10987c478bd9Sstevel@tonic-gate 		varsize = sizeof (struct Arglist)+ARG_MAX-PATH_MAX-n-1;
10997c478bd9Sstevel@tonic-gate 	}
11007c478bd9Sstevel@tonic-gate 	ap = (struct Arglist *)malloc(varsize+1);
11017c478bd9Sstevel@tonic-gate 	ap->end = (char *)ap + varsize;
11027c478bd9Sstevel@tonic-gate 	ap->nextstr = ap->end;
11037c478bd9Sstevel@tonic-gate 	ap->nextvar = ap->arglist;
11047c478bd9Sstevel@tonic-gate 	while (*ap->nextvar++ = *com++);
11057c478bd9Sstevel@tonic-gate 	ap->nextvar--;
11067c478bd9Sstevel@tonic-gate 	ap->firstvar = ap->nextvar;
11077c478bd9Sstevel@tonic-gate 	ap->next = lastlist;
11087c478bd9Sstevel@tonic-gate 	lastlist = ap;
11097c478bd9Sstevel@tonic-gate 	return (ap);
11107c478bd9Sstevel@tonic-gate }
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate /*
11137c478bd9Sstevel@tonic-gate  * filter command support
11147c478bd9Sstevel@tonic-gate  * fork and exec cmd(argv) according to mode:
11157c478bd9Sstevel@tonic-gate  *
11167c478bd9Sstevel@tonic-gate  *	"r"	with fp as stdin of cmd (default stdin), cmd stdout returned
11177c478bd9Sstevel@tonic-gate  *	"w"	with fp as stdout of cmd (default stdout), cmd stdin returned
11187c478bd9Sstevel@tonic-gate  */
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate #define	CMDERR	((1<<8)-1)	/* command error exit code		*/
11217c478bd9Sstevel@tonic-gate #define	MAXCMDS	8		/* max # simultaneous cmdopen()'s	*/
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate static struct			/* info for each cmdopen()		*/
11247c478bd9Sstevel@tonic-gate {
11257c478bd9Sstevel@tonic-gate 	FILE	*fp;		/* returned by cmdopen()		*/
11267c478bd9Sstevel@tonic-gate 	pid_t	pid;		/* pid used by cmdopen()		*/
11277c478bd9Sstevel@tonic-gate } cmdproc[MAXCMDS];
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate static FILE *
11307c478bd9Sstevel@tonic-gate cmdopen(cmd, argv, mode, fp)
11317c478bd9Sstevel@tonic-gate char	*cmd;
11327c478bd9Sstevel@tonic-gate char	**argv;
11337c478bd9Sstevel@tonic-gate char	*mode;
11347c478bd9Sstevel@tonic-gate FILE	*fp;
11357c478bd9Sstevel@tonic-gate {
11367c478bd9Sstevel@tonic-gate 	int	proc;
11377c478bd9Sstevel@tonic-gate 	int	cmdfd;
11387c478bd9Sstevel@tonic-gate 	int	usrfd;
11397c478bd9Sstevel@tonic-gate 	int		pio[2];
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	switch (*mode) {
11427c478bd9Sstevel@tonic-gate 	case 'r':
11437c478bd9Sstevel@tonic-gate 		cmdfd = 1;
11447c478bd9Sstevel@tonic-gate 		usrfd = 0;
11457c478bd9Sstevel@tonic-gate 		break;
11467c478bd9Sstevel@tonic-gate 	case 'w':
11477c478bd9Sstevel@tonic-gate 		cmdfd = 0;
11487c478bd9Sstevel@tonic-gate 		usrfd = 1;
11497c478bd9Sstevel@tonic-gate 		break;
11507c478bd9Sstevel@tonic-gate 	default:
11517c478bd9Sstevel@tonic-gate 		return (0);
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	for (proc = 0; proc < MAXCMDS; proc++)
11557c478bd9Sstevel@tonic-gate 		if (!cmdproc[proc].fp)
11567c478bd9Sstevel@tonic-gate 			break;
11577c478bd9Sstevel@tonic-gate 	if (proc >= MAXCMDS)
11587c478bd9Sstevel@tonic-gate 		return (0);
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	if (pipe(pio))
11617c478bd9Sstevel@tonic-gate 		return (0);
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	switch (cmdproc[proc].pid = fork()) {
11647c478bd9Sstevel@tonic-gate 	case -1:
11657c478bd9Sstevel@tonic-gate 		return (0);
11667c478bd9Sstevel@tonic-gate 	case 0:
11677c478bd9Sstevel@tonic-gate 		if (fp && fileno(fp) != usrfd) {
11687c478bd9Sstevel@tonic-gate 			(void) close(usrfd);
11697c478bd9Sstevel@tonic-gate 			if (dup2(fileno(fp), usrfd) != usrfd)
11707c478bd9Sstevel@tonic-gate 				_exit(CMDERR);
11717c478bd9Sstevel@tonic-gate 			(void) close(fileno(fp));
11727c478bd9Sstevel@tonic-gate 		}
11737c478bd9Sstevel@tonic-gate 		(void) close(cmdfd);
11747c478bd9Sstevel@tonic-gate 		if (dup2(pio[cmdfd], cmdfd) != cmdfd)
11757c478bd9Sstevel@tonic-gate 			_exit(CMDERR);
11767c478bd9Sstevel@tonic-gate 		(void) close(pio[cmdfd]);
11777c478bd9Sstevel@tonic-gate 		(void) close(pio[usrfd]);
11787c478bd9Sstevel@tonic-gate 		(void) execvp(cmd, argv);
11797c478bd9Sstevel@tonic-gate 		if (errno == ENOEXEC) {
11807c478bd9Sstevel@tonic-gate 			char	**p;
11817c478bd9Sstevel@tonic-gate 			char		**v;
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 			/*
11847c478bd9Sstevel@tonic-gate 			 * assume cmd is a shell script
11857c478bd9Sstevel@tonic-gate 			 */
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 			p = argv;
11887c478bd9Sstevel@tonic-gate 			while (*p++);
11897c478bd9Sstevel@tonic-gate 			if (v = (char **)malloc((p - argv + 1) *
11907c478bd9Sstevel@tonic-gate 					sizeof (char **))) {
11917c478bd9Sstevel@tonic-gate 				p = v;
11927c478bd9Sstevel@tonic-gate 				*p++ = cmd;
11937c478bd9Sstevel@tonic-gate 				if (*argv) argv++;
11947c478bd9Sstevel@tonic-gate 				while (*p++ = *argv++);
11957c478bd9Sstevel@tonic-gate 				(void) execv(getshell(), v);
11967c478bd9Sstevel@tonic-gate 			}
11977c478bd9Sstevel@tonic-gate 		}
11987c478bd9Sstevel@tonic-gate 		_exit(CMDERR);
11997c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
12007c478bd9Sstevel@tonic-gate 	default:
12017c478bd9Sstevel@tonic-gate 		(void) close(pio[cmdfd]);
12027c478bd9Sstevel@tonic-gate 		return (cmdproc[proc].fp = fdopen(pio[usrfd], mode));
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate /*
12077c478bd9Sstevel@tonic-gate  * close a stream opened by cmdopen()
12087c478bd9Sstevel@tonic-gate  * -1 returned if cmdopen() had a problem
12097c478bd9Sstevel@tonic-gate  * otherwise exit() status of command is returned
12107c478bd9Sstevel@tonic-gate  */
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate static int
12137c478bd9Sstevel@tonic-gate cmdclose(fp)
12147c478bd9Sstevel@tonic-gate FILE	*fp;
12157c478bd9Sstevel@tonic-gate {
12167c478bd9Sstevel@tonic-gate 	int	i;
12177c478bd9Sstevel@tonic-gate 	pid_t	p, pid;
12187c478bd9Sstevel@tonic-gate 	int		status;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAXCMDS; i++)
12217c478bd9Sstevel@tonic-gate 		if (fp == cmdproc[i].fp) break;
12227c478bd9Sstevel@tonic-gate 	if (i >= MAXCMDS)
12237c478bd9Sstevel@tonic-gate 		return (-1);
12247c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
12257c478bd9Sstevel@tonic-gate 	cmdproc[i].fp = 0;
12267c478bd9Sstevel@tonic-gate 	pid = cmdproc[i].pid;
12277c478bd9Sstevel@tonic-gate 	while ((p = wait(&status)) != pid && p != (pid_t)-1);
12287c478bd9Sstevel@tonic-gate 	if (p == pid) {
12297c478bd9Sstevel@tonic-gate 		status = (status >> 8) & CMDERR;
12307c478bd9Sstevel@tonic-gate 		if (status == CMDERR)
12317c478bd9Sstevel@tonic-gate 			status = -1;
12327c478bd9Sstevel@tonic-gate 	}
12337c478bd9Sstevel@tonic-gate 	else
12347c478bd9Sstevel@tonic-gate 		status = -1;
12357c478bd9Sstevel@tonic-gate 	return (status);
12367c478bd9Sstevel@tonic-gate }
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate /*
12397c478bd9Sstevel@tonic-gate  * return pointer to the full path name of the shell
12407c478bd9Sstevel@tonic-gate  *
12417c478bd9Sstevel@tonic-gate  * SHELL is read from the environment and must start with /
12427c478bd9Sstevel@tonic-gate  *
12437c478bd9Sstevel@tonic-gate  * if set-uid or set-gid then the executable and its containing
12447c478bd9Sstevel@tonic-gate  * directory must not be writable by the real user
12457c478bd9Sstevel@tonic-gate  *
12467c478bd9Sstevel@tonic-gate  * /usr/bin/sh is returned by default
12477c478bd9Sstevel@tonic-gate  */
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate char *
12507c478bd9Sstevel@tonic-gate getshell()
12517c478bd9Sstevel@tonic-gate {
12527c478bd9Sstevel@tonic-gate 	char	*s;
12537c478bd9Sstevel@tonic-gate 	char	*sh;
12547c478bd9Sstevel@tonic-gate 	uid_t	u;
12557c478bd9Sstevel@tonic-gate 	int	j;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	if (((sh = getenv("SHELL")) != 0) && *sh == '/') {
12587c478bd9Sstevel@tonic-gate 		if (u = getuid()) {
12597c478bd9Sstevel@tonic-gate 			if ((u != geteuid() || getgid() != getegid()) &&
12607c478bd9Sstevel@tonic-gate 					!access(sh, 2))
12617c478bd9Sstevel@tonic-gate 				goto defshell;
12627c478bd9Sstevel@tonic-gate 			s = strrchr(sh, '/');
12637c478bd9Sstevel@tonic-gate 			*s = 0;
12647c478bd9Sstevel@tonic-gate 			j = access(sh, 2);
12657c478bd9Sstevel@tonic-gate 			*s = '/';
12667c478bd9Sstevel@tonic-gate 			if (!j) goto defshell;
12677c478bd9Sstevel@tonic-gate 		}
12687c478bd9Sstevel@tonic-gate 		return (sh);
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate defshell:
12717c478bd9Sstevel@tonic-gate 	return ("/usr/bin/sh");
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate /*
12757c478bd9Sstevel@tonic-gate  * the following functions implement the added "-ls" option
12767c478bd9Sstevel@tonic-gate  */
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate #include <utmpx.h>
12797c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate struct		utmpx utmpx;
12827c478bd9Sstevel@tonic-gate #define	NMAX	(sizeof (utmpx.ut_name))
12837c478bd9Sstevel@tonic-gate #define	SCPYN(a, b)	(void) strncpy(a, b, NMAX)
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate #define	NUID	64
12867c478bd9Sstevel@tonic-gate #define	NGID	64
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate static struct ncache {
12897c478bd9Sstevel@tonic-gate 	int	id;
12907c478bd9Sstevel@tonic-gate 	char	name[NMAX+1];
12917c478bd9Sstevel@tonic-gate } nc[NUID], gc[NGID];
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate /*
12947c478bd9Sstevel@tonic-gate  * This function assumes that the password file is hashed
12957c478bd9Sstevel@tonic-gate  * (or some such) to allow fast access based on a name key.
12967c478bd9Sstevel@tonic-gate  */
12977c478bd9Sstevel@tonic-gate static char *
12987c478bd9Sstevel@tonic-gate getname(uid_t uid)
12997c478bd9Sstevel@tonic-gate {
13007c478bd9Sstevel@tonic-gate 	struct passwd *pw;
13017c478bd9Sstevel@tonic-gate 	int cp;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate #if	(((NUID) & ((NUID) - 1)) != 0)
13047c478bd9Sstevel@tonic-gate 	cp = uid % (NUID);
13057c478bd9Sstevel@tonic-gate #else
13067c478bd9Sstevel@tonic-gate 	cp = uid & ((NUID) - 1);
13077c478bd9Sstevel@tonic-gate #endif
13087c478bd9Sstevel@tonic-gate 	if (uid >= 0 && nc[cp].id == uid && nc[cp].name[0])
13097c478bd9Sstevel@tonic-gate 		return (nc[cp].name);
13107c478bd9Sstevel@tonic-gate 	pw = getpwuid(uid);
13117c478bd9Sstevel@tonic-gate 	if (!pw)
13127c478bd9Sstevel@tonic-gate 		return (0);
13137c478bd9Sstevel@tonic-gate 	nc[cp].id = uid;
13147c478bd9Sstevel@tonic-gate 	SCPYN(nc[cp].name, pw->pw_name);
13157c478bd9Sstevel@tonic-gate 	return (nc[cp].name);
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate /*
13197c478bd9Sstevel@tonic-gate  * This function assumes that the group file is hashed
13207c478bd9Sstevel@tonic-gate  * (or some such) to allow fast access based on a name key.
13217c478bd9Sstevel@tonic-gate  */
13227c478bd9Sstevel@tonic-gate static char *
13237c478bd9Sstevel@tonic-gate getgroup(gid_t gid)
13247c478bd9Sstevel@tonic-gate {
13257c478bd9Sstevel@tonic-gate 	struct group *gr;
13267c478bd9Sstevel@tonic-gate 	int cp;
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate #if	(((NGID) & ((NGID) - 1)) != 0)
13297c478bd9Sstevel@tonic-gate 	cp = gid % (NGID);
13307c478bd9Sstevel@tonic-gate #else
13317c478bd9Sstevel@tonic-gate 	cp = gid & ((NGID) - 1);
13327c478bd9Sstevel@tonic-gate #endif
13337c478bd9Sstevel@tonic-gate 	if (gid >= 0 && gc[cp].id == gid && gc[cp].name[0])
13347c478bd9Sstevel@tonic-gate 		return (gc[cp].name);
13357c478bd9Sstevel@tonic-gate 	gr = getgrgid(gid);
13367c478bd9Sstevel@tonic-gate 	if (!gr)
13377c478bd9Sstevel@tonic-gate 		return (0);
13387c478bd9Sstevel@tonic-gate 	gc[cp].id = gid;
13397c478bd9Sstevel@tonic-gate 	SCPYN(gc[cp].name, gr->gr_name);
13407c478bd9Sstevel@tonic-gate 	return (gc[cp].name);
13417c478bd9Sstevel@tonic-gate }
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate #define	permoffset(who)		((who) * 3)
13447c478bd9Sstevel@tonic-gate #define	permission(who, type)	((type) >> permoffset(who))
13457c478bd9Sstevel@tonic-gate #define	kbytes(bytes)		(((bytes) + 1023) / 1024)
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate static int
13487c478bd9Sstevel@tonic-gate list(file, stp)
13497c478bd9Sstevel@tonic-gate 	char *file;
13507c478bd9Sstevel@tonic-gate 	struct stat *stp;
13517c478bd9Sstevel@tonic-gate {
13527c478bd9Sstevel@tonic-gate 	char pmode[32], uname[32], gname[32], fsize[32], ftime[32];
1353*fa9e4066Sahrens 	int trivial;
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate /*
13567c478bd9Sstevel@tonic-gate  * Each line below contains the relevant permission (column 1) and character
13577c478bd9Sstevel@tonic-gate  * shown when  the corresponding execute bit is either clear (column 2)
13587c478bd9Sstevel@tonic-gate  * or set (column 3)
13597c478bd9Sstevel@tonic-gate  * These permissions are as shown by ls(1b)
13607c478bd9Sstevel@tonic-gate  */
13617c478bd9Sstevel@tonic-gate 	static long special[] = {	S_ISUID, 'S', 's',
13627c478bd9Sstevel@tonic-gate 					S_ISGID, 'S', 's',
13637c478bd9Sstevel@tonic-gate 					S_ISVTX, 'T', 't' };
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	static time_t sixmonthsago = -1;
13667c478bd9Sstevel@tonic-gate #ifdef	S_IFLNK
13677c478bd9Sstevel@tonic-gate 	char flink[MAXPATHLEN + 1];
13687c478bd9Sstevel@tonic-gate #endif
13697c478bd9Sstevel@tonic-gate 	int who;
13707c478bd9Sstevel@tonic-gate 	char *cp;
13717c478bd9Sstevel@tonic-gate 	char *tailname;
13727c478bd9Sstevel@tonic-gate 	time_t now;
13737c478bd9Sstevel@tonic-gate 	long long ksize;
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	if (file == NULL || stp == NULL)
13767c478bd9Sstevel@tonic-gate 		return (-1);
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	(void) time(&now);
13797c478bd9Sstevel@tonic-gate 	if (sixmonthsago == -1)
13807c478bd9Sstevel@tonic-gate 		sixmonthsago = now - 6L*30L*24L*60L*60L;
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	switch (stp->st_mode & S_IFMT) {
13837c478bd9Sstevel@tonic-gate #ifdef	S_IFDIR
13847c478bd9Sstevel@tonic-gate 	case S_IFDIR:	/* directory */
13857c478bd9Sstevel@tonic-gate 		pmode[0] = 'd';
13867c478bd9Sstevel@tonic-gate 		break;
13877c478bd9Sstevel@tonic-gate #endif
13887c478bd9Sstevel@tonic-gate #ifdef	S_IFCHR
13897c478bd9Sstevel@tonic-gate 	case S_IFCHR:	/* character special */
13907c478bd9Sstevel@tonic-gate 		pmode[0] = 'c';
13917c478bd9Sstevel@tonic-gate 		break;
13927c478bd9Sstevel@tonic-gate #endif
13937c478bd9Sstevel@tonic-gate #ifdef	S_IFBLK
13947c478bd9Sstevel@tonic-gate 	case S_IFBLK:	/* block special */
13957c478bd9Sstevel@tonic-gate 		pmode[0] = 'b';
13967c478bd9Sstevel@tonic-gate 		break;
13977c478bd9Sstevel@tonic-gate #endif
13987c478bd9Sstevel@tonic-gate #ifdef	S_IFIFO
13997c478bd9Sstevel@tonic-gate 	case S_IFIFO:	/* fifo special */
14007c478bd9Sstevel@tonic-gate 		pmode[0] = 'p';
14017c478bd9Sstevel@tonic-gate 		break;
14027c478bd9Sstevel@tonic-gate #endif
14037c478bd9Sstevel@tonic-gate #ifdef	S_IFLNK
14047c478bd9Sstevel@tonic-gate 	case S_IFLNK:	/* symbolic link */
14057c478bd9Sstevel@tonic-gate 		pmode[0] = 'l';
14067c478bd9Sstevel@tonic-gate 		break;
14077c478bd9Sstevel@tonic-gate #endif
14087c478bd9Sstevel@tonic-gate #ifdef	S_IFSOCK
14097c478bd9Sstevel@tonic-gate 	case S_IFSOCK:	/* socket */
14107c478bd9Sstevel@tonic-gate 		pmode[0] = 's';
14117c478bd9Sstevel@tonic-gate 		break;
14127c478bd9Sstevel@tonic-gate #endif
14137c478bd9Sstevel@tonic-gate #ifdef	S_IFDOOR
14147c478bd9Sstevel@tonic-gate 	case S_IFDOOR:	/* door */
14157c478bd9Sstevel@tonic-gate 		pmode[0] = 'D';
14167c478bd9Sstevel@tonic-gate 		break;
14177c478bd9Sstevel@tonic-gate #endif
14187c478bd9Sstevel@tonic-gate #ifdef	S_IFREG
14197c478bd9Sstevel@tonic-gate 	case S_IFREG:	/* regular */
14207c478bd9Sstevel@tonic-gate 		pmode[0] = '-';
14217c478bd9Sstevel@tonic-gate 		break;
14227c478bd9Sstevel@tonic-gate #endif
14237c478bd9Sstevel@tonic-gate 	default:
14247c478bd9Sstevel@tonic-gate 		pmode[0] = '?';
14257c478bd9Sstevel@tonic-gate 		break;
14267c478bd9Sstevel@tonic-gate 	}
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	for (who = 0; who < 3; who++) {
14297c478bd9Sstevel@tonic-gate 		int is_exec =  stp->st_mode & permission(who, S_IEXEC)? 1 : 0;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 		if (stp->st_mode & permission(who, S_IREAD))
14327c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 1] = 'r';
14337c478bd9Sstevel@tonic-gate 		else
14347c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 1] = '-';
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 		if (stp->st_mode & permission(who, S_IWRITE))
14377c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 2] = 'w';
14387c478bd9Sstevel@tonic-gate 		else
14397c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 2] = '-';
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 		if (stp->st_mode & special[who * 3])
14427c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 3] =
14437c478bd9Sstevel@tonic-gate 				special[who * 3 + 1 + is_exec];
14447c478bd9Sstevel@tonic-gate 		else if (is_exec)
14457c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 3] = 'x';
14467c478bd9Sstevel@tonic-gate 		else
14477c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 3] = '-';
14487c478bd9Sstevel@tonic-gate 	}
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	/*
14517c478bd9Sstevel@tonic-gate 	 * Need to get the tail of the file name, since we have
14527c478bd9Sstevel@tonic-gate 	 * already chdir()ed into the directory of the file
14537c478bd9Sstevel@tonic-gate 	 */
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	tailname = gettail(file);
14567c478bd9Sstevel@tonic-gate 
1457*fa9e4066Sahrens 	trivial = acl_trivial(tailname);
1458*fa9e4066Sahrens 	if (trivial == -1)
1459*fa9e4066Sahrens 		trivial =  0;
1460*fa9e4066Sahrens 
1461*fa9e4066Sahrens 	if (trivial == 1)
14627c478bd9Sstevel@tonic-gate 		pmode[permoffset(who) + 1] = '+';
14637c478bd9Sstevel@tonic-gate 	else
14647c478bd9Sstevel@tonic-gate 		pmode[permoffset(who) + 1] = ' ';
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	pmode[permoffset(who) + 2] = '\0';
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	/*
14697c478bd9Sstevel@tonic-gate 	 * Prepare uname and gname.  Always add a space afterwards
14707c478bd9Sstevel@tonic-gate 	 * to keep columns from running together.
14717c478bd9Sstevel@tonic-gate 	 */
14727c478bd9Sstevel@tonic-gate 	cp = getname(stp->st_uid);
14737c478bd9Sstevel@tonic-gate 	if (cp != NULL)
14747c478bd9Sstevel@tonic-gate 		(void) sprintf(uname, "%-8s ", cp);
14757c478bd9Sstevel@tonic-gate 	else
14767c478bd9Sstevel@tonic-gate 		(void) sprintf(uname, "%-8ld ", stp->st_uid);
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	cp = getgroup(stp->st_gid);
14797c478bd9Sstevel@tonic-gate 	if (cp != NULL)
14807c478bd9Sstevel@tonic-gate 		(void) sprintf(gname, "%-8s ", cp);
14817c478bd9Sstevel@tonic-gate 	else
14827c478bd9Sstevel@tonic-gate 		(void) sprintf(gname, "%-8ld ", stp->st_gid);
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	if (pmode[0] == 'b' || pmode[0] == 'c')
14857c478bd9Sstevel@tonic-gate 		(void) sprintf(fsize, "%3ld,%4ld",
14867c478bd9Sstevel@tonic-gate 			major(stp->st_rdev), minor(stp->st_rdev));
14877c478bd9Sstevel@tonic-gate 	else {
14887c478bd9Sstevel@tonic-gate 		(void) sprintf(fsize, (stp->st_size < 100000000) ?
14897c478bd9Sstevel@tonic-gate 			"%8lld" : "%lld", stp->st_size);
14907c478bd9Sstevel@tonic-gate #ifdef	S_IFLNK
14917c478bd9Sstevel@tonic-gate 		if (pmode[0] == 'l') {
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 			who = readlink(tailname, flink, sizeof (flink) - 1);
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 			if (who >= 0)
14977c478bd9Sstevel@tonic-gate 				flink[who] = '\0';
14987c478bd9Sstevel@tonic-gate 			else
14997c478bd9Sstevel@tonic-gate 				flink[0] = '\0';
15007c478bd9Sstevel@tonic-gate 		}
15017c478bd9Sstevel@tonic-gate #endif
15027c478bd9Sstevel@tonic-gate 	}
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	cp = ctime(&stp->st_mtime);
15057c478bd9Sstevel@tonic-gate 	if (stp->st_mtime < sixmonthsago || stp->st_mtime > now)
15067c478bd9Sstevel@tonic-gate 		(void) sprintf(ftime, "%-7.7s %-4.4s", cp + 4, cp + 20);
15077c478bd9Sstevel@tonic-gate 	else
15087c478bd9Sstevel@tonic-gate 		(void) sprintf(ftime, "%-12.12s", cp + 4);
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	(void) printf((stp->st_ino < 100000) ? "%5llu " :
15117c478bd9Sstevel@tonic-gate 		"%llu ", stp->st_ino);  /* inode #	*/
15127c478bd9Sstevel@tonic-gate #ifdef	S_IFSOCK
15137c478bd9Sstevel@tonic-gate 	ksize = (long long) kbytes(ldbtob(stp->st_blocks)); /* kbytes */
15147c478bd9Sstevel@tonic-gate #else
15157c478bd9Sstevel@tonic-gate 	ksize = (long long) kbytes(stp->st_size); /* kbytes */
15167c478bd9Sstevel@tonic-gate #endif
15177c478bd9Sstevel@tonic-gate 	(void) printf((ksize < 10000) ? "%4lld " : "%lld ", ksize);
15187c478bd9Sstevel@tonic-gate 	(void) printf("%s %2ld %s%s%s %s %s%s%s\n",
15197c478bd9Sstevel@tonic-gate 		pmode,					/* protection	*/
15207c478bd9Sstevel@tonic-gate 		stp->st_nlink,				/* # of links	*/
15217c478bd9Sstevel@tonic-gate 		uname,					/* owner	*/
15227c478bd9Sstevel@tonic-gate 		gname,					/* group	*/
15237c478bd9Sstevel@tonic-gate 		fsize,					/* # of bytes	*/
15247c478bd9Sstevel@tonic-gate 		ftime,					/* modify time	*/
15257c478bd9Sstevel@tonic-gate 		file,					/* name		*/
15267c478bd9Sstevel@tonic-gate #ifdef	S_IFLNK
15277c478bd9Sstevel@tonic-gate 		(pmode[0] == 'l') ? " -> " : "",
15287c478bd9Sstevel@tonic-gate 		(pmode[0] == 'l') ? flink  : ""		/* symlink	*/
15297c478bd9Sstevel@tonic-gate #else
15307c478bd9Sstevel@tonic-gate 		"",
15317c478bd9Sstevel@tonic-gate 		""
15327c478bd9Sstevel@tonic-gate #endif
15337c478bd9Sstevel@tonic-gate );
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	return (0);
15367c478bd9Sstevel@tonic-gate }
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate static char *
15397c478bd9Sstevel@tonic-gate new_string(char *s)
15407c478bd9Sstevel@tonic-gate {
15417c478bd9Sstevel@tonic-gate 	char *p = strdup(s);
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	if (p)
15447c478bd9Sstevel@tonic-gate 		return (p);
15457c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%s: out of memory\n"), cmdname);
15467c478bd9Sstevel@tonic-gate 	exit(1);
15477c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
15487c478bd9Sstevel@tonic-gate }
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate /*
15517c478bd9Sstevel@tonic-gate  * Read remote file system types from REMOTE_FS into the
15527c478bd9Sstevel@tonic-gate  * remote_fstypes array.
15537c478bd9Sstevel@tonic-gate  */
15547c478bd9Sstevel@tonic-gate static void
15557c478bd9Sstevel@tonic-gate init_remote_fs()
15567c478bd9Sstevel@tonic-gate {
15577c478bd9Sstevel@tonic-gate 	FILE    *fp;
15587c478bd9Sstevel@tonic-gate 	char    line_buf[LINEBUF_SIZE];
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	if ((fp = fopen(REMOTE_FS, "r")) == NULL) {
15617c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
15627c478bd9Sstevel@tonic-gate 			gettext("%s: Warning: can't open %s, ignored\n"),
15637c478bd9Sstevel@tonic-gate 				REMOTE_FS, cmdname);
15647c478bd9Sstevel@tonic-gate 		/* Use default string name for NFS */
15657c478bd9Sstevel@tonic-gate 		remote_fstypes[fstype_index++] = "nfs";
15667c478bd9Sstevel@tonic-gate 		return;
15677c478bd9Sstevel@tonic-gate 	}
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	while (fgets(line_buf, sizeof (line_buf), fp) != NULL) {
15707c478bd9Sstevel@tonic-gate 		char buf[LINEBUF_SIZE];
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 		/* LINTED - unbounded string specifier */
15737c478bd9Sstevel@tonic-gate 		(void) sscanf(line_buf, "%s", buf);
15747c478bd9Sstevel@tonic-gate 		remote_fstypes[fstype_index++] = new_string(buf);
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 		if (fstype_index == N_FSTYPES)
15777c478bd9Sstevel@tonic-gate 			break;
15787c478bd9Sstevel@tonic-gate 	}
15797c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
15807c478bd9Sstevel@tonic-gate }
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate #define	NPERM	30			/* Largest machine */
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate /*
15857c478bd9Sstevel@tonic-gate  * The PERM struct is the machine that builds permissions.  The p_special
15867c478bd9Sstevel@tonic-gate  * field contains what permissions need to be checked at run-time in
15877c478bd9Sstevel@tonic-gate  * getmode().  This is one of 'X', 'u', 'g', or 'o'.  It contains '\0' to
15887c478bd9Sstevel@tonic-gate  * indicate normal processing.
15897c478bd9Sstevel@tonic-gate  */
15907c478bd9Sstevel@tonic-gate typedef	struct	PERMST	{
15917c478bd9Sstevel@tonic-gate 	ushort_t	p_who;		/* Range of permission (e.g. ugo) */
15927c478bd9Sstevel@tonic-gate 	ushort_t	p_perm;		/* Bits to turn on, off, assign */
15937c478bd9Sstevel@tonic-gate 	uchar_t		p_op;		/* Operation: + - = */
15947c478bd9Sstevel@tonic-gate 	uchar_t		p_special;	/* Special handling? */
15957c478bd9Sstevel@tonic-gate }	PERMST;
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate #ifndef	S_ISVTX
15987c478bd9Sstevel@tonic-gate #define	S_ISVTX	0			/* Not .1 */
15997c478bd9Sstevel@tonic-gate #endif
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate /* Mask values */
16027c478bd9Sstevel@tonic-gate #define	P_A	(S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* allbits */
16037c478bd9Sstevel@tonic-gate #define	P_U	(S_ISUID|S_ISVTX|S_IRWXU)		/* user */
16047c478bd9Sstevel@tonic-gate #define	P_G	(S_ISGID|S_ISVTX|S_IRWXG)		/* group */
16057c478bd9Sstevel@tonic-gate #define	P_O	(S_ISVTX|S_IRWXO)			/* other */
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate static	int	iswho(int c);
16087c478bd9Sstevel@tonic-gate static	int	isop(int c);
16097c478bd9Sstevel@tonic-gate static	int	isperm(PERMST *pp, int c);
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate static	PERMST	machine[NPERM];		/* Permission construction machine */
16127c478bd9Sstevel@tonic-gate static	PERMST	*endp;			/* Last used PERM structure */
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate static	uint_t	nowho;			/* No who for this mode (DOS kludge) */
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate /*
16177c478bd9Sstevel@tonic-gate  * Read an ASCII string containing the symbolic/octal mode and
16187c478bd9Sstevel@tonic-gate  * compile an automaton that recognizes it.  The return value
16197c478bd9Sstevel@tonic-gate  * is NULL if everything is OK, otherwise it is -1.
16207c478bd9Sstevel@tonic-gate  */
16217c478bd9Sstevel@tonic-gate static int
16227c478bd9Sstevel@tonic-gate readmode(ascmode)
16237c478bd9Sstevel@tonic-gate const char *ascmode;
16247c478bd9Sstevel@tonic-gate {
16257c478bd9Sstevel@tonic-gate 	const char *amode = ascmode;
16267c478bd9Sstevel@tonic-gate 	PERMST *pp;
16277c478bd9Sstevel@tonic-gate 	int seen_X;
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	nowho = 0;
16307c478bd9Sstevel@tonic-gate 	seen_X = 0;
16317c478bd9Sstevel@tonic-gate 	pp = &machine[0];
16327c478bd9Sstevel@tonic-gate 	if (*amode >= '0' && *amode <= '7') {
16337c478bd9Sstevel@tonic-gate 		int mode;
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 		mode = 0;
16367c478bd9Sstevel@tonic-gate 		while (*amode >= '0' && *amode <= '7')
16377c478bd9Sstevel@tonic-gate 			mode = (mode<<3) + *amode++ - '0';
16387c478bd9Sstevel@tonic-gate 		if (*amode != '\0')
16397c478bd9Sstevel@tonic-gate 			return (-1);
16407c478bd9Sstevel@tonic-gate #if	S_ISUID != 04000 || S_ISGID != 02000 || \
16417c478bd9Sstevel@tonic-gate 	S_IRUSR != 0400 || S_IWUSR != 0200 || S_IXUSR != 0100 || \
16427c478bd9Sstevel@tonic-gate 	S_IRGRP != 0040 || S_IWGRP != 0020 || S_IXGRP != 0010 || \
16437c478bd9Sstevel@tonic-gate 	S_IROTH != 0004 || S_IWOTH != 0002 || S_IXOTH != 0001
16447c478bd9Sstevel@tonic-gate 		/*
16457c478bd9Sstevel@tonic-gate 		 * There is no requirement of the octal mode bits being
16467c478bd9Sstevel@tonic-gate 		 * the same as the S_ macros.
16477c478bd9Sstevel@tonic-gate 		 */
16487c478bd9Sstevel@tonic-gate 	{
16497c478bd9Sstevel@tonic-gate 		mode_t mapping[] = {
16507c478bd9Sstevel@tonic-gate 			S_IXOTH, S_IWOTH, S_IROTH,
16517c478bd9Sstevel@tonic-gate 			S_IXGRP, S_IWGRP, S_IRGRP,
16527c478bd9Sstevel@tonic-gate 			S_IXUSR, S_IWUSR, S_IRUSR,
16537c478bd9Sstevel@tonic-gate 			S_ISGID, S_ISUID,
16547c478bd9Sstevel@tonic-gate 			0
16557c478bd9Sstevel@tonic-gate 		};
16567c478bd9Sstevel@tonic-gate 		int i, newmode = 0;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 		for (i = 0; mapping[i] != 0; i++)
16597c478bd9Sstevel@tonic-gate 			if (mode & (1<<i))
16607c478bd9Sstevel@tonic-gate 				newmode |= mapping[i];
16617c478bd9Sstevel@tonic-gate 		mode = newmode;
16627c478bd9Sstevel@tonic-gate 	}
16637c478bd9Sstevel@tonic-gate #endif
16647c478bd9Sstevel@tonic-gate 		pp->p_who = P_A;
16657c478bd9Sstevel@tonic-gate 		pp->p_perm = mode;
16667c478bd9Sstevel@tonic-gate 		pp->p_op = '=';
16677c478bd9Sstevel@tonic-gate 	} else	for (;;) {
16687c478bd9Sstevel@tonic-gate 		int t;
16697c478bd9Sstevel@tonic-gate 		int who = 0;
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 		while ((t = iswho(*amode)) != 0) {
16727c478bd9Sstevel@tonic-gate 			++amode;
16737c478bd9Sstevel@tonic-gate 			who |= t;
16747c478bd9Sstevel@tonic-gate 		}
16757c478bd9Sstevel@tonic-gate 		if (who == 0) {
16767c478bd9Sstevel@tonic-gate 			mode_t currmask;
16777c478bd9Sstevel@tonic-gate 			(void) umask(currmask = umask((mode_t)0));
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 			/*
16807c478bd9Sstevel@tonic-gate 			 * If no who specified, must use contents of
16817c478bd9Sstevel@tonic-gate 			 * umask to determine which bits to flip.  This
16827c478bd9Sstevel@tonic-gate 			 * is POSIX/V7/BSD behaviour, but not SVID.
16837c478bd9Sstevel@tonic-gate 			 */
16847c478bd9Sstevel@tonic-gate 			who = (~currmask)&P_A;
16857c478bd9Sstevel@tonic-gate 			++nowho;
16867c478bd9Sstevel@tonic-gate 		} else
16877c478bd9Sstevel@tonic-gate 			nowho = 0;
16887c478bd9Sstevel@tonic-gate 	samewho:
16897c478bd9Sstevel@tonic-gate 		if (!isop(pp->p_op = *amode++))
16907c478bd9Sstevel@tonic-gate 			return (-1);
16917c478bd9Sstevel@tonic-gate 		pp->p_perm = 0;
16927c478bd9Sstevel@tonic-gate 		pp->p_special = 0;
16937c478bd9Sstevel@tonic-gate 		while ((t = isperm(pp, *amode)) != 0) {
16947c478bd9Sstevel@tonic-gate 			if (pp->p_special == 'X') {
16957c478bd9Sstevel@tonic-gate 				seen_X = 1;
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 				if (pp->p_perm != 0) {
16987c478bd9Sstevel@tonic-gate 					ushort_t op;
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 					/*
17017c478bd9Sstevel@tonic-gate 					 * Remember the 'who' for the previous
17027c478bd9Sstevel@tonic-gate 					 * transformation.
17037c478bd9Sstevel@tonic-gate 					 */
17047c478bd9Sstevel@tonic-gate 					pp->p_who = who;
17057c478bd9Sstevel@tonic-gate 					pp->p_special = 0;
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 					op = pp->p_op;
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 					/* Keep 'X' separate */
17107c478bd9Sstevel@tonic-gate 					++pp;
17117c478bd9Sstevel@tonic-gate 					pp->p_special = 'X';
17127c478bd9Sstevel@tonic-gate 					pp->p_op = op;
17137c478bd9Sstevel@tonic-gate 				}
17147c478bd9Sstevel@tonic-gate 			} else if (seen_X) {
17157c478bd9Sstevel@tonic-gate 				ushort_t op;
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 				/* Remember the 'who' for the X */
17187c478bd9Sstevel@tonic-gate 				pp->p_who = who;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 				op = pp->p_op;
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 				/* Keep 'X' separate */
17237c478bd9Sstevel@tonic-gate 				++pp;
17247c478bd9Sstevel@tonic-gate 				pp->p_perm = 0;
17257c478bd9Sstevel@tonic-gate 				pp->p_special = 0;
17267c478bd9Sstevel@tonic-gate 				pp->p_op = op;
17277c478bd9Sstevel@tonic-gate 			}
17287c478bd9Sstevel@tonic-gate 			++amode;
17297c478bd9Sstevel@tonic-gate 			pp->p_perm |= t;
17307c478bd9Sstevel@tonic-gate 		}
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 		/*
17337c478bd9Sstevel@tonic-gate 		 * These returned 0, but were actually parsed, so
17347c478bd9Sstevel@tonic-gate 		 * don't look at them again.
17357c478bd9Sstevel@tonic-gate 		 */
17367c478bd9Sstevel@tonic-gate 		switch (pp->p_special) {
17377c478bd9Sstevel@tonic-gate 		case 'u':
17387c478bd9Sstevel@tonic-gate 		case 'g':
17397c478bd9Sstevel@tonic-gate 		case 'o':
17407c478bd9Sstevel@tonic-gate 			++amode;
17417c478bd9Sstevel@tonic-gate 			break;
17427c478bd9Sstevel@tonic-gate 		}
17437c478bd9Sstevel@tonic-gate 		pp->p_who = who;
17447c478bd9Sstevel@tonic-gate 		switch (*amode) {
17457c478bd9Sstevel@tonic-gate 		case '\0':
17467c478bd9Sstevel@tonic-gate 			break;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 		case ',':
17497c478bd9Sstevel@tonic-gate 			++amode;
17507c478bd9Sstevel@tonic-gate 			++pp;
17517c478bd9Sstevel@tonic-gate 			continue;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 		default:
17547c478bd9Sstevel@tonic-gate 			++pp;
17557c478bd9Sstevel@tonic-gate 			goto samewho;
17567c478bd9Sstevel@tonic-gate 		}
17577c478bd9Sstevel@tonic-gate 		break;
17587c478bd9Sstevel@tonic-gate 	}
17597c478bd9Sstevel@tonic-gate 	endp = pp;
17607c478bd9Sstevel@tonic-gate 	return (NULL);
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate /*
17647c478bd9Sstevel@tonic-gate  * Given a character from the mode, return the associated
17657c478bd9Sstevel@tonic-gate  * value as who (user designation) mask or 0 if this isn't valid.
17667c478bd9Sstevel@tonic-gate  */
17677c478bd9Sstevel@tonic-gate static int
17687c478bd9Sstevel@tonic-gate iswho(c)
17697c478bd9Sstevel@tonic-gate int c;
17707c478bd9Sstevel@tonic-gate {
17717c478bd9Sstevel@tonic-gate 	switch (c) {
17727c478bd9Sstevel@tonic-gate 	case 'a':
17737c478bd9Sstevel@tonic-gate 		return (P_A);
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	case 'u':
17767c478bd9Sstevel@tonic-gate 		return (P_U);
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	case 'g':
17797c478bd9Sstevel@tonic-gate 		return (P_G);
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	case 'o':
17827c478bd9Sstevel@tonic-gate 		return (P_O);
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	default:
17857c478bd9Sstevel@tonic-gate 		return (0);
17867c478bd9Sstevel@tonic-gate 	}
17877c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
17887c478bd9Sstevel@tonic-gate }
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate /*
17917c478bd9Sstevel@tonic-gate  * Return non-zero if this is a valid op code
17927c478bd9Sstevel@tonic-gate  * in a symbolic mode.
17937c478bd9Sstevel@tonic-gate  */
17947c478bd9Sstevel@tonic-gate static int
17957c478bd9Sstevel@tonic-gate isop(c)
17967c478bd9Sstevel@tonic-gate int c;
17977c478bd9Sstevel@tonic-gate {
17987c478bd9Sstevel@tonic-gate 	switch (c) {
17997c478bd9Sstevel@tonic-gate 	case '+':
18007c478bd9Sstevel@tonic-gate 	case '-':
18017c478bd9Sstevel@tonic-gate 	case '=':
18027c478bd9Sstevel@tonic-gate 		return (1);
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	default:
18057c478bd9Sstevel@tonic-gate 		return (0);
18067c478bd9Sstevel@tonic-gate 	}
18077c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
18087c478bd9Sstevel@tonic-gate }
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate /*
18117c478bd9Sstevel@tonic-gate  * Return the permission bits implied by this character or 0
18127c478bd9Sstevel@tonic-gate  * if it isn't valid.  Also returns 0 when the pseudo-permissions 'u', 'g', or
18137c478bd9Sstevel@tonic-gate  * 'o' are used, and sets pp->p_special to the one used.
18147c478bd9Sstevel@tonic-gate  */
18157c478bd9Sstevel@tonic-gate static int
18167c478bd9Sstevel@tonic-gate isperm(pp, c)
18177c478bd9Sstevel@tonic-gate PERMST *pp;
18187c478bd9Sstevel@tonic-gate int c;
18197c478bd9Sstevel@tonic-gate {
18207c478bd9Sstevel@tonic-gate 	switch (c) {
18217c478bd9Sstevel@tonic-gate 	case 'u':
18227c478bd9Sstevel@tonic-gate 	case 'g':
18237c478bd9Sstevel@tonic-gate 	case 'o':
18247c478bd9Sstevel@tonic-gate 		pp->p_special = c;
18257c478bd9Sstevel@tonic-gate 		return (0);
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	case 'r':
18287c478bd9Sstevel@tonic-gate 		return (S_IRUSR|S_IRGRP|S_IROTH);
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	case 'w':
18317c478bd9Sstevel@tonic-gate 		return (S_IWUSR|S_IWGRP|S_IWOTH);
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	case 'x':
18347c478bd9Sstevel@tonic-gate 		return (S_IXUSR|S_IXGRP|S_IXOTH);
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate #if S_ISVTX != 0
18377c478bd9Sstevel@tonic-gate 	case 't':
18387c478bd9Sstevel@tonic-gate 		return (S_ISVTX);
18397c478bd9Sstevel@tonic-gate #endif
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	case 'X':
18427c478bd9Sstevel@tonic-gate 		pp->p_special = 'X';
18437c478bd9Sstevel@tonic-gate 		return (S_IXUSR|S_IXGRP|S_IXOTH);
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate #if S_ISVTX != 0
18467c478bd9Sstevel@tonic-gate 	case 'a':
18477c478bd9Sstevel@tonic-gate 		return (S_ISVTX);
18487c478bd9Sstevel@tonic-gate #endif
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	case 'h':
18517c478bd9Sstevel@tonic-gate 		return (S_ISUID);
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	/*
18547c478bd9Sstevel@tonic-gate 	 * This change makes:
18557c478bd9Sstevel@tonic-gate 	 *	chmod +s file
18567c478bd9Sstevel@tonic-gate 	 * set the system bit on dos but means that
18577c478bd9Sstevel@tonic-gate 	 *	chmod u+s file
18587c478bd9Sstevel@tonic-gate 	 *	chmod g+s file
18597c478bd9Sstevel@tonic-gate 	 *	chmod a+s file
18607c478bd9Sstevel@tonic-gate 	 * are all like UNIX.
18617c478bd9Sstevel@tonic-gate 	 */
18627c478bd9Sstevel@tonic-gate 	case 's':
18637c478bd9Sstevel@tonic-gate 		return (nowho ? S_ISGID : S_ISGID|S_ISUID);
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	default:
18667c478bd9Sstevel@tonic-gate 		return (0);
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
18697c478bd9Sstevel@tonic-gate }
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate /*
18727c478bd9Sstevel@tonic-gate  * Execute the automaton that is created by readmode()
18737c478bd9Sstevel@tonic-gate  * to generate the final mode that will be used.  This
18747c478bd9Sstevel@tonic-gate  * code is passed a starting mode that is usually the original
18757c478bd9Sstevel@tonic-gate  * mode of the file being changed (or 0).  Note that this mode must contain
18767c478bd9Sstevel@tonic-gate  * the file-type bits as well, so that S_ISDIR will succeed on directories.
18777c478bd9Sstevel@tonic-gate  */
18787c478bd9Sstevel@tonic-gate static mode_t
18797c478bd9Sstevel@tonic-gate getmode(mode_t startmode)
18807c478bd9Sstevel@tonic-gate {
18817c478bd9Sstevel@tonic-gate 	PERMST *pp;
18827c478bd9Sstevel@tonic-gate 	mode_t temp;
18837c478bd9Sstevel@tonic-gate 	mode_t perm;
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	for (pp = &machine[0]; pp <= endp; ++pp) {
18867c478bd9Sstevel@tonic-gate 		perm = (mode_t)0;
18877c478bd9Sstevel@tonic-gate 		/*
18887c478bd9Sstevel@tonic-gate 		 * For the special modes 'u', 'g' and 'o', the named portion
18897c478bd9Sstevel@tonic-gate 		 * of the mode refers to after the previous clause has been
18907c478bd9Sstevel@tonic-gate 		 * processed, while the 'X' mode refers to the contents of the
18917c478bd9Sstevel@tonic-gate 		 * mode before any clauses have been processed.
18927c478bd9Sstevel@tonic-gate 		 *
18937c478bd9Sstevel@tonic-gate 		 * References: P1003.2/D11.2, Section 4.7.7,
18947c478bd9Sstevel@tonic-gate 		 *  lines 2568-2570, 2578-2583
18957c478bd9Sstevel@tonic-gate 		 */
18967c478bd9Sstevel@tonic-gate 		switch (pp->p_special) {
18977c478bd9Sstevel@tonic-gate 		case 'u':
18987c478bd9Sstevel@tonic-gate 			temp = startmode & S_IRWXU;
18997c478bd9Sstevel@tonic-gate 			if (temp & (S_IRUSR|S_IRGRP|S_IROTH))
19007c478bd9Sstevel@tonic-gate 				perm |= ((S_IRUSR|S_IRGRP|S_IROTH) &
19017c478bd9Sstevel@tonic-gate 				    pp->p_who);
19027c478bd9Sstevel@tonic-gate 			if (temp & (S_IWUSR|S_IWGRP|S_IWOTH))
19037c478bd9Sstevel@tonic-gate 				perm |= ((S_IWUSR|S_IWGRP|S_IWOTH) & pp->p_who);
19047c478bd9Sstevel@tonic-gate 			if (temp & (S_IXUSR|S_IXGRP|S_IXOTH))
19057c478bd9Sstevel@tonic-gate 				perm |= ((S_IXUSR|S_IXGRP|S_IXOTH) & pp->p_who);
19067c478bd9Sstevel@tonic-gate 			break;
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 		case 'g':
19097c478bd9Sstevel@tonic-gate 			temp = startmode & S_IRWXG;
19107c478bd9Sstevel@tonic-gate 			if (temp & (S_IRUSR|S_IRGRP|S_IROTH))
19117c478bd9Sstevel@tonic-gate 				perm |= ((S_IRUSR|S_IRGRP|S_IROTH) & pp->p_who);
19127c478bd9Sstevel@tonic-gate 			if (temp & (S_IWUSR|S_IWGRP|S_IWOTH))
19137c478bd9Sstevel@tonic-gate 				perm |= ((S_IWUSR|S_IWGRP|S_IWOTH) & pp->p_who);
19147c478bd9Sstevel@tonic-gate 			if (temp & (S_IXUSR|S_IXGRP|S_IXOTH))
19157c478bd9Sstevel@tonic-gate 				perm |= ((S_IXUSR|S_IXGRP|S_IXOTH) & pp->p_who);
19167c478bd9Sstevel@tonic-gate 			break;
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 		case 'o':
19197c478bd9Sstevel@tonic-gate 			temp = startmode & S_IRWXO;
19207c478bd9Sstevel@tonic-gate 			if (temp & (S_IRUSR|S_IRGRP|S_IROTH))
19217c478bd9Sstevel@tonic-gate 				perm |= ((S_IRUSR|S_IRGRP|S_IROTH) & pp->p_who);
19227c478bd9Sstevel@tonic-gate 			if (temp & (S_IWUSR|S_IWGRP|S_IWOTH))
19237c478bd9Sstevel@tonic-gate 				perm |= ((S_IWUSR|S_IWGRP|S_IWOTH) & pp->p_who);
19247c478bd9Sstevel@tonic-gate 			if (temp & (S_IXUSR|S_IXGRP|S_IXOTH))
19257c478bd9Sstevel@tonic-gate 				perm |= ((S_IXUSR|S_IXGRP|S_IXOTH) & pp->p_who);
19267c478bd9Sstevel@tonic-gate 			break;
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 		case 'X':
19297c478bd9Sstevel@tonic-gate 			perm = pp->p_perm;
19307c478bd9Sstevel@tonic-gate 			break;
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 		default:
19337c478bd9Sstevel@tonic-gate 			perm = pp->p_perm;
19347c478bd9Sstevel@tonic-gate 			break;
19357c478bd9Sstevel@tonic-gate 		}
19367c478bd9Sstevel@tonic-gate 		switch (pp->p_op) {
19377c478bd9Sstevel@tonic-gate 		case '-':
19387c478bd9Sstevel@tonic-gate 			startmode &= ~(perm & pp->p_who);
19397c478bd9Sstevel@tonic-gate 			break;
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 		case '=':
19427c478bd9Sstevel@tonic-gate 			startmode &= ~pp->p_who;
19437c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
19447c478bd9Sstevel@tonic-gate 		case '+':
19457c478bd9Sstevel@tonic-gate 			startmode |= (perm & pp->p_who);
19467c478bd9Sstevel@tonic-gate 			break;
19477c478bd9Sstevel@tonic-gate 		}
19487c478bd9Sstevel@tonic-gate 	}
19497c478bd9Sstevel@tonic-gate 	return (startmode);
19507c478bd9Sstevel@tonic-gate }
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate /*
19537c478bd9Sstevel@tonic-gate  * Returns the last component of a path name, unless it is
19547c478bd9Sstevel@tonic-gate  * an absolute path, in which case it returns the whole path
19557c478bd9Sstevel@tonic-gate  */
19567c478bd9Sstevel@tonic-gate static char
19577c478bd9Sstevel@tonic-gate *gettail(char *fname)
19587c478bd9Sstevel@tonic-gate {
19597c478bd9Sstevel@tonic-gate 	char	*base = fname;
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	if (*fname != '/') {
19627c478bd9Sstevel@tonic-gate 		if ((base = strrchr(fname, '/')) != NULL)
19637c478bd9Sstevel@tonic-gate 			base++;
19647c478bd9Sstevel@tonic-gate 		else
19657c478bd9Sstevel@tonic-gate 			base = fname;
19667c478bd9Sstevel@tonic-gate 	}
19677c478bd9Sstevel@tonic-gate 	return (base);
19687c478bd9Sstevel@tonic-gate }
1969