xref: /freebsd/usr.bin/find/function.c (revision 9b50d9027575220cb6dd09b3e62f03f511e908b8)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1990, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * Cimarron D. Taylor of the University of California, Berkeley.
79b50d902SRodney W. Grimes  *
89b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes  * are met:
119b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
169b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
179b50d902SRodney W. Grimes  *    must display the following acknowledgement:
189b50d902SRodney W. Grimes  *	This product includes software developed by the University of
199b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
209b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
219b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
229b50d902SRodney W. Grimes  *    without specific prior written permission.
239b50d902SRodney W. Grimes  *
249b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349b50d902SRodney W. Grimes  * SUCH DAMAGE.
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #ifndef lint
389b50d902SRodney W. Grimes static char sccsid[] = "@(#)function.c	8.6 (Berkeley) 4/1/94";
399b50d902SRodney W. Grimes #endif /* not lint */
409b50d902SRodney W. Grimes 
419b50d902SRodney W. Grimes #include <sys/param.h>
429b50d902SRodney W. Grimes #include <sys/ucred.h>
439b50d902SRodney W. Grimes #include <sys/stat.h>
449b50d902SRodney W. Grimes #include <sys/wait.h>
459b50d902SRodney W. Grimes #include <sys/mount.h>
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <err.h>
489b50d902SRodney W. Grimes #include <errno.h>
499b50d902SRodney W. Grimes #include <fnmatch.h>
509b50d902SRodney W. Grimes #include <fts.h>
519b50d902SRodney W. Grimes #include <grp.h>
529b50d902SRodney W. Grimes #include <pwd.h>
539b50d902SRodney W. Grimes #include <stdio.h>
549b50d902SRodney W. Grimes #include <stdlib.h>
559b50d902SRodney W. Grimes #include <string.h>
569b50d902SRodney W. Grimes #include <tzfile.h>
579b50d902SRodney W. Grimes #include <unistd.h>
589b50d902SRodney W. Grimes 
599b50d902SRodney W. Grimes #include "find.h"
609b50d902SRodney W. Grimes 
619b50d902SRodney W. Grimes #define	COMPARE(a, b) {							\
629b50d902SRodney W. Grimes 	switch (plan->flags) {						\
639b50d902SRodney W. Grimes 	case F_EQUAL:							\
649b50d902SRodney W. Grimes 		return (a == b);					\
659b50d902SRodney W. Grimes 	case F_LESSTHAN:						\
669b50d902SRodney W. Grimes 		return (a < b);						\
679b50d902SRodney W. Grimes 	case F_GREATER:							\
689b50d902SRodney W. Grimes 		return (a > b);						\
699b50d902SRodney W. Grimes 	default:							\
709b50d902SRodney W. Grimes 		abort();						\
719b50d902SRodney W. Grimes 	}								\
729b50d902SRodney W. Grimes }
739b50d902SRodney W. Grimes 
749b50d902SRodney W. Grimes static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes /*
779b50d902SRodney W. Grimes  * find_parsenum --
789b50d902SRodney W. Grimes  *	Parse a string of the form [+-]# and return the value.
799b50d902SRodney W. Grimes  */
809b50d902SRodney W. Grimes static long
819b50d902SRodney W. Grimes find_parsenum(plan, option, vp, endch)
829b50d902SRodney W. Grimes 	PLAN *plan;
839b50d902SRodney W. Grimes 	char *option, *vp, *endch;
849b50d902SRodney W. Grimes {
859b50d902SRodney W. Grimes 	long value;
869b50d902SRodney W. Grimes 	char *endchar, *str;	/* Pointer to character ending conversion. */
879b50d902SRodney W. Grimes 
889b50d902SRodney W. Grimes 	/* Determine comparison from leading + or -. */
899b50d902SRodney W. Grimes 	str = vp;
909b50d902SRodney W. Grimes 	switch (*str) {
919b50d902SRodney W. Grimes 	case '+':
929b50d902SRodney W. Grimes 		++str;
939b50d902SRodney W. Grimes 		plan->flags = F_GREATER;
949b50d902SRodney W. Grimes 		break;
959b50d902SRodney W. Grimes 	case '-':
969b50d902SRodney W. Grimes 		++str;
979b50d902SRodney W. Grimes 		plan->flags = F_LESSTHAN;
989b50d902SRodney W. Grimes 		break;
999b50d902SRodney W. Grimes 	default:
1009b50d902SRodney W. Grimes 		plan->flags = F_EQUAL;
1019b50d902SRodney W. Grimes 		break;
1029b50d902SRodney W. Grimes 	}
1039b50d902SRodney W. Grimes 
1049b50d902SRodney W. Grimes 	/*
1059b50d902SRodney W. Grimes 	 * Convert the string with strtol().  Note, if strtol() returns zero
1069b50d902SRodney W. Grimes 	 * and endchar points to the beginning of the string we know we have
1079b50d902SRodney W. Grimes 	 * a syntax error.
1089b50d902SRodney W. Grimes 	 */
1099b50d902SRodney W. Grimes 	value = strtol(str, &endchar, 10);
1109b50d902SRodney W. Grimes 	if (value == 0 && endchar == str)
1119b50d902SRodney W. Grimes 		errx(1, "%s: %s: illegal numeric value", option, vp);
1129b50d902SRodney W. Grimes 	if (endchar[0] && (endch == NULL || endchar[0] != *endch))
1139b50d902SRodney W. Grimes 		errx(1, "%s: %s: illegal trailing character", option, vp);
1149b50d902SRodney W. Grimes 	if (endch)
1159b50d902SRodney W. Grimes 		*endch = endchar[0];
1169b50d902SRodney W. Grimes 	return (value);
1179b50d902SRodney W. Grimes }
1189b50d902SRodney W. Grimes 
1199b50d902SRodney W. Grimes /*
1209b50d902SRodney W. Grimes  * The value of n for the inode times (atime, ctime, and mtime) is a range,
1219b50d902SRodney W. Grimes  * i.e. n matches from (n - 1) to n 24 hour periods.  This interacts with
1229b50d902SRodney W. Grimes  * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
1239b50d902SRodney W. Grimes  * user wanted.  Correct so that -1 is "less than 1".
1249b50d902SRodney W. Grimes  */
1259b50d902SRodney W. Grimes #define	TIME_CORRECT(p, ttype)						\
1269b50d902SRodney W. Grimes 	if ((p)->type == ttype && (p)->flags == F_LESSTHAN)		\
1279b50d902SRodney W. Grimes 		++((p)->t_data);
1289b50d902SRodney W. Grimes 
1299b50d902SRodney W. Grimes /*
1309b50d902SRodney W. Grimes  * -atime n functions --
1319b50d902SRodney W. Grimes  *
1329b50d902SRodney W. Grimes  *	True if the difference between the file access time and the
1339b50d902SRodney W. Grimes  *	current time is n 24 hour periods.
1349b50d902SRodney W. Grimes  */
1359b50d902SRodney W. Grimes int
1369b50d902SRodney W. Grimes f_atime(plan, entry)
1379b50d902SRodney W. Grimes 	PLAN *plan;
1389b50d902SRodney W. Grimes 	FTSENT *entry;
1399b50d902SRodney W. Grimes {
1409b50d902SRodney W. Grimes 	extern time_t now;
1419b50d902SRodney W. Grimes 
1429b50d902SRodney W. Grimes 	COMPARE((now - entry->fts_statp->st_atime +
1439b50d902SRodney W. Grimes 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
1449b50d902SRodney W. Grimes }
1459b50d902SRodney W. Grimes 
1469b50d902SRodney W. Grimes PLAN *
1479b50d902SRodney W. Grimes c_atime(arg)
1489b50d902SRodney W. Grimes 	char *arg;
1499b50d902SRodney W. Grimes {
1509b50d902SRodney W. Grimes 	PLAN *new;
1519b50d902SRodney W. Grimes 
1529b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
1539b50d902SRodney W. Grimes 
1549b50d902SRodney W. Grimes 	new = palloc(N_ATIME, f_atime);
1559b50d902SRodney W. Grimes 	new->t_data = find_parsenum(new, "-atime", arg, NULL);
1569b50d902SRodney W. Grimes 	TIME_CORRECT(new, N_ATIME);
1579b50d902SRodney W. Grimes 	return (new);
1589b50d902SRodney W. Grimes }
1599b50d902SRodney W. Grimes /*
1609b50d902SRodney W. Grimes  * -ctime n functions --
1619b50d902SRodney W. Grimes  *
1629b50d902SRodney W. Grimes  *	True if the difference between the last change of file
1639b50d902SRodney W. Grimes  *	status information and the current time is n 24 hour periods.
1649b50d902SRodney W. Grimes  */
1659b50d902SRodney W. Grimes int
1669b50d902SRodney W. Grimes f_ctime(plan, entry)
1679b50d902SRodney W. Grimes 	PLAN *plan;
1689b50d902SRodney W. Grimes 	FTSENT *entry;
1699b50d902SRodney W. Grimes {
1709b50d902SRodney W. Grimes 	extern time_t now;
1719b50d902SRodney W. Grimes 
1729b50d902SRodney W. Grimes 	COMPARE((now - entry->fts_statp->st_ctime +
1739b50d902SRodney W. Grimes 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
1749b50d902SRodney W. Grimes }
1759b50d902SRodney W. Grimes 
1769b50d902SRodney W. Grimes PLAN *
1779b50d902SRodney W. Grimes c_ctime(arg)
1789b50d902SRodney W. Grimes 	char *arg;
1799b50d902SRodney W. Grimes {
1809b50d902SRodney W. Grimes 	PLAN *new;
1819b50d902SRodney W. Grimes 
1829b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
1839b50d902SRodney W. Grimes 
1849b50d902SRodney W. Grimes 	new = palloc(N_CTIME, f_ctime);
1859b50d902SRodney W. Grimes 	new->t_data = find_parsenum(new, "-ctime", arg, NULL);
1869b50d902SRodney W. Grimes 	TIME_CORRECT(new, N_CTIME);
1879b50d902SRodney W. Grimes 	return (new);
1889b50d902SRodney W. Grimes }
1899b50d902SRodney W. Grimes 
1909b50d902SRodney W. Grimes /*
1919b50d902SRodney W. Grimes  * -depth functions --
1929b50d902SRodney W. Grimes  *
1939b50d902SRodney W. Grimes  *	Always true, causes descent of the directory hierarchy to be done
1949b50d902SRodney W. Grimes  *	so that all entries in a directory are acted on before the directory
1959b50d902SRodney W. Grimes  *	itself.
1969b50d902SRodney W. Grimes  */
1979b50d902SRodney W. Grimes int
1989b50d902SRodney W. Grimes f_always_true(plan, entry)
1999b50d902SRodney W. Grimes 	PLAN *plan;
2009b50d902SRodney W. Grimes 	FTSENT *entry;
2019b50d902SRodney W. Grimes {
2029b50d902SRodney W. Grimes 	return (1);
2039b50d902SRodney W. Grimes }
2049b50d902SRodney W. Grimes 
2059b50d902SRodney W. Grimes PLAN *
2069b50d902SRodney W. Grimes c_depth()
2079b50d902SRodney W. Grimes {
2089b50d902SRodney W. Grimes 	isdepth = 1;
2099b50d902SRodney W. Grimes 
2109b50d902SRodney W. Grimes 	return (palloc(N_DEPTH, f_always_true));
2119b50d902SRodney W. Grimes }
2129b50d902SRodney W. Grimes 
2139b50d902SRodney W. Grimes /*
2149b50d902SRodney W. Grimes  * [-exec | -ok] utility [arg ... ] ; functions --
2159b50d902SRodney W. Grimes  *
2169b50d902SRodney W. Grimes  *	True if the executed utility returns a zero value as exit status.
2179b50d902SRodney W. Grimes  *	The end of the primary expression is delimited by a semicolon.  If
2189b50d902SRodney W. Grimes  *	"{}" occurs anywhere, it gets replaced by the current pathname.
2199b50d902SRodney W. Grimes  *	The current directory for the execution of utility is the same as
2209b50d902SRodney W. Grimes  *	the current directory when the find utility was started.
2219b50d902SRodney W. Grimes  *
2229b50d902SRodney W. Grimes  *	The primary -ok is different in that it requests affirmation of the
2239b50d902SRodney W. Grimes  *	user before executing the utility.
2249b50d902SRodney W. Grimes  */
2259b50d902SRodney W. Grimes int
2269b50d902SRodney W. Grimes f_exec(plan, entry)
2279b50d902SRodney W. Grimes 	register PLAN *plan;
2289b50d902SRodney W. Grimes 	FTSENT *entry;
2299b50d902SRodney W. Grimes {
2309b50d902SRodney W. Grimes 	extern int dotfd;
2319b50d902SRodney W. Grimes 	register int cnt;
2329b50d902SRodney W. Grimes 	pid_t pid;
2339b50d902SRodney W. Grimes 	int status;
2349b50d902SRodney W. Grimes 
2359b50d902SRodney W. Grimes 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
2369b50d902SRodney W. Grimes 		if (plan->e_len[cnt])
2379b50d902SRodney W. Grimes 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
2389b50d902SRodney W. Grimes 			    entry->fts_path, plan->e_len[cnt]);
2399b50d902SRodney W. Grimes 
2409b50d902SRodney W. Grimes 	if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
2419b50d902SRodney W. Grimes 		return (0);
2429b50d902SRodney W. Grimes 
2439b50d902SRodney W. Grimes 	switch (pid = vfork()) {
2449b50d902SRodney W. Grimes 	case -1:
2459b50d902SRodney W. Grimes 		err(1, "fork");
2469b50d902SRodney W. Grimes 		/* NOTREACHED */
2479b50d902SRodney W. Grimes 	case 0:
2489b50d902SRodney W. Grimes 		if (fchdir(dotfd)) {
2499b50d902SRodney W. Grimes 			warn("chdir");
2509b50d902SRodney W. Grimes 			_exit(1);
2519b50d902SRodney W. Grimes 		}
2529b50d902SRodney W. Grimes 		execvp(plan->e_argv[0], plan->e_argv);
2539b50d902SRodney W. Grimes 		warn("%s", plan->e_argv[0]);
2549b50d902SRodney W. Grimes 		_exit(1);
2559b50d902SRodney W. Grimes 	}
2569b50d902SRodney W. Grimes 	pid = waitpid(pid, &status, 0);
2579b50d902SRodney W. Grimes 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
2589b50d902SRodney W. Grimes }
2599b50d902SRodney W. Grimes 
2609b50d902SRodney W. Grimes /*
2619b50d902SRodney W. Grimes  * c_exec --
2629b50d902SRodney W. Grimes  *	build three parallel arrays, one with pointers to the strings passed
2639b50d902SRodney W. Grimes  *	on the command line, one with (possibly duplicated) pointers to the
2649b50d902SRodney W. Grimes  *	argv array, and one with integer values that are lengths of the
2659b50d902SRodney W. Grimes  *	strings, but also flags meaning that the string has to be massaged.
2669b50d902SRodney W. Grimes  */
2679b50d902SRodney W. Grimes PLAN *
2689b50d902SRodney W. Grimes c_exec(argvp, isok)
2699b50d902SRodney W. Grimes 	char ***argvp;
2709b50d902SRodney W. Grimes 	int isok;
2719b50d902SRodney W. Grimes {
2729b50d902SRodney W. Grimes 	PLAN *new;			/* node returned */
2739b50d902SRodney W. Grimes 	register int cnt;
2749b50d902SRodney W. Grimes 	register char **argv, **ap, *p;
2759b50d902SRodney W. Grimes 
2769b50d902SRodney W. Grimes 	isoutput = 1;
2779b50d902SRodney W. Grimes 
2789b50d902SRodney W. Grimes 	new = palloc(N_EXEC, f_exec);
2799b50d902SRodney W. Grimes 	if (isok)
2809b50d902SRodney W. Grimes 		new->flags = F_NEEDOK;
2819b50d902SRodney W. Grimes 
2829b50d902SRodney W. Grimes 	for (ap = argv = *argvp;; ++ap) {
2839b50d902SRodney W. Grimes 		if (!*ap)
2849b50d902SRodney W. Grimes 			errx(1,
2859b50d902SRodney W. Grimes 			    "%s: no terminating \";\"", isok ? "-ok" : "-exec");
2869b50d902SRodney W. Grimes 		if (**ap == ';')
2879b50d902SRodney W. Grimes 			break;
2889b50d902SRodney W. Grimes 	}
2899b50d902SRodney W. Grimes 
2909b50d902SRodney W. Grimes 	cnt = ap - *argvp + 1;
2919b50d902SRodney W. Grimes 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
2929b50d902SRodney W. Grimes 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
2939b50d902SRodney W. Grimes 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
2949b50d902SRodney W. Grimes 
2959b50d902SRodney W. Grimes 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
2969b50d902SRodney W. Grimes 		new->e_orig[cnt] = *argv;
2979b50d902SRodney W. Grimes 		for (p = *argv; *p; ++p)
2989b50d902SRodney W. Grimes 			if (p[0] == '{' && p[1] == '}') {
2999b50d902SRodney W. Grimes 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
3009b50d902SRodney W. Grimes 				new->e_len[cnt] = MAXPATHLEN;
3019b50d902SRodney W. Grimes 				break;
3029b50d902SRodney W. Grimes 			}
3039b50d902SRodney W. Grimes 		if (!*p) {
3049b50d902SRodney W. Grimes 			new->e_argv[cnt] = *argv;
3059b50d902SRodney W. Grimes 			new->e_len[cnt] = 0;
3069b50d902SRodney W. Grimes 		}
3079b50d902SRodney W. Grimes 	}
3089b50d902SRodney W. Grimes 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
3099b50d902SRodney W. Grimes 
3109b50d902SRodney W. Grimes 	*argvp = argv + 1;
3119b50d902SRodney W. Grimes 	return (new);
3129b50d902SRodney W. Grimes }
3139b50d902SRodney W. Grimes 
3149b50d902SRodney W. Grimes /*
3159b50d902SRodney W. Grimes  * -follow functions --
3169b50d902SRodney W. Grimes  *
3179b50d902SRodney W. Grimes  *	Always true, causes symbolic links to be followed on a global
3189b50d902SRodney W. Grimes  *	basis.
3199b50d902SRodney W. Grimes  */
3209b50d902SRodney W. Grimes PLAN *
3219b50d902SRodney W. Grimes c_follow()
3229b50d902SRodney W. Grimes {
3239b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_PHYSICAL;
3249b50d902SRodney W. Grimes 	ftsoptions |= FTS_LOGICAL;
3259b50d902SRodney W. Grimes 
3269b50d902SRodney W. Grimes 	return (palloc(N_FOLLOW, f_always_true));
3279b50d902SRodney W. Grimes }
3289b50d902SRodney W. Grimes 
3299b50d902SRodney W. Grimes /*
3309b50d902SRodney W. Grimes  * -fstype functions --
3319b50d902SRodney W. Grimes  *
3329b50d902SRodney W. Grimes  *	True if the file is of a certain type.
3339b50d902SRodney W. Grimes  */
3349b50d902SRodney W. Grimes int
3359b50d902SRodney W. Grimes f_fstype(plan, entry)
3369b50d902SRodney W. Grimes 	PLAN *plan;
3379b50d902SRodney W. Grimes 	FTSENT *entry;
3389b50d902SRodney W. Grimes {
3399b50d902SRodney W. Grimes 	static dev_t curdev;	/* need a guaranteed illegal dev value */
3409b50d902SRodney W. Grimes 	static int first = 1;
3419b50d902SRodney W. Grimes 	struct statfs sb;
3429b50d902SRodney W. Grimes 	static short val;
3439b50d902SRodney W. Grimes 	char *p, save[2];
3449b50d902SRodney W. Grimes 
3459b50d902SRodney W. Grimes 	/* Only check when we cross mount point. */
3469b50d902SRodney W. Grimes 	if (first || curdev != entry->fts_statp->st_dev) {
3479b50d902SRodney W. Grimes 		curdev = entry->fts_statp->st_dev;
3489b50d902SRodney W. Grimes 
3499b50d902SRodney W. Grimes 		/*
3509b50d902SRodney W. Grimes 		 * Statfs follows symlinks; find wants the link's file system,
3519b50d902SRodney W. Grimes 		 * not where it points.
3529b50d902SRodney W. Grimes 		 */
3539b50d902SRodney W. Grimes 		if (entry->fts_info == FTS_SL ||
3549b50d902SRodney W. Grimes 		    entry->fts_info == FTS_SLNONE) {
3559b50d902SRodney W. Grimes 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
3569b50d902SRodney W. Grimes 				++p;
3579b50d902SRodney W. Grimes 			else
3589b50d902SRodney W. Grimes 				p = entry->fts_accpath;
3599b50d902SRodney W. Grimes 			save[0] = p[0];
3609b50d902SRodney W. Grimes 			p[0] = '.';
3619b50d902SRodney W. Grimes 			save[1] = p[1];
3629b50d902SRodney W. Grimes 			p[1] = '\0';
3639b50d902SRodney W. Grimes 
3649b50d902SRodney W. Grimes 		} else
3659b50d902SRodney W. Grimes 			p = NULL;
3669b50d902SRodney W. Grimes 
3679b50d902SRodney W. Grimes 		if (statfs(entry->fts_accpath, &sb))
3689b50d902SRodney W. Grimes 			err(1, "%s", entry->fts_accpath);
3699b50d902SRodney W. Grimes 
3709b50d902SRodney W. Grimes 		if (p) {
3719b50d902SRodney W. Grimes 			p[0] = save[0];
3729b50d902SRodney W. Grimes 			p[1] = save[1];
3739b50d902SRodney W. Grimes 		}
3749b50d902SRodney W. Grimes 
3759b50d902SRodney W. Grimes 		first = 0;
3769b50d902SRodney W. Grimes 		switch (plan->flags) {
3779b50d902SRodney W. Grimes 		case F_MTFLAG:
3789b50d902SRodney W. Grimes 			val = sb.f_flags;
3799b50d902SRodney W. Grimes 			break;
3809b50d902SRodney W. Grimes 		case F_MTTYPE:
3819b50d902SRodney W. Grimes 			val = sb.f_type;
3829b50d902SRodney W. Grimes 			break;
3839b50d902SRodney W. Grimes 		default:
3849b50d902SRodney W. Grimes 			abort();
3859b50d902SRodney W. Grimes 		}
3869b50d902SRodney W. Grimes 	}
3879b50d902SRodney W. Grimes 	switch(plan->flags) {
3889b50d902SRodney W. Grimes 	case F_MTFLAG:
3899b50d902SRodney W. Grimes 		return (val & plan->mt_data);
3909b50d902SRodney W. Grimes 	case F_MTTYPE:
3919b50d902SRodney W. Grimes 		return (val == plan->mt_data);
3929b50d902SRodney W. Grimes 	default:
3939b50d902SRodney W. Grimes 		abort();
3949b50d902SRodney W. Grimes 	}
3959b50d902SRodney W. Grimes }
3969b50d902SRodney W. Grimes 
3979b50d902SRodney W. Grimes PLAN *
3989b50d902SRodney W. Grimes c_fstype(arg)
3999b50d902SRodney W. Grimes 	char *arg;
4009b50d902SRodney W. Grimes {
4019b50d902SRodney W. Grimes 	register PLAN *new;
4029b50d902SRodney W. Grimes 
4039b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
4049b50d902SRodney W. Grimes 
4059b50d902SRodney W. Grimes 	new = palloc(N_FSTYPE, f_fstype);
4069b50d902SRodney W. Grimes 	switch (*arg) {
4079b50d902SRodney W. Grimes 	case 'l':
4089b50d902SRodney W. Grimes 		if (!strcmp(arg, "local")) {
4099b50d902SRodney W. Grimes 			new->flags = F_MTFLAG;
4109b50d902SRodney W. Grimes 			new->mt_data = MNT_LOCAL;
4119b50d902SRodney W. Grimes 			return (new);
4129b50d902SRodney W. Grimes 		}
4139b50d902SRodney W. Grimes 		break;
4149b50d902SRodney W. Grimes 	case 'm':
4159b50d902SRodney W. Grimes 		if (!strcmp(arg, "mfs")) {
4169b50d902SRodney W. Grimes 			new->flags = F_MTTYPE;
4179b50d902SRodney W. Grimes 			new->mt_data = MOUNT_MFS;
4189b50d902SRodney W. Grimes 			return (new);
4199b50d902SRodney W. Grimes 		}
4209b50d902SRodney W. Grimes 		break;
4219b50d902SRodney W. Grimes 	case 'n':
4229b50d902SRodney W. Grimes 		if (!strcmp(arg, "nfs")) {
4239b50d902SRodney W. Grimes 			new->flags = F_MTTYPE;
4249b50d902SRodney W. Grimes 			new->mt_data = MOUNT_NFS;
4259b50d902SRodney W. Grimes 			return (new);
4269b50d902SRodney W. Grimes 		}
4279b50d902SRodney W. Grimes 		break;
4289b50d902SRodney W. Grimes 	case 'p':
4299b50d902SRodney W. Grimes 		if (!strcmp(arg, "msdos")) {
4309b50d902SRodney W. Grimes 			new->flags = F_MTTYPE;
4319b50d902SRodney W. Grimes 			new->mt_data = MOUNT_MSDOS;
4329b50d902SRodney W. Grimes 			return (new);
4339b50d902SRodney W. Grimes 		}
4349b50d902SRodney W. Grimes 		break;
4359b50d902SRodney W. Grimes 	case 'r':
4369b50d902SRodney W. Grimes 		if (!strcmp(arg, "rdonly")) {
4379b50d902SRodney W. Grimes 			new->flags = F_MTFLAG;
4389b50d902SRodney W. Grimes 			new->mt_data = MNT_RDONLY;
4399b50d902SRodney W. Grimes 			return (new);
4409b50d902SRodney W. Grimes 		}
4419b50d902SRodney W. Grimes 		break;
4429b50d902SRodney W. Grimes 	case 'u':
4439b50d902SRodney W. Grimes 		if (!strcmp(arg, "ufs")) {
4449b50d902SRodney W. Grimes 			new->flags = F_MTTYPE;
4459b50d902SRodney W. Grimes 			new->mt_data = MOUNT_UFS;
4469b50d902SRodney W. Grimes 			return (new);
4479b50d902SRodney W. Grimes 		}
4489b50d902SRodney W. Grimes 		break;
4499b50d902SRodney W. Grimes 	}
4509b50d902SRodney W. Grimes 	errx(1, "%s: unknown file type", arg);
4519b50d902SRodney W. Grimes 	/* NOTREACHED */
4529b50d902SRodney W. Grimes }
4539b50d902SRodney W. Grimes 
4549b50d902SRodney W. Grimes /*
4559b50d902SRodney W. Grimes  * -group gname functions --
4569b50d902SRodney W. Grimes  *
4579b50d902SRodney W. Grimes  *	True if the file belongs to the group gname.  If gname is numeric and
4589b50d902SRodney W. Grimes  *	an equivalent of the getgrnam() function does not return a valid group
4599b50d902SRodney W. Grimes  *	name, gname is taken as a group ID.
4609b50d902SRodney W. Grimes  */
4619b50d902SRodney W. Grimes int
4629b50d902SRodney W. Grimes f_group(plan, entry)
4639b50d902SRodney W. Grimes 	PLAN *plan;
4649b50d902SRodney W. Grimes 	FTSENT *entry;
4659b50d902SRodney W. Grimes {
4669b50d902SRodney W. Grimes 	return (entry->fts_statp->st_gid == plan->g_data);
4679b50d902SRodney W. Grimes }
4689b50d902SRodney W. Grimes 
4699b50d902SRodney W. Grimes PLAN *
4709b50d902SRodney W. Grimes c_group(gname)
4719b50d902SRodney W. Grimes 	char *gname;
4729b50d902SRodney W. Grimes {
4739b50d902SRodney W. Grimes 	PLAN *new;
4749b50d902SRodney W. Grimes 	struct group *g;
4759b50d902SRodney W. Grimes 	gid_t gid;
4769b50d902SRodney W. Grimes 
4779b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
4789b50d902SRodney W. Grimes 
4799b50d902SRodney W. Grimes 	g = getgrnam(gname);
4809b50d902SRodney W. Grimes 	if (g == NULL) {
4819b50d902SRodney W. Grimes 		gid = atoi(gname);
4829b50d902SRodney W. Grimes 		if (gid == 0 && gname[0] != '0')
4839b50d902SRodney W. Grimes 			errx(1, "-group: %s: no such group", gname);
4849b50d902SRodney W. Grimes 	} else
4859b50d902SRodney W. Grimes 		gid = g->gr_gid;
4869b50d902SRodney W. Grimes 
4879b50d902SRodney W. Grimes 	new = palloc(N_GROUP, f_group);
4889b50d902SRodney W. Grimes 	new->g_data = gid;
4899b50d902SRodney W. Grimes 	return (new);
4909b50d902SRodney W. Grimes }
4919b50d902SRodney W. Grimes 
4929b50d902SRodney W. Grimes /*
4939b50d902SRodney W. Grimes  * -inum n functions --
4949b50d902SRodney W. Grimes  *
4959b50d902SRodney W. Grimes  *	True if the file has inode # n.
4969b50d902SRodney W. Grimes  */
4979b50d902SRodney W. Grimes int
4989b50d902SRodney W. Grimes f_inum(plan, entry)
4999b50d902SRodney W. Grimes 	PLAN *plan;
5009b50d902SRodney W. Grimes 	FTSENT *entry;
5019b50d902SRodney W. Grimes {
5029b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
5039b50d902SRodney W. Grimes }
5049b50d902SRodney W. Grimes 
5059b50d902SRodney W. Grimes PLAN *
5069b50d902SRodney W. Grimes c_inum(arg)
5079b50d902SRodney W. Grimes 	char *arg;
5089b50d902SRodney W. Grimes {
5099b50d902SRodney W. Grimes 	PLAN *new;
5109b50d902SRodney W. Grimes 
5119b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
5129b50d902SRodney W. Grimes 
5139b50d902SRodney W. Grimes 	new = palloc(N_INUM, f_inum);
5149b50d902SRodney W. Grimes 	new->i_data = find_parsenum(new, "-inum", arg, NULL);
5159b50d902SRodney W. Grimes 	return (new);
5169b50d902SRodney W. Grimes }
5179b50d902SRodney W. Grimes 
5189b50d902SRodney W. Grimes /*
5199b50d902SRodney W. Grimes  * -links n functions --
5209b50d902SRodney W. Grimes  *
5219b50d902SRodney W. Grimes  *	True if the file has n links.
5229b50d902SRodney W. Grimes  */
5239b50d902SRodney W. Grimes int
5249b50d902SRodney W. Grimes f_links(plan, entry)
5259b50d902SRodney W. Grimes 	PLAN *plan;
5269b50d902SRodney W. Grimes 	FTSENT *entry;
5279b50d902SRodney W. Grimes {
5289b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
5299b50d902SRodney W. Grimes }
5309b50d902SRodney W. Grimes 
5319b50d902SRodney W. Grimes PLAN *
5329b50d902SRodney W. Grimes c_links(arg)
5339b50d902SRodney W. Grimes 	char *arg;
5349b50d902SRodney W. Grimes {
5359b50d902SRodney W. Grimes 	PLAN *new;
5369b50d902SRodney W. Grimes 
5379b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
5389b50d902SRodney W. Grimes 
5399b50d902SRodney W. Grimes 	new = palloc(N_LINKS, f_links);
5409b50d902SRodney W. Grimes 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
5419b50d902SRodney W. Grimes 	return (new);
5429b50d902SRodney W. Grimes }
5439b50d902SRodney W. Grimes 
5449b50d902SRodney W. Grimes /*
5459b50d902SRodney W. Grimes  * -ls functions --
5469b50d902SRodney W. Grimes  *
5479b50d902SRodney W. Grimes  *	Always true - prints the current entry to stdout in "ls" format.
5489b50d902SRodney W. Grimes  */
5499b50d902SRodney W. Grimes int
5509b50d902SRodney W. Grimes f_ls(plan, entry)
5519b50d902SRodney W. Grimes 	PLAN *plan;
5529b50d902SRodney W. Grimes 	FTSENT *entry;
5539b50d902SRodney W. Grimes {
5549b50d902SRodney W. Grimes 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
5559b50d902SRodney W. Grimes 	return (1);
5569b50d902SRodney W. Grimes }
5579b50d902SRodney W. Grimes 
5589b50d902SRodney W. Grimes PLAN *
5599b50d902SRodney W. Grimes c_ls()
5609b50d902SRodney W. Grimes {
5619b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
5629b50d902SRodney W. Grimes 	isoutput = 1;
5639b50d902SRodney W. Grimes 
5649b50d902SRodney W. Grimes 	return (palloc(N_LS, f_ls));
5659b50d902SRodney W. Grimes }
5669b50d902SRodney W. Grimes 
5679b50d902SRodney W. Grimes /*
5689b50d902SRodney W. Grimes  * -mtime n functions --
5699b50d902SRodney W. Grimes  *
5709b50d902SRodney W. Grimes  *	True if the difference between the file modification time and the
5719b50d902SRodney W. Grimes  *	current time is n 24 hour periods.
5729b50d902SRodney W. Grimes  */
5739b50d902SRodney W. Grimes int
5749b50d902SRodney W. Grimes f_mtime(plan, entry)
5759b50d902SRodney W. Grimes 	PLAN *plan;
5769b50d902SRodney W. Grimes 	FTSENT *entry;
5779b50d902SRodney W. Grimes {
5789b50d902SRodney W. Grimes 	extern time_t now;
5799b50d902SRodney W. Grimes 
5809b50d902SRodney W. Grimes 	COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
5819b50d902SRodney W. Grimes 	    SECSPERDAY, plan->t_data);
5829b50d902SRodney W. Grimes }
5839b50d902SRodney W. Grimes 
5849b50d902SRodney W. Grimes PLAN *
5859b50d902SRodney W. Grimes c_mtime(arg)
5869b50d902SRodney W. Grimes 	char *arg;
5879b50d902SRodney W. Grimes {
5889b50d902SRodney W. Grimes 	PLAN *new;
5899b50d902SRodney W. Grimes 
5909b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
5919b50d902SRodney W. Grimes 
5929b50d902SRodney W. Grimes 	new = palloc(N_MTIME, f_mtime);
5939b50d902SRodney W. Grimes 	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
5949b50d902SRodney W. Grimes 	TIME_CORRECT(new, N_MTIME);
5959b50d902SRodney W. Grimes 	return (new);
5969b50d902SRodney W. Grimes }
5979b50d902SRodney W. Grimes 
5989b50d902SRodney W. Grimes /*
5999b50d902SRodney W. Grimes  * -name functions --
6009b50d902SRodney W. Grimes  *
6019b50d902SRodney W. Grimes  *	True if the basename of the filename being examined
6029b50d902SRodney W. Grimes  *	matches pattern using Pattern Matching Notation S3.14
6039b50d902SRodney W. Grimes  */
6049b50d902SRodney W. Grimes int
6059b50d902SRodney W. Grimes f_name(plan, entry)
6069b50d902SRodney W. Grimes 	PLAN *plan;
6079b50d902SRodney W. Grimes 	FTSENT *entry;
6089b50d902SRodney W. Grimes {
6099b50d902SRodney W. Grimes 	return (!fnmatch(plan->c_data, entry->fts_name, 0));
6109b50d902SRodney W. Grimes }
6119b50d902SRodney W. Grimes 
6129b50d902SRodney W. Grimes PLAN *
6139b50d902SRodney W. Grimes c_name(pattern)
6149b50d902SRodney W. Grimes 	char *pattern;
6159b50d902SRodney W. Grimes {
6169b50d902SRodney W. Grimes 	PLAN *new;
6179b50d902SRodney W. Grimes 
6189b50d902SRodney W. Grimes 	new = palloc(N_NAME, f_name);
6199b50d902SRodney W. Grimes 	new->c_data = pattern;
6209b50d902SRodney W. Grimes 	return (new);
6219b50d902SRodney W. Grimes }
6229b50d902SRodney W. Grimes 
6239b50d902SRodney W. Grimes /*
6249b50d902SRodney W. Grimes  * -newer file functions --
6259b50d902SRodney W. Grimes  *
6269b50d902SRodney W. Grimes  *	True if the current file has been modified more recently
6279b50d902SRodney W. Grimes  *	then the modification time of the file named by the pathname
6289b50d902SRodney W. Grimes  *	file.
6299b50d902SRodney W. Grimes  */
6309b50d902SRodney W. Grimes int
6319b50d902SRodney W. Grimes f_newer(plan, entry)
6329b50d902SRodney W. Grimes 	PLAN *plan;
6339b50d902SRodney W. Grimes 	FTSENT *entry;
6349b50d902SRodney W. Grimes {
6359b50d902SRodney W. Grimes 	return (entry->fts_statp->st_mtime > plan->t_data);
6369b50d902SRodney W. Grimes }
6379b50d902SRodney W. Grimes 
6389b50d902SRodney W. Grimes PLAN *
6399b50d902SRodney W. Grimes c_newer(filename)
6409b50d902SRodney W. Grimes 	char *filename;
6419b50d902SRodney W. Grimes {
6429b50d902SRodney W. Grimes 	PLAN *new;
6439b50d902SRodney W. Grimes 	struct stat sb;
6449b50d902SRodney W. Grimes 
6459b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
6469b50d902SRodney W. Grimes 
6479b50d902SRodney W. Grimes 	if (stat(filename, &sb))
6489b50d902SRodney W. Grimes 		err(1, "%s", filename);
6499b50d902SRodney W. Grimes 	new = palloc(N_NEWER, f_newer);
6509b50d902SRodney W. Grimes 	new->t_data = sb.st_mtime;
6519b50d902SRodney W. Grimes 	return (new);
6529b50d902SRodney W. Grimes }
6539b50d902SRodney W. Grimes 
6549b50d902SRodney W. Grimes /*
6559b50d902SRodney W. Grimes  * -nogroup functions --
6569b50d902SRodney W. Grimes  *
6579b50d902SRodney W. Grimes  *	True if file belongs to a user ID for which the equivalent
6589b50d902SRodney W. Grimes  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
6599b50d902SRodney W. Grimes  */
6609b50d902SRodney W. Grimes int
6619b50d902SRodney W. Grimes f_nogroup(plan, entry)
6629b50d902SRodney W. Grimes 	PLAN *plan;
6639b50d902SRodney W. Grimes 	FTSENT *entry;
6649b50d902SRodney W. Grimes {
6659b50d902SRodney W. Grimes 	char *group_from_gid();
6669b50d902SRodney W. Grimes 
6679b50d902SRodney W. Grimes 	return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
6689b50d902SRodney W. Grimes }
6699b50d902SRodney W. Grimes 
6709b50d902SRodney W. Grimes PLAN *
6719b50d902SRodney W. Grimes c_nogroup()
6729b50d902SRodney W. Grimes {
6739b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
6749b50d902SRodney W. Grimes 
6759b50d902SRodney W. Grimes 	return (palloc(N_NOGROUP, f_nogroup));
6769b50d902SRodney W. Grimes }
6779b50d902SRodney W. Grimes 
6789b50d902SRodney W. Grimes /*
6799b50d902SRodney W. Grimes  * -nouser functions --
6809b50d902SRodney W. Grimes  *
6819b50d902SRodney W. Grimes  *	True if file belongs to a user ID for which the equivalent
6829b50d902SRodney W. Grimes  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
6839b50d902SRodney W. Grimes  */
6849b50d902SRodney W. Grimes int
6859b50d902SRodney W. Grimes f_nouser(plan, entry)
6869b50d902SRodney W. Grimes 	PLAN *plan;
6879b50d902SRodney W. Grimes 	FTSENT *entry;
6889b50d902SRodney W. Grimes {
6899b50d902SRodney W. Grimes 	char *user_from_uid();
6909b50d902SRodney W. Grimes 
6919b50d902SRodney W. Grimes 	return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
6929b50d902SRodney W. Grimes }
6939b50d902SRodney W. Grimes 
6949b50d902SRodney W. Grimes PLAN *
6959b50d902SRodney W. Grimes c_nouser()
6969b50d902SRodney W. Grimes {
6979b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
6989b50d902SRodney W. Grimes 
6999b50d902SRodney W. Grimes 	return (palloc(N_NOUSER, f_nouser));
7009b50d902SRodney W. Grimes }
7019b50d902SRodney W. Grimes 
7029b50d902SRodney W. Grimes /*
7039b50d902SRodney W. Grimes  * -path functions --
7049b50d902SRodney W. Grimes  *
7059b50d902SRodney W. Grimes  *	True if the path of the filename being examined
7069b50d902SRodney W. Grimes  *	matches pattern using Pattern Matching Notation S3.14
7079b50d902SRodney W. Grimes  */
7089b50d902SRodney W. Grimes int
7099b50d902SRodney W. Grimes f_path(plan, entry)
7109b50d902SRodney W. Grimes 	PLAN *plan;
7119b50d902SRodney W. Grimes 	FTSENT *entry;
7129b50d902SRodney W. Grimes {
7139b50d902SRodney W. Grimes 	return (!fnmatch(plan->c_data, entry->fts_path, 0));
7149b50d902SRodney W. Grimes }
7159b50d902SRodney W. Grimes 
7169b50d902SRodney W. Grimes PLAN *
7179b50d902SRodney W. Grimes c_path(pattern)
7189b50d902SRodney W. Grimes 	char *pattern;
7199b50d902SRodney W. Grimes {
7209b50d902SRodney W. Grimes 	PLAN *new;
7219b50d902SRodney W. Grimes 
7229b50d902SRodney W. Grimes 	new = palloc(N_NAME, f_path);
7239b50d902SRodney W. Grimes 	new->c_data = pattern;
7249b50d902SRodney W. Grimes 	return (new);
7259b50d902SRodney W. Grimes }
7269b50d902SRodney W. Grimes 
7279b50d902SRodney W. Grimes /*
7289b50d902SRodney W. Grimes  * -perm functions --
7299b50d902SRodney W. Grimes  *
7309b50d902SRodney W. Grimes  *	The mode argument is used to represent file mode bits.  If it starts
7319b50d902SRodney W. Grimes  *	with a leading digit, it's treated as an octal mode, otherwise as a
7329b50d902SRodney W. Grimes  *	symbolic mode.
7339b50d902SRodney W. Grimes  */
7349b50d902SRodney W. Grimes int
7359b50d902SRodney W. Grimes f_perm(plan, entry)
7369b50d902SRodney W. Grimes 	PLAN *plan;
7379b50d902SRodney W. Grimes 	FTSENT *entry;
7389b50d902SRodney W. Grimes {
7399b50d902SRodney W. Grimes 	mode_t mode;
7409b50d902SRodney W. Grimes 
7419b50d902SRodney W. Grimes 	mode = entry->fts_statp->st_mode &
7429b50d902SRodney W. Grimes 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
7439b50d902SRodney W. Grimes 	if (plan->flags == F_ATLEAST)
7449b50d902SRodney W. Grimes 		return ((plan->m_data | mode) == mode);
7459b50d902SRodney W. Grimes 	else
7469b50d902SRodney W. Grimes 		return (mode == plan->m_data);
7479b50d902SRodney W. Grimes 	/* NOTREACHED */
7489b50d902SRodney W. Grimes }
7499b50d902SRodney W. Grimes 
7509b50d902SRodney W. Grimes PLAN *
7519b50d902SRodney W. Grimes c_perm(perm)
7529b50d902SRodney W. Grimes 	char *perm;
7539b50d902SRodney W. Grimes {
7549b50d902SRodney W. Grimes 	PLAN *new;
7559b50d902SRodney W. Grimes 	mode_t *set;
7569b50d902SRodney W. Grimes 
7579b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
7589b50d902SRodney W. Grimes 
7599b50d902SRodney W. Grimes 	new = palloc(N_PERM, f_perm);
7609b50d902SRodney W. Grimes 
7619b50d902SRodney W. Grimes 	if (*perm == '-') {
7629b50d902SRodney W. Grimes 		new->flags = F_ATLEAST;
7639b50d902SRodney W. Grimes 		++perm;
7649b50d902SRodney W. Grimes 	}
7659b50d902SRodney W. Grimes 
7669b50d902SRodney W. Grimes 	if ((set = setmode(perm)) == NULL)
7679b50d902SRodney W. Grimes 		err(1, "-perm: %s: illegal mode string", perm);
7689b50d902SRodney W. Grimes 
7699b50d902SRodney W. Grimes 	new->m_data = getmode(set, 0);
7709b50d902SRodney W. Grimes 	return (new);
7719b50d902SRodney W. Grimes }
7729b50d902SRodney W. Grimes 
7739b50d902SRodney W. Grimes /*
7749b50d902SRodney W. Grimes  * -print functions --
7759b50d902SRodney W. Grimes  *
7769b50d902SRodney W. Grimes  *	Always true, causes the current pathame to be written to
7779b50d902SRodney W. Grimes  *	standard output.
7789b50d902SRodney W. Grimes  */
7799b50d902SRodney W. Grimes int
7809b50d902SRodney W. Grimes f_print(plan, entry)
7819b50d902SRodney W. Grimes 	PLAN *plan;
7829b50d902SRodney W. Grimes 	FTSENT *entry;
7839b50d902SRodney W. Grimes {
7849b50d902SRodney W. Grimes 	(void)printf("%s\n", entry->fts_path);
7859b50d902SRodney W. Grimes 	return (1);
7869b50d902SRodney W. Grimes }
7879b50d902SRodney W. Grimes 
7889b50d902SRodney W. Grimes PLAN *
7899b50d902SRodney W. Grimes c_print()
7909b50d902SRodney W. Grimes {
7919b50d902SRodney W. Grimes 	isoutput = 1;
7929b50d902SRodney W. Grimes 
7939b50d902SRodney W. Grimes 	return (palloc(N_PRINT, f_print));
7949b50d902SRodney W. Grimes }
7959b50d902SRodney W. Grimes 
7969b50d902SRodney W. Grimes /*
7979b50d902SRodney W. Grimes  * -prune functions --
7989b50d902SRodney W. Grimes  *
7999b50d902SRodney W. Grimes  *	Prune a portion of the hierarchy.
8009b50d902SRodney W. Grimes  */
8019b50d902SRodney W. Grimes int
8029b50d902SRodney W. Grimes f_prune(plan, entry)
8039b50d902SRodney W. Grimes 	PLAN *plan;
8049b50d902SRodney W. Grimes 	FTSENT *entry;
8059b50d902SRodney W. Grimes {
8069b50d902SRodney W. Grimes 	extern FTS *tree;
8079b50d902SRodney W. Grimes 
8089b50d902SRodney W. Grimes 	if (fts_set(tree, entry, FTS_SKIP))
8099b50d902SRodney W. Grimes 		err(1, "%s", entry->fts_path);
8109b50d902SRodney W. Grimes 	return (1);
8119b50d902SRodney W. Grimes }
8129b50d902SRodney W. Grimes 
8139b50d902SRodney W. Grimes PLAN *
8149b50d902SRodney W. Grimes c_prune()
8159b50d902SRodney W. Grimes {
8169b50d902SRodney W. Grimes 	return (palloc(N_PRUNE, f_prune));
8179b50d902SRodney W. Grimes }
8189b50d902SRodney W. Grimes 
8199b50d902SRodney W. Grimes /*
8209b50d902SRodney W. Grimes  * -size n[c] functions --
8219b50d902SRodney W. Grimes  *
8229b50d902SRodney W. Grimes  *	True if the file size in bytes, divided by an implementation defined
8239b50d902SRodney W. Grimes  *	value and rounded up to the next integer, is n.  If n is followed by
8249b50d902SRodney W. Grimes  *	a c, the size is in bytes.
8259b50d902SRodney W. Grimes  */
8269b50d902SRodney W. Grimes #define	FIND_SIZE	512
8279b50d902SRodney W. Grimes static int divsize = 1;
8289b50d902SRodney W. Grimes 
8299b50d902SRodney W. Grimes int
8309b50d902SRodney W. Grimes f_size(plan, entry)
8319b50d902SRodney W. Grimes 	PLAN *plan;
8329b50d902SRodney W. Grimes 	FTSENT *entry;
8339b50d902SRodney W. Grimes {
8349b50d902SRodney W. Grimes 	off_t size;
8359b50d902SRodney W. Grimes 
8369b50d902SRodney W. Grimes 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
8379b50d902SRodney W. Grimes 	    FIND_SIZE : entry->fts_statp->st_size;
8389b50d902SRodney W. Grimes 	COMPARE(size, plan->o_data);
8399b50d902SRodney W. Grimes }
8409b50d902SRodney W. Grimes 
8419b50d902SRodney W. Grimes PLAN *
8429b50d902SRodney W. Grimes c_size(arg)
8439b50d902SRodney W. Grimes 	char *arg;
8449b50d902SRodney W. Grimes {
8459b50d902SRodney W. Grimes 	PLAN *new;
8469b50d902SRodney W. Grimes 	char endch;
8479b50d902SRodney W. Grimes 
8489b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
8499b50d902SRodney W. Grimes 
8509b50d902SRodney W. Grimes 	new = palloc(N_SIZE, f_size);
8519b50d902SRodney W. Grimes 	endch = 'c';
8529b50d902SRodney W. Grimes 	new->o_data = find_parsenum(new, "-size", arg, &endch);
8539b50d902SRodney W. Grimes 	if (endch == 'c')
8549b50d902SRodney W. Grimes 		divsize = 0;
8559b50d902SRodney W. Grimes 	return (new);
8569b50d902SRodney W. Grimes }
8579b50d902SRodney W. Grimes 
8589b50d902SRodney W. Grimes /*
8599b50d902SRodney W. Grimes  * -type c functions --
8609b50d902SRodney W. Grimes  *
8619b50d902SRodney W. Grimes  *	True if the type of the file is c, where c is b, c, d, p, or f for
8629b50d902SRodney W. Grimes  *	block special file, character special file, directory, FIFO, or
8639b50d902SRodney W. Grimes  *	regular file, respectively.
8649b50d902SRodney W. Grimes  */
8659b50d902SRodney W. Grimes int
8669b50d902SRodney W. Grimes f_type(plan, entry)
8679b50d902SRodney W. Grimes 	PLAN *plan;
8689b50d902SRodney W. Grimes 	FTSENT *entry;
8699b50d902SRodney W. Grimes {
8709b50d902SRodney W. Grimes 	return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
8719b50d902SRodney W. Grimes }
8729b50d902SRodney W. Grimes 
8739b50d902SRodney W. Grimes PLAN *
8749b50d902SRodney W. Grimes c_type(typestring)
8759b50d902SRodney W. Grimes 	char *typestring;
8769b50d902SRodney W. Grimes {
8779b50d902SRodney W. Grimes 	PLAN *new;
8789b50d902SRodney W. Grimes 	mode_t  mask;
8799b50d902SRodney W. Grimes 
8809b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
8819b50d902SRodney W. Grimes 
8829b50d902SRodney W. Grimes 	switch (typestring[0]) {
8839b50d902SRodney W. Grimes 	case 'b':
8849b50d902SRodney W. Grimes 		mask = S_IFBLK;
8859b50d902SRodney W. Grimes 		break;
8869b50d902SRodney W. Grimes 	case 'c':
8879b50d902SRodney W. Grimes 		mask = S_IFCHR;
8889b50d902SRodney W. Grimes 		break;
8899b50d902SRodney W. Grimes 	case 'd':
8909b50d902SRodney W. Grimes 		mask = S_IFDIR;
8919b50d902SRodney W. Grimes 		break;
8929b50d902SRodney W. Grimes 	case 'f':
8939b50d902SRodney W. Grimes 		mask = S_IFREG;
8949b50d902SRodney W. Grimes 		break;
8959b50d902SRodney W. Grimes 	case 'l':
8969b50d902SRodney W. Grimes 		mask = S_IFLNK;
8979b50d902SRodney W. Grimes 		break;
8989b50d902SRodney W. Grimes 	case 'p':
8999b50d902SRodney W. Grimes 		mask = S_IFIFO;
9009b50d902SRodney W. Grimes 		break;
9019b50d902SRodney W. Grimes 	case 's':
9029b50d902SRodney W. Grimes 		mask = S_IFSOCK;
9039b50d902SRodney W. Grimes 		break;
9049b50d902SRodney W. Grimes 	default:
9059b50d902SRodney W. Grimes 		errx(1, "-type: %s: unknown type", typestring);
9069b50d902SRodney W. Grimes 	}
9079b50d902SRodney W. Grimes 
9089b50d902SRodney W. Grimes 	new = palloc(N_TYPE, f_type);
9099b50d902SRodney W. Grimes 	new->m_data = mask;
9109b50d902SRodney W. Grimes 	return (new);
9119b50d902SRodney W. Grimes }
9129b50d902SRodney W. Grimes 
9139b50d902SRodney W. Grimes /*
9149b50d902SRodney W. Grimes  * -user uname functions --
9159b50d902SRodney W. Grimes  *
9169b50d902SRodney W. Grimes  *	True if the file belongs to the user uname.  If uname is numeric and
9179b50d902SRodney W. Grimes  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
9189b50d902SRodney W. Grimes  *	return a valid user name, uname is taken as a user ID.
9199b50d902SRodney W. Grimes  */
9209b50d902SRodney W. Grimes int
9219b50d902SRodney W. Grimes f_user(plan, entry)
9229b50d902SRodney W. Grimes 	PLAN *plan;
9239b50d902SRodney W. Grimes 	FTSENT *entry;
9249b50d902SRodney W. Grimes {
9259b50d902SRodney W. Grimes 	return (entry->fts_statp->st_uid == plan->u_data);
9269b50d902SRodney W. Grimes }
9279b50d902SRodney W. Grimes 
9289b50d902SRodney W. Grimes PLAN *
9299b50d902SRodney W. Grimes c_user(username)
9309b50d902SRodney W. Grimes 	char *username;
9319b50d902SRodney W. Grimes {
9329b50d902SRodney W. Grimes 	PLAN *new;
9339b50d902SRodney W. Grimes 	struct passwd *p;
9349b50d902SRodney W. Grimes 	uid_t uid;
9359b50d902SRodney W. Grimes 
9369b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
9379b50d902SRodney W. Grimes 
9389b50d902SRodney W. Grimes 	p = getpwnam(username);
9399b50d902SRodney W. Grimes 	if (p == NULL) {
9409b50d902SRodney W. Grimes 		uid = atoi(username);
9419b50d902SRodney W. Grimes 		if (uid == 0 && username[0] != '0')
9429b50d902SRodney W. Grimes 			errx(1, "-user: %s: no such user", username);
9439b50d902SRodney W. Grimes 	} else
9449b50d902SRodney W. Grimes 		uid = p->pw_uid;
9459b50d902SRodney W. Grimes 
9469b50d902SRodney W. Grimes 	new = palloc(N_USER, f_user);
9479b50d902SRodney W. Grimes 	new->u_data = uid;
9489b50d902SRodney W. Grimes 	return (new);
9499b50d902SRodney W. Grimes }
9509b50d902SRodney W. Grimes 
9519b50d902SRodney W. Grimes /*
9529b50d902SRodney W. Grimes  * -xdev functions --
9539b50d902SRodney W. Grimes  *
9549b50d902SRodney W. Grimes  *	Always true, causes find not to decend past directories that have a
9559b50d902SRodney W. Grimes  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
9569b50d902SRodney W. Grimes  */
9579b50d902SRodney W. Grimes PLAN *
9589b50d902SRodney W. Grimes c_xdev()
9599b50d902SRodney W. Grimes {
9609b50d902SRodney W. Grimes 	ftsoptions |= FTS_XDEV;
9619b50d902SRodney W. Grimes 
9629b50d902SRodney W. Grimes 	return (palloc(N_XDEV, f_always_true));
9639b50d902SRodney W. Grimes }
9649b50d902SRodney W. Grimes 
9659b50d902SRodney W. Grimes /*
9669b50d902SRodney W. Grimes  * ( expression ) functions --
9679b50d902SRodney W. Grimes  *
9689b50d902SRodney W. Grimes  *	True if expression is true.
9699b50d902SRodney W. Grimes  */
9709b50d902SRodney W. Grimes int
9719b50d902SRodney W. Grimes f_expr(plan, entry)
9729b50d902SRodney W. Grimes 	PLAN *plan;
9739b50d902SRodney W. Grimes 	FTSENT *entry;
9749b50d902SRodney W. Grimes {
9759b50d902SRodney W. Grimes 	register PLAN *p;
9769b50d902SRodney W. Grimes 	register int state;
9779b50d902SRodney W. Grimes 
9789b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
9799b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
9809b50d902SRodney W. Grimes 	return (state);
9819b50d902SRodney W. Grimes }
9829b50d902SRodney W. Grimes 
9839b50d902SRodney W. Grimes /*
9849b50d902SRodney W. Grimes  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
9859b50d902SRodney W. Grimes  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
9869b50d902SRodney W. Grimes  * to a N_EXPR node containing the expression and the ')' node is discarded.
9879b50d902SRodney W. Grimes  */
9889b50d902SRodney W. Grimes PLAN *
9899b50d902SRodney W. Grimes c_openparen()
9909b50d902SRodney W. Grimes {
9919b50d902SRodney W. Grimes 	return (palloc(N_OPENPAREN, (int (*)())-1));
9929b50d902SRodney W. Grimes }
9939b50d902SRodney W. Grimes 
9949b50d902SRodney W. Grimes PLAN *
9959b50d902SRodney W. Grimes c_closeparen()
9969b50d902SRodney W. Grimes {
9979b50d902SRodney W. Grimes 	return (palloc(N_CLOSEPAREN, (int (*)())-1));
9989b50d902SRodney W. Grimes }
9999b50d902SRodney W. Grimes 
10009b50d902SRodney W. Grimes /*
10019b50d902SRodney W. Grimes  * ! expression functions --
10029b50d902SRodney W. Grimes  *
10039b50d902SRodney W. Grimes  *	Negation of a primary; the unary NOT operator.
10049b50d902SRodney W. Grimes  */
10059b50d902SRodney W. Grimes int
10069b50d902SRodney W. Grimes f_not(plan, entry)
10079b50d902SRodney W. Grimes 	PLAN *plan;
10089b50d902SRodney W. Grimes 	FTSENT *entry;
10099b50d902SRodney W. Grimes {
10109b50d902SRodney W. Grimes 	register PLAN *p;
10119b50d902SRodney W. Grimes 	register int state;
10129b50d902SRodney W. Grimes 
10139b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
10149b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
10159b50d902SRodney W. Grimes 	return (!state);
10169b50d902SRodney W. Grimes }
10179b50d902SRodney W. Grimes 
10189b50d902SRodney W. Grimes PLAN *
10199b50d902SRodney W. Grimes c_not()
10209b50d902SRodney W. Grimes {
10219b50d902SRodney W. Grimes 	return (palloc(N_NOT, f_not));
10229b50d902SRodney W. Grimes }
10239b50d902SRodney W. Grimes 
10249b50d902SRodney W. Grimes /*
10259b50d902SRodney W. Grimes  * expression -o expression functions --
10269b50d902SRodney W. Grimes  *
10279b50d902SRodney W. Grimes  *	Alternation of primaries; the OR operator.  The second expression is
10289b50d902SRodney W. Grimes  * not evaluated if the first expression is true.
10299b50d902SRodney W. Grimes  */
10309b50d902SRodney W. Grimes int
10319b50d902SRodney W. Grimes f_or(plan, entry)
10329b50d902SRodney W. Grimes 	PLAN *plan;
10339b50d902SRodney W. Grimes 	FTSENT *entry;
10349b50d902SRodney W. Grimes {
10359b50d902SRodney W. Grimes 	register PLAN *p;
10369b50d902SRodney W. Grimes 	register int state;
10379b50d902SRodney W. Grimes 
10389b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
10399b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
10409b50d902SRodney W. Grimes 
10419b50d902SRodney W. Grimes 	if (state)
10429b50d902SRodney W. Grimes 		return (1);
10439b50d902SRodney W. Grimes 
10449b50d902SRodney W. Grimes 	for (p = plan->p_data[1];
10459b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
10469b50d902SRodney W. Grimes 	return (state);
10479b50d902SRodney W. Grimes }
10489b50d902SRodney W. Grimes 
10499b50d902SRodney W. Grimes PLAN *
10509b50d902SRodney W. Grimes c_or()
10519b50d902SRodney W. Grimes {
10529b50d902SRodney W. Grimes 	return (palloc(N_OR, f_or));
10539b50d902SRodney W. Grimes }
10549b50d902SRodney W. Grimes 
10559b50d902SRodney W. Grimes static PLAN *
10569b50d902SRodney W. Grimes palloc(t, f)
10579b50d902SRodney W. Grimes 	enum ntype t;
10589b50d902SRodney W. Grimes 	int (*f) __P((PLAN *, FTSENT *));
10599b50d902SRodney W. Grimes {
10609b50d902SRodney W. Grimes 	PLAN *new;
10619b50d902SRodney W. Grimes 
10629b50d902SRodney W. Grimes 	if ((new = malloc(sizeof(PLAN))) == NULL)
10639b50d902SRodney W. Grimes 		err(1, NULL);
10649b50d902SRodney W. Grimes 	new->type = t;
10659b50d902SRodney W. Grimes 	new->eval = f;
10669b50d902SRodney W. Grimes 	new->flags = 0;
10679b50d902SRodney W. Grimes 	new->next = NULL;
10689b50d902SRodney W. Grimes 	return (new);
10699b50d902SRodney W. Grimes }
1070