xref: /freebsd/usr.bin/find/function.c (revision 567664c4a7534c7e998249b59cba10d5395c9086)
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";
39567664c4SOllivier Robert static char rcsid[] = "$FreeBSD$";
409b50d902SRodney W. Grimes #endif /* not lint */
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes #include <sys/param.h>
439b50d902SRodney W. Grimes #include <sys/ucred.h>
449b50d902SRodney W. Grimes #include <sys/stat.h>
459b50d902SRodney W. Grimes #include <sys/wait.h>
469b50d902SRodney W. Grimes #include <sys/mount.h>
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <err.h>
499b50d902SRodney W. Grimes #include <errno.h>
509b50d902SRodney W. Grimes #include <fnmatch.h>
519b50d902SRodney W. Grimes #include <fts.h>
529b50d902SRodney W. Grimes #include <grp.h>
539b50d902SRodney W. Grimes #include <pwd.h>
549b50d902SRodney W. Grimes #include <stdio.h>
559b50d902SRodney W. Grimes #include <stdlib.h>
569b50d902SRodney W. Grimes #include <string.h>
579b50d902SRodney W. Grimes #include <unistd.h>
589b50d902SRodney W. Grimes 
599b50d902SRodney W. Grimes #include "find.h"
609b50d902SRodney W. Grimes 
61567664c4SOllivier Robert int string_to_flags __P((char **, u_long *, u_long *));
62567664c4SOllivier Robert 
639b50d902SRodney W. Grimes #define	COMPARE(a, b) {							\
649b50d902SRodney W. Grimes 	switch (plan->flags) {						\
659b50d902SRodney W. Grimes 	case F_EQUAL:							\
669b50d902SRodney W. Grimes 		return (a == b);					\
679b50d902SRodney W. Grimes 	case F_LESSTHAN:						\
689b50d902SRodney W. Grimes 		return (a < b);						\
699b50d902SRodney W. Grimes 	case F_GREATER:							\
709b50d902SRodney W. Grimes 		return (a > b);						\
719b50d902SRodney W. Grimes 	default:							\
729b50d902SRodney W. Grimes 		abort();						\
739b50d902SRodney W. Grimes 	}								\
749b50d902SRodney W. Grimes }
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
779b50d902SRodney W. Grimes 
789b50d902SRodney W. Grimes /*
799b50d902SRodney W. Grimes  * find_parsenum --
809b50d902SRodney W. Grimes  *	Parse a string of the form [+-]# and return the value.
819b50d902SRodney W. Grimes  */
829192bbf4SBruce Evans static long long
839b50d902SRodney W. Grimes find_parsenum(plan, option, vp, endch)
849b50d902SRodney W. Grimes 	PLAN *plan;
859b50d902SRodney W. Grimes 	char *option, *vp, *endch;
869b50d902SRodney W. Grimes {
879192bbf4SBruce Evans 	long long value;
889b50d902SRodney W. Grimes 	char *endchar, *str;	/* Pointer to character ending conversion. */
899b50d902SRodney W. Grimes 
909b50d902SRodney W. Grimes 	/* Determine comparison from leading + or -. */
919b50d902SRodney W. Grimes 	str = vp;
929b50d902SRodney W. Grimes 	switch (*str) {
939b50d902SRodney W. Grimes 	case '+':
949b50d902SRodney W. Grimes 		++str;
959b50d902SRodney W. Grimes 		plan->flags = F_GREATER;
969b50d902SRodney W. Grimes 		break;
979b50d902SRodney W. Grimes 	case '-':
989b50d902SRodney W. Grimes 		++str;
999b50d902SRodney W. Grimes 		plan->flags = F_LESSTHAN;
1009b50d902SRodney W. Grimes 		break;
1019b50d902SRodney W. Grimes 	default:
1029b50d902SRodney W. Grimes 		plan->flags = F_EQUAL;
1039b50d902SRodney W. Grimes 		break;
1049b50d902SRodney W. Grimes 	}
1059b50d902SRodney W. Grimes 
1069b50d902SRodney W. Grimes 	/*
1079192bbf4SBruce Evans 	 * Convert the string with strtoq().  Note, if strtoq() returns zero
1089b50d902SRodney W. Grimes 	 * and endchar points to the beginning of the string we know we have
1099b50d902SRodney W. Grimes 	 * a syntax error.
1109b50d902SRodney W. Grimes 	 */
1119192bbf4SBruce Evans 	value = strtoq(str, &endchar, 10);
1129b50d902SRodney W. Grimes 	if (value == 0 && endchar == str)
1139b50d902SRodney W. Grimes 		errx(1, "%s: %s: illegal numeric value", option, vp);
1149b50d902SRodney W. Grimes 	if (endchar[0] && (endch == NULL || endchar[0] != *endch))
1159b50d902SRodney W. Grimes 		errx(1, "%s: %s: illegal trailing character", option, vp);
1169b50d902SRodney W. Grimes 	if (endch)
1179b50d902SRodney W. Grimes 		*endch = endchar[0];
1189b50d902SRodney W. Grimes 	return (value);
1199b50d902SRodney W. Grimes }
1209b50d902SRodney W. Grimes 
1219b50d902SRodney W. Grimes /*
1229b50d902SRodney W. Grimes  * The value of n for the inode times (atime, ctime, and mtime) is a range,
1239b50d902SRodney W. Grimes  * i.e. n matches from (n - 1) to n 24 hour periods.  This interacts with
1249b50d902SRodney W. Grimes  * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
1259b50d902SRodney W. Grimes  * user wanted.  Correct so that -1 is "less than 1".
1269b50d902SRodney W. Grimes  */
1279b50d902SRodney W. Grimes #define	TIME_CORRECT(p, ttype)						\
1289b50d902SRodney W. Grimes 	if ((p)->type == ttype && (p)->flags == F_LESSTHAN)		\
1299b50d902SRodney W. Grimes 		++((p)->t_data);
1309b50d902SRodney W. Grimes 
1319b50d902SRodney W. Grimes /*
1323f5223f8SWolfram Schneider  * -amin n functions --
1333f5223f8SWolfram Schneider  *
1343f5223f8SWolfram Schneider  *	True if the difference between the file access time and the
1353f5223f8SWolfram Schneider  *	current time is n min periods.
1363f5223f8SWolfram Schneider  */
1373f5223f8SWolfram Schneider int
1383f5223f8SWolfram Schneider f_amin(plan, entry)
1393f5223f8SWolfram Schneider 	PLAN *plan;
1403f5223f8SWolfram Schneider 	FTSENT *entry;
1413f5223f8SWolfram Schneider {
1423f5223f8SWolfram Schneider 	extern time_t now;
1433f5223f8SWolfram Schneider 
1443f5223f8SWolfram Schneider 	COMPARE((now - entry->fts_statp->st_atime +
1453f5223f8SWolfram Schneider 	    60 - 1) / 60, plan->t_data);
1463f5223f8SWolfram Schneider }
1473f5223f8SWolfram Schneider 
1483f5223f8SWolfram Schneider PLAN *
1493f5223f8SWolfram Schneider c_amin(arg)
1503f5223f8SWolfram Schneider 	char *arg;
1513f5223f8SWolfram Schneider {
1523f5223f8SWolfram Schneider 	PLAN *new;
1533f5223f8SWolfram Schneider 
1543f5223f8SWolfram Schneider 	ftsoptions &= ~FTS_NOSTAT;
1553f5223f8SWolfram Schneider 
1563f5223f8SWolfram Schneider 	new = palloc(N_AMIN, f_amin);
1573f5223f8SWolfram Schneider 	new->t_data = find_parsenum(new, "-amin", arg, NULL);
1583f5223f8SWolfram Schneider 	TIME_CORRECT(new, N_AMIN);
1593f5223f8SWolfram Schneider 	return (new);
1603f5223f8SWolfram Schneider }
1613f5223f8SWolfram Schneider 
1623f5223f8SWolfram Schneider 
1633f5223f8SWolfram Schneider /*
1649b50d902SRodney W. Grimes  * -atime n functions --
1659b50d902SRodney W. Grimes  *
1669b50d902SRodney W. Grimes  *	True if the difference between the file access time and the
1679b50d902SRodney W. Grimes  *	current time is n 24 hour periods.
1689b50d902SRodney W. Grimes  */
1699b50d902SRodney W. Grimes int
1709b50d902SRodney W. Grimes f_atime(plan, entry)
1719b50d902SRodney W. Grimes 	PLAN *plan;
1729b50d902SRodney W. Grimes 	FTSENT *entry;
1739b50d902SRodney W. Grimes {
1749b50d902SRodney W. Grimes 	extern time_t now;
1759b50d902SRodney W. Grimes 
1769b50d902SRodney W. Grimes 	COMPARE((now - entry->fts_statp->st_atime +
177656dcd43SGarrett Wollman 	    86400 - 1) / 86400, plan->t_data);
1789b50d902SRodney W. Grimes }
1799b50d902SRodney W. Grimes 
1809b50d902SRodney W. Grimes PLAN *
1819b50d902SRodney W. Grimes c_atime(arg)
1829b50d902SRodney W. Grimes 	char *arg;
1839b50d902SRodney W. Grimes {
1849b50d902SRodney W. Grimes 	PLAN *new;
1859b50d902SRodney W. Grimes 
1869b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
1879b50d902SRodney W. Grimes 
1889b50d902SRodney W. Grimes 	new = palloc(N_ATIME, f_atime);
1899b50d902SRodney W. Grimes 	new->t_data = find_parsenum(new, "-atime", arg, NULL);
1909b50d902SRodney W. Grimes 	TIME_CORRECT(new, N_ATIME);
1919b50d902SRodney W. Grimes 	return (new);
1929b50d902SRodney W. Grimes }
1933f5223f8SWolfram Schneider 
1943f5223f8SWolfram Schneider 
1953f5223f8SWolfram Schneider /*
1963f5223f8SWolfram Schneider  * -cmin n functions --
1973f5223f8SWolfram Schneider  *
1983f5223f8SWolfram Schneider  *	True if the difference between the last change of file
1993f5223f8SWolfram Schneider  *	status information and the current time is n min periods.
2003f5223f8SWolfram Schneider  */
2013f5223f8SWolfram Schneider int
2023f5223f8SWolfram Schneider f_cmin(plan, entry)
2033f5223f8SWolfram Schneider 	PLAN *plan;
2043f5223f8SWolfram Schneider 	FTSENT *entry;
2053f5223f8SWolfram Schneider {
2063f5223f8SWolfram Schneider 	extern time_t now;
2073f5223f8SWolfram Schneider 
2083f5223f8SWolfram Schneider 	COMPARE((now - entry->fts_statp->st_ctime +
2093f5223f8SWolfram Schneider 	    60 - 1) / 60, plan->t_data);
2103f5223f8SWolfram Schneider }
2113f5223f8SWolfram Schneider 
2123f5223f8SWolfram Schneider PLAN *
2133f5223f8SWolfram Schneider c_cmin(arg)
2143f5223f8SWolfram Schneider 	char *arg;
2153f5223f8SWolfram Schneider {
2163f5223f8SWolfram Schneider 	PLAN *new;
2173f5223f8SWolfram Schneider 
2183f5223f8SWolfram Schneider 	ftsoptions &= ~FTS_NOSTAT;
2193f5223f8SWolfram Schneider 
2203f5223f8SWolfram Schneider 	new = palloc(N_CMIN, f_cmin);
2213f5223f8SWolfram Schneider 	new->t_data = find_parsenum(new, "-cmin", arg, NULL);
2223f5223f8SWolfram Schneider 	TIME_CORRECT(new, N_CMIN);
2233f5223f8SWolfram Schneider 	return (new);
2243f5223f8SWolfram Schneider }
2253f5223f8SWolfram Schneider 
2269b50d902SRodney W. Grimes /*
2279b50d902SRodney W. Grimes  * -ctime n functions --
2289b50d902SRodney W. Grimes  *
2299b50d902SRodney W. Grimes  *	True if the difference between the last change of file
2309b50d902SRodney W. Grimes  *	status information and the current time is n 24 hour periods.
2319b50d902SRodney W. Grimes  */
2329b50d902SRodney W. Grimes int
2339b50d902SRodney W. Grimes f_ctime(plan, entry)
2349b50d902SRodney W. Grimes 	PLAN *plan;
2359b50d902SRodney W. Grimes 	FTSENT *entry;
2369b50d902SRodney W. Grimes {
2379b50d902SRodney W. Grimes 	extern time_t now;
2389b50d902SRodney W. Grimes 
2399b50d902SRodney W. Grimes 	COMPARE((now - entry->fts_statp->st_ctime +
240656dcd43SGarrett Wollman 	    86400 - 1) / 86400, plan->t_data);
2419b50d902SRodney W. Grimes }
2429b50d902SRodney W. Grimes 
2439b50d902SRodney W. Grimes PLAN *
2449b50d902SRodney W. Grimes c_ctime(arg)
2459b50d902SRodney W. Grimes 	char *arg;
2469b50d902SRodney W. Grimes {
2479b50d902SRodney W. Grimes 	PLAN *new;
2489b50d902SRodney W. Grimes 
2499b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
2509b50d902SRodney W. Grimes 
2519b50d902SRodney W. Grimes 	new = palloc(N_CTIME, f_ctime);
2529b50d902SRodney W. Grimes 	new->t_data = find_parsenum(new, "-ctime", arg, NULL);
2539b50d902SRodney W. Grimes 	TIME_CORRECT(new, N_CTIME);
2549b50d902SRodney W. Grimes 	return (new);
2559b50d902SRodney W. Grimes }
2569b50d902SRodney W. Grimes 
2573f5223f8SWolfram Schneider 
2589b50d902SRodney W. Grimes /*
2599b50d902SRodney W. Grimes  * -depth functions --
2609b50d902SRodney W. Grimes  *
2619b50d902SRodney W. Grimes  *	Always true, causes descent of the directory hierarchy to be done
2629b50d902SRodney W. Grimes  *	so that all entries in a directory are acted on before the directory
2639b50d902SRodney W. Grimes  *	itself.
2649b50d902SRodney W. Grimes  */
2659b50d902SRodney W. Grimes int
2669b50d902SRodney W. Grimes f_always_true(plan, entry)
2679b50d902SRodney W. Grimes 	PLAN *plan;
2689b50d902SRodney W. Grimes 	FTSENT *entry;
2699b50d902SRodney W. Grimes {
2709b50d902SRodney W. Grimes 	return (1);
2719b50d902SRodney W. Grimes }
2729b50d902SRodney W. Grimes 
2739b50d902SRodney W. Grimes PLAN *
2749b50d902SRodney W. Grimes c_depth()
2759b50d902SRodney W. Grimes {
2769b50d902SRodney W. Grimes 	isdepth = 1;
2779b50d902SRodney W. Grimes 
2789b50d902SRodney W. Grimes 	return (palloc(N_DEPTH, f_always_true));
2799b50d902SRodney W. Grimes }
2809b50d902SRodney W. Grimes 
2819b50d902SRodney W. Grimes /*
2829b50d902SRodney W. Grimes  * [-exec | -ok] utility [arg ... ] ; functions --
2839b50d902SRodney W. Grimes  *
2849b50d902SRodney W. Grimes  *	True if the executed utility returns a zero value as exit status.
2859b50d902SRodney W. Grimes  *	The end of the primary expression is delimited by a semicolon.  If
2869b50d902SRodney W. Grimes  *	"{}" occurs anywhere, it gets replaced by the current pathname.
2879b50d902SRodney W. Grimes  *	The current directory for the execution of utility is the same as
2889b50d902SRodney W. Grimes  *	the current directory when the find utility was started.
2899b50d902SRodney W. Grimes  *
2909b50d902SRodney W. Grimes  *	The primary -ok is different in that it requests affirmation of the
2919b50d902SRodney W. Grimes  *	user before executing the utility.
2929b50d902SRodney W. Grimes  */
2939b50d902SRodney W. Grimes int
2949b50d902SRodney W. Grimes f_exec(plan, entry)
2959b50d902SRodney W. Grimes 	register PLAN *plan;
2969b50d902SRodney W. Grimes 	FTSENT *entry;
2979b50d902SRodney W. Grimes {
2989b50d902SRodney W. Grimes 	extern int dotfd;
2999b50d902SRodney W. Grimes 	register int cnt;
3009b50d902SRodney W. Grimes 	pid_t pid;
3019b50d902SRodney W. Grimes 	int status;
3029b50d902SRodney W. Grimes 
3039b50d902SRodney W. Grimes 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
3049b50d902SRodney W. Grimes 		if (plan->e_len[cnt])
3059b50d902SRodney W. Grimes 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
3069b50d902SRodney W. Grimes 			    entry->fts_path, plan->e_len[cnt]);
3079b50d902SRodney W. Grimes 
3089b50d902SRodney W. Grimes 	if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
3099b50d902SRodney W. Grimes 		return (0);
3109b50d902SRodney W. Grimes 
311e9f1a293SNate Williams 	/* make sure find output is interspersed correctly with subprocesses */
312e9f1a293SNate Williams 	fflush(stdout);
313e9f1a293SNate Williams 
3141fd98d7dSDag-Erling Smørgrav 	switch (pid = fork()) {
3159b50d902SRodney W. Grimes 	case -1:
3169b50d902SRodney W. Grimes 		err(1, "fork");
3179b50d902SRodney W. Grimes 		/* NOTREACHED */
3189b50d902SRodney W. Grimes 	case 0:
3199b50d902SRodney W. Grimes 		if (fchdir(dotfd)) {
3209b50d902SRodney W. Grimes 			warn("chdir");
3219b50d902SRodney W. Grimes 			_exit(1);
3229b50d902SRodney W. Grimes 		}
3239b50d902SRodney W. Grimes 		execvp(plan->e_argv[0], plan->e_argv);
3249b50d902SRodney W. Grimes 		warn("%s", plan->e_argv[0]);
3259b50d902SRodney W. Grimes 		_exit(1);
3269b50d902SRodney W. Grimes 	}
3279b50d902SRodney W. Grimes 	pid = waitpid(pid, &status, 0);
3289b50d902SRodney W. Grimes 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
3299b50d902SRodney W. Grimes }
3309b50d902SRodney W. Grimes 
3319b50d902SRodney W. Grimes /*
3329b50d902SRodney W. Grimes  * c_exec --
3339b50d902SRodney W. Grimes  *	build three parallel arrays, one with pointers to the strings passed
3349b50d902SRodney W. Grimes  *	on the command line, one with (possibly duplicated) pointers to the
3359b50d902SRodney W. Grimes  *	argv array, and one with integer values that are lengths of the
3369b50d902SRodney W. Grimes  *	strings, but also flags meaning that the string has to be massaged.
3379b50d902SRodney W. Grimes  */
3389b50d902SRodney W. Grimes PLAN *
3399b50d902SRodney W. Grimes c_exec(argvp, isok)
3409b50d902SRodney W. Grimes 	char ***argvp;
3419b50d902SRodney W. Grimes 	int isok;
3429b50d902SRodney W. Grimes {
3439b50d902SRodney W. Grimes 	PLAN *new;			/* node returned */
3449b50d902SRodney W. Grimes 	register int cnt;
3459b50d902SRodney W. Grimes 	register char **argv, **ap, *p;
3469b50d902SRodney W. Grimes 
3479b50d902SRodney W. Grimes 	isoutput = 1;
3489b50d902SRodney W. Grimes 
3499b50d902SRodney W. Grimes 	new = palloc(N_EXEC, f_exec);
3509b50d902SRodney W. Grimes 	if (isok)
3519b50d902SRodney W. Grimes 		new->flags = F_NEEDOK;
3529b50d902SRodney W. Grimes 
3539b50d902SRodney W. Grimes 	for (ap = argv = *argvp;; ++ap) {
3549b50d902SRodney W. Grimes 		if (!*ap)
3559b50d902SRodney W. Grimes 			errx(1,
3569b50d902SRodney W. Grimes 			    "%s: no terminating \";\"", isok ? "-ok" : "-exec");
3579b50d902SRodney W. Grimes 		if (**ap == ';')
3589b50d902SRodney W. Grimes 			break;
3599b50d902SRodney W. Grimes 	}
3609b50d902SRodney W. Grimes 
3619b50d902SRodney W. Grimes 	cnt = ap - *argvp + 1;
3629b50d902SRodney W. Grimes 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
3639b50d902SRodney W. Grimes 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
3649b50d902SRodney W. Grimes 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
3659b50d902SRodney W. Grimes 
3669b50d902SRodney W. Grimes 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
3679b50d902SRodney W. Grimes 		new->e_orig[cnt] = *argv;
3689b50d902SRodney W. Grimes 		for (p = *argv; *p; ++p)
3699b50d902SRodney W. Grimes 			if (p[0] == '{' && p[1] == '}') {
3709b50d902SRodney W. Grimes 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
3719b50d902SRodney W. Grimes 				new->e_len[cnt] = MAXPATHLEN;
3729b50d902SRodney W. Grimes 				break;
3739b50d902SRodney W. Grimes 			}
3749b50d902SRodney W. Grimes 		if (!*p) {
3759b50d902SRodney W. Grimes 			new->e_argv[cnt] = *argv;
3769b50d902SRodney W. Grimes 			new->e_len[cnt] = 0;
3779b50d902SRodney W. Grimes 		}
3789b50d902SRodney W. Grimes 	}
3799b50d902SRodney W. Grimes 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
3809b50d902SRodney W. Grimes 
3819b50d902SRodney W. Grimes 	*argvp = argv + 1;
3829b50d902SRodney W. Grimes 	return (new);
3839b50d902SRodney W. Grimes }
384127d7563SWarner Losh 
385127d7563SWarner Losh /*
386127d7563SWarner Losh  * -execdir utility [arg ... ] ; functions --
387127d7563SWarner Losh  *
388127d7563SWarner Losh  *	True if the executed utility returns a zero value as exit status.
389127d7563SWarner Losh  *	The end of the primary expression is delimited by a semicolon.  If
390127d7563SWarner Losh  *	"{}" occurs anywhere, it gets replaced by the unqualified pathname.
391127d7563SWarner Losh  *	The current directory for the execution of utility is the same as
392127d7563SWarner Losh  *	the directory where the file lives.
393127d7563SWarner Losh  */
394127d7563SWarner Losh int
395127d7563SWarner Losh f_execdir(plan, entry)
396127d7563SWarner Losh 	register PLAN *plan;
397127d7563SWarner Losh 	FTSENT *entry;
398127d7563SWarner Losh {
399127d7563SWarner Losh 	register int cnt;
400127d7563SWarner Losh 	pid_t pid;
401127d7563SWarner Losh 	int status;
402127d7563SWarner Losh 	char *file;
403127d7563SWarner Losh 
404127d7563SWarner Losh 	/* XXX - if file/dir ends in '/' this will not work -- can it? */
405127d7563SWarner Losh 	if ((file = strrchr(entry->fts_path, '/')))
406127d7563SWarner Losh 	    file++;
407127d7563SWarner Losh 	else
408127d7563SWarner Losh 	    file = entry->fts_path;
409127d7563SWarner Losh 
410127d7563SWarner Losh 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
411127d7563SWarner Losh 		if (plan->e_len[cnt])
412127d7563SWarner Losh 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
413127d7563SWarner Losh 			    file, plan->e_len[cnt]);
414127d7563SWarner Losh 
415127d7563SWarner Losh 	/* don't mix output of command with find output */
416127d7563SWarner Losh 	fflush(stdout);
417127d7563SWarner Losh 	fflush(stderr);
418127d7563SWarner Losh 
4191fd98d7dSDag-Erling Smørgrav 	switch (pid = fork()) {
420127d7563SWarner Losh 	case -1:
421127d7563SWarner Losh 		err(1, "fork");
422127d7563SWarner Losh 		/* NOTREACHED */
423127d7563SWarner Losh 	case 0:
424127d7563SWarner Losh 		execvp(plan->e_argv[0], plan->e_argv);
425127d7563SWarner Losh 		warn("%s", plan->e_argv[0]);
426127d7563SWarner Losh 		_exit(1);
427127d7563SWarner Losh 	}
428127d7563SWarner Losh 	pid = waitpid(pid, &status, 0);
429127d7563SWarner Losh 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
430127d7563SWarner Losh }
431127d7563SWarner Losh 
432127d7563SWarner Losh /*
433127d7563SWarner Losh  * c_execdir --
434127d7563SWarner Losh  *	build three parallel arrays, one with pointers to the strings passed
435127d7563SWarner Losh  *	on the command line, one with (possibly duplicated) pointers to the
436127d7563SWarner Losh  *	argv array, and one with integer values that are lengths of the
437127d7563SWarner Losh  *	strings, but also flags meaning that the string has to be massaged.
438127d7563SWarner Losh  */
439127d7563SWarner Losh PLAN *
440127d7563SWarner Losh c_execdir(argvp)
441127d7563SWarner Losh 	char ***argvp;
442127d7563SWarner Losh {
443127d7563SWarner Losh 	PLAN *new;			/* node returned */
444127d7563SWarner Losh 	register int cnt;
445127d7563SWarner Losh 	register char **argv, **ap, *p;
446127d7563SWarner Losh 
447127d7563SWarner Losh 	ftsoptions &= ~FTS_NOSTAT;
448127d7563SWarner Losh 	isoutput = 1;
449127d7563SWarner Losh 
450127d7563SWarner Losh 	new = palloc(N_EXECDIR, f_execdir);
451127d7563SWarner Losh 
452127d7563SWarner Losh 	for (ap = argv = *argvp;; ++ap) {
453127d7563SWarner Losh 		if (!*ap)
454127d7563SWarner Losh 			errx(1,
455127d7563SWarner Losh 			    "-execdir: no terminating \";\"");
456127d7563SWarner Losh 		if (**ap == ';')
457127d7563SWarner Losh 			break;
458127d7563SWarner Losh 	}
459127d7563SWarner Losh 
460127d7563SWarner Losh 	cnt = ap - *argvp + 1;
461127d7563SWarner Losh 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
462127d7563SWarner Losh 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
463127d7563SWarner Losh 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
464127d7563SWarner Losh 
465127d7563SWarner Losh 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
466127d7563SWarner Losh 		new->e_orig[cnt] = *argv;
467127d7563SWarner Losh 		for (p = *argv; *p; ++p)
468127d7563SWarner Losh 			if (p[0] == '{' && p[1] == '}') {
469127d7563SWarner Losh 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
470127d7563SWarner Losh 				new->e_len[cnt] = MAXPATHLEN;
471127d7563SWarner Losh 				break;
472127d7563SWarner Losh 			}
473127d7563SWarner Losh 		if (!*p) {
474127d7563SWarner Losh 			new->e_argv[cnt] = *argv;
475127d7563SWarner Losh 			new->e_len[cnt] = 0;
476127d7563SWarner Losh 		}
477127d7563SWarner Losh 	}
478127d7563SWarner Losh 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
479127d7563SWarner Losh 
480127d7563SWarner Losh 	*argvp = argv + 1;
481127d7563SWarner Losh 	return (new);
482127d7563SWarner Losh }
4839b50d902SRodney W. Grimes 
4849b50d902SRodney W. Grimes /*
4859b50d902SRodney W. Grimes  * -follow functions --
4869b50d902SRodney W. Grimes  *
4879b50d902SRodney W. Grimes  *	Always true, causes symbolic links to be followed on a global
4889b50d902SRodney W. Grimes  *	basis.
4899b50d902SRodney W. Grimes  */
4909b50d902SRodney W. Grimes PLAN *
4919b50d902SRodney W. Grimes c_follow()
4929b50d902SRodney W. Grimes {
4939b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_PHYSICAL;
4949b50d902SRodney W. Grimes 	ftsoptions |= FTS_LOGICAL;
4959b50d902SRodney W. Grimes 
4969b50d902SRodney W. Grimes 	return (palloc(N_FOLLOW, f_always_true));
4979b50d902SRodney W. Grimes }
4989b50d902SRodney W. Grimes 
4999b50d902SRodney W. Grimes /*
5009b50d902SRodney W. Grimes  * -fstype functions --
5019b50d902SRodney W. Grimes  *
5029b50d902SRodney W. Grimes  *	True if the file is of a certain type.
5039b50d902SRodney W. Grimes  */
5049b50d902SRodney W. Grimes int
5059b50d902SRodney W. Grimes f_fstype(plan, entry)
5069b50d902SRodney W. Grimes 	PLAN *plan;
5079b50d902SRodney W. Grimes 	FTSENT *entry;
5089b50d902SRodney W. Grimes {
5099b50d902SRodney W. Grimes 	static dev_t curdev;	/* need a guaranteed illegal dev value */
5109b50d902SRodney W. Grimes 	static int first = 1;
5119b50d902SRodney W. Grimes 	struct statfs sb;
5129d08e419SPeter Wemm 	static int val_type, val_flags;
5139b50d902SRodney W. Grimes 	char *p, save[2];
5149b50d902SRodney W. Grimes 
5159b50d902SRodney W. Grimes 	/* Only check when we cross mount point. */
5169b50d902SRodney W. Grimes 	if (first || curdev != entry->fts_statp->st_dev) {
5179b50d902SRodney W. Grimes 		curdev = entry->fts_statp->st_dev;
5189b50d902SRodney W. Grimes 
5199b50d902SRodney W. Grimes 		/*
5209b50d902SRodney W. Grimes 		 * Statfs follows symlinks; find wants the link's file system,
5219b50d902SRodney W. Grimes 		 * not where it points.
5229b50d902SRodney W. Grimes 		 */
5239b50d902SRodney W. Grimes 		if (entry->fts_info == FTS_SL ||
5249b50d902SRodney W. Grimes 		    entry->fts_info == FTS_SLNONE) {
5259b50d902SRodney W. Grimes 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
5269b50d902SRodney W. Grimes 				++p;
5279b50d902SRodney W. Grimes 			else
5289b50d902SRodney W. Grimes 				p = entry->fts_accpath;
5299b50d902SRodney W. Grimes 			save[0] = p[0];
5309b50d902SRodney W. Grimes 			p[0] = '.';
5319b50d902SRodney W. Grimes 			save[1] = p[1];
5329b50d902SRodney W. Grimes 			p[1] = '\0';
5339b50d902SRodney W. Grimes 
5349b50d902SRodney W. Grimes 		} else
5359b50d902SRodney W. Grimes 			p = NULL;
5369b50d902SRodney W. Grimes 
5379b50d902SRodney W. Grimes 		if (statfs(entry->fts_accpath, &sb))
5389b50d902SRodney W. Grimes 			err(1, "%s", entry->fts_accpath);
5399b50d902SRodney W. Grimes 
5409b50d902SRodney W. Grimes 		if (p) {
5419b50d902SRodney W. Grimes 			p[0] = save[0];
5429b50d902SRodney W. Grimes 			p[1] = save[1];
5439b50d902SRodney W. Grimes 		}
5449b50d902SRodney W. Grimes 
5459b50d902SRodney W. Grimes 		first = 0;
546841484cdSPeter Wemm 
547841484cdSPeter Wemm 		/*
548841484cdSPeter Wemm 		 * Further tests may need both of these values, so
549841484cdSPeter Wemm 		 * always copy both of them.
550841484cdSPeter Wemm 		 */
5519d08e419SPeter Wemm 		val_flags = sb.f_flags;
5529d08e419SPeter Wemm 		val_type = sb.f_type;
5539b50d902SRodney W. Grimes 	}
5549b50d902SRodney W. Grimes 	switch (plan->flags) {
5559b50d902SRodney W. Grimes 	case F_MTFLAG:
5569d08e419SPeter Wemm 		return (val_flags & plan->mt_data) != 0;
5579b50d902SRodney W. Grimes 	case F_MTTYPE:
5589d08e419SPeter Wemm 		return (val_type == plan->mt_data);
5599b50d902SRodney W. Grimes 	default:
5609b50d902SRodney W. Grimes 		abort();
5619b50d902SRodney W. Grimes 	}
5629b50d902SRodney W. Grimes }
5639b50d902SRodney W. Grimes 
56494aacc4fSJohn Birrell #if !defined(__NetBSD__)
5659b50d902SRodney W. Grimes PLAN *
5669b50d902SRodney W. Grimes c_fstype(arg)
5679b50d902SRodney W. Grimes 	char *arg;
5689b50d902SRodney W. Grimes {
5699b50d902SRodney W. Grimes 	register PLAN *new;
570841484cdSPeter Wemm 	struct vfsconf vfc;
5719b50d902SRodney W. Grimes 
5729b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
5739b50d902SRodney W. Grimes 
5749b50d902SRodney W. Grimes 	new = palloc(N_FSTYPE, f_fstype);
575841484cdSPeter Wemm 
576841484cdSPeter Wemm 	/*
577841484cdSPeter Wemm 	 * Check first for a filesystem name.
578841484cdSPeter Wemm 	 */
579841484cdSPeter Wemm 	if (getvfsbyname(arg, &vfc) == 0) {
580841484cdSPeter Wemm 		new->flags = F_MTTYPE;
581841484cdSPeter Wemm 		new->mt_data = vfc.vfc_typenum;
582841484cdSPeter Wemm 		return (new);
583841484cdSPeter Wemm 	}
584841484cdSPeter Wemm 
5859b50d902SRodney W. Grimes 	switch (*arg) {
5869b50d902SRodney W. Grimes 	case 'l':
5879b50d902SRodney W. Grimes 		if (!strcmp(arg, "local")) {
5889b50d902SRodney W. Grimes 			new->flags = F_MTFLAG;
5899b50d902SRodney W. Grimes 			new->mt_data = MNT_LOCAL;
5909b50d902SRodney W. Grimes 			return (new);
5919b50d902SRodney W. Grimes 		}
5929b50d902SRodney W. Grimes 		break;
5939b50d902SRodney W. Grimes 	case 'r':
5949b50d902SRodney W. Grimes 		if (!strcmp(arg, "rdonly")) {
5959b50d902SRodney W. Grimes 			new->flags = F_MTFLAG;
5969b50d902SRodney W. Grimes 			new->mt_data = MNT_RDONLY;
5979b50d902SRodney W. Grimes 			return (new);
5989b50d902SRodney W. Grimes 		}
5999b50d902SRodney W. Grimes 		break;
6009b50d902SRodney W. Grimes 	}
6019b50d902SRodney W. Grimes 	errx(1, "%s: unknown file type", arg);
6029b50d902SRodney W. Grimes 	/* NOTREACHED */
6039b50d902SRodney W. Grimes }
60494aacc4fSJohn Birrell #endif
6059b50d902SRodney W. Grimes 
6069b50d902SRodney W. Grimes /*
6079b50d902SRodney W. Grimes  * -group gname functions --
6089b50d902SRodney W. Grimes  *
6099b50d902SRodney W. Grimes  *	True if the file belongs to the group gname.  If gname is numeric and
6109b50d902SRodney W. Grimes  *	an equivalent of the getgrnam() function does not return a valid group
6119b50d902SRodney W. Grimes  *	name, gname is taken as a group ID.
6129b50d902SRodney W. Grimes  */
6139b50d902SRodney W. Grimes int
6149b50d902SRodney W. Grimes f_group(plan, entry)
6159b50d902SRodney W. Grimes 	PLAN *plan;
6169b50d902SRodney W. Grimes 	FTSENT *entry;
6179b50d902SRodney W. Grimes {
6189b50d902SRodney W. Grimes 	return (entry->fts_statp->st_gid == plan->g_data);
6199b50d902SRodney W. Grimes }
6209b50d902SRodney W. Grimes 
6219b50d902SRodney W. Grimes PLAN *
6229b50d902SRodney W. Grimes c_group(gname)
6239b50d902SRodney W. Grimes 	char *gname;
6249b50d902SRodney W. Grimes {
6259b50d902SRodney W. Grimes 	PLAN *new;
6269b50d902SRodney W. Grimes 	struct group *g;
6279b50d902SRodney W. Grimes 	gid_t gid;
6289b50d902SRodney W. Grimes 
6299b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
6309b50d902SRodney W. Grimes 
6319b50d902SRodney W. Grimes 	g = getgrnam(gname);
6329b50d902SRodney W. Grimes 	if (g == NULL) {
6339b50d902SRodney W. Grimes 		gid = atoi(gname);
6349b50d902SRodney W. Grimes 		if (gid == 0 && gname[0] != '0')
6359b50d902SRodney W. Grimes 			errx(1, "-group: %s: no such group", gname);
6369b50d902SRodney W. Grimes 	} else
6379b50d902SRodney W. Grimes 		gid = g->gr_gid;
6389b50d902SRodney W. Grimes 
6399b50d902SRodney W. Grimes 	new = palloc(N_GROUP, f_group);
6409b50d902SRodney W. Grimes 	new->g_data = gid;
6419b50d902SRodney W. Grimes 	return (new);
6429b50d902SRodney W. Grimes }
6439b50d902SRodney W. Grimes 
6449b50d902SRodney W. Grimes /*
6459b50d902SRodney W. Grimes  * -inum n functions --
6469b50d902SRodney W. Grimes  *
6479b50d902SRodney W. Grimes  *	True if the file has inode # n.
6489b50d902SRodney W. Grimes  */
6499b50d902SRodney W. Grimes int
6509b50d902SRodney W. Grimes f_inum(plan, entry)
6519b50d902SRodney W. Grimes 	PLAN *plan;
6529b50d902SRodney W. Grimes 	FTSENT *entry;
6539b50d902SRodney W. Grimes {
6549b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
6559b50d902SRodney W. Grimes }
6569b50d902SRodney W. Grimes 
6579b50d902SRodney W. Grimes PLAN *
6589b50d902SRodney W. Grimes c_inum(arg)
6599b50d902SRodney W. Grimes 	char *arg;
6609b50d902SRodney W. Grimes {
6619b50d902SRodney W. Grimes 	PLAN *new;
6629b50d902SRodney W. Grimes 
6639b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
6649b50d902SRodney W. Grimes 
6659b50d902SRodney W. Grimes 	new = palloc(N_INUM, f_inum);
6669b50d902SRodney W. Grimes 	new->i_data = find_parsenum(new, "-inum", arg, NULL);
6679b50d902SRodney W. Grimes 	return (new);
6689b50d902SRodney W. Grimes }
6699b50d902SRodney W. Grimes 
6709b50d902SRodney W. Grimes /*
6719b50d902SRodney W. Grimes  * -links n functions --
6729b50d902SRodney W. Grimes  *
6739b50d902SRodney W. Grimes  *	True if the file has n links.
6749b50d902SRodney W. Grimes  */
6759b50d902SRodney W. Grimes int
6769b50d902SRodney W. Grimes f_links(plan, entry)
6779b50d902SRodney W. Grimes 	PLAN *plan;
6789b50d902SRodney W. Grimes 	FTSENT *entry;
6799b50d902SRodney W. Grimes {
6809b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
6819b50d902SRodney W. Grimes }
6829b50d902SRodney W. Grimes 
6839b50d902SRodney W. Grimes PLAN *
6849b50d902SRodney W. Grimes c_links(arg)
6859b50d902SRodney W. Grimes 	char *arg;
6869b50d902SRodney W. Grimes {
6879b50d902SRodney W. Grimes 	PLAN *new;
6889b50d902SRodney W. Grimes 
6899b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
6909b50d902SRodney W. Grimes 
6919b50d902SRodney W. Grimes 	new = palloc(N_LINKS, f_links);
6929b50d902SRodney W. Grimes 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
6939b50d902SRodney W. Grimes 	return (new);
6949b50d902SRodney W. Grimes }
6959b50d902SRodney W. Grimes 
6969b50d902SRodney W. Grimes /*
6979b50d902SRodney W. Grimes  * -ls functions --
6989b50d902SRodney W. Grimes  *
6999b50d902SRodney W. Grimes  *	Always true - prints the current entry to stdout in "ls" format.
7009b50d902SRodney W. Grimes  */
7019b50d902SRodney W. Grimes int
7029b50d902SRodney W. Grimes f_ls(plan, entry)
7039b50d902SRodney W. Grimes 	PLAN *plan;
7049b50d902SRodney W. Grimes 	FTSENT *entry;
7059b50d902SRodney W. Grimes {
7069b50d902SRodney W. Grimes 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
7079b50d902SRodney W. Grimes 	return (1);
7089b50d902SRodney W. Grimes }
7099b50d902SRodney W. Grimes 
7109b50d902SRodney W. Grimes PLAN *
7119b50d902SRodney W. Grimes c_ls()
7129b50d902SRodney W. Grimes {
7139b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
7149b50d902SRodney W. Grimes 	isoutput = 1;
7159b50d902SRodney W. Grimes 
7169b50d902SRodney W. Grimes 	return (palloc(N_LS, f_ls));
7179b50d902SRodney W. Grimes }
7189b50d902SRodney W. Grimes 
7199b50d902SRodney W. Grimes /*
7209b50d902SRodney W. Grimes  * -mtime n functions --
7219b50d902SRodney W. Grimes  *
7229b50d902SRodney W. Grimes  *	True if the difference between the file modification time and the
7239b50d902SRodney W. Grimes  *	current time is n 24 hour periods.
7249b50d902SRodney W. Grimes  */
7259b50d902SRodney W. Grimes int
7269b50d902SRodney W. Grimes f_mtime(plan, entry)
7279b50d902SRodney W. Grimes 	PLAN *plan;
7289b50d902SRodney W. Grimes 	FTSENT *entry;
7299b50d902SRodney W. Grimes {
7309b50d902SRodney W. Grimes 	extern time_t now;
7319b50d902SRodney W. Grimes 
732656dcd43SGarrett Wollman 	COMPARE((now - entry->fts_statp->st_mtime + 86400 - 1) /
733656dcd43SGarrett Wollman 	    86400, plan->t_data);
7349b50d902SRodney W. Grimes }
7359b50d902SRodney W. Grimes 
7369b50d902SRodney W. Grimes PLAN *
7379b50d902SRodney W. Grimes c_mtime(arg)
7389b50d902SRodney W. Grimes 	char *arg;
7399b50d902SRodney W. Grimes {
7409b50d902SRodney W. Grimes 	PLAN *new;
7419b50d902SRodney W. Grimes 
7429b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
7439b50d902SRodney W. Grimes 
7449b50d902SRodney W. Grimes 	new = palloc(N_MTIME, f_mtime);
7459b50d902SRodney W. Grimes 	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
7469b50d902SRodney W. Grimes 	TIME_CORRECT(new, N_MTIME);
7479b50d902SRodney W. Grimes 	return (new);
7489b50d902SRodney W. Grimes }
7499b50d902SRodney W. Grimes 
7509b50d902SRodney W. Grimes /*
7513f5223f8SWolfram Schneider  * -mmin n functions --
7523f5223f8SWolfram Schneider  *
7533f5223f8SWolfram Schneider  *	True if the difference between the file modification time and the
7543f5223f8SWolfram Schneider  *	current time is n min periods.
7553f5223f8SWolfram Schneider  */
7563f5223f8SWolfram Schneider int
7573f5223f8SWolfram Schneider f_mmin(plan, entry)
7583f5223f8SWolfram Schneider 	PLAN *plan;
7593f5223f8SWolfram Schneider 	FTSENT *entry;
7603f5223f8SWolfram Schneider {
7613f5223f8SWolfram Schneider 	extern time_t now;
7623f5223f8SWolfram Schneider 
7633f5223f8SWolfram Schneider 	COMPARE((now - entry->fts_statp->st_mtime + 60 - 1) /
7643f5223f8SWolfram Schneider 	    60, plan->t_data);
7653f5223f8SWolfram Schneider }
7663f5223f8SWolfram Schneider 
7673f5223f8SWolfram Schneider PLAN *
7683f5223f8SWolfram Schneider c_mmin(arg)
7693f5223f8SWolfram Schneider 	char *arg;
7703f5223f8SWolfram Schneider {
7713f5223f8SWolfram Schneider 	PLAN *new;
7723f5223f8SWolfram Schneider 
7733f5223f8SWolfram Schneider 	ftsoptions &= ~FTS_NOSTAT;
7743f5223f8SWolfram Schneider 
7753f5223f8SWolfram Schneider 	new = palloc(N_MMIN, f_mmin);
7763f5223f8SWolfram Schneider 	new->t_data = find_parsenum(new, "-mmin", arg, NULL);
7773f5223f8SWolfram Schneider 	TIME_CORRECT(new, N_MMIN);
7783f5223f8SWolfram Schneider 	return (new);
7793f5223f8SWolfram Schneider }
7803f5223f8SWolfram Schneider 
7813f5223f8SWolfram Schneider 
7823f5223f8SWolfram Schneider /*
7839b50d902SRodney W. Grimes  * -name functions --
7849b50d902SRodney W. Grimes  *
7859b50d902SRodney W. Grimes  *	True if the basename of the filename being examined
7869b50d902SRodney W. Grimes  *	matches pattern using Pattern Matching Notation S3.14
7879b50d902SRodney W. Grimes  */
7889b50d902SRodney W. Grimes int
7899b50d902SRodney W. Grimes f_name(plan, entry)
7909b50d902SRodney W. Grimes 	PLAN *plan;
7919b50d902SRodney W. Grimes 	FTSENT *entry;
7929b50d902SRodney W. Grimes {
7939b50d902SRodney W. Grimes 	return (!fnmatch(plan->c_data, entry->fts_name, 0));
7949b50d902SRodney W. Grimes }
7959b50d902SRodney W. Grimes 
7969b50d902SRodney W. Grimes PLAN *
7979b50d902SRodney W. Grimes c_name(pattern)
7989b50d902SRodney W. Grimes 	char *pattern;
7999b50d902SRodney W. Grimes {
8009b50d902SRodney W. Grimes 	PLAN *new;
8019b50d902SRodney W. Grimes 
8029b50d902SRodney W. Grimes 	new = palloc(N_NAME, f_name);
8039b50d902SRodney W. Grimes 	new->c_data = pattern;
8049b50d902SRodney W. Grimes 	return (new);
8059b50d902SRodney W. Grimes }
8069b50d902SRodney W. Grimes 
8079b50d902SRodney W. Grimes /*
8089b50d902SRodney W. Grimes  * -newer file functions --
8099b50d902SRodney W. Grimes  *
8109b50d902SRodney W. Grimes  *	True if the current file has been modified more recently
8119b50d902SRodney W. Grimes  *	then the modification time of the file named by the pathname
8129b50d902SRodney W. Grimes  *	file.
8139b50d902SRodney W. Grimes  */
8149b50d902SRodney W. Grimes int
8159b50d902SRodney W. Grimes f_newer(plan, entry)
8169b50d902SRodney W. Grimes 	PLAN *plan;
8179b50d902SRodney W. Grimes 	FTSENT *entry;
8189b50d902SRodney W. Grimes {
8199b50d902SRodney W. Grimes 	return (entry->fts_statp->st_mtime > plan->t_data);
8209b50d902SRodney W. Grimes }
8219b50d902SRodney W. Grimes 
8229b50d902SRodney W. Grimes PLAN *
8239b50d902SRodney W. Grimes c_newer(filename)
8249b50d902SRodney W. Grimes 	char *filename;
8259b50d902SRodney W. Grimes {
8269b50d902SRodney W. Grimes 	PLAN *new;
8279b50d902SRodney W. Grimes 	struct stat sb;
8289b50d902SRodney W. Grimes 
8299b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
8309b50d902SRodney W. Grimes 
8319b50d902SRodney W. Grimes 	if (stat(filename, &sb))
8329b50d902SRodney W. Grimes 		err(1, "%s", filename);
8339b50d902SRodney W. Grimes 	new = palloc(N_NEWER, f_newer);
8349b50d902SRodney W. Grimes 	new->t_data = sb.st_mtime;
8359b50d902SRodney W. Grimes 	return (new);
8369b50d902SRodney W. Grimes }
8379b50d902SRodney W. Grimes 
8389b50d902SRodney W. Grimes /*
8399b50d902SRodney W. Grimes  * -nogroup functions --
8409b50d902SRodney W. Grimes  *
8419b50d902SRodney W. Grimes  *	True if file belongs to a user ID for which the equivalent
8429b50d902SRodney W. Grimes  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
8439b50d902SRodney W. Grimes  */
8449b50d902SRodney W. Grimes int
8459b50d902SRodney W. Grimes f_nogroup(plan, entry)
8469b50d902SRodney W. Grimes 	PLAN *plan;
8479b50d902SRodney W. Grimes 	FTSENT *entry;
8489b50d902SRodney W. Grimes {
8499b50d902SRodney W. Grimes 	char *group_from_gid();
8509b50d902SRodney W. Grimes 
8519b50d902SRodney W. Grimes 	return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
8529b50d902SRodney W. Grimes }
8539b50d902SRodney W. Grimes 
8549b50d902SRodney W. Grimes PLAN *
8559b50d902SRodney W. Grimes c_nogroup()
8569b50d902SRodney W. Grimes {
8579b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
8589b50d902SRodney W. Grimes 
8599b50d902SRodney W. Grimes 	return (palloc(N_NOGROUP, f_nogroup));
8609b50d902SRodney W. Grimes }
8619b50d902SRodney W. Grimes 
8629b50d902SRodney W. Grimes /*
8639b50d902SRodney W. Grimes  * -nouser functions --
8649b50d902SRodney W. Grimes  *
8659b50d902SRodney W. Grimes  *	True if file belongs to a user ID for which the equivalent
8669b50d902SRodney W. Grimes  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
8679b50d902SRodney W. Grimes  */
8689b50d902SRodney W. Grimes int
8699b50d902SRodney W. Grimes f_nouser(plan, entry)
8709b50d902SRodney W. Grimes 	PLAN *plan;
8719b50d902SRodney W. Grimes 	FTSENT *entry;
8729b50d902SRodney W. Grimes {
8739b50d902SRodney W. Grimes 	char *user_from_uid();
8749b50d902SRodney W. Grimes 
8759b50d902SRodney W. Grimes 	return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
8769b50d902SRodney W. Grimes }
8779b50d902SRodney W. Grimes 
8789b50d902SRodney W. Grimes PLAN *
8799b50d902SRodney W. Grimes c_nouser()
8809b50d902SRodney W. Grimes {
8819b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
8829b50d902SRodney W. Grimes 
8839b50d902SRodney W. Grimes 	return (palloc(N_NOUSER, f_nouser));
8849b50d902SRodney W. Grimes }
8859b50d902SRodney W. Grimes 
8869b50d902SRodney W. Grimes /*
8879b50d902SRodney W. Grimes  * -path functions --
8889b50d902SRodney W. Grimes  *
8899b50d902SRodney W. Grimes  *	True if the path of the filename being examined
8909b50d902SRodney W. Grimes  *	matches pattern using Pattern Matching Notation S3.14
8919b50d902SRodney W. Grimes  */
8929b50d902SRodney W. Grimes int
8939b50d902SRodney W. Grimes f_path(plan, entry)
8949b50d902SRodney W. Grimes 	PLAN *plan;
8959b50d902SRodney W. Grimes 	FTSENT *entry;
8969b50d902SRodney W. Grimes {
8979b50d902SRodney W. Grimes 	return (!fnmatch(plan->c_data, entry->fts_path, 0));
8989b50d902SRodney W. Grimes }
8999b50d902SRodney W. Grimes 
9009b50d902SRodney W. Grimes PLAN *
9019b50d902SRodney W. Grimes c_path(pattern)
9029b50d902SRodney W. Grimes 	char *pattern;
9039b50d902SRodney W. Grimes {
9049b50d902SRodney W. Grimes 	PLAN *new;
9059b50d902SRodney W. Grimes 
9069b50d902SRodney W. Grimes 	new = palloc(N_NAME, f_path);
9079b50d902SRodney W. Grimes 	new->c_data = pattern;
9089b50d902SRodney W. Grimes 	return (new);
9099b50d902SRodney W. Grimes }
9109b50d902SRodney W. Grimes 
9119b50d902SRodney W. Grimes /*
9129b50d902SRodney W. Grimes  * -perm functions --
9139b50d902SRodney W. Grimes  *
9149b50d902SRodney W. Grimes  *	The mode argument is used to represent file mode bits.  If it starts
9159b50d902SRodney W. Grimes  *	with a leading digit, it's treated as an octal mode, otherwise as a
9169b50d902SRodney W. Grimes  *	symbolic mode.
9179b50d902SRodney W. Grimes  */
9189b50d902SRodney W. Grimes int
9199b50d902SRodney W. Grimes f_perm(plan, entry)
9209b50d902SRodney W. Grimes 	PLAN *plan;
9219b50d902SRodney W. Grimes 	FTSENT *entry;
9229b50d902SRodney W. Grimes {
9239b50d902SRodney W. Grimes 	mode_t mode;
9249b50d902SRodney W. Grimes 
9259b50d902SRodney W. Grimes 	mode = entry->fts_statp->st_mode &
9269b50d902SRodney W. Grimes 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
9279b50d902SRodney W. Grimes 	if (plan->flags == F_ATLEAST)
9289b50d902SRodney W. Grimes 		return ((plan->m_data | mode) == mode);
9299b50d902SRodney W. Grimes 	else
9309b50d902SRodney W. Grimes 		return (mode == plan->m_data);
9319b50d902SRodney W. Grimes 	/* NOTREACHED */
9329b50d902SRodney W. Grimes }
9339b50d902SRodney W. Grimes 
9349b50d902SRodney W. Grimes PLAN *
9359b50d902SRodney W. Grimes c_perm(perm)
9369b50d902SRodney W. Grimes 	char *perm;
9379b50d902SRodney W. Grimes {
9389b50d902SRodney W. Grimes 	PLAN *new;
9399b50d902SRodney W. Grimes 	mode_t *set;
9409b50d902SRodney W. Grimes 
9419b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
9429b50d902SRodney W. Grimes 
9439b50d902SRodney W. Grimes 	new = palloc(N_PERM, f_perm);
9449b50d902SRodney W. Grimes 
9459b50d902SRodney W. Grimes 	if (*perm == '-') {
9469b50d902SRodney W. Grimes 		new->flags = F_ATLEAST;
9479b50d902SRodney W. Grimes 		++perm;
9489b50d902SRodney W. Grimes 	}
9499b50d902SRodney W. Grimes 
9509b50d902SRodney W. Grimes 	if ((set = setmode(perm)) == NULL)
951567664c4SOllivier Robert 		errx(1, "-perm: %s: illegal mode string", perm);
9529b50d902SRodney W. Grimes 
9539b50d902SRodney W. Grimes 	new->m_data = getmode(set, 0);
95483268d4dSWarner Losh 	free(set);
9559b50d902SRodney W. Grimes 	return (new);
9569b50d902SRodney W. Grimes }
9579b50d902SRodney W. Grimes 
9589b50d902SRodney W. Grimes /*
959567664c4SOllivier Robert  * -flags functions --
960567664c4SOllivier Robert  *
961567664c4SOllivier Robert  *	The flags argument is used to represent file flags bits.
962567664c4SOllivier Robert  */
963567664c4SOllivier Robert int
964567664c4SOllivier Robert f_flags(plan, entry)
965567664c4SOllivier Robert 	PLAN *plan;
966567664c4SOllivier Robert 	FTSENT *entry;
967567664c4SOllivier Robert {
968567664c4SOllivier Robert 	u_long flags;
969567664c4SOllivier Robert 
970567664c4SOllivier Robert 	flags = entry->fts_statp->st_flags &
971567664c4SOllivier Robert 	    (UF_NODUMP | UF_IMMUTABLE | UF_APPEND | UF_OPAQUE |
972567664c4SOllivier Robert 	     SF_ARCHIVED | SF_IMMUTABLE | SF_APPEND);
973567664c4SOllivier Robert 	if (plan->flags == F_ATLEAST)
974567664c4SOllivier Robert 		/* note that plan->fl_flags always is a subset of
975567664c4SOllivier Robert 		   plan->fl_mask */
976567664c4SOllivier Robert 		return (flags & plan->fl_mask) == plan->fl_flags;
977567664c4SOllivier Robert 	else
978567664c4SOllivier Robert 		return flags == plan->fl_flags;
979567664c4SOllivier Robert 	/* NOTREACHED */
980567664c4SOllivier Robert }
981567664c4SOllivier Robert 
982567664c4SOllivier Robert PLAN *
983567664c4SOllivier Robert c_flags(flags_str)
984567664c4SOllivier Robert 	char *flags_str;
985567664c4SOllivier Robert {
986567664c4SOllivier Robert 	PLAN *new;
987567664c4SOllivier Robert 	u_long flags, notflags;
988567664c4SOllivier Robert 
989567664c4SOllivier Robert 	ftsoptions &= ~FTS_NOSTAT;
990567664c4SOllivier Robert 
991567664c4SOllivier Robert 	new = palloc(N_FLAGS, f_flags);
992567664c4SOllivier Robert 
993567664c4SOllivier Robert 	if (*flags_str == '-') {
994567664c4SOllivier Robert 		new->flags = F_ATLEAST;
995567664c4SOllivier Robert 		flags_str++;
996567664c4SOllivier Robert 	}
997567664c4SOllivier Robert 	if (string_to_flags(&flags_str, &flags, &notflags) == 1)
998567664c4SOllivier Robert 		errx(1, "-flags: %s: illegal flags string", flags_str);
999567664c4SOllivier Robert 
1000567664c4SOllivier Robert 	new->fl_flags = flags;
1001567664c4SOllivier Robert 	new->fl_mask = flags | notflags;
1002567664c4SOllivier Robert #if 0
1003567664c4SOllivier Robert 	printf("flags = %08x, mask = %08x (%08x, %08x)\n",
1004567664c4SOllivier Robert 		new->fl_flags, new->fl_mask, flags, notflags);
1005567664c4SOllivier Robert #endif
1006567664c4SOllivier Robert 	return new;
1007567664c4SOllivier Robert }
1008567664c4SOllivier Robert 
1009567664c4SOllivier Robert   /*
1010567664c4SOllivier Robert 
1011567664c4SOllivier Robert 
1012567664c4SOllivier Robert /*
10139b50d902SRodney W. Grimes  * -print functions --
10149b50d902SRodney W. Grimes  *
10159b50d902SRodney W. Grimes  *	Always true, causes the current pathame to be written to
10169b50d902SRodney W. Grimes  *	standard output.
10179b50d902SRodney W. Grimes  */
10189b50d902SRodney W. Grimes int
10199b50d902SRodney W. Grimes f_print(plan, entry)
10209b50d902SRodney W. Grimes 	PLAN *plan;
10219b50d902SRodney W. Grimes 	FTSENT *entry;
10229b50d902SRodney W. Grimes {
102381e236a0SGarrett Wollman 	(void)puts(entry->fts_path);
10249b50d902SRodney W. Grimes 	return (1);
10259b50d902SRodney W. Grimes }
10269b50d902SRodney W. Grimes 
10279b50d902SRodney W. Grimes PLAN *
10289b50d902SRodney W. Grimes c_print()
10299b50d902SRodney W. Grimes {
10309b50d902SRodney W. Grimes 	isoutput = 1;
10319b50d902SRodney W. Grimes 
10329b50d902SRodney W. Grimes 	return (palloc(N_PRINT, f_print));
10339b50d902SRodney W. Grimes }
10349b50d902SRodney W. Grimes 
10359b50d902SRodney W. Grimes /*
10367cd23434SGarrett Wollman  * -print0 functions --
10377cd23434SGarrett Wollman  *
10387cd23434SGarrett Wollman  *	Always true, causes the current pathame to be written to
10397cd23434SGarrett Wollman  *	standard output followed by a NUL character
10407cd23434SGarrett Wollman  */
10417cd23434SGarrett Wollman int
10427cd23434SGarrett Wollman f_print0(plan, entry)
10437cd23434SGarrett Wollman 	PLAN *plan;
10447cd23434SGarrett Wollman 	FTSENT *entry;
10457cd23434SGarrett Wollman {
10467cd23434SGarrett Wollman 	fputs(entry->fts_path, stdout);
10477cd23434SGarrett Wollman 	fputc('\0', stdout);
10487cd23434SGarrett Wollman 	return (1);
10497cd23434SGarrett Wollman }
10507cd23434SGarrett Wollman 
10517cd23434SGarrett Wollman PLAN *
10527cd23434SGarrett Wollman c_print0()
10537cd23434SGarrett Wollman {
10547cd23434SGarrett Wollman 	isoutput = 1;
10557cd23434SGarrett Wollman 
10567cd23434SGarrett Wollman 	return (palloc(N_PRINT0, f_print0));
10577cd23434SGarrett Wollman }
10587cd23434SGarrett Wollman 
10597cd23434SGarrett Wollman /*
10609b50d902SRodney W. Grimes  * -prune functions --
10619b50d902SRodney W. Grimes  *
10629b50d902SRodney W. Grimes  *	Prune a portion of the hierarchy.
10639b50d902SRodney W. Grimes  */
10649b50d902SRodney W. Grimes int
10659b50d902SRodney W. Grimes f_prune(plan, entry)
10669b50d902SRodney W. Grimes 	PLAN *plan;
10679b50d902SRodney W. Grimes 	FTSENT *entry;
10689b50d902SRodney W. Grimes {
10699b50d902SRodney W. Grimes 	extern FTS *tree;
10709b50d902SRodney W. Grimes 
10719b50d902SRodney W. Grimes 	if (fts_set(tree, entry, FTS_SKIP))
10729b50d902SRodney W. Grimes 		err(1, "%s", entry->fts_path);
10739b50d902SRodney W. Grimes 	return (1);
10749b50d902SRodney W. Grimes }
10759b50d902SRodney W. Grimes 
10769b50d902SRodney W. Grimes PLAN *
10779b50d902SRodney W. Grimes c_prune()
10789b50d902SRodney W. Grimes {
10799b50d902SRodney W. Grimes 	return (palloc(N_PRUNE, f_prune));
10809b50d902SRodney W. Grimes }
10819b50d902SRodney W. Grimes 
10829b50d902SRodney W. Grimes /*
10839b50d902SRodney W. Grimes  * -size n[c] functions --
10849b50d902SRodney W. Grimes  *
10859b50d902SRodney W. Grimes  *	True if the file size in bytes, divided by an implementation defined
10869b50d902SRodney W. Grimes  *	value and rounded up to the next integer, is n.  If n is followed by
10879b50d902SRodney W. Grimes  *	a c, the size is in bytes.
10889b50d902SRodney W. Grimes  */
10899b50d902SRodney W. Grimes #define	FIND_SIZE	512
10909b50d902SRodney W. Grimes static int divsize = 1;
10919b50d902SRodney W. Grimes 
10929b50d902SRodney W. Grimes int
10939b50d902SRodney W. Grimes f_size(plan, entry)
10949b50d902SRodney W. Grimes 	PLAN *plan;
10959b50d902SRodney W. Grimes 	FTSENT *entry;
10969b50d902SRodney W. Grimes {
10979b50d902SRodney W. Grimes 	off_t size;
10989b50d902SRodney W. Grimes 
10999b50d902SRodney W. Grimes 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
11009b50d902SRodney W. Grimes 	    FIND_SIZE : entry->fts_statp->st_size;
11019b50d902SRodney W. Grimes 	COMPARE(size, plan->o_data);
11029b50d902SRodney W. Grimes }
11039b50d902SRodney W. Grimes 
11049b50d902SRodney W. Grimes PLAN *
11059b50d902SRodney W. Grimes c_size(arg)
11069b50d902SRodney W. Grimes 	char *arg;
11079b50d902SRodney W. Grimes {
11089b50d902SRodney W. Grimes 	PLAN *new;
11099b50d902SRodney W. Grimes 	char endch;
11109b50d902SRodney W. Grimes 
11119b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
11129b50d902SRodney W. Grimes 
11139b50d902SRodney W. Grimes 	new = palloc(N_SIZE, f_size);
11149b50d902SRodney W. Grimes 	endch = 'c';
11159b50d902SRodney W. Grimes 	new->o_data = find_parsenum(new, "-size", arg, &endch);
11169b50d902SRodney W. Grimes 	if (endch == 'c')
11179b50d902SRodney W. Grimes 		divsize = 0;
11189b50d902SRodney W. Grimes 	return (new);
11199b50d902SRodney W. Grimes }
11209b50d902SRodney W. Grimes 
11219b50d902SRodney W. Grimes /*
11229b50d902SRodney W. Grimes  * -type c functions --
11239b50d902SRodney W. Grimes  *
1124841484cdSPeter Wemm  *	True if the type of the file is c, where c is b, c, d, p, f or w
1125841484cdSPeter Wemm  *	for block special file, character special file, directory, FIFO,
1126841484cdSPeter Wemm  *	regular file or whiteout respectively.
11279b50d902SRodney W. Grimes  */
11289b50d902SRodney W. Grimes int
11299b50d902SRodney W. Grimes f_type(plan, entry)
11309b50d902SRodney W. Grimes 	PLAN *plan;
11319b50d902SRodney W. Grimes 	FTSENT *entry;
11329b50d902SRodney W. Grimes {
11339b50d902SRodney W. Grimes 	return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
11349b50d902SRodney W. Grimes }
11359b50d902SRodney W. Grimes 
11369b50d902SRodney W. Grimes PLAN *
11379b50d902SRodney W. Grimes c_type(typestring)
11389b50d902SRodney W. Grimes 	char *typestring;
11399b50d902SRodney W. Grimes {
11409b50d902SRodney W. Grimes 	PLAN *new;
11419b50d902SRodney W. Grimes 	mode_t  mask;
11429b50d902SRodney W. Grimes 
11439b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
11449b50d902SRodney W. Grimes 
11459b50d902SRodney W. Grimes 	switch (typestring[0]) {
11469b50d902SRodney W. Grimes 	case 'b':
11479b50d902SRodney W. Grimes 		mask = S_IFBLK;
11489b50d902SRodney W. Grimes 		break;
11499b50d902SRodney W. Grimes 	case 'c':
11509b50d902SRodney W. Grimes 		mask = S_IFCHR;
11519b50d902SRodney W. Grimes 		break;
11529b50d902SRodney W. Grimes 	case 'd':
11539b50d902SRodney W. Grimes 		mask = S_IFDIR;
11549b50d902SRodney W. Grimes 		break;
11559b50d902SRodney W. Grimes 	case 'f':
11569b50d902SRodney W. Grimes 		mask = S_IFREG;
11579b50d902SRodney W. Grimes 		break;
11589b50d902SRodney W. Grimes 	case 'l':
11599b50d902SRodney W. Grimes 		mask = S_IFLNK;
11609b50d902SRodney W. Grimes 		break;
11619b50d902SRodney W. Grimes 	case 'p':
11629b50d902SRodney W. Grimes 		mask = S_IFIFO;
11639b50d902SRodney W. Grimes 		break;
11649b50d902SRodney W. Grimes 	case 's':
11659b50d902SRodney W. Grimes 		mask = S_IFSOCK;
11669b50d902SRodney W. Grimes 		break;
1167841484cdSPeter Wemm #ifdef FTS_WHITEOUT
1168841484cdSPeter Wemm 	case 'w':
1169841484cdSPeter Wemm 		mask = S_IFWHT;
1170841484cdSPeter Wemm 		ftsoptions |= FTS_WHITEOUT;
1171841484cdSPeter Wemm 		break;
1172841484cdSPeter Wemm #endif /* FTS_WHITEOUT */
11739b50d902SRodney W. Grimes 	default:
11749b50d902SRodney W. Grimes 		errx(1, "-type: %s: unknown type", typestring);
11759b50d902SRodney W. Grimes 	}
11769b50d902SRodney W. Grimes 
11779b50d902SRodney W. Grimes 	new = palloc(N_TYPE, f_type);
11789b50d902SRodney W. Grimes 	new->m_data = mask;
11799b50d902SRodney W. Grimes 	return (new);
11809b50d902SRodney W. Grimes }
11819b50d902SRodney W. Grimes 
11829b50d902SRodney W. Grimes /*
1183abacbbbfSPeter Wemm  * -delete functions --
1184abacbbbfSPeter Wemm  *
1185abacbbbfSPeter Wemm  *	True always.  Makes it's best shot and continues on regardless.
1186abacbbbfSPeter Wemm  */
1187abacbbbfSPeter Wemm int
1188abacbbbfSPeter Wemm f_delete(plan, entry)
1189abacbbbfSPeter Wemm 	PLAN *plan;
1190abacbbbfSPeter Wemm 	FTSENT *entry;
1191abacbbbfSPeter Wemm {
1192abacbbbfSPeter Wemm 	/* ignore these from fts */
1193abacbbbfSPeter Wemm 	if (strcmp(entry->fts_accpath, ".") == 0 ||
1194abacbbbfSPeter Wemm 	    strcmp(entry->fts_accpath, "..") == 0)
1195abacbbbfSPeter Wemm 		return (1);
1196abacbbbfSPeter Wemm 
1197abacbbbfSPeter Wemm 	/* sanity check */
1198abacbbbfSPeter Wemm 	if (isdepth == 0 ||			/* depth off */
1199abacbbbfSPeter Wemm 	    (ftsoptions & FTS_NOSTAT) ||	/* not stat()ing */
1200abacbbbfSPeter Wemm 	    !(ftsoptions & FTS_PHYSICAL) ||	/* physical off */
1201abacbbbfSPeter Wemm 	    (ftsoptions & FTS_LOGICAL))		/* or finally, logical on */
1202abacbbbfSPeter Wemm 		errx(1, "-delete: insecure options got turned on");
1203abacbbbfSPeter Wemm 
1204abacbbbfSPeter Wemm 	/* Potentially unsafe - do not accept relative paths whatsoever */
1205abacbbbfSPeter Wemm 	if (strchr(entry->fts_accpath, '/') != NULL)
1206abacbbbfSPeter Wemm 		errx(1, "-delete: %s: relative path potentially not safe",
1207abacbbbfSPeter Wemm 			entry->fts_accpath);
1208abacbbbfSPeter Wemm 
1209242ab807SPeter Wemm 	/* Turn off user immutable bits if running as root */
1210242ab807SPeter Wemm 	if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
1211242ab807SPeter Wemm 	    !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
1212242ab807SPeter Wemm 	    geteuid() == 0)
1213242ab807SPeter Wemm 		chflags(entry->fts_accpath,
1214242ab807SPeter Wemm 		       entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
1215242ab807SPeter Wemm 
1216abacbbbfSPeter Wemm 	/* rmdir directories, unlink everything else */
1217abacbbbfSPeter Wemm 	if (S_ISDIR(entry->fts_statp->st_mode)) {
12183598e52cSPeter Wemm 		if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
1219abacbbbfSPeter Wemm 			warn("-delete: rmdir(%s)", entry->fts_path);
1220abacbbbfSPeter Wemm 	} else {
1221abacbbbfSPeter Wemm 		if (unlink(entry->fts_accpath) < 0)
1222abacbbbfSPeter Wemm 			warn("-delete: unlink(%s)", entry->fts_path);
1223abacbbbfSPeter Wemm 	}
1224abacbbbfSPeter Wemm 
1225abacbbbfSPeter Wemm 	/* "succeed" */
1226abacbbbfSPeter Wemm 	return (1);
1227abacbbbfSPeter Wemm }
1228abacbbbfSPeter Wemm 
1229abacbbbfSPeter Wemm PLAN *
1230abacbbbfSPeter Wemm c_delete()
1231abacbbbfSPeter Wemm {
1232abacbbbfSPeter Wemm 
1233abacbbbfSPeter Wemm 	ftsoptions &= ~FTS_NOSTAT;	/* no optimise */
1234abacbbbfSPeter Wemm 	ftsoptions |= FTS_PHYSICAL;	/* disable -follow */
1235abacbbbfSPeter Wemm 	ftsoptions &= ~FTS_LOGICAL;	/* disable -follow */
1236abacbbbfSPeter Wemm 	isoutput = 1;			/* possible output */
1237abacbbbfSPeter Wemm 	isdepth = 1;			/* -depth implied */
1238abacbbbfSPeter Wemm 
1239abacbbbfSPeter Wemm 	return (palloc(N_DELETE, f_delete));
1240abacbbbfSPeter Wemm }
1241abacbbbfSPeter Wemm 
1242abacbbbfSPeter Wemm /*
12439b50d902SRodney W. Grimes  * -user uname functions --
12449b50d902SRodney W. Grimes  *
12459b50d902SRodney W. Grimes  *	True if the file belongs to the user uname.  If uname is numeric and
12469b50d902SRodney W. Grimes  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
12479b50d902SRodney W. Grimes  *	return a valid user name, uname is taken as a user ID.
12489b50d902SRodney W. Grimes  */
12499b50d902SRodney W. Grimes int
12509b50d902SRodney W. Grimes f_user(plan, entry)
12519b50d902SRodney W. Grimes 	PLAN *plan;
12529b50d902SRodney W. Grimes 	FTSENT *entry;
12539b50d902SRodney W. Grimes {
12549b50d902SRodney W. Grimes 	return (entry->fts_statp->st_uid == plan->u_data);
12559b50d902SRodney W. Grimes }
12569b50d902SRodney W. Grimes 
12579b50d902SRodney W. Grimes PLAN *
12589b50d902SRodney W. Grimes c_user(username)
12599b50d902SRodney W. Grimes 	char *username;
12609b50d902SRodney W. Grimes {
12619b50d902SRodney W. Grimes 	PLAN *new;
12629b50d902SRodney W. Grimes 	struct passwd *p;
12639b50d902SRodney W. Grimes 	uid_t uid;
12649b50d902SRodney W. Grimes 
12659b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
12669b50d902SRodney W. Grimes 
12679b50d902SRodney W. Grimes 	p = getpwnam(username);
12689b50d902SRodney W. Grimes 	if (p == NULL) {
12699b50d902SRodney W. Grimes 		uid = atoi(username);
12709b50d902SRodney W. Grimes 		if (uid == 0 && username[0] != '0')
12719b50d902SRodney W. Grimes 			errx(1, "-user: %s: no such user", username);
12729b50d902SRodney W. Grimes 	} else
12739b50d902SRodney W. Grimes 		uid = p->pw_uid;
12749b50d902SRodney W. Grimes 
12759b50d902SRodney W. Grimes 	new = palloc(N_USER, f_user);
12769b50d902SRodney W. Grimes 	new->u_data = uid;
12779b50d902SRodney W. Grimes 	return (new);
12789b50d902SRodney W. Grimes }
12799b50d902SRodney W. Grimes 
12809b50d902SRodney W. Grimes /*
12819b50d902SRodney W. Grimes  * -xdev functions --
12829b50d902SRodney W. Grimes  *
12839b50d902SRodney W. Grimes  *	Always true, causes find not to decend past directories that have a
12849b50d902SRodney W. Grimes  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
12859b50d902SRodney W. Grimes  */
12869b50d902SRodney W. Grimes PLAN *
12879b50d902SRodney W. Grimes c_xdev()
12889b50d902SRodney W. Grimes {
12899b50d902SRodney W. Grimes 	ftsoptions |= FTS_XDEV;
12909b50d902SRodney W. Grimes 
12919b50d902SRodney W. Grimes 	return (palloc(N_XDEV, f_always_true));
12929b50d902SRodney W. Grimes }
12939b50d902SRodney W. Grimes 
12949b50d902SRodney W. Grimes /*
12959b50d902SRodney W. Grimes  * ( expression ) functions --
12969b50d902SRodney W. Grimes  *
12979b50d902SRodney W. Grimes  *	True if expression is true.
12989b50d902SRodney W. Grimes  */
12999b50d902SRodney W. Grimes int
13009b50d902SRodney W. Grimes f_expr(plan, entry)
13019b50d902SRodney W. Grimes 	PLAN *plan;
13029b50d902SRodney W. Grimes 	FTSENT *entry;
13039b50d902SRodney W. Grimes {
13049b50d902SRodney W. Grimes 	register PLAN *p;
13059b50d902SRodney W. Grimes 	register int state;
13069b50d902SRodney W. Grimes 
1307389017e5SBill Fumerola 	state = 0;
13089b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
13099b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
13109b50d902SRodney W. Grimes 	return (state);
13119b50d902SRodney W. Grimes }
13129b50d902SRodney W. Grimes 
13139b50d902SRodney W. Grimes /*
13149b50d902SRodney W. Grimes  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
13159b50d902SRodney W. Grimes  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
13169b50d902SRodney W. Grimes  * to a N_EXPR node containing the expression and the ')' node is discarded.
13179b50d902SRodney W. Grimes  */
13189b50d902SRodney W. Grimes PLAN *
13199b50d902SRodney W. Grimes c_openparen()
13209b50d902SRodney W. Grimes {
13219b50d902SRodney W. Grimes 	return (palloc(N_OPENPAREN, (int (*)())-1));
13229b50d902SRodney W. Grimes }
13239b50d902SRodney W. Grimes 
13249b50d902SRodney W. Grimes PLAN *
13259b50d902SRodney W. Grimes c_closeparen()
13269b50d902SRodney W. Grimes {
13279b50d902SRodney W. Grimes 	return (palloc(N_CLOSEPAREN, (int (*)())-1));
13289b50d902SRodney W. Grimes }
13299b50d902SRodney W. Grimes 
13309b50d902SRodney W. Grimes /*
13319b50d902SRodney W. Grimes  * ! expression functions --
13329b50d902SRodney W. Grimes  *
13339b50d902SRodney W. Grimes  *	Negation of a primary; the unary NOT operator.
13349b50d902SRodney W. Grimes  */
13359b50d902SRodney W. Grimes int
13369b50d902SRodney W. Grimes f_not(plan, entry)
13379b50d902SRodney W. Grimes 	PLAN *plan;
13389b50d902SRodney W. Grimes 	FTSENT *entry;
13399b50d902SRodney W. Grimes {
13409b50d902SRodney W. Grimes 	register PLAN *p;
13419b50d902SRodney W. Grimes 	register int state;
13429b50d902SRodney W. Grimes 
1343389017e5SBill Fumerola 	state = 0;
13449b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
13459b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
13469b50d902SRodney W. Grimes 	return (!state);
13479b50d902SRodney W. Grimes }
13489b50d902SRodney W. Grimes 
13499b50d902SRodney W. Grimes PLAN *
13509b50d902SRodney W. Grimes c_not()
13519b50d902SRodney W. Grimes {
13529b50d902SRodney W. Grimes 	return (palloc(N_NOT, f_not));
13539b50d902SRodney W. Grimes }
13549b50d902SRodney W. Grimes 
13559b50d902SRodney W. Grimes /*
13569b50d902SRodney W. Grimes  * expression -o expression functions --
13579b50d902SRodney W. Grimes  *
13589b50d902SRodney W. Grimes  *	Alternation of primaries; the OR operator.  The second expression is
13599b50d902SRodney W. Grimes  * not evaluated if the first expression is true.
13609b50d902SRodney W. Grimes  */
13619b50d902SRodney W. Grimes int
13629b50d902SRodney W. Grimes f_or(plan, entry)
13639b50d902SRodney W. Grimes 	PLAN *plan;
13649b50d902SRodney W. Grimes 	FTSENT *entry;
13659b50d902SRodney W. Grimes {
13669b50d902SRodney W. Grimes 	register PLAN *p;
13679b50d902SRodney W. Grimes 	register int state;
13689b50d902SRodney W. Grimes 
1369389017e5SBill Fumerola 	state = 0;
13709b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
13719b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
13729b50d902SRodney W. Grimes 
13739b50d902SRodney W. Grimes 	if (state)
13749b50d902SRodney W. Grimes 		return (1);
13759b50d902SRodney W. Grimes 
13769b50d902SRodney W. Grimes 	for (p = plan->p_data[1];
13779b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
13789b50d902SRodney W. Grimes 	return (state);
13799b50d902SRodney W. Grimes }
13809b50d902SRodney W. Grimes 
13819b50d902SRodney W. Grimes PLAN *
13829b50d902SRodney W. Grimes c_or()
13839b50d902SRodney W. Grimes {
13849b50d902SRodney W. Grimes 	return (palloc(N_OR, f_or));
13859b50d902SRodney W. Grimes }
13869b50d902SRodney W. Grimes 
13879b50d902SRodney W. Grimes static PLAN *
13889b50d902SRodney W. Grimes palloc(t, f)
13899b50d902SRodney W. Grimes 	enum ntype t;
13909b50d902SRodney W. Grimes 	int (*f) __P((PLAN *, FTSENT *));
13919b50d902SRodney W. Grimes {
13929b50d902SRodney W. Grimes 	PLAN *new;
13939b50d902SRodney W. Grimes 
13949b50d902SRodney W. Grimes 	if ((new = malloc(sizeof(PLAN))) == NULL)
13959b50d902SRodney W. Grimes 		err(1, NULL);
13969b50d902SRodney W. Grimes 	new->type = t;
13979b50d902SRodney W. Grimes 	new->eval = f;
13989b50d902SRodney W. Grimes 	new->flags = 0;
13999b50d902SRodney W. Grimes 	new->next = NULL;
14009b50d902SRodney W. Grimes 	return (new);
14019b50d902SRodney W. Grimes }
1402