xref: /freebsd/usr.bin/find/function.c (revision 841484cd4291e19802fb976f22b28d5dc6706468)
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
38841484cdSPeter Wemm static char sccsid[] = "@(#)function.c	8.10 (Berkeley) 5/4/95";
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 <unistd.h>
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes #include "find.h"
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes #define	COMPARE(a, b) {							\
619b50d902SRodney W. Grimes 	switch (plan->flags) {						\
629b50d902SRodney W. Grimes 	case F_EQUAL:							\
639b50d902SRodney W. Grimes 		return (a == b);					\
649b50d902SRodney W. Grimes 	case F_LESSTHAN:						\
659b50d902SRodney W. Grimes 		return (a < b);						\
669b50d902SRodney W. Grimes 	case F_GREATER:							\
679b50d902SRodney W. Grimes 		return (a > b);						\
689b50d902SRodney W. Grimes 	default:							\
699b50d902SRodney W. Grimes 		abort();						\
709b50d902SRodney W. Grimes 	}								\
719b50d902SRodney W. Grimes }
729b50d902SRodney W. Grimes 
739b50d902SRodney W. Grimes static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
749b50d902SRodney W. Grimes 
759b50d902SRodney W. Grimes /*
769b50d902SRodney W. Grimes  * find_parsenum --
779b50d902SRodney W. Grimes  *	Parse a string of the form [+-]# and return the value.
789b50d902SRodney W. Grimes  */
799192bbf4SBruce Evans static long long
809b50d902SRodney W. Grimes find_parsenum(plan, option, vp, endch)
819b50d902SRodney W. Grimes 	PLAN *plan;
829b50d902SRodney W. Grimes 	char *option, *vp, *endch;
839b50d902SRodney W. Grimes {
849192bbf4SBruce Evans 	long long value;
859b50d902SRodney W. Grimes 	char *endchar, *str;	/* Pointer to character ending conversion. */
869b50d902SRodney W. Grimes 
879b50d902SRodney W. Grimes 	/* Determine comparison from leading + or -. */
889b50d902SRodney W. Grimes 	str = vp;
899b50d902SRodney W. Grimes 	switch (*str) {
909b50d902SRodney W. Grimes 	case '+':
919b50d902SRodney W. Grimes 		++str;
929b50d902SRodney W. Grimes 		plan->flags = F_GREATER;
939b50d902SRodney W. Grimes 		break;
949b50d902SRodney W. Grimes 	case '-':
959b50d902SRodney W. Grimes 		++str;
969b50d902SRodney W. Grimes 		plan->flags = F_LESSTHAN;
979b50d902SRodney W. Grimes 		break;
989b50d902SRodney W. Grimes 	default:
999b50d902SRodney W. Grimes 		plan->flags = F_EQUAL;
1009b50d902SRodney W. Grimes 		break;
1019b50d902SRodney W. Grimes 	}
1029b50d902SRodney W. Grimes 
1039b50d902SRodney W. Grimes 	/*
1049192bbf4SBruce Evans 	 * Convert the string with strtoq().  Note, if strtoq() returns zero
1059b50d902SRodney W. Grimes 	 * and endchar points to the beginning of the string we know we have
1069b50d902SRodney W. Grimes 	 * a syntax error.
1079b50d902SRodney W. Grimes 	 */
1089192bbf4SBruce Evans 	value = strtoq(str, &endchar, 10);
1099b50d902SRodney W. Grimes 	if (value == 0 && endchar == str)
1109b50d902SRodney W. Grimes 		errx(1, "%s: %s: illegal numeric value", option, vp);
1119b50d902SRodney W. Grimes 	if (endchar[0] && (endch == NULL || endchar[0] != *endch))
1129b50d902SRodney W. Grimes 		errx(1, "%s: %s: illegal trailing character", option, vp);
1139b50d902SRodney W. Grimes 	if (endch)
1149b50d902SRodney W. Grimes 		*endch = endchar[0];
1159b50d902SRodney W. Grimes 	return (value);
1169b50d902SRodney W. Grimes }
1179b50d902SRodney W. Grimes 
1189b50d902SRodney W. Grimes /*
1199b50d902SRodney W. Grimes  * The value of n for the inode times (atime, ctime, and mtime) is a range,
1209b50d902SRodney W. Grimes  * i.e. n matches from (n - 1) to n 24 hour periods.  This interacts with
1219b50d902SRodney W. Grimes  * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
1229b50d902SRodney W. Grimes  * user wanted.  Correct so that -1 is "less than 1".
1239b50d902SRodney W. Grimes  */
1249b50d902SRodney W. Grimes #define	TIME_CORRECT(p, ttype)						\
1259b50d902SRodney W. Grimes 	if ((p)->type == ttype && (p)->flags == F_LESSTHAN)		\
1269b50d902SRodney W. Grimes 		++((p)->t_data);
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes /*
1299b50d902SRodney W. Grimes  * -atime n functions --
1309b50d902SRodney W. Grimes  *
1319b50d902SRodney W. Grimes  *	True if the difference between the file access time and the
1329b50d902SRodney W. Grimes  *	current time is n 24 hour periods.
1339b50d902SRodney W. Grimes  */
1349b50d902SRodney W. Grimes int
1359b50d902SRodney W. Grimes f_atime(plan, entry)
1369b50d902SRodney W. Grimes 	PLAN *plan;
1379b50d902SRodney W. Grimes 	FTSENT *entry;
1389b50d902SRodney W. Grimes {
1399b50d902SRodney W. Grimes 	extern time_t now;
1409b50d902SRodney W. Grimes 
1419b50d902SRodney W. Grimes 	COMPARE((now - entry->fts_statp->st_atime +
142656dcd43SGarrett Wollman 	    86400 - 1) / 86400, plan->t_data);
1439b50d902SRodney W. Grimes }
1449b50d902SRodney W. Grimes 
1459b50d902SRodney W. Grimes PLAN *
1469b50d902SRodney W. Grimes c_atime(arg)
1479b50d902SRodney W. Grimes 	char *arg;
1489b50d902SRodney W. Grimes {
1499b50d902SRodney W. Grimes 	PLAN *new;
1509b50d902SRodney W. Grimes 
1519b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
1529b50d902SRodney W. Grimes 
1539b50d902SRodney W. Grimes 	new = palloc(N_ATIME, f_atime);
1549b50d902SRodney W. Grimes 	new->t_data = find_parsenum(new, "-atime", arg, NULL);
1559b50d902SRodney W. Grimes 	TIME_CORRECT(new, N_ATIME);
1569b50d902SRodney W. Grimes 	return (new);
1579b50d902SRodney W. Grimes }
1589b50d902SRodney W. Grimes /*
1599b50d902SRodney W. Grimes  * -ctime n functions --
1609b50d902SRodney W. Grimes  *
1619b50d902SRodney W. Grimes  *	True if the difference between the last change of file
1629b50d902SRodney W. Grimes  *	status information and the current time is n 24 hour periods.
1639b50d902SRodney W. Grimes  */
1649b50d902SRodney W. Grimes int
1659b50d902SRodney W. Grimes f_ctime(plan, entry)
1669b50d902SRodney W. Grimes 	PLAN *plan;
1679b50d902SRodney W. Grimes 	FTSENT *entry;
1689b50d902SRodney W. Grimes {
1699b50d902SRodney W. Grimes 	extern time_t now;
1709b50d902SRodney W. Grimes 
1719b50d902SRodney W. Grimes 	COMPARE((now - entry->fts_statp->st_ctime +
172656dcd43SGarrett Wollman 	    86400 - 1) / 86400, plan->t_data);
1739b50d902SRodney W. Grimes }
1749b50d902SRodney W. Grimes 
1759b50d902SRodney W. Grimes PLAN *
1769b50d902SRodney W. Grimes c_ctime(arg)
1779b50d902SRodney W. Grimes 	char *arg;
1789b50d902SRodney W. Grimes {
1799b50d902SRodney W. Grimes 	PLAN *new;
1809b50d902SRodney W. Grimes 
1819b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
1829b50d902SRodney W. Grimes 
1839b50d902SRodney W. Grimes 	new = palloc(N_CTIME, f_ctime);
1849b50d902SRodney W. Grimes 	new->t_data = find_parsenum(new, "-ctime", arg, NULL);
1859b50d902SRodney W. Grimes 	TIME_CORRECT(new, N_CTIME);
1869b50d902SRodney W. Grimes 	return (new);
1879b50d902SRodney W. Grimes }
1889b50d902SRodney W. Grimes 
1899b50d902SRodney W. Grimes /*
1909b50d902SRodney W. Grimes  * -depth functions --
1919b50d902SRodney W. Grimes  *
1929b50d902SRodney W. Grimes  *	Always true, causes descent of the directory hierarchy to be done
1939b50d902SRodney W. Grimes  *	so that all entries in a directory are acted on before the directory
1949b50d902SRodney W. Grimes  *	itself.
1959b50d902SRodney W. Grimes  */
1969b50d902SRodney W. Grimes int
1979b50d902SRodney W. Grimes f_always_true(plan, entry)
1989b50d902SRodney W. Grimes 	PLAN *plan;
1999b50d902SRodney W. Grimes 	FTSENT *entry;
2009b50d902SRodney W. Grimes {
2019b50d902SRodney W. Grimes 	return (1);
2029b50d902SRodney W. Grimes }
2039b50d902SRodney W. Grimes 
2049b50d902SRodney W. Grimes PLAN *
2059b50d902SRodney W. Grimes c_depth()
2069b50d902SRodney W. Grimes {
2079b50d902SRodney W. Grimes 	isdepth = 1;
2089b50d902SRodney W. Grimes 
2099b50d902SRodney W. Grimes 	return (palloc(N_DEPTH, f_always_true));
2109b50d902SRodney W. Grimes }
2119b50d902SRodney W. Grimes 
2129b50d902SRodney W. Grimes /*
2139b50d902SRodney W. Grimes  * [-exec | -ok] utility [arg ... ] ; functions --
2149b50d902SRodney W. Grimes  *
2159b50d902SRodney W. Grimes  *	True if the executed utility returns a zero value as exit status.
2169b50d902SRodney W. Grimes  *	The end of the primary expression is delimited by a semicolon.  If
2179b50d902SRodney W. Grimes  *	"{}" occurs anywhere, it gets replaced by the current pathname.
2189b50d902SRodney W. Grimes  *	The current directory for the execution of utility is the same as
2199b50d902SRodney W. Grimes  *	the current directory when the find utility was started.
2209b50d902SRodney W. Grimes  *
2219b50d902SRodney W. Grimes  *	The primary -ok is different in that it requests affirmation of the
2229b50d902SRodney W. Grimes  *	user before executing the utility.
2239b50d902SRodney W. Grimes  */
2249b50d902SRodney W. Grimes int
2259b50d902SRodney W. Grimes f_exec(plan, entry)
2269b50d902SRodney W. Grimes 	register PLAN *plan;
2279b50d902SRodney W. Grimes 	FTSENT *entry;
2289b50d902SRodney W. Grimes {
2299b50d902SRodney W. Grimes 	extern int dotfd;
2309b50d902SRodney W. Grimes 	register int cnt;
2319b50d902SRodney W. Grimes 	pid_t pid;
2329b50d902SRodney W. Grimes 	int status;
2339b50d902SRodney W. Grimes 
2349b50d902SRodney W. Grimes 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
2359b50d902SRodney W. Grimes 		if (plan->e_len[cnt])
2369b50d902SRodney W. Grimes 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
2379b50d902SRodney W. Grimes 			    entry->fts_path, plan->e_len[cnt]);
2389b50d902SRodney W. Grimes 
2399b50d902SRodney W. Grimes 	if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
2409b50d902SRodney W. Grimes 		return (0);
2419b50d902SRodney W. Grimes 
242e9f1a293SNate Williams 	/* make sure find output is interspersed correctly with subprocesses */
243e9f1a293SNate Williams 	fflush(stdout);
244e9f1a293SNate Williams 
2459b50d902SRodney W. Grimes 	switch (pid = vfork()) {
2469b50d902SRodney W. Grimes 	case -1:
2479b50d902SRodney W. Grimes 		err(1, "fork");
2489b50d902SRodney W. Grimes 		/* NOTREACHED */
2499b50d902SRodney W. Grimes 	case 0:
2509b50d902SRodney W. Grimes 		if (fchdir(dotfd)) {
2519b50d902SRodney W. Grimes 			warn("chdir");
2529b50d902SRodney W. Grimes 			_exit(1);
2539b50d902SRodney W. Grimes 		}
2549b50d902SRodney W. Grimes 		execvp(plan->e_argv[0], plan->e_argv);
2559b50d902SRodney W. Grimes 		warn("%s", plan->e_argv[0]);
2569b50d902SRodney W. Grimes 		_exit(1);
2579b50d902SRodney W. Grimes 	}
2589b50d902SRodney W. Grimes 	pid = waitpid(pid, &status, 0);
2599b50d902SRodney W. Grimes 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
2609b50d902SRodney W. Grimes }
2619b50d902SRodney W. Grimes 
2629b50d902SRodney W. Grimes /*
2639b50d902SRodney W. Grimes  * c_exec --
2649b50d902SRodney W. Grimes  *	build three parallel arrays, one with pointers to the strings passed
2659b50d902SRodney W. Grimes  *	on the command line, one with (possibly duplicated) pointers to the
2669b50d902SRodney W. Grimes  *	argv array, and one with integer values that are lengths of the
2679b50d902SRodney W. Grimes  *	strings, but also flags meaning that the string has to be massaged.
2689b50d902SRodney W. Grimes  */
2699b50d902SRodney W. Grimes PLAN *
2709b50d902SRodney W. Grimes c_exec(argvp, isok)
2719b50d902SRodney W. Grimes 	char ***argvp;
2729b50d902SRodney W. Grimes 	int isok;
2739b50d902SRodney W. Grimes {
2749b50d902SRodney W. Grimes 	PLAN *new;			/* node returned */
2759b50d902SRodney W. Grimes 	register int cnt;
2769b50d902SRodney W. Grimes 	register char **argv, **ap, *p;
2779b50d902SRodney W. Grimes 
2789b50d902SRodney W. Grimes 	isoutput = 1;
2799b50d902SRodney W. Grimes 
2809b50d902SRodney W. Grimes 	new = palloc(N_EXEC, f_exec);
2819b50d902SRodney W. Grimes 	if (isok)
2829b50d902SRodney W. Grimes 		new->flags = F_NEEDOK;
2839b50d902SRodney W. Grimes 
2849b50d902SRodney W. Grimes 	for (ap = argv = *argvp;; ++ap) {
2859b50d902SRodney W. Grimes 		if (!*ap)
2869b50d902SRodney W. Grimes 			errx(1,
2879b50d902SRodney W. Grimes 			    "%s: no terminating \";\"", isok ? "-ok" : "-exec");
2889b50d902SRodney W. Grimes 		if (**ap == ';')
2899b50d902SRodney W. Grimes 			break;
2909b50d902SRodney W. Grimes 	}
2919b50d902SRodney W. Grimes 
2929b50d902SRodney W. Grimes 	cnt = ap - *argvp + 1;
2939b50d902SRodney W. Grimes 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
2949b50d902SRodney W. Grimes 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
2959b50d902SRodney W. Grimes 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
2969b50d902SRodney W. Grimes 
2979b50d902SRodney W. Grimes 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
2989b50d902SRodney W. Grimes 		new->e_orig[cnt] = *argv;
2999b50d902SRodney W. Grimes 		for (p = *argv; *p; ++p)
3009b50d902SRodney W. Grimes 			if (p[0] == '{' && p[1] == '}') {
3019b50d902SRodney W. Grimes 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
3029b50d902SRodney W. Grimes 				new->e_len[cnt] = MAXPATHLEN;
3039b50d902SRodney W. Grimes 				break;
3049b50d902SRodney W. Grimes 			}
3059b50d902SRodney W. Grimes 		if (!*p) {
3069b50d902SRodney W. Grimes 			new->e_argv[cnt] = *argv;
3079b50d902SRodney W. Grimes 			new->e_len[cnt] = 0;
3089b50d902SRodney W. Grimes 		}
3099b50d902SRodney W. Grimes 	}
3109b50d902SRodney W. Grimes 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes 	*argvp = argv + 1;
3139b50d902SRodney W. Grimes 	return (new);
3149b50d902SRodney W. Grimes }
3159b50d902SRodney W. Grimes 
3169b50d902SRodney W. Grimes /*
3179b50d902SRodney W. Grimes  * -follow functions --
3189b50d902SRodney W. Grimes  *
3199b50d902SRodney W. Grimes  *	Always true, causes symbolic links to be followed on a global
3209b50d902SRodney W. Grimes  *	basis.
3219b50d902SRodney W. Grimes  */
3229b50d902SRodney W. Grimes PLAN *
3239b50d902SRodney W. Grimes c_follow()
3249b50d902SRodney W. Grimes {
3259b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_PHYSICAL;
3269b50d902SRodney W. Grimes 	ftsoptions |= FTS_LOGICAL;
3279b50d902SRodney W. Grimes 
3289b50d902SRodney W. Grimes 	return (palloc(N_FOLLOW, f_always_true));
3299b50d902SRodney W. Grimes }
3309b50d902SRodney W. Grimes 
3319b50d902SRodney W. Grimes /*
3329b50d902SRodney W. Grimes  * -fstype functions --
3339b50d902SRodney W. Grimes  *
3349b50d902SRodney W. Grimes  *	True if the file is of a certain type.
3359b50d902SRodney W. Grimes  */
3369b50d902SRodney W. Grimes int
3379b50d902SRodney W. Grimes f_fstype(plan, entry)
3389b50d902SRodney W. Grimes 	PLAN *plan;
3399b50d902SRodney W. Grimes 	FTSENT *entry;
3409b50d902SRodney W. Grimes {
3419b50d902SRodney W. Grimes 	static dev_t curdev;	/* need a guaranteed illegal dev value */
3429b50d902SRodney W. Grimes 	static int first = 1;
3439b50d902SRodney W. Grimes 	struct statfs sb;
3449b50d902SRodney W. Grimes 	static short val;
3459b50d902SRodney W. Grimes 	char *p, save[2];
3469b50d902SRodney W. Grimes 
3479b50d902SRodney W. Grimes 	/* Only check when we cross mount point. */
3489b50d902SRodney W. Grimes 	if (first || curdev != entry->fts_statp->st_dev) {
3499b50d902SRodney W. Grimes 		curdev = entry->fts_statp->st_dev;
3509b50d902SRodney W. Grimes 
3519b50d902SRodney W. Grimes 		/*
3529b50d902SRodney W. Grimes 		 * Statfs follows symlinks; find wants the link's file system,
3539b50d902SRodney W. Grimes 		 * not where it points.
3549b50d902SRodney W. Grimes 		 */
3559b50d902SRodney W. Grimes 		if (entry->fts_info == FTS_SL ||
3569b50d902SRodney W. Grimes 		    entry->fts_info == FTS_SLNONE) {
3579b50d902SRodney W. Grimes 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
3589b50d902SRodney W. Grimes 				++p;
3599b50d902SRodney W. Grimes 			else
3609b50d902SRodney W. Grimes 				p = entry->fts_accpath;
3619b50d902SRodney W. Grimes 			save[0] = p[0];
3629b50d902SRodney W. Grimes 			p[0] = '.';
3639b50d902SRodney W. Grimes 			save[1] = p[1];
3649b50d902SRodney W. Grimes 			p[1] = '\0';
3659b50d902SRodney W. Grimes 
3669b50d902SRodney W. Grimes 		} else
3679b50d902SRodney W. Grimes 			p = NULL;
3689b50d902SRodney W. Grimes 
3699b50d902SRodney W. Grimes 		if (statfs(entry->fts_accpath, &sb))
3709b50d902SRodney W. Grimes 			err(1, "%s", entry->fts_accpath);
3719b50d902SRodney W. Grimes 
3729b50d902SRodney W. Grimes 		if (p) {
3739b50d902SRodney W. Grimes 			p[0] = save[0];
3749b50d902SRodney W. Grimes 			p[1] = save[1];
3759b50d902SRodney W. Grimes 		}
3769b50d902SRodney W. Grimes 
3779b50d902SRodney W. Grimes 		first = 0;
378841484cdSPeter Wemm 
379841484cdSPeter Wemm 		/*
380841484cdSPeter Wemm 		 * Further tests may need both of these values, so
381841484cdSPeter Wemm 		 * always copy both of them.
382841484cdSPeter Wemm 		 */
3839b50d902SRodney W. Grimes 		val = sb.f_flags;
3849b50d902SRodney W. Grimes 		val = sb.f_type;
3859b50d902SRodney W. Grimes 	}
3869b50d902SRodney W. Grimes 	switch (plan->flags) {
3879b50d902SRodney W. Grimes 	case F_MTFLAG:
3889b50d902SRodney W. Grimes 		return (val & plan->mt_data);
3899b50d902SRodney W. Grimes 	case F_MTTYPE:
3909b50d902SRodney W. Grimes 		return (val == plan->mt_data);
3919b50d902SRodney W. Grimes 	default:
3929b50d902SRodney W. Grimes 		abort();
3939b50d902SRodney W. Grimes 	}
3949b50d902SRodney W. Grimes }
3959b50d902SRodney W. Grimes 
3969b50d902SRodney W. Grimes PLAN *
3979b50d902SRodney W. Grimes c_fstype(arg)
3989b50d902SRodney W. Grimes 	char *arg;
3999b50d902SRodney W. Grimes {
4009b50d902SRodney W. Grimes 	register PLAN *new;
401841484cdSPeter Wemm 	struct vfsconf vfc;
4029b50d902SRodney W. Grimes 
4039b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
4049b50d902SRodney W. Grimes 
4059b50d902SRodney W. Grimes 	new = palloc(N_FSTYPE, f_fstype);
406841484cdSPeter Wemm 
407841484cdSPeter Wemm 	/*
408841484cdSPeter Wemm 	 * Check first for a filesystem name.
409841484cdSPeter Wemm 	 */
410841484cdSPeter Wemm 	if (getvfsbyname(arg, &vfc) == 0) {
411841484cdSPeter Wemm 		new->flags = F_MTTYPE;
412841484cdSPeter Wemm 		new->mt_data = vfc.vfc_typenum;
413841484cdSPeter Wemm 		return (new);
414841484cdSPeter Wemm 	}
415841484cdSPeter Wemm 
4169b50d902SRodney W. Grimes 	switch (*arg) {
4179b50d902SRodney W. Grimes 	case 'l':
4189b50d902SRodney W. Grimes 		if (!strcmp(arg, "local")) {
4199b50d902SRodney W. Grimes 			new->flags = F_MTFLAG;
4209b50d902SRodney W. Grimes 			new->mt_data = MNT_LOCAL;
4219b50d902SRodney W. Grimes 			return (new);
4229b50d902SRodney W. Grimes 		}
4239b50d902SRodney W. Grimes 		break;
4249b50d902SRodney W. Grimes 	case 'r':
4259b50d902SRodney W. Grimes 		if (!strcmp(arg, "rdonly")) {
4269b50d902SRodney W. Grimes 			new->flags = F_MTFLAG;
4279b50d902SRodney W. Grimes 			new->mt_data = MNT_RDONLY;
4289b50d902SRodney W. Grimes 			return (new);
4299b50d902SRodney W. Grimes 		}
4309b50d902SRodney W. Grimes 		break;
4319b50d902SRodney W. Grimes 	}
4329b50d902SRodney W. Grimes 	errx(1, "%s: unknown file type", arg);
4339b50d902SRodney W. Grimes 	/* NOTREACHED */
4349b50d902SRodney W. Grimes }
4359b50d902SRodney W. Grimes 
4369b50d902SRodney W. Grimes /*
4379b50d902SRodney W. Grimes  * -group gname functions --
4389b50d902SRodney W. Grimes  *
4399b50d902SRodney W. Grimes  *	True if the file belongs to the group gname.  If gname is numeric and
4409b50d902SRodney W. Grimes  *	an equivalent of the getgrnam() function does not return a valid group
4419b50d902SRodney W. Grimes  *	name, gname is taken as a group ID.
4429b50d902SRodney W. Grimes  */
4439b50d902SRodney W. Grimes int
4449b50d902SRodney W. Grimes f_group(plan, entry)
4459b50d902SRodney W. Grimes 	PLAN *plan;
4469b50d902SRodney W. Grimes 	FTSENT *entry;
4479b50d902SRodney W. Grimes {
4489b50d902SRodney W. Grimes 	return (entry->fts_statp->st_gid == plan->g_data);
4499b50d902SRodney W. Grimes }
4509b50d902SRodney W. Grimes 
4519b50d902SRodney W. Grimes PLAN *
4529b50d902SRodney W. Grimes c_group(gname)
4539b50d902SRodney W. Grimes 	char *gname;
4549b50d902SRodney W. Grimes {
4559b50d902SRodney W. Grimes 	PLAN *new;
4569b50d902SRodney W. Grimes 	struct group *g;
4579b50d902SRodney W. Grimes 	gid_t gid;
4589b50d902SRodney W. Grimes 
4599b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
4609b50d902SRodney W. Grimes 
4619b50d902SRodney W. Grimes 	g = getgrnam(gname);
4629b50d902SRodney W. Grimes 	if (g == NULL) {
4639b50d902SRodney W. Grimes 		gid = atoi(gname);
4649b50d902SRodney W. Grimes 		if (gid == 0 && gname[0] != '0')
4659b50d902SRodney W. Grimes 			errx(1, "-group: %s: no such group", gname);
4669b50d902SRodney W. Grimes 	} else
4679b50d902SRodney W. Grimes 		gid = g->gr_gid;
4689b50d902SRodney W. Grimes 
4699b50d902SRodney W. Grimes 	new = palloc(N_GROUP, f_group);
4709b50d902SRodney W. Grimes 	new->g_data = gid;
4719b50d902SRodney W. Grimes 	return (new);
4729b50d902SRodney W. Grimes }
4739b50d902SRodney W. Grimes 
4749b50d902SRodney W. Grimes /*
4759b50d902SRodney W. Grimes  * -inum n functions --
4769b50d902SRodney W. Grimes  *
4779b50d902SRodney W. Grimes  *	True if the file has inode # n.
4789b50d902SRodney W. Grimes  */
4799b50d902SRodney W. Grimes int
4809b50d902SRodney W. Grimes f_inum(plan, entry)
4819b50d902SRodney W. Grimes 	PLAN *plan;
4829b50d902SRodney W. Grimes 	FTSENT *entry;
4839b50d902SRodney W. Grimes {
4849b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
4859b50d902SRodney W. Grimes }
4869b50d902SRodney W. Grimes 
4879b50d902SRodney W. Grimes PLAN *
4889b50d902SRodney W. Grimes c_inum(arg)
4899b50d902SRodney W. Grimes 	char *arg;
4909b50d902SRodney W. Grimes {
4919b50d902SRodney W. Grimes 	PLAN *new;
4929b50d902SRodney W. Grimes 
4939b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
4949b50d902SRodney W. Grimes 
4959b50d902SRodney W. Grimes 	new = palloc(N_INUM, f_inum);
4969b50d902SRodney W. Grimes 	new->i_data = find_parsenum(new, "-inum", arg, NULL);
4979b50d902SRodney W. Grimes 	return (new);
4989b50d902SRodney W. Grimes }
4999b50d902SRodney W. Grimes 
5009b50d902SRodney W. Grimes /*
5019b50d902SRodney W. Grimes  * -links n functions --
5029b50d902SRodney W. Grimes  *
5039b50d902SRodney W. Grimes  *	True if the file has n links.
5049b50d902SRodney W. Grimes  */
5059b50d902SRodney W. Grimes int
5069b50d902SRodney W. Grimes f_links(plan, entry)
5079b50d902SRodney W. Grimes 	PLAN *plan;
5089b50d902SRodney W. Grimes 	FTSENT *entry;
5099b50d902SRodney W. Grimes {
5109b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
5119b50d902SRodney W. Grimes }
5129b50d902SRodney W. Grimes 
5139b50d902SRodney W. Grimes PLAN *
5149b50d902SRodney W. Grimes c_links(arg)
5159b50d902SRodney W. Grimes 	char *arg;
5169b50d902SRodney W. Grimes {
5179b50d902SRodney W. Grimes 	PLAN *new;
5189b50d902SRodney W. Grimes 
5199b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
5209b50d902SRodney W. Grimes 
5219b50d902SRodney W. Grimes 	new = palloc(N_LINKS, f_links);
5229b50d902SRodney W. Grimes 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
5239b50d902SRodney W. Grimes 	return (new);
5249b50d902SRodney W. Grimes }
5259b50d902SRodney W. Grimes 
5269b50d902SRodney W. Grimes /*
5279b50d902SRodney W. Grimes  * -ls functions --
5289b50d902SRodney W. Grimes  *
5299b50d902SRodney W. Grimes  *	Always true - prints the current entry to stdout in "ls" format.
5309b50d902SRodney W. Grimes  */
5319b50d902SRodney W. Grimes int
5329b50d902SRodney W. Grimes f_ls(plan, entry)
5339b50d902SRodney W. Grimes 	PLAN *plan;
5349b50d902SRodney W. Grimes 	FTSENT *entry;
5359b50d902SRodney W. Grimes {
5369b50d902SRodney W. Grimes 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
5379b50d902SRodney W. Grimes 	return (1);
5389b50d902SRodney W. Grimes }
5399b50d902SRodney W. Grimes 
5409b50d902SRodney W. Grimes PLAN *
5419b50d902SRodney W. Grimes c_ls()
5429b50d902SRodney W. Grimes {
5439b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
5449b50d902SRodney W. Grimes 	isoutput = 1;
5459b50d902SRodney W. Grimes 
5469b50d902SRodney W. Grimes 	return (palloc(N_LS, f_ls));
5479b50d902SRodney W. Grimes }
5489b50d902SRodney W. Grimes 
5499b50d902SRodney W. Grimes /*
5509b50d902SRodney W. Grimes  * -mtime n functions --
5519b50d902SRodney W. Grimes  *
5529b50d902SRodney W. Grimes  *	True if the difference between the file modification time and the
5539b50d902SRodney W. Grimes  *	current time is n 24 hour periods.
5549b50d902SRodney W. Grimes  */
5559b50d902SRodney W. Grimes int
5569b50d902SRodney W. Grimes f_mtime(plan, entry)
5579b50d902SRodney W. Grimes 	PLAN *plan;
5589b50d902SRodney W. Grimes 	FTSENT *entry;
5599b50d902SRodney W. Grimes {
5609b50d902SRodney W. Grimes 	extern time_t now;
5619b50d902SRodney W. Grimes 
562656dcd43SGarrett Wollman 	COMPARE((now - entry->fts_statp->st_mtime + 86400 - 1) /
563656dcd43SGarrett Wollman 	    86400, plan->t_data);
5649b50d902SRodney W. Grimes }
5659b50d902SRodney W. Grimes 
5669b50d902SRodney W. Grimes PLAN *
5679b50d902SRodney W. Grimes c_mtime(arg)
5689b50d902SRodney W. Grimes 	char *arg;
5699b50d902SRodney W. Grimes {
5709b50d902SRodney W. Grimes 	PLAN *new;
5719b50d902SRodney W. Grimes 
5729b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
5739b50d902SRodney W. Grimes 
5749b50d902SRodney W. Grimes 	new = palloc(N_MTIME, f_mtime);
5759b50d902SRodney W. Grimes 	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
5769b50d902SRodney W. Grimes 	TIME_CORRECT(new, N_MTIME);
5779b50d902SRodney W. Grimes 	return (new);
5789b50d902SRodney W. Grimes }
5799b50d902SRodney W. Grimes 
5809b50d902SRodney W. Grimes /*
5819b50d902SRodney W. Grimes  * -name functions --
5829b50d902SRodney W. Grimes  *
5839b50d902SRodney W. Grimes  *	True if the basename of the filename being examined
5849b50d902SRodney W. Grimes  *	matches pattern using Pattern Matching Notation S3.14
5859b50d902SRodney W. Grimes  */
5869b50d902SRodney W. Grimes int
5879b50d902SRodney W. Grimes f_name(plan, entry)
5889b50d902SRodney W. Grimes 	PLAN *plan;
5899b50d902SRodney W. Grimes 	FTSENT *entry;
5909b50d902SRodney W. Grimes {
5919b50d902SRodney W. Grimes 	return (!fnmatch(plan->c_data, entry->fts_name, 0));
5929b50d902SRodney W. Grimes }
5939b50d902SRodney W. Grimes 
5949b50d902SRodney W. Grimes PLAN *
5959b50d902SRodney W. Grimes c_name(pattern)
5969b50d902SRodney W. Grimes 	char *pattern;
5979b50d902SRodney W. Grimes {
5989b50d902SRodney W. Grimes 	PLAN *new;
5999b50d902SRodney W. Grimes 
6009b50d902SRodney W. Grimes 	new = palloc(N_NAME, f_name);
6019b50d902SRodney W. Grimes 	new->c_data = pattern;
6029b50d902SRodney W. Grimes 	return (new);
6039b50d902SRodney W. Grimes }
6049b50d902SRodney W. Grimes 
6059b50d902SRodney W. Grimes /*
6069b50d902SRodney W. Grimes  * -newer file functions --
6079b50d902SRodney W. Grimes  *
6089b50d902SRodney W. Grimes  *	True if the current file has been modified more recently
6099b50d902SRodney W. Grimes  *	then the modification time of the file named by the pathname
6109b50d902SRodney W. Grimes  *	file.
6119b50d902SRodney W. Grimes  */
6129b50d902SRodney W. Grimes int
6139b50d902SRodney W. Grimes f_newer(plan, entry)
6149b50d902SRodney W. Grimes 	PLAN *plan;
6159b50d902SRodney W. Grimes 	FTSENT *entry;
6169b50d902SRodney W. Grimes {
6179b50d902SRodney W. Grimes 	return (entry->fts_statp->st_mtime > plan->t_data);
6189b50d902SRodney W. Grimes }
6199b50d902SRodney W. Grimes 
6209b50d902SRodney W. Grimes PLAN *
6219b50d902SRodney W. Grimes c_newer(filename)
6229b50d902SRodney W. Grimes 	char *filename;
6239b50d902SRodney W. Grimes {
6249b50d902SRodney W. Grimes 	PLAN *new;
6259b50d902SRodney W. Grimes 	struct stat sb;
6269b50d902SRodney W. Grimes 
6279b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
6289b50d902SRodney W. Grimes 
6299b50d902SRodney W. Grimes 	if (stat(filename, &sb))
6309b50d902SRodney W. Grimes 		err(1, "%s", filename);
6319b50d902SRodney W. Grimes 	new = palloc(N_NEWER, f_newer);
6329b50d902SRodney W. Grimes 	new->t_data = sb.st_mtime;
6339b50d902SRodney W. Grimes 	return (new);
6349b50d902SRodney W. Grimes }
6359b50d902SRodney W. Grimes 
6369b50d902SRodney W. Grimes /*
6379b50d902SRodney W. Grimes  * -nogroup functions --
6389b50d902SRodney W. Grimes  *
6399b50d902SRodney W. Grimes  *	True if file belongs to a user ID for which the equivalent
6409b50d902SRodney W. Grimes  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
6419b50d902SRodney W. Grimes  */
6429b50d902SRodney W. Grimes int
6439b50d902SRodney W. Grimes f_nogroup(plan, entry)
6449b50d902SRodney W. Grimes 	PLAN *plan;
6459b50d902SRodney W. Grimes 	FTSENT *entry;
6469b50d902SRodney W. Grimes {
6479b50d902SRodney W. Grimes 	char *group_from_gid();
6489b50d902SRodney W. Grimes 
6499b50d902SRodney W. Grimes 	return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
6509b50d902SRodney W. Grimes }
6519b50d902SRodney W. Grimes 
6529b50d902SRodney W. Grimes PLAN *
6539b50d902SRodney W. Grimes c_nogroup()
6549b50d902SRodney W. Grimes {
6559b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
6569b50d902SRodney W. Grimes 
6579b50d902SRodney W. Grimes 	return (palloc(N_NOGROUP, f_nogroup));
6589b50d902SRodney W. Grimes }
6599b50d902SRodney W. Grimes 
6609b50d902SRodney W. Grimes /*
6619b50d902SRodney W. Grimes  * -nouser functions --
6629b50d902SRodney W. Grimes  *
6639b50d902SRodney W. Grimes  *	True if file belongs to a user ID for which the equivalent
6649b50d902SRodney W. Grimes  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
6659b50d902SRodney W. Grimes  */
6669b50d902SRodney W. Grimes int
6679b50d902SRodney W. Grimes f_nouser(plan, entry)
6689b50d902SRodney W. Grimes 	PLAN *plan;
6699b50d902SRodney W. Grimes 	FTSENT *entry;
6709b50d902SRodney W. Grimes {
6719b50d902SRodney W. Grimes 	char *user_from_uid();
6729b50d902SRodney W. Grimes 
6739b50d902SRodney W. Grimes 	return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
6749b50d902SRodney W. Grimes }
6759b50d902SRodney W. Grimes 
6769b50d902SRodney W. Grimes PLAN *
6779b50d902SRodney W. Grimes c_nouser()
6789b50d902SRodney W. Grimes {
6799b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
6809b50d902SRodney W. Grimes 
6819b50d902SRodney W. Grimes 	return (palloc(N_NOUSER, f_nouser));
6829b50d902SRodney W. Grimes }
6839b50d902SRodney W. Grimes 
6849b50d902SRodney W. Grimes /*
6859b50d902SRodney W. Grimes  * -path functions --
6869b50d902SRodney W. Grimes  *
6879b50d902SRodney W. Grimes  *	True if the path of the filename being examined
6889b50d902SRodney W. Grimes  *	matches pattern using Pattern Matching Notation S3.14
6899b50d902SRodney W. Grimes  */
6909b50d902SRodney W. Grimes int
6919b50d902SRodney W. Grimes f_path(plan, entry)
6929b50d902SRodney W. Grimes 	PLAN *plan;
6939b50d902SRodney W. Grimes 	FTSENT *entry;
6949b50d902SRodney W. Grimes {
6959b50d902SRodney W. Grimes 	return (!fnmatch(plan->c_data, entry->fts_path, 0));
6969b50d902SRodney W. Grimes }
6979b50d902SRodney W. Grimes 
6989b50d902SRodney W. Grimes PLAN *
6999b50d902SRodney W. Grimes c_path(pattern)
7009b50d902SRodney W. Grimes 	char *pattern;
7019b50d902SRodney W. Grimes {
7029b50d902SRodney W. Grimes 	PLAN *new;
7039b50d902SRodney W. Grimes 
7049b50d902SRodney W. Grimes 	new = palloc(N_NAME, f_path);
7059b50d902SRodney W. Grimes 	new->c_data = pattern;
7069b50d902SRodney W. Grimes 	return (new);
7079b50d902SRodney W. Grimes }
7089b50d902SRodney W. Grimes 
7099b50d902SRodney W. Grimes /*
7109b50d902SRodney W. Grimes  * -perm functions --
7119b50d902SRodney W. Grimes  *
7129b50d902SRodney W. Grimes  *	The mode argument is used to represent file mode bits.  If it starts
7139b50d902SRodney W. Grimes  *	with a leading digit, it's treated as an octal mode, otherwise as a
7149b50d902SRodney W. Grimes  *	symbolic mode.
7159b50d902SRodney W. Grimes  */
7169b50d902SRodney W. Grimes int
7179b50d902SRodney W. Grimes f_perm(plan, entry)
7189b50d902SRodney W. Grimes 	PLAN *plan;
7199b50d902SRodney W. Grimes 	FTSENT *entry;
7209b50d902SRodney W. Grimes {
7219b50d902SRodney W. Grimes 	mode_t mode;
7229b50d902SRodney W. Grimes 
7239b50d902SRodney W. Grimes 	mode = entry->fts_statp->st_mode &
7249b50d902SRodney W. Grimes 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
7259b50d902SRodney W. Grimes 	if (plan->flags == F_ATLEAST)
7269b50d902SRodney W. Grimes 		return ((plan->m_data | mode) == mode);
7279b50d902SRodney W. Grimes 	else
7289b50d902SRodney W. Grimes 		return (mode == plan->m_data);
7299b50d902SRodney W. Grimes 	/* NOTREACHED */
7309b50d902SRodney W. Grimes }
7319b50d902SRodney W. Grimes 
7329b50d902SRodney W. Grimes PLAN *
7339b50d902SRodney W. Grimes c_perm(perm)
7349b50d902SRodney W. Grimes 	char *perm;
7359b50d902SRodney W. Grimes {
7369b50d902SRodney W. Grimes 	PLAN *new;
7379b50d902SRodney W. Grimes 	mode_t *set;
7389b50d902SRodney W. Grimes 
7399b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
7409b50d902SRodney W. Grimes 
7419b50d902SRodney W. Grimes 	new = palloc(N_PERM, f_perm);
7429b50d902SRodney W. Grimes 
7439b50d902SRodney W. Grimes 	if (*perm == '-') {
7449b50d902SRodney W. Grimes 		new->flags = F_ATLEAST;
7459b50d902SRodney W. Grimes 		++perm;
7469b50d902SRodney W. Grimes 	}
7479b50d902SRodney W. Grimes 
7489b50d902SRodney W. Grimes 	if ((set = setmode(perm)) == NULL)
7499b50d902SRodney W. Grimes 		err(1, "-perm: %s: illegal mode string", perm);
7509b50d902SRodney W. Grimes 
7519b50d902SRodney W. Grimes 	new->m_data = getmode(set, 0);
7529b50d902SRodney W. Grimes 	return (new);
7539b50d902SRodney W. Grimes }
7549b50d902SRodney W. Grimes 
7559b50d902SRodney W. Grimes /*
7569b50d902SRodney W. Grimes  * -print functions --
7579b50d902SRodney W. Grimes  *
7589b50d902SRodney W. Grimes  *	Always true, causes the current pathame to be written to
7599b50d902SRodney W. Grimes  *	standard output.
7609b50d902SRodney W. Grimes  */
7619b50d902SRodney W. Grimes int
7629b50d902SRodney W. Grimes f_print(plan, entry)
7639b50d902SRodney W. Grimes 	PLAN *plan;
7649b50d902SRodney W. Grimes 	FTSENT *entry;
7659b50d902SRodney W. Grimes {
76681e236a0SGarrett Wollman 	(void)puts(entry->fts_path);
7679b50d902SRodney W. Grimes 	return (1);
7689b50d902SRodney W. Grimes }
7699b50d902SRodney W. Grimes 
7709b50d902SRodney W. Grimes PLAN *
7719b50d902SRodney W. Grimes c_print()
7729b50d902SRodney W. Grimes {
7739b50d902SRodney W. Grimes 	isoutput = 1;
7749b50d902SRodney W. Grimes 
7759b50d902SRodney W. Grimes 	return (palloc(N_PRINT, f_print));
7769b50d902SRodney W. Grimes }
7779b50d902SRodney W. Grimes 
7789b50d902SRodney W. Grimes /*
7797cd23434SGarrett Wollman  * -print0 functions --
7807cd23434SGarrett Wollman  *
7817cd23434SGarrett Wollman  *	Always true, causes the current pathame to be written to
7827cd23434SGarrett Wollman  *	standard output followed by a NUL character
7837cd23434SGarrett Wollman  */
7847cd23434SGarrett Wollman int
7857cd23434SGarrett Wollman f_print0(plan, entry)
7867cd23434SGarrett Wollman 	PLAN *plan;
7877cd23434SGarrett Wollman 	FTSENT *entry;
7887cd23434SGarrett Wollman {
7897cd23434SGarrett Wollman 	fputs(entry->fts_path, stdout);
7907cd23434SGarrett Wollman 	fputc('\0', stdout);
7917cd23434SGarrett Wollman 	return (1);
7927cd23434SGarrett Wollman }
7937cd23434SGarrett Wollman 
7947cd23434SGarrett Wollman PLAN *
7957cd23434SGarrett Wollman c_print0()
7967cd23434SGarrett Wollman {
7977cd23434SGarrett Wollman 	isoutput = 1;
7987cd23434SGarrett Wollman 
7997cd23434SGarrett Wollman 	return (palloc(N_PRINT0, f_print0));
8007cd23434SGarrett Wollman }
8017cd23434SGarrett Wollman 
8027cd23434SGarrett Wollman /*
8039b50d902SRodney W. Grimes  * -prune functions --
8049b50d902SRodney W. Grimes  *
8059b50d902SRodney W. Grimes  *	Prune a portion of the hierarchy.
8069b50d902SRodney W. Grimes  */
8079b50d902SRodney W. Grimes int
8089b50d902SRodney W. Grimes f_prune(plan, entry)
8099b50d902SRodney W. Grimes 	PLAN *plan;
8109b50d902SRodney W. Grimes 	FTSENT *entry;
8119b50d902SRodney W. Grimes {
8129b50d902SRodney W. Grimes 	extern FTS *tree;
8139b50d902SRodney W. Grimes 
8149b50d902SRodney W. Grimes 	if (fts_set(tree, entry, FTS_SKIP))
8159b50d902SRodney W. Grimes 		err(1, "%s", entry->fts_path);
8169b50d902SRodney W. Grimes 	return (1);
8179b50d902SRodney W. Grimes }
8189b50d902SRodney W. Grimes 
8199b50d902SRodney W. Grimes PLAN *
8209b50d902SRodney W. Grimes c_prune()
8219b50d902SRodney W. Grimes {
8229b50d902SRodney W. Grimes 	return (palloc(N_PRUNE, f_prune));
8239b50d902SRodney W. Grimes }
8249b50d902SRodney W. Grimes 
8259b50d902SRodney W. Grimes /*
8269b50d902SRodney W. Grimes  * -size n[c] functions --
8279b50d902SRodney W. Grimes  *
8289b50d902SRodney W. Grimes  *	True if the file size in bytes, divided by an implementation defined
8299b50d902SRodney W. Grimes  *	value and rounded up to the next integer, is n.  If n is followed by
8309b50d902SRodney W. Grimes  *	a c, the size is in bytes.
8319b50d902SRodney W. Grimes  */
8329b50d902SRodney W. Grimes #define	FIND_SIZE	512
8339b50d902SRodney W. Grimes static int divsize = 1;
8349b50d902SRodney W. Grimes 
8359b50d902SRodney W. Grimes int
8369b50d902SRodney W. Grimes f_size(plan, entry)
8379b50d902SRodney W. Grimes 	PLAN *plan;
8389b50d902SRodney W. Grimes 	FTSENT *entry;
8399b50d902SRodney W. Grimes {
8409b50d902SRodney W. Grimes 	off_t size;
8419b50d902SRodney W. Grimes 
8429b50d902SRodney W. Grimes 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
8439b50d902SRodney W. Grimes 	    FIND_SIZE : entry->fts_statp->st_size;
8449b50d902SRodney W. Grimes 	COMPARE(size, plan->o_data);
8459b50d902SRodney W. Grimes }
8469b50d902SRodney W. Grimes 
8479b50d902SRodney W. Grimes PLAN *
8489b50d902SRodney W. Grimes c_size(arg)
8499b50d902SRodney W. Grimes 	char *arg;
8509b50d902SRodney W. Grimes {
8519b50d902SRodney W. Grimes 	PLAN *new;
8529b50d902SRodney W. Grimes 	char endch;
8539b50d902SRodney W. Grimes 
8549b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
8559b50d902SRodney W. Grimes 
8569b50d902SRodney W. Grimes 	new = palloc(N_SIZE, f_size);
8579b50d902SRodney W. Grimes 	endch = 'c';
8589b50d902SRodney W. Grimes 	new->o_data = find_parsenum(new, "-size", arg, &endch);
8599b50d902SRodney W. Grimes 	if (endch == 'c')
8609b50d902SRodney W. Grimes 		divsize = 0;
8619b50d902SRodney W. Grimes 	return (new);
8629b50d902SRodney W. Grimes }
8639b50d902SRodney W. Grimes 
8649b50d902SRodney W. Grimes /*
8659b50d902SRodney W. Grimes  * -type c functions --
8669b50d902SRodney W. Grimes  *
867841484cdSPeter Wemm  *	True if the type of the file is c, where c is b, c, d, p, f or w
868841484cdSPeter Wemm  *	for block special file, character special file, directory, FIFO,
869841484cdSPeter Wemm  *	regular file or whiteout respectively.
8709b50d902SRodney W. Grimes  */
8719b50d902SRodney W. Grimes int
8729b50d902SRodney W. Grimes f_type(plan, entry)
8739b50d902SRodney W. Grimes 	PLAN *plan;
8749b50d902SRodney W. Grimes 	FTSENT *entry;
8759b50d902SRodney W. Grimes {
8769b50d902SRodney W. Grimes 	return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
8779b50d902SRodney W. Grimes }
8789b50d902SRodney W. Grimes 
8799b50d902SRodney W. Grimes PLAN *
8809b50d902SRodney W. Grimes c_type(typestring)
8819b50d902SRodney W. Grimes 	char *typestring;
8829b50d902SRodney W. Grimes {
8839b50d902SRodney W. Grimes 	PLAN *new;
8849b50d902SRodney W. Grimes 	mode_t  mask;
8859b50d902SRodney W. Grimes 
8869b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
8879b50d902SRodney W. Grimes 
8889b50d902SRodney W. Grimes 	switch (typestring[0]) {
8899b50d902SRodney W. Grimes 	case 'b':
8909b50d902SRodney W. Grimes 		mask = S_IFBLK;
8919b50d902SRodney W. Grimes 		break;
8929b50d902SRodney W. Grimes 	case 'c':
8939b50d902SRodney W. Grimes 		mask = S_IFCHR;
8949b50d902SRodney W. Grimes 		break;
8959b50d902SRodney W. Grimes 	case 'd':
8969b50d902SRodney W. Grimes 		mask = S_IFDIR;
8979b50d902SRodney W. Grimes 		break;
8989b50d902SRodney W. Grimes 	case 'f':
8999b50d902SRodney W. Grimes 		mask = S_IFREG;
9009b50d902SRodney W. Grimes 		break;
9019b50d902SRodney W. Grimes 	case 'l':
9029b50d902SRodney W. Grimes 		mask = S_IFLNK;
9039b50d902SRodney W. Grimes 		break;
9049b50d902SRodney W. Grimes 	case 'p':
9059b50d902SRodney W. Grimes 		mask = S_IFIFO;
9069b50d902SRodney W. Grimes 		break;
9079b50d902SRodney W. Grimes 	case 's':
9089b50d902SRodney W. Grimes 		mask = S_IFSOCK;
9099b50d902SRodney W. Grimes 		break;
910841484cdSPeter Wemm #ifdef FTS_WHITEOUT
911841484cdSPeter Wemm 	case 'w':
912841484cdSPeter Wemm 		mask = S_IFWHT;
913841484cdSPeter Wemm 		ftsoptions |= FTS_WHITEOUT;
914841484cdSPeter Wemm 		break;
915841484cdSPeter Wemm #endif /* FTS_WHITEOUT */
9169b50d902SRodney W. Grimes 	default:
9179b50d902SRodney W. Grimes 		errx(1, "-type: %s: unknown type", typestring);
9189b50d902SRodney W. Grimes 	}
9199b50d902SRodney W. Grimes 
9209b50d902SRodney W. Grimes 	new = palloc(N_TYPE, f_type);
9219b50d902SRodney W. Grimes 	new->m_data = mask;
9229b50d902SRodney W. Grimes 	return (new);
9239b50d902SRodney W. Grimes }
9249b50d902SRodney W. Grimes 
9259b50d902SRodney W. Grimes /*
926abacbbbfSPeter Wemm  * -delete functions --
927abacbbbfSPeter Wemm  *
928abacbbbfSPeter Wemm  *	True always.  Makes it's best shot and continues on regardless.
929abacbbbfSPeter Wemm  */
930abacbbbfSPeter Wemm int
931abacbbbfSPeter Wemm f_delete(plan, entry)
932abacbbbfSPeter Wemm 	PLAN *plan;
933abacbbbfSPeter Wemm 	FTSENT *entry;
934abacbbbfSPeter Wemm {
935abacbbbfSPeter Wemm 	/* ignore these from fts */
936abacbbbfSPeter Wemm 	if (strcmp(entry->fts_accpath, ".") == 0 ||
937abacbbbfSPeter Wemm 	    strcmp(entry->fts_accpath, "..") == 0)
938abacbbbfSPeter Wemm 		return (1);
939abacbbbfSPeter Wemm 
940abacbbbfSPeter Wemm 	/* sanity check */
941abacbbbfSPeter Wemm 	if (isdepth == 0 ||			/* depth off */
942abacbbbfSPeter Wemm 	    (ftsoptions & FTS_NOSTAT) ||	/* not stat()ing */
943abacbbbfSPeter Wemm 	    !(ftsoptions & FTS_PHYSICAL) ||	/* physical off */
944abacbbbfSPeter Wemm 	    (ftsoptions & FTS_LOGICAL))		/* or finally, logical on */
945abacbbbfSPeter Wemm 		errx(1, "-delete: insecure options got turned on");
946abacbbbfSPeter Wemm 
947abacbbbfSPeter Wemm 	/* Potentially unsafe - do not accept relative paths whatsoever */
948abacbbbfSPeter Wemm 	if (strchr(entry->fts_accpath, '/') != NULL)
949abacbbbfSPeter Wemm 		errx(1, "-delete: %s: relative path potentially not safe",
950abacbbbfSPeter Wemm 			entry->fts_accpath);
951abacbbbfSPeter Wemm 
952242ab807SPeter Wemm 	/* Turn off user immutable bits if running as root */
953242ab807SPeter Wemm 	if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
954242ab807SPeter Wemm 	    !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
955242ab807SPeter Wemm 	    geteuid() == 0)
956242ab807SPeter Wemm 		chflags(entry->fts_accpath,
957242ab807SPeter Wemm 		       entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
958242ab807SPeter Wemm 
959abacbbbfSPeter Wemm 	/* rmdir directories, unlink everything else */
960abacbbbfSPeter Wemm 	if (S_ISDIR(entry->fts_statp->st_mode)) {
9613598e52cSPeter Wemm 		if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
962abacbbbfSPeter Wemm 			warn("-delete: rmdir(%s)", entry->fts_path);
963abacbbbfSPeter Wemm 	} else {
964abacbbbfSPeter Wemm 		if (unlink(entry->fts_accpath) < 0)
965abacbbbfSPeter Wemm 			warn("-delete: unlink(%s)", entry->fts_path);
966abacbbbfSPeter Wemm 	}
967abacbbbfSPeter Wemm 
968abacbbbfSPeter Wemm 	/* "succeed" */
969abacbbbfSPeter Wemm 	return (1);
970abacbbbfSPeter Wemm }
971abacbbbfSPeter Wemm 
972abacbbbfSPeter Wemm PLAN *
973abacbbbfSPeter Wemm c_delete()
974abacbbbfSPeter Wemm {
975abacbbbfSPeter Wemm 
976abacbbbfSPeter Wemm 	ftsoptions &= ~FTS_NOSTAT;	/* no optimise */
977abacbbbfSPeter Wemm 	ftsoptions |= FTS_PHYSICAL;	/* disable -follow */
978abacbbbfSPeter Wemm 	ftsoptions &= ~FTS_LOGICAL;	/* disable -follow */
979abacbbbfSPeter Wemm 	isoutput = 1;			/* possible output */
980abacbbbfSPeter Wemm 	isdepth = 1;			/* -depth implied */
981abacbbbfSPeter Wemm 
982abacbbbfSPeter Wemm 	return (palloc(N_DELETE, f_delete));
983abacbbbfSPeter Wemm }
984abacbbbfSPeter Wemm 
985abacbbbfSPeter Wemm /*
9869b50d902SRodney W. Grimes  * -user uname functions --
9879b50d902SRodney W. Grimes  *
9889b50d902SRodney W. Grimes  *	True if the file belongs to the user uname.  If uname is numeric and
9899b50d902SRodney W. Grimes  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
9909b50d902SRodney W. Grimes  *	return a valid user name, uname is taken as a user ID.
9919b50d902SRodney W. Grimes  */
9929b50d902SRodney W. Grimes int
9939b50d902SRodney W. Grimes f_user(plan, entry)
9949b50d902SRodney W. Grimes 	PLAN *plan;
9959b50d902SRodney W. Grimes 	FTSENT *entry;
9969b50d902SRodney W. Grimes {
9979b50d902SRodney W. Grimes 	return (entry->fts_statp->st_uid == plan->u_data);
9989b50d902SRodney W. Grimes }
9999b50d902SRodney W. Grimes 
10009b50d902SRodney W. Grimes PLAN *
10019b50d902SRodney W. Grimes c_user(username)
10029b50d902SRodney W. Grimes 	char *username;
10039b50d902SRodney W. Grimes {
10049b50d902SRodney W. Grimes 	PLAN *new;
10059b50d902SRodney W. Grimes 	struct passwd *p;
10069b50d902SRodney W. Grimes 	uid_t uid;
10079b50d902SRodney W. Grimes 
10089b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
10099b50d902SRodney W. Grimes 
10109b50d902SRodney W. Grimes 	p = getpwnam(username);
10119b50d902SRodney W. Grimes 	if (p == NULL) {
10129b50d902SRodney W. Grimes 		uid = atoi(username);
10139b50d902SRodney W. Grimes 		if (uid == 0 && username[0] != '0')
10149b50d902SRodney W. Grimes 			errx(1, "-user: %s: no such user", username);
10159b50d902SRodney W. Grimes 	} else
10169b50d902SRodney W. Grimes 		uid = p->pw_uid;
10179b50d902SRodney W. Grimes 
10189b50d902SRodney W. Grimes 	new = palloc(N_USER, f_user);
10199b50d902SRodney W. Grimes 	new->u_data = uid;
10209b50d902SRodney W. Grimes 	return (new);
10219b50d902SRodney W. Grimes }
10229b50d902SRodney W. Grimes 
10239b50d902SRodney W. Grimes /*
10249b50d902SRodney W. Grimes  * -xdev functions --
10259b50d902SRodney W. Grimes  *
10269b50d902SRodney W. Grimes  *	Always true, causes find not to decend past directories that have a
10279b50d902SRodney W. Grimes  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
10289b50d902SRodney W. Grimes  */
10299b50d902SRodney W. Grimes PLAN *
10309b50d902SRodney W. Grimes c_xdev()
10319b50d902SRodney W. Grimes {
10329b50d902SRodney W. Grimes 	ftsoptions |= FTS_XDEV;
10339b50d902SRodney W. Grimes 
10349b50d902SRodney W. Grimes 	return (palloc(N_XDEV, f_always_true));
10359b50d902SRodney W. Grimes }
10369b50d902SRodney W. Grimes 
10379b50d902SRodney W. Grimes /*
10389b50d902SRodney W. Grimes  * ( expression ) functions --
10399b50d902SRodney W. Grimes  *
10409b50d902SRodney W. Grimes  *	True if expression is true.
10419b50d902SRodney W. Grimes  */
10429b50d902SRodney W. Grimes int
10439b50d902SRodney W. Grimes f_expr(plan, entry)
10449b50d902SRodney W. Grimes 	PLAN *plan;
10459b50d902SRodney W. Grimes 	FTSENT *entry;
10469b50d902SRodney W. Grimes {
10479b50d902SRodney W. Grimes 	register PLAN *p;
10489b50d902SRodney W. Grimes 	register int state;
10499b50d902SRodney W. Grimes 
10509b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
10519b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
10529b50d902SRodney W. Grimes 	return (state);
10539b50d902SRodney W. Grimes }
10549b50d902SRodney W. Grimes 
10559b50d902SRodney W. Grimes /*
10569b50d902SRodney W. Grimes  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
10579b50d902SRodney W. Grimes  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
10589b50d902SRodney W. Grimes  * to a N_EXPR node containing the expression and the ')' node is discarded.
10599b50d902SRodney W. Grimes  */
10609b50d902SRodney W. Grimes PLAN *
10619b50d902SRodney W. Grimes c_openparen()
10629b50d902SRodney W. Grimes {
10639b50d902SRodney W. Grimes 	return (palloc(N_OPENPAREN, (int (*)())-1));
10649b50d902SRodney W. Grimes }
10659b50d902SRodney W. Grimes 
10669b50d902SRodney W. Grimes PLAN *
10679b50d902SRodney W. Grimes c_closeparen()
10689b50d902SRodney W. Grimes {
10699b50d902SRodney W. Grimes 	return (palloc(N_CLOSEPAREN, (int (*)())-1));
10709b50d902SRodney W. Grimes }
10719b50d902SRodney W. Grimes 
10729b50d902SRodney W. Grimes /*
10739b50d902SRodney W. Grimes  * ! expression functions --
10749b50d902SRodney W. Grimes  *
10759b50d902SRodney W. Grimes  *	Negation of a primary; the unary NOT operator.
10769b50d902SRodney W. Grimes  */
10779b50d902SRodney W. Grimes int
10789b50d902SRodney W. Grimes f_not(plan, entry)
10799b50d902SRodney W. Grimes 	PLAN *plan;
10809b50d902SRodney W. Grimes 	FTSENT *entry;
10819b50d902SRodney W. Grimes {
10829b50d902SRodney W. Grimes 	register PLAN *p;
10839b50d902SRodney W. Grimes 	register int state;
10849b50d902SRodney W. Grimes 
10859b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
10869b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
10879b50d902SRodney W. Grimes 	return (!state);
10889b50d902SRodney W. Grimes }
10899b50d902SRodney W. Grimes 
10909b50d902SRodney W. Grimes PLAN *
10919b50d902SRodney W. Grimes c_not()
10929b50d902SRodney W. Grimes {
10939b50d902SRodney W. Grimes 	return (palloc(N_NOT, f_not));
10949b50d902SRodney W. Grimes }
10959b50d902SRodney W. Grimes 
10969b50d902SRodney W. Grimes /*
10979b50d902SRodney W. Grimes  * expression -o expression functions --
10989b50d902SRodney W. Grimes  *
10999b50d902SRodney W. Grimes  *	Alternation of primaries; the OR operator.  The second expression is
11009b50d902SRodney W. Grimes  * not evaluated if the first expression is true.
11019b50d902SRodney W. Grimes  */
11029b50d902SRodney W. Grimes int
11039b50d902SRodney W. Grimes f_or(plan, entry)
11049b50d902SRodney W. Grimes 	PLAN *plan;
11059b50d902SRodney W. Grimes 	FTSENT *entry;
11069b50d902SRodney W. Grimes {
11079b50d902SRodney W. Grimes 	register PLAN *p;
11089b50d902SRodney W. Grimes 	register int state;
11099b50d902SRodney W. Grimes 
11109b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
11119b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
11129b50d902SRodney W. Grimes 
11139b50d902SRodney W. Grimes 	if (state)
11149b50d902SRodney W. Grimes 		return (1);
11159b50d902SRodney W. Grimes 
11169b50d902SRodney W. Grimes 	for (p = plan->p_data[1];
11179b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
11189b50d902SRodney W. Grimes 	return (state);
11199b50d902SRodney W. Grimes }
11209b50d902SRodney W. Grimes 
11219b50d902SRodney W. Grimes PLAN *
11229b50d902SRodney W. Grimes c_or()
11239b50d902SRodney W. Grimes {
11249b50d902SRodney W. Grimes 	return (palloc(N_OR, f_or));
11259b50d902SRodney W. Grimes }
11269b50d902SRodney W. Grimes 
11279b50d902SRodney W. Grimes static PLAN *
11289b50d902SRodney W. Grimes palloc(t, f)
11299b50d902SRodney W. Grimes 	enum ntype t;
11309b50d902SRodney W. Grimes 	int (*f) __P((PLAN *, FTSENT *));
11319b50d902SRodney W. Grimes {
11329b50d902SRodney W. Grimes 	PLAN *new;
11339b50d902SRodney W. Grimes 
11349b50d902SRodney W. Grimes 	if ((new = malloc(sizeof(PLAN))) == NULL)
11359b50d902SRodney W. Grimes 		err(1, NULL);
11369b50d902SRodney W. Grimes 	new->type = t;
11379b50d902SRodney W. Grimes 	new->eval = f;
11389b50d902SRodney W. Grimes 	new->flags = 0;
11399b50d902SRodney W. Grimes 	new->next = NULL;
11409b50d902SRodney W. Grimes 	return (new);
11419b50d902SRodney W. Grimes }
1142