xref: /onnv-gate/usr/src/cmd/find/find.c (revision 12685:2d7ff21e61eb)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51506Srm88369  * Common Development and Distribution License (the "License").
61506Srm88369  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212543Srich.burridge@oracle.com  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
2551Sjonb 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*	Parts of this product may be derived from		*/
310Sstevel@tonic-gate /*	Mortice Kern Systems Inc. and Berkeley 4.3 BSD systems.	*/
320Sstevel@tonic-gate /*	licensed from  Mortice Kern Systems Inc. and 		*/
330Sstevel@tonic-gate /*	the University of California.				*/
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate  * Copyright 1985, 1990 by Mortice Kern Systems Inc.  All rights reserved.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <pwd.h>
420Sstevel@tonic-gate #include <grp.h>
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate #include <sys/param.h>
460Sstevel@tonic-gate #include <sys/acl.h>
470Sstevel@tonic-gate #include <limits.h>
480Sstevel@tonic-gate #include <unistd.h>
490Sstevel@tonic-gate #include <stdlib.h>
500Sstevel@tonic-gate #include <locale.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate #include <strings.h>
530Sstevel@tonic-gate #include <ctype.h>
540Sstevel@tonic-gate #include <wait.h>
550Sstevel@tonic-gate #include <fnmatch.h>
560Sstevel@tonic-gate #include <langinfo.h>
570Sstevel@tonic-gate #include <ftw.h>
586584Schin #include <libgen.h>
594774Sas145665 #include "getresponse.h"
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	A_DAY		(long)(60*60*24)	/* a day full of seconds */
6251Sjonb #define	A_MIN		(long)(60)
630Sstevel@tonic-gate #define	BLKSIZ		512
640Sstevel@tonic-gate #define	round(x, s)	(((x)+(s)-1)&~((s)-1))
650Sstevel@tonic-gate #ifndef FTW_SLN
660Sstevel@tonic-gate #define	FTW_SLN		7
670Sstevel@tonic-gate #endif
680Sstevel@tonic-gate #define	LINEBUF_SIZE		LINE_MAX	/* input or output lines */
690Sstevel@tonic-gate #define	REMOTE_FS		"/etc/dfs/fstypes"
700Sstevel@tonic-gate #define	N_FSTYPES		20
711506Srm88369 #define	SHELL_MAXARGS		253	/* see doexec() for description */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * This is the list of operations
750Sstevel@tonic-gate  * F_USER and F_GROUP are named to avoid conflict with USER and GROUP defined
760Sstevel@tonic-gate  * in sys/acl.h
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate enum Command
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	PRINT, DEPTH, LOCAL, MOUNT, ATIME, MTIME, CTIME, NEWER,
820Sstevel@tonic-gate 	NAME, F_USER, F_GROUP, INUM, SIZE, LINKS, PERM, EXEC, OK, CPIO, NCPIO,
830Sstevel@tonic-gate 	TYPE, AND, OR, NOT, LPAREN, RPAREN, CSIZE, VARARGS, FOLLOW,
8451Sjonb 	PRUNE, NOUSER, NOGRP, FSTYPE, LS, XATTR, ACL, MMIN, AMIN, CMIN
850Sstevel@tonic-gate };
860Sstevel@tonic-gate 
870Sstevel@tonic-gate enum Type
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	Unary, Id, Num, Str, Exec, Cpio, Op
900Sstevel@tonic-gate };
910Sstevel@tonic-gate 
920Sstevel@tonic-gate struct Args
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	char		name[10];
950Sstevel@tonic-gate 	enum Command	action;
960Sstevel@tonic-gate 	enum Type	type;
970Sstevel@tonic-gate };
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * Except for pathnames, these are the only legal arguments
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate static struct Args commands[] =
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	"!",		NOT,	Op,
1050Sstevel@tonic-gate 	"(",		LPAREN,	Unary,
1060Sstevel@tonic-gate 	")",		RPAREN,	Unary,
1070Sstevel@tonic-gate 	"-a",		AND,	Op,
10851Sjonb 	"-amin",	AMIN,	Num,
1090Sstevel@tonic-gate 	"-atime",	ATIME,	Num,
1100Sstevel@tonic-gate 	"-cpio",	CPIO,	Cpio,
11151Sjonb 	"-cmin",	CMIN,	Num,
1120Sstevel@tonic-gate 	"-ctime",	CTIME,	Num,
1130Sstevel@tonic-gate 	"-depth",	DEPTH,	Unary,
1140Sstevel@tonic-gate 	"-exec",	EXEC,	Exec,
1150Sstevel@tonic-gate 	"-follow",	FOLLOW, Unary,
1160Sstevel@tonic-gate 	"-group",	F_GROUP,	Num,
1170Sstevel@tonic-gate 	"-inum",	INUM,	Num,
1180Sstevel@tonic-gate 	"-links",	LINKS,	Num,
1190Sstevel@tonic-gate 	"-local",	LOCAL,	Unary,
1200Sstevel@tonic-gate 	"-mount",	MOUNT,	Unary,
12151Sjonb 	"-mmin",	MMIN,	Num,
1220Sstevel@tonic-gate 	"-mtime",	MTIME,	Num,
1230Sstevel@tonic-gate 	"-name",	NAME,	Str,
1240Sstevel@tonic-gate 	"-ncpio",	NCPIO,  Cpio,
1250Sstevel@tonic-gate 	"-newer",	NEWER,	Str,
1260Sstevel@tonic-gate 	"-o",		OR,	Op,
1270Sstevel@tonic-gate 	"-ok",		OK,	Exec,
1280Sstevel@tonic-gate 	"-perm",	PERM,	Num,
1290Sstevel@tonic-gate 	"-print",	PRINT,	Unary,
1300Sstevel@tonic-gate 	"-size",	SIZE,	Num,
1310Sstevel@tonic-gate 	"-type",	TYPE,	Num,
1320Sstevel@tonic-gate 	"-xdev",	MOUNT,	Unary,
1330Sstevel@tonic-gate 	"-user",	F_USER,	Num,
1340Sstevel@tonic-gate 	"-prune",	PRUNE,	Unary,
1350Sstevel@tonic-gate 	"-nouser",	NOUSER,	Unary,
1360Sstevel@tonic-gate 	"-nogroup",	NOGRP,	Unary,
1370Sstevel@tonic-gate 	"-fstype",	FSTYPE,	Str,
1380Sstevel@tonic-gate 	"-ls",		LS,	Unary,
1390Sstevel@tonic-gate 	"-xattr",	XATTR,	Unary,
1400Sstevel@tonic-gate 	"-acl",		ACL,	Unary,
1410Sstevel@tonic-gate 	NULL,		0,	0
1420Sstevel@tonic-gate };
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate union Item
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	struct Node	*np;
1470Sstevel@tonic-gate 	struct Arglist	*vp;
1480Sstevel@tonic-gate 	time_t		t;
1490Sstevel@tonic-gate 	char		*cp;
1500Sstevel@tonic-gate 	char		**ap;
1510Sstevel@tonic-gate 	long		l;
1520Sstevel@tonic-gate 	int		i;
1530Sstevel@tonic-gate 	long long	ll;
1540Sstevel@tonic-gate };
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate struct Node
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	struct Node	*next;
1590Sstevel@tonic-gate 	enum Command	action;
1600Sstevel@tonic-gate 	enum Type	type;
1610Sstevel@tonic-gate 	union Item	first;
1620Sstevel@tonic-gate 	union Item	second;
1630Sstevel@tonic-gate };
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /* if no -print, -exec or -ok replace "expression" with "(expression) -print" */
1660Sstevel@tonic-gate static	struct	Node PRINT_NODE = { 0, PRINT, 0, 0};
1670Sstevel@tonic-gate static	struct	Node LPAREN_NODE = { 0, LPAREN, 0, 0};
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * Prototype variable size arglist buffer
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate struct Arglist
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	struct Arglist	*next;
1770Sstevel@tonic-gate 	char		*end;
1780Sstevel@tonic-gate 	char		*nextstr;
1790Sstevel@tonic-gate 	char		**firstvar;
1800Sstevel@tonic-gate 	char		**nextvar;
1810Sstevel@tonic-gate 	char		*arglist[1];
1820Sstevel@tonic-gate };
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate static int		compile();
1860Sstevel@tonic-gate static int		execute();
1871506Srm88369 static int		doexec(char *, char **, int *);
1880Sstevel@tonic-gate static struct Args	*lookup();
1890Sstevel@tonic-gate static int		ok();
19018Srobbin static void		usage(void)	__NORETURN;
1910Sstevel@tonic-gate static struct Arglist	*varargs();
1920Sstevel@tonic-gate static int		list();
1930Sstevel@tonic-gate static char		*getgroup();
1940Sstevel@tonic-gate static FILE		*cmdopen();
1950Sstevel@tonic-gate static int		cmdclose();
1960Sstevel@tonic-gate static char		*getshell();
1970Sstevel@tonic-gate static void 		init_remote_fs();
1980Sstevel@tonic-gate static char		*getname();
1990Sstevel@tonic-gate static int		readmode();
2000Sstevel@tonic-gate static mode_t		getmode();
2010Sstevel@tonic-gate static char		*gettail();
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 
2043523Scf46844 static int walkflags = FTW_CHDIR|FTW_PHYS|FTW_ANYERR|FTW_NOLOOP;
2050Sstevel@tonic-gate static struct Node	*topnode;
2060Sstevel@tonic-gate static struct Node	*freenode;	/* next free node we may use later */
2070Sstevel@tonic-gate static char		*cpio[] = { "cpio", "-o", 0 };
2080Sstevel@tonic-gate static char		*ncpio[] = { "cpio", "-oc", 0 };
2090Sstevel@tonic-gate static char		*cpiol[] = { "cpio", "-oL", 0 };
2100Sstevel@tonic-gate static char		*ncpiol[] = { "cpio", "-ocL", 0 };
2110Sstevel@tonic-gate static time_t		now;
2120Sstevel@tonic-gate static FILE		*output;
2130Sstevel@tonic-gate static char		*dummyarg = (char *)-1;
2140Sstevel@tonic-gate static int		lastval;
2150Sstevel@tonic-gate static int		varsize;
2160Sstevel@tonic-gate static struct Arglist	*lastlist;
2170Sstevel@tonic-gate static char		*cmdname;
2180Sstevel@tonic-gate static char		*remote_fstypes[N_FSTYPES+1];
2190Sstevel@tonic-gate static int		fstype_index = 0;
2200Sstevel@tonic-gate static int		action_expression = 0;	/* -print, -exec, or -ok */
2210Sstevel@tonic-gate static int		error = 0;
2220Sstevel@tonic-gate static int		paren_cnt = 0;	/* keeps track of parentheses */
2230Sstevel@tonic-gate static int		hflag = 0;
2240Sstevel@tonic-gate static int		lflag = 0;
2251506Srm88369 /* set when doexec()-invoked utility returns non-zero */
2261506Srm88369 static int		exec_exitcode = 0;
2270Sstevel@tonic-gate extern char		**environ;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate int
main(int argc,char ** argv)2300Sstevel@tonic-gate main(int argc, char **argv)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	char *cp;
2330Sstevel@tonic-gate 	int c;
2340Sstevel@tonic-gate 	int paths;
2350Sstevel@tonic-gate 	char *cwdpath;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2380Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2390Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2400Sstevel@tonic-gate #endif
2410Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	cmdname = argv[0];
2440Sstevel@tonic-gate 	if (time(&now) == (time_t)(-1)) {
2450Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: time() %s\n"),
2464774Sas145665 		    cmdname, strerror(errno));
2470Sstevel@tonic-gate 		exit(1);
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "HL")) != -1) {
2500Sstevel@tonic-gate 		switch (c) {
2510Sstevel@tonic-gate 		case 'H':
2520Sstevel@tonic-gate 			hflag = 1;
2530Sstevel@tonic-gate 			lflag = 0;
2540Sstevel@tonic-gate 			break;
2550Sstevel@tonic-gate 		case 'L':
2560Sstevel@tonic-gate 			hflag = 0;
2570Sstevel@tonic-gate 			lflag = 1;
2580Sstevel@tonic-gate 			break;
2590Sstevel@tonic-gate 		case '?':
2600Sstevel@tonic-gate 			usage();
2610Sstevel@tonic-gate 			break;
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	argc -= optind;
2660Sstevel@tonic-gate 	argv += optind;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if (argc < 1) {
2690Sstevel@tonic-gate 		(void) fprintf(stderr,
2700Sstevel@tonic-gate 		    gettext("%s: insufficient number of arguments\n"), cmdname);
2710Sstevel@tonic-gate 		usage();
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	for (paths = 0; (cp = argv[paths]) != 0; ++paths) {
2750Sstevel@tonic-gate 		if (*cp == '-')
2760Sstevel@tonic-gate 			break;
2770Sstevel@tonic-gate 		else if ((*cp == '!' || *cp == '(') && *(cp+1) == 0)
2780Sstevel@tonic-gate 			break;
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	if (paths == 0) /* no path-list */
2820Sstevel@tonic-gate 		usage();
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	output = stdout;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/* lflag is the same as -follow */
2870Sstevel@tonic-gate 	if (lflag)
2880Sstevel@tonic-gate 		walkflags &= ~FTW_PHYS;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/* allocate enough space for the compiler */
2910Sstevel@tonic-gate 	topnode = malloc((argc + 1) * sizeof (struct Node));
2920Sstevel@tonic-gate 	(void) memset(topnode, 0, (argc + 1) * sizeof (struct Node));
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	if (compile(argv + paths, topnode, &action_expression) == 0) {
2950Sstevel@tonic-gate 		/* no expression, default to -print */
2960Sstevel@tonic-gate 		(void) memcpy(topnode, &PRINT_NODE, sizeof (struct Node));
2970Sstevel@tonic-gate 	} else if (!action_expression) {
2980Sstevel@tonic-gate 		/*
2990Sstevel@tonic-gate 		 * if no action expression, insert an LPAREN node above topnode,
3000Sstevel@tonic-gate 		 * with a PRINT node as its next node
3010Sstevel@tonic-gate 		 */
3020Sstevel@tonic-gate 		struct Node *savenode;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 		if (freenode == NULL) {
3050Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: can't append -print"
3064774Sas145665 			    " implicitly; try explicit -print option\n"),
3074774Sas145665 			    cmdname);
3080Sstevel@tonic-gate 			exit(1);
3090Sstevel@tonic-gate 		}
3100Sstevel@tonic-gate 		savenode = topnode;
3110Sstevel@tonic-gate 		topnode = freenode++;
3120Sstevel@tonic-gate 		(void) memcpy(topnode, &LPAREN_NODE, sizeof (struct Node));
3130Sstevel@tonic-gate 		topnode->next = freenode;
3140Sstevel@tonic-gate 		topnode->first.np = savenode;
3150Sstevel@tonic-gate 		(void) memcpy(topnode->next, &PRINT_NODE, sizeof (struct Node));
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	while (paths--) {
3190Sstevel@tonic-gate 		char *curpath;
3200Sstevel@tonic-gate 		struct stat sb;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 		curpath = *(argv++);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 		/*
3250Sstevel@tonic-gate 		 * If -H is specified, it means we walk the first
3260Sstevel@tonic-gate 		 * level (pathname on command line) logically, following
3270Sstevel@tonic-gate 		 * symlinks, but lower levels are walked physically.
3280Sstevel@tonic-gate 		 * We use our own secret interface to nftw() to change
3290Sstevel@tonic-gate 		 * the from stat to lstat after the top level is walked.
3300Sstevel@tonic-gate 		 */
3310Sstevel@tonic-gate 		if (hflag) {
3320Sstevel@tonic-gate 			if (stat(curpath, &sb) < 0 && errno == ENOENT)
3330Sstevel@tonic-gate 				walkflags &= ~FTW_HOPTION;
3340Sstevel@tonic-gate 			else
3350Sstevel@tonic-gate 				walkflags |= FTW_HOPTION;
3360Sstevel@tonic-gate 		}
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 		/*
3390Sstevel@tonic-gate 		 * We need this check as nftw needs a CWD and we have no
3400Sstevel@tonic-gate 		 * way of returning back from that code with a meaningful
3410Sstevel@tonic-gate 		 * error related to this
3420Sstevel@tonic-gate 		 */
3430Sstevel@tonic-gate 		if ((cwdpath = getcwd(NULL, PATH_MAX)) == NULL) {
34412577Srich.burridge@oracle.com 			if ((errno == EACCES) && (walkflags & FTW_CHDIR)) {
34512577Srich.burridge@oracle.com 				/*
34612577Srich.burridge@oracle.com 				 * A directory above cwd is inaccessible,
34712577Srich.burridge@oracle.com 				 * so don't do chdir(2)s. Slower, but at least
34812577Srich.burridge@oracle.com 				 * it works.
34912577Srich.burridge@oracle.com 				 */
35012577Srich.burridge@oracle.com 				walkflags &= ~FTW_CHDIR;
35112577Srich.burridge@oracle.com 				free(cwdpath);
35212577Srich.burridge@oracle.com 			} else {
35312577Srich.burridge@oracle.com 				(void) fprintf(stderr,
35412577Srich.burridge@oracle.com 				    gettext("%s : cannot get the current "
35512577Srich.burridge@oracle.com 				    "working directory\n"), cmdname);
35612577Srich.burridge@oracle.com 				exit(1);
35712577Srich.burridge@oracle.com 			}
3580Sstevel@tonic-gate 		} else
3590Sstevel@tonic-gate 			free(cwdpath);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 		if (nftw(curpath, execute, 1000, walkflags)) {
3630Sstevel@tonic-gate 			(void) fprintf(stderr,
3640Sstevel@tonic-gate 			    gettext("%s: cannot open %s: %s\n"),
3650Sstevel@tonic-gate 			    cmdname, curpath, strerror(errno));
3660Sstevel@tonic-gate 			error = 1;
3670Sstevel@tonic-gate 		}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	/* execute any remaining variable length lists */
3720Sstevel@tonic-gate 	while (lastlist) {
3730Sstevel@tonic-gate 		if (lastlist->end != lastlist->nextstr) {
3740Sstevel@tonic-gate 			*lastlist->nextvar = 0;
3751506Srm88369 			(void) doexec((char *)0, lastlist->arglist,
3761506Srm88369 			    &exec_exitcode);
3770Sstevel@tonic-gate 		}
3780Sstevel@tonic-gate 		lastlist = lastlist->next;
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 	if (output != stdout)
3810Sstevel@tonic-gate 		return (cmdclose(output));
3821506Srm88369 	return ((exec_exitcode != 0) ? exec_exitcode : error);
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate /*
3860Sstevel@tonic-gate  * compile the arguments
3870Sstevel@tonic-gate  */
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate static int
compile(argv,np,actionp)3900Sstevel@tonic-gate compile(argv, np, actionp)
3910Sstevel@tonic-gate char **argv;
3920Sstevel@tonic-gate struct Node *np;
3930Sstevel@tonic-gate int *actionp;
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate 	char *b;
3960Sstevel@tonic-gate 	char **av;
3970Sstevel@tonic-gate 	struct Node *oldnp = topnode;
3980Sstevel@tonic-gate 	struct Args *argp;
3990Sstevel@tonic-gate 	char **com;
4000Sstevel@tonic-gate 	int i;
4010Sstevel@tonic-gate 	enum Command wasop = PRINT;
4020Sstevel@tonic-gate 
4034774Sas145665 	if (init_yes() < 0) {
4044774Sas145665 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
4054774Sas145665 		    strerror(errno));
4064774Sas145665 		exit(1);
4074774Sas145665 	}
4084774Sas145665 
4090Sstevel@tonic-gate 	for (av = argv; *av && (argp = lookup(*av)); av++) {
4100Sstevel@tonic-gate 		np->next = 0;
4110Sstevel@tonic-gate 		np->action = argp->action;
4120Sstevel@tonic-gate 		np->type = argp->type;
4130Sstevel@tonic-gate 		np->second.i = 0;
4140Sstevel@tonic-gate 		if (argp->type == Op) {
4150Sstevel@tonic-gate 			if (wasop == NOT || (wasop && np->action != NOT)) {
4160Sstevel@tonic-gate 				(void) fprintf(stderr,
4170Sstevel@tonic-gate 				gettext("%s: operand follows operand\n"),
4180Sstevel@tonic-gate 						cmdname);
4190Sstevel@tonic-gate 				exit(1);
4200Sstevel@tonic-gate 			}
4210Sstevel@tonic-gate 			if (np->action != NOT && oldnp == 0)
4220Sstevel@tonic-gate 				goto err;
4230Sstevel@tonic-gate 			wasop = argp->action;
4240Sstevel@tonic-gate 		} else {
4250Sstevel@tonic-gate 			wasop = PRINT;
4260Sstevel@tonic-gate 			if (argp->type != Unary) {
4270Sstevel@tonic-gate 				if (!(b = *++av)) {
4280Sstevel@tonic-gate 					(void) fprintf(stderr,
4290Sstevel@tonic-gate 					gettext("%s: incomplete statement\n"),
4300Sstevel@tonic-gate 							cmdname);
4310Sstevel@tonic-gate 					exit(1);
4320Sstevel@tonic-gate 				}
4330Sstevel@tonic-gate 				if (argp->type == Num) {
4340Sstevel@tonic-gate 					if ((argp->action != PERM) ||
4350Sstevel@tonic-gate 					    (*b != '+')) {
4360Sstevel@tonic-gate 						if (*b == '+' || *b == '-') {
4370Sstevel@tonic-gate 							np->second.i = *b;
4380Sstevel@tonic-gate 							b++;
4390Sstevel@tonic-gate 						}
4400Sstevel@tonic-gate 					}
4410Sstevel@tonic-gate 				}
4420Sstevel@tonic-gate 			}
4430Sstevel@tonic-gate 		}
4440Sstevel@tonic-gate 		switch (argp->action) {
4450Sstevel@tonic-gate 		case AND:
4460Sstevel@tonic-gate 			break;
4470Sstevel@tonic-gate 		case NOT:
4480Sstevel@tonic-gate 			break;
4490Sstevel@tonic-gate 		case OR:
4500Sstevel@tonic-gate 			np->first.np = topnode;
4510Sstevel@tonic-gate 			topnode = np;
4520Sstevel@tonic-gate 			oldnp->next = 0;
4530Sstevel@tonic-gate 			break;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		case LPAREN: {
4560Sstevel@tonic-gate 			struct Node *save = topnode;
4570Sstevel@tonic-gate 			topnode = np+1;
4580Sstevel@tonic-gate 			paren_cnt++;
4590Sstevel@tonic-gate 			i = compile(++av, topnode, actionp);
4600Sstevel@tonic-gate 			np->first.np = topnode;
4610Sstevel@tonic-gate 			topnode = save;
4620Sstevel@tonic-gate 			av += i;
4630Sstevel@tonic-gate 			oldnp = np;
4640Sstevel@tonic-gate 			np += i + 1;
4650Sstevel@tonic-gate 			oldnp->next = np;
4660Sstevel@tonic-gate 			continue;
4670Sstevel@tonic-gate 		}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 		case RPAREN:
4700Sstevel@tonic-gate 			if (paren_cnt <= 0) {
4710Sstevel@tonic-gate 				(void) fprintf(stderr,
4720Sstevel@tonic-gate 				    gettext("%s: unmatched ')'\n"),
4730Sstevel@tonic-gate 				    cmdname);
4740Sstevel@tonic-gate 				exit(1);
4750Sstevel@tonic-gate 			}
4760Sstevel@tonic-gate 			paren_cnt--;
4770Sstevel@tonic-gate 			if (oldnp == 0)
4780Sstevel@tonic-gate 				goto err;
4790Sstevel@tonic-gate 			if (oldnp->type == Op) {
4800Sstevel@tonic-gate 				(void) fprintf(stderr,
4810Sstevel@tonic-gate 				    gettext("%s: cannot immediately"
4820Sstevel@tonic-gate 				    " follow an operand with ')'\n"),
4830Sstevel@tonic-gate 				    cmdname);
4840Sstevel@tonic-gate 				exit(1);
4850Sstevel@tonic-gate 			}
4860Sstevel@tonic-gate 			oldnp->next = 0;
4870Sstevel@tonic-gate 			return (av-argv);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 		case FOLLOW:
4900Sstevel@tonic-gate 			walkflags &= ~FTW_PHYS;
4910Sstevel@tonic-gate 			break;
4920Sstevel@tonic-gate 		case MOUNT:
4930Sstevel@tonic-gate 			walkflags |= FTW_MOUNT;
4940Sstevel@tonic-gate 			break;
4950Sstevel@tonic-gate 		case DEPTH:
4960Sstevel@tonic-gate 			walkflags |= FTW_DEPTH;
4970Sstevel@tonic-gate 			break;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 		case LOCAL:
5000Sstevel@tonic-gate 			np->first.l = 0L;
5010Sstevel@tonic-gate 			np->first.ll = 0LL;
5020Sstevel@tonic-gate 			np->second.i = '+';
5030Sstevel@tonic-gate 			/*
5040Sstevel@tonic-gate 			 * Make it compatible to df -l for
5050Sstevel@tonic-gate 			 * future enhancement. So, anything
5060Sstevel@tonic-gate 			 * that is not remote, then it is
5070Sstevel@tonic-gate 			 * local.
5080Sstevel@tonic-gate 			 */
5090Sstevel@tonic-gate 			init_remote_fs();
5100Sstevel@tonic-gate 			break;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 		case SIZE:
5130Sstevel@tonic-gate 			if (b[strlen(b)-1] == 'c')
5140Sstevel@tonic-gate 				np->action = CSIZE;
5150Sstevel@tonic-gate 			/*FALLTHROUGH*/
5160Sstevel@tonic-gate 		case INUM:
5170Sstevel@tonic-gate 			np->first.ll = atoll(b);
5180Sstevel@tonic-gate 			break;
5190Sstevel@tonic-gate 
52051Sjonb 		case CMIN:
5210Sstevel@tonic-gate 		case CTIME:
52251Sjonb 		case MMIN:
5230Sstevel@tonic-gate 		case MTIME:
52451Sjonb 		case AMIN:
5250Sstevel@tonic-gate 		case ATIME:
5260Sstevel@tonic-gate 		case LINKS:
5270Sstevel@tonic-gate 			np->first.l = atol(b);
5280Sstevel@tonic-gate 			break;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 		case F_USER:
5310Sstevel@tonic-gate 		case F_GROUP: {
5320Sstevel@tonic-gate 			struct	passwd	*pw;
5330Sstevel@tonic-gate 			struct	group *gr;
53412543Srich.burridge@oracle.com 			long value;
53512543Srich.burridge@oracle.com 			char *q;
53612543Srich.burridge@oracle.com 
53712543Srich.burridge@oracle.com 			value = -1;
5380Sstevel@tonic-gate 			if (argp->action == F_USER) {
5390Sstevel@tonic-gate 				if ((pw = getpwnam(b)) != 0)
54012543Srich.burridge@oracle.com 					value = (long)pw->pw_uid;
5410Sstevel@tonic-gate 			} else {
5420Sstevel@tonic-gate 				if ((gr = getgrnam(b)) != 0)
54312543Srich.burridge@oracle.com 					value = (long)gr->gr_gid;
5440Sstevel@tonic-gate 			}
54512543Srich.burridge@oracle.com 			if (value == -1) {
54612543Srich.burridge@oracle.com 				errno = 0;
54712543Srich.burridge@oracle.com 				value = strtol(b, &q, 10);
54812543Srich.burridge@oracle.com 				if (errno != 0 || q == b || *q != '\0') {
5490Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
5500Sstevel@tonic-gate 					    "%s: cannot find %s name\n"),
5510Sstevel@tonic-gate 						cmdname, *av);
5520Sstevel@tonic-gate 					exit(1);
5530Sstevel@tonic-gate 				}
5540Sstevel@tonic-gate 			}
55512543Srich.burridge@oracle.com 			np->first.l = value;
5560Sstevel@tonic-gate 			break;
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 		case EXEC:
5600Sstevel@tonic-gate 		case OK:
5610Sstevel@tonic-gate 			walkflags &= ~FTW_CHDIR;
5620Sstevel@tonic-gate 			np->first.ap = av;
5630Sstevel@tonic-gate 			(*actionp)++;
5640Sstevel@tonic-gate 			for (;;) {
5650Sstevel@tonic-gate 				if ((b = *av) == 0) {
5660Sstevel@tonic-gate 					(void) fprintf(stderr,
5670Sstevel@tonic-gate 					gettext("%s: incomplete statement\n"),
5680Sstevel@tonic-gate 						cmdname);
5690Sstevel@tonic-gate 					exit(1);
5700Sstevel@tonic-gate 				}
5710Sstevel@tonic-gate 				if (strcmp(b, ";") == 0) {
5720Sstevel@tonic-gate 					*av = 0;
5730Sstevel@tonic-gate 					break;
5740Sstevel@tonic-gate 				} else if (strcmp(b, "{}") == 0)
5750Sstevel@tonic-gate 					*av = dummyarg;
5760Sstevel@tonic-gate 				else if (strcmp(b, "+") == 0 &&
5770Sstevel@tonic-gate 					av[-1] == dummyarg &&
5780Sstevel@tonic-gate 					np->action == EXEC) {
5790Sstevel@tonic-gate 					av[-1] = 0;
5800Sstevel@tonic-gate 					np->first.vp = varargs(np->first.ap);
5810Sstevel@tonic-gate 					np->action = VARARGS;
5820Sstevel@tonic-gate 					break;
5830Sstevel@tonic-gate 				}
5840Sstevel@tonic-gate 				av++;
5850Sstevel@tonic-gate 			}
5860Sstevel@tonic-gate 			break;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 		case NAME:
5890Sstevel@tonic-gate 			np->first.cp = b;
5900Sstevel@tonic-gate 			break;
5910Sstevel@tonic-gate 		case PERM:
5920Sstevel@tonic-gate 			if (*b == '-')
5930Sstevel@tonic-gate 				++b;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 			if (readmode(b) != NULL) {
5960Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
5970Sstevel@tonic-gate 				    "find: -perm: Bad permission string\n"));
5980Sstevel@tonic-gate 				usage();
5990Sstevel@tonic-gate 			}
6000Sstevel@tonic-gate 			np->first.l = (long)getmode((mode_t)0);
6010Sstevel@tonic-gate 			break;
6020Sstevel@tonic-gate 		case TYPE:
6030Sstevel@tonic-gate 			i = *b;
6040Sstevel@tonic-gate 			np->first.l =
6050Sstevel@tonic-gate 			    i == 'd' ? S_IFDIR :
6060Sstevel@tonic-gate 			    i == 'b' ? S_IFBLK :
6070Sstevel@tonic-gate 			    i == 'c' ? S_IFCHR :
6080Sstevel@tonic-gate #ifdef S_IFIFO
6090Sstevel@tonic-gate 			    i == 'p' ? S_IFIFO :
6100Sstevel@tonic-gate #endif
6110Sstevel@tonic-gate 			    i == 'f' ? S_IFREG :
6120Sstevel@tonic-gate #ifdef S_IFLNK
6130Sstevel@tonic-gate 			    i == 'l' ? S_IFLNK :
6140Sstevel@tonic-gate #endif
6150Sstevel@tonic-gate #ifdef S_IFSOCK
6160Sstevel@tonic-gate 			    i == 's' ? S_IFSOCK :
6170Sstevel@tonic-gate #endif
6180Sstevel@tonic-gate #ifdef S_IFDOOR
6190Sstevel@tonic-gate 			    i == 'D' ? S_IFDOOR :
6200Sstevel@tonic-gate #endif
6210Sstevel@tonic-gate 			    0;
6220Sstevel@tonic-gate 			break;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		case CPIO:
6250Sstevel@tonic-gate 			if (walkflags & FTW_PHYS)
6260Sstevel@tonic-gate 				com = cpio;
6270Sstevel@tonic-gate 			else
6280Sstevel@tonic-gate 				com = cpiol;
6290Sstevel@tonic-gate 			goto common;
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		case NCPIO: {
6320Sstevel@tonic-gate 			FILE *fd;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 			if (walkflags & FTW_PHYS)
6350Sstevel@tonic-gate 				com = ncpio;
6360Sstevel@tonic-gate 			else
6370Sstevel@tonic-gate 				com = ncpiol;
6380Sstevel@tonic-gate 		common:
6390Sstevel@tonic-gate 			/* set up cpio */
6400Sstevel@tonic-gate 			if ((fd = fopen(b, "w")) == NULL) {
6410Sstevel@tonic-gate 				(void) fprintf(stderr,
6420Sstevel@tonic-gate 					gettext("%s: cannot create %s\n"),
6430Sstevel@tonic-gate 					cmdname, b);
6440Sstevel@tonic-gate 				exit(1);
6450Sstevel@tonic-gate 			}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 			np->first.l = (long)cmdopen("cpio", com, "w", fd);
6480Sstevel@tonic-gate 			(void) fclose(fd);
6490Sstevel@tonic-gate 			walkflags |= FTW_DEPTH;
6500Sstevel@tonic-gate 			np->action = CPIO;
6510Sstevel@tonic-gate 		}
6520Sstevel@tonic-gate 			/*FALLTHROUGH*/
6530Sstevel@tonic-gate 		case PRINT:
6540Sstevel@tonic-gate 			(*actionp)++;
6550Sstevel@tonic-gate 			break;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 		case NEWER: {
6580Sstevel@tonic-gate 			struct stat statb;
6590Sstevel@tonic-gate 			if (stat(b, &statb) < 0) {
6600Sstevel@tonic-gate 				(void) fprintf(stderr,
6610Sstevel@tonic-gate 					gettext("%s: cannot access %s\n"),
6620Sstevel@tonic-gate 					cmdname, b);
6630Sstevel@tonic-gate 				exit(1);
6640Sstevel@tonic-gate 			}
6650Sstevel@tonic-gate 			np->first.l = statb.st_mtime;
6660Sstevel@tonic-gate 			np->second.i = '+';
6670Sstevel@tonic-gate 			break;
6680Sstevel@tonic-gate 		}
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 		case PRUNE:
6710Sstevel@tonic-gate 		case NOUSER:
6720Sstevel@tonic-gate 		case NOGRP:
6730Sstevel@tonic-gate 			break;
6740Sstevel@tonic-gate 		case FSTYPE:
6750Sstevel@tonic-gate 			np->first.cp = b;
6760Sstevel@tonic-gate 			break;
6770Sstevel@tonic-gate 		case LS:
6780Sstevel@tonic-gate 			(*actionp)++;
6790Sstevel@tonic-gate 			break;
6800Sstevel@tonic-gate 		case XATTR:
6810Sstevel@tonic-gate 			break;
6820Sstevel@tonic-gate 		case ACL:
6830Sstevel@tonic-gate 			break;
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		oldnp = np++;
6870Sstevel@tonic-gate 		oldnp->next = np;
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	if ((*av) || (wasop))
6910Sstevel@tonic-gate 		goto err;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	if (paren_cnt != 0) {
6940Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: unmatched '('\n"),
6950Sstevel@tonic-gate 		cmdname);
6960Sstevel@tonic-gate 		exit(1);
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	/* just before returning, save next free node from the list */
7000Sstevel@tonic-gate 	freenode = oldnp->next;
7010Sstevel@tonic-gate 	oldnp->next = 0;
7020Sstevel@tonic-gate 	return (av-argv);
7030Sstevel@tonic-gate err:
7040Sstevel@tonic-gate 	if (*av)
7050Sstevel@tonic-gate 		(void) fprintf(stderr,
7060Sstevel@tonic-gate 		    gettext("%s: bad option %s\n"), cmdname, *av);
7070Sstevel@tonic-gate 	else
7080Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: bad option\n"), cmdname);
7090Sstevel@tonic-gate 	usage();
7100Sstevel@tonic-gate 	/*NOTREACHED*/
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate /*
7140Sstevel@tonic-gate  * print out a usage message
7150Sstevel@tonic-gate  */
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate static void
usage(void)71818Srobbin usage(void)
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate 	(void) fprintf(stderr,
7210Sstevel@tonic-gate 	    gettext("%s: [-H | -L] path-list predicate-list\n"), cmdname);
7220Sstevel@tonic-gate 	exit(1);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate  * This is the function that gets executed at each node
7270Sstevel@tonic-gate  */
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate static int
execute(name,statb,type,state)7300Sstevel@tonic-gate execute(name, statb, type, state)
7310Sstevel@tonic-gate char *name;
7320Sstevel@tonic-gate struct stat *statb;
7330Sstevel@tonic-gate int type;
7340Sstevel@tonic-gate struct FTW *state;
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate 	struct Node *np = topnode;
7370Sstevel@tonic-gate 	int val;
7380Sstevel@tonic-gate 	time_t t;
7390Sstevel@tonic-gate 	long l;
7400Sstevel@tonic-gate 	long long ll;
7410Sstevel@tonic-gate 	int not = 1;
7420Sstevel@tonic-gate 	char *filename;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	if (type == FTW_NS) {
7450Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: stat() error %s: %s\n"),
7460Sstevel@tonic-gate 			cmdname, name, strerror(errno));
7470Sstevel@tonic-gate 		error = 1;
7480Sstevel@tonic-gate 		return (0);
7490Sstevel@tonic-gate 	} else if (type == FTW_DNR) {
7500Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: cannot read dir %s: %s\n"),
7510Sstevel@tonic-gate 			cmdname, name, strerror(errno));
7520Sstevel@tonic-gate 		error = 1;
753*12685Srich.burridge@oracle.com 	} else if (type == FTW_SLN && lflag == 1) {
7540Sstevel@tonic-gate 		(void) fprintf(stderr,
7550Sstevel@tonic-gate 			gettext("%s: cannot follow symbolic link %s: %s\n"),
7560Sstevel@tonic-gate 			cmdname, name, strerror(errno));
7570Sstevel@tonic-gate 		error = 1;
7583523Scf46844 	} else if (type == FTW_DL) {
7593523Scf46844 		(void) fprintf(stderr, gettext("%s: cycle detected for %s\n"),
7603523Scf46844 			cmdname, name);
7613523Scf46844 		error = 1;
7623523Scf46844 		return (0);
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	while (np) {
7660Sstevel@tonic-gate 		switch (np->action) {
7670Sstevel@tonic-gate 		case NOT:
7680Sstevel@tonic-gate 			not = !not;
7690Sstevel@tonic-gate 			np = np->next;
7700Sstevel@tonic-gate 			continue;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 		case AND:
7730Sstevel@tonic-gate 			np = np->next;
7740Sstevel@tonic-gate 			continue;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 		case OR:
7770Sstevel@tonic-gate 			if (np->first.np == np) {
7780Sstevel@tonic-gate 				/*
7790Sstevel@tonic-gate 				 * handle naked OR (no term on left hand side)
7800Sstevel@tonic-gate 				 */
7810Sstevel@tonic-gate 				(void) fprintf(stderr,
7820Sstevel@tonic-gate 				    gettext("%s: invalid -o construction\n"),
7830Sstevel@tonic-gate 				    cmdname);
7840Sstevel@tonic-gate 				exit(2);
7850Sstevel@tonic-gate 			}
7860Sstevel@tonic-gate 			/* FALLTHROUGH */
7870Sstevel@tonic-gate 		case LPAREN: {
7880Sstevel@tonic-gate 			struct Node *save = topnode;
7890Sstevel@tonic-gate 			topnode = np->first.np;
7900Sstevel@tonic-gate 			(void) execute(name, statb, type, state);
7910Sstevel@tonic-gate 			val = lastval;
7920Sstevel@tonic-gate 			topnode = save;
7930Sstevel@tonic-gate 			if (np->action == OR) {
7940Sstevel@tonic-gate 				if (val)
7950Sstevel@tonic-gate 					return (0);
7960Sstevel@tonic-gate 				val = 1;
7970Sstevel@tonic-gate 			}
7980Sstevel@tonic-gate 			break;
7990Sstevel@tonic-gate 		}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 		case LOCAL: {
8020Sstevel@tonic-gate 			int	nremfs;
8030Sstevel@tonic-gate 			val = 1;
8040Sstevel@tonic-gate 			/*
8050Sstevel@tonic-gate 			 * If file system type matches the remote
8060Sstevel@tonic-gate 			 * file system type, then it is not local.
8070Sstevel@tonic-gate 			 */
8080Sstevel@tonic-gate 			for (nremfs = 0; nremfs < fstype_index; nremfs++) {
8090Sstevel@tonic-gate 				if (strcmp(remote_fstypes[nremfs],
8100Sstevel@tonic-gate 						statb->st_fstype) == 0) {
8110Sstevel@tonic-gate 					val = 0;
8120Sstevel@tonic-gate 					break;
8130Sstevel@tonic-gate 				}
8140Sstevel@tonic-gate 			}
8150Sstevel@tonic-gate 			break;
8160Sstevel@tonic-gate 		}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 		case TYPE:
8190Sstevel@tonic-gate 			l = (long)statb->st_mode&S_IFMT;
8200Sstevel@tonic-gate 			goto num;
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 		case PERM:
8230Sstevel@tonic-gate 			l = (long)statb->st_mode&07777;
8240Sstevel@tonic-gate 			if (np->second.i == '-')
8250Sstevel@tonic-gate 				val = ((l&np->first.l) == np->first.l);
8260Sstevel@tonic-gate 			else
8270Sstevel@tonic-gate 				val = (l == np->first.l);
8280Sstevel@tonic-gate 			break;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 		case INUM:
8310Sstevel@tonic-gate 			ll = (long long)statb->st_ino;
8320Sstevel@tonic-gate 			goto llnum;
8330Sstevel@tonic-gate 		case NEWER:
8340Sstevel@tonic-gate 			l = statb->st_mtime;
8350Sstevel@tonic-gate 			goto num;
8360Sstevel@tonic-gate 		case ATIME:
8370Sstevel@tonic-gate 			t = statb->st_atime;
8380Sstevel@tonic-gate 			goto days;
8390Sstevel@tonic-gate 		case CTIME:
8400Sstevel@tonic-gate 			t = statb->st_ctime;
8410Sstevel@tonic-gate 			goto days;
8420Sstevel@tonic-gate 		case MTIME:
8430Sstevel@tonic-gate 			t = statb->st_mtime;
8440Sstevel@tonic-gate 		days:
8450Sstevel@tonic-gate 			l = (now-t)/A_DAY;
8460Sstevel@tonic-gate 			goto num;
84751Sjonb 		case MMIN:
84851Sjonb 			t = statb->st_mtime;
84951Sjonb 			goto mins;
85051Sjonb 		case AMIN:
85151Sjonb 			t = statb->st_atime;
85251Sjonb 			goto mins;
85351Sjonb 		case CMIN:
85451Sjonb 			t = statb->st_ctime;
85551Sjonb 			goto mins;
85651Sjonb 		mins:
85751Sjonb 			l = (now-t)/A_MIN;
85851Sjonb 			goto num;
8590Sstevel@tonic-gate 		case CSIZE:
8600Sstevel@tonic-gate 			ll = (long long)statb->st_size;
8610Sstevel@tonic-gate 			goto llnum;
8620Sstevel@tonic-gate 		case SIZE:
8630Sstevel@tonic-gate 			ll = (long long)round(statb->st_size, BLKSIZ)/BLKSIZ;
8640Sstevel@tonic-gate 			goto llnum;
8650Sstevel@tonic-gate 		case F_USER:
8660Sstevel@tonic-gate 			l = (long)statb->st_uid;
8670Sstevel@tonic-gate 			goto num;
8680Sstevel@tonic-gate 		case F_GROUP:
8690Sstevel@tonic-gate 			l = (long)statb->st_gid;
8700Sstevel@tonic-gate 			goto num;
8710Sstevel@tonic-gate 		case LINKS:
8720Sstevel@tonic-gate 			l = (long)statb->st_nlink;
8730Sstevel@tonic-gate 			goto num;
8740Sstevel@tonic-gate 		llnum:
8750Sstevel@tonic-gate 			if (np->second.i == '+')
8760Sstevel@tonic-gate 				val = (ll > np->first.ll);
8770Sstevel@tonic-gate 			else if (np->second.i == '-')
8780Sstevel@tonic-gate 				val = (ll < np->first.ll);
8790Sstevel@tonic-gate 			else
8800Sstevel@tonic-gate 				val = (ll == np->first.ll);
8810Sstevel@tonic-gate 			break;
8820Sstevel@tonic-gate 		num:
8830Sstevel@tonic-gate 			if (np->second.i == '+')
8840Sstevel@tonic-gate 				val = (l > np->first.l);
8850Sstevel@tonic-gate 			else if (np->second.i == '-')
8860Sstevel@tonic-gate 				val = (l < np->first.l);
8870Sstevel@tonic-gate 			else
8880Sstevel@tonic-gate 				val = (l == np->first.l);
8890Sstevel@tonic-gate 			break;
8900Sstevel@tonic-gate 		case OK:
8910Sstevel@tonic-gate 			val = ok(name, np->first.ap);
8920Sstevel@tonic-gate 			break;
8930Sstevel@tonic-gate 		case EXEC:
8941506Srm88369 			val = doexec(name, np->first.ap, NULL);
8950Sstevel@tonic-gate 			break;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 		case VARARGS: {
8980Sstevel@tonic-gate 			struct Arglist *ap = np->first.vp;
8990Sstevel@tonic-gate 			char *cp;
9000Sstevel@tonic-gate 			cp = ap->nextstr - (strlen(name)+1);
9010Sstevel@tonic-gate 			if (cp >= (char *)(ap->nextvar+3)) {
9020Sstevel@tonic-gate 				/* there is room just copy the name */
9030Sstevel@tonic-gate 				val = 1;
9040Sstevel@tonic-gate 				(void) strcpy(cp, name);
9050Sstevel@tonic-gate 				*ap->nextvar++ = cp;
9060Sstevel@tonic-gate 				ap->nextstr = cp;
9070Sstevel@tonic-gate 			} else {
9080Sstevel@tonic-gate 				/* no more room, exec command */
9090Sstevel@tonic-gate 				*ap->nextvar++ = name;
9100Sstevel@tonic-gate 				*ap->nextvar = 0;
9110Sstevel@tonic-gate 				val = 1;
9121506Srm88369 				(void) doexec((char *)0, ap->arglist,
9131506Srm88369 				    &exec_exitcode);
9140Sstevel@tonic-gate 				ap->nextstr = ap->end;
9150Sstevel@tonic-gate 				ap->nextvar = ap->firstvar;
9160Sstevel@tonic-gate 			}
9170Sstevel@tonic-gate 			break;
9180Sstevel@tonic-gate 		}
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 		case DEPTH:
9210Sstevel@tonic-gate 		case MOUNT:
9220Sstevel@tonic-gate 		case FOLLOW:
9230Sstevel@tonic-gate 			val = 1;
9240Sstevel@tonic-gate 			break;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 		case NAME: {
9276584Schin 			char *name1;
9286584Schin 
9296584Schin 			/*
9306584Schin 			 * basename(3c) may modify name, so
9316584Schin 			 * we need to pass another string
9326584Schin 			 */
9336584Schin 			if ((name1 = strdup(name)) == NULL) {
9346584Schin 				(void) fprintf(stderr,
9356584Schin 				    gettext("%s: cannot strdup() %s: %s\n"),
9366588Schin 				    cmdname, name, strerror(errno));
9376584Schin 				exit(2);
9386584Schin 			}
9390Sstevel@tonic-gate 			/*
9400Sstevel@tonic-gate 			 * XPG4 find should not treat a leading '.' in a
9410Sstevel@tonic-gate 			 * filename specially for pattern matching.
9420Sstevel@tonic-gate 			 * /usr/bin/find  will not pattern match a leading
9430Sstevel@tonic-gate 			 * '.' in a filename, unless '.' is explicitly
9440Sstevel@tonic-gate 			 * specified.
9450Sstevel@tonic-gate 			 */
9460Sstevel@tonic-gate #ifdef XPG4
9470Sstevel@tonic-gate 			val = !fnmatch(np->first.cp,
9486584Schin 			    basename(name1), 0);
9490Sstevel@tonic-gate #else
9500Sstevel@tonic-gate 			val = !fnmatch(np->first.cp,
9516584Schin 			    basename(name1), FNM_PERIOD);
9520Sstevel@tonic-gate #endif
9536598Schin 			free(name1);
9540Sstevel@tonic-gate 			break;
9550Sstevel@tonic-gate 		}
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 		case PRUNE:
9580Sstevel@tonic-gate 			if (type == FTW_D)
9590Sstevel@tonic-gate 				state->quit = FTW_PRUNE;
9600Sstevel@tonic-gate 			val = 1;
9610Sstevel@tonic-gate 			break;
9620Sstevel@tonic-gate 		case NOUSER:
9630Sstevel@tonic-gate 			val = ((getpwuid(statb->st_uid)) == 0);
9640Sstevel@tonic-gate 			break;
9650Sstevel@tonic-gate 		case NOGRP:
9660Sstevel@tonic-gate 			val = ((getgrgid(statb->st_gid)) == 0);
9670Sstevel@tonic-gate 			break;
9680Sstevel@tonic-gate 		case FSTYPE:
9690Sstevel@tonic-gate 			val = (strcmp(np->first.cp, statb->st_fstype) == 0);
9700Sstevel@tonic-gate 			break;
9710Sstevel@tonic-gate 		case CPIO:
9720Sstevel@tonic-gate 			output = (FILE *)np->first.l;
9730Sstevel@tonic-gate 			(void) fprintf(output, "%s\n", name);
9740Sstevel@tonic-gate 			val = 1;
9750Sstevel@tonic-gate 			break;
9760Sstevel@tonic-gate 		case PRINT:
9770Sstevel@tonic-gate 			(void) fprintf(stdout, "%s\n", name);
9780Sstevel@tonic-gate 			val = 1;
9790Sstevel@tonic-gate 			break;
9800Sstevel@tonic-gate 		case LS:
9810Sstevel@tonic-gate 			(void) list(name, statb);
9820Sstevel@tonic-gate 			val = 1;
9830Sstevel@tonic-gate 			break;
9840Sstevel@tonic-gate 		case XATTR:
9857530SJohn.Sonnenschein@Sun.COM 			filename = (walkflags & FTW_CHDIR) ?
9867530SJohn.Sonnenschein@Sun.COM 				gettail(name) : name;
9870Sstevel@tonic-gate 			val = (pathconf(filename, _PC_XATTR_EXISTS) == 1);
9880Sstevel@tonic-gate 			break;
9890Sstevel@tonic-gate 		case ACL:
9900Sstevel@tonic-gate 			/*
9910Sstevel@tonic-gate 			 * Need to get the tail of the file name, since we have
9920Sstevel@tonic-gate 			 * already chdir()ed into the directory (performed in
9930Sstevel@tonic-gate 			 * nftw()) of the file
9940Sstevel@tonic-gate 			 */
9957530SJohn.Sonnenschein@Sun.COM 			filename = (walkflags & FTW_CHDIR) ?
9967530SJohn.Sonnenschein@Sun.COM 				gettail(name) : name;
9977530SJohn.Sonnenschein@Sun.COM 			val = acl_trivial(filename);
9980Sstevel@tonic-gate 			break;
9990Sstevel@tonic-gate 		}
10000Sstevel@tonic-gate 		/*
10010Sstevel@tonic-gate 		 * evaluate 'val' and 'not' (exclusive-or)
10020Sstevel@tonic-gate 		 * if no inversion (not == 1), return only when val == 0
10030Sstevel@tonic-gate 		 * (primary not true). Otherwise, invert the primary
10040Sstevel@tonic-gate 		 * and return when the primary is true.
10050Sstevel@tonic-gate 		 * 'Lastval' saves the last result (fail or pass) when
10060Sstevel@tonic-gate 		 * returning back to the calling routine.
10070Sstevel@tonic-gate 		 */
10080Sstevel@tonic-gate 		if (val^not) {
10090Sstevel@tonic-gate 			lastval = 0;
10100Sstevel@tonic-gate 			return (0);
10110Sstevel@tonic-gate 		}
10120Sstevel@tonic-gate 		lastval = 1;
10130Sstevel@tonic-gate 		not = 1;
10140Sstevel@tonic-gate 		np = np->next;
10150Sstevel@tonic-gate 	}
10160Sstevel@tonic-gate 	return (0);
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate /*
10200Sstevel@tonic-gate  * code for the -ok option
10210Sstevel@tonic-gate  */
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate static int
ok(name,argv)10240Sstevel@tonic-gate ok(name, argv)
10250Sstevel@tonic-gate char *name;
10260Sstevel@tonic-gate char *argv[];
10270Sstevel@tonic-gate {
10284774Sas145665 	int  c;
10294774Sas145665 	int i = 0;
10304774Sas145665 	char resp[LINE_MAX + 1];
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	(void) fflush(stdout); 	/* to flush possible `-print' */
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	if ((*argv != dummyarg) && (strcmp(*argv, name)))
10350Sstevel@tonic-gate 		(void) fprintf(stderr, "< %s ... %s >?   ", *argv, name);
10360Sstevel@tonic-gate 	else
10370Sstevel@tonic-gate 		(void) fprintf(stderr, "< {} ... %s >?   ", name);
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	(void) fflush(stderr);
10404774Sas145665 
10414774Sas145665 	while ((c = getchar()) != '\n') {
10420Sstevel@tonic-gate 		if (c == EOF)
10430Sstevel@tonic-gate 			exit(2);
10444774Sas145665 		if (i < LINE_MAX)
10454774Sas145665 			resp[i++] = c;
10464774Sas145665 	}
10474774Sas145665 	resp[i] = '\0';
10484774Sas145665 
10494774Sas145665 	if (yes_check(resp))
10504774Sas145665 		return (doexec(name, argv, NULL));
10514774Sas145665 	else
10524774Sas145665 		return (0);
10530Sstevel@tonic-gate }
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate /*
10560Sstevel@tonic-gate  * execute argv with {} replaced by name
10571506Srm88369  *
10581506Srm88369  * Per XPG6, find must exit non-zero if an invocation through
10591506Srm88369  * -exec, punctuated by a plus sign, exits non-zero, so set
10601506Srm88369  * exitcode if we see a non-zero exit.
10611506Srm88369  * exitcode should be NULL when -exec or -ok is not punctuated
10621506Srm88369  * by a plus sign.
10630Sstevel@tonic-gate  */
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate static int
doexec(char * name,char * argv[],int * exitcode)10661506Srm88369 doexec(char *name, char *argv[], int *exitcode)
10670Sstevel@tonic-gate {
10680Sstevel@tonic-gate 	char *cp;
10690Sstevel@tonic-gate 	char **av = argv;
10701506Srm88369 	char *newargs[1 + SHELL_MAXARGS + 1];
10710Sstevel@tonic-gate 	int dummyseen = 0;
10721506Srm88369 	int i, j, status, rc, r = 0;
10731506Srm88369 	int exit_status = 0;
10741506Srm88369 	pid_t pid, pid1;
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	(void) fflush(stdout);	  /* to flush possible `-print' */
10770Sstevel@tonic-gate 	if (name) {
10780Sstevel@tonic-gate 		while (cp = *av++) {
10790Sstevel@tonic-gate 			if (cp == dummyarg) {
10800Sstevel@tonic-gate 				dummyseen = 1;
10810Sstevel@tonic-gate 				av[-1] = name;
10820Sstevel@tonic-gate 			}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 		}
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 	if (argv[0] == NULL)    /* null command line */
10870Sstevel@tonic-gate 		return (r);
10880Sstevel@tonic-gate 
10891506Srm88369 	if ((pid = fork()) == -1) {
10901506Srm88369 		/* fork failed */
10911506Srm88369 		if (exitcode != NULL)
10921506Srm88369 			*exitcode = 1;
10931506Srm88369 		return (0);
10941506Srm88369 	}
10951506Srm88369 	if (pid != 0) {
10961506Srm88369 		/* parent */
10971506Srm88369 		do {
10981506Srm88369 			/* wait for child to exit */
10991506Srm88369 			if ((rc = wait(&r)) == -1 && errno != EINTR) {
11001506Srm88369 				(void) fprintf(stderr,
11011506Srm88369 				    gettext("wait failed %s"), strerror(errno));
11021506Srm88369 
11031506Srm88369 				if (exitcode != NULL)
11041506Srm88369 					*exitcode = 1;
11051506Srm88369 				return (0);
11061506Srm88369 			}
11071506Srm88369 		} while (rc != pid);
11081506Srm88369 	} else {
11091506Srm88369 		/* child */
11100Sstevel@tonic-gate 		(void) execvp(argv[0], argv);
11111506Srm88369 		if (errno != E2BIG)
11121506Srm88369 			exit(1);
11131506Srm88369 
11141506Srm88369 		/*
11151506Srm88369 		 * We are in a situation where argv[0] points to a
11161506Srm88369 		 * script without the interpreter line, e.g. #!/bin/sh.
11171506Srm88369 		 * execvp() will execute either /usr/bin/sh or
11181506Srm88369 		 * /usr/xpg4/bin/sh against the script, and you will be
11191506Srm88369 		 * limited to SHELL_MAXARGS arguments. If you try to
11201506Srm88369 		 * pass more than SHELL_MAXARGS arguments, execvp()
11211506Srm88369 		 * fails with E2BIG.
11221506Srm88369 		 * See usr/src/lib/libc/port/gen/execvp.c.
11231506Srm88369 		 *
11241506Srm88369 		 * In this situation, process the argument list by
11251506Srm88369 		 * packets of SHELL_MAXARGS arguments with respect of
11261506Srm88369 		 * the following rules:
11271506Srm88369 		 * 1. the invocations have to complete before find exits
11281506Srm88369 		 * 2. only one invocation can be running at a time
11291506Srm88369 		 */
11301506Srm88369 
11311506Srm88369 		i = 1;
11321506Srm88369 		newargs[0] = argv[0];
11331506Srm88369 
11341506Srm88369 		while (argv[i]) {
11351506Srm88369 			j = 1;
11361506Srm88369 			while (j <= SHELL_MAXARGS && argv[i]) {
11371506Srm88369 				newargs[j++] = argv[i++];
11381506Srm88369 			}
11391506Srm88369 			newargs[j] = NULL;
11401506Srm88369 
11411506Srm88369 			if ((pid1 = fork()) == -1) {
11421506Srm88369 				/* fork failed */
11431506Srm88369 				exit(1);
11441506Srm88369 			}
11451506Srm88369 			if (pid1 == 0) {
11461506Srm88369 				/* child */
11471506Srm88369 				(void) execvp(newargs[0], newargs);
11481506Srm88369 				exit(1);
11491506Srm88369 			}
11501506Srm88369 
11511506Srm88369 			status = 0;
11521506Srm88369 
11531506Srm88369 			do {
11541506Srm88369 				/* wait for the child to exit */
11551506Srm88369 				if ((rc = wait(&status)) == -1 &&
11561506Srm88369 				    errno != EINTR) {
11571506Srm88369 					(void) fprintf(stderr,
11581506Srm88369 					    gettext("wait failed %s"),
11591506Srm88369 					    strerror(errno));
11601506Srm88369 					exit(1);
11611506Srm88369 				}
11621506Srm88369 			} while (rc != pid1);
11631506Srm88369 
11641506Srm88369 			if (status)
11651506Srm88369 				exit_status = 1;
11661506Srm88369 		}
11671506Srm88369 		/* all the invocations have completed */
11681506Srm88369 		exit(exit_status);
11690Sstevel@tonic-gate 	}
11701506Srm88369 
11710Sstevel@tonic-gate 	if (name && dummyseen) {
11720Sstevel@tonic-gate 		for (av = argv; cp = *av++; ) {
11730Sstevel@tonic-gate 			if (cp == name)
11740Sstevel@tonic-gate 				av[-1] = dummyarg;
11750Sstevel@tonic-gate 		}
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11781506Srm88369 	if (r && exitcode != NULL)
11791506Srm88369 		*exitcode = 3; /* use to indicate error in cmd invocation */
11801506Srm88369 
11810Sstevel@tonic-gate 	return (!r);
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate /*
11860Sstevel@tonic-gate  *  Table lookup routine
11870Sstevel@tonic-gate  */
11880Sstevel@tonic-gate static struct Args *
lookup(word)11890Sstevel@tonic-gate lookup(word)
11900Sstevel@tonic-gate char *word;
11910Sstevel@tonic-gate {
11920Sstevel@tonic-gate 	struct Args *argp = commands;
11930Sstevel@tonic-gate 	int second;
11940Sstevel@tonic-gate 	if (word == 0 || *word == 0)
11950Sstevel@tonic-gate 		return (0);
11960Sstevel@tonic-gate 	second = word[1];
11970Sstevel@tonic-gate 	while (*argp->name) {
11980Sstevel@tonic-gate 		if (second == argp->name[1] && strcmp(word, argp->name) == 0)
11990Sstevel@tonic-gate 			return (argp);
12000Sstevel@tonic-gate 		argp++;
12010Sstevel@tonic-gate 	}
12020Sstevel@tonic-gate 	return (0);
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate /*
12070Sstevel@tonic-gate  * Get space for variable length argument list
12080Sstevel@tonic-gate  */
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate static struct Arglist *
varargs(com)12110Sstevel@tonic-gate varargs(com)
12120Sstevel@tonic-gate char **com;
12130Sstevel@tonic-gate {
12140Sstevel@tonic-gate 	struct Arglist *ap;
12150Sstevel@tonic-gate 	int n;
12160Sstevel@tonic-gate 	char **ep;
12170Sstevel@tonic-gate 	if (varsize == 0) {
12180Sstevel@tonic-gate 		n = 2*sizeof (char **);
12190Sstevel@tonic-gate 		for (ep = environ; *ep; ep++)
12200Sstevel@tonic-gate 			n += (strlen(*ep)+sizeof (ep) + 1);
12210Sstevel@tonic-gate 		varsize = sizeof (struct Arglist)+ARG_MAX-PATH_MAX-n-1;
12220Sstevel@tonic-gate 	}
12230Sstevel@tonic-gate 	ap = (struct Arglist *)malloc(varsize+1);
12240Sstevel@tonic-gate 	ap->end = (char *)ap + varsize;
12250Sstevel@tonic-gate 	ap->nextstr = ap->end;
12260Sstevel@tonic-gate 	ap->nextvar = ap->arglist;
12270Sstevel@tonic-gate 	while (*ap->nextvar++ = *com++);
12280Sstevel@tonic-gate 	ap->nextvar--;
12290Sstevel@tonic-gate 	ap->firstvar = ap->nextvar;
12300Sstevel@tonic-gate 	ap->next = lastlist;
12310Sstevel@tonic-gate 	lastlist = ap;
12320Sstevel@tonic-gate 	return (ap);
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate /*
12360Sstevel@tonic-gate  * filter command support
12370Sstevel@tonic-gate  * fork and exec cmd(argv) according to mode:
12380Sstevel@tonic-gate  *
12390Sstevel@tonic-gate  *	"r"	with fp as stdin of cmd (default stdin), cmd stdout returned
12400Sstevel@tonic-gate  *	"w"	with fp as stdout of cmd (default stdout), cmd stdin returned
12410Sstevel@tonic-gate  */
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate #define	CMDERR	((1<<8)-1)	/* command error exit code		*/
12440Sstevel@tonic-gate #define	MAXCMDS	8		/* max # simultaneous cmdopen()'s	*/
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate static struct			/* info for each cmdopen()		*/
12470Sstevel@tonic-gate {
12480Sstevel@tonic-gate 	FILE	*fp;		/* returned by cmdopen()		*/
12490Sstevel@tonic-gate 	pid_t	pid;		/* pid used by cmdopen()		*/
12500Sstevel@tonic-gate } cmdproc[MAXCMDS];
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate static FILE *
cmdopen(cmd,argv,mode,fp)12530Sstevel@tonic-gate cmdopen(cmd, argv, mode, fp)
12540Sstevel@tonic-gate char	*cmd;
12550Sstevel@tonic-gate char	**argv;
12560Sstevel@tonic-gate char	*mode;
12570Sstevel@tonic-gate FILE	*fp;
12580Sstevel@tonic-gate {
12590Sstevel@tonic-gate 	int	proc;
12600Sstevel@tonic-gate 	int	cmdfd;
12610Sstevel@tonic-gate 	int	usrfd;
12620Sstevel@tonic-gate 	int		pio[2];
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	switch (*mode) {
12650Sstevel@tonic-gate 	case 'r':
12660Sstevel@tonic-gate 		cmdfd = 1;
12670Sstevel@tonic-gate 		usrfd = 0;
12680Sstevel@tonic-gate 		break;
12690Sstevel@tonic-gate 	case 'w':
12700Sstevel@tonic-gate 		cmdfd = 0;
12710Sstevel@tonic-gate 		usrfd = 1;
12720Sstevel@tonic-gate 		break;
12730Sstevel@tonic-gate 	default:
12740Sstevel@tonic-gate 		return (0);
12750Sstevel@tonic-gate 	}
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	for (proc = 0; proc < MAXCMDS; proc++)
12780Sstevel@tonic-gate 		if (!cmdproc[proc].fp)
12790Sstevel@tonic-gate 			break;
12800Sstevel@tonic-gate 	if (proc >= MAXCMDS)
12810Sstevel@tonic-gate 		return (0);
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 	if (pipe(pio))
12840Sstevel@tonic-gate 		return (0);
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	switch (cmdproc[proc].pid = fork()) {
12870Sstevel@tonic-gate 	case -1:
12880Sstevel@tonic-gate 		return (0);
12890Sstevel@tonic-gate 	case 0:
12900Sstevel@tonic-gate 		if (fp && fileno(fp) != usrfd) {
12910Sstevel@tonic-gate 			(void) close(usrfd);
12920Sstevel@tonic-gate 			if (dup2(fileno(fp), usrfd) != usrfd)
12930Sstevel@tonic-gate 				_exit(CMDERR);
12940Sstevel@tonic-gate 			(void) close(fileno(fp));
12950Sstevel@tonic-gate 		}
12960Sstevel@tonic-gate 		(void) close(cmdfd);
12970Sstevel@tonic-gate 		if (dup2(pio[cmdfd], cmdfd) != cmdfd)
12980Sstevel@tonic-gate 			_exit(CMDERR);
12990Sstevel@tonic-gate 		(void) close(pio[cmdfd]);
13000Sstevel@tonic-gate 		(void) close(pio[usrfd]);
13010Sstevel@tonic-gate 		(void) execvp(cmd, argv);
13020Sstevel@tonic-gate 		if (errno == ENOEXEC) {
13030Sstevel@tonic-gate 			char	**p;
13040Sstevel@tonic-gate 			char		**v;
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 			/*
13070Sstevel@tonic-gate 			 * assume cmd is a shell script
13080Sstevel@tonic-gate 			 */
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 			p = argv;
13110Sstevel@tonic-gate 			while (*p++);
13120Sstevel@tonic-gate 			if (v = (char **)malloc((p - argv + 1) *
13130Sstevel@tonic-gate 					sizeof (char **))) {
13140Sstevel@tonic-gate 				p = v;
13150Sstevel@tonic-gate 				*p++ = cmd;
13160Sstevel@tonic-gate 				if (*argv) argv++;
13170Sstevel@tonic-gate 				while (*p++ = *argv++);
13180Sstevel@tonic-gate 				(void) execv(getshell(), v);
13190Sstevel@tonic-gate 			}
13200Sstevel@tonic-gate 		}
13210Sstevel@tonic-gate 		_exit(CMDERR);
13220Sstevel@tonic-gate 		/*NOTREACHED*/
13230Sstevel@tonic-gate 	default:
13240Sstevel@tonic-gate 		(void) close(pio[cmdfd]);
13250Sstevel@tonic-gate 		return (cmdproc[proc].fp = fdopen(pio[usrfd], mode));
13260Sstevel@tonic-gate 	}
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate /*
13300Sstevel@tonic-gate  * close a stream opened by cmdopen()
13310Sstevel@tonic-gate  * -1 returned if cmdopen() had a problem
13320Sstevel@tonic-gate  * otherwise exit() status of command is returned
13330Sstevel@tonic-gate  */
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate static int
cmdclose(fp)13360Sstevel@tonic-gate cmdclose(fp)
13370Sstevel@tonic-gate FILE	*fp;
13380Sstevel@tonic-gate {
13390Sstevel@tonic-gate 	int	i;
13400Sstevel@tonic-gate 	pid_t	p, pid;
13410Sstevel@tonic-gate 	int		status;
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	for (i = 0; i < MAXCMDS; i++)
13440Sstevel@tonic-gate 		if (fp == cmdproc[i].fp) break;
13450Sstevel@tonic-gate 	if (i >= MAXCMDS)
13460Sstevel@tonic-gate 		return (-1);
13470Sstevel@tonic-gate 	(void) fclose(fp);
13480Sstevel@tonic-gate 	cmdproc[i].fp = 0;
13490Sstevel@tonic-gate 	pid = cmdproc[i].pid;
13500Sstevel@tonic-gate 	while ((p = wait(&status)) != pid && p != (pid_t)-1);
13510Sstevel@tonic-gate 	if (p == pid) {
13520Sstevel@tonic-gate 		status = (status >> 8) & CMDERR;
13530Sstevel@tonic-gate 		if (status == CMDERR)
13540Sstevel@tonic-gate 			status = -1;
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 	else
13570Sstevel@tonic-gate 		status = -1;
13580Sstevel@tonic-gate 	return (status);
13590Sstevel@tonic-gate }
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate /*
13620Sstevel@tonic-gate  * return pointer to the full path name of the shell
13630Sstevel@tonic-gate  *
13640Sstevel@tonic-gate  * SHELL is read from the environment and must start with /
13650Sstevel@tonic-gate  *
13660Sstevel@tonic-gate  * if set-uid or set-gid then the executable and its containing
13670Sstevel@tonic-gate  * directory must not be writable by the real user
13680Sstevel@tonic-gate  *
13690Sstevel@tonic-gate  * /usr/bin/sh is returned by default
13700Sstevel@tonic-gate  */
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate char *
getshell()13730Sstevel@tonic-gate getshell()
13740Sstevel@tonic-gate {
13750Sstevel@tonic-gate 	char	*s;
13760Sstevel@tonic-gate 	char	*sh;
13770Sstevel@tonic-gate 	uid_t	u;
13780Sstevel@tonic-gate 	int	j;
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	if (((sh = getenv("SHELL")) != 0) && *sh == '/') {
13810Sstevel@tonic-gate 		if (u = getuid()) {
13820Sstevel@tonic-gate 			if ((u != geteuid() || getgid() != getegid()) &&
13834774Sas145665 			    access(sh, 2) == 0)
13840Sstevel@tonic-gate 				goto defshell;
13850Sstevel@tonic-gate 			s = strrchr(sh, '/');
13860Sstevel@tonic-gate 			*s = 0;
13870Sstevel@tonic-gate 			j = access(sh, 2);
13880Sstevel@tonic-gate 			*s = '/';
13890Sstevel@tonic-gate 			if (!j) goto defshell;
13900Sstevel@tonic-gate 		}
13910Sstevel@tonic-gate 		return (sh);
13920Sstevel@tonic-gate 	}
13930Sstevel@tonic-gate defshell:
13940Sstevel@tonic-gate 	return ("/usr/bin/sh");
13950Sstevel@tonic-gate }
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate /*
13980Sstevel@tonic-gate  * the following functions implement the added "-ls" option
13990Sstevel@tonic-gate  */
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate #include <utmpx.h>
14020Sstevel@tonic-gate #include <sys/mkdev.h>
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate struct		utmpx utmpx;
14050Sstevel@tonic-gate #define	NMAX	(sizeof (utmpx.ut_name))
14060Sstevel@tonic-gate #define	SCPYN(a, b)	(void) strncpy(a, b, NMAX)
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate #define	NUID	64
14090Sstevel@tonic-gate #define	NGID	64
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate static struct ncache {
14120Sstevel@tonic-gate 	int	id;
14130Sstevel@tonic-gate 	char	name[NMAX+1];
14140Sstevel@tonic-gate } nc[NUID], gc[NGID];
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate /*
14170Sstevel@tonic-gate  * This function assumes that the password file is hashed
14180Sstevel@tonic-gate  * (or some such) to allow fast access based on a name key.
14190Sstevel@tonic-gate  */
14200Sstevel@tonic-gate static char *
getname(uid_t uid)14210Sstevel@tonic-gate getname(uid_t uid)
14220Sstevel@tonic-gate {
14230Sstevel@tonic-gate 	struct passwd *pw;
14240Sstevel@tonic-gate 	int cp;
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate #if	(((NUID) & ((NUID) - 1)) != 0)
14270Sstevel@tonic-gate 	cp = uid % (NUID);
14280Sstevel@tonic-gate #else
14290Sstevel@tonic-gate 	cp = uid & ((NUID) - 1);
14300Sstevel@tonic-gate #endif
14314321Scasper 	if (nc[cp].id == uid && nc[cp].name[0])
14320Sstevel@tonic-gate 		return (nc[cp].name);
14330Sstevel@tonic-gate 	pw = getpwuid(uid);
14340Sstevel@tonic-gate 	if (!pw)
14350Sstevel@tonic-gate 		return (0);
14360Sstevel@tonic-gate 	nc[cp].id = uid;
14370Sstevel@tonic-gate 	SCPYN(nc[cp].name, pw->pw_name);
14380Sstevel@tonic-gate 	return (nc[cp].name);
14390Sstevel@tonic-gate }
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate /*
14420Sstevel@tonic-gate  * This function assumes that the group file is hashed
14430Sstevel@tonic-gate  * (or some such) to allow fast access based on a name key.
14440Sstevel@tonic-gate  */
14450Sstevel@tonic-gate static char *
getgroup(gid_t gid)14460Sstevel@tonic-gate getgroup(gid_t gid)
14470Sstevel@tonic-gate {
14480Sstevel@tonic-gate 	struct group *gr;
14490Sstevel@tonic-gate 	int cp;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate #if	(((NGID) & ((NGID) - 1)) != 0)
14520Sstevel@tonic-gate 	cp = gid % (NGID);
14530Sstevel@tonic-gate #else
14540Sstevel@tonic-gate 	cp = gid & ((NGID) - 1);
14550Sstevel@tonic-gate #endif
14564321Scasper 	if (gc[cp].id == gid && gc[cp].name[0])
14570Sstevel@tonic-gate 		return (gc[cp].name);
14580Sstevel@tonic-gate 	gr = getgrgid(gid);
14590Sstevel@tonic-gate 	if (!gr)
14600Sstevel@tonic-gate 		return (0);
14610Sstevel@tonic-gate 	gc[cp].id = gid;
14620Sstevel@tonic-gate 	SCPYN(gc[cp].name, gr->gr_name);
14630Sstevel@tonic-gate 	return (gc[cp].name);
14640Sstevel@tonic-gate }
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate #define	permoffset(who)		((who) * 3)
14670Sstevel@tonic-gate #define	permission(who, type)	((type) >> permoffset(who))
14680Sstevel@tonic-gate #define	kbytes(bytes)		(((bytes) + 1023) / 1024)
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate static int
list(file,stp)14710Sstevel@tonic-gate list(file, stp)
14720Sstevel@tonic-gate 	char *file;
14730Sstevel@tonic-gate 	struct stat *stp;
14740Sstevel@tonic-gate {
14750Sstevel@tonic-gate 	char pmode[32], uname[32], gname[32], fsize[32], ftime[32];
1476789Sahrens 	int trivial;
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate /*
14790Sstevel@tonic-gate  * Each line below contains the relevant permission (column 1) and character
14800Sstevel@tonic-gate  * shown when  the corresponding execute bit is either clear (column 2)
14810Sstevel@tonic-gate  * or set (column 3)
14820Sstevel@tonic-gate  * These permissions are as shown by ls(1b)
14830Sstevel@tonic-gate  */
14840Sstevel@tonic-gate 	static long special[] = {	S_ISUID, 'S', 's',
14850Sstevel@tonic-gate 					S_ISGID, 'S', 's',
14860Sstevel@tonic-gate 					S_ISVTX, 'T', 't' };
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 	static time_t sixmonthsago = -1;
14890Sstevel@tonic-gate #ifdef	S_IFLNK
14900Sstevel@tonic-gate 	char flink[MAXPATHLEN + 1];
14910Sstevel@tonic-gate #endif
14920Sstevel@tonic-gate 	int who;
14930Sstevel@tonic-gate 	char *cp;
14940Sstevel@tonic-gate 	char *tailname;
14950Sstevel@tonic-gate 	time_t now;
14960Sstevel@tonic-gate 	long long ksize;
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	if (file == NULL || stp == NULL)
14990Sstevel@tonic-gate 		return (-1);
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	(void) time(&now);
15020Sstevel@tonic-gate 	if (sixmonthsago == -1)
15030Sstevel@tonic-gate 		sixmonthsago = now - 6L*30L*24L*60L*60L;
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	switch (stp->st_mode & S_IFMT) {
15060Sstevel@tonic-gate #ifdef	S_IFDIR
15070Sstevel@tonic-gate 	case S_IFDIR:	/* directory */
15080Sstevel@tonic-gate 		pmode[0] = 'd';
15090Sstevel@tonic-gate 		break;
15100Sstevel@tonic-gate #endif
15110Sstevel@tonic-gate #ifdef	S_IFCHR
15120Sstevel@tonic-gate 	case S_IFCHR:	/* character special */
15130Sstevel@tonic-gate 		pmode[0] = 'c';
15140Sstevel@tonic-gate 		break;
15150Sstevel@tonic-gate #endif
15160Sstevel@tonic-gate #ifdef	S_IFBLK
15170Sstevel@tonic-gate 	case S_IFBLK:	/* block special */
15180Sstevel@tonic-gate 		pmode[0] = 'b';
15190Sstevel@tonic-gate 		break;
15200Sstevel@tonic-gate #endif
15210Sstevel@tonic-gate #ifdef	S_IFIFO
15220Sstevel@tonic-gate 	case S_IFIFO:	/* fifo special */
15230Sstevel@tonic-gate 		pmode[0] = 'p';
15240Sstevel@tonic-gate 		break;
15250Sstevel@tonic-gate #endif
15260Sstevel@tonic-gate #ifdef	S_IFLNK
15270Sstevel@tonic-gate 	case S_IFLNK:	/* symbolic link */
15280Sstevel@tonic-gate 		pmode[0] = 'l';
15290Sstevel@tonic-gate 		break;
15300Sstevel@tonic-gate #endif
15310Sstevel@tonic-gate #ifdef	S_IFSOCK
15320Sstevel@tonic-gate 	case S_IFSOCK:	/* socket */
15330Sstevel@tonic-gate 		pmode[0] = 's';
15340Sstevel@tonic-gate 		break;
15350Sstevel@tonic-gate #endif
15360Sstevel@tonic-gate #ifdef	S_IFDOOR
15370Sstevel@tonic-gate 	case S_IFDOOR:	/* door */
15380Sstevel@tonic-gate 		pmode[0] = 'D';
15390Sstevel@tonic-gate 		break;
15400Sstevel@tonic-gate #endif
15410Sstevel@tonic-gate #ifdef	S_IFREG
15420Sstevel@tonic-gate 	case S_IFREG:	/* regular */
15430Sstevel@tonic-gate 		pmode[0] = '-';
15440Sstevel@tonic-gate 		break;
15450Sstevel@tonic-gate #endif
15460Sstevel@tonic-gate 	default:
15470Sstevel@tonic-gate 		pmode[0] = '?';
15480Sstevel@tonic-gate 		break;
15490Sstevel@tonic-gate 	}
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	for (who = 0; who < 3; who++) {
15520Sstevel@tonic-gate 		int is_exec =  stp->st_mode & permission(who, S_IEXEC)? 1 : 0;
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 		if (stp->st_mode & permission(who, S_IREAD))
15550Sstevel@tonic-gate 			pmode[permoffset(who) + 1] = 'r';
15560Sstevel@tonic-gate 		else
15570Sstevel@tonic-gate 			pmode[permoffset(who) + 1] = '-';
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 		if (stp->st_mode & permission(who, S_IWRITE))
15600Sstevel@tonic-gate 			pmode[permoffset(who) + 2] = 'w';
15610Sstevel@tonic-gate 		else
15620Sstevel@tonic-gate 			pmode[permoffset(who) + 2] = '-';
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 		if (stp->st_mode & special[who * 3])
15650Sstevel@tonic-gate 			pmode[permoffset(who) + 3] =
15660Sstevel@tonic-gate 				special[who * 3 + 1 + is_exec];
15670Sstevel@tonic-gate 		else if (is_exec)
15680Sstevel@tonic-gate 			pmode[permoffset(who) + 3] = 'x';
15690Sstevel@tonic-gate 		else
15700Sstevel@tonic-gate 			pmode[permoffset(who) + 3] = '-';
15710Sstevel@tonic-gate 	}
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	/*
15740Sstevel@tonic-gate 	 * Need to get the tail of the file name, since we have
15750Sstevel@tonic-gate 	 * already chdir()ed into the directory of the file
15760Sstevel@tonic-gate 	 */
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 	tailname = gettail(file);
15790Sstevel@tonic-gate 
1580789Sahrens 	trivial = acl_trivial(tailname);
1581789Sahrens 	if (trivial == -1)
1582789Sahrens 		trivial =  0;
1583789Sahrens 
1584789Sahrens 	if (trivial == 1)
15850Sstevel@tonic-gate 		pmode[permoffset(who) + 1] = '+';
15860Sstevel@tonic-gate 	else
15870Sstevel@tonic-gate 		pmode[permoffset(who) + 1] = ' ';
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	pmode[permoffset(who) + 2] = '\0';
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	/*
15920Sstevel@tonic-gate 	 * Prepare uname and gname.  Always add a space afterwards
15930Sstevel@tonic-gate 	 * to keep columns from running together.
15940Sstevel@tonic-gate 	 */
15950Sstevel@tonic-gate 	cp = getname(stp->st_uid);
15960Sstevel@tonic-gate 	if (cp != NULL)
15970Sstevel@tonic-gate 		(void) sprintf(uname, "%-8s ", cp);
15980Sstevel@tonic-gate 	else
15994321Scasper 		(void) sprintf(uname, "%-8u ", stp->st_uid);
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	cp = getgroup(stp->st_gid);
16020Sstevel@tonic-gate 	if (cp != NULL)
16030Sstevel@tonic-gate 		(void) sprintf(gname, "%-8s ", cp);
16040Sstevel@tonic-gate 	else
16054321Scasper 		(void) sprintf(gname, "%-8u ", stp->st_gid);
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 	if (pmode[0] == 'b' || pmode[0] == 'c')
16080Sstevel@tonic-gate 		(void) sprintf(fsize, "%3ld,%4ld",
16090Sstevel@tonic-gate 			major(stp->st_rdev), minor(stp->st_rdev));
16100Sstevel@tonic-gate 	else {
16110Sstevel@tonic-gate 		(void) sprintf(fsize, (stp->st_size < 100000000) ?
16120Sstevel@tonic-gate 			"%8lld" : "%lld", stp->st_size);
16130Sstevel@tonic-gate #ifdef	S_IFLNK
16140Sstevel@tonic-gate 		if (pmode[0] == 'l') {
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 			who = readlink(tailname, flink, sizeof (flink) - 1);
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 			if (who >= 0)
16200Sstevel@tonic-gate 				flink[who] = '\0';
16210Sstevel@tonic-gate 			else
16220Sstevel@tonic-gate 				flink[0] = '\0';
16230Sstevel@tonic-gate 		}
16240Sstevel@tonic-gate #endif
16250Sstevel@tonic-gate 	}
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	cp = ctime(&stp->st_mtime);
16280Sstevel@tonic-gate 	if (stp->st_mtime < sixmonthsago || stp->st_mtime > now)
16290Sstevel@tonic-gate 		(void) sprintf(ftime, "%-7.7s %-4.4s", cp + 4, cp + 20);
16300Sstevel@tonic-gate 	else
16310Sstevel@tonic-gate 		(void) sprintf(ftime, "%-12.12s", cp + 4);
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	(void) printf((stp->st_ino < 100000) ? "%5llu " :
16340Sstevel@tonic-gate 		"%llu ", stp->st_ino);  /* inode #	*/
16350Sstevel@tonic-gate #ifdef	S_IFSOCK
16360Sstevel@tonic-gate 	ksize = (long long) kbytes(ldbtob(stp->st_blocks)); /* kbytes */
16370Sstevel@tonic-gate #else
16380Sstevel@tonic-gate 	ksize = (long long) kbytes(stp->st_size); /* kbytes */
16390Sstevel@tonic-gate #endif
16400Sstevel@tonic-gate 	(void) printf((ksize < 10000) ? "%4lld " : "%lld ", ksize);
16410Sstevel@tonic-gate 	(void) printf("%s %2ld %s%s%s %s %s%s%s\n",
16420Sstevel@tonic-gate 		pmode,					/* protection	*/
16430Sstevel@tonic-gate 		stp->st_nlink,				/* # of links	*/
16440Sstevel@tonic-gate 		uname,					/* owner	*/
16450Sstevel@tonic-gate 		gname,					/* group	*/
16460Sstevel@tonic-gate 		fsize,					/* # of bytes	*/
16470Sstevel@tonic-gate 		ftime,					/* modify time	*/
16480Sstevel@tonic-gate 		file,					/* name		*/
16490Sstevel@tonic-gate #ifdef	S_IFLNK
16500Sstevel@tonic-gate 		(pmode[0] == 'l') ? " -> " : "",
16510Sstevel@tonic-gate 		(pmode[0] == 'l') ? flink  : ""		/* symlink	*/
16520Sstevel@tonic-gate #else
16530Sstevel@tonic-gate 		"",
16540Sstevel@tonic-gate 		""
16550Sstevel@tonic-gate #endif
16560Sstevel@tonic-gate );
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	return (0);
16590Sstevel@tonic-gate }
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate static char *
new_string(char * s)16620Sstevel@tonic-gate new_string(char *s)
16630Sstevel@tonic-gate {
16640Sstevel@tonic-gate 	char *p = strdup(s);
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	if (p)
16670Sstevel@tonic-gate 		return (p);
16680Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%s: out of memory\n"), cmdname);
16690Sstevel@tonic-gate 	exit(1);
16700Sstevel@tonic-gate 	/*NOTREACHED*/
16710Sstevel@tonic-gate }
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate /*
16740Sstevel@tonic-gate  * Read remote file system types from REMOTE_FS into the
16750Sstevel@tonic-gate  * remote_fstypes array.
16760Sstevel@tonic-gate  */
16770Sstevel@tonic-gate static void
init_remote_fs()16780Sstevel@tonic-gate init_remote_fs()
16790Sstevel@tonic-gate {
16800Sstevel@tonic-gate 	FILE    *fp;
16810Sstevel@tonic-gate 	char    line_buf[LINEBUF_SIZE];
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	if ((fp = fopen(REMOTE_FS, "r")) == NULL) {
16840Sstevel@tonic-gate 		(void) fprintf(stderr,
16854774Sas145665 		    gettext("%s: Warning: can't open %s, ignored\n"),
16864774Sas145665 		    REMOTE_FS, cmdname);
16870Sstevel@tonic-gate 		/* Use default string name for NFS */
16880Sstevel@tonic-gate 		remote_fstypes[fstype_index++] = "nfs";
16890Sstevel@tonic-gate 		return;
16900Sstevel@tonic-gate 	}
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	while (fgets(line_buf, sizeof (line_buf), fp) != NULL) {
16930Sstevel@tonic-gate 		char buf[LINEBUF_SIZE];
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 		/* LINTED - unbounded string specifier */
16960Sstevel@tonic-gate 		(void) sscanf(line_buf, "%s", buf);
16970Sstevel@tonic-gate 		remote_fstypes[fstype_index++] = new_string(buf);
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 		if (fstype_index == N_FSTYPES)
17000Sstevel@tonic-gate 			break;
17010Sstevel@tonic-gate 	}
17020Sstevel@tonic-gate 	(void) fclose(fp);
17030Sstevel@tonic-gate }
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate #define	NPERM	30			/* Largest machine */
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate /*
17080Sstevel@tonic-gate  * The PERM struct is the machine that builds permissions.  The p_special
17090Sstevel@tonic-gate  * field contains what permissions need to be checked at run-time in
17100Sstevel@tonic-gate  * getmode().  This is one of 'X', 'u', 'g', or 'o'.  It contains '\0' to
17110Sstevel@tonic-gate  * indicate normal processing.
17120Sstevel@tonic-gate  */
17130Sstevel@tonic-gate typedef	struct	PERMST	{
17140Sstevel@tonic-gate 	ushort_t	p_who;		/* Range of permission (e.g. ugo) */
17150Sstevel@tonic-gate 	ushort_t	p_perm;		/* Bits to turn on, off, assign */
17160Sstevel@tonic-gate 	uchar_t		p_op;		/* Operation: + - = */
17170Sstevel@tonic-gate 	uchar_t		p_special;	/* Special handling? */
17180Sstevel@tonic-gate }	PERMST;
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate #ifndef	S_ISVTX
17210Sstevel@tonic-gate #define	S_ISVTX	0			/* Not .1 */
17220Sstevel@tonic-gate #endif
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate /* Mask values */
17250Sstevel@tonic-gate #define	P_A	(S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* allbits */
17260Sstevel@tonic-gate #define	P_U	(S_ISUID|S_ISVTX|S_IRWXU)		/* user */
17270Sstevel@tonic-gate #define	P_G	(S_ISGID|S_ISVTX|S_IRWXG)		/* group */
17280Sstevel@tonic-gate #define	P_O	(S_ISVTX|S_IRWXO)			/* other */
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate static	int	iswho(int c);
17310Sstevel@tonic-gate static	int	isop(int c);
17320Sstevel@tonic-gate static	int	isperm(PERMST *pp, int c);
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate static	PERMST	machine[NPERM];		/* Permission construction machine */
17350Sstevel@tonic-gate static	PERMST	*endp;			/* Last used PERM structure */
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate static	uint_t	nowho;			/* No who for this mode (DOS kludge) */
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate /*
17400Sstevel@tonic-gate  * Read an ASCII string containing the symbolic/octal mode and
17410Sstevel@tonic-gate  * compile an automaton that recognizes it.  The return value
17420Sstevel@tonic-gate  * is NULL if everything is OK, otherwise it is -1.
17430Sstevel@tonic-gate  */
17440Sstevel@tonic-gate static int
readmode(ascmode)17450Sstevel@tonic-gate readmode(ascmode)
17460Sstevel@tonic-gate const char *ascmode;
17470Sstevel@tonic-gate {
17480Sstevel@tonic-gate 	const char *amode = ascmode;
17490Sstevel@tonic-gate 	PERMST *pp;
17500Sstevel@tonic-gate 	int seen_X;
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	nowho = 0;
17530Sstevel@tonic-gate 	seen_X = 0;
17540Sstevel@tonic-gate 	pp = &machine[0];
17550Sstevel@tonic-gate 	if (*amode >= '0' && *amode <= '7') {
17560Sstevel@tonic-gate 		int mode;
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 		mode = 0;
17590Sstevel@tonic-gate 		while (*amode >= '0' && *amode <= '7')
17600Sstevel@tonic-gate 			mode = (mode<<3) + *amode++ - '0';
17610Sstevel@tonic-gate 		if (*amode != '\0')
17620Sstevel@tonic-gate 			return (-1);
17630Sstevel@tonic-gate #if	S_ISUID != 04000 || S_ISGID != 02000 || \
17640Sstevel@tonic-gate 	S_IRUSR != 0400 || S_IWUSR != 0200 || S_IXUSR != 0100 || \
17650Sstevel@tonic-gate 	S_IRGRP != 0040 || S_IWGRP != 0020 || S_IXGRP != 0010 || \
17660Sstevel@tonic-gate 	S_IROTH != 0004 || S_IWOTH != 0002 || S_IXOTH != 0001
17670Sstevel@tonic-gate 		/*
17680Sstevel@tonic-gate 		 * There is no requirement of the octal mode bits being
17690Sstevel@tonic-gate 		 * the same as the S_ macros.
17700Sstevel@tonic-gate 		 */
17710Sstevel@tonic-gate 	{
17720Sstevel@tonic-gate 		mode_t mapping[] = {
17730Sstevel@tonic-gate 			S_IXOTH, S_IWOTH, S_IROTH,
17740Sstevel@tonic-gate 			S_IXGRP, S_IWGRP, S_IRGRP,
17750Sstevel@tonic-gate 			S_IXUSR, S_IWUSR, S_IRUSR,
17760Sstevel@tonic-gate 			S_ISGID, S_ISUID,
17770Sstevel@tonic-gate 			0
17780Sstevel@tonic-gate 		};
17790Sstevel@tonic-gate 		int i, newmode = 0;
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 		for (i = 0; mapping[i] != 0; i++)
17820Sstevel@tonic-gate 			if (mode & (1<<i))
17830Sstevel@tonic-gate 				newmode |= mapping[i];
17840Sstevel@tonic-gate 		mode = newmode;
17850Sstevel@tonic-gate 	}
17860Sstevel@tonic-gate #endif
17870Sstevel@tonic-gate 		pp->p_who = P_A;
17880Sstevel@tonic-gate 		pp->p_perm = mode;
17890Sstevel@tonic-gate 		pp->p_op = '=';
17900Sstevel@tonic-gate 	} else	for (;;) {
17910Sstevel@tonic-gate 		int t;
17920Sstevel@tonic-gate 		int who = 0;
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 		while ((t = iswho(*amode)) != 0) {
17950Sstevel@tonic-gate 			++amode;
17960Sstevel@tonic-gate 			who |= t;
17970Sstevel@tonic-gate 		}
17980Sstevel@tonic-gate 		if (who == 0) {
17990Sstevel@tonic-gate 			mode_t currmask;
18000Sstevel@tonic-gate 			(void) umask(currmask = umask((mode_t)0));
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 			/*
18030Sstevel@tonic-gate 			 * If no who specified, must use contents of
18040Sstevel@tonic-gate 			 * umask to determine which bits to flip.  This
18050Sstevel@tonic-gate 			 * is POSIX/V7/BSD behaviour, but not SVID.
18060Sstevel@tonic-gate 			 */
18070Sstevel@tonic-gate 			who = (~currmask)&P_A;
18080Sstevel@tonic-gate 			++nowho;
18090Sstevel@tonic-gate 		} else
18100Sstevel@tonic-gate 			nowho = 0;
18110Sstevel@tonic-gate 	samewho:
18120Sstevel@tonic-gate 		if (!isop(pp->p_op = *amode++))
18130Sstevel@tonic-gate 			return (-1);
18140Sstevel@tonic-gate 		pp->p_perm = 0;
18150Sstevel@tonic-gate 		pp->p_special = 0;
18160Sstevel@tonic-gate 		while ((t = isperm(pp, *amode)) != 0) {
18170Sstevel@tonic-gate 			if (pp->p_special == 'X') {
18180Sstevel@tonic-gate 				seen_X = 1;
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 				if (pp->p_perm != 0) {
18210Sstevel@tonic-gate 					ushort_t op;
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 					/*
18240Sstevel@tonic-gate 					 * Remember the 'who' for the previous
18250Sstevel@tonic-gate 					 * transformation.
18260Sstevel@tonic-gate 					 */
18270Sstevel@tonic-gate 					pp->p_who = who;
18280Sstevel@tonic-gate 					pp->p_special = 0;
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 					op = pp->p_op;
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate 					/* Keep 'X' separate */
18330Sstevel@tonic-gate 					++pp;
18340Sstevel@tonic-gate 					pp->p_special = 'X';
18350Sstevel@tonic-gate 					pp->p_op = op;
18360Sstevel@tonic-gate 				}
18370Sstevel@tonic-gate 			} else if (seen_X) {
18380Sstevel@tonic-gate 				ushort_t op;
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 				/* Remember the 'who' for the X */
18410Sstevel@tonic-gate 				pp->p_who = who;
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 				op = pp->p_op;
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 				/* Keep 'X' separate */
18460Sstevel@tonic-gate 				++pp;
18470Sstevel@tonic-gate 				pp->p_perm = 0;
18480Sstevel@tonic-gate 				pp->p_special = 0;
18490Sstevel@tonic-gate 				pp->p_op = op;
18500Sstevel@tonic-gate 			}
18510Sstevel@tonic-gate 			++amode;
18520Sstevel@tonic-gate 			pp->p_perm |= t;
18530Sstevel@tonic-gate 		}
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 		/*
18560Sstevel@tonic-gate 		 * These returned 0, but were actually parsed, so
18570Sstevel@tonic-gate 		 * don't look at them again.
18580Sstevel@tonic-gate 		 */
18590Sstevel@tonic-gate 		switch (pp->p_special) {
18600Sstevel@tonic-gate 		case 'u':
18610Sstevel@tonic-gate 		case 'g':
18620Sstevel@tonic-gate 		case 'o':
18630Sstevel@tonic-gate 			++amode;
18640Sstevel@tonic-gate 			break;
18650Sstevel@tonic-gate 		}
18660Sstevel@tonic-gate 		pp->p_who = who;
18670Sstevel@tonic-gate 		switch (*amode) {
18680Sstevel@tonic-gate 		case '\0':
18690Sstevel@tonic-gate 			break;
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 		case ',':
18720Sstevel@tonic-gate 			++amode;
18730Sstevel@tonic-gate 			++pp;
18740Sstevel@tonic-gate 			continue;
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 		default:
18770Sstevel@tonic-gate 			++pp;
18780Sstevel@tonic-gate 			goto samewho;
18790Sstevel@tonic-gate 		}
18800Sstevel@tonic-gate 		break;
18810Sstevel@tonic-gate 	}
18820Sstevel@tonic-gate 	endp = pp;
18830Sstevel@tonic-gate 	return (NULL);
18840Sstevel@tonic-gate }
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate /*
18870Sstevel@tonic-gate  * Given a character from the mode, return the associated
18880Sstevel@tonic-gate  * value as who (user designation) mask or 0 if this isn't valid.
18890Sstevel@tonic-gate  */
18900Sstevel@tonic-gate static int
iswho(c)18910Sstevel@tonic-gate iswho(c)
18920Sstevel@tonic-gate int c;
18930Sstevel@tonic-gate {
18940Sstevel@tonic-gate 	switch (c) {
18950Sstevel@tonic-gate 	case 'a':
18960Sstevel@tonic-gate 		return (P_A);
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	case 'u':
18990Sstevel@tonic-gate 		return (P_U);
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 	case 'g':
19020Sstevel@tonic-gate 		return (P_G);
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 	case 'o':
19050Sstevel@tonic-gate 		return (P_O);
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 	default:
19080Sstevel@tonic-gate 		return (0);
19090Sstevel@tonic-gate 	}
19100Sstevel@tonic-gate 	/* NOTREACHED */
19110Sstevel@tonic-gate }
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate /*
19140Sstevel@tonic-gate  * Return non-zero if this is a valid op code
19150Sstevel@tonic-gate  * in a symbolic mode.
19160Sstevel@tonic-gate  */
19170Sstevel@tonic-gate static int
isop(c)19180Sstevel@tonic-gate isop(c)
19190Sstevel@tonic-gate int c;
19200Sstevel@tonic-gate {
19210Sstevel@tonic-gate 	switch (c) {
19220Sstevel@tonic-gate 	case '+':
19230Sstevel@tonic-gate 	case '-':
19240Sstevel@tonic-gate 	case '=':
19250Sstevel@tonic-gate 		return (1);
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 	default:
19280Sstevel@tonic-gate 		return (0);
19290Sstevel@tonic-gate 	}
19300Sstevel@tonic-gate 	/* NOTREACHED */
19310Sstevel@tonic-gate }
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate /*
19340Sstevel@tonic-gate  * Return the permission bits implied by this character or 0
19350Sstevel@tonic-gate  * if it isn't valid.  Also returns 0 when the pseudo-permissions 'u', 'g', or
19360Sstevel@tonic-gate  * 'o' are used, and sets pp->p_special to the one used.
19370Sstevel@tonic-gate  */
19380Sstevel@tonic-gate static int
isperm(pp,c)19390Sstevel@tonic-gate isperm(pp, c)
19400Sstevel@tonic-gate PERMST *pp;
19410Sstevel@tonic-gate int c;
19420Sstevel@tonic-gate {
19430Sstevel@tonic-gate 	switch (c) {
19440Sstevel@tonic-gate 	case 'u':
19450Sstevel@tonic-gate 	case 'g':
19460Sstevel@tonic-gate 	case 'o':
19470Sstevel@tonic-gate 		pp->p_special = c;
19480Sstevel@tonic-gate 		return (0);
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 	case 'r':
19510Sstevel@tonic-gate 		return (S_IRUSR|S_IRGRP|S_IROTH);
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	case 'w':
19540Sstevel@tonic-gate 		return (S_IWUSR|S_IWGRP|S_IWOTH);
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 	case 'x':
19570Sstevel@tonic-gate 		return (S_IXUSR|S_IXGRP|S_IXOTH);
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate #if S_ISVTX != 0
19600Sstevel@tonic-gate 	case 't':
19610Sstevel@tonic-gate 		return (S_ISVTX);
19620Sstevel@tonic-gate #endif
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 	case 'X':
19650Sstevel@tonic-gate 		pp->p_special = 'X';
19660Sstevel@tonic-gate 		return (S_IXUSR|S_IXGRP|S_IXOTH);
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate #if S_ISVTX != 0
19690Sstevel@tonic-gate 	case 'a':
19700Sstevel@tonic-gate 		return (S_ISVTX);
19710Sstevel@tonic-gate #endif
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	case 'h':
19740Sstevel@tonic-gate 		return (S_ISUID);
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 	/*
19770Sstevel@tonic-gate 	 * This change makes:
19780Sstevel@tonic-gate 	 *	chmod +s file
19790Sstevel@tonic-gate 	 * set the system bit on dos but means that
19800Sstevel@tonic-gate 	 *	chmod u+s file
19810Sstevel@tonic-gate 	 *	chmod g+s file
19820Sstevel@tonic-gate 	 *	chmod a+s file
19830Sstevel@tonic-gate 	 * are all like UNIX.
19840Sstevel@tonic-gate 	 */
19850Sstevel@tonic-gate 	case 's':
19860Sstevel@tonic-gate 		return (nowho ? S_ISGID : S_ISGID|S_ISUID);
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	default:
19890Sstevel@tonic-gate 		return (0);
19900Sstevel@tonic-gate 	}
19910Sstevel@tonic-gate 	/* NOTREACHED */
19920Sstevel@tonic-gate }
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate /*
19950Sstevel@tonic-gate  * Execute the automaton that is created by readmode()
19960Sstevel@tonic-gate  * to generate the final mode that will be used.  This
19970Sstevel@tonic-gate  * code is passed a starting mode that is usually the original
19980Sstevel@tonic-gate  * mode of the file being changed (or 0).  Note that this mode must contain
19990Sstevel@tonic-gate  * the file-type bits as well, so that S_ISDIR will succeed on directories.
20000Sstevel@tonic-gate  */
20010Sstevel@tonic-gate static mode_t
getmode(mode_t startmode)20020Sstevel@tonic-gate getmode(mode_t startmode)
20030Sstevel@tonic-gate {
20040Sstevel@tonic-gate 	PERMST *pp;
20050Sstevel@tonic-gate 	mode_t temp;
20060Sstevel@tonic-gate 	mode_t perm;
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate 	for (pp = &machine[0]; pp <= endp; ++pp) {
20090Sstevel@tonic-gate 		perm = (mode_t)0;
20100Sstevel@tonic-gate 		/*
20110Sstevel@tonic-gate 		 * For the special modes 'u', 'g' and 'o', the named portion
20120Sstevel@tonic-gate 		 * of the mode refers to after the previous clause has been
20130Sstevel@tonic-gate 		 * processed, while the 'X' mode refers to the contents of the
20140Sstevel@tonic-gate 		 * mode before any clauses have been processed.
20150Sstevel@tonic-gate 		 *
20160Sstevel@tonic-gate 		 * References: P1003.2/D11.2, Section 4.7.7,
20170Sstevel@tonic-gate 		 *  lines 2568-2570, 2578-2583
20180Sstevel@tonic-gate 		 */
20190Sstevel@tonic-gate 		switch (pp->p_special) {
20200Sstevel@tonic-gate 		case 'u':
20210Sstevel@tonic-gate 			temp = startmode & S_IRWXU;
20220Sstevel@tonic-gate 			if (temp & (S_IRUSR|S_IRGRP|S_IROTH))
20230Sstevel@tonic-gate 				perm |= ((S_IRUSR|S_IRGRP|S_IROTH) &
20240Sstevel@tonic-gate 				    pp->p_who);
20250Sstevel@tonic-gate 			if (temp & (S_IWUSR|S_IWGRP|S_IWOTH))
20260Sstevel@tonic-gate 				perm |= ((S_IWUSR|S_IWGRP|S_IWOTH) & pp->p_who);
20270Sstevel@tonic-gate 			if (temp & (S_IXUSR|S_IXGRP|S_IXOTH))
20280Sstevel@tonic-gate 				perm |= ((S_IXUSR|S_IXGRP|S_IXOTH) & pp->p_who);
20290Sstevel@tonic-gate 			break;
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 		case 'g':
20320Sstevel@tonic-gate 			temp = startmode & S_IRWXG;
20330Sstevel@tonic-gate 			if (temp & (S_IRUSR|S_IRGRP|S_IROTH))
20340Sstevel@tonic-gate 				perm |= ((S_IRUSR|S_IRGRP|S_IROTH) & pp->p_who);
20350Sstevel@tonic-gate 			if (temp & (S_IWUSR|S_IWGRP|S_IWOTH))
20360Sstevel@tonic-gate 				perm |= ((S_IWUSR|S_IWGRP|S_IWOTH) & pp->p_who);
20370Sstevel@tonic-gate 			if (temp & (S_IXUSR|S_IXGRP|S_IXOTH))
20380Sstevel@tonic-gate 				perm |= ((S_IXUSR|S_IXGRP|S_IXOTH) & pp->p_who);
20390Sstevel@tonic-gate 			break;
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 		case 'o':
20420Sstevel@tonic-gate 			temp = startmode & S_IRWXO;
20430Sstevel@tonic-gate 			if (temp & (S_IRUSR|S_IRGRP|S_IROTH))
20440Sstevel@tonic-gate 				perm |= ((S_IRUSR|S_IRGRP|S_IROTH) & pp->p_who);
20450Sstevel@tonic-gate 			if (temp & (S_IWUSR|S_IWGRP|S_IWOTH))
20460Sstevel@tonic-gate 				perm |= ((S_IWUSR|S_IWGRP|S_IWOTH) & pp->p_who);
20470Sstevel@tonic-gate 			if (temp & (S_IXUSR|S_IXGRP|S_IXOTH))
20480Sstevel@tonic-gate 				perm |= ((S_IXUSR|S_IXGRP|S_IXOTH) & pp->p_who);
20490Sstevel@tonic-gate 			break;
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 		case 'X':
20520Sstevel@tonic-gate 			perm = pp->p_perm;
20530Sstevel@tonic-gate 			break;
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 		default:
20560Sstevel@tonic-gate 			perm = pp->p_perm;
20570Sstevel@tonic-gate 			break;
20580Sstevel@tonic-gate 		}
20590Sstevel@tonic-gate 		switch (pp->p_op) {
20600Sstevel@tonic-gate 		case '-':
20610Sstevel@tonic-gate 			startmode &= ~(perm & pp->p_who);
20620Sstevel@tonic-gate 			break;
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 		case '=':
20650Sstevel@tonic-gate 			startmode &= ~pp->p_who;
20660Sstevel@tonic-gate 			/* FALLTHROUGH */
20670Sstevel@tonic-gate 		case '+':
20680Sstevel@tonic-gate 			startmode |= (perm & pp->p_who);
20690Sstevel@tonic-gate 			break;
20700Sstevel@tonic-gate 		}
20710Sstevel@tonic-gate 	}
20720Sstevel@tonic-gate 	return (startmode);
20730Sstevel@tonic-gate }
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate /*
20760Sstevel@tonic-gate  * Returns the last component of a path name, unless it is
20770Sstevel@tonic-gate  * an absolute path, in which case it returns the whole path
20780Sstevel@tonic-gate  */
20790Sstevel@tonic-gate static char
gettail(char * fname)20800Sstevel@tonic-gate *gettail(char *fname)
20810Sstevel@tonic-gate {
20820Sstevel@tonic-gate 	char	*base = fname;
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	if (*fname != '/') {
20850Sstevel@tonic-gate 		if ((base = strrchr(fname, '/')) != NULL)
20860Sstevel@tonic-gate 			base++;
20870Sstevel@tonic-gate 		else
20880Sstevel@tonic-gate 			base = fname;
20890Sstevel@tonic-gate 	}
20900Sstevel@tonic-gate 	return (base);
20910Sstevel@tonic-gate }
2092