xref: /titanic_44/usr/src/cmd/find/find.c (revision ab823b7f933f787dbdf7a0bb790639210c583869)
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
5d35170d6Srm88369  * Common Development and Distribution License (the "License").
6d35170d6Srm88369  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22ef497ae3SRich Burridge  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
2303f45afcSYuri Pankov  * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
2405f32410SAndy Stormont  * Copyright (c) 2013 Andrew Stormont.  All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27da1a9cbeSjonb 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*	Parts of this product may be derived from		*/
337c478bd9Sstevel@tonic-gate /*	Mortice Kern Systems Inc. and Berkeley 4.3 BSD systems.	*/
347c478bd9Sstevel@tonic-gate /*	licensed from  Mortice Kern Systems Inc. and 		*/
357c478bd9Sstevel@tonic-gate /*	the University of California.				*/
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * Copyright 1985, 1990 by Mortice Kern Systems Inc.  All rights reserved.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <pwd.h>
447c478bd9Sstevel@tonic-gate #include <grp.h>
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <sys/param.h>
487c478bd9Sstevel@tonic-gate #include <sys/acl.h>
497c478bd9Sstevel@tonic-gate #include <limits.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <stdlib.h>
527c478bd9Sstevel@tonic-gate #include <locale.h>
537c478bd9Sstevel@tonic-gate #include <string.h>
547c478bd9Sstevel@tonic-gate #include <strings.h>
557c478bd9Sstevel@tonic-gate #include <ctype.h>
567c478bd9Sstevel@tonic-gate #include <wait.h>
577c478bd9Sstevel@tonic-gate #include <fnmatch.h>
587c478bd9Sstevel@tonic-gate #include <langinfo.h>
597c478bd9Sstevel@tonic-gate #include <ftw.h>
609ab6dc39Schin #include <libgen.h>
61b34cd89aSYuri Pankov #include <err.h>
62b34cd89aSYuri Pankov #include <regex.h>
633d63ea05Sas145665 #include "getresponse.h"
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	A_DAY		(long)(60*60*24)	/* a day full of seconds */
66da1a9cbeSjonb #define	A_MIN		(long)(60)
677c478bd9Sstevel@tonic-gate #define	BLKSIZ		512
687c478bd9Sstevel@tonic-gate #define	round(x, s)	(((x)+(s)-1)&~((s)-1))
697c478bd9Sstevel@tonic-gate #ifndef FTW_SLN
707c478bd9Sstevel@tonic-gate #define	FTW_SLN		7
717c478bd9Sstevel@tonic-gate #endif
727c478bd9Sstevel@tonic-gate #define	LINEBUF_SIZE		LINE_MAX	/* input or output lines */
737c478bd9Sstevel@tonic-gate #define	REMOTE_FS		"/etc/dfs/fstypes"
747c478bd9Sstevel@tonic-gate #define	N_FSTYPES		20
75d35170d6Srm88369 #define	SHELL_MAXARGS		253	/* see doexec() for description */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * This is the list of operations
797c478bd9Sstevel@tonic-gate  * F_USER and F_GROUP are named to avoid conflict with USER and GROUP defined
807c478bd9Sstevel@tonic-gate  * in sys/acl.h
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate enum Command
847c478bd9Sstevel@tonic-gate {
85b34cd89aSYuri Pankov 	PRINT,
86b34cd89aSYuri Pankov 	ACL, AMIN, AND, ATIME, CMIN, CPIO, CSIZE, CTIME, DEPTH, EXEC, F_GROUP,
8705f32410SAndy Stormont 	F_GROUPACL, F_USER, F_USERACL, FOLLOW, FSTYPE, INAME, INUM, IPATH,
8805f32410SAndy Stormont 	IREGEX,	LINKS, LOCAL, LPAREN, LS, MAXDEPTH, MINDEPTH, MMIN, MOUNT,
8905f32410SAndy Stormont 	MTIME, NAME, NCPIO, NEWER, NOGRP, NOT, NOUSER, OK, OR, PATH, PERM,
90*ab823b7fSPrasad Joshi 	PRINT0, PRUNE, REGEX, RPAREN, SIZE, TYPE, VARARGS, XATTR, DELETE
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate enum Type
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	Unary, Id, Num, Str, Exec, Cpio, Op
967c478bd9Sstevel@tonic-gate };
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate struct Args
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	char		name[10];
1017c478bd9Sstevel@tonic-gate 	enum Command	action;
1027c478bd9Sstevel@tonic-gate 	enum Type	type;
1037c478bd9Sstevel@tonic-gate };
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Except for pathnames, these are the only legal arguments
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate static struct Args commands[] =
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	"!",		NOT,		Op,
1117c478bd9Sstevel@tonic-gate 	"(",		LPAREN,		Unary,
1127c478bd9Sstevel@tonic-gate 	")",		RPAREN,		Unary,
1137c478bd9Sstevel@tonic-gate 	"-a",		AND,		Op,
114b34cd89aSYuri Pankov 	"-acl",		ACL,		Unary,
115da1a9cbeSjonb 	"-amin",	AMIN,		Num,
116b34cd89aSYuri Pankov 	"-and",		AND,		Op,
1177c478bd9Sstevel@tonic-gate 	"-atime",	ATIME,		Num,
118da1a9cbeSjonb 	"-cmin",	CMIN,		Num,
119b34cd89aSYuri Pankov 	"-cpio",	CPIO,		Cpio,
1207c478bd9Sstevel@tonic-gate 	"-ctime",	CTIME,		Num,
1217c478bd9Sstevel@tonic-gate 	"-depth",	DEPTH,		Unary,
122*ab823b7fSPrasad Joshi 	"-delete",	DELETE,		Unary,
1237c478bd9Sstevel@tonic-gate 	"-exec",	EXEC,		Exec,
1247c478bd9Sstevel@tonic-gate 	"-follow",	FOLLOW,		Unary,
125b34cd89aSYuri Pankov 	"-fstype",	FSTYPE,		Str,
1267c478bd9Sstevel@tonic-gate 	"-group",	F_GROUP,	Num,
127b34cd89aSYuri Pankov 	"-groupacl",	F_GROUPACL,	Num,
128b34cd89aSYuri Pankov 	"-iname",	INAME,		Str,
1297c478bd9Sstevel@tonic-gate 	"-inum",	INUM,		Num,
13005f32410SAndy Stormont 	"-ipath",	IPATH,		Str,
131b34cd89aSYuri Pankov 	"-iregex",	IREGEX,		Str,
13203f45afcSYuri Pankov 	"-links",	LINKS,		Num,
13303f45afcSYuri Pankov 	"-local",	LOCAL,		Unary,
134b34cd89aSYuri Pankov 	"-ls",		LS,		Unary,
135b34cd89aSYuri Pankov 	"-maxdepth",	MAXDEPTH,	Num,
136b34cd89aSYuri Pankov 	"-mindepth",	MINDEPTH,	Num,
137da1a9cbeSjonb 	"-mmin",	MMIN,		Num,
138b34cd89aSYuri Pankov 	"-mount",	MOUNT,		Unary,
1397c478bd9Sstevel@tonic-gate 	"-mtime",	MTIME,		Num,
1407c478bd9Sstevel@tonic-gate 	"-name",	NAME,		Str,
1417c478bd9Sstevel@tonic-gate 	"-ncpio",	NCPIO,		Cpio,
1427c478bd9Sstevel@tonic-gate 	"-newer",	NEWER,		Str,
143b34cd89aSYuri Pankov 	"-nogroup",	NOGRP,		Unary,
144b34cd89aSYuri Pankov 	"-not",		NOT,		Op,
145b34cd89aSYuri Pankov 	"-nouser",	NOUSER,		Unary,
1467c478bd9Sstevel@tonic-gate 	"-o",		OR,		Op,
1477c478bd9Sstevel@tonic-gate 	"-ok",		OK,		Exec,
148b34cd89aSYuri Pankov 	"-or",		OR,		Op,
14905f32410SAndy Stormont 	"-path",	PATH,		Str,
1507c478bd9Sstevel@tonic-gate 	"-perm",	PERM,		Num,
1517c478bd9Sstevel@tonic-gate 	"-print",	PRINT,		Unary,
152b34cd89aSYuri Pankov 	"-print0",	PRINT0,		Unary,
153b34cd89aSYuri Pankov 	"-prune",	PRUNE,		Unary,
154b34cd89aSYuri Pankov 	"-regex",	REGEX,		Str,
1557c478bd9Sstevel@tonic-gate 	"-size",	SIZE,		Num,
1567c478bd9Sstevel@tonic-gate 	"-type",	TYPE,		Num,
1577c478bd9Sstevel@tonic-gate 	"-user",	F_USER,		Num,
158b34cd89aSYuri Pankov 	"-useracl",	F_USERACL,	Num,
1597c478bd9Sstevel@tonic-gate 	"-xattr",	XATTR,		Unary,
160b34cd89aSYuri Pankov 	"-xdev",	MOUNT,		Unary,
1617c478bd9Sstevel@tonic-gate 	NULL,		0,		0
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate union Item
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	struct Node	*np;
1677c478bd9Sstevel@tonic-gate 	struct Arglist	*vp;
1687c478bd9Sstevel@tonic-gate 	time_t		t;
1697c478bd9Sstevel@tonic-gate 	char		*cp;
1707c478bd9Sstevel@tonic-gate 	char		**ap;
1717c478bd9Sstevel@tonic-gate 	long		l;
1727c478bd9Sstevel@tonic-gate 	int		i;
1737c478bd9Sstevel@tonic-gate 	long long	ll;
1747c478bd9Sstevel@tonic-gate };
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate struct Node
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	struct Node	*next;
1797c478bd9Sstevel@tonic-gate 	enum Command	action;
1807c478bd9Sstevel@tonic-gate 	enum Type	type;
1817c478bd9Sstevel@tonic-gate 	union Item	first;
1827c478bd9Sstevel@tonic-gate 	union Item	second;
1837c478bd9Sstevel@tonic-gate };
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /* if no -print, -exec or -ok replace "expression" with "(expression) -print" */
1867c478bd9Sstevel@tonic-gate static	struct	Node PRINT_NODE = { 0, PRINT, 0, 0};
1877c478bd9Sstevel@tonic-gate static	struct	Node LPAREN_NODE = { 0, LPAREN, 0, 0};
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * Prototype variable size arglist buffer
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate struct Arglist
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	struct Arglist	*next;
1977c478bd9Sstevel@tonic-gate 	char		*end;
1987c478bd9Sstevel@tonic-gate 	char		*nextstr;
1997c478bd9Sstevel@tonic-gate 	char		**firstvar;
2007c478bd9Sstevel@tonic-gate 	char		**nextvar;
2017c478bd9Sstevel@tonic-gate 	char		*arglist[1];
2027c478bd9Sstevel@tonic-gate };
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static int		compile();
2067c478bd9Sstevel@tonic-gate static int		execute();
207d35170d6Srm88369 static int		doexec(char *, char **, int *);
208*ab823b7fSPrasad Joshi static int		dodelete(char *, struct stat *, struct FTW *);
2097c478bd9Sstevel@tonic-gate static struct Args	*lookup();
2107c478bd9Sstevel@tonic-gate static int		ok();
2116c83d09fSrobbin static void		usage(void)	__NORETURN;
2127c478bd9Sstevel@tonic-gate static struct Arglist	*varargs();
2137c478bd9Sstevel@tonic-gate static int		list();
2147c478bd9Sstevel@tonic-gate static char		*getgroup();
2157c478bd9Sstevel@tonic-gate static FILE		*cmdopen();
2167c478bd9Sstevel@tonic-gate static int		cmdclose();
2177c478bd9Sstevel@tonic-gate static char		*getshell();
2187c478bd9Sstevel@tonic-gate static void 		init_remote_fs();
2197c478bd9Sstevel@tonic-gate static char		*getname();
2207c478bd9Sstevel@tonic-gate static int		readmode();
2217c478bd9Sstevel@tonic-gate static mode_t		getmode();
2227c478bd9Sstevel@tonic-gate static char		*gettail();
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 
22568a94df1Scf46844 static int walkflags = FTW_CHDIR|FTW_PHYS|FTW_ANYERR|FTW_NOLOOP;
2267c478bd9Sstevel@tonic-gate static struct Node	*topnode;
2277c478bd9Sstevel@tonic-gate static struct Node	*freenode;	/* next free node we may use later */
2287c478bd9Sstevel@tonic-gate static char		*cpio[] = { "cpio", "-o", 0 };
2297c478bd9Sstevel@tonic-gate static char		*ncpio[] = { "cpio", "-oc", 0 };
2307c478bd9Sstevel@tonic-gate static char		*cpiol[] = { "cpio", "-oL", 0 };
2317c478bd9Sstevel@tonic-gate static char		*ncpiol[] = { "cpio", "-ocL", 0 };
2327c478bd9Sstevel@tonic-gate static time_t		now;
2337c478bd9Sstevel@tonic-gate static FILE		*output;
2347c478bd9Sstevel@tonic-gate static char		*dummyarg = (char *)-1;
2357c478bd9Sstevel@tonic-gate static int		lastval;
2367c478bd9Sstevel@tonic-gate static int		varsize;
2377c478bd9Sstevel@tonic-gate static struct Arglist	*lastlist;
2387c478bd9Sstevel@tonic-gate static char		*cmdname;
2397c478bd9Sstevel@tonic-gate static char		*remote_fstypes[N_FSTYPES+1];
2407c478bd9Sstevel@tonic-gate static int		fstype_index = 0;
2417c478bd9Sstevel@tonic-gate static int		action_expression = 0;	/* -print, -exec, or -ok */
2427c478bd9Sstevel@tonic-gate static int		error = 0;
2437c478bd9Sstevel@tonic-gate static int		paren_cnt = 0;	/* keeps track of parentheses */
244b34cd89aSYuri Pankov static int		Eflag = 0;
2457c478bd9Sstevel@tonic-gate static int		hflag = 0;
2467c478bd9Sstevel@tonic-gate static int		lflag = 0;
247d35170d6Srm88369 /* set when doexec()-invoked utility returns non-zero */
248d35170d6Srm88369 static int		exec_exitcode = 0;
249b34cd89aSYuri Pankov static regex_t		*preg = NULL;
250b34cd89aSYuri Pankov static int		npreg = 0;
251b34cd89aSYuri Pankov static int		mindepth = -1, maxdepth = -1;
2527c478bd9Sstevel@tonic-gate extern char		**environ;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)2557c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	char *cp;
2587c478bd9Sstevel@tonic-gate 	int c;
2597c478bd9Sstevel@tonic-gate 	int paths;
2607c478bd9Sstevel@tonic-gate 	char *cwdpath;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2637c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2647c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2657c478bd9Sstevel@tonic-gate #endif
2667c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	cmdname = argv[0];
2697c478bd9Sstevel@tonic-gate 	if (time(&now) == (time_t)(-1)) {
2707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: time() %s\n"),
2717c478bd9Sstevel@tonic-gate 		    cmdname, strerror(errno));
2727c478bd9Sstevel@tonic-gate 		exit(1);
2737c478bd9Sstevel@tonic-gate 	}
274b34cd89aSYuri Pankov 	while ((c = getopt(argc, argv, "EHL")) != -1) {
2757c478bd9Sstevel@tonic-gate 		switch (c) {
276b34cd89aSYuri Pankov 		case 'E':
277b34cd89aSYuri Pankov 			Eflag = 1;
278b34cd89aSYuri Pankov 			break;
2797c478bd9Sstevel@tonic-gate 		case 'H':
2807c478bd9Sstevel@tonic-gate 			hflag = 1;
2817c478bd9Sstevel@tonic-gate 			lflag = 0;
2827c478bd9Sstevel@tonic-gate 			break;
2837c478bd9Sstevel@tonic-gate 		case 'L':
2847c478bd9Sstevel@tonic-gate 			hflag = 0;
2857c478bd9Sstevel@tonic-gate 			lflag = 1;
2867c478bd9Sstevel@tonic-gate 			break;
2877c478bd9Sstevel@tonic-gate 		case '?':
2887c478bd9Sstevel@tonic-gate 			usage();
2897c478bd9Sstevel@tonic-gate 			break;
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	argc -= optind;
2947c478bd9Sstevel@tonic-gate 	argv += optind;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if (argc < 1) {
2977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2987c478bd9Sstevel@tonic-gate 		    gettext("%s: insufficient number of arguments\n"), cmdname);
2997c478bd9Sstevel@tonic-gate 		usage();
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	for (paths = 0; (cp = argv[paths]) != 0; ++paths) {
3037c478bd9Sstevel@tonic-gate 		if (*cp == '-')
3047c478bd9Sstevel@tonic-gate 			break;
3057c478bd9Sstevel@tonic-gate 		else if ((*cp == '!' || *cp == '(') && *(cp+1) == 0)
3067c478bd9Sstevel@tonic-gate 			break;
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (paths == 0) /* no path-list */
3107c478bd9Sstevel@tonic-gate 		usage();
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	output = stdout;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/* lflag is the same as -follow */
3157c478bd9Sstevel@tonic-gate 	if (lflag)
3167c478bd9Sstevel@tonic-gate 		walkflags &= ~FTW_PHYS;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/* allocate enough space for the compiler */
3197c478bd9Sstevel@tonic-gate 	topnode = malloc((argc + 1) * sizeof (struct Node));
3207c478bd9Sstevel@tonic-gate 	(void) memset(topnode, 0, (argc + 1) * sizeof (struct Node));
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (compile(argv + paths, topnode, &action_expression) == 0) {
3237c478bd9Sstevel@tonic-gate 		/* no expression, default to -print */
3247c478bd9Sstevel@tonic-gate 		(void) memcpy(topnode, &PRINT_NODE, sizeof (struct Node));
3257c478bd9Sstevel@tonic-gate 	} else if (!action_expression) {
3267c478bd9Sstevel@tonic-gate 		/*
3277c478bd9Sstevel@tonic-gate 		 * if no action expression, insert an LPAREN node above topnode,
3287c478bd9Sstevel@tonic-gate 		 * with a PRINT node as its next node
3297c478bd9Sstevel@tonic-gate 		 */
3307c478bd9Sstevel@tonic-gate 		struct Node *savenode;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		if (freenode == NULL) {
3337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: can't append -print"
3347c478bd9Sstevel@tonic-gate 			    " implicitly; try explicit -print option\n"),
3357c478bd9Sstevel@tonic-gate 			    cmdname);
3367c478bd9Sstevel@tonic-gate 			exit(1);
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 		savenode = topnode;
3397c478bd9Sstevel@tonic-gate 		topnode = freenode++;
3407c478bd9Sstevel@tonic-gate 		(void) memcpy(topnode, &LPAREN_NODE, sizeof (struct Node));
3417c478bd9Sstevel@tonic-gate 		topnode->next = freenode;
3427c478bd9Sstevel@tonic-gate 		topnode->first.np = savenode;
3437c478bd9Sstevel@tonic-gate 		(void) memcpy(topnode->next, &PRINT_NODE, sizeof (struct Node));
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	while (paths--) {
3477c478bd9Sstevel@tonic-gate 		char *curpath;
3487c478bd9Sstevel@tonic-gate 		struct stat sb;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		curpath = *(argv++);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 		/*
3537c478bd9Sstevel@tonic-gate 		 * If -H is specified, it means we walk the first
3547c478bd9Sstevel@tonic-gate 		 * level (pathname on command line) logically, following
3557c478bd9Sstevel@tonic-gate 		 * symlinks, but lower levels are walked physically.
3567c478bd9Sstevel@tonic-gate 		 * We use our own secret interface to nftw() to change
3577c478bd9Sstevel@tonic-gate 		 * the from stat to lstat after the top level is walked.
3587c478bd9Sstevel@tonic-gate 		 */
3597c478bd9Sstevel@tonic-gate 		if (hflag) {
3607c478bd9Sstevel@tonic-gate 			if (stat(curpath, &sb) < 0 && errno == ENOENT)
3617c478bd9Sstevel@tonic-gate 				walkflags &= ~FTW_HOPTION;
3627c478bd9Sstevel@tonic-gate 			else
3637c478bd9Sstevel@tonic-gate 				walkflags |= FTW_HOPTION;
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		/*
3677c478bd9Sstevel@tonic-gate 		 * We need this check as nftw needs a CWD and we have no
3687c478bd9Sstevel@tonic-gate 		 * way of returning back from that code with a meaningful
3697c478bd9Sstevel@tonic-gate 		 * error related to this
3707c478bd9Sstevel@tonic-gate 		 */
3717c478bd9Sstevel@tonic-gate 		if ((cwdpath = getcwd(NULL, PATH_MAX)) == NULL) {
3724b808d43SRich Burridge 			if ((errno == EACCES) && (walkflags & FTW_CHDIR)) {
3734b808d43SRich Burridge 				/*
3744b808d43SRich Burridge 				 * A directory above cwd is inaccessible,
3754b808d43SRich Burridge 				 * so don't do chdir(2)s. Slower, but at least
3764b808d43SRich Burridge 				 * it works.
3774b808d43SRich Burridge 				 */
3784b808d43SRich Burridge 				walkflags &= ~FTW_CHDIR;
3794b808d43SRich Burridge 				free(cwdpath);
3804b808d43SRich Burridge 			} else {
3817c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3824b808d43SRich Burridge 				    gettext("%s : cannot get the current "
3834b808d43SRich Burridge 				    "working directory\n"), cmdname);
3847c478bd9Sstevel@tonic-gate 				exit(1);
3854b808d43SRich Burridge 			}
3867c478bd9Sstevel@tonic-gate 		} else
3877c478bd9Sstevel@tonic-gate 			free(cwdpath);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		if (nftw(curpath, execute, 1000, walkflags)) {
3917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3927c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot open %s: %s\n"),
3937c478bd9Sstevel@tonic-gate 			    cmdname, curpath, strerror(errno));
3947c478bd9Sstevel@tonic-gate 			error = 1;
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	/* execute any remaining variable length lists */
4007c478bd9Sstevel@tonic-gate 	while (lastlist) {
4017c478bd9Sstevel@tonic-gate 		if (lastlist->end != lastlist->nextstr) {
4027c478bd9Sstevel@tonic-gate 			*lastlist->nextvar = 0;
403d35170d6Srm88369 			(void) doexec((char *)0, lastlist->arglist,
404d35170d6Srm88369 			    &exec_exitcode);
4057c478bd9Sstevel@tonic-gate 		}
4067c478bd9Sstevel@tonic-gate 		lastlist = lastlist->next;
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	if (output != stdout)
4097c478bd9Sstevel@tonic-gate 		return (cmdclose(output));
410d35170d6Srm88369 	return ((exec_exitcode != 0) ? exec_exitcode : error);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate /*
4147c478bd9Sstevel@tonic-gate  * compile the arguments
4157c478bd9Sstevel@tonic-gate  */
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate static int
compile(argv,np,actionp)4187c478bd9Sstevel@tonic-gate compile(argv, np, actionp)
4197c478bd9Sstevel@tonic-gate char **argv;
4207c478bd9Sstevel@tonic-gate struct Node *np;
4217c478bd9Sstevel@tonic-gate int *actionp;
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	char *b;
4247c478bd9Sstevel@tonic-gate 	char **av;
4257c478bd9Sstevel@tonic-gate 	struct Node *oldnp = topnode;
4267c478bd9Sstevel@tonic-gate 	struct Args *argp;
4277c478bd9Sstevel@tonic-gate 	char **com;
4287c478bd9Sstevel@tonic-gate 	int i;
4297c478bd9Sstevel@tonic-gate 	enum Command wasop = PRINT;
4307c478bd9Sstevel@tonic-gate 
4313d63ea05Sas145665 	if (init_yes() < 0) {
4323d63ea05Sas145665 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
4333d63ea05Sas145665 		    strerror(errno));
4343d63ea05Sas145665 		exit(1);
4353d63ea05Sas145665 	}
4363d63ea05Sas145665 
4377c478bd9Sstevel@tonic-gate 	for (av = argv; *av && (argp = lookup(*av)); av++) {
4387c478bd9Sstevel@tonic-gate 		np->next = 0;
4397c478bd9Sstevel@tonic-gate 		np->action = argp->action;
4407c478bd9Sstevel@tonic-gate 		np->type = argp->type;
4417c478bd9Sstevel@tonic-gate 		np->second.i = 0;
4427c478bd9Sstevel@tonic-gate 		if (argp->type == Op) {
4437c478bd9Sstevel@tonic-gate 			if (wasop == NOT || (wasop && np->action != NOT)) {
4447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4457c478bd9Sstevel@tonic-gate 				gettext("%s: operand follows operand\n"),
4467c478bd9Sstevel@tonic-gate 						cmdname);
4477c478bd9Sstevel@tonic-gate 				exit(1);
4487c478bd9Sstevel@tonic-gate 			}
4497c478bd9Sstevel@tonic-gate 			if (np->action != NOT && oldnp == 0)
4507c478bd9Sstevel@tonic-gate 				goto err;
4517c478bd9Sstevel@tonic-gate 			wasop = argp->action;
4527c478bd9Sstevel@tonic-gate 		} else {
4537c478bd9Sstevel@tonic-gate 			wasop = PRINT;
4547c478bd9Sstevel@tonic-gate 			if (argp->type != Unary) {
4557c478bd9Sstevel@tonic-gate 				if (!(b = *++av)) {
4567c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
4577c478bd9Sstevel@tonic-gate 					gettext("%s: incomplete statement\n"),
4587c478bd9Sstevel@tonic-gate 							cmdname);
4597c478bd9Sstevel@tonic-gate 					exit(1);
4607c478bd9Sstevel@tonic-gate 				}
4617c478bd9Sstevel@tonic-gate 				if (argp->type == Num) {
462b34cd89aSYuri Pankov 					if (((argp->action == MAXDEPTH) ||
463b34cd89aSYuri Pankov 					    (argp->action == MINDEPTH)) &&
464b34cd89aSYuri Pankov 					    ((int)strtol(b, (char **)NULL,
465b34cd89aSYuri Pankov 					    10) < 0))
466b34cd89aSYuri Pankov 						errx(1,
467b34cd89aSYuri Pankov 					gettext("%s: value must be positive"),
468b34cd89aSYuri Pankov 						    (argp->action == MAXDEPTH) ?
469b34cd89aSYuri Pankov 						    "maxdepth" : "mindepth");
4707c478bd9Sstevel@tonic-gate 					if ((argp->action != PERM) ||
4717c478bd9Sstevel@tonic-gate 					    (*b != '+')) {
4727c478bd9Sstevel@tonic-gate 						if (*b == '+' || *b == '-') {
4737c478bd9Sstevel@tonic-gate 							np->second.i = *b;
4747c478bd9Sstevel@tonic-gate 							b++;
4757c478bd9Sstevel@tonic-gate 						}
4767c478bd9Sstevel@tonic-gate 					}
4777c478bd9Sstevel@tonic-gate 				}
4787c478bd9Sstevel@tonic-gate 			}
4797c478bd9Sstevel@tonic-gate 		}
4807c478bd9Sstevel@tonic-gate 		switch (argp->action) {
4817c478bd9Sstevel@tonic-gate 		case AND:
4827c478bd9Sstevel@tonic-gate 			break;
4837c478bd9Sstevel@tonic-gate 		case NOT:
4847c478bd9Sstevel@tonic-gate 			break;
4857c478bd9Sstevel@tonic-gate 		case OR:
4867c478bd9Sstevel@tonic-gate 			np->first.np = topnode;
4877c478bd9Sstevel@tonic-gate 			topnode = np;
4887c478bd9Sstevel@tonic-gate 			oldnp->next = 0;
4897c478bd9Sstevel@tonic-gate 			break;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 		case LPAREN: {
4927c478bd9Sstevel@tonic-gate 			struct Node *save = topnode;
4937c478bd9Sstevel@tonic-gate 			topnode = np+1;
4947c478bd9Sstevel@tonic-gate 			paren_cnt++;
4957c478bd9Sstevel@tonic-gate 			i = compile(++av, topnode, actionp);
4967c478bd9Sstevel@tonic-gate 			np->first.np = topnode;
4977c478bd9Sstevel@tonic-gate 			topnode = save;
4987c478bd9Sstevel@tonic-gate 			av += i;
4997c478bd9Sstevel@tonic-gate 			oldnp = np;
5007c478bd9Sstevel@tonic-gate 			np += i + 1;
5017c478bd9Sstevel@tonic-gate 			oldnp->next = np;
5027c478bd9Sstevel@tonic-gate 			continue;
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		case RPAREN:
5067c478bd9Sstevel@tonic-gate 			if (paren_cnt <= 0) {
5077c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5087c478bd9Sstevel@tonic-gate 				    gettext("%s: unmatched ')'\n"),
5097c478bd9Sstevel@tonic-gate 				    cmdname);
5107c478bd9Sstevel@tonic-gate 				exit(1);
5117c478bd9Sstevel@tonic-gate 			}
5127c478bd9Sstevel@tonic-gate 			paren_cnt--;
5137c478bd9Sstevel@tonic-gate 			if (oldnp == 0)
5147c478bd9Sstevel@tonic-gate 				goto err;
5157c478bd9Sstevel@tonic-gate 			if (oldnp->type == Op) {
5167c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5177c478bd9Sstevel@tonic-gate 				    gettext("%s: cannot immediately"
5187c478bd9Sstevel@tonic-gate 				    " follow an operand with ')'\n"),
5197c478bd9Sstevel@tonic-gate 				    cmdname);
5207c478bd9Sstevel@tonic-gate 				exit(1);
5217c478bd9Sstevel@tonic-gate 			}
5227c478bd9Sstevel@tonic-gate 			oldnp->next = 0;
5237c478bd9Sstevel@tonic-gate 			return (av-argv);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		case FOLLOW:
5267c478bd9Sstevel@tonic-gate 			walkflags &= ~FTW_PHYS;
5277c478bd9Sstevel@tonic-gate 			break;
5287c478bd9Sstevel@tonic-gate 		case MOUNT:
5297c478bd9Sstevel@tonic-gate 			walkflags |= FTW_MOUNT;
5307c478bd9Sstevel@tonic-gate 			break;
5317c478bd9Sstevel@tonic-gate 		case DEPTH:
5327c478bd9Sstevel@tonic-gate 			walkflags |= FTW_DEPTH;
5337c478bd9Sstevel@tonic-gate 			break;
534*ab823b7fSPrasad Joshi 		case DELETE:
535*ab823b7fSPrasad Joshi 			walkflags |= (FTW_DEPTH | FTW_PHYS);
536*ab823b7fSPrasad Joshi 			walkflags &= ~FTW_CHDIR;
537*ab823b7fSPrasad Joshi 			(*actionp)++;
538*ab823b7fSPrasad Joshi 			break;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 		case LOCAL:
5417c478bd9Sstevel@tonic-gate 			np->first.l = 0L;
5427c478bd9Sstevel@tonic-gate 			np->first.ll = 0LL;
5437c478bd9Sstevel@tonic-gate 			np->second.i = '+';
5447c478bd9Sstevel@tonic-gate 			/*
5457c478bd9Sstevel@tonic-gate 			 * Make it compatible to df -l for
5467c478bd9Sstevel@tonic-gate 			 * future enhancement. So, anything
5477c478bd9Sstevel@tonic-gate 			 * that is not remote, then it is
5487c478bd9Sstevel@tonic-gate 			 * local.
5497c478bd9Sstevel@tonic-gate 			 */
5507c478bd9Sstevel@tonic-gate 			init_remote_fs();
5517c478bd9Sstevel@tonic-gate 			break;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 		case SIZE:
5547c478bd9Sstevel@tonic-gate 			if (b[strlen(b)-1] == 'c')
5557c478bd9Sstevel@tonic-gate 				np->action = CSIZE;
5567c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
5577c478bd9Sstevel@tonic-gate 		case INUM:
5587c478bd9Sstevel@tonic-gate 			np->first.ll = atoll(b);
5597c478bd9Sstevel@tonic-gate 			break;
5607c478bd9Sstevel@tonic-gate 
561da1a9cbeSjonb 		case CMIN:
5627c478bd9Sstevel@tonic-gate 		case CTIME:
563da1a9cbeSjonb 		case MMIN:
5647c478bd9Sstevel@tonic-gate 		case MTIME:
565da1a9cbeSjonb 		case AMIN:
5667c478bd9Sstevel@tonic-gate 		case ATIME:
5677c478bd9Sstevel@tonic-gate 		case LINKS:
5687c478bd9Sstevel@tonic-gate 			np->first.l = atol(b);
5697c478bd9Sstevel@tonic-gate 			break;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		case F_USER:
572b34cd89aSYuri Pankov 		case F_GROUP:
573b34cd89aSYuri Pankov 		case F_USERACL:
574b34cd89aSYuri Pankov 		case F_GROUPACL: {
5757c478bd9Sstevel@tonic-gate 			struct	passwd	*pw;
5767c478bd9Sstevel@tonic-gate 			struct	group *gr;
577ef497ae3SRich Burridge 			long value;
578ef497ae3SRich Burridge 			char *q;
579ef497ae3SRich Burridge 
580ef497ae3SRich Burridge 			value = -1;
581b34cd89aSYuri Pankov 			if (argp->action == F_USER ||
582b34cd89aSYuri Pankov 			    argp->action == F_USERACL) {
5837c478bd9Sstevel@tonic-gate 				if ((pw = getpwnam(b)) != 0)
584ef497ae3SRich Burridge 					value = (long)pw->pw_uid;
5857c478bd9Sstevel@tonic-gate 			} else {
5867c478bd9Sstevel@tonic-gate 				if ((gr = getgrnam(b)) != 0)
587ef497ae3SRich Burridge 					value = (long)gr->gr_gid;
5887c478bd9Sstevel@tonic-gate 			}
589ef497ae3SRich Burridge 			if (value == -1) {
590ef497ae3SRich Burridge 				errno = 0;
591ef497ae3SRich Burridge 				value = strtol(b, &q, 10);
592ef497ae3SRich Burridge 				if (errno != 0 || q == b || *q != '\0') {
5937c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
5947c478bd9Sstevel@tonic-gate 					    "%s: cannot find %s name\n"),
5957c478bd9Sstevel@tonic-gate 						cmdname, *av);
5967c478bd9Sstevel@tonic-gate 					exit(1);
5977c478bd9Sstevel@tonic-gate 				}
5987c478bd9Sstevel@tonic-gate 			}
599ef497ae3SRich Burridge 			np->first.l = value;
6007c478bd9Sstevel@tonic-gate 			break;
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		case EXEC:
6047c478bd9Sstevel@tonic-gate 		case OK:
6057c478bd9Sstevel@tonic-gate 			walkflags &= ~FTW_CHDIR;
6067c478bd9Sstevel@tonic-gate 			np->first.ap = av;
6077c478bd9Sstevel@tonic-gate 			(*actionp)++;
6087c478bd9Sstevel@tonic-gate 			for (;;) {
6097c478bd9Sstevel@tonic-gate 				if ((b = *av) == 0) {
6107c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6117c478bd9Sstevel@tonic-gate 					gettext("%s: incomplete statement\n"),
6127c478bd9Sstevel@tonic-gate 						cmdname);
6137c478bd9Sstevel@tonic-gate 					exit(1);
6147c478bd9Sstevel@tonic-gate 				}
6157c478bd9Sstevel@tonic-gate 				if (strcmp(b, ";") == 0) {
6167c478bd9Sstevel@tonic-gate 					*av = 0;
6177c478bd9Sstevel@tonic-gate 					break;
6187c478bd9Sstevel@tonic-gate 				} else if (strcmp(b, "{}") == 0)
6197c478bd9Sstevel@tonic-gate 					*av = dummyarg;
6207c478bd9Sstevel@tonic-gate 				else if (strcmp(b, "+") == 0 &&
6217c478bd9Sstevel@tonic-gate 					av[-1] == dummyarg &&
6227c478bd9Sstevel@tonic-gate 					np->action == EXEC) {
6237c478bd9Sstevel@tonic-gate 					av[-1] = 0;
6247c478bd9Sstevel@tonic-gate 					np->first.vp = varargs(np->first.ap);
6257c478bd9Sstevel@tonic-gate 					np->action = VARARGS;
6267c478bd9Sstevel@tonic-gate 					break;
6277c478bd9Sstevel@tonic-gate 				}
6287c478bd9Sstevel@tonic-gate 				av++;
6297c478bd9Sstevel@tonic-gate 			}
6307c478bd9Sstevel@tonic-gate 			break;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 		case NAME:
633b34cd89aSYuri Pankov 		case INAME:
63405f32410SAndy Stormont 		case PATH:
63505f32410SAndy Stormont 		case IPATH:
6367c478bd9Sstevel@tonic-gate 			np->first.cp = b;
6377c478bd9Sstevel@tonic-gate 			break;
638b34cd89aSYuri Pankov 		case REGEX:
639b34cd89aSYuri Pankov 		case IREGEX: {
640b34cd89aSYuri Pankov 			int error;
641b34cd89aSYuri Pankov 			size_t errlen;
642b34cd89aSYuri Pankov 			char *errmsg;
643b34cd89aSYuri Pankov 
644b34cd89aSYuri Pankov 			if ((preg = realloc(preg, (npreg + 1) *
645b34cd89aSYuri Pankov 			    sizeof (regex_t))) == NULL)
646b34cd89aSYuri Pankov 				err(1, "realloc");
647b34cd89aSYuri Pankov 			if ((error = regcomp(&preg[npreg], b,
648b34cd89aSYuri Pankov 			    ((np->action == IREGEX) ? REG_ICASE : 0) |
649b34cd89aSYuri Pankov 			    ((Eflag) ? REG_EXTENDED : 0))) != 0) {
650b34cd89aSYuri Pankov 				errlen = regerror(error, &preg[npreg], NULL, 0);
651b34cd89aSYuri Pankov 				if ((errmsg = malloc(errlen)) == NULL)
652b34cd89aSYuri Pankov 					err(1, "malloc");
653b34cd89aSYuri Pankov 				(void) regerror(error, &preg[npreg], errmsg,
654b34cd89aSYuri Pankov 				    errlen);
655b34cd89aSYuri Pankov 				errx(1, gettext("RE error: %s"), errmsg);
656b34cd89aSYuri Pankov 			}
657b34cd89aSYuri Pankov 			npreg++;
658b34cd89aSYuri Pankov 			break;
659b34cd89aSYuri Pankov 		}
6607c478bd9Sstevel@tonic-gate 		case PERM:
6617c478bd9Sstevel@tonic-gate 			if (*b == '-')
6627c478bd9Sstevel@tonic-gate 				++b;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 			if (readmode(b) != NULL) {
6657c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
6667c478bd9Sstevel@tonic-gate 				    "find: -perm: Bad permission string\n"));
6677c478bd9Sstevel@tonic-gate 				usage();
6687c478bd9Sstevel@tonic-gate 			}
6697c478bd9Sstevel@tonic-gate 			np->first.l = (long)getmode((mode_t)0);
6707c478bd9Sstevel@tonic-gate 			break;
6717c478bd9Sstevel@tonic-gate 		case TYPE:
6727c478bd9Sstevel@tonic-gate 			i = *b;
6737c478bd9Sstevel@tonic-gate 			np->first.l =
6747c478bd9Sstevel@tonic-gate 			    i == 'd' ? S_IFDIR :
6757c478bd9Sstevel@tonic-gate 			    i == 'b' ? S_IFBLK :
6767c478bd9Sstevel@tonic-gate 			    i == 'c' ? S_IFCHR :
6777c478bd9Sstevel@tonic-gate #ifdef S_IFIFO
6787c478bd9Sstevel@tonic-gate 			    i == 'p' ? S_IFIFO :
6797c478bd9Sstevel@tonic-gate #endif
6807c478bd9Sstevel@tonic-gate 			    i == 'f' ? S_IFREG :
6817c478bd9Sstevel@tonic-gate #ifdef S_IFLNK
6827c478bd9Sstevel@tonic-gate 			    i == 'l' ? S_IFLNK :
6837c478bd9Sstevel@tonic-gate #endif
6847c478bd9Sstevel@tonic-gate #ifdef S_IFSOCK
6857c478bd9Sstevel@tonic-gate 			    i == 's' ? S_IFSOCK :
6867c478bd9Sstevel@tonic-gate #endif
6877c478bd9Sstevel@tonic-gate #ifdef S_IFDOOR
6887c478bd9Sstevel@tonic-gate 			    i == 'D' ? S_IFDOOR :
6897c478bd9Sstevel@tonic-gate #endif
6907c478bd9Sstevel@tonic-gate 			    0;
6917c478bd9Sstevel@tonic-gate 			break;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		case CPIO:
6947c478bd9Sstevel@tonic-gate 			if (walkflags & FTW_PHYS)
6957c478bd9Sstevel@tonic-gate 				com = cpio;
6967c478bd9Sstevel@tonic-gate 			else
6977c478bd9Sstevel@tonic-gate 				com = cpiol;
6987c478bd9Sstevel@tonic-gate 			goto common;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		case NCPIO: {
7017c478bd9Sstevel@tonic-gate 			FILE *fd;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 			if (walkflags & FTW_PHYS)
7047c478bd9Sstevel@tonic-gate 				com = ncpio;
7057c478bd9Sstevel@tonic-gate 			else
7067c478bd9Sstevel@tonic-gate 				com = ncpiol;
7077c478bd9Sstevel@tonic-gate 		common:
7087c478bd9Sstevel@tonic-gate 			/* set up cpio */
7097c478bd9Sstevel@tonic-gate 			if ((fd = fopen(b, "w")) == NULL) {
7107c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
7117c478bd9Sstevel@tonic-gate 					gettext("%s: cannot create %s\n"),
7127c478bd9Sstevel@tonic-gate 					cmdname, b);
7137c478bd9Sstevel@tonic-gate 				exit(1);
7147c478bd9Sstevel@tonic-gate 			}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 			np->first.l = (long)cmdopen("cpio", com, "w", fd);
7177c478bd9Sstevel@tonic-gate 			(void) fclose(fd);
7187c478bd9Sstevel@tonic-gate 			walkflags |= FTW_DEPTH;
7197c478bd9Sstevel@tonic-gate 			np->action = CPIO;
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
7227c478bd9Sstevel@tonic-gate 		case PRINT:
723b34cd89aSYuri Pankov 		case PRINT0:
7247c478bd9Sstevel@tonic-gate 			(*actionp)++;
7257c478bd9Sstevel@tonic-gate 			break;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 		case NEWER: {
7287c478bd9Sstevel@tonic-gate 			struct stat statb;
7297c478bd9Sstevel@tonic-gate 			if (stat(b, &statb) < 0) {
7307c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
7317c478bd9Sstevel@tonic-gate 					gettext("%s: cannot access %s\n"),
7327c478bd9Sstevel@tonic-gate 					cmdname, b);
7337c478bd9Sstevel@tonic-gate 				exit(1);
7347c478bd9Sstevel@tonic-gate 			}
7357c478bd9Sstevel@tonic-gate 			np->first.l = statb.st_mtime;
7367c478bd9Sstevel@tonic-gate 			np->second.i = '+';
7377c478bd9Sstevel@tonic-gate 			break;
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 		case PRUNE:
7417c478bd9Sstevel@tonic-gate 		case NOUSER:
7427c478bd9Sstevel@tonic-gate 		case NOGRP:
7437c478bd9Sstevel@tonic-gate 			break;
7447c478bd9Sstevel@tonic-gate 		case FSTYPE:
7457c478bd9Sstevel@tonic-gate 			np->first.cp = b;
7467c478bd9Sstevel@tonic-gate 			break;
7477c478bd9Sstevel@tonic-gate 		case LS:
7487c478bd9Sstevel@tonic-gate 			(*actionp)++;
7497c478bd9Sstevel@tonic-gate 			break;
7507c478bd9Sstevel@tonic-gate 		case XATTR:
7517c478bd9Sstevel@tonic-gate 			break;
7527c478bd9Sstevel@tonic-gate 		case ACL:
7537c478bd9Sstevel@tonic-gate 			break;
754b34cd89aSYuri Pankov 		case MAXDEPTH:
755b34cd89aSYuri Pankov 			maxdepth = (int)strtol(b, (char **)NULL, 10);
756b34cd89aSYuri Pankov 			break;
757b34cd89aSYuri Pankov 		case MINDEPTH:
758b34cd89aSYuri Pankov 			mindepth = (int)strtol(b, (char **)NULL, 10);
759b34cd89aSYuri Pankov 			break;
7607c478bd9Sstevel@tonic-gate 		}
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 		oldnp = np++;
7637c478bd9Sstevel@tonic-gate 		oldnp->next = np;
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if ((*av) || (wasop))
7677c478bd9Sstevel@tonic-gate 		goto err;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	if (paren_cnt != 0) {
7707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: unmatched '('\n"),
7717c478bd9Sstevel@tonic-gate 		cmdname);
7727c478bd9Sstevel@tonic-gate 		exit(1);
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/* just before returning, save next free node from the list */
7767c478bd9Sstevel@tonic-gate 	freenode = oldnp->next;
7777c478bd9Sstevel@tonic-gate 	oldnp->next = 0;
7787c478bd9Sstevel@tonic-gate 	return (av-argv);
7797c478bd9Sstevel@tonic-gate err:
7807c478bd9Sstevel@tonic-gate 	if (*av)
7817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7827c478bd9Sstevel@tonic-gate 		    gettext("%s: bad option %s\n"), cmdname, *av);
7837c478bd9Sstevel@tonic-gate 	else
7847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: bad option\n"), cmdname);
7857c478bd9Sstevel@tonic-gate 	usage();
7867c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * print out a usage message
7917c478bd9Sstevel@tonic-gate  */
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate static void
usage(void)7946c83d09fSrobbin usage(void)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
797b34cd89aSYuri Pankov 	    gettext("%s: [-E] [-H | -L] path-list predicate-list\n"), cmdname);
7987c478bd9Sstevel@tonic-gate 	exit(1);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate /*
8027c478bd9Sstevel@tonic-gate  * This is the function that gets executed at each node
8037c478bd9Sstevel@tonic-gate  */
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate static int
execute(name,statb,type,state)8067c478bd9Sstevel@tonic-gate execute(name, statb, type, state)
8077c478bd9Sstevel@tonic-gate char *name;
8087c478bd9Sstevel@tonic-gate struct stat *statb;
8097c478bd9Sstevel@tonic-gate int type;
8107c478bd9Sstevel@tonic-gate struct FTW *state;
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	struct Node *np = topnode;
8137c478bd9Sstevel@tonic-gate 	int val;
8147c478bd9Sstevel@tonic-gate 	time_t t;
8157c478bd9Sstevel@tonic-gate 	long l;
8167c478bd9Sstevel@tonic-gate 	long long ll;
8177c478bd9Sstevel@tonic-gate 	int not = 1;
8187c478bd9Sstevel@tonic-gate 	char *filename;
819b34cd89aSYuri Pankov 	int cnpreg = 0;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	if (type == FTW_NS) {
8227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: stat() error %s: %s\n"),
8237c478bd9Sstevel@tonic-gate 			cmdname, name, strerror(errno));
8247c478bd9Sstevel@tonic-gate 		error = 1;
8257c478bd9Sstevel@tonic-gate 		return (0);
8267c478bd9Sstevel@tonic-gate 	} else if (type == FTW_DNR) {
8277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: cannot read dir %s: %s\n"),
8287c478bd9Sstevel@tonic-gate 			cmdname, name, strerror(errno));
8297c478bd9Sstevel@tonic-gate 		error = 1;
8300729abfeSRich Burridge 	} else if (type == FTW_SLN && lflag == 1) {
8317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8327c478bd9Sstevel@tonic-gate 			gettext("%s: cannot follow symbolic link %s: %s\n"),
8337c478bd9Sstevel@tonic-gate 			cmdname, name, strerror(errno));
8347c478bd9Sstevel@tonic-gate 		error = 1;
83568a94df1Scf46844 	} else if (type == FTW_DL) {
83668a94df1Scf46844 		(void) fprintf(stderr, gettext("%s: cycle detected for %s\n"),
83768a94df1Scf46844 			cmdname, name);
83868a94df1Scf46844 		error = 1;
83968a94df1Scf46844 		return (0);
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 
842b34cd89aSYuri Pankov 	if ((maxdepth != -1 && state->level > maxdepth) ||
843b34cd89aSYuri Pankov 	    (mindepth != -1 && state->level < mindepth))
844b34cd89aSYuri Pankov 		return (0);
845b34cd89aSYuri Pankov 
8467c478bd9Sstevel@tonic-gate 	while (np) {
8477c478bd9Sstevel@tonic-gate 		switch (np->action) {
8487c478bd9Sstevel@tonic-gate 		case NOT:
8497c478bd9Sstevel@tonic-gate 			not = !not;
8507c478bd9Sstevel@tonic-gate 			np = np->next;
8517c478bd9Sstevel@tonic-gate 			continue;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 		case AND:
8547c478bd9Sstevel@tonic-gate 			np = np->next;
8557c478bd9Sstevel@tonic-gate 			continue;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 		case OR:
8587c478bd9Sstevel@tonic-gate 			if (np->first.np == np) {
8597c478bd9Sstevel@tonic-gate 				/*
8607c478bd9Sstevel@tonic-gate 				 * handle naked OR (no term on left hand side)
8617c478bd9Sstevel@tonic-gate 				 */
8627c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
8637c478bd9Sstevel@tonic-gate 				    gettext("%s: invalid -o construction\n"),
8647c478bd9Sstevel@tonic-gate 				    cmdname);
8657c478bd9Sstevel@tonic-gate 				exit(2);
8667c478bd9Sstevel@tonic-gate 			}
8677c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
8687c478bd9Sstevel@tonic-gate 		case LPAREN: {
8697c478bd9Sstevel@tonic-gate 			struct Node *save = topnode;
8707c478bd9Sstevel@tonic-gate 			topnode = np->first.np;
8717c478bd9Sstevel@tonic-gate 			(void) execute(name, statb, type, state);
8727c478bd9Sstevel@tonic-gate 			val = lastval;
8737c478bd9Sstevel@tonic-gate 			topnode = save;
8747c478bd9Sstevel@tonic-gate 			if (np->action == OR) {
8757c478bd9Sstevel@tonic-gate 				if (val)
8767c478bd9Sstevel@tonic-gate 					return (0);
8777c478bd9Sstevel@tonic-gate 				val = 1;
8787c478bd9Sstevel@tonic-gate 			}
8797c478bd9Sstevel@tonic-gate 			break;
8807c478bd9Sstevel@tonic-gate 		}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		case LOCAL: {
8837c478bd9Sstevel@tonic-gate 			int	nremfs;
8847c478bd9Sstevel@tonic-gate 			val = 1;
8857c478bd9Sstevel@tonic-gate 			/*
8867c478bd9Sstevel@tonic-gate 			 * If file system type matches the remote
8877c478bd9Sstevel@tonic-gate 			 * file system type, then it is not local.
8887c478bd9Sstevel@tonic-gate 			 */
8897c478bd9Sstevel@tonic-gate 			for (nremfs = 0; nremfs < fstype_index; nremfs++) {
8907c478bd9Sstevel@tonic-gate 				if (strcmp(remote_fstypes[nremfs],
8917c478bd9Sstevel@tonic-gate 						statb->st_fstype) == 0) {
8927c478bd9Sstevel@tonic-gate 					val = 0;
8937c478bd9Sstevel@tonic-gate 					break;
8947c478bd9Sstevel@tonic-gate 				}
8957c478bd9Sstevel@tonic-gate 			}
8967c478bd9Sstevel@tonic-gate 			break;
8977c478bd9Sstevel@tonic-gate 		}
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 		case TYPE:
9007c478bd9Sstevel@tonic-gate 			l = (long)statb->st_mode&S_IFMT;
9017c478bd9Sstevel@tonic-gate 			goto num;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 		case PERM:
9047c478bd9Sstevel@tonic-gate 			l = (long)statb->st_mode&07777;
9057c478bd9Sstevel@tonic-gate 			if (np->second.i == '-')
9067c478bd9Sstevel@tonic-gate 				val = ((l&np->first.l) == np->first.l);
9077c478bd9Sstevel@tonic-gate 			else
9087c478bd9Sstevel@tonic-gate 				val = (l == np->first.l);
9097c478bd9Sstevel@tonic-gate 			break;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		case INUM:
9127c478bd9Sstevel@tonic-gate 			ll = (long long)statb->st_ino;
9137c478bd9Sstevel@tonic-gate 			goto llnum;
9147c478bd9Sstevel@tonic-gate 		case NEWER:
9157c478bd9Sstevel@tonic-gate 			l = statb->st_mtime;
9167c478bd9Sstevel@tonic-gate 			goto num;
9177c478bd9Sstevel@tonic-gate 		case ATIME:
9187c478bd9Sstevel@tonic-gate 			t = statb->st_atime;
9197c478bd9Sstevel@tonic-gate 			goto days;
9207c478bd9Sstevel@tonic-gate 		case CTIME:
9217c478bd9Sstevel@tonic-gate 			t = statb->st_ctime;
9227c478bd9Sstevel@tonic-gate 			goto days;
9237c478bd9Sstevel@tonic-gate 		case MTIME:
9247c478bd9Sstevel@tonic-gate 			t = statb->st_mtime;
9257c478bd9Sstevel@tonic-gate 		days:
9267c478bd9Sstevel@tonic-gate 			l = (now-t)/A_DAY;
9277c478bd9Sstevel@tonic-gate 			goto num;
928da1a9cbeSjonb 		case MMIN:
929da1a9cbeSjonb 			t = statb->st_mtime;
930da1a9cbeSjonb 			goto mins;
931da1a9cbeSjonb 		case AMIN:
932da1a9cbeSjonb 			t = statb->st_atime;
933da1a9cbeSjonb 			goto mins;
934da1a9cbeSjonb 		case CMIN:
935da1a9cbeSjonb 			t = statb->st_ctime;
936da1a9cbeSjonb 			goto mins;
937da1a9cbeSjonb 		mins:
938da1a9cbeSjonb 			l = (now-t)/A_MIN;
939da1a9cbeSjonb 			goto num;
9407c478bd9Sstevel@tonic-gate 		case CSIZE:
9417c478bd9Sstevel@tonic-gate 			ll = (long long)statb->st_size;
9427c478bd9Sstevel@tonic-gate 			goto llnum;
9437c478bd9Sstevel@tonic-gate 		case SIZE:
9447c478bd9Sstevel@tonic-gate 			ll = (long long)round(statb->st_size, BLKSIZ)/BLKSIZ;
9457c478bd9Sstevel@tonic-gate 			goto llnum;
9467c478bd9Sstevel@tonic-gate 		case F_USER:
9477c478bd9Sstevel@tonic-gate 			l = (long)statb->st_uid;
9487c478bd9Sstevel@tonic-gate 			goto num;
9497c478bd9Sstevel@tonic-gate 		case F_GROUP:
9507c478bd9Sstevel@tonic-gate 			l = (long)statb->st_gid;
9517c478bd9Sstevel@tonic-gate 			goto num;
9527c478bd9Sstevel@tonic-gate 		case LINKS:
9537c478bd9Sstevel@tonic-gate 			l = (long)statb->st_nlink;
9547c478bd9Sstevel@tonic-gate 			goto num;
9557c478bd9Sstevel@tonic-gate 		llnum:
9567c478bd9Sstevel@tonic-gate 			if (np->second.i == '+')
9577c478bd9Sstevel@tonic-gate 				val = (ll > np->first.ll);
9587c478bd9Sstevel@tonic-gate 			else if (np->second.i == '-')
9597c478bd9Sstevel@tonic-gate 				val = (ll < np->first.ll);
9607c478bd9Sstevel@tonic-gate 			else
9617c478bd9Sstevel@tonic-gate 				val = (ll == np->first.ll);
9627c478bd9Sstevel@tonic-gate 			break;
9637c478bd9Sstevel@tonic-gate 		num:
9647c478bd9Sstevel@tonic-gate 			if (np->second.i == '+')
9657c478bd9Sstevel@tonic-gate 				val = (l > np->first.l);
9667c478bd9Sstevel@tonic-gate 			else if (np->second.i == '-')
9677c478bd9Sstevel@tonic-gate 				val = (l < np->first.l);
9687c478bd9Sstevel@tonic-gate 			else
9697c478bd9Sstevel@tonic-gate 				val = (l == np->first.l);
9707c478bd9Sstevel@tonic-gate 			break;
9717c478bd9Sstevel@tonic-gate 		case OK:
9727c478bd9Sstevel@tonic-gate 			val = ok(name, np->first.ap);
9737c478bd9Sstevel@tonic-gate 			break;
9747c478bd9Sstevel@tonic-gate 		case EXEC:
975d35170d6Srm88369 			val = doexec(name, np->first.ap, NULL);
9767c478bd9Sstevel@tonic-gate 			break;
977*ab823b7fSPrasad Joshi 		case DELETE:
978*ab823b7fSPrasad Joshi 			val = dodelete(name, statb, state);
979*ab823b7fSPrasad Joshi 			break;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		case VARARGS: {
9827c478bd9Sstevel@tonic-gate 			struct Arglist *ap = np->first.vp;
9837c478bd9Sstevel@tonic-gate 			char *cp;
9847c478bd9Sstevel@tonic-gate 			cp = ap->nextstr - (strlen(name)+1);
9857c478bd9Sstevel@tonic-gate 			if (cp >= (char *)(ap->nextvar+3)) {
9867c478bd9Sstevel@tonic-gate 				/* there is room just copy the name */
9877c478bd9Sstevel@tonic-gate 				val = 1;
9887c478bd9Sstevel@tonic-gate 				(void) strcpy(cp, name);
9897c478bd9Sstevel@tonic-gate 				*ap->nextvar++ = cp;
9907c478bd9Sstevel@tonic-gate 				ap->nextstr = cp;
9917c478bd9Sstevel@tonic-gate 			} else {
9927c478bd9Sstevel@tonic-gate 				/* no more room, exec command */
9937c478bd9Sstevel@tonic-gate 				*ap->nextvar++ = name;
9947c478bd9Sstevel@tonic-gate 				*ap->nextvar = 0;
9957c478bd9Sstevel@tonic-gate 				val = 1;
996d35170d6Srm88369 				(void) doexec((char *)0, ap->arglist,
997d35170d6Srm88369 				    &exec_exitcode);
9987c478bd9Sstevel@tonic-gate 				ap->nextstr = ap->end;
9997c478bd9Sstevel@tonic-gate 				ap->nextvar = ap->firstvar;
10007c478bd9Sstevel@tonic-gate 			}
10017c478bd9Sstevel@tonic-gate 			break;
10027c478bd9Sstevel@tonic-gate 		}
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 		case DEPTH:
10057c478bd9Sstevel@tonic-gate 		case MOUNT:
10067c478bd9Sstevel@tonic-gate 		case FOLLOW:
10077c478bd9Sstevel@tonic-gate 			val = 1;
10087c478bd9Sstevel@tonic-gate 			break;
10097c478bd9Sstevel@tonic-gate 
1010b34cd89aSYuri Pankov 		case NAME:
101105f32410SAndy Stormont 		case INAME:
101205f32410SAndy Stormont 		case PATH:
101305f32410SAndy Stormont 		case IPATH: {
101405f32410SAndy Stormont 			char *path;
101505f32410SAndy Stormont 			int fnmflags = 0;
101605f32410SAndy Stormont 
101705f32410SAndy Stormont 			if (np->action == INAME || np->action == IPATH)
101805f32410SAndy Stormont 				fnmflags = FNM_IGNORECASE;
10199ab6dc39Schin 
10209ab6dc39Schin 			/*
10219ab6dc39Schin 			 * basename(3c) may modify name, so
10229ab6dc39Schin 			 * we need to pass another string
10239ab6dc39Schin 			 */
102405f32410SAndy Stormont 			if ((path = strdup(name)) == NULL) {
10259ab6dc39Schin 				(void) fprintf(stderr,
10269ab6dc39Schin 				    gettext("%s: cannot strdup() %s: %s\n"),
10276b238a5aSchin 				    cmdname, name, strerror(errno));
10289ab6dc39Schin 				exit(2);
10299ab6dc39Schin 			}
10307c478bd9Sstevel@tonic-gate 			/*
10317c478bd9Sstevel@tonic-gate 			 * XPG4 find should not treat a leading '.' in a
10327c478bd9Sstevel@tonic-gate 			 * filename specially for pattern matching.
10337c478bd9Sstevel@tonic-gate 			 * /usr/bin/find  will not pattern match a leading
10347c478bd9Sstevel@tonic-gate 			 * '.' in a filename, unless '.' is explicitly
10357c478bd9Sstevel@tonic-gate 			 * specified.
10367c478bd9Sstevel@tonic-gate 			 */
1037b34cd89aSYuri Pankov #ifndef XPG4
1038b34cd89aSYuri Pankov 			fnmflags |= FNM_PERIOD;
10397c478bd9Sstevel@tonic-gate #endif
104005f32410SAndy Stormont 
104105f32410SAndy Stormont 			val = !fnmatch(np->first.cp,
104205f32410SAndy Stormont 			    (np->action == NAME || np->action == INAME)
104305f32410SAndy Stormont 				? basename(path) : path, fnmflags);
104405f32410SAndy Stormont 			free(path);
10457c478bd9Sstevel@tonic-gate 			break;
10467c478bd9Sstevel@tonic-gate 		}
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 		case PRUNE:
10497c478bd9Sstevel@tonic-gate 			if (type == FTW_D)
10507c478bd9Sstevel@tonic-gate 				state->quit = FTW_PRUNE;
10517c478bd9Sstevel@tonic-gate 			val = 1;
10527c478bd9Sstevel@tonic-gate 			break;
10537c478bd9Sstevel@tonic-gate 		case NOUSER:
10547c478bd9Sstevel@tonic-gate 			val = ((getpwuid(statb->st_uid)) == 0);
10557c478bd9Sstevel@tonic-gate 			break;
10567c478bd9Sstevel@tonic-gate 		case NOGRP:
10577c478bd9Sstevel@tonic-gate 			val = ((getgrgid(statb->st_gid)) == 0);
10587c478bd9Sstevel@tonic-gate 			break;
10597c478bd9Sstevel@tonic-gate 		case FSTYPE:
10607c478bd9Sstevel@tonic-gate 			val = (strcmp(np->first.cp, statb->st_fstype) == 0);
10617c478bd9Sstevel@tonic-gate 			break;
10627c478bd9Sstevel@tonic-gate 		case CPIO:
10637c478bd9Sstevel@tonic-gate 			output = (FILE *)np->first.l;
10647c478bd9Sstevel@tonic-gate 			(void) fprintf(output, "%s\n", name);
10657c478bd9Sstevel@tonic-gate 			val = 1;
10667c478bd9Sstevel@tonic-gate 			break;
10677c478bd9Sstevel@tonic-gate 		case PRINT:
1068b34cd89aSYuri Pankov 		case PRINT0:
1069b34cd89aSYuri Pankov 			(void) fprintf(stdout, "%s%c", name,
1070b34cd89aSYuri Pankov 			    (np->action == PRINT) ? '\n' : '\0');
10717c478bd9Sstevel@tonic-gate 			val = 1;
10727c478bd9Sstevel@tonic-gate 			break;
10737c478bd9Sstevel@tonic-gate 		case LS:
10747c478bd9Sstevel@tonic-gate 			(void) list(name, statb);
10757c478bd9Sstevel@tonic-gate 			val = 1;
10767c478bd9Sstevel@tonic-gate 			break;
10777c478bd9Sstevel@tonic-gate 		case XATTR:
1078f467e6fbSJohn Sonnenschein 			filename = (walkflags & FTW_CHDIR) ?
1079f467e6fbSJohn Sonnenschein 				gettail(name) : name;
10807c478bd9Sstevel@tonic-gate 			val = (pathconf(filename, _PC_XATTR_EXISTS) == 1);
10817c478bd9Sstevel@tonic-gate 			break;
10827c478bd9Sstevel@tonic-gate 		case ACL:
10837c478bd9Sstevel@tonic-gate 			/*
10847c478bd9Sstevel@tonic-gate 			 * Need to get the tail of the file name, since we have
10857c478bd9Sstevel@tonic-gate 			 * already chdir()ed into the directory (performed in
10867c478bd9Sstevel@tonic-gate 			 * nftw()) of the file
10877c478bd9Sstevel@tonic-gate 			 */
1088f467e6fbSJohn Sonnenschein 			filename = (walkflags & FTW_CHDIR) ?
1089f467e6fbSJohn Sonnenschein 				gettail(name) : name;
1090f467e6fbSJohn Sonnenschein 			val = acl_trivial(filename);
10917c478bd9Sstevel@tonic-gate 			break;
1092b34cd89aSYuri Pankov 		case F_USERACL:
1093b34cd89aSYuri Pankov 		case F_GROUPACL: {
1094b34cd89aSYuri Pankov 			int i;
1095b34cd89aSYuri Pankov 			acl_t *acl;
1096b34cd89aSYuri Pankov 			void *acl_entry;
1097b34cd89aSYuri Pankov 			aclent_t *p1;
1098b34cd89aSYuri Pankov 			ace_t *p2;
1099b34cd89aSYuri Pankov 
1100b34cd89aSYuri Pankov 			filename = (walkflags & FTW_CHDIR) ?
1101b34cd89aSYuri Pankov 			    gettail(name) : name;
1102b34cd89aSYuri Pankov 			val = 0;
1103b34cd89aSYuri Pankov 			if (acl_get(filename, 0, &acl) != 0)
1104b34cd89aSYuri Pankov 				break;
1105b34cd89aSYuri Pankov 			for (i = 0, acl_entry = acl->acl_aclp;
1106b34cd89aSYuri Pankov 			    i != acl->acl_cnt; i++) {
1107b34cd89aSYuri Pankov 				if (acl->acl_type == ACLENT_T) {
1108b34cd89aSYuri Pankov 					p1 = (aclent_t *)acl_entry;
1109b34cd89aSYuri Pankov 					if (p1->a_id == np->first.l) {
1110b34cd89aSYuri Pankov 						val = 1;
1111b34cd89aSYuri Pankov 						acl_free(acl);
1112b34cd89aSYuri Pankov 						break;
1113b34cd89aSYuri Pankov 					}
1114b34cd89aSYuri Pankov 				} else {
1115b34cd89aSYuri Pankov 					p2 = (ace_t *)acl_entry;
1116b34cd89aSYuri Pankov 					if (p2->a_who == np->first.l) {
1117b34cd89aSYuri Pankov 						val = 1;
1118b34cd89aSYuri Pankov 						acl_free(acl);
1119b34cd89aSYuri Pankov 						break;
1120b34cd89aSYuri Pankov 					}
1121b34cd89aSYuri Pankov 				}
1122b34cd89aSYuri Pankov 				acl_entry = ((char *)acl_entry +
1123b34cd89aSYuri Pankov 				    acl->acl_entry_size);
1124b34cd89aSYuri Pankov 			}
1125b34cd89aSYuri Pankov 			acl_free(acl);
1126b34cd89aSYuri Pankov 			break;
1127b34cd89aSYuri Pankov 		}
1128b34cd89aSYuri Pankov 		case IREGEX:
1129b34cd89aSYuri Pankov 		case REGEX: {
1130b34cd89aSYuri Pankov 			regmatch_t pmatch;
1131b34cd89aSYuri Pankov 
1132b34cd89aSYuri Pankov 			val = 0;
1133b34cd89aSYuri Pankov 			if (regexec(&preg[cnpreg], name, 1, &pmatch, NULL) == 0)
1134b34cd89aSYuri Pankov 				val = ((pmatch.rm_so == 0) &&
1135b34cd89aSYuri Pankov 				    (pmatch.rm_eo == strlen(name)));
1136b34cd89aSYuri Pankov 			cnpreg++;
1137b34cd89aSYuri Pankov 			break;
1138b34cd89aSYuri Pankov 		}
1139b34cd89aSYuri Pankov 		case MAXDEPTH:
1140b34cd89aSYuri Pankov 			if (state->level == maxdepth && type == FTW_D)
1141b34cd89aSYuri Pankov 				state->quit = FTW_PRUNE;
1142b34cd89aSYuri Pankov 			/* FALLTHROUGH */
1143b34cd89aSYuri Pankov 		case MINDEPTH:
1144b34cd89aSYuri Pankov 			val = 1;
1145b34cd89aSYuri Pankov 			break;
11467c478bd9Sstevel@tonic-gate 		}
11477c478bd9Sstevel@tonic-gate 		/*
11487c478bd9Sstevel@tonic-gate 		 * evaluate 'val' and 'not' (exclusive-or)
11497c478bd9Sstevel@tonic-gate 		 * if no inversion (not == 1), return only when val == 0
11507c478bd9Sstevel@tonic-gate 		 * (primary not true). Otherwise, invert the primary
11517c478bd9Sstevel@tonic-gate 		 * and return when the primary is true.
11527c478bd9Sstevel@tonic-gate 		 * 'Lastval' saves the last result (fail or pass) when
11537c478bd9Sstevel@tonic-gate 		 * returning back to the calling routine.
11547c478bd9Sstevel@tonic-gate 		 */
11557c478bd9Sstevel@tonic-gate 		if (val^not) {
11567c478bd9Sstevel@tonic-gate 			lastval = 0;
11577c478bd9Sstevel@tonic-gate 			return (0);
11587c478bd9Sstevel@tonic-gate 		}
11597c478bd9Sstevel@tonic-gate 		lastval = 1;
11607c478bd9Sstevel@tonic-gate 		not = 1;
11617c478bd9Sstevel@tonic-gate 		np = np->next;
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 	return (0);
11647c478bd9Sstevel@tonic-gate }
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate /*
11677c478bd9Sstevel@tonic-gate  * code for the -ok option
11687c478bd9Sstevel@tonic-gate  */
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate static int
ok(name,argv)11717c478bd9Sstevel@tonic-gate ok(name, argv)
11727c478bd9Sstevel@tonic-gate char *name;
11737c478bd9Sstevel@tonic-gate char *argv[];
11747c478bd9Sstevel@tonic-gate {
11753d63ea05Sas145665 	int  c;
11763d63ea05Sas145665 	int i = 0;
11773d63ea05Sas145665 	char resp[LINE_MAX + 1];
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	(void) fflush(stdout); 	/* to flush possible `-print' */
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	if ((*argv != dummyarg) && (strcmp(*argv, name)))
11827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "< %s ... %s >?   ", *argv, name);
11837c478bd9Sstevel@tonic-gate 	else
11847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "< {} ... %s >?   ", name);
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	(void) fflush(stderr);
11873d63ea05Sas145665 
11883d63ea05Sas145665 	while ((c = getchar()) != '\n') {
11897c478bd9Sstevel@tonic-gate 		if (c == EOF)
11907c478bd9Sstevel@tonic-gate 			exit(2);
11913d63ea05Sas145665 		if (i < LINE_MAX)
11923d63ea05Sas145665 			resp[i++] = c;
11933d63ea05Sas145665 	}
11943d63ea05Sas145665 	resp[i] = '\0';
11953d63ea05Sas145665 
11963d63ea05Sas145665 	if (yes_check(resp))
11973d63ea05Sas145665 		return (doexec(name, argv, NULL));
11987c478bd9Sstevel@tonic-gate 	else
11993d63ea05Sas145665 		return (0);
12007c478bd9Sstevel@tonic-gate }
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate /*
12037c478bd9Sstevel@tonic-gate  * execute argv with {} replaced by name
1204d35170d6Srm88369  *
1205d35170d6Srm88369  * Per XPG6, find must exit non-zero if an invocation through
1206d35170d6Srm88369  * -exec, punctuated by a plus sign, exits non-zero, so set
1207d35170d6Srm88369  * exitcode if we see a non-zero exit.
1208d35170d6Srm88369  * exitcode should be NULL when -exec or -ok is not punctuated
1209d35170d6Srm88369  * by a plus sign.
12107c478bd9Sstevel@tonic-gate  */
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate static int
doexec(char * name,char * argv[],int * exitcode)1213d35170d6Srm88369 doexec(char *name, char *argv[], int *exitcode)
12147c478bd9Sstevel@tonic-gate {
12157c478bd9Sstevel@tonic-gate 	char *cp;
12167c478bd9Sstevel@tonic-gate 	char **av = argv;
1217d35170d6Srm88369 	char *newargs[1 + SHELL_MAXARGS + 1];
12187c478bd9Sstevel@tonic-gate 	int dummyseen = 0;
1219d35170d6Srm88369 	int i, j, status, rc, r = 0;
1220d35170d6Srm88369 	int exit_status = 0;
1221d35170d6Srm88369 	pid_t pid, pid1;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);	  /* to flush possible `-print' */
12247c478bd9Sstevel@tonic-gate 	if (name) {
12257c478bd9Sstevel@tonic-gate 		while (cp = *av++) {
12267c478bd9Sstevel@tonic-gate 			if (cp == dummyarg) {
12277c478bd9Sstevel@tonic-gate 				dummyseen = 1;
12287c478bd9Sstevel@tonic-gate 				av[-1] = name;
12297c478bd9Sstevel@tonic-gate 			}
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 		}
12327c478bd9Sstevel@tonic-gate 	}
12337c478bd9Sstevel@tonic-gate 	if (argv[0] == NULL)    /* null command line */
12347c478bd9Sstevel@tonic-gate 		return (r);
12357c478bd9Sstevel@tonic-gate 
1236d35170d6Srm88369 	if ((pid = fork()) == -1) {
1237d35170d6Srm88369 		/* fork failed */
1238d35170d6Srm88369 		if (exitcode != NULL)
1239d35170d6Srm88369 			*exitcode = 1;
1240d35170d6Srm88369 		return (0);
1241d35170d6Srm88369 	}
1242d35170d6Srm88369 	if (pid != 0) {
1243d35170d6Srm88369 		/* parent */
1244d35170d6Srm88369 		do {
1245d35170d6Srm88369 			/* wait for child to exit */
1246d35170d6Srm88369 			if ((rc = wait(&r)) == -1 && errno != EINTR) {
1247d35170d6Srm88369 				(void) fprintf(stderr,
1248d35170d6Srm88369 				    gettext("wait failed %s"), strerror(errno));
1249d35170d6Srm88369 
1250d35170d6Srm88369 				if (exitcode != NULL)
1251d35170d6Srm88369 					*exitcode = 1;
1252d35170d6Srm88369 				return (0);
1253d35170d6Srm88369 			}
1254d35170d6Srm88369 		} while (rc != pid);
1255d35170d6Srm88369 	} else {
1256d35170d6Srm88369 		/* child */
12577c478bd9Sstevel@tonic-gate 		(void) execvp(argv[0], argv);
1258d35170d6Srm88369 		if (errno != E2BIG)
1259d35170d6Srm88369 			exit(1);
1260d35170d6Srm88369 
1261d35170d6Srm88369 		/*
1262d35170d6Srm88369 		 * We are in a situation where argv[0] points to a
1263d35170d6Srm88369 		 * script without the interpreter line, e.g. #!/bin/sh.
1264d35170d6Srm88369 		 * execvp() will execute either /usr/bin/sh or
1265d35170d6Srm88369 		 * /usr/xpg4/bin/sh against the script, and you will be
1266d35170d6Srm88369 		 * limited to SHELL_MAXARGS arguments. If you try to
1267d35170d6Srm88369 		 * pass more than SHELL_MAXARGS arguments, execvp()
1268d35170d6Srm88369 		 * fails with E2BIG.
1269d35170d6Srm88369 		 * See usr/src/lib/libc/port/gen/execvp.c.
1270d35170d6Srm88369 		 *
1271d35170d6Srm88369 		 * In this situation, process the argument list by
1272d35170d6Srm88369 		 * packets of SHELL_MAXARGS arguments with respect of
1273d35170d6Srm88369 		 * the following rules:
1274d35170d6Srm88369 		 * 1. the invocations have to complete before find exits
1275d35170d6Srm88369 		 * 2. only one invocation can be running at a time
1276d35170d6Srm88369 		 */
1277d35170d6Srm88369 
1278d35170d6Srm88369 		i = 1;
1279d35170d6Srm88369 		newargs[0] = argv[0];
1280d35170d6Srm88369 
1281d35170d6Srm88369 		while (argv[i]) {
1282d35170d6Srm88369 			j = 1;
1283d35170d6Srm88369 			while (j <= SHELL_MAXARGS && argv[i]) {
1284d35170d6Srm88369 				newargs[j++] = argv[i++];
1285d35170d6Srm88369 			}
1286d35170d6Srm88369 			newargs[j] = NULL;
1287d35170d6Srm88369 
1288d35170d6Srm88369 			if ((pid1 = fork()) == -1) {
1289d35170d6Srm88369 				/* fork failed */
12907c478bd9Sstevel@tonic-gate 				exit(1);
12917c478bd9Sstevel@tonic-gate 			}
1292d35170d6Srm88369 			if (pid1 == 0) {
1293d35170d6Srm88369 				/* child */
1294d35170d6Srm88369 				(void) execvp(newargs[0], newargs);
1295d35170d6Srm88369 				exit(1);
1296d35170d6Srm88369 			}
1297d35170d6Srm88369 
1298d35170d6Srm88369 			status = 0;
1299d35170d6Srm88369 
1300d35170d6Srm88369 			do {
1301d35170d6Srm88369 				/* wait for the child to exit */
1302d35170d6Srm88369 				if ((rc = wait(&status)) == -1 &&
1303d35170d6Srm88369 				    errno != EINTR) {
1304d35170d6Srm88369 					(void) fprintf(stderr,
1305d35170d6Srm88369 					    gettext("wait failed %s"),
1306d35170d6Srm88369 					    strerror(errno));
1307d35170d6Srm88369 					exit(1);
1308d35170d6Srm88369 				}
1309d35170d6Srm88369 			} while (rc != pid1);
1310d35170d6Srm88369 
1311d35170d6Srm88369 			if (status)
1312d35170d6Srm88369 				exit_status = 1;
1313d35170d6Srm88369 		}
1314d35170d6Srm88369 		/* all the invocations have completed */
1315d35170d6Srm88369 		exit(exit_status);
1316d35170d6Srm88369 	}
1317d35170d6Srm88369 
13187c478bd9Sstevel@tonic-gate 	if (name && dummyseen) {
13197c478bd9Sstevel@tonic-gate 		for (av = argv; cp = *av++; ) {
13207c478bd9Sstevel@tonic-gate 			if (cp == name)
13217c478bd9Sstevel@tonic-gate 				av[-1] = dummyarg;
13227c478bd9Sstevel@tonic-gate 		}
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate 
1325d35170d6Srm88369 	if (r && exitcode != NULL)
1326d35170d6Srm88369 		*exitcode = 3; /* use to indicate error in cmd invocation */
1327d35170d6Srm88369 
13287c478bd9Sstevel@tonic-gate 	return (!r);
13297c478bd9Sstevel@tonic-gate }
13307c478bd9Sstevel@tonic-gate 
1331*ab823b7fSPrasad Joshi static int
dodelete(char * name,struct stat * statb,struct FTW * state)1332*ab823b7fSPrasad Joshi dodelete(char *name, struct stat *statb, struct FTW *state)
1333*ab823b7fSPrasad Joshi {
1334*ab823b7fSPrasad Joshi 	char *fn;
1335*ab823b7fSPrasad Joshi 	int rc = 0;
1336*ab823b7fSPrasad Joshi 
1337*ab823b7fSPrasad Joshi 	/* restrict symlinks */
1338*ab823b7fSPrasad Joshi 	if ((walkflags & FTW_PHYS) == 0) {
1339*ab823b7fSPrasad Joshi 		(void) fprintf(stderr,
1340*ab823b7fSPrasad Joshi 		    gettext("-delete is not allowed when symlinks are "
1341*ab823b7fSPrasad Joshi 		    "followed.\n"));
1342*ab823b7fSPrasad Joshi 		return (1);
1343*ab823b7fSPrasad Joshi 	}
1344*ab823b7fSPrasad Joshi 
1345*ab823b7fSPrasad Joshi 	fn = name + state->base;
1346*ab823b7fSPrasad Joshi 	if (strcmp(fn, ".") == 0) {
1347*ab823b7fSPrasad Joshi 		/* nothing to do */
1348*ab823b7fSPrasad Joshi 		return (1);
1349*ab823b7fSPrasad Joshi 	}
1350*ab823b7fSPrasad Joshi 
1351*ab823b7fSPrasad Joshi 	if (strchr(fn, '/') != NULL) {
1352*ab823b7fSPrasad Joshi 		(void) fprintf(stderr,
1353*ab823b7fSPrasad Joshi 		    gettext("-delete with relative path is unsafe."));
1354*ab823b7fSPrasad Joshi 		return (1);
1355*ab823b7fSPrasad Joshi 	}
1356*ab823b7fSPrasad Joshi 
1357*ab823b7fSPrasad Joshi 	if (S_ISDIR(statb->st_mode)) {
1358*ab823b7fSPrasad Joshi 		/* delete directory */
1359*ab823b7fSPrasad Joshi 		rc = rmdir(name);
1360*ab823b7fSPrasad Joshi 	} else {
1361*ab823b7fSPrasad Joshi 		/* delete file */
1362*ab823b7fSPrasad Joshi 		rc = unlink(name);
1363*ab823b7fSPrasad Joshi 	}
1364*ab823b7fSPrasad Joshi 
1365*ab823b7fSPrasad Joshi 	if (rc < 0) {
1366*ab823b7fSPrasad Joshi 		/* operation failed */
1367*ab823b7fSPrasad Joshi 		(void) fprintf(stderr, gettext("delete failed %s: %s\n"),
1368*ab823b7fSPrasad Joshi 		    name, strerror(errno));
1369*ab823b7fSPrasad Joshi 		return (1);
1370*ab823b7fSPrasad Joshi 	}
1371*ab823b7fSPrasad Joshi 
1372*ab823b7fSPrasad Joshi 	return (1);
1373*ab823b7fSPrasad Joshi }
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate /*
13767c478bd9Sstevel@tonic-gate  *  Table lookup routine
13777c478bd9Sstevel@tonic-gate  */
13787c478bd9Sstevel@tonic-gate static struct Args *
lookup(word)13797c478bd9Sstevel@tonic-gate lookup(word)
13807c478bd9Sstevel@tonic-gate char *word;
13817c478bd9Sstevel@tonic-gate {
13827c478bd9Sstevel@tonic-gate 	struct Args *argp = commands;
13837c478bd9Sstevel@tonic-gate 	int second;
13847c478bd9Sstevel@tonic-gate 	if (word == 0 || *word == 0)
13857c478bd9Sstevel@tonic-gate 		return (0);
13867c478bd9Sstevel@tonic-gate 	second = word[1];
13877c478bd9Sstevel@tonic-gate 	while (*argp->name) {
13887c478bd9Sstevel@tonic-gate 		if (second == argp->name[1] && strcmp(word, argp->name) == 0)
13897c478bd9Sstevel@tonic-gate 			return (argp);
13907c478bd9Sstevel@tonic-gate 		argp++;
13917c478bd9Sstevel@tonic-gate 	}
13927c478bd9Sstevel@tonic-gate 	return (0);
13937c478bd9Sstevel@tonic-gate }
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate /*
13977c478bd9Sstevel@tonic-gate  * Get space for variable length argument list
13987c478bd9Sstevel@tonic-gate  */
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate static struct Arglist *
varargs(com)14017c478bd9Sstevel@tonic-gate varargs(com)
14027c478bd9Sstevel@tonic-gate char **com;
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate 	struct Arglist *ap;
14057c478bd9Sstevel@tonic-gate 	int n;
14067c478bd9Sstevel@tonic-gate 	char **ep;
14077c478bd9Sstevel@tonic-gate 	if (varsize == 0) {
14087c478bd9Sstevel@tonic-gate 		n = 2*sizeof (char **);
14097c478bd9Sstevel@tonic-gate 		for (ep = environ; *ep; ep++)
14107c478bd9Sstevel@tonic-gate 			n += (strlen(*ep)+sizeof (ep) + 1);
14117c478bd9Sstevel@tonic-gate 		varsize = sizeof (struct Arglist)+ARG_MAX-PATH_MAX-n-1;
14127c478bd9Sstevel@tonic-gate 	}
14137c478bd9Sstevel@tonic-gate 	ap = (struct Arglist *)malloc(varsize+1);
14147c478bd9Sstevel@tonic-gate 	ap->end = (char *)ap + varsize;
14157c478bd9Sstevel@tonic-gate 	ap->nextstr = ap->end;
14167c478bd9Sstevel@tonic-gate 	ap->nextvar = ap->arglist;
14177c478bd9Sstevel@tonic-gate 	while (*ap->nextvar++ = *com++);
14187c478bd9Sstevel@tonic-gate 	ap->nextvar--;
14197c478bd9Sstevel@tonic-gate 	ap->firstvar = ap->nextvar;
14207c478bd9Sstevel@tonic-gate 	ap->next = lastlist;
14217c478bd9Sstevel@tonic-gate 	lastlist = ap;
14227c478bd9Sstevel@tonic-gate 	return (ap);
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate /*
14267c478bd9Sstevel@tonic-gate  * filter command support
14277c478bd9Sstevel@tonic-gate  * fork and exec cmd(argv) according to mode:
14287c478bd9Sstevel@tonic-gate  *
14297c478bd9Sstevel@tonic-gate  *	"r"	with fp as stdin of cmd (default stdin), cmd stdout returned
14307c478bd9Sstevel@tonic-gate  *	"w"	with fp as stdout of cmd (default stdout), cmd stdin returned
14317c478bd9Sstevel@tonic-gate  */
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate #define	CMDERR	((1<<8)-1)	/* command error exit code		*/
14347c478bd9Sstevel@tonic-gate #define	MAXCMDS	8		/* max # simultaneous cmdopen()'s	*/
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate static struct			/* info for each cmdopen()		*/
14377c478bd9Sstevel@tonic-gate {
14387c478bd9Sstevel@tonic-gate 	FILE	*fp;		/* returned by cmdopen()		*/
14397c478bd9Sstevel@tonic-gate 	pid_t	pid;		/* pid used by cmdopen()		*/
14407c478bd9Sstevel@tonic-gate } cmdproc[MAXCMDS];
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate static FILE *
cmdopen(cmd,argv,mode,fp)14437c478bd9Sstevel@tonic-gate cmdopen(cmd, argv, mode, fp)
14447c478bd9Sstevel@tonic-gate char	*cmd;
14457c478bd9Sstevel@tonic-gate char	**argv;
14467c478bd9Sstevel@tonic-gate char	*mode;
14477c478bd9Sstevel@tonic-gate FILE	*fp;
14487c478bd9Sstevel@tonic-gate {
14497c478bd9Sstevel@tonic-gate 	int	proc;
14507c478bd9Sstevel@tonic-gate 	int	cmdfd;
14517c478bd9Sstevel@tonic-gate 	int	usrfd;
14527c478bd9Sstevel@tonic-gate 	int		pio[2];
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	switch (*mode) {
14557c478bd9Sstevel@tonic-gate 	case 'r':
14567c478bd9Sstevel@tonic-gate 		cmdfd = 1;
14577c478bd9Sstevel@tonic-gate 		usrfd = 0;
14587c478bd9Sstevel@tonic-gate 		break;
14597c478bd9Sstevel@tonic-gate 	case 'w':
14607c478bd9Sstevel@tonic-gate 		cmdfd = 0;
14617c478bd9Sstevel@tonic-gate 		usrfd = 1;
14627c478bd9Sstevel@tonic-gate 		break;
14637c478bd9Sstevel@tonic-gate 	default:
14647c478bd9Sstevel@tonic-gate 		return (0);
14657c478bd9Sstevel@tonic-gate 	}
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	for (proc = 0; proc < MAXCMDS; proc++)
14687c478bd9Sstevel@tonic-gate 		if (!cmdproc[proc].fp)
14697c478bd9Sstevel@tonic-gate 			break;
14707c478bd9Sstevel@tonic-gate 	if (proc >= MAXCMDS)
14717c478bd9Sstevel@tonic-gate 		return (0);
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	if (pipe(pio))
14747c478bd9Sstevel@tonic-gate 		return (0);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	switch (cmdproc[proc].pid = fork()) {
14777c478bd9Sstevel@tonic-gate 	case -1:
14787c478bd9Sstevel@tonic-gate 		return (0);
14797c478bd9Sstevel@tonic-gate 	case 0:
14807c478bd9Sstevel@tonic-gate 		if (fp && fileno(fp) != usrfd) {
14817c478bd9Sstevel@tonic-gate 			(void) close(usrfd);
14827c478bd9Sstevel@tonic-gate 			if (dup2(fileno(fp), usrfd) != usrfd)
14837c478bd9Sstevel@tonic-gate 				_exit(CMDERR);
14847c478bd9Sstevel@tonic-gate 			(void) close(fileno(fp));
14857c478bd9Sstevel@tonic-gate 		}
14867c478bd9Sstevel@tonic-gate 		(void) close(cmdfd);
14877c478bd9Sstevel@tonic-gate 		if (dup2(pio[cmdfd], cmdfd) != cmdfd)
14887c478bd9Sstevel@tonic-gate 			_exit(CMDERR);
14897c478bd9Sstevel@tonic-gate 		(void) close(pio[cmdfd]);
14907c478bd9Sstevel@tonic-gate 		(void) close(pio[usrfd]);
14917c478bd9Sstevel@tonic-gate 		(void) execvp(cmd, argv);
14927c478bd9Sstevel@tonic-gate 		if (errno == ENOEXEC) {
14937c478bd9Sstevel@tonic-gate 			char	**p;
14947c478bd9Sstevel@tonic-gate 			char		**v;
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 			/*
14977c478bd9Sstevel@tonic-gate 			 * assume cmd is a shell script
14987c478bd9Sstevel@tonic-gate 			 */
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 			p = argv;
15017c478bd9Sstevel@tonic-gate 			while (*p++);
15027c478bd9Sstevel@tonic-gate 			if (v = (char **)malloc((p - argv + 1) *
15037c478bd9Sstevel@tonic-gate 					sizeof (char **))) {
15047c478bd9Sstevel@tonic-gate 				p = v;
15057c478bd9Sstevel@tonic-gate 				*p++ = cmd;
15067c478bd9Sstevel@tonic-gate 				if (*argv) argv++;
15077c478bd9Sstevel@tonic-gate 				while (*p++ = *argv++);
15087c478bd9Sstevel@tonic-gate 				(void) execv(getshell(), v);
15097c478bd9Sstevel@tonic-gate 			}
15107c478bd9Sstevel@tonic-gate 		}
15117c478bd9Sstevel@tonic-gate 		_exit(CMDERR);
15127c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
15137c478bd9Sstevel@tonic-gate 	default:
15147c478bd9Sstevel@tonic-gate 		(void) close(pio[cmdfd]);
15157c478bd9Sstevel@tonic-gate 		return (cmdproc[proc].fp = fdopen(pio[usrfd], mode));
15167c478bd9Sstevel@tonic-gate 	}
15177c478bd9Sstevel@tonic-gate }
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate /*
15207c478bd9Sstevel@tonic-gate  * close a stream opened by cmdopen()
15217c478bd9Sstevel@tonic-gate  * -1 returned if cmdopen() had a problem
15227c478bd9Sstevel@tonic-gate  * otherwise exit() status of command is returned
15237c478bd9Sstevel@tonic-gate  */
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate static int
cmdclose(fp)15267c478bd9Sstevel@tonic-gate cmdclose(fp)
15277c478bd9Sstevel@tonic-gate FILE	*fp;
15287c478bd9Sstevel@tonic-gate {
15297c478bd9Sstevel@tonic-gate 	int	i;
15307c478bd9Sstevel@tonic-gate 	pid_t	p, pid;
15317c478bd9Sstevel@tonic-gate 	int		status;
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAXCMDS; i++)
15347c478bd9Sstevel@tonic-gate 		if (fp == cmdproc[i].fp) break;
15357c478bd9Sstevel@tonic-gate 	if (i >= MAXCMDS)
15367c478bd9Sstevel@tonic-gate 		return (-1);
15377c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
15387c478bd9Sstevel@tonic-gate 	cmdproc[i].fp = 0;
15397c478bd9Sstevel@tonic-gate 	pid = cmdproc[i].pid;
15407c478bd9Sstevel@tonic-gate 	while ((p = wait(&status)) != pid && p != (pid_t)-1);
15417c478bd9Sstevel@tonic-gate 	if (p == pid) {
15427c478bd9Sstevel@tonic-gate 		status = (status >> 8) & CMDERR;
15437c478bd9Sstevel@tonic-gate 		if (status == CMDERR)
15447c478bd9Sstevel@tonic-gate 			status = -1;
15457c478bd9Sstevel@tonic-gate 	}
15467c478bd9Sstevel@tonic-gate 	else
15477c478bd9Sstevel@tonic-gate 		status = -1;
15487c478bd9Sstevel@tonic-gate 	return (status);
15497c478bd9Sstevel@tonic-gate }
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate /*
15527c478bd9Sstevel@tonic-gate  * return pointer to the full path name of the shell
15537c478bd9Sstevel@tonic-gate  *
15547c478bd9Sstevel@tonic-gate  * SHELL is read from the environment and must start with /
15557c478bd9Sstevel@tonic-gate  *
15567c478bd9Sstevel@tonic-gate  * if set-uid or set-gid then the executable and its containing
15577c478bd9Sstevel@tonic-gate  * directory must not be writable by the real user
15587c478bd9Sstevel@tonic-gate  *
15597c478bd9Sstevel@tonic-gate  * /usr/bin/sh is returned by default
15607c478bd9Sstevel@tonic-gate  */
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate char *
getshell()15637c478bd9Sstevel@tonic-gate getshell()
15647c478bd9Sstevel@tonic-gate {
15657c478bd9Sstevel@tonic-gate 	char	*s;
15667c478bd9Sstevel@tonic-gate 	char	*sh;
15677c478bd9Sstevel@tonic-gate 	uid_t	u;
15687c478bd9Sstevel@tonic-gate 	int	j;
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 	if (((sh = getenv("SHELL")) != 0) && *sh == '/') {
15717c478bd9Sstevel@tonic-gate 		if (u = getuid()) {
15727c478bd9Sstevel@tonic-gate 			if ((u != geteuid() || getgid() != getegid()) &&
15733d63ea05Sas145665 			    access(sh, 2) == 0)
15747c478bd9Sstevel@tonic-gate 				goto defshell;
15757c478bd9Sstevel@tonic-gate 			s = strrchr(sh, '/');
15767c478bd9Sstevel@tonic-gate 			*s = 0;
15777c478bd9Sstevel@tonic-gate 			j = access(sh, 2);
15787c478bd9Sstevel@tonic-gate 			*s = '/';
15797c478bd9Sstevel@tonic-gate 			if (!j) goto defshell;
15807c478bd9Sstevel@tonic-gate 		}
15817c478bd9Sstevel@tonic-gate 		return (sh);
15827c478bd9Sstevel@tonic-gate 	}
15837c478bd9Sstevel@tonic-gate defshell:
15847c478bd9Sstevel@tonic-gate 	return ("/usr/bin/sh");
15857c478bd9Sstevel@tonic-gate }
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate /*
15887c478bd9Sstevel@tonic-gate  * the following functions implement the added "-ls" option
15897c478bd9Sstevel@tonic-gate  */
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate #include <utmpx.h>
15927c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate struct		utmpx utmpx;
15957c478bd9Sstevel@tonic-gate #define	NMAX	(sizeof (utmpx.ut_name))
15967c478bd9Sstevel@tonic-gate #define	SCPYN(a, b)	(void) strncpy(a, b, NMAX)
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate #define	NUID	64
15997c478bd9Sstevel@tonic-gate #define	NGID	64
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate static struct ncache {
16027c478bd9Sstevel@tonic-gate 	int	id;
16037c478bd9Sstevel@tonic-gate 	char	name[NMAX+1];
16047c478bd9Sstevel@tonic-gate } nc[NUID], gc[NGID];
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate /*
16077c478bd9Sstevel@tonic-gate  * This function assumes that the password file is hashed
16087c478bd9Sstevel@tonic-gate  * (or some such) to allow fast access based on a name key.
16097c478bd9Sstevel@tonic-gate  */
16107c478bd9Sstevel@tonic-gate static char *
getname(uid_t uid)16117c478bd9Sstevel@tonic-gate getname(uid_t uid)
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate 	struct passwd *pw;
16147c478bd9Sstevel@tonic-gate 	int cp;
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate #if	(((NUID) & ((NUID) - 1)) != 0)
16177c478bd9Sstevel@tonic-gate 	cp = uid % (NUID);
16187c478bd9Sstevel@tonic-gate #else
16197c478bd9Sstevel@tonic-gate 	cp = uid & ((NUID) - 1);
16207c478bd9Sstevel@tonic-gate #endif
1621f48205beScasper 	if (nc[cp].id == uid && nc[cp].name[0])
16227c478bd9Sstevel@tonic-gate 		return (nc[cp].name);
16237c478bd9Sstevel@tonic-gate 	pw = getpwuid(uid);
16247c478bd9Sstevel@tonic-gate 	if (!pw)
16257c478bd9Sstevel@tonic-gate 		return (0);
16267c478bd9Sstevel@tonic-gate 	nc[cp].id = uid;
16277c478bd9Sstevel@tonic-gate 	SCPYN(nc[cp].name, pw->pw_name);
16287c478bd9Sstevel@tonic-gate 	return (nc[cp].name);
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate /*
16327c478bd9Sstevel@tonic-gate  * This function assumes that the group file is hashed
16337c478bd9Sstevel@tonic-gate  * (or some such) to allow fast access based on a name key.
16347c478bd9Sstevel@tonic-gate  */
16357c478bd9Sstevel@tonic-gate static char *
getgroup(gid_t gid)16367c478bd9Sstevel@tonic-gate getgroup(gid_t gid)
16377c478bd9Sstevel@tonic-gate {
16387c478bd9Sstevel@tonic-gate 	struct group *gr;
16397c478bd9Sstevel@tonic-gate 	int cp;
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate #if	(((NGID) & ((NGID) - 1)) != 0)
16427c478bd9Sstevel@tonic-gate 	cp = gid % (NGID);
16437c478bd9Sstevel@tonic-gate #else
16447c478bd9Sstevel@tonic-gate 	cp = gid & ((NGID) - 1);
16457c478bd9Sstevel@tonic-gate #endif
1646f48205beScasper 	if (gc[cp].id == gid && gc[cp].name[0])
16477c478bd9Sstevel@tonic-gate 		return (gc[cp].name);
16487c478bd9Sstevel@tonic-gate 	gr = getgrgid(gid);
16497c478bd9Sstevel@tonic-gate 	if (!gr)
16507c478bd9Sstevel@tonic-gate 		return (0);
16517c478bd9Sstevel@tonic-gate 	gc[cp].id = gid;
16527c478bd9Sstevel@tonic-gate 	SCPYN(gc[cp].name, gr->gr_name);
16537c478bd9Sstevel@tonic-gate 	return (gc[cp].name);
16547c478bd9Sstevel@tonic-gate }
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate #define	permoffset(who)		((who) * 3)
16577c478bd9Sstevel@tonic-gate #define	permission(who, type)	((type) >> permoffset(who))
16587c478bd9Sstevel@tonic-gate #define	kbytes(bytes)		(((bytes) + 1023) / 1024)
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate static int
list(file,stp)16617c478bd9Sstevel@tonic-gate list(file, stp)
16627c478bd9Sstevel@tonic-gate 	char *file;
16637c478bd9Sstevel@tonic-gate 	struct stat *stp;
16647c478bd9Sstevel@tonic-gate {
16657c478bd9Sstevel@tonic-gate 	char pmode[32], uname[32], gname[32], fsize[32], ftime[32];
1666fa9e4066Sahrens 	int trivial;
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate /*
16697c478bd9Sstevel@tonic-gate  * Each line below contains the relevant permission (column 1) and character
16707c478bd9Sstevel@tonic-gate  * shown when  the corresponding execute bit is either clear (column 2)
16717c478bd9Sstevel@tonic-gate  * or set (column 3)
16727c478bd9Sstevel@tonic-gate  * These permissions are as shown by ls(1b)
16737c478bd9Sstevel@tonic-gate  */
16747c478bd9Sstevel@tonic-gate 	static long special[] = {	S_ISUID, 'S', 's',
16757c478bd9Sstevel@tonic-gate 					S_ISGID, 'S', 's',
16767c478bd9Sstevel@tonic-gate 					S_ISVTX, 'T', 't' };
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	static time_t sixmonthsago = -1;
16797c478bd9Sstevel@tonic-gate #ifdef	S_IFLNK
16807c478bd9Sstevel@tonic-gate 	char flink[MAXPATHLEN + 1];
16817c478bd9Sstevel@tonic-gate #endif
16827c478bd9Sstevel@tonic-gate 	int who;
16837c478bd9Sstevel@tonic-gate 	char *cp;
16847c478bd9Sstevel@tonic-gate 	char *tailname;
16857c478bd9Sstevel@tonic-gate 	time_t now;
16867c478bd9Sstevel@tonic-gate 	long long ksize;
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	if (file == NULL || stp == NULL)
16897c478bd9Sstevel@tonic-gate 		return (-1);
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	(void) time(&now);
16927c478bd9Sstevel@tonic-gate 	if (sixmonthsago == -1)
16937c478bd9Sstevel@tonic-gate 		sixmonthsago = now - 6L*30L*24L*60L*60L;
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 	switch (stp->st_mode & S_IFMT) {
16967c478bd9Sstevel@tonic-gate #ifdef	S_IFDIR
16977c478bd9Sstevel@tonic-gate 	case S_IFDIR:	/* directory */
16987c478bd9Sstevel@tonic-gate 		pmode[0] = 'd';
16997c478bd9Sstevel@tonic-gate 		break;
17007c478bd9Sstevel@tonic-gate #endif
17017c478bd9Sstevel@tonic-gate #ifdef	S_IFCHR
17027c478bd9Sstevel@tonic-gate 	case S_IFCHR:	/* character special */
17037c478bd9Sstevel@tonic-gate 		pmode[0] = 'c';
17047c478bd9Sstevel@tonic-gate 		break;
17057c478bd9Sstevel@tonic-gate #endif
17067c478bd9Sstevel@tonic-gate #ifdef	S_IFBLK
17077c478bd9Sstevel@tonic-gate 	case S_IFBLK:	/* block special */
17087c478bd9Sstevel@tonic-gate 		pmode[0] = 'b';
17097c478bd9Sstevel@tonic-gate 		break;
17107c478bd9Sstevel@tonic-gate #endif
17117c478bd9Sstevel@tonic-gate #ifdef	S_IFIFO
17127c478bd9Sstevel@tonic-gate 	case S_IFIFO:	/* fifo special */
17137c478bd9Sstevel@tonic-gate 		pmode[0] = 'p';
17147c478bd9Sstevel@tonic-gate 		break;
17157c478bd9Sstevel@tonic-gate #endif
17167c478bd9Sstevel@tonic-gate #ifdef	S_IFLNK
17177c478bd9Sstevel@tonic-gate 	case S_IFLNK:	/* symbolic link */
17187c478bd9Sstevel@tonic-gate 		pmode[0] = 'l';
17197c478bd9Sstevel@tonic-gate 		break;
17207c478bd9Sstevel@tonic-gate #endif
17217c478bd9Sstevel@tonic-gate #ifdef	S_IFSOCK
17227c478bd9Sstevel@tonic-gate 	case S_IFSOCK:	/* socket */
17237c478bd9Sstevel@tonic-gate 		pmode[0] = 's';
17247c478bd9Sstevel@tonic-gate 		break;
17257c478bd9Sstevel@tonic-gate #endif
17267c478bd9Sstevel@tonic-gate #ifdef	S_IFDOOR
17277c478bd9Sstevel@tonic-gate 	case S_IFDOOR:	/* door */
17287c478bd9Sstevel@tonic-gate 		pmode[0] = 'D';
17297c478bd9Sstevel@tonic-gate 		break;
17307c478bd9Sstevel@tonic-gate #endif
17317c478bd9Sstevel@tonic-gate #ifdef	S_IFREG
17327c478bd9Sstevel@tonic-gate 	case S_IFREG:	/* regular */
17337c478bd9Sstevel@tonic-gate 		pmode[0] = '-';
17347c478bd9Sstevel@tonic-gate 		break;
17357c478bd9Sstevel@tonic-gate #endif
17367c478bd9Sstevel@tonic-gate 	default:
17377c478bd9Sstevel@tonic-gate 		pmode[0] = '?';
17387c478bd9Sstevel@tonic-gate 		break;
17397c478bd9Sstevel@tonic-gate 	}
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	for (who = 0; who < 3; who++) {
17427c478bd9Sstevel@tonic-gate 		int is_exec =  stp->st_mode & permission(who, S_IEXEC)? 1 : 0;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 		if (stp->st_mode & permission(who, S_IREAD))
17457c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 1] = 'r';
17467c478bd9Sstevel@tonic-gate 		else
17477c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 1] = '-';
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 		if (stp->st_mode & permission(who, S_IWRITE))
17507c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 2] = 'w';
17517c478bd9Sstevel@tonic-gate 		else
17527c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 2] = '-';
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 		if (stp->st_mode & special[who * 3])
17557c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 3] =
17567c478bd9Sstevel@tonic-gate 				special[who * 3 + 1 + is_exec];
17577c478bd9Sstevel@tonic-gate 		else if (is_exec)
17587c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 3] = 'x';
17597c478bd9Sstevel@tonic-gate 		else
17607c478bd9Sstevel@tonic-gate 			pmode[permoffset(who) + 3] = '-';
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	/*
17647c478bd9Sstevel@tonic-gate 	 * Need to get the tail of the file name, since we have
17657c478bd9Sstevel@tonic-gate 	 * already chdir()ed into the directory of the file
17667c478bd9Sstevel@tonic-gate 	 */
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	tailname = gettail(file);
17697c478bd9Sstevel@tonic-gate 
1770fa9e4066Sahrens 	trivial = acl_trivial(tailname);
1771fa9e4066Sahrens 	if (trivial == -1)
1772fa9e4066Sahrens 		trivial =  0;
1773fa9e4066Sahrens 
1774fa9e4066Sahrens 	if (trivial == 1)
17757c478bd9Sstevel@tonic-gate 		pmode[permoffset(who) + 1] = '+';
17767c478bd9Sstevel@tonic-gate 	else
17777c478bd9Sstevel@tonic-gate 		pmode[permoffset(who) + 1] = ' ';
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	pmode[permoffset(who) + 2] = '\0';
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	/*
17827c478bd9Sstevel@tonic-gate 	 * Prepare uname and gname.  Always add a space afterwards
17837c478bd9Sstevel@tonic-gate 	 * to keep columns from running together.
17847c478bd9Sstevel@tonic-gate 	 */
17857c478bd9Sstevel@tonic-gate 	cp = getname(stp->st_uid);
17867c478bd9Sstevel@tonic-gate 	if (cp != NULL)
17877c478bd9Sstevel@tonic-gate 		(void) sprintf(uname, "%-8s ", cp);
17887c478bd9Sstevel@tonic-gate 	else
1789f48205beScasper 		(void) sprintf(uname, "%-8u ", stp->st_uid);
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	cp = getgroup(stp->st_gid);
17927c478bd9Sstevel@tonic-gate 	if (cp != NULL)
17937c478bd9Sstevel@tonic-gate 		(void) sprintf(gname, "%-8s ", cp);
17947c478bd9Sstevel@tonic-gate 	else
1795f48205beScasper 		(void) sprintf(gname, "%-8u ", stp->st_gid);
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	if (pmode[0] == 'b' || pmode[0] == 'c')
17987c478bd9Sstevel@tonic-gate 		(void) sprintf(fsize, "%3ld,%4ld",
17997c478bd9Sstevel@tonic-gate 			major(stp->st_rdev), minor(stp->st_rdev));
18007c478bd9Sstevel@tonic-gate 	else {
18017c478bd9Sstevel@tonic-gate 		(void) sprintf(fsize, (stp->st_size < 100000000) ?
18027c478bd9Sstevel@tonic-gate 			"%8lld" : "%lld", stp->st_size);
18037c478bd9Sstevel@tonic-gate #ifdef	S_IFLNK
18047c478bd9Sstevel@tonic-gate 		if (pmode[0] == 'l') {
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 			who = readlink(tailname, flink, sizeof (flink) - 1);
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 			if (who >= 0)
18107c478bd9Sstevel@tonic-gate 				flink[who] = '\0';
18117c478bd9Sstevel@tonic-gate 			else
18127c478bd9Sstevel@tonic-gate 				flink[0] = '\0';
18137c478bd9Sstevel@tonic-gate 		}
18147c478bd9Sstevel@tonic-gate #endif
18157c478bd9Sstevel@tonic-gate 	}
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	cp = ctime(&stp->st_mtime);
18187c478bd9Sstevel@tonic-gate 	if (stp->st_mtime < sixmonthsago || stp->st_mtime > now)
18197c478bd9Sstevel@tonic-gate 		(void) sprintf(ftime, "%-7.7s %-4.4s", cp + 4, cp + 20);
18207c478bd9Sstevel@tonic-gate 	else
18217c478bd9Sstevel@tonic-gate 		(void) sprintf(ftime, "%-12.12s", cp + 4);
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	(void) printf((stp->st_ino < 100000) ? "%5llu " :
18247c478bd9Sstevel@tonic-gate 		"%llu ", stp->st_ino);  /* inode #	*/
18257c478bd9Sstevel@tonic-gate #ifdef	S_IFSOCK
18267c478bd9Sstevel@tonic-gate 	ksize = (long long) kbytes(ldbtob(stp->st_blocks)); /* kbytes */
18277c478bd9Sstevel@tonic-gate #else
18287c478bd9Sstevel@tonic-gate 	ksize = (long long) kbytes(stp->st_size); /* kbytes */
18297c478bd9Sstevel@tonic-gate #endif
18307c478bd9Sstevel@tonic-gate 	(void) printf((ksize < 10000) ? "%4lld " : "%lld ", ksize);
18317c478bd9Sstevel@tonic-gate 	(void) printf("%s %2ld %s%s%s %s %s%s%s\n",
18327c478bd9Sstevel@tonic-gate 		pmode,					/* protection	*/
18337c478bd9Sstevel@tonic-gate 		stp->st_nlink,				/* # of links	*/
18347c478bd9Sstevel@tonic-gate 		uname,					/* owner	*/
18357c478bd9Sstevel@tonic-gate 		gname,					/* group	*/
18367c478bd9Sstevel@tonic-gate 		fsize,					/* # of bytes	*/
18377c478bd9Sstevel@tonic-gate 		ftime,					/* modify time	*/
18387c478bd9Sstevel@tonic-gate 		file,					/* name		*/
18397c478bd9Sstevel@tonic-gate #ifdef	S_IFLNK
18407c478bd9Sstevel@tonic-gate 		(pmode[0] == 'l') ? " -> " : "",
18417c478bd9Sstevel@tonic-gate 		(pmode[0] == 'l') ? flink  : ""		/* symlink	*/
18427c478bd9Sstevel@tonic-gate #else
18437c478bd9Sstevel@tonic-gate 		"",
18447c478bd9Sstevel@tonic-gate 		""
18457c478bd9Sstevel@tonic-gate #endif
18467c478bd9Sstevel@tonic-gate );
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	return (0);
18497c478bd9Sstevel@tonic-gate }
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate static char *
new_string(char * s)18527c478bd9Sstevel@tonic-gate new_string(char *s)
18537c478bd9Sstevel@tonic-gate {
18547c478bd9Sstevel@tonic-gate 	char *p = strdup(s);
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	if (p)
18577c478bd9Sstevel@tonic-gate 		return (p);
18587c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%s: out of memory\n"), cmdname);
18597c478bd9Sstevel@tonic-gate 	exit(1);
18607c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
18617c478bd9Sstevel@tonic-gate }
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate /*
18647c478bd9Sstevel@tonic-gate  * Read remote file system types from REMOTE_FS into the
18657c478bd9Sstevel@tonic-gate  * remote_fstypes array.
18667c478bd9Sstevel@tonic-gate  */
18677c478bd9Sstevel@tonic-gate static void
init_remote_fs()18687c478bd9Sstevel@tonic-gate init_remote_fs()
18697c478bd9Sstevel@tonic-gate {
18707c478bd9Sstevel@tonic-gate 	FILE    *fp;
18717c478bd9Sstevel@tonic-gate 	char    line_buf[LINEBUF_SIZE];
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	if ((fp = fopen(REMOTE_FS, "r")) == NULL) {
18747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18757c478bd9Sstevel@tonic-gate 		    gettext("%s: Warning: can't open %s, ignored\n"),
18767c478bd9Sstevel@tonic-gate 		    REMOTE_FS, cmdname);
18777c478bd9Sstevel@tonic-gate 		/* Use default string name for NFS */
18787c478bd9Sstevel@tonic-gate 		remote_fstypes[fstype_index++] = "nfs";
18797c478bd9Sstevel@tonic-gate 		return;
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	while (fgets(line_buf, sizeof (line_buf), fp) != NULL) {
18837c478bd9Sstevel@tonic-gate 		char buf[LINEBUF_SIZE];
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 		/* LINTED - unbounded string specifier */
18867c478bd9Sstevel@tonic-gate 		(void) sscanf(line_buf, "%s", buf);
18877c478bd9Sstevel@tonic-gate 		remote_fstypes[fstype_index++] = new_string(buf);
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 		if (fstype_index == N_FSTYPES)
18907c478bd9Sstevel@tonic-gate 			break;
18917c478bd9Sstevel@tonic-gate 	}
18927c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate #define	NPERM	30			/* Largest machine */
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate /*
18987c478bd9Sstevel@tonic-gate  * The PERM struct is the machine that builds permissions.  The p_special
18997c478bd9Sstevel@tonic-gate  * field contains what permissions need to be checked at run-time in
19007c478bd9Sstevel@tonic-gate  * getmode().  This is one of 'X', 'u', 'g', or 'o'.  It contains '\0' to
19017c478bd9Sstevel@tonic-gate  * indicate normal processing.
19027c478bd9Sstevel@tonic-gate  */
19037c478bd9Sstevel@tonic-gate typedef	struct	PERMST	{
19047c478bd9Sstevel@tonic-gate 	ushort_t	p_who;		/* Range of permission (e.g. ugo) */
19057c478bd9Sstevel@tonic-gate 	ushort_t	p_perm;		/* Bits to turn on, off, assign */
19067c478bd9Sstevel@tonic-gate 	uchar_t		p_op;		/* Operation: + - = */
19077c478bd9Sstevel@tonic-gate 	uchar_t		p_special;	/* Special handling? */
19087c478bd9Sstevel@tonic-gate }	PERMST;
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate #ifndef	S_ISVTX
19117c478bd9Sstevel@tonic-gate #define	S_ISVTX	0			/* Not .1 */
19127c478bd9Sstevel@tonic-gate #endif
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate /* Mask values */
19157c478bd9Sstevel@tonic-gate #define	P_A	(S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* allbits */
19167c478bd9Sstevel@tonic-gate #define	P_U	(S_ISUID|S_ISVTX|S_IRWXU)		/* user */
19177c478bd9Sstevel@tonic-gate #define	P_G	(S_ISGID|S_ISVTX|S_IRWXG)		/* group */
19187c478bd9Sstevel@tonic-gate #define	P_O	(S_ISVTX|S_IRWXO)			/* other */
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate static	int	iswho(int c);
19217c478bd9Sstevel@tonic-gate static	int	isop(int c);
19227c478bd9Sstevel@tonic-gate static	int	isperm(PERMST *pp, int c);
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate static	PERMST	machine[NPERM];		/* Permission construction machine */
19257c478bd9Sstevel@tonic-gate static	PERMST	*endp;			/* Last used PERM structure */
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate static	uint_t	nowho;			/* No who for this mode (DOS kludge) */
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate /*
19307c478bd9Sstevel@tonic-gate  * Read an ASCII string containing the symbolic/octal mode and
19317c478bd9Sstevel@tonic-gate  * compile an automaton that recognizes it.  The return value
19327c478bd9Sstevel@tonic-gate  * is NULL if everything is OK, otherwise it is -1.
19337c478bd9Sstevel@tonic-gate  */
19347c478bd9Sstevel@tonic-gate static int
readmode(ascmode)19357c478bd9Sstevel@tonic-gate readmode(ascmode)
19367c478bd9Sstevel@tonic-gate const char *ascmode;
19377c478bd9Sstevel@tonic-gate {
19387c478bd9Sstevel@tonic-gate 	const char *amode = ascmode;
19397c478bd9Sstevel@tonic-gate 	PERMST *pp;
19407c478bd9Sstevel@tonic-gate 	int seen_X;
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 	nowho = 0;
19437c478bd9Sstevel@tonic-gate 	seen_X = 0;
19447c478bd9Sstevel@tonic-gate 	pp = &machine[0];
19457c478bd9Sstevel@tonic-gate 	if (*amode >= '0' && *amode <= '7') {
19467c478bd9Sstevel@tonic-gate 		int mode;
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 		mode = 0;
19497c478bd9Sstevel@tonic-gate 		while (*amode >= '0' && *amode <= '7')
19507c478bd9Sstevel@tonic-gate 			mode = (mode<<3) + *amode++ - '0';
19517c478bd9Sstevel@tonic-gate 		if (*amode != '\0')
19527c478bd9Sstevel@tonic-gate 			return (-1);
19537c478bd9Sstevel@tonic-gate #if	S_ISUID != 04000 || S_ISGID != 02000 || \
19547c478bd9Sstevel@tonic-gate 	S_IRUSR != 0400 || S_IWUSR != 0200 || S_IXUSR != 0100 || \
19557c478bd9Sstevel@tonic-gate 	S_IRGRP != 0040 || S_IWGRP != 0020 || S_IXGRP != 0010 || \
19567c478bd9Sstevel@tonic-gate 	S_IROTH != 0004 || S_IWOTH != 0002 || S_IXOTH != 0001
19577c478bd9Sstevel@tonic-gate 		/*
19587c478bd9Sstevel@tonic-gate 		 * There is no requirement of the octal mode bits being
19597c478bd9Sstevel@tonic-gate 		 * the same as the S_ macros.
19607c478bd9Sstevel@tonic-gate 		 */
19617c478bd9Sstevel@tonic-gate 	{
19627c478bd9Sstevel@tonic-gate 		mode_t mapping[] = {
19637c478bd9Sstevel@tonic-gate 			S_IXOTH, S_IWOTH, S_IROTH,
19647c478bd9Sstevel@tonic-gate 			S_IXGRP, S_IWGRP, S_IRGRP,
19657c478bd9Sstevel@tonic-gate 			S_IXUSR, S_IWUSR, S_IRUSR,
19667c478bd9Sstevel@tonic-gate 			S_ISGID, S_ISUID,
19677c478bd9Sstevel@tonic-gate 			0
19687c478bd9Sstevel@tonic-gate 		};
19697c478bd9Sstevel@tonic-gate 		int i, newmode = 0;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 		for (i = 0; mapping[i] != 0; i++)
19727c478bd9Sstevel@tonic-gate 			if (mode & (1<<i))
19737c478bd9Sstevel@tonic-gate 				newmode |= mapping[i];
19747c478bd9Sstevel@tonic-gate 		mode = newmode;
19757c478bd9Sstevel@tonic-gate 	}
19767c478bd9Sstevel@tonic-gate #endif
19777c478bd9Sstevel@tonic-gate 		pp->p_who = P_A;
19787c478bd9Sstevel@tonic-gate 		pp->p_perm = mode;
19797c478bd9Sstevel@tonic-gate 		pp->p_op = '=';
19807c478bd9Sstevel@tonic-gate 	} else	for (;;) {
19817c478bd9Sstevel@tonic-gate 		int t;
19827c478bd9Sstevel@tonic-gate 		int who = 0;
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 		while ((t = iswho(*amode)) != 0) {
19857c478bd9Sstevel@tonic-gate 			++amode;
19867c478bd9Sstevel@tonic-gate 			who |= t;
19877c478bd9Sstevel@tonic-gate 		}
19887c478bd9Sstevel@tonic-gate 		if (who == 0) {
19897c478bd9Sstevel@tonic-gate 			mode_t currmask;
19907c478bd9Sstevel@tonic-gate 			(void) umask(currmask = umask((mode_t)0));
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 			/*
19937c478bd9Sstevel@tonic-gate 			 * If no who specified, must use contents of
19947c478bd9Sstevel@tonic-gate 			 * umask to determine which bits to flip.  This
19957c478bd9Sstevel@tonic-gate 			 * is POSIX/V7/BSD behaviour, but not SVID.
19967c478bd9Sstevel@tonic-gate 			 */
19977c478bd9Sstevel@tonic-gate 			who = (~currmask)&P_A;
19987c478bd9Sstevel@tonic-gate 			++nowho;
19997c478bd9Sstevel@tonic-gate 		} else
20007c478bd9Sstevel@tonic-gate 			nowho = 0;
20017c478bd9Sstevel@tonic-gate 	samewho:
20027c478bd9Sstevel@tonic-gate 		if (!isop(pp->p_op = *amode++))
20037c478bd9Sstevel@tonic-gate 			return (-1);
20047c478bd9Sstevel@tonic-gate 		pp->p_perm = 0;
20057c478bd9Sstevel@tonic-gate 		pp->p_special = 0;
20067c478bd9Sstevel@tonic-gate 		while ((t = isperm(pp, *amode)) != 0) {
20077c478bd9Sstevel@tonic-gate 			if (pp->p_special == 'X') {
20087c478bd9Sstevel@tonic-gate 				seen_X = 1;
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 				if (pp->p_perm != 0) {
20117c478bd9Sstevel@tonic-gate 					ushort_t op;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 					/*
20147c478bd9Sstevel@tonic-gate 					 * Remember the 'who' for the previous
20157c478bd9Sstevel@tonic-gate 					 * transformation.
20167c478bd9Sstevel@tonic-gate 					 */
20177c478bd9Sstevel@tonic-gate 					pp->p_who = who;
20187c478bd9Sstevel@tonic-gate 					pp->p_special = 0;
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 					op = pp->p_op;
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 					/* Keep 'X' separate */
20237c478bd9Sstevel@tonic-gate 					++pp;
20247c478bd9Sstevel@tonic-gate 					pp->p_special = 'X';
20257c478bd9Sstevel@tonic-gate 					pp->p_op = op;
20267c478bd9Sstevel@tonic-gate 				}
20277c478bd9Sstevel@tonic-gate 			} else if (seen_X) {
20287c478bd9Sstevel@tonic-gate 				ushort_t op;
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 				/* Remember the 'who' for the X */
20317c478bd9Sstevel@tonic-gate 				pp->p_who = who;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 				op = pp->p_op;
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 				/* Keep 'X' separate */
20367c478bd9Sstevel@tonic-gate 				++pp;
20377c478bd9Sstevel@tonic-gate 				pp->p_perm = 0;
20387c478bd9Sstevel@tonic-gate 				pp->p_special = 0;
20397c478bd9Sstevel@tonic-gate 				pp->p_op = op;
20407c478bd9Sstevel@tonic-gate 			}
20417c478bd9Sstevel@tonic-gate 			++amode;
20427c478bd9Sstevel@tonic-gate 			pp->p_perm |= t;
20437c478bd9Sstevel@tonic-gate 		}
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 		/*
20467c478bd9Sstevel@tonic-gate 		 * These returned 0, but were actually parsed, so
20477c478bd9Sstevel@tonic-gate 		 * don't look at them again.
20487c478bd9Sstevel@tonic-gate 		 */
20497c478bd9Sstevel@tonic-gate 		switch (pp->p_special) {
20507c478bd9Sstevel@tonic-gate 		case 'u':
20517c478bd9Sstevel@tonic-gate 		case 'g':
20527c478bd9Sstevel@tonic-gate 		case 'o':
20537c478bd9Sstevel@tonic-gate 			++amode;
20547c478bd9Sstevel@tonic-gate 			break;
20557c478bd9Sstevel@tonic-gate 		}
20567c478bd9Sstevel@tonic-gate 		pp->p_who = who;
20577c478bd9Sstevel@tonic-gate 		switch (*amode) {
20587c478bd9Sstevel@tonic-gate 		case '\0':
20597c478bd9Sstevel@tonic-gate 			break;
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 		case ',':
20627c478bd9Sstevel@tonic-gate 			++amode;
20637c478bd9Sstevel@tonic-gate 			++pp;
20647c478bd9Sstevel@tonic-gate 			continue;
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 		default:
20677c478bd9Sstevel@tonic-gate 			++pp;
20687c478bd9Sstevel@tonic-gate 			goto samewho;
20697c478bd9Sstevel@tonic-gate 		}
20707c478bd9Sstevel@tonic-gate 		break;
20717c478bd9Sstevel@tonic-gate 	}
20727c478bd9Sstevel@tonic-gate 	endp = pp;
20737c478bd9Sstevel@tonic-gate 	return (NULL);
20747c478bd9Sstevel@tonic-gate }
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate /*
20777c478bd9Sstevel@tonic-gate  * Given a character from the mode, return the associated
20787c478bd9Sstevel@tonic-gate  * value as who (user designation) mask or 0 if this isn't valid.
20797c478bd9Sstevel@tonic-gate  */
20807c478bd9Sstevel@tonic-gate static int
iswho(c)20817c478bd9Sstevel@tonic-gate iswho(c)
20827c478bd9Sstevel@tonic-gate int c;
20837c478bd9Sstevel@tonic-gate {
20847c478bd9Sstevel@tonic-gate 	switch (c) {
20857c478bd9Sstevel@tonic-gate 	case 'a':
20867c478bd9Sstevel@tonic-gate 		return (P_A);
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	case 'u':
20897c478bd9Sstevel@tonic-gate 		return (P_U);
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	case 'g':
20927c478bd9Sstevel@tonic-gate 		return (P_G);
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	case 'o':
20957c478bd9Sstevel@tonic-gate 		return (P_O);
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	default:
20987c478bd9Sstevel@tonic-gate 		return (0);
20997c478bd9Sstevel@tonic-gate 	}
21007c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
21017c478bd9Sstevel@tonic-gate }
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate /*
21047c478bd9Sstevel@tonic-gate  * Return non-zero if this is a valid op code
21057c478bd9Sstevel@tonic-gate  * in a symbolic mode.
21067c478bd9Sstevel@tonic-gate  */
21077c478bd9Sstevel@tonic-gate static int
isop(c)21087c478bd9Sstevel@tonic-gate isop(c)
21097c478bd9Sstevel@tonic-gate int c;
21107c478bd9Sstevel@tonic-gate {
21117c478bd9Sstevel@tonic-gate 	switch (c) {
21127c478bd9Sstevel@tonic-gate 	case '+':
21137c478bd9Sstevel@tonic-gate 	case '-':
21147c478bd9Sstevel@tonic-gate 	case '=':
21157c478bd9Sstevel@tonic-gate 		return (1);
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	default:
21187c478bd9Sstevel@tonic-gate 		return (0);
21197c478bd9Sstevel@tonic-gate 	}
21207c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
21217c478bd9Sstevel@tonic-gate }
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate /*
21247c478bd9Sstevel@tonic-gate  * Return the permission bits implied by this character or 0
21257c478bd9Sstevel@tonic-gate  * if it isn't valid.  Also returns 0 when the pseudo-permissions 'u', 'g', or
21267c478bd9Sstevel@tonic-gate  * 'o' are used, and sets pp->p_special to the one used.
21277c478bd9Sstevel@tonic-gate  */
21287c478bd9Sstevel@tonic-gate static int
isperm(pp,c)21297c478bd9Sstevel@tonic-gate isperm(pp, c)
21307c478bd9Sstevel@tonic-gate PERMST *pp;
21317c478bd9Sstevel@tonic-gate int c;
21327c478bd9Sstevel@tonic-gate {
21337c478bd9Sstevel@tonic-gate 	switch (c) {
21347c478bd9Sstevel@tonic-gate 	case 'u':
21357c478bd9Sstevel@tonic-gate 	case 'g':
21367c478bd9Sstevel@tonic-gate 	case 'o':
21377c478bd9Sstevel@tonic-gate 		pp->p_special = c;
21387c478bd9Sstevel@tonic-gate 		return (0);
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	case 'r':
21417c478bd9Sstevel@tonic-gate 		return (S_IRUSR|S_IRGRP|S_IROTH);
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	case 'w':
21447c478bd9Sstevel@tonic-gate 		return (S_IWUSR|S_IWGRP|S_IWOTH);
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	case 'x':
21477c478bd9Sstevel@tonic-gate 		return (S_IXUSR|S_IXGRP|S_IXOTH);
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate #if S_ISVTX != 0
21507c478bd9Sstevel@tonic-gate 	case 't':
21517c478bd9Sstevel@tonic-gate 		return (S_ISVTX);
21527c478bd9Sstevel@tonic-gate #endif
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 	case 'X':
21557c478bd9Sstevel@tonic-gate 		pp->p_special = 'X';
21567c478bd9Sstevel@tonic-gate 		return (S_IXUSR|S_IXGRP|S_IXOTH);
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate #if S_ISVTX != 0
21597c478bd9Sstevel@tonic-gate 	case 'a':
21607c478bd9Sstevel@tonic-gate 		return (S_ISVTX);
21617c478bd9Sstevel@tonic-gate #endif
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	case 'h':
21647c478bd9Sstevel@tonic-gate 		return (S_ISUID);
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	/*
21677c478bd9Sstevel@tonic-gate 	 * This change makes:
21687c478bd9Sstevel@tonic-gate 	 *	chmod +s file
21697c478bd9Sstevel@tonic-gate 	 * set the system bit on dos but means that
21707c478bd9Sstevel@tonic-gate 	 *	chmod u+s file
21717c478bd9Sstevel@tonic-gate 	 *	chmod g+s file
21727c478bd9Sstevel@tonic-gate 	 *	chmod a+s file
21737c478bd9Sstevel@tonic-gate 	 * are all like UNIX.
21747c478bd9Sstevel@tonic-gate 	 */
21757c478bd9Sstevel@tonic-gate 	case 's':
21767c478bd9Sstevel@tonic-gate 		return (nowho ? S_ISGID : S_ISGID|S_ISUID);
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	default:
21797c478bd9Sstevel@tonic-gate 		return (0);
21807c478bd9Sstevel@tonic-gate 	}
21817c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
21827c478bd9Sstevel@tonic-gate }
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate /*
21857c478bd9Sstevel@tonic-gate  * Execute the automaton that is created by readmode()
21867c478bd9Sstevel@tonic-gate  * to generate the final mode that will be used.  This
21877c478bd9Sstevel@tonic-gate  * code is passed a starting mode that is usually the original
21887c478bd9Sstevel@tonic-gate  * mode of the file being changed (or 0).  Note that this mode must contain
21897c478bd9Sstevel@tonic-gate  * the file-type bits as well, so that S_ISDIR will succeed on directories.
21907c478bd9Sstevel@tonic-gate  */
21917c478bd9Sstevel@tonic-gate static mode_t
getmode(mode_t startmode)21927c478bd9Sstevel@tonic-gate getmode(mode_t startmode)
21937c478bd9Sstevel@tonic-gate {
21947c478bd9Sstevel@tonic-gate 	PERMST *pp;
21957c478bd9Sstevel@tonic-gate 	mode_t temp;
21967c478bd9Sstevel@tonic-gate 	mode_t perm;
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	for (pp = &machine[0]; pp <= endp; ++pp) {
21997c478bd9Sstevel@tonic-gate 		perm = (mode_t)0;
22007c478bd9Sstevel@tonic-gate 		/*
22017c478bd9Sstevel@tonic-gate 		 * For the special modes 'u', 'g' and 'o', the named portion
22027c478bd9Sstevel@tonic-gate 		 * of the mode refers to after the previous clause has been
22037c478bd9Sstevel@tonic-gate 		 * processed, while the 'X' mode refers to the contents of the
22047c478bd9Sstevel@tonic-gate 		 * mode before any clauses have been processed.
22057c478bd9Sstevel@tonic-gate 		 *
22067c478bd9Sstevel@tonic-gate 		 * References: P1003.2/D11.2, Section 4.7.7,
22077c478bd9Sstevel@tonic-gate 		 *  lines 2568-2570, 2578-2583
22087c478bd9Sstevel@tonic-gate 		 */
22097c478bd9Sstevel@tonic-gate 		switch (pp->p_special) {
22107c478bd9Sstevel@tonic-gate 		case 'u':
22117c478bd9Sstevel@tonic-gate 			temp = startmode & S_IRWXU;
22127c478bd9Sstevel@tonic-gate 			if (temp & (S_IRUSR|S_IRGRP|S_IROTH))
22137c478bd9Sstevel@tonic-gate 				perm |= ((S_IRUSR|S_IRGRP|S_IROTH) &
22147c478bd9Sstevel@tonic-gate 				    pp->p_who);
22157c478bd9Sstevel@tonic-gate 			if (temp & (S_IWUSR|S_IWGRP|S_IWOTH))
22167c478bd9Sstevel@tonic-gate 				perm |= ((S_IWUSR|S_IWGRP|S_IWOTH) & pp->p_who);
22177c478bd9Sstevel@tonic-gate 			if (temp & (S_IXUSR|S_IXGRP|S_IXOTH))
22187c478bd9Sstevel@tonic-gate 				perm |= ((S_IXUSR|S_IXGRP|S_IXOTH) & pp->p_who);
22197c478bd9Sstevel@tonic-gate 			break;
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 		case 'g':
22227c478bd9Sstevel@tonic-gate 			temp = startmode & S_IRWXG;
22237c478bd9Sstevel@tonic-gate 			if (temp & (S_IRUSR|S_IRGRP|S_IROTH))
22247c478bd9Sstevel@tonic-gate 				perm |= ((S_IRUSR|S_IRGRP|S_IROTH) & pp->p_who);
22257c478bd9Sstevel@tonic-gate 			if (temp & (S_IWUSR|S_IWGRP|S_IWOTH))
22267c478bd9Sstevel@tonic-gate 				perm |= ((S_IWUSR|S_IWGRP|S_IWOTH) & pp->p_who);
22277c478bd9Sstevel@tonic-gate 			if (temp & (S_IXUSR|S_IXGRP|S_IXOTH))
22287c478bd9Sstevel@tonic-gate 				perm |= ((S_IXUSR|S_IXGRP|S_IXOTH) & pp->p_who);
22297c478bd9Sstevel@tonic-gate 			break;
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 		case 'o':
22327c478bd9Sstevel@tonic-gate 			temp = startmode & S_IRWXO;
22337c478bd9Sstevel@tonic-gate 			if (temp & (S_IRUSR|S_IRGRP|S_IROTH))
22347c478bd9Sstevel@tonic-gate 				perm |= ((S_IRUSR|S_IRGRP|S_IROTH) & pp->p_who);
22357c478bd9Sstevel@tonic-gate 			if (temp & (S_IWUSR|S_IWGRP|S_IWOTH))
22367c478bd9Sstevel@tonic-gate 				perm |= ((S_IWUSR|S_IWGRP|S_IWOTH) & pp->p_who);
22377c478bd9Sstevel@tonic-gate 			if (temp & (S_IXUSR|S_IXGRP|S_IXOTH))
22387c478bd9Sstevel@tonic-gate 				perm |= ((S_IXUSR|S_IXGRP|S_IXOTH) & pp->p_who);
22397c478bd9Sstevel@tonic-gate 			break;
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 		case 'X':
22427c478bd9Sstevel@tonic-gate 			perm = pp->p_perm;
22437c478bd9Sstevel@tonic-gate 			break;
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 		default:
22467c478bd9Sstevel@tonic-gate 			perm = pp->p_perm;
22477c478bd9Sstevel@tonic-gate 			break;
22487c478bd9Sstevel@tonic-gate 		}
22497c478bd9Sstevel@tonic-gate 		switch (pp->p_op) {
22507c478bd9Sstevel@tonic-gate 		case '-':
22517c478bd9Sstevel@tonic-gate 			startmode &= ~(perm & pp->p_who);
22527c478bd9Sstevel@tonic-gate 			break;
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 		case '=':
22557c478bd9Sstevel@tonic-gate 			startmode &= ~pp->p_who;
22567c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
22577c478bd9Sstevel@tonic-gate 		case '+':
22587c478bd9Sstevel@tonic-gate 			startmode |= (perm & pp->p_who);
22597c478bd9Sstevel@tonic-gate 			break;
22607c478bd9Sstevel@tonic-gate 		}
22617c478bd9Sstevel@tonic-gate 	}
22627c478bd9Sstevel@tonic-gate 	return (startmode);
22637c478bd9Sstevel@tonic-gate }
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate /*
22667c478bd9Sstevel@tonic-gate  * Returns the last component of a path name, unless it is
22677c478bd9Sstevel@tonic-gate  * an absolute path, in which case it returns the whole path
22687c478bd9Sstevel@tonic-gate  */
22697c478bd9Sstevel@tonic-gate static char
gettail(char * fname)22707c478bd9Sstevel@tonic-gate *gettail(char *fname)
22717c478bd9Sstevel@tonic-gate {
22727c478bd9Sstevel@tonic-gate 	char	*base = fname;
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 	if (*fname != '/') {
22757c478bd9Sstevel@tonic-gate 		if ((base = strrchr(fname, '/')) != NULL)
22767c478bd9Sstevel@tonic-gate 			base++;
22777c478bd9Sstevel@tonic-gate 		else
22787c478bd9Sstevel@tonic-gate 			base = fname;
22797c478bd9Sstevel@tonic-gate 	}
22807c478bd9Sstevel@tonic-gate 	return (base);
22817c478bd9Sstevel@tonic-gate }
2282