xref: /freebsd/usr.bin/find/function.c (revision 3f5223f84ac91b218fb0544edf5e914e339023b3)
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 /*
1293f5223f8SWolfram Schneider  * -amin n functions --
1303f5223f8SWolfram Schneider  *
1313f5223f8SWolfram Schneider  *	True if the difference between the file access time and the
1323f5223f8SWolfram Schneider  *	current time is n min periods.
1333f5223f8SWolfram Schneider  */
1343f5223f8SWolfram Schneider int
1353f5223f8SWolfram Schneider f_amin(plan, entry)
1363f5223f8SWolfram Schneider 	PLAN *plan;
1373f5223f8SWolfram Schneider 	FTSENT *entry;
1383f5223f8SWolfram Schneider {
1393f5223f8SWolfram Schneider 	extern time_t now;
1403f5223f8SWolfram Schneider 
1413f5223f8SWolfram Schneider 	COMPARE((now - entry->fts_statp->st_atime +
1423f5223f8SWolfram Schneider 	    60 - 1) / 60, plan->t_data);
1433f5223f8SWolfram Schneider }
1443f5223f8SWolfram Schneider 
1453f5223f8SWolfram Schneider PLAN *
1463f5223f8SWolfram Schneider c_amin(arg)
1473f5223f8SWolfram Schneider 	char *arg;
1483f5223f8SWolfram Schneider {
1493f5223f8SWolfram Schneider 	PLAN *new;
1503f5223f8SWolfram Schneider 
1513f5223f8SWolfram Schneider 	ftsoptions &= ~FTS_NOSTAT;
1523f5223f8SWolfram Schneider 
1533f5223f8SWolfram Schneider 	new = palloc(N_AMIN, f_amin);
1543f5223f8SWolfram Schneider 	new->t_data = find_parsenum(new, "-amin", arg, NULL);
1553f5223f8SWolfram Schneider 	TIME_CORRECT(new, N_AMIN);
1563f5223f8SWolfram Schneider 	return (new);
1573f5223f8SWolfram Schneider }
1583f5223f8SWolfram Schneider 
1593f5223f8SWolfram Schneider 
1603f5223f8SWolfram Schneider /*
1619b50d902SRodney W. Grimes  * -atime n functions --
1629b50d902SRodney W. Grimes  *
1639b50d902SRodney W. Grimes  *	True if the difference between the file access time and the
1649b50d902SRodney W. Grimes  *	current time is n 24 hour periods.
1659b50d902SRodney W. Grimes  */
1669b50d902SRodney W. Grimes int
1679b50d902SRodney W. Grimes f_atime(plan, entry)
1689b50d902SRodney W. Grimes 	PLAN *plan;
1699b50d902SRodney W. Grimes 	FTSENT *entry;
1709b50d902SRodney W. Grimes {
1719b50d902SRodney W. Grimes 	extern time_t now;
1729b50d902SRodney W. Grimes 
1739b50d902SRodney W. Grimes 	COMPARE((now - entry->fts_statp->st_atime +
174656dcd43SGarrett Wollman 	    86400 - 1) / 86400, plan->t_data);
1759b50d902SRodney W. Grimes }
1769b50d902SRodney W. Grimes 
1779b50d902SRodney W. Grimes PLAN *
1789b50d902SRodney W. Grimes c_atime(arg)
1799b50d902SRodney W. Grimes 	char *arg;
1809b50d902SRodney W. Grimes {
1819b50d902SRodney W. Grimes 	PLAN *new;
1829b50d902SRodney W. Grimes 
1839b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
1849b50d902SRodney W. Grimes 
1859b50d902SRodney W. Grimes 	new = palloc(N_ATIME, f_atime);
1869b50d902SRodney W. Grimes 	new->t_data = find_parsenum(new, "-atime", arg, NULL);
1879b50d902SRodney W. Grimes 	TIME_CORRECT(new, N_ATIME);
1889b50d902SRodney W. Grimes 	return (new);
1899b50d902SRodney W. Grimes }
1903f5223f8SWolfram Schneider 
1913f5223f8SWolfram Schneider 
1923f5223f8SWolfram Schneider /*
1933f5223f8SWolfram Schneider  * -cmin n functions --
1943f5223f8SWolfram Schneider  *
1953f5223f8SWolfram Schneider  *	True if the difference between the last change of file
1963f5223f8SWolfram Schneider  *	status information and the current time is n min periods.
1973f5223f8SWolfram Schneider  */
1983f5223f8SWolfram Schneider int
1993f5223f8SWolfram Schneider f_cmin(plan, entry)
2003f5223f8SWolfram Schneider 	PLAN *plan;
2013f5223f8SWolfram Schneider 	FTSENT *entry;
2023f5223f8SWolfram Schneider {
2033f5223f8SWolfram Schneider 	extern time_t now;
2043f5223f8SWolfram Schneider 
2053f5223f8SWolfram Schneider 	COMPARE((now - entry->fts_statp->st_ctime +
2063f5223f8SWolfram Schneider 	    60 - 1) / 60, plan->t_data);
2073f5223f8SWolfram Schneider }
2083f5223f8SWolfram Schneider 
2093f5223f8SWolfram Schneider PLAN *
2103f5223f8SWolfram Schneider c_cmin(arg)
2113f5223f8SWolfram Schneider 	char *arg;
2123f5223f8SWolfram Schneider {
2133f5223f8SWolfram Schneider 	PLAN *new;
2143f5223f8SWolfram Schneider 
2153f5223f8SWolfram Schneider 	ftsoptions &= ~FTS_NOSTAT;
2163f5223f8SWolfram Schneider 
2173f5223f8SWolfram Schneider 	new = palloc(N_CMIN, f_cmin);
2183f5223f8SWolfram Schneider 	new->t_data = find_parsenum(new, "-cmin", arg, NULL);
2193f5223f8SWolfram Schneider 	TIME_CORRECT(new, N_CMIN);
2203f5223f8SWolfram Schneider 	return (new);
2213f5223f8SWolfram Schneider }
2223f5223f8SWolfram Schneider 
2239b50d902SRodney W. Grimes /*
2249b50d902SRodney W. Grimes  * -ctime n functions --
2259b50d902SRodney W. Grimes  *
2269b50d902SRodney W. Grimes  *	True if the difference between the last change of file
2279b50d902SRodney W. Grimes  *	status information and the current time is n 24 hour periods.
2289b50d902SRodney W. Grimes  */
2299b50d902SRodney W. Grimes int
2309b50d902SRodney W. Grimes f_ctime(plan, entry)
2319b50d902SRodney W. Grimes 	PLAN *plan;
2329b50d902SRodney W. Grimes 	FTSENT *entry;
2339b50d902SRodney W. Grimes {
2349b50d902SRodney W. Grimes 	extern time_t now;
2359b50d902SRodney W. Grimes 
2369b50d902SRodney W. Grimes 	COMPARE((now - entry->fts_statp->st_ctime +
237656dcd43SGarrett Wollman 	    86400 - 1) / 86400, plan->t_data);
2389b50d902SRodney W. Grimes }
2399b50d902SRodney W. Grimes 
2409b50d902SRodney W. Grimes PLAN *
2419b50d902SRodney W. Grimes c_ctime(arg)
2429b50d902SRodney W. Grimes 	char *arg;
2439b50d902SRodney W. Grimes {
2449b50d902SRodney W. Grimes 	PLAN *new;
2459b50d902SRodney W. Grimes 
2469b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes 	new = palloc(N_CTIME, f_ctime);
2499b50d902SRodney W. Grimes 	new->t_data = find_parsenum(new, "-ctime", arg, NULL);
2509b50d902SRodney W. Grimes 	TIME_CORRECT(new, N_CTIME);
2519b50d902SRodney W. Grimes 	return (new);
2529b50d902SRodney W. Grimes }
2539b50d902SRodney W. Grimes 
2543f5223f8SWolfram Schneider 
2559b50d902SRodney W. Grimes /*
2569b50d902SRodney W. Grimes  * -depth functions --
2579b50d902SRodney W. Grimes  *
2589b50d902SRodney W. Grimes  *	Always true, causes descent of the directory hierarchy to be done
2599b50d902SRodney W. Grimes  *	so that all entries in a directory are acted on before the directory
2609b50d902SRodney W. Grimes  *	itself.
2619b50d902SRodney W. Grimes  */
2629b50d902SRodney W. Grimes int
2639b50d902SRodney W. Grimes f_always_true(plan, entry)
2649b50d902SRodney W. Grimes 	PLAN *plan;
2659b50d902SRodney W. Grimes 	FTSENT *entry;
2669b50d902SRodney W. Grimes {
2679b50d902SRodney W. Grimes 	return (1);
2689b50d902SRodney W. Grimes }
2699b50d902SRodney W. Grimes 
2709b50d902SRodney W. Grimes PLAN *
2719b50d902SRodney W. Grimes c_depth()
2729b50d902SRodney W. Grimes {
2739b50d902SRodney W. Grimes 	isdepth = 1;
2749b50d902SRodney W. Grimes 
2759b50d902SRodney W. Grimes 	return (palloc(N_DEPTH, f_always_true));
2769b50d902SRodney W. Grimes }
2779b50d902SRodney W. Grimes 
2789b50d902SRodney W. Grimes /*
2799b50d902SRodney W. Grimes  * [-exec | -ok] utility [arg ... ] ; functions --
2809b50d902SRodney W. Grimes  *
2819b50d902SRodney W. Grimes  *	True if the executed utility returns a zero value as exit status.
2829b50d902SRodney W. Grimes  *	The end of the primary expression is delimited by a semicolon.  If
2839b50d902SRodney W. Grimes  *	"{}" occurs anywhere, it gets replaced by the current pathname.
2849b50d902SRodney W. Grimes  *	The current directory for the execution of utility is the same as
2859b50d902SRodney W. Grimes  *	the current directory when the find utility was started.
2869b50d902SRodney W. Grimes  *
2879b50d902SRodney W. Grimes  *	The primary -ok is different in that it requests affirmation of the
2889b50d902SRodney W. Grimes  *	user before executing the utility.
2899b50d902SRodney W. Grimes  */
2909b50d902SRodney W. Grimes int
2919b50d902SRodney W. Grimes f_exec(plan, entry)
2929b50d902SRodney W. Grimes 	register PLAN *plan;
2939b50d902SRodney W. Grimes 	FTSENT *entry;
2949b50d902SRodney W. Grimes {
2959b50d902SRodney W. Grimes 	extern int dotfd;
2969b50d902SRodney W. Grimes 	register int cnt;
2979b50d902SRodney W. Grimes 	pid_t pid;
2989b50d902SRodney W. Grimes 	int status;
2999b50d902SRodney W. Grimes 
3009b50d902SRodney W. Grimes 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
3019b50d902SRodney W. Grimes 		if (plan->e_len[cnt])
3029b50d902SRodney W. Grimes 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
3039b50d902SRodney W. Grimes 			    entry->fts_path, plan->e_len[cnt]);
3049b50d902SRodney W. Grimes 
3059b50d902SRodney W. Grimes 	if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
3069b50d902SRodney W. Grimes 		return (0);
3079b50d902SRodney W. Grimes 
308e9f1a293SNate Williams 	/* make sure find output is interspersed correctly with subprocesses */
309e9f1a293SNate Williams 	fflush(stdout);
310e9f1a293SNate Williams 
3119b50d902SRodney W. Grimes 	switch (pid = vfork()) {
3129b50d902SRodney W. Grimes 	case -1:
3139b50d902SRodney W. Grimes 		err(1, "fork");
3149b50d902SRodney W. Grimes 		/* NOTREACHED */
3159b50d902SRodney W. Grimes 	case 0:
3169b50d902SRodney W. Grimes 		if (fchdir(dotfd)) {
3179b50d902SRodney W. Grimes 			warn("chdir");
3189b50d902SRodney W. Grimes 			_exit(1);
3199b50d902SRodney W. Grimes 		}
3209b50d902SRodney W. Grimes 		execvp(plan->e_argv[0], plan->e_argv);
3219b50d902SRodney W. Grimes 		warn("%s", plan->e_argv[0]);
3229b50d902SRodney W. Grimes 		_exit(1);
3239b50d902SRodney W. Grimes 	}
3249b50d902SRodney W. Grimes 	pid = waitpid(pid, &status, 0);
3259b50d902SRodney W. Grimes 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
3269b50d902SRodney W. Grimes }
3279b50d902SRodney W. Grimes 
3289b50d902SRodney W. Grimes /*
3299b50d902SRodney W. Grimes  * c_exec --
3309b50d902SRodney W. Grimes  *	build three parallel arrays, one with pointers to the strings passed
3319b50d902SRodney W. Grimes  *	on the command line, one with (possibly duplicated) pointers to the
3329b50d902SRodney W. Grimes  *	argv array, and one with integer values that are lengths of the
3339b50d902SRodney W. Grimes  *	strings, but also flags meaning that the string has to be massaged.
3349b50d902SRodney W. Grimes  */
3359b50d902SRodney W. Grimes PLAN *
3369b50d902SRodney W. Grimes c_exec(argvp, isok)
3379b50d902SRodney W. Grimes 	char ***argvp;
3389b50d902SRodney W. Grimes 	int isok;
3399b50d902SRodney W. Grimes {
3409b50d902SRodney W. Grimes 	PLAN *new;			/* node returned */
3419b50d902SRodney W. Grimes 	register int cnt;
3429b50d902SRodney W. Grimes 	register char **argv, **ap, *p;
3439b50d902SRodney W. Grimes 
3449b50d902SRodney W. Grimes 	isoutput = 1;
3459b50d902SRodney W. Grimes 
3469b50d902SRodney W. Grimes 	new = palloc(N_EXEC, f_exec);
3479b50d902SRodney W. Grimes 	if (isok)
3489b50d902SRodney W. Grimes 		new->flags = F_NEEDOK;
3499b50d902SRodney W. Grimes 
3509b50d902SRodney W. Grimes 	for (ap = argv = *argvp;; ++ap) {
3519b50d902SRodney W. Grimes 		if (!*ap)
3529b50d902SRodney W. Grimes 			errx(1,
3539b50d902SRodney W. Grimes 			    "%s: no terminating \";\"", isok ? "-ok" : "-exec");
3549b50d902SRodney W. Grimes 		if (**ap == ';')
3559b50d902SRodney W. Grimes 			break;
3569b50d902SRodney W. Grimes 	}
3579b50d902SRodney W. Grimes 
3589b50d902SRodney W. Grimes 	cnt = ap - *argvp + 1;
3599b50d902SRodney W. Grimes 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
3609b50d902SRodney W. Grimes 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
3619b50d902SRodney W. Grimes 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
3629b50d902SRodney W. Grimes 
3639b50d902SRodney W. Grimes 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
3649b50d902SRodney W. Grimes 		new->e_orig[cnt] = *argv;
3659b50d902SRodney W. Grimes 		for (p = *argv; *p; ++p)
3669b50d902SRodney W. Grimes 			if (p[0] == '{' && p[1] == '}') {
3679b50d902SRodney W. Grimes 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
3689b50d902SRodney W. Grimes 				new->e_len[cnt] = MAXPATHLEN;
3699b50d902SRodney W. Grimes 				break;
3709b50d902SRodney W. Grimes 			}
3719b50d902SRodney W. Grimes 		if (!*p) {
3729b50d902SRodney W. Grimes 			new->e_argv[cnt] = *argv;
3739b50d902SRodney W. Grimes 			new->e_len[cnt] = 0;
3749b50d902SRodney W. Grimes 		}
3759b50d902SRodney W. Grimes 	}
3769b50d902SRodney W. Grimes 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
3779b50d902SRodney W. Grimes 
3789b50d902SRodney W. Grimes 	*argvp = argv + 1;
3799b50d902SRodney W. Grimes 	return (new);
3809b50d902SRodney W. Grimes }
381127d7563SWarner Losh 
382127d7563SWarner Losh /*
383127d7563SWarner Losh  * -execdir utility [arg ... ] ; functions --
384127d7563SWarner Losh  *
385127d7563SWarner Losh  *	True if the executed utility returns a zero value as exit status.
386127d7563SWarner Losh  *	The end of the primary expression is delimited by a semicolon.  If
387127d7563SWarner Losh  *	"{}" occurs anywhere, it gets replaced by the unqualified pathname.
388127d7563SWarner Losh  *	The current directory for the execution of utility is the same as
389127d7563SWarner Losh  *	the directory where the file lives.
390127d7563SWarner Losh  */
391127d7563SWarner Losh int
392127d7563SWarner Losh f_execdir(plan, entry)
393127d7563SWarner Losh 	register PLAN *plan;
394127d7563SWarner Losh 	FTSENT *entry;
395127d7563SWarner Losh {
396127d7563SWarner Losh 	extern int dotfd;
397127d7563SWarner Losh 	register int cnt;
398127d7563SWarner Losh 	pid_t pid;
399127d7563SWarner Losh 	int status;
400127d7563SWarner Losh 	char *file;
401127d7563SWarner Losh 
402127d7563SWarner Losh 	/* XXX - if file/dir ends in '/' this will not work -- can it? */
403127d7563SWarner Losh 	if ((file = strrchr(entry->fts_path, '/')))
404127d7563SWarner Losh 	    file++;
405127d7563SWarner Losh 	else
406127d7563SWarner Losh 	    file = entry->fts_path;
407127d7563SWarner Losh 
408127d7563SWarner Losh 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
409127d7563SWarner Losh 		if (plan->e_len[cnt])
410127d7563SWarner Losh 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
411127d7563SWarner Losh 			    file, plan->e_len[cnt]);
412127d7563SWarner Losh 
413127d7563SWarner Losh 	/* don't mix output of command with find output */
414127d7563SWarner Losh 	fflush(stdout);
415127d7563SWarner Losh 	fflush(stderr);
416127d7563SWarner Losh 
417127d7563SWarner Losh 	switch (pid = vfork()) {
418127d7563SWarner Losh 	case -1:
419127d7563SWarner Losh 		err(1, "fork");
420127d7563SWarner Losh 		/* NOTREACHED */
421127d7563SWarner Losh 	case 0:
422127d7563SWarner Losh 		execvp(plan->e_argv[0], plan->e_argv);
423127d7563SWarner Losh 		warn("%s", plan->e_argv[0]);
424127d7563SWarner Losh 		_exit(1);
425127d7563SWarner Losh 	}
426127d7563SWarner Losh 	pid = waitpid(pid, &status, 0);
427127d7563SWarner Losh 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
428127d7563SWarner Losh }
429127d7563SWarner Losh 
430127d7563SWarner Losh /*
431127d7563SWarner Losh  * c_execdir --
432127d7563SWarner Losh  *	build three parallel arrays, one with pointers to the strings passed
433127d7563SWarner Losh  *	on the command line, one with (possibly duplicated) pointers to the
434127d7563SWarner Losh  *	argv array, and one with integer values that are lengths of the
435127d7563SWarner Losh  *	strings, but also flags meaning that the string has to be massaged.
436127d7563SWarner Losh  */
437127d7563SWarner Losh PLAN *
438127d7563SWarner Losh c_execdir(argvp)
439127d7563SWarner Losh 	char ***argvp;
440127d7563SWarner Losh {
441127d7563SWarner Losh 	PLAN *new;			/* node returned */
442127d7563SWarner Losh 	register int cnt;
443127d7563SWarner Losh 	register char **argv, **ap, *p;
444127d7563SWarner Losh 
445127d7563SWarner Losh 	ftsoptions &= ~FTS_NOSTAT;
446127d7563SWarner Losh 	isoutput = 1;
447127d7563SWarner Losh 
448127d7563SWarner Losh 	new = palloc(N_EXECDIR, f_execdir);
449127d7563SWarner Losh 
450127d7563SWarner Losh 	for (ap = argv = *argvp;; ++ap) {
451127d7563SWarner Losh 		if (!*ap)
452127d7563SWarner Losh 			errx(1,
453127d7563SWarner Losh 			    "-execdir: no terminating \";\"");
454127d7563SWarner Losh 		if (**ap == ';')
455127d7563SWarner Losh 			break;
456127d7563SWarner Losh 	}
457127d7563SWarner Losh 
458127d7563SWarner Losh 	cnt = ap - *argvp + 1;
459127d7563SWarner Losh 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
460127d7563SWarner Losh 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
461127d7563SWarner Losh 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
462127d7563SWarner Losh 
463127d7563SWarner Losh 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
464127d7563SWarner Losh 		new->e_orig[cnt] = *argv;
465127d7563SWarner Losh 		for (p = *argv; *p; ++p)
466127d7563SWarner Losh 			if (p[0] == '{' && p[1] == '}') {
467127d7563SWarner Losh 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
468127d7563SWarner Losh 				new->e_len[cnt] = MAXPATHLEN;
469127d7563SWarner Losh 				break;
470127d7563SWarner Losh 			}
471127d7563SWarner Losh 		if (!*p) {
472127d7563SWarner Losh 			new->e_argv[cnt] = *argv;
473127d7563SWarner Losh 			new->e_len[cnt] = 0;
474127d7563SWarner Losh 		}
475127d7563SWarner Losh 	}
476127d7563SWarner Losh 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
477127d7563SWarner Losh 
478127d7563SWarner Losh 	*argvp = argv + 1;
479127d7563SWarner Losh 	return (new);
480127d7563SWarner Losh }
4819b50d902SRodney W. Grimes 
4829b50d902SRodney W. Grimes /*
4839b50d902SRodney W. Grimes  * -follow functions --
4849b50d902SRodney W. Grimes  *
4859b50d902SRodney W. Grimes  *	Always true, causes symbolic links to be followed on a global
4869b50d902SRodney W. Grimes  *	basis.
4879b50d902SRodney W. Grimes  */
4889b50d902SRodney W. Grimes PLAN *
4899b50d902SRodney W. Grimes c_follow()
4909b50d902SRodney W. Grimes {
4919b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_PHYSICAL;
4929b50d902SRodney W. Grimes 	ftsoptions |= FTS_LOGICAL;
4939b50d902SRodney W. Grimes 
4949b50d902SRodney W. Grimes 	return (palloc(N_FOLLOW, f_always_true));
4959b50d902SRodney W. Grimes }
4969b50d902SRodney W. Grimes 
4979b50d902SRodney W. Grimes /*
4989b50d902SRodney W. Grimes  * -fstype functions --
4999b50d902SRodney W. Grimes  *
5009b50d902SRodney W. Grimes  *	True if the file is of a certain type.
5019b50d902SRodney W. Grimes  */
5029b50d902SRodney W. Grimes int
5039b50d902SRodney W. Grimes f_fstype(plan, entry)
5049b50d902SRodney W. Grimes 	PLAN *plan;
5059b50d902SRodney W. Grimes 	FTSENT *entry;
5069b50d902SRodney W. Grimes {
5079b50d902SRodney W. Grimes 	static dev_t curdev;	/* need a guaranteed illegal dev value */
5089b50d902SRodney W. Grimes 	static int first = 1;
5099b50d902SRodney W. Grimes 	struct statfs sb;
5109d08e419SPeter Wemm 	static int val_type, val_flags;
5119b50d902SRodney W. Grimes 	char *p, save[2];
5129b50d902SRodney W. Grimes 
5139b50d902SRodney W. Grimes 	/* Only check when we cross mount point. */
5149b50d902SRodney W. Grimes 	if (first || curdev != entry->fts_statp->st_dev) {
5159b50d902SRodney W. Grimes 		curdev = entry->fts_statp->st_dev;
5169b50d902SRodney W. Grimes 
5179b50d902SRodney W. Grimes 		/*
5189b50d902SRodney W. Grimes 		 * Statfs follows symlinks; find wants the link's file system,
5199b50d902SRodney W. Grimes 		 * not where it points.
5209b50d902SRodney W. Grimes 		 */
5219b50d902SRodney W. Grimes 		if (entry->fts_info == FTS_SL ||
5229b50d902SRodney W. Grimes 		    entry->fts_info == FTS_SLNONE) {
5239b50d902SRodney W. Grimes 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
5249b50d902SRodney W. Grimes 				++p;
5259b50d902SRodney W. Grimes 			else
5269b50d902SRodney W. Grimes 				p = entry->fts_accpath;
5279b50d902SRodney W. Grimes 			save[0] = p[0];
5289b50d902SRodney W. Grimes 			p[0] = '.';
5299b50d902SRodney W. Grimes 			save[1] = p[1];
5309b50d902SRodney W. Grimes 			p[1] = '\0';
5319b50d902SRodney W. Grimes 
5329b50d902SRodney W. Grimes 		} else
5339b50d902SRodney W. Grimes 			p = NULL;
5349b50d902SRodney W. Grimes 
5359b50d902SRodney W. Grimes 		if (statfs(entry->fts_accpath, &sb))
5369b50d902SRodney W. Grimes 			err(1, "%s", entry->fts_accpath);
5379b50d902SRodney W. Grimes 
5389b50d902SRodney W. Grimes 		if (p) {
5399b50d902SRodney W. Grimes 			p[0] = save[0];
5409b50d902SRodney W. Grimes 			p[1] = save[1];
5419b50d902SRodney W. Grimes 		}
5429b50d902SRodney W. Grimes 
5439b50d902SRodney W. Grimes 		first = 0;
544841484cdSPeter Wemm 
545841484cdSPeter Wemm 		/*
546841484cdSPeter Wemm 		 * Further tests may need both of these values, so
547841484cdSPeter Wemm 		 * always copy both of them.
548841484cdSPeter Wemm 		 */
5499d08e419SPeter Wemm 		val_flags = sb.f_flags;
5509d08e419SPeter Wemm 		val_type = sb.f_type;
5519b50d902SRodney W. Grimes 	}
5529b50d902SRodney W. Grimes 	switch (plan->flags) {
5539b50d902SRodney W. Grimes 	case F_MTFLAG:
5549d08e419SPeter Wemm 		return (val_flags & plan->mt_data) != 0;
5559b50d902SRodney W. Grimes 	case F_MTTYPE:
5569d08e419SPeter Wemm 		return (val_type == plan->mt_data);
5579b50d902SRodney W. Grimes 	default:
5589b50d902SRodney W. Grimes 		abort();
5599b50d902SRodney W. Grimes 	}
5609b50d902SRodney W. Grimes }
5619b50d902SRodney W. Grimes 
5629b50d902SRodney W. Grimes PLAN *
5639b50d902SRodney W. Grimes c_fstype(arg)
5649b50d902SRodney W. Grimes 	char *arg;
5659b50d902SRodney W. Grimes {
5669b50d902SRodney W. Grimes 	register PLAN *new;
567841484cdSPeter Wemm 	struct vfsconf vfc;
5689b50d902SRodney W. Grimes 
5699b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
5709b50d902SRodney W. Grimes 
5719b50d902SRodney W. Grimes 	new = palloc(N_FSTYPE, f_fstype);
572841484cdSPeter Wemm 
573841484cdSPeter Wemm 	/*
574841484cdSPeter Wemm 	 * Check first for a filesystem name.
575841484cdSPeter Wemm 	 */
576841484cdSPeter Wemm 	if (getvfsbyname(arg, &vfc) == 0) {
577841484cdSPeter Wemm 		new->flags = F_MTTYPE;
578841484cdSPeter Wemm 		new->mt_data = vfc.vfc_typenum;
579841484cdSPeter Wemm 		return (new);
580841484cdSPeter Wemm 	}
581841484cdSPeter Wemm 
5829b50d902SRodney W. Grimes 	switch (*arg) {
5839b50d902SRodney W. Grimes 	case 'l':
5849b50d902SRodney W. Grimes 		if (!strcmp(arg, "local")) {
5859b50d902SRodney W. Grimes 			new->flags = F_MTFLAG;
5869b50d902SRodney W. Grimes 			new->mt_data = MNT_LOCAL;
5879b50d902SRodney W. Grimes 			return (new);
5889b50d902SRodney W. Grimes 		}
5899b50d902SRodney W. Grimes 		break;
5909b50d902SRodney W. Grimes 	case 'r':
5919b50d902SRodney W. Grimes 		if (!strcmp(arg, "rdonly")) {
5929b50d902SRodney W. Grimes 			new->flags = F_MTFLAG;
5939b50d902SRodney W. Grimes 			new->mt_data = MNT_RDONLY;
5949b50d902SRodney W. Grimes 			return (new);
5959b50d902SRodney W. Grimes 		}
5969b50d902SRodney W. Grimes 		break;
5979b50d902SRodney W. Grimes 	}
5989b50d902SRodney W. Grimes 	errx(1, "%s: unknown file type", arg);
5999b50d902SRodney W. Grimes 	/* NOTREACHED */
6009b50d902SRodney W. Grimes }
6019b50d902SRodney W. Grimes 
6029b50d902SRodney W. Grimes /*
6039b50d902SRodney W. Grimes  * -group gname functions --
6049b50d902SRodney W. Grimes  *
6059b50d902SRodney W. Grimes  *	True if the file belongs to the group gname.  If gname is numeric and
6069b50d902SRodney W. Grimes  *	an equivalent of the getgrnam() function does not return a valid group
6079b50d902SRodney W. Grimes  *	name, gname is taken as a group ID.
6089b50d902SRodney W. Grimes  */
6099b50d902SRodney W. Grimes int
6109b50d902SRodney W. Grimes f_group(plan, entry)
6119b50d902SRodney W. Grimes 	PLAN *plan;
6129b50d902SRodney W. Grimes 	FTSENT *entry;
6139b50d902SRodney W. Grimes {
6149b50d902SRodney W. Grimes 	return (entry->fts_statp->st_gid == plan->g_data);
6159b50d902SRodney W. Grimes }
6169b50d902SRodney W. Grimes 
6179b50d902SRodney W. Grimes PLAN *
6189b50d902SRodney W. Grimes c_group(gname)
6199b50d902SRodney W. Grimes 	char *gname;
6209b50d902SRodney W. Grimes {
6219b50d902SRodney W. Grimes 	PLAN *new;
6229b50d902SRodney W. Grimes 	struct group *g;
6239b50d902SRodney W. Grimes 	gid_t gid;
6249b50d902SRodney W. Grimes 
6259b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
6269b50d902SRodney W. Grimes 
6279b50d902SRodney W. Grimes 	g = getgrnam(gname);
6289b50d902SRodney W. Grimes 	if (g == NULL) {
6299b50d902SRodney W. Grimes 		gid = atoi(gname);
6309b50d902SRodney W. Grimes 		if (gid == 0 && gname[0] != '0')
6319b50d902SRodney W. Grimes 			errx(1, "-group: %s: no such group", gname);
6329b50d902SRodney W. Grimes 	} else
6339b50d902SRodney W. Grimes 		gid = g->gr_gid;
6349b50d902SRodney W. Grimes 
6359b50d902SRodney W. Grimes 	new = palloc(N_GROUP, f_group);
6369b50d902SRodney W. Grimes 	new->g_data = gid;
6379b50d902SRodney W. Grimes 	return (new);
6389b50d902SRodney W. Grimes }
6399b50d902SRodney W. Grimes 
6409b50d902SRodney W. Grimes /*
6419b50d902SRodney W. Grimes  * -inum n functions --
6429b50d902SRodney W. Grimes  *
6439b50d902SRodney W. Grimes  *	True if the file has inode # n.
6449b50d902SRodney W. Grimes  */
6459b50d902SRodney W. Grimes int
6469b50d902SRodney W. Grimes f_inum(plan, entry)
6479b50d902SRodney W. Grimes 	PLAN *plan;
6489b50d902SRodney W. Grimes 	FTSENT *entry;
6499b50d902SRodney W. Grimes {
6509b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
6519b50d902SRodney W. Grimes }
6529b50d902SRodney W. Grimes 
6539b50d902SRodney W. Grimes PLAN *
6549b50d902SRodney W. Grimes c_inum(arg)
6559b50d902SRodney W. Grimes 	char *arg;
6569b50d902SRodney W. Grimes {
6579b50d902SRodney W. Grimes 	PLAN *new;
6589b50d902SRodney W. Grimes 
6599b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
6609b50d902SRodney W. Grimes 
6619b50d902SRodney W. Grimes 	new = palloc(N_INUM, f_inum);
6629b50d902SRodney W. Grimes 	new->i_data = find_parsenum(new, "-inum", arg, NULL);
6639b50d902SRodney W. Grimes 	return (new);
6649b50d902SRodney W. Grimes }
6659b50d902SRodney W. Grimes 
6669b50d902SRodney W. Grimes /*
6679b50d902SRodney W. Grimes  * -links n functions --
6689b50d902SRodney W. Grimes  *
6699b50d902SRodney W. Grimes  *	True if the file has n links.
6709b50d902SRodney W. Grimes  */
6719b50d902SRodney W. Grimes int
6729b50d902SRodney W. Grimes f_links(plan, entry)
6739b50d902SRodney W. Grimes 	PLAN *plan;
6749b50d902SRodney W. Grimes 	FTSENT *entry;
6759b50d902SRodney W. Grimes {
6769b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
6779b50d902SRodney W. Grimes }
6789b50d902SRodney W. Grimes 
6799b50d902SRodney W. Grimes PLAN *
6809b50d902SRodney W. Grimes c_links(arg)
6819b50d902SRodney W. Grimes 	char *arg;
6829b50d902SRodney W. Grimes {
6839b50d902SRodney W. Grimes 	PLAN *new;
6849b50d902SRodney W. Grimes 
6859b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
6869b50d902SRodney W. Grimes 
6879b50d902SRodney W. Grimes 	new = palloc(N_LINKS, f_links);
6889b50d902SRodney W. Grimes 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
6899b50d902SRodney W. Grimes 	return (new);
6909b50d902SRodney W. Grimes }
6919b50d902SRodney W. Grimes 
6929b50d902SRodney W. Grimes /*
6939b50d902SRodney W. Grimes  * -ls functions --
6949b50d902SRodney W. Grimes  *
6959b50d902SRodney W. Grimes  *	Always true - prints the current entry to stdout in "ls" format.
6969b50d902SRodney W. Grimes  */
6979b50d902SRodney W. Grimes int
6989b50d902SRodney W. Grimes f_ls(plan, entry)
6999b50d902SRodney W. Grimes 	PLAN *plan;
7009b50d902SRodney W. Grimes 	FTSENT *entry;
7019b50d902SRodney W. Grimes {
7029b50d902SRodney W. Grimes 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
7039b50d902SRodney W. Grimes 	return (1);
7049b50d902SRodney W. Grimes }
7059b50d902SRodney W. Grimes 
7069b50d902SRodney W. Grimes PLAN *
7079b50d902SRodney W. Grimes c_ls()
7089b50d902SRodney W. Grimes {
7099b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
7109b50d902SRodney W. Grimes 	isoutput = 1;
7119b50d902SRodney W. Grimes 
7129b50d902SRodney W. Grimes 	return (palloc(N_LS, f_ls));
7139b50d902SRodney W. Grimes }
7149b50d902SRodney W. Grimes 
7159b50d902SRodney W. Grimes /*
7169b50d902SRodney W. Grimes  * -mtime n functions --
7179b50d902SRodney W. Grimes  *
7189b50d902SRodney W. Grimes  *	True if the difference between the file modification time and the
7199b50d902SRodney W. Grimes  *	current time is n 24 hour periods.
7209b50d902SRodney W. Grimes  */
7219b50d902SRodney W. Grimes int
7229b50d902SRodney W. Grimes f_mtime(plan, entry)
7239b50d902SRodney W. Grimes 	PLAN *plan;
7249b50d902SRodney W. Grimes 	FTSENT *entry;
7259b50d902SRodney W. Grimes {
7269b50d902SRodney W. Grimes 	extern time_t now;
7279b50d902SRodney W. Grimes 
728656dcd43SGarrett Wollman 	COMPARE((now - entry->fts_statp->st_mtime + 86400 - 1) /
729656dcd43SGarrett Wollman 	    86400, plan->t_data);
7309b50d902SRodney W. Grimes }
7319b50d902SRodney W. Grimes 
7329b50d902SRodney W. Grimes PLAN *
7339b50d902SRodney W. Grimes c_mtime(arg)
7349b50d902SRodney W. Grimes 	char *arg;
7359b50d902SRodney W. Grimes {
7369b50d902SRodney W. Grimes 	PLAN *new;
7379b50d902SRodney W. Grimes 
7389b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
7399b50d902SRodney W. Grimes 
7409b50d902SRodney W. Grimes 	new = palloc(N_MTIME, f_mtime);
7419b50d902SRodney W. Grimes 	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
7429b50d902SRodney W. Grimes 	TIME_CORRECT(new, N_MTIME);
7439b50d902SRodney W. Grimes 	return (new);
7449b50d902SRodney W. Grimes }
7459b50d902SRodney W. Grimes 
7469b50d902SRodney W. Grimes /*
7473f5223f8SWolfram Schneider  * -mmin n functions --
7483f5223f8SWolfram Schneider  *
7493f5223f8SWolfram Schneider  *	True if the difference between the file modification time and the
7503f5223f8SWolfram Schneider  *	current time is n min periods.
7513f5223f8SWolfram Schneider  */
7523f5223f8SWolfram Schneider int
7533f5223f8SWolfram Schneider f_mmin(plan, entry)
7543f5223f8SWolfram Schneider 	PLAN *plan;
7553f5223f8SWolfram Schneider 	FTSENT *entry;
7563f5223f8SWolfram Schneider {
7573f5223f8SWolfram Schneider 	extern time_t now;
7583f5223f8SWolfram Schneider 
7593f5223f8SWolfram Schneider 	COMPARE((now - entry->fts_statp->st_mtime + 60 - 1) /
7603f5223f8SWolfram Schneider 	    60, plan->t_data);
7613f5223f8SWolfram Schneider }
7623f5223f8SWolfram Schneider 
7633f5223f8SWolfram Schneider PLAN *
7643f5223f8SWolfram Schneider c_mmin(arg)
7653f5223f8SWolfram Schneider 	char *arg;
7663f5223f8SWolfram Schneider {
7673f5223f8SWolfram Schneider 	PLAN *new;
7683f5223f8SWolfram Schneider 
7693f5223f8SWolfram Schneider 	ftsoptions &= ~FTS_NOSTAT;
7703f5223f8SWolfram Schneider 
7713f5223f8SWolfram Schneider 	new = palloc(N_MMIN, f_mmin);
7723f5223f8SWolfram Schneider 	new->t_data = find_parsenum(new, "-mmin", arg, NULL);
7733f5223f8SWolfram Schneider 	TIME_CORRECT(new, N_MMIN);
7743f5223f8SWolfram Schneider 	return (new);
7753f5223f8SWolfram Schneider }
7763f5223f8SWolfram Schneider 
7773f5223f8SWolfram Schneider 
7783f5223f8SWolfram Schneider /*
7799b50d902SRodney W. Grimes  * -name functions --
7809b50d902SRodney W. Grimes  *
7819b50d902SRodney W. Grimes  *	True if the basename of the filename being examined
7829b50d902SRodney W. Grimes  *	matches pattern using Pattern Matching Notation S3.14
7839b50d902SRodney W. Grimes  */
7849b50d902SRodney W. Grimes int
7859b50d902SRodney W. Grimes f_name(plan, entry)
7869b50d902SRodney W. Grimes 	PLAN *plan;
7879b50d902SRodney W. Grimes 	FTSENT *entry;
7889b50d902SRodney W. Grimes {
7899b50d902SRodney W. Grimes 	return (!fnmatch(plan->c_data, entry->fts_name, 0));
7909b50d902SRodney W. Grimes }
7919b50d902SRodney W. Grimes 
7929b50d902SRodney W. Grimes PLAN *
7939b50d902SRodney W. Grimes c_name(pattern)
7949b50d902SRodney W. Grimes 	char *pattern;
7959b50d902SRodney W. Grimes {
7969b50d902SRodney W. Grimes 	PLAN *new;
7979b50d902SRodney W. Grimes 
7989b50d902SRodney W. Grimes 	new = palloc(N_NAME, f_name);
7999b50d902SRodney W. Grimes 	new->c_data = pattern;
8009b50d902SRodney W. Grimes 	return (new);
8019b50d902SRodney W. Grimes }
8029b50d902SRodney W. Grimes 
8039b50d902SRodney W. Grimes /*
8049b50d902SRodney W. Grimes  * -newer file functions --
8059b50d902SRodney W. Grimes  *
8069b50d902SRodney W. Grimes  *	True if the current file has been modified more recently
8079b50d902SRodney W. Grimes  *	then the modification time of the file named by the pathname
8089b50d902SRodney W. Grimes  *	file.
8099b50d902SRodney W. Grimes  */
8109b50d902SRodney W. Grimes int
8119b50d902SRodney W. Grimes f_newer(plan, entry)
8129b50d902SRodney W. Grimes 	PLAN *plan;
8139b50d902SRodney W. Grimes 	FTSENT *entry;
8149b50d902SRodney W. Grimes {
8159b50d902SRodney W. Grimes 	return (entry->fts_statp->st_mtime > plan->t_data);
8169b50d902SRodney W. Grimes }
8179b50d902SRodney W. Grimes 
8189b50d902SRodney W. Grimes PLAN *
8199b50d902SRodney W. Grimes c_newer(filename)
8209b50d902SRodney W. Grimes 	char *filename;
8219b50d902SRodney W. Grimes {
8229b50d902SRodney W. Grimes 	PLAN *new;
8239b50d902SRodney W. Grimes 	struct stat sb;
8249b50d902SRodney W. Grimes 
8259b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
8269b50d902SRodney W. Grimes 
8279b50d902SRodney W. Grimes 	if (stat(filename, &sb))
8289b50d902SRodney W. Grimes 		err(1, "%s", filename);
8299b50d902SRodney W. Grimes 	new = palloc(N_NEWER, f_newer);
8309b50d902SRodney W. Grimes 	new->t_data = sb.st_mtime;
8319b50d902SRodney W. Grimes 	return (new);
8329b50d902SRodney W. Grimes }
8339b50d902SRodney W. Grimes 
8349b50d902SRodney W. Grimes /*
8359b50d902SRodney W. Grimes  * -nogroup functions --
8369b50d902SRodney W. Grimes  *
8379b50d902SRodney W. Grimes  *	True if file belongs to a user ID for which the equivalent
8389b50d902SRodney W. Grimes  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
8399b50d902SRodney W. Grimes  */
8409b50d902SRodney W. Grimes int
8419b50d902SRodney W. Grimes f_nogroup(plan, entry)
8429b50d902SRodney W. Grimes 	PLAN *plan;
8439b50d902SRodney W. Grimes 	FTSENT *entry;
8449b50d902SRodney W. Grimes {
8459b50d902SRodney W. Grimes 	char *group_from_gid();
8469b50d902SRodney W. Grimes 
8479b50d902SRodney W. Grimes 	return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
8489b50d902SRodney W. Grimes }
8499b50d902SRodney W. Grimes 
8509b50d902SRodney W. Grimes PLAN *
8519b50d902SRodney W. Grimes c_nogroup()
8529b50d902SRodney W. Grimes {
8539b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
8549b50d902SRodney W. Grimes 
8559b50d902SRodney W. Grimes 	return (palloc(N_NOGROUP, f_nogroup));
8569b50d902SRodney W. Grimes }
8579b50d902SRodney W. Grimes 
8589b50d902SRodney W. Grimes /*
8599b50d902SRodney W. Grimes  * -nouser functions --
8609b50d902SRodney W. Grimes  *
8619b50d902SRodney W. Grimes  *	True if file belongs to a user ID for which the equivalent
8629b50d902SRodney W. Grimes  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
8639b50d902SRodney W. Grimes  */
8649b50d902SRodney W. Grimes int
8659b50d902SRodney W. Grimes f_nouser(plan, entry)
8669b50d902SRodney W. Grimes 	PLAN *plan;
8679b50d902SRodney W. Grimes 	FTSENT *entry;
8689b50d902SRodney W. Grimes {
8699b50d902SRodney W. Grimes 	char *user_from_uid();
8709b50d902SRodney W. Grimes 
8719b50d902SRodney W. Grimes 	return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
8729b50d902SRodney W. Grimes }
8739b50d902SRodney W. Grimes 
8749b50d902SRodney W. Grimes PLAN *
8759b50d902SRodney W. Grimes c_nouser()
8769b50d902SRodney W. Grimes {
8779b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
8789b50d902SRodney W. Grimes 
8799b50d902SRodney W. Grimes 	return (palloc(N_NOUSER, f_nouser));
8809b50d902SRodney W. Grimes }
8819b50d902SRodney W. Grimes 
8829b50d902SRodney W. Grimes /*
8839b50d902SRodney W. Grimes  * -path functions --
8849b50d902SRodney W. Grimes  *
8859b50d902SRodney W. Grimes  *	True if the path of the filename being examined
8869b50d902SRodney W. Grimes  *	matches pattern using Pattern Matching Notation S3.14
8879b50d902SRodney W. Grimes  */
8889b50d902SRodney W. Grimes int
8899b50d902SRodney W. Grimes f_path(plan, entry)
8909b50d902SRodney W. Grimes 	PLAN *plan;
8919b50d902SRodney W. Grimes 	FTSENT *entry;
8929b50d902SRodney W. Grimes {
8939b50d902SRodney W. Grimes 	return (!fnmatch(plan->c_data, entry->fts_path, 0));
8949b50d902SRodney W. Grimes }
8959b50d902SRodney W. Grimes 
8969b50d902SRodney W. Grimes PLAN *
8979b50d902SRodney W. Grimes c_path(pattern)
8989b50d902SRodney W. Grimes 	char *pattern;
8999b50d902SRodney W. Grimes {
9009b50d902SRodney W. Grimes 	PLAN *new;
9019b50d902SRodney W. Grimes 
9029b50d902SRodney W. Grimes 	new = palloc(N_NAME, f_path);
9039b50d902SRodney W. Grimes 	new->c_data = pattern;
9049b50d902SRodney W. Grimes 	return (new);
9059b50d902SRodney W. Grimes }
9069b50d902SRodney W. Grimes 
9079b50d902SRodney W. Grimes /*
9089b50d902SRodney W. Grimes  * -perm functions --
9099b50d902SRodney W. Grimes  *
9109b50d902SRodney W. Grimes  *	The mode argument is used to represent file mode bits.  If it starts
9119b50d902SRodney W. Grimes  *	with a leading digit, it's treated as an octal mode, otherwise as a
9129b50d902SRodney W. Grimes  *	symbolic mode.
9139b50d902SRodney W. Grimes  */
9149b50d902SRodney W. Grimes int
9159b50d902SRodney W. Grimes f_perm(plan, entry)
9169b50d902SRodney W. Grimes 	PLAN *plan;
9179b50d902SRodney W. Grimes 	FTSENT *entry;
9189b50d902SRodney W. Grimes {
9199b50d902SRodney W. Grimes 	mode_t mode;
9209b50d902SRodney W. Grimes 
9219b50d902SRodney W. Grimes 	mode = entry->fts_statp->st_mode &
9229b50d902SRodney W. Grimes 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
9239b50d902SRodney W. Grimes 	if (plan->flags == F_ATLEAST)
9249b50d902SRodney W. Grimes 		return ((plan->m_data | mode) == mode);
9259b50d902SRodney W. Grimes 	else
9269b50d902SRodney W. Grimes 		return (mode == plan->m_data);
9279b50d902SRodney W. Grimes 	/* NOTREACHED */
9289b50d902SRodney W. Grimes }
9299b50d902SRodney W. Grimes 
9309b50d902SRodney W. Grimes PLAN *
9319b50d902SRodney W. Grimes c_perm(perm)
9329b50d902SRodney W. Grimes 	char *perm;
9339b50d902SRodney W. Grimes {
9349b50d902SRodney W. Grimes 	PLAN *new;
9359b50d902SRodney W. Grimes 	mode_t *set;
9369b50d902SRodney W. Grimes 
9379b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
9389b50d902SRodney W. Grimes 
9399b50d902SRodney W. Grimes 	new = palloc(N_PERM, f_perm);
9409b50d902SRodney W. Grimes 
9419b50d902SRodney W. Grimes 	if (*perm == '-') {
9429b50d902SRodney W. Grimes 		new->flags = F_ATLEAST;
9439b50d902SRodney W. Grimes 		++perm;
9449b50d902SRodney W. Grimes 	}
9459b50d902SRodney W. Grimes 
9469b50d902SRodney W. Grimes 	if ((set = setmode(perm)) == NULL)
9479b50d902SRodney W. Grimes 		err(1, "-perm: %s: illegal mode string", perm);
9489b50d902SRodney W. Grimes 
9499b50d902SRodney W. Grimes 	new->m_data = getmode(set, 0);
9509b50d902SRodney W. Grimes 	return (new);
9519b50d902SRodney W. Grimes }
9529b50d902SRodney W. Grimes 
9539b50d902SRodney W. Grimes /*
9549b50d902SRodney W. Grimes  * -print functions --
9559b50d902SRodney W. Grimes  *
9569b50d902SRodney W. Grimes  *	Always true, causes the current pathame to be written to
9579b50d902SRodney W. Grimes  *	standard output.
9589b50d902SRodney W. Grimes  */
9599b50d902SRodney W. Grimes int
9609b50d902SRodney W. Grimes f_print(plan, entry)
9619b50d902SRodney W. Grimes 	PLAN *plan;
9629b50d902SRodney W. Grimes 	FTSENT *entry;
9639b50d902SRodney W. Grimes {
96481e236a0SGarrett Wollman 	(void)puts(entry->fts_path);
9659b50d902SRodney W. Grimes 	return (1);
9669b50d902SRodney W. Grimes }
9679b50d902SRodney W. Grimes 
9689b50d902SRodney W. Grimes PLAN *
9699b50d902SRodney W. Grimes c_print()
9709b50d902SRodney W. Grimes {
9719b50d902SRodney W. Grimes 	isoutput = 1;
9729b50d902SRodney W. Grimes 
9739b50d902SRodney W. Grimes 	return (palloc(N_PRINT, f_print));
9749b50d902SRodney W. Grimes }
9759b50d902SRodney W. Grimes 
9769b50d902SRodney W. Grimes /*
9777cd23434SGarrett Wollman  * -print0 functions --
9787cd23434SGarrett Wollman  *
9797cd23434SGarrett Wollman  *	Always true, causes the current pathame to be written to
9807cd23434SGarrett Wollman  *	standard output followed by a NUL character
9817cd23434SGarrett Wollman  */
9827cd23434SGarrett Wollman int
9837cd23434SGarrett Wollman f_print0(plan, entry)
9847cd23434SGarrett Wollman 	PLAN *plan;
9857cd23434SGarrett Wollman 	FTSENT *entry;
9867cd23434SGarrett Wollman {
9877cd23434SGarrett Wollman 	fputs(entry->fts_path, stdout);
9887cd23434SGarrett Wollman 	fputc('\0', stdout);
9897cd23434SGarrett Wollman 	return (1);
9907cd23434SGarrett Wollman }
9917cd23434SGarrett Wollman 
9927cd23434SGarrett Wollman PLAN *
9937cd23434SGarrett Wollman c_print0()
9947cd23434SGarrett Wollman {
9957cd23434SGarrett Wollman 	isoutput = 1;
9967cd23434SGarrett Wollman 
9977cd23434SGarrett Wollman 	return (palloc(N_PRINT0, f_print0));
9987cd23434SGarrett Wollman }
9997cd23434SGarrett Wollman 
10007cd23434SGarrett Wollman /*
10019b50d902SRodney W. Grimes  * -prune functions --
10029b50d902SRodney W. Grimes  *
10039b50d902SRodney W. Grimes  *	Prune a portion of the hierarchy.
10049b50d902SRodney W. Grimes  */
10059b50d902SRodney W. Grimes int
10069b50d902SRodney W. Grimes f_prune(plan, entry)
10079b50d902SRodney W. Grimes 	PLAN *plan;
10089b50d902SRodney W. Grimes 	FTSENT *entry;
10099b50d902SRodney W. Grimes {
10109b50d902SRodney W. Grimes 	extern FTS *tree;
10119b50d902SRodney W. Grimes 
10129b50d902SRodney W. Grimes 	if (fts_set(tree, entry, FTS_SKIP))
10139b50d902SRodney W. Grimes 		err(1, "%s", entry->fts_path);
10149b50d902SRodney W. Grimes 	return (1);
10159b50d902SRodney W. Grimes }
10169b50d902SRodney W. Grimes 
10179b50d902SRodney W. Grimes PLAN *
10189b50d902SRodney W. Grimes c_prune()
10199b50d902SRodney W. Grimes {
10209b50d902SRodney W. Grimes 	return (palloc(N_PRUNE, f_prune));
10219b50d902SRodney W. Grimes }
10229b50d902SRodney W. Grimes 
10239b50d902SRodney W. Grimes /*
10249b50d902SRodney W. Grimes  * -size n[c] functions --
10259b50d902SRodney W. Grimes  *
10269b50d902SRodney W. Grimes  *	True if the file size in bytes, divided by an implementation defined
10279b50d902SRodney W. Grimes  *	value and rounded up to the next integer, is n.  If n is followed by
10289b50d902SRodney W. Grimes  *	a c, the size is in bytes.
10299b50d902SRodney W. Grimes  */
10309b50d902SRodney W. Grimes #define	FIND_SIZE	512
10319b50d902SRodney W. Grimes static int divsize = 1;
10329b50d902SRodney W. Grimes 
10339b50d902SRodney W. Grimes int
10349b50d902SRodney W. Grimes f_size(plan, entry)
10359b50d902SRodney W. Grimes 	PLAN *plan;
10369b50d902SRodney W. Grimes 	FTSENT *entry;
10379b50d902SRodney W. Grimes {
10389b50d902SRodney W. Grimes 	off_t size;
10399b50d902SRodney W. Grimes 
10409b50d902SRodney W. Grimes 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
10419b50d902SRodney W. Grimes 	    FIND_SIZE : entry->fts_statp->st_size;
10429b50d902SRodney W. Grimes 	COMPARE(size, plan->o_data);
10439b50d902SRodney W. Grimes }
10449b50d902SRodney W. Grimes 
10459b50d902SRodney W. Grimes PLAN *
10469b50d902SRodney W. Grimes c_size(arg)
10479b50d902SRodney W. Grimes 	char *arg;
10489b50d902SRodney W. Grimes {
10499b50d902SRodney W. Grimes 	PLAN *new;
10509b50d902SRodney W. Grimes 	char endch;
10519b50d902SRodney W. Grimes 
10529b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
10539b50d902SRodney W. Grimes 
10549b50d902SRodney W. Grimes 	new = palloc(N_SIZE, f_size);
10559b50d902SRodney W. Grimes 	endch = 'c';
10569b50d902SRodney W. Grimes 	new->o_data = find_parsenum(new, "-size", arg, &endch);
10579b50d902SRodney W. Grimes 	if (endch == 'c')
10589b50d902SRodney W. Grimes 		divsize = 0;
10599b50d902SRodney W. Grimes 	return (new);
10609b50d902SRodney W. Grimes }
10619b50d902SRodney W. Grimes 
10629b50d902SRodney W. Grimes /*
10639b50d902SRodney W. Grimes  * -type c functions --
10649b50d902SRodney W. Grimes  *
1065841484cdSPeter Wemm  *	True if the type of the file is c, where c is b, c, d, p, f or w
1066841484cdSPeter Wemm  *	for block special file, character special file, directory, FIFO,
1067841484cdSPeter Wemm  *	regular file or whiteout respectively.
10689b50d902SRodney W. Grimes  */
10699b50d902SRodney W. Grimes int
10709b50d902SRodney W. Grimes f_type(plan, entry)
10719b50d902SRodney W. Grimes 	PLAN *plan;
10729b50d902SRodney W. Grimes 	FTSENT *entry;
10739b50d902SRodney W. Grimes {
10749b50d902SRodney W. Grimes 	return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
10759b50d902SRodney W. Grimes }
10769b50d902SRodney W. Grimes 
10779b50d902SRodney W. Grimes PLAN *
10789b50d902SRodney W. Grimes c_type(typestring)
10799b50d902SRodney W. Grimes 	char *typestring;
10809b50d902SRodney W. Grimes {
10819b50d902SRodney W. Grimes 	PLAN *new;
10829b50d902SRodney W. Grimes 	mode_t  mask;
10839b50d902SRodney W. Grimes 
10849b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
10859b50d902SRodney W. Grimes 
10869b50d902SRodney W. Grimes 	switch (typestring[0]) {
10879b50d902SRodney W. Grimes 	case 'b':
10889b50d902SRodney W. Grimes 		mask = S_IFBLK;
10899b50d902SRodney W. Grimes 		break;
10909b50d902SRodney W. Grimes 	case 'c':
10919b50d902SRodney W. Grimes 		mask = S_IFCHR;
10929b50d902SRodney W. Grimes 		break;
10939b50d902SRodney W. Grimes 	case 'd':
10949b50d902SRodney W. Grimes 		mask = S_IFDIR;
10959b50d902SRodney W. Grimes 		break;
10969b50d902SRodney W. Grimes 	case 'f':
10979b50d902SRodney W. Grimes 		mask = S_IFREG;
10989b50d902SRodney W. Grimes 		break;
10999b50d902SRodney W. Grimes 	case 'l':
11009b50d902SRodney W. Grimes 		mask = S_IFLNK;
11019b50d902SRodney W. Grimes 		break;
11029b50d902SRodney W. Grimes 	case 'p':
11039b50d902SRodney W. Grimes 		mask = S_IFIFO;
11049b50d902SRodney W. Grimes 		break;
11059b50d902SRodney W. Grimes 	case 's':
11069b50d902SRodney W. Grimes 		mask = S_IFSOCK;
11079b50d902SRodney W. Grimes 		break;
1108841484cdSPeter Wemm #ifdef FTS_WHITEOUT
1109841484cdSPeter Wemm 	case 'w':
1110841484cdSPeter Wemm 		mask = S_IFWHT;
1111841484cdSPeter Wemm 		ftsoptions |= FTS_WHITEOUT;
1112841484cdSPeter Wemm 		break;
1113841484cdSPeter Wemm #endif /* FTS_WHITEOUT */
11149b50d902SRodney W. Grimes 	default:
11159b50d902SRodney W. Grimes 		errx(1, "-type: %s: unknown type", typestring);
11169b50d902SRodney W. Grimes 	}
11179b50d902SRodney W. Grimes 
11189b50d902SRodney W. Grimes 	new = palloc(N_TYPE, f_type);
11199b50d902SRodney W. Grimes 	new->m_data = mask;
11209b50d902SRodney W. Grimes 	return (new);
11219b50d902SRodney W. Grimes }
11229b50d902SRodney W. Grimes 
11239b50d902SRodney W. Grimes /*
1124abacbbbfSPeter Wemm  * -delete functions --
1125abacbbbfSPeter Wemm  *
1126abacbbbfSPeter Wemm  *	True always.  Makes it's best shot and continues on regardless.
1127abacbbbfSPeter Wemm  */
1128abacbbbfSPeter Wemm int
1129abacbbbfSPeter Wemm f_delete(plan, entry)
1130abacbbbfSPeter Wemm 	PLAN *plan;
1131abacbbbfSPeter Wemm 	FTSENT *entry;
1132abacbbbfSPeter Wemm {
1133abacbbbfSPeter Wemm 	/* ignore these from fts */
1134abacbbbfSPeter Wemm 	if (strcmp(entry->fts_accpath, ".") == 0 ||
1135abacbbbfSPeter Wemm 	    strcmp(entry->fts_accpath, "..") == 0)
1136abacbbbfSPeter Wemm 		return (1);
1137abacbbbfSPeter Wemm 
1138abacbbbfSPeter Wemm 	/* sanity check */
1139abacbbbfSPeter Wemm 	if (isdepth == 0 ||			/* depth off */
1140abacbbbfSPeter Wemm 	    (ftsoptions & FTS_NOSTAT) ||	/* not stat()ing */
1141abacbbbfSPeter Wemm 	    !(ftsoptions & FTS_PHYSICAL) ||	/* physical off */
1142abacbbbfSPeter Wemm 	    (ftsoptions & FTS_LOGICAL))		/* or finally, logical on */
1143abacbbbfSPeter Wemm 		errx(1, "-delete: insecure options got turned on");
1144abacbbbfSPeter Wemm 
1145abacbbbfSPeter Wemm 	/* Potentially unsafe - do not accept relative paths whatsoever */
1146abacbbbfSPeter Wemm 	if (strchr(entry->fts_accpath, '/') != NULL)
1147abacbbbfSPeter Wemm 		errx(1, "-delete: %s: relative path potentially not safe",
1148abacbbbfSPeter Wemm 			entry->fts_accpath);
1149abacbbbfSPeter Wemm 
1150242ab807SPeter Wemm 	/* Turn off user immutable bits if running as root */
1151242ab807SPeter Wemm 	if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
1152242ab807SPeter Wemm 	    !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
1153242ab807SPeter Wemm 	    geteuid() == 0)
1154242ab807SPeter Wemm 		chflags(entry->fts_accpath,
1155242ab807SPeter Wemm 		       entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
1156242ab807SPeter Wemm 
1157abacbbbfSPeter Wemm 	/* rmdir directories, unlink everything else */
1158abacbbbfSPeter Wemm 	if (S_ISDIR(entry->fts_statp->st_mode)) {
11593598e52cSPeter Wemm 		if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
1160abacbbbfSPeter Wemm 			warn("-delete: rmdir(%s)", entry->fts_path);
1161abacbbbfSPeter Wemm 	} else {
1162abacbbbfSPeter Wemm 		if (unlink(entry->fts_accpath) < 0)
1163abacbbbfSPeter Wemm 			warn("-delete: unlink(%s)", entry->fts_path);
1164abacbbbfSPeter Wemm 	}
1165abacbbbfSPeter Wemm 
1166abacbbbfSPeter Wemm 	/* "succeed" */
1167abacbbbfSPeter Wemm 	return (1);
1168abacbbbfSPeter Wemm }
1169abacbbbfSPeter Wemm 
1170abacbbbfSPeter Wemm PLAN *
1171abacbbbfSPeter Wemm c_delete()
1172abacbbbfSPeter Wemm {
1173abacbbbfSPeter Wemm 
1174abacbbbfSPeter Wemm 	ftsoptions &= ~FTS_NOSTAT;	/* no optimise */
1175abacbbbfSPeter Wemm 	ftsoptions |= FTS_PHYSICAL;	/* disable -follow */
1176abacbbbfSPeter Wemm 	ftsoptions &= ~FTS_LOGICAL;	/* disable -follow */
1177abacbbbfSPeter Wemm 	isoutput = 1;			/* possible output */
1178abacbbbfSPeter Wemm 	isdepth = 1;			/* -depth implied */
1179abacbbbfSPeter Wemm 
1180abacbbbfSPeter Wemm 	return (palloc(N_DELETE, f_delete));
1181abacbbbfSPeter Wemm }
1182abacbbbfSPeter Wemm 
1183abacbbbfSPeter Wemm /*
11849b50d902SRodney W. Grimes  * -user uname functions --
11859b50d902SRodney W. Grimes  *
11869b50d902SRodney W. Grimes  *	True if the file belongs to the user uname.  If uname is numeric and
11879b50d902SRodney W. Grimes  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
11889b50d902SRodney W. Grimes  *	return a valid user name, uname is taken as a user ID.
11899b50d902SRodney W. Grimes  */
11909b50d902SRodney W. Grimes int
11919b50d902SRodney W. Grimes f_user(plan, entry)
11929b50d902SRodney W. Grimes 	PLAN *plan;
11939b50d902SRodney W. Grimes 	FTSENT *entry;
11949b50d902SRodney W. Grimes {
11959b50d902SRodney W. Grimes 	return (entry->fts_statp->st_uid == plan->u_data);
11969b50d902SRodney W. Grimes }
11979b50d902SRodney W. Grimes 
11989b50d902SRodney W. Grimes PLAN *
11999b50d902SRodney W. Grimes c_user(username)
12009b50d902SRodney W. Grimes 	char *username;
12019b50d902SRodney W. Grimes {
12029b50d902SRodney W. Grimes 	PLAN *new;
12039b50d902SRodney W. Grimes 	struct passwd *p;
12049b50d902SRodney W. Grimes 	uid_t uid;
12059b50d902SRodney W. Grimes 
12069b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
12079b50d902SRodney W. Grimes 
12089b50d902SRodney W. Grimes 	p = getpwnam(username);
12099b50d902SRodney W. Grimes 	if (p == NULL) {
12109b50d902SRodney W. Grimes 		uid = atoi(username);
12119b50d902SRodney W. Grimes 		if (uid == 0 && username[0] != '0')
12129b50d902SRodney W. Grimes 			errx(1, "-user: %s: no such user", username);
12139b50d902SRodney W. Grimes 	} else
12149b50d902SRodney W. Grimes 		uid = p->pw_uid;
12159b50d902SRodney W. Grimes 
12169b50d902SRodney W. Grimes 	new = palloc(N_USER, f_user);
12179b50d902SRodney W. Grimes 	new->u_data = uid;
12189b50d902SRodney W. Grimes 	return (new);
12199b50d902SRodney W. Grimes }
12209b50d902SRodney W. Grimes 
12219b50d902SRodney W. Grimes /*
12229b50d902SRodney W. Grimes  * -xdev functions --
12239b50d902SRodney W. Grimes  *
12249b50d902SRodney W. Grimes  *	Always true, causes find not to decend past directories that have a
12259b50d902SRodney W. Grimes  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
12269b50d902SRodney W. Grimes  */
12279b50d902SRodney W. Grimes PLAN *
12289b50d902SRodney W. Grimes c_xdev()
12299b50d902SRodney W. Grimes {
12309b50d902SRodney W. Grimes 	ftsoptions |= FTS_XDEV;
12319b50d902SRodney W. Grimes 
12329b50d902SRodney W. Grimes 	return (palloc(N_XDEV, f_always_true));
12339b50d902SRodney W. Grimes }
12349b50d902SRodney W. Grimes 
12359b50d902SRodney W. Grimes /*
12369b50d902SRodney W. Grimes  * ( expression ) functions --
12379b50d902SRodney W. Grimes  *
12389b50d902SRodney W. Grimes  *	True if expression is true.
12399b50d902SRodney W. Grimes  */
12409b50d902SRodney W. Grimes int
12419b50d902SRodney W. Grimes f_expr(plan, entry)
12429b50d902SRodney W. Grimes 	PLAN *plan;
12439b50d902SRodney W. Grimes 	FTSENT *entry;
12449b50d902SRodney W. Grimes {
12459b50d902SRodney W. Grimes 	register PLAN *p;
12469b50d902SRodney W. Grimes 	register int state;
12479b50d902SRodney W. Grimes 
12489b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
12499b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
12509b50d902SRodney W. Grimes 	return (state);
12519b50d902SRodney W. Grimes }
12529b50d902SRodney W. Grimes 
12539b50d902SRodney W. Grimes /*
12549b50d902SRodney W. Grimes  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
12559b50d902SRodney W. Grimes  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
12569b50d902SRodney W. Grimes  * to a N_EXPR node containing the expression and the ')' node is discarded.
12579b50d902SRodney W. Grimes  */
12589b50d902SRodney W. Grimes PLAN *
12599b50d902SRodney W. Grimes c_openparen()
12609b50d902SRodney W. Grimes {
12619b50d902SRodney W. Grimes 	return (palloc(N_OPENPAREN, (int (*)())-1));
12629b50d902SRodney W. Grimes }
12639b50d902SRodney W. Grimes 
12649b50d902SRodney W. Grimes PLAN *
12659b50d902SRodney W. Grimes c_closeparen()
12669b50d902SRodney W. Grimes {
12679b50d902SRodney W. Grimes 	return (palloc(N_CLOSEPAREN, (int (*)())-1));
12689b50d902SRodney W. Grimes }
12699b50d902SRodney W. Grimes 
12709b50d902SRodney W. Grimes /*
12719b50d902SRodney W. Grimes  * ! expression functions --
12729b50d902SRodney W. Grimes  *
12739b50d902SRodney W. Grimes  *	Negation of a primary; the unary NOT operator.
12749b50d902SRodney W. Grimes  */
12759b50d902SRodney W. Grimes int
12769b50d902SRodney W. Grimes f_not(plan, entry)
12779b50d902SRodney W. Grimes 	PLAN *plan;
12789b50d902SRodney W. Grimes 	FTSENT *entry;
12799b50d902SRodney W. Grimes {
12809b50d902SRodney W. Grimes 	register PLAN *p;
12819b50d902SRodney W. Grimes 	register int state;
12829b50d902SRodney W. Grimes 
12839b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
12849b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
12859b50d902SRodney W. Grimes 	return (!state);
12869b50d902SRodney W. Grimes }
12879b50d902SRodney W. Grimes 
12889b50d902SRodney W. Grimes PLAN *
12899b50d902SRodney W. Grimes c_not()
12909b50d902SRodney W. Grimes {
12919b50d902SRodney W. Grimes 	return (palloc(N_NOT, f_not));
12929b50d902SRodney W. Grimes }
12939b50d902SRodney W. Grimes 
12949b50d902SRodney W. Grimes /*
12959b50d902SRodney W. Grimes  * expression -o expression functions --
12969b50d902SRodney W. Grimes  *
12979b50d902SRodney W. Grimes  *	Alternation of primaries; the OR operator.  The second expression is
12989b50d902SRodney W. Grimes  * not evaluated if the first expression is true.
12999b50d902SRodney W. Grimes  */
13009b50d902SRodney W. Grimes int
13019b50d902SRodney W. Grimes f_or(plan, entry)
13029b50d902SRodney W. Grimes 	PLAN *plan;
13039b50d902SRodney W. Grimes 	FTSENT *entry;
13049b50d902SRodney W. Grimes {
13059b50d902SRodney W. Grimes 	register PLAN *p;
13069b50d902SRodney W. Grimes 	register int state;
13079b50d902SRodney W. Grimes 
13089b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
13099b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
13109b50d902SRodney W. Grimes 
13119b50d902SRodney W. Grimes 	if (state)
13129b50d902SRodney W. Grimes 		return (1);
13139b50d902SRodney W. Grimes 
13149b50d902SRodney W. Grimes 	for (p = plan->p_data[1];
13159b50d902SRodney W. Grimes 	    p && (state = (p->eval)(p, entry)); p = p->next);
13169b50d902SRodney W. Grimes 	return (state);
13179b50d902SRodney W. Grimes }
13189b50d902SRodney W. Grimes 
13199b50d902SRodney W. Grimes PLAN *
13209b50d902SRodney W. Grimes c_or()
13219b50d902SRodney W. Grimes {
13229b50d902SRodney W. Grimes 	return (palloc(N_OR, f_or));
13239b50d902SRodney W. Grimes }
13249b50d902SRodney W. Grimes 
13259b50d902SRodney W. Grimes static PLAN *
13269b50d902SRodney W. Grimes palloc(t, f)
13279b50d902SRodney W. Grimes 	enum ntype t;
13289b50d902SRodney W. Grimes 	int (*f) __P((PLAN *, FTSENT *));
13299b50d902SRodney W. Grimes {
13309b50d902SRodney W. Grimes 	PLAN *new;
13319b50d902SRodney W. Grimes 
13329b50d902SRodney W. Grimes 	if ((new = malloc(sizeof(PLAN))) == NULL)
13339b50d902SRodney W. Grimes 		err(1, NULL);
13349b50d902SRodney W. Grimes 	new->type = t;
13359b50d902SRodney W. Grimes 	new->eval = f;
13369b50d902SRodney W. Grimes 	new->flags = 0;
13379b50d902SRodney W. Grimes 	new->next = NULL;
13389b50d902SRodney W. Grimes 	return (new);
13399b50d902SRodney W. Grimes }
1340