xref: /freebsd/usr.bin/find/function.c (revision 9c5d31dff2f1e62c01964e9959f997fe1b6169d2)
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
38c76bc8f3SOllivier Robert #if 0
39c76bc8f3SOllivier Robert static const char sccsid[] = "@(#)function.c	8.10 (Berkeley) 5/4/95";
40c76bc8f3SOllivier Robert #endif
419b50d902SRodney W. Grimes #endif /* not lint */
42ef646f18SMark Murray 
433077469eSDavid E. O'Brien #include <sys/cdefs.h>
443077469eSDavid E. O'Brien __FBSDID("$FreeBSD$");
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #include <sys/param.h>
479b50d902SRodney W. Grimes #include <sys/ucred.h>
489b50d902SRodney W. Grimes #include <sys/stat.h>
499c5d31dfSBosko Milekic #include <sys/types.h>
509c5d31dfSBosko Milekic #include <sys/acl.h>
519b50d902SRodney W. Grimes #include <sys/wait.h>
529b50d902SRodney W. Grimes #include <sys/mount.h>
53ea92232aSPoul-Henning Kamp #include <sys/timeb.h>
549b50d902SRodney W. Grimes 
55ed1a4621SPeter Wemm #include <dirent.h>
569b50d902SRodney W. Grimes #include <err.h>
579b50d902SRodney W. Grimes #include <errno.h>
589b50d902SRodney W. Grimes #include <fnmatch.h>
599b50d902SRodney W. Grimes #include <fts.h>
609b50d902SRodney W. Grimes #include <grp.h>
615e25d888STim J. Robbins #include <limits.h>
629b50d902SRodney W. Grimes #include <pwd.h>
637c1d4b3aSAkinori MUSHA #include <regex.h>
649b50d902SRodney W. Grimes #include <stdio.h>
659b50d902SRodney W. Grimes #include <stdlib.h>
669b50d902SRodney W. Grimes #include <string.h>
679b50d902SRodney W. Grimes #include <unistd.h>
689b50d902SRodney W. Grimes 
699b50d902SRodney W. Grimes #include "find.h"
709b50d902SRodney W. Grimes 
71ecca1f1cSMark Murray static PLAN *palloc(OPTION *);
72ecca1f1cSMark Murray static long long find_parsenum(PLAN *, const char *, char *, char *);
73ecca1f1cSMark Murray static long long find_parsetime(PLAN *, const char *, char *);
74ecca1f1cSMark Murray static char *nextarg(OPTION *, char ***);
75ea92232aSPoul-Henning Kamp 
76a07af811STim J. Robbins extern char **environ;
77a07af811STim J. Robbins 
78adff4fcaSRuslan Ermilov #define	COMPARE(a, b) do {						\
79ea92232aSPoul-Henning Kamp 	switch (plan->flags & F_ELG_MASK) {				\
809b50d902SRodney W. Grimes 	case F_EQUAL:							\
819b50d902SRodney W. Grimes 		return (a == b);					\
829b50d902SRodney W. Grimes 	case F_LESSTHAN:						\
839b50d902SRodney W. Grimes 		return (a < b);						\
849b50d902SRodney W. Grimes 	case F_GREATER:							\
859b50d902SRodney W. Grimes 		return (a > b);						\
869b50d902SRodney W. Grimes 	default:							\
879b50d902SRodney W. Grimes 		abort();						\
889b50d902SRodney W. Grimes 	}								\
89adff4fcaSRuslan Ermilov } while(0)
909b50d902SRodney W. Grimes 
91ea92232aSPoul-Henning Kamp static PLAN *
92ef646f18SMark Murray palloc(OPTION *option)
93ea92232aSPoul-Henning Kamp {
94ea92232aSPoul-Henning Kamp 	PLAN *new;
95ea92232aSPoul-Henning Kamp 
96ea92232aSPoul-Henning Kamp 	if ((new = malloc(sizeof(PLAN))) == NULL)
97ea92232aSPoul-Henning Kamp 		err(1, NULL);
98ea92232aSPoul-Henning Kamp 	new->execute = option->execute;
99ea92232aSPoul-Henning Kamp 	new->flags = option->flags;
100ea92232aSPoul-Henning Kamp 	new->next = NULL;
101ea92232aSPoul-Henning Kamp 	return new;
102ea92232aSPoul-Henning Kamp }
1039b50d902SRodney W. Grimes 
1049b50d902SRodney W. Grimes /*
1059b50d902SRodney W. Grimes  * find_parsenum --
1069b50d902SRodney W. Grimes  *	Parse a string of the form [+-]# and return the value.
1079b50d902SRodney W. Grimes  */
1089192bbf4SBruce Evans static long long
109ef646f18SMark Murray find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
1109b50d902SRodney W. Grimes {
1119192bbf4SBruce Evans 	long long value;
1129b50d902SRodney W. Grimes 	char *endchar, *str;	/* Pointer to character ending conversion. */
1139b50d902SRodney W. Grimes 
1149b50d902SRodney W. Grimes 	/* Determine comparison from leading + or -. */
1159b50d902SRodney W. Grimes 	str = vp;
1169b50d902SRodney W. Grimes 	switch (*str) {
1179b50d902SRodney W. Grimes 	case '+':
1189b50d902SRodney W. Grimes 		++str;
119ea92232aSPoul-Henning Kamp 		plan->flags |= F_GREATER;
1209b50d902SRodney W. Grimes 		break;
1219b50d902SRodney W. Grimes 	case '-':
1229b50d902SRodney W. Grimes 		++str;
123ea92232aSPoul-Henning Kamp 		plan->flags |= F_LESSTHAN;
1249b50d902SRodney W. Grimes 		break;
1259b50d902SRodney W. Grimes 	default:
126ea92232aSPoul-Henning Kamp 		plan->flags |= F_EQUAL;
1279b50d902SRodney W. Grimes 		break;
1289b50d902SRodney W. Grimes 	}
1299b50d902SRodney W. Grimes 
1309b50d902SRodney W. Grimes 	/*
1319192bbf4SBruce Evans 	 * Convert the string with strtoq().  Note, if strtoq() returns zero
1329b50d902SRodney W. Grimes 	 * and endchar points to the beginning of the string we know we have
1339b50d902SRodney W. Grimes 	 * a syntax error.
1349b50d902SRodney W. Grimes 	 */
1359192bbf4SBruce Evans 	value = strtoq(str, &endchar, 10);
1369b50d902SRodney W. Grimes 	if (value == 0 && endchar == str)
1379b50d902SRodney W. Grimes 		errx(1, "%s: %s: illegal numeric value", option, vp);
1389b50d902SRodney W. Grimes 	if (endchar[0] && (endch == NULL || endchar[0] != *endch))
1399b50d902SRodney W. Grimes 		errx(1, "%s: %s: illegal trailing character", option, vp);
1409b50d902SRodney W. Grimes 	if (endch)
1419b50d902SRodney W. Grimes 		*endch = endchar[0];
142ea92232aSPoul-Henning Kamp 	return value;
1439b50d902SRodney W. Grimes }
1449b50d902SRodney W. Grimes 
1459b50d902SRodney W. Grimes /*
146adff4fcaSRuslan Ermilov  * find_parsetime --
147adff4fcaSRuslan Ermilov  *	Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
148adff4fcaSRuslan Ermilov  */
149adff4fcaSRuslan Ermilov static long long
150ef646f18SMark Murray find_parsetime(PLAN *plan, const char *option, char *vp)
151adff4fcaSRuslan Ermilov {
152adff4fcaSRuslan Ermilov 	long long secs, value;
153adff4fcaSRuslan Ermilov 	char *str, *unit;	/* Pointer to character ending conversion. */
154adff4fcaSRuslan Ermilov 
155adff4fcaSRuslan Ermilov 	/* Determine comparison from leading + or -. */
156adff4fcaSRuslan Ermilov 	str = vp;
157adff4fcaSRuslan Ermilov 	switch (*str) {
158adff4fcaSRuslan Ermilov 	case '+':
159adff4fcaSRuslan Ermilov 		++str;
160adff4fcaSRuslan Ermilov 		plan->flags |= F_GREATER;
161adff4fcaSRuslan Ermilov 		break;
162adff4fcaSRuslan Ermilov 	case '-':
163adff4fcaSRuslan Ermilov 		++str;
164adff4fcaSRuslan Ermilov 		plan->flags |= F_LESSTHAN;
165adff4fcaSRuslan Ermilov 		break;
166adff4fcaSRuslan Ermilov 	default:
167adff4fcaSRuslan Ermilov 		plan->flags |= F_EQUAL;
168adff4fcaSRuslan Ermilov 		break;
169adff4fcaSRuslan Ermilov 	}
170adff4fcaSRuslan Ermilov 
171adff4fcaSRuslan Ermilov 	value = strtoq(str, &unit, 10);
172adff4fcaSRuslan Ermilov 	if (value == 0 && unit == str) {
173adff4fcaSRuslan Ermilov 		errx(1, "%s: %s: illegal time value", option, vp);
174adff4fcaSRuslan Ermilov 		/* NOTREACHED */
175adff4fcaSRuslan Ermilov 	}
176adff4fcaSRuslan Ermilov 	if (*unit == '\0')
177adff4fcaSRuslan Ermilov 		return value;
178adff4fcaSRuslan Ermilov 
179adff4fcaSRuslan Ermilov 	/* Units syntax. */
180adff4fcaSRuslan Ermilov 	secs = 0;
181adff4fcaSRuslan Ermilov 	for (;;) {
182adff4fcaSRuslan Ermilov 		switch(*unit) {
183adff4fcaSRuslan Ermilov 		case 's':	/* seconds */
184adff4fcaSRuslan Ermilov 			secs += value;
185adff4fcaSRuslan Ermilov 			break;
186adff4fcaSRuslan Ermilov 		case 'm':	/* minutes */
187adff4fcaSRuslan Ermilov 			secs += value * 60;
188adff4fcaSRuslan Ermilov 			break;
189adff4fcaSRuslan Ermilov 		case 'h':	/* hours */
190adff4fcaSRuslan Ermilov 			secs += value * 3600;
191adff4fcaSRuslan Ermilov 			break;
192adff4fcaSRuslan Ermilov 		case 'd':	/* days */
193adff4fcaSRuslan Ermilov 			secs += value * 86400;
194adff4fcaSRuslan Ermilov 			break;
195adff4fcaSRuslan Ermilov 		case 'w':	/* weeks */
196adff4fcaSRuslan Ermilov 			secs += value * 604800;
197adff4fcaSRuslan Ermilov 			break;
198adff4fcaSRuslan Ermilov 		default:
199adff4fcaSRuslan Ermilov 			errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
200adff4fcaSRuslan Ermilov 			/* NOTREACHED */
201adff4fcaSRuslan Ermilov 		}
202adff4fcaSRuslan Ermilov 		str = unit + 1;
203adff4fcaSRuslan Ermilov 		if (*str == '\0')	/* EOS */
204adff4fcaSRuslan Ermilov 			break;
205adff4fcaSRuslan Ermilov 		value = strtoq(str, &unit, 10);
206adff4fcaSRuslan Ermilov 		if (value == 0 && unit == str) {
207adff4fcaSRuslan Ermilov 			errx(1, "%s: %s: illegal time value", option, vp);
208adff4fcaSRuslan Ermilov 			/* NOTREACHED */
209adff4fcaSRuslan Ermilov 		}
210adff4fcaSRuslan Ermilov 		if (*unit == '\0') {
211adff4fcaSRuslan Ermilov 			errx(1, "%s: %s: missing trailing unit", option, vp);
212adff4fcaSRuslan Ermilov 			/* NOTREACHED */
213adff4fcaSRuslan Ermilov 		}
214adff4fcaSRuslan Ermilov 	}
215adff4fcaSRuslan Ermilov 	plan->flags |= F_EXACTTIME;
216adff4fcaSRuslan Ermilov 	return secs;
217adff4fcaSRuslan Ermilov }
218adff4fcaSRuslan Ermilov 
219adff4fcaSRuslan Ermilov /*
220ea92232aSPoul-Henning Kamp  * nextarg --
221ea92232aSPoul-Henning Kamp  *	Check that another argument still exists, return a pointer to it,
222ea92232aSPoul-Henning Kamp  *	and increment the argument vector pointer.
223ea92232aSPoul-Henning Kamp  */
224ea92232aSPoul-Henning Kamp static char *
225ef646f18SMark Murray nextarg(OPTION *option, char ***argvp)
226ea92232aSPoul-Henning Kamp {
227ea92232aSPoul-Henning Kamp 	char *arg;
228ea92232aSPoul-Henning Kamp 
229ea92232aSPoul-Henning Kamp 	if ((arg = **argvp) == 0)
230ea92232aSPoul-Henning Kamp 		errx(1, "%s: requires additional arguments", option->name);
231ea92232aSPoul-Henning Kamp 	(*argvp)++;
232ea92232aSPoul-Henning Kamp 	return arg;
233ea92232aSPoul-Henning Kamp } /* nextarg() */
234ea92232aSPoul-Henning Kamp 
235ea92232aSPoul-Henning Kamp /*
2369b50d902SRodney W. Grimes  * The value of n for the inode times (atime, ctime, and mtime) is a range,
2379b50d902SRodney W. Grimes  * i.e. n matches from (n - 1) to n 24 hour periods.  This interacts with
2389b50d902SRodney W. Grimes  * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
2399b50d902SRodney W. Grimes  * user wanted.  Correct so that -1 is "less than 1".
2409b50d902SRodney W. Grimes  */
241ea92232aSPoul-Henning Kamp #define	TIME_CORRECT(p) \
242ea92232aSPoul-Henning Kamp 	if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
2439b50d902SRodney W. Grimes 		++((p)->t_data);
2449b50d902SRodney W. Grimes 
2459b50d902SRodney W. Grimes /*
246ea92232aSPoul-Henning Kamp  * -[acm]min n functions --
2473f5223f8SWolfram Schneider  *
248ea92232aSPoul-Henning Kamp  *    True if the difference between the
249ea92232aSPoul-Henning Kamp  *		file access time (-amin)
250ea92232aSPoul-Henning Kamp  *		last change of file status information (-cmin)
251ea92232aSPoul-Henning Kamp  *		file modification time (-mmin)
252ea92232aSPoul-Henning Kamp  *    and the current time is n min periods.
2533f5223f8SWolfram Schneider  */
2543f5223f8SWolfram Schneider int
255ef646f18SMark Murray f_Xmin(PLAN *plan, FTSENT *entry)
2563f5223f8SWolfram Schneider {
257ea92232aSPoul-Henning Kamp 	if (plan->flags & F_TIME_C) {
2583f5223f8SWolfram Schneider 		COMPARE((now - entry->fts_statp->st_ctime +
2593f5223f8SWolfram Schneider 		    60 - 1) / 60, plan->t_data);
260ea92232aSPoul-Henning Kamp 	} else if (plan->flags & F_TIME_A) {
261ea92232aSPoul-Henning Kamp 		COMPARE((now - entry->fts_statp->st_atime +
262ea92232aSPoul-Henning Kamp 		    60 - 1) / 60, plan->t_data);
263ea92232aSPoul-Henning Kamp 	} else {
264ea92232aSPoul-Henning Kamp 		COMPARE((now - entry->fts_statp->st_mtime +
265ea92232aSPoul-Henning Kamp 		    60 - 1) / 60, plan->t_data);
266ea92232aSPoul-Henning Kamp 	}
2673f5223f8SWolfram Schneider }
2683f5223f8SWolfram Schneider 
2693f5223f8SWolfram Schneider PLAN *
270ef646f18SMark Murray c_Xmin(OPTION *option, char ***argvp)
2713f5223f8SWolfram Schneider {
272ea92232aSPoul-Henning Kamp 	char *nmins;
2733f5223f8SWolfram Schneider 	PLAN *new;
2743f5223f8SWolfram Schneider 
275ea92232aSPoul-Henning Kamp 	nmins = nextarg(option, argvp);
2763f5223f8SWolfram Schneider 	ftsoptions &= ~FTS_NOSTAT;
2773f5223f8SWolfram Schneider 
278ea92232aSPoul-Henning Kamp 	new = palloc(option);
279ea92232aSPoul-Henning Kamp 	new->t_data = find_parsenum(new, option->name, nmins, NULL);
280ea92232aSPoul-Henning Kamp 	TIME_CORRECT(new);
281ea92232aSPoul-Henning Kamp 	return new;
2823f5223f8SWolfram Schneider }
2833f5223f8SWolfram Schneider 
2849b50d902SRodney W. Grimes /*
285ea92232aSPoul-Henning Kamp  * -[acm]time n functions --
2869b50d902SRodney W. Grimes  *
287ea92232aSPoul-Henning Kamp  *	True if the difference between the
288ea92232aSPoul-Henning Kamp  *		file access time (-atime)
289ea92232aSPoul-Henning Kamp  *		last change of file status information (-ctime)
290ea92232aSPoul-Henning Kamp  *		file modification time (-mtime)
291ea92232aSPoul-Henning Kamp  *	and the current time is n 24 hour periods.
2929b50d902SRodney W. Grimes  */
293ea92232aSPoul-Henning Kamp 
2949b50d902SRodney W. Grimes int
295ef646f18SMark Murray f_Xtime(PLAN *plan, FTSENT *entry)
2969b50d902SRodney W. Grimes {
297631a8765SRuslan Ermilov 	time_t xtime;
298adff4fcaSRuslan Ermilov 
299631a8765SRuslan Ermilov 	if (plan->flags & F_TIME_A)
300631a8765SRuslan Ermilov 		xtime = entry->fts_statp->st_atime;
301631a8765SRuslan Ermilov 	else if (plan->flags & F_TIME_C)
302631a8765SRuslan Ermilov 		xtime = entry->fts_statp->st_ctime;
303631a8765SRuslan Ermilov 	else
304631a8765SRuslan Ermilov 		xtime = entry->fts_statp->st_mtime;
3059b50d902SRodney W. Grimes 
306631a8765SRuslan Ermilov 	if (plan->flags & F_EXACTTIME)
307631a8765SRuslan Ermilov 		COMPARE(now - xtime, plan->t_data);
308adff4fcaSRuslan Ermilov 	else
309631a8765SRuslan Ermilov 		COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data);
3109b50d902SRodney W. Grimes }
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes PLAN *
313ef646f18SMark Murray c_Xtime(OPTION *option, char ***argvp)
3149b50d902SRodney W. Grimes {
315adff4fcaSRuslan Ermilov 	char *value;
3169b50d902SRodney W. Grimes 	PLAN *new;
3179b50d902SRodney W. Grimes 
318adff4fcaSRuslan Ermilov 	value = nextarg(option, argvp);
3199b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
3209b50d902SRodney W. Grimes 
321ea92232aSPoul-Henning Kamp 	new = palloc(option);
322adff4fcaSRuslan Ermilov 	new->t_data = find_parsetime(new, option->name, value);
323adff4fcaSRuslan Ermilov 	if (!(new->flags & F_EXACTTIME))
324ea92232aSPoul-Henning Kamp 		TIME_CORRECT(new);
325ea92232aSPoul-Henning Kamp 	return new;
326ea92232aSPoul-Henning Kamp }
327ea92232aSPoul-Henning Kamp 
328ea92232aSPoul-Henning Kamp /*
329ea92232aSPoul-Henning Kamp  * -maxdepth/-mindepth n functions --
330ea92232aSPoul-Henning Kamp  *
331ea92232aSPoul-Henning Kamp  *        Does the same as -prune if the level of the current file is
332ea92232aSPoul-Henning Kamp  *        greater/less than the specified maximum/minimum depth.
333ea92232aSPoul-Henning Kamp  *
334ea92232aSPoul-Henning Kamp  *        Note that -maxdepth and -mindepth are handled specially in
335ea92232aSPoul-Henning Kamp  *        find_execute() so their f_* functions are set to f_always_true().
336ea92232aSPoul-Henning Kamp  */
337ea92232aSPoul-Henning Kamp PLAN *
338ef646f18SMark Murray c_mXXdepth(OPTION *option, char ***argvp)
339ea92232aSPoul-Henning Kamp {
340ea92232aSPoul-Henning Kamp 	char *dstr;
341ea92232aSPoul-Henning Kamp 	PLAN *new;
342ea92232aSPoul-Henning Kamp 
343ea92232aSPoul-Henning Kamp 	dstr = nextarg(option, argvp);
344ea92232aSPoul-Henning Kamp 	if (dstr[0] == '-')
345ea92232aSPoul-Henning Kamp 		/* all other errors handled by find_parsenum() */
346ea92232aSPoul-Henning Kamp 		errx(1, "%s: %s: value must be positive", option->name, dstr);
347ea92232aSPoul-Henning Kamp 
348ea92232aSPoul-Henning Kamp 	new = palloc(option);
349ea92232aSPoul-Henning Kamp 	if (option->flags & F_MAXDEPTH)
350ea92232aSPoul-Henning Kamp 		maxdepth = find_parsenum(new, option->name, dstr, NULL);
351ea92232aSPoul-Henning Kamp 	else
352ea92232aSPoul-Henning Kamp 		mindepth = find_parsenum(new, option->name, dstr, NULL);
353ea92232aSPoul-Henning Kamp 	return new;
354ea92232aSPoul-Henning Kamp }
355ea92232aSPoul-Henning Kamp 
356ea92232aSPoul-Henning Kamp /*
3579c5d31dfSBosko Milekic  * -acl function --
3589c5d31dfSBosko Milekic  *
3599c5d31dfSBosko Milekic  *	Show files with EXTENDED ACL attributes.
3609c5d31dfSBosko Milekic  */
3619c5d31dfSBosko Milekic int
3629c5d31dfSBosko Milekic f_acl(PLAN *plan__unused, FTSENT *entry)
3639c5d31dfSBosko Milekic {
3649c5d31dfSBosko Milekic 	int match, entries;
3659c5d31dfSBosko Milekic 	acl_entry_t ae;
3669c5d31dfSBosko Milekic 	acl_t facl;
3679c5d31dfSBosko Milekic 
3689c5d31dfSBosko Milekic 	if (S_ISLNK(entry->fts_statp->st_mode))
3699c5d31dfSBosko Milekic 		return 0;
3709c5d31dfSBosko Milekic 	if ((match = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED)) <= 0) {
3719c5d31dfSBosko Milekic 		if (match < 0 && errno != EINVAL)
3729c5d31dfSBosko Milekic 			warn("%s", entry->fts_accpath);
3739c5d31dfSBosko Milekic 	else
3749c5d31dfSBosko Milekic 		return 0;
3759c5d31dfSBosko Milekic 	}
3769c5d31dfSBosko Milekic 	match = 0;
3779c5d31dfSBosko Milekic 	if ((facl = acl_get_file(entry->fts_accpath,ACL_TYPE_ACCESS)) != NULL) {
3789c5d31dfSBosko Milekic 		if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 1) {
3799c5d31dfSBosko Milekic 			/*
3809c5d31dfSBosko Milekic 			 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS
3819c5d31dfSBosko Milekic 			 * must have at least three entries (owner, group,
3829c5d31dfSBosko Milekic 			 * other).
3839c5d31dfSBosko Milekic 			 */
3849c5d31dfSBosko Milekic 			entries = 1;
3859c5d31dfSBosko Milekic 			while (acl_get_entry(facl, ACL_NEXT_ENTRY, &ae) == 1) {
3869c5d31dfSBosko Milekic 				if (++entries > 3) {
3879c5d31dfSBosko Milekic 					match = 1;
3889c5d31dfSBosko Milekic 					break;
3899c5d31dfSBosko Milekic 				}
3909c5d31dfSBosko Milekic 			}
3919c5d31dfSBosko Milekic 		}
3929c5d31dfSBosko Milekic 		acl_free(facl);
3939c5d31dfSBosko Milekic 	} else
3949c5d31dfSBosko Milekic 		warn("%s", entry->fts_accpath);
3959c5d31dfSBosko Milekic 	return match;
3969c5d31dfSBosko Milekic }
3979c5d31dfSBosko Milekic 
3989c5d31dfSBosko Milekic PLAN *
3999c5d31dfSBosko Milekic c_acl(OPTION *option, char ***argvp__unused)
4009c5d31dfSBosko Milekic {
4019c5d31dfSBosko Milekic 	ftsoptions &= ~FTS_NOSTAT;
4029c5d31dfSBosko Milekic 	return (palloc(option));
4039c5d31dfSBosko Milekic }
4049c5d31dfSBosko Milekic 
4059c5d31dfSBosko Milekic /*
406ea92232aSPoul-Henning Kamp  * -delete functions --
407ea92232aSPoul-Henning Kamp  *
408ea92232aSPoul-Henning Kamp  *	True always.  Makes its best shot and continues on regardless.
409ea92232aSPoul-Henning Kamp  */
410ea92232aSPoul-Henning Kamp int
411ef646f18SMark Murray f_delete(PLAN *plan __unused, FTSENT *entry)
412ea92232aSPoul-Henning Kamp {
413ea92232aSPoul-Henning Kamp 	/* ignore these from fts */
414ea92232aSPoul-Henning Kamp 	if (strcmp(entry->fts_accpath, ".") == 0 ||
415ea92232aSPoul-Henning Kamp 	    strcmp(entry->fts_accpath, "..") == 0)
416ea92232aSPoul-Henning Kamp 		return 1;
417ea92232aSPoul-Henning Kamp 
418ea92232aSPoul-Henning Kamp 	/* sanity check */
419ea92232aSPoul-Henning Kamp 	if (isdepth == 0 ||			/* depth off */
420ea92232aSPoul-Henning Kamp 	    (ftsoptions & FTS_NOSTAT) ||	/* not stat()ing */
421ea92232aSPoul-Henning Kamp 	    !(ftsoptions & FTS_PHYSICAL) ||	/* physical off */
422ea92232aSPoul-Henning Kamp 	    (ftsoptions & FTS_LOGICAL))		/* or finally, logical on */
423ea92232aSPoul-Henning Kamp 		errx(1, "-delete: insecure options got turned on");
424ea92232aSPoul-Henning Kamp 
425ea92232aSPoul-Henning Kamp 	/* Potentially unsafe - do not accept relative paths whatsoever */
426ea92232aSPoul-Henning Kamp 	if (strchr(entry->fts_accpath, '/') != NULL)
427ea92232aSPoul-Henning Kamp 		errx(1, "-delete: %s: relative path potentially not safe",
428ea92232aSPoul-Henning Kamp 			entry->fts_accpath);
429ea92232aSPoul-Henning Kamp 
430ea92232aSPoul-Henning Kamp 	/* Turn off user immutable bits if running as root */
431ea92232aSPoul-Henning Kamp 	if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
432ea92232aSPoul-Henning Kamp 	    !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
433ea92232aSPoul-Henning Kamp 	    geteuid() == 0)
434ea92232aSPoul-Henning Kamp 		chflags(entry->fts_accpath,
435ea92232aSPoul-Henning Kamp 		       entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
436ea92232aSPoul-Henning Kamp 
437ea92232aSPoul-Henning Kamp 	/* rmdir directories, unlink everything else */
438ea92232aSPoul-Henning Kamp 	if (S_ISDIR(entry->fts_statp->st_mode)) {
439ea92232aSPoul-Henning Kamp 		if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
440ea92232aSPoul-Henning Kamp 			warn("-delete: rmdir(%s)", entry->fts_path);
441ea92232aSPoul-Henning Kamp 	} else {
442ea92232aSPoul-Henning Kamp 		if (unlink(entry->fts_accpath) < 0)
443ea92232aSPoul-Henning Kamp 			warn("-delete: unlink(%s)", entry->fts_path);
444ea92232aSPoul-Henning Kamp 	}
445ea92232aSPoul-Henning Kamp 
446ea92232aSPoul-Henning Kamp 	/* "succeed" */
447ea92232aSPoul-Henning Kamp 	return 1;
448ea92232aSPoul-Henning Kamp }
449ea92232aSPoul-Henning Kamp 
450ea92232aSPoul-Henning Kamp PLAN *
451ef646f18SMark Murray c_delete(OPTION *option, char ***argvp __unused)
452ea92232aSPoul-Henning Kamp {
453ea92232aSPoul-Henning Kamp 
454ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;	/* no optimise */
455ea92232aSPoul-Henning Kamp 	ftsoptions |= FTS_PHYSICAL;	/* disable -follow */
456ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_LOGICAL;	/* disable -follow */
457ea92232aSPoul-Henning Kamp 	isoutput = 1;			/* possible output */
458ea92232aSPoul-Henning Kamp 	isdepth = 1;			/* -depth implied */
459ea92232aSPoul-Henning Kamp 
460ea92232aSPoul-Henning Kamp 	return palloc(option);
4619b50d902SRodney W. Grimes }
4629b50d902SRodney W. Grimes 
4633f5223f8SWolfram Schneider 
4649b50d902SRodney W. Grimes /*
4659b50d902SRodney W. Grimes  * -depth functions --
4669b50d902SRodney W. Grimes  *
4679b50d902SRodney W. Grimes  *	Always true, causes descent of the directory hierarchy to be done
4689b50d902SRodney W. Grimes  *	so that all entries in a directory are acted on before the directory
4699b50d902SRodney W. Grimes  *	itself.
4709b50d902SRodney W. Grimes  */
4719b50d902SRodney W. Grimes int
472ef646f18SMark Murray f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
4739b50d902SRodney W. Grimes {
474ea92232aSPoul-Henning Kamp 	return 1;
4759b50d902SRodney W. Grimes }
4769b50d902SRodney W. Grimes 
4779b50d902SRodney W. Grimes PLAN *
478ef646f18SMark Murray c_depth(OPTION *option, char ***argvp __unused)
4799b50d902SRodney W. Grimes {
4809b50d902SRodney W. Grimes 	isdepth = 1;
4819b50d902SRodney W. Grimes 
482ea92232aSPoul-Henning Kamp 	return palloc(option);
4839b50d902SRodney W. Grimes }
484127d7563SWarner Losh 
485127d7563SWarner Losh /*
486ed1a4621SPeter Wemm  * -empty functions --
487ed1a4621SPeter Wemm  *
488ed1a4621SPeter Wemm  *	True if the file or directory is empty
489ed1a4621SPeter Wemm  */
490ed1a4621SPeter Wemm int
491ef646f18SMark Murray f_empty(PLAN *plan __unused, FTSENT *entry)
492ed1a4621SPeter Wemm {
493ea92232aSPoul-Henning Kamp 	if (S_ISREG(entry->fts_statp->st_mode) &&
494ea92232aSPoul-Henning Kamp 	    entry->fts_statp->st_size == 0)
495ea92232aSPoul-Henning Kamp 		return 1;
496ed1a4621SPeter Wemm 	if (S_ISDIR(entry->fts_statp->st_mode)) {
497ed1a4621SPeter Wemm 		struct dirent *dp;
498ed1a4621SPeter Wemm 		int empty;
499ed1a4621SPeter Wemm 		DIR *dir;
500ed1a4621SPeter Wemm 
501ed1a4621SPeter Wemm 		empty = 1;
502ed1a4621SPeter Wemm 		dir = opendir(entry->fts_accpath);
503ed1a4621SPeter Wemm 		if (dir == NULL)
504ed1a4621SPeter Wemm 			err(1, "%s", entry->fts_accpath);
505ed1a4621SPeter Wemm 		for (dp = readdir(dir); dp; dp = readdir(dir))
506ed1a4621SPeter Wemm 			if (dp->d_name[0] != '.' ||
507ed1a4621SPeter Wemm 			    (dp->d_name[1] != '\0' &&
508ed1a4621SPeter Wemm 			     (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
509ed1a4621SPeter Wemm 				empty = 0;
510ed1a4621SPeter Wemm 				break;
511ed1a4621SPeter Wemm 			}
512ed1a4621SPeter Wemm 		closedir(dir);
513ea92232aSPoul-Henning Kamp 		return empty;
514ed1a4621SPeter Wemm 	}
515ea92232aSPoul-Henning Kamp 	return 0;
516ed1a4621SPeter Wemm }
517ed1a4621SPeter Wemm 
518ed1a4621SPeter Wemm PLAN *
519ef646f18SMark Murray c_empty(OPTION *option, char ***argvp __unused)
520ed1a4621SPeter Wemm {
521ed1a4621SPeter Wemm 	ftsoptions &= ~FTS_NOSTAT;
522ed1a4621SPeter Wemm 
523ea92232aSPoul-Henning Kamp 	return palloc(option);
524ed1a4621SPeter Wemm }
525ed1a4621SPeter Wemm 
526ed1a4621SPeter Wemm /*
527ea92232aSPoul-Henning Kamp  * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
528127d7563SWarner Losh  *
529127d7563SWarner Losh  *	True if the executed utility returns a zero value as exit status.
530127d7563SWarner Losh  *	The end of the primary expression is delimited by a semicolon.  If
531ea92232aSPoul-Henning Kamp  *	"{}" occurs anywhere, it gets replaced by the current pathname,
532ea92232aSPoul-Henning Kamp  *	or, in the case of -execdir, the current basename (filename
533ea92232aSPoul-Henning Kamp  *	without leading directory prefix). For -exec and -ok,
534ea92232aSPoul-Henning Kamp  *	the current directory for the execution of utility is the same as
535ea92232aSPoul-Henning Kamp  *	the current directory when the find utility was started, whereas
536ea92232aSPoul-Henning Kamp  *	for -execdir, it is the directory the file resides in.
537ea92232aSPoul-Henning Kamp  *
538ea92232aSPoul-Henning Kamp  *	The primary -ok differs from -exec in that it requests affirmation
539ea92232aSPoul-Henning Kamp  *	of the user before executing the utility.
540127d7563SWarner Losh  */
541127d7563SWarner Losh int
542ef646f18SMark Murray f_exec(PLAN *plan, FTSENT *entry)
543127d7563SWarner Losh {
544e98080b1SDavid Malone 	int cnt;
545127d7563SWarner Losh 	pid_t pid;
546127d7563SWarner Losh 	int status;
547127d7563SWarner Losh 	char *file;
548127d7563SWarner Losh 
5495e25d888STim J. Robbins 	if (entry == NULL && plan->flags & F_EXECPLUS) {
5505e25d888STim J. Robbins 		if (plan->e_ppos == plan->e_pbnum)
5515e25d888STim J. Robbins 			return (1);
5525e25d888STim J. Robbins 		plan->e_argv[plan->e_ppos] = NULL;
5535e25d888STim J. Robbins 		goto doexec;
5545e25d888STim J. Robbins 	}
5555e25d888STim J. Robbins 
556127d7563SWarner Losh 	/* XXX - if file/dir ends in '/' this will not work -- can it? */
557ea92232aSPoul-Henning Kamp 	if ((plan->flags & F_EXECDIR) && \
558ea92232aSPoul-Henning Kamp 	    (file = strrchr(entry->fts_path, '/')))
559127d7563SWarner Losh 		file++;
560127d7563SWarner Losh 	else
561127d7563SWarner Losh 		file = entry->fts_path;
562127d7563SWarner Losh 
5635e25d888STim J. Robbins 	if (plan->flags & F_EXECPLUS) {
5645e25d888STim J. Robbins 		if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
5655e25d888STim J. Robbins 			err(1, NULL);
5665e25d888STim J. Robbins 		plan->e_len[plan->e_ppos] = strlen(file);
5675e25d888STim J. Robbins 		plan->e_psize += plan->e_len[plan->e_ppos];
5685e25d888STim J. Robbins 		if (++plan->e_ppos < plan->e_pnummax &&
5695e25d888STim J. Robbins 		    plan->e_psize < plan->e_psizemax)
5705e25d888STim J. Robbins 			return (1);
5715e25d888STim J. Robbins 		plan->e_argv[plan->e_ppos] = NULL;
5725e25d888STim J. Robbins 	} else {
573127d7563SWarner Losh 		for (cnt = 0; plan->e_argv[cnt]; ++cnt)
574127d7563SWarner Losh 			if (plan->e_len[cnt])
5755e25d888STim J. Robbins 				brace_subst(plan->e_orig[cnt],
5765e25d888STim J. Robbins 				    &plan->e_argv[cnt], file,
5775e25d888STim J. Robbins 				    plan->e_len[cnt]);
5785e25d888STim J. Robbins 	}
579127d7563SWarner Losh 
5805e25d888STim J. Robbins doexec:	if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
581ea92232aSPoul-Henning Kamp 		return 0;
582ea92232aSPoul-Henning Kamp 
583ea92232aSPoul-Henning Kamp 	/* make sure find output is interspersed correctly with subprocesses */
584127d7563SWarner Losh 	fflush(stdout);
585127d7563SWarner Losh 	fflush(stderr);
586127d7563SWarner Losh 
5871fd98d7dSDag-Erling Smørgrav 	switch (pid = fork()) {
588127d7563SWarner Losh 	case -1:
589127d7563SWarner Losh 		err(1, "fork");
590127d7563SWarner Losh 		/* NOTREACHED */
591127d7563SWarner Losh 	case 0:
592ea92232aSPoul-Henning Kamp 		/* change dir back from where we started */
593ea92232aSPoul-Henning Kamp 		if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
594ea92232aSPoul-Henning Kamp 			warn("chdir");
595ea92232aSPoul-Henning Kamp 			_exit(1);
596ea92232aSPoul-Henning Kamp 		}
597127d7563SWarner Losh 		execvp(plan->e_argv[0], plan->e_argv);
598127d7563SWarner Losh 		warn("%s", plan->e_argv[0]);
599127d7563SWarner Losh 		_exit(1);
600127d7563SWarner Losh 	}
6015e25d888STim J. Robbins 	if (plan->flags & F_EXECPLUS) {
6025e25d888STim J. Robbins 		while (--plan->e_ppos >= plan->e_pbnum)
6035e25d888STim J. Robbins 			free(plan->e_argv[plan->e_ppos]);
6045e25d888STim J. Robbins 		plan->e_ppos = plan->e_pbnum;
6055e25d888STim J. Robbins 		plan->e_psize = plan->e_pbsize;
6065e25d888STim J. Robbins 	}
607127d7563SWarner Losh 	pid = waitpid(pid, &status, 0);
608127d7563SWarner Losh 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
609127d7563SWarner Losh }
610127d7563SWarner Losh 
611127d7563SWarner Losh /*
612ea92232aSPoul-Henning Kamp  * c_exec, c_execdir, c_ok --
613127d7563SWarner Losh  *	build three parallel arrays, one with pointers to the strings passed
614127d7563SWarner Losh  *	on the command line, one with (possibly duplicated) pointers to the
615127d7563SWarner Losh  *	argv array, and one with integer values that are lengths of the
616127d7563SWarner Losh  *	strings, but also flags meaning that the string has to be massaged.
617127d7563SWarner Losh  */
618127d7563SWarner Losh PLAN *
619ef646f18SMark Murray c_exec(OPTION *option, char ***argvp)
620127d7563SWarner Losh {
621127d7563SWarner Losh 	PLAN *new;			/* node returned */
6225e25d888STim J. Robbins 	long argmax;
6235e25d888STim J. Robbins 	int cnt, i;
624a07af811STim J. Robbins 	char **argv, **ap, **ep, *p;
625127d7563SWarner Losh 
626ea92232aSPoul-Henning Kamp 	/* XXX - was in c_execdir, but seems unnecessary!?
627127d7563SWarner Losh 	ftsoptions &= ~FTS_NOSTAT;
628ea92232aSPoul-Henning Kamp 	*/
629127d7563SWarner Losh 	isoutput = 1;
630127d7563SWarner Losh 
631ea92232aSPoul-Henning Kamp 	/* XXX - this is a change from the previous coding */
632ea92232aSPoul-Henning Kamp 	new = palloc(option);
633127d7563SWarner Losh 
634127d7563SWarner Losh 	for (ap = argv = *argvp;; ++ap) {
635127d7563SWarner Losh 		if (!*ap)
636127d7563SWarner Losh 			errx(1,
637ea92232aSPoul-Henning Kamp 			    "%s: no terminating \";\"", option->name);
638127d7563SWarner Losh 		if (**ap == ';')
639127d7563SWarner Losh 			break;
6405e25d888STim J. Robbins 		if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
6415e25d888STim J. Robbins 			new->flags |= F_EXECPLUS;
6425e25d888STim J. Robbins 			break;
6435e25d888STim J. Robbins 		}
644127d7563SWarner Losh 	}
645127d7563SWarner Losh 
64651b0534fSJuli Mallett 	if (ap == argv)
64751b0534fSJuli Mallett 		errx(1, "%s: no command specified", option->name);
64851b0534fSJuli Mallett 
649127d7563SWarner Losh 	cnt = ap - *argvp + 1;
6505e25d888STim J. Robbins 	if (new->flags & F_EXECPLUS) {
6515e25d888STim J. Robbins 		new->e_ppos = new->e_pbnum = cnt - 2;
6525e25d888STim J. Robbins 		if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
6535e25d888STim J. Robbins 			warn("sysconf(_SC_ARG_MAX)");
6545e25d888STim J. Robbins 			argmax = _POSIX_ARG_MAX;
6555e25d888STim J. Robbins 		}
656a07af811STim J. Robbins 		argmax -= 1024;
657a07af811STim J. Robbins 		for (ep = environ; *ep != NULL; ep++)
658a07af811STim J. Robbins 			argmax -= strlen(*ep) + 1 + sizeof(*ep);
659a07af811STim J. Robbins 		argmax -= 1 + sizeof(*ep);
660a07af811STim J. Robbins 		new->e_pnummax = argmax / 16;
661a07af811STim J. Robbins 		argmax -= sizeof(char *) * new->e_pnummax;
662a07af811STim J. Robbins 		if (argmax <= 0)
663a07af811STim J. Robbins 			errx(1, "no space for arguments");
664a07af811STim J. Robbins 		new->e_psizemax = argmax;
6655e25d888STim J. Robbins 		new->e_pbsize = 0;
6665e25d888STim J. Robbins 		cnt += new->e_pnummax + 1;
6675e25d888STim J. Robbins 	}
66847bca8b0SJuli Mallett 	if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
66947bca8b0SJuli Mallett 		err(1, NULL);
67047bca8b0SJuli Mallett 	if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
67147bca8b0SJuli Mallett 		err(1, NULL);
67247bca8b0SJuli Mallett 	if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
67347bca8b0SJuli Mallett 		err(1, NULL);
674127d7563SWarner Losh 
675127d7563SWarner Losh 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
676127d7563SWarner Losh 		new->e_orig[cnt] = *argv;
6775e25d888STim J. Robbins 		if (new->flags & F_EXECPLUS)
6785e25d888STim J. Robbins 			new->e_pbsize += strlen(*argv) + 1;
679127d7563SWarner Losh 		for (p = *argv; *p; ++p)
6805e25d888STim J. Robbins 			if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
6815e25d888STim J. Robbins 			    p[1] == '}') {
682f0cb9537SDavid E. O'Brien 				if ((new->e_argv[cnt] =
68347bca8b0SJuli Mallett 				    malloc(MAXPATHLEN)) == NULL)
68447bca8b0SJuli Mallett 					err(1, NULL);
685127d7563SWarner Losh 				new->e_len[cnt] = MAXPATHLEN;
686127d7563SWarner Losh 				break;
687127d7563SWarner Losh 			}
688127d7563SWarner Losh 		if (!*p) {
689127d7563SWarner Losh 			new->e_argv[cnt] = *argv;
690127d7563SWarner Losh 			new->e_len[cnt] = 0;
691127d7563SWarner Losh 		}
692127d7563SWarner Losh 	}
6935e25d888STim J. Robbins 	if (new->flags & F_EXECPLUS) {
6945e25d888STim J. Robbins 		new->e_psize = new->e_pbsize;
6955e25d888STim J. Robbins 		cnt--;
6965e25d888STim J. Robbins 		for (i = 0; i < new->e_pnummax; i++) {
6975e25d888STim J. Robbins 			new->e_argv[cnt] = NULL;
6985e25d888STim J. Robbins 			new->e_len[cnt] = 0;
6995e25d888STim J. Robbins 			cnt++;
7005e25d888STim J. Robbins 		}
7015e25d888STim J. Robbins 		argv = ap;
7025e25d888STim J. Robbins 		goto done;
7035e25d888STim J. Robbins 	}
704127d7563SWarner Losh 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
705127d7563SWarner Losh 
7065e25d888STim J. Robbins done:	*argvp = argv + 1;
707ea92232aSPoul-Henning Kamp 	return new;
708ea92232aSPoul-Henning Kamp }
709ea92232aSPoul-Henning Kamp 
710ea92232aSPoul-Henning Kamp int
711ef646f18SMark Murray f_flags(PLAN *plan, FTSENT *entry)
712ea92232aSPoul-Henning Kamp {
713ea92232aSPoul-Henning Kamp 	u_long flags;
714ea92232aSPoul-Henning Kamp 
7157fd5ee41SRuslan Ermilov 	flags = entry->fts_statp->st_flags;
716ea92232aSPoul-Henning Kamp 	if (plan->flags & F_ATLEAST)
7177fd5ee41SRuslan Ermilov 		return (flags | plan->fl_flags) == flags &&
7187fd5ee41SRuslan Ermilov 		    !(flags & plan->fl_notflags);
7197fd5ee41SRuslan Ermilov 	else if (plan->flags & F_ANY)
7207fd5ee41SRuslan Ermilov 		return (flags & plan->fl_flags) ||
7217fd5ee41SRuslan Ermilov 		    (flags | plan->fl_notflags) != flags;
722ea92232aSPoul-Henning Kamp 	else
7237fd5ee41SRuslan Ermilov 		return flags == plan->fl_flags &&
7247fd5ee41SRuslan Ermilov 		    !(plan->fl_flags & plan->fl_notflags);
725ea92232aSPoul-Henning Kamp }
726ea92232aSPoul-Henning Kamp 
727ea92232aSPoul-Henning Kamp PLAN *
728ef646f18SMark Murray c_flags(OPTION *option, char ***argvp)
729ea92232aSPoul-Henning Kamp {
730ea92232aSPoul-Henning Kamp 	char *flags_str;
731ea92232aSPoul-Henning Kamp 	PLAN *new;
732ea92232aSPoul-Henning Kamp 	u_long flags, notflags;
733ea92232aSPoul-Henning Kamp 
734ea92232aSPoul-Henning Kamp 	flags_str = nextarg(option, argvp);
735ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
736ea92232aSPoul-Henning Kamp 
737ea92232aSPoul-Henning Kamp 	new = palloc(option);
738ea92232aSPoul-Henning Kamp 
739ea92232aSPoul-Henning Kamp 	if (*flags_str == '-') {
740ea92232aSPoul-Henning Kamp 		new->flags |= F_ATLEAST;
741ea92232aSPoul-Henning Kamp 		flags_str++;
7427fd5ee41SRuslan Ermilov 	} else if (*flags_str == '+') {
7437fd5ee41SRuslan Ermilov 		new->flags |= F_ANY;
7447fd5ee41SRuslan Ermilov 		flags_str++;
745ea92232aSPoul-Henning Kamp 	}
746ea92232aSPoul-Henning Kamp 	if (strtofflags(&flags_str, &flags, &notflags) == 1)
747ea92232aSPoul-Henning Kamp 		errx(1, "%s: %s: illegal flags string", option->name, flags_str);
748ea92232aSPoul-Henning Kamp 
749ea92232aSPoul-Henning Kamp 	new->fl_flags = flags;
7507fd5ee41SRuslan Ermilov 	new->fl_notflags = notflags;
751ea92232aSPoul-Henning Kamp 	return new;
752127d7563SWarner Losh }
7539b50d902SRodney W. Grimes 
7549b50d902SRodney W. Grimes /*
7559b50d902SRodney W. Grimes  * -follow functions --
7569b50d902SRodney W. Grimes  *
7579b50d902SRodney W. Grimes  *	Always true, causes symbolic links to be followed on a global
7589b50d902SRodney W. Grimes  *	basis.
7599b50d902SRodney W. Grimes  */
7609b50d902SRodney W. Grimes PLAN *
761ef646f18SMark Murray c_follow(OPTION *option, char ***argvp __unused)
7629b50d902SRodney W. Grimes {
7639b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_PHYSICAL;
7649b50d902SRodney W. Grimes 	ftsoptions |= FTS_LOGICAL;
7659b50d902SRodney W. Grimes 
766ea92232aSPoul-Henning Kamp 	return palloc(option);
7679b50d902SRodney W. Grimes }
7689b50d902SRodney W. Grimes 
7699b50d902SRodney W. Grimes /*
7709b50d902SRodney W. Grimes  * -fstype functions --
7719b50d902SRodney W. Grimes  *
7729b50d902SRodney W. Grimes  *	True if the file is of a certain type.
7739b50d902SRodney W. Grimes  */
7749b50d902SRodney W. Grimes int
775ef646f18SMark Murray f_fstype(PLAN *plan, FTSENT *entry)
7769b50d902SRodney W. Grimes {
7779b50d902SRodney W. Grimes 	static dev_t curdev;	/* need a guaranteed illegal dev value */
7789b50d902SRodney W. Grimes 	static int first = 1;
7799b50d902SRodney W. Grimes 	struct statfs sb;
7809d08e419SPeter Wemm 	static int val_type, val_flags;
7819b50d902SRodney W. Grimes 	char *p, save[2];
7829b50d902SRodney W. Grimes 
783ea92232aSPoul-Henning Kamp 	if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
784ea92232aSPoul-Henning Kamp 		return 0;
785ea92232aSPoul-Henning Kamp 
7869b50d902SRodney W. Grimes 	/* Only check when we cross mount point. */
7879b50d902SRodney W. Grimes 	if (first || curdev != entry->fts_statp->st_dev) {
7889b50d902SRodney W. Grimes 		curdev = entry->fts_statp->st_dev;
7899b50d902SRodney W. Grimes 
7909b50d902SRodney W. Grimes 		/*
7919b50d902SRodney W. Grimes 		 * Statfs follows symlinks; find wants the link's filesystem,
7929b50d902SRodney W. Grimes 		 * not where it points.
7939b50d902SRodney W. Grimes 		 */
7949b50d902SRodney W. Grimes 		if (entry->fts_info == FTS_SL ||
7959b50d902SRodney W. Grimes 		    entry->fts_info == FTS_SLNONE) {
7969b50d902SRodney W. Grimes 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
7979b50d902SRodney W. Grimes 				++p;
7989b50d902SRodney W. Grimes 			else
7999b50d902SRodney W. Grimes 				p = entry->fts_accpath;
8009b50d902SRodney W. Grimes 			save[0] = p[0];
8019b50d902SRodney W. Grimes 			p[0] = '.';
8029b50d902SRodney W. Grimes 			save[1] = p[1];
8039b50d902SRodney W. Grimes 			p[1] = '\0';
8049b50d902SRodney W. Grimes 		} else
8059b50d902SRodney W. Grimes 			p = NULL;
8069b50d902SRodney W. Grimes 
8079b50d902SRodney W. Grimes 		if (statfs(entry->fts_accpath, &sb))
8089b50d902SRodney W. Grimes 			err(1, "%s", entry->fts_accpath);
8099b50d902SRodney W. Grimes 
8109b50d902SRodney W. Grimes 		if (p) {
8119b50d902SRodney W. Grimes 			p[0] = save[0];
8129b50d902SRodney W. Grimes 			p[1] = save[1];
8139b50d902SRodney W. Grimes 		}
8149b50d902SRodney W. Grimes 
8159b50d902SRodney W. Grimes 		first = 0;
816841484cdSPeter Wemm 
817841484cdSPeter Wemm 		/*
818841484cdSPeter Wemm 		 * Further tests may need both of these values, so
819841484cdSPeter Wemm 		 * always copy both of them.
820841484cdSPeter Wemm 		 */
8219d08e419SPeter Wemm 		val_flags = sb.f_flags;
8229d08e419SPeter Wemm 		val_type = sb.f_type;
8239b50d902SRodney W. Grimes 	}
824ea92232aSPoul-Henning Kamp 	switch (plan->flags & F_MTMASK) {
8259b50d902SRodney W. Grimes 	case F_MTFLAG:
826ea92232aSPoul-Henning Kamp 		return val_flags & plan->mt_data;
8279b50d902SRodney W. Grimes 	case F_MTTYPE:
828ea92232aSPoul-Henning Kamp 		return val_type == plan->mt_data;
8299b50d902SRodney W. Grimes 	default:
8309b50d902SRodney W. Grimes 		abort();
8319b50d902SRodney W. Grimes 	}
8329b50d902SRodney W. Grimes }
8339b50d902SRodney W. Grimes 
83494aacc4fSJohn Birrell #if !defined(__NetBSD__)
8359b50d902SRodney W. Grimes PLAN *
836ef646f18SMark Murray c_fstype(OPTION *option, char ***argvp)
8379b50d902SRodney W. Grimes {
838ea92232aSPoul-Henning Kamp 	char *fsname;
839e98080b1SDavid Malone 	PLAN *new;
8405965373eSMaxime Henrion 	struct xvfsconf vfc;
8419b50d902SRodney W. Grimes 
842ea92232aSPoul-Henning Kamp 	fsname = nextarg(option, argvp);
8439b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
8449b50d902SRodney W. Grimes 
845ea92232aSPoul-Henning Kamp 	new = palloc(option);
846841484cdSPeter Wemm 
847841484cdSPeter Wemm 	/*
848841484cdSPeter Wemm 	 * Check first for a filesystem name.
849841484cdSPeter Wemm 	 */
850ea92232aSPoul-Henning Kamp 	if (getvfsbyname(fsname, &vfc) == 0) {
851ea92232aSPoul-Henning Kamp 		new->flags |= F_MTTYPE;
852841484cdSPeter Wemm 		new->mt_data = vfc.vfc_typenum;
853ea92232aSPoul-Henning Kamp 		return new;
854841484cdSPeter Wemm 	}
855841484cdSPeter Wemm 
856ea92232aSPoul-Henning Kamp 	switch (*fsname) {
8579b50d902SRodney W. Grimes 	case 'l':
858ea92232aSPoul-Henning Kamp 		if (!strcmp(fsname, "local")) {
859ea92232aSPoul-Henning Kamp 			new->flags |= F_MTFLAG;
8609b50d902SRodney W. Grimes 			new->mt_data = MNT_LOCAL;
861ea92232aSPoul-Henning Kamp 			return new;
8629b50d902SRodney W. Grimes 		}
8639b50d902SRodney W. Grimes 		break;
8649b50d902SRodney W. Grimes 	case 'r':
865ea92232aSPoul-Henning Kamp 		if (!strcmp(fsname, "rdonly")) {
866ea92232aSPoul-Henning Kamp 			new->flags |= F_MTFLAG;
8679b50d902SRodney W. Grimes 			new->mt_data = MNT_RDONLY;
868ea92232aSPoul-Henning Kamp 			return new;
8699b50d902SRodney W. Grimes 		}
8709b50d902SRodney W. Grimes 		break;
8719b50d902SRodney W. Grimes 	}
872ea92232aSPoul-Henning Kamp 
8731e2f8412SEivind Eklund 	/*
8741e2f8412SEivind Eklund 	 * We need to make filesystem checks for filesystems
8751e2f8412SEivind Eklund 	 * that exists but aren't in the kernel work.
8761e2f8412SEivind Eklund 	 */
877ea92232aSPoul-Henning Kamp 	fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname);
878ea92232aSPoul-Henning Kamp 	new->flags |= F_MTUNKNOWN;
879ea92232aSPoul-Henning Kamp 	return new;
8809b50d902SRodney W. Grimes }
881ea92232aSPoul-Henning Kamp #endif /* __NetBSD__ */
8829b50d902SRodney W. Grimes 
8839b50d902SRodney W. Grimes /*
8849b50d902SRodney W. Grimes  * -group gname functions --
8859b50d902SRodney W. Grimes  *
8869b50d902SRodney W. Grimes  *	True if the file belongs to the group gname.  If gname is numeric and
8879b50d902SRodney W. Grimes  *	an equivalent of the getgrnam() function does not return a valid group
8889b50d902SRodney W. Grimes  *	name, gname is taken as a group ID.
8899b50d902SRodney W. Grimes  */
8909b50d902SRodney W. Grimes int
891ef646f18SMark Murray f_group(PLAN *plan, FTSENT *entry)
8929b50d902SRodney W. Grimes {
893ea92232aSPoul-Henning Kamp 	return entry->fts_statp->st_gid == plan->g_data;
8949b50d902SRodney W. Grimes }
8959b50d902SRodney W. Grimes 
8969b50d902SRodney W. Grimes PLAN *
897ef646f18SMark Murray c_group(OPTION *option, char ***argvp)
8989b50d902SRodney W. Grimes {
899ea92232aSPoul-Henning Kamp 	char *gname;
9009b50d902SRodney W. Grimes 	PLAN *new;
9019b50d902SRodney W. Grimes 	struct group *g;
9029b50d902SRodney W. Grimes 	gid_t gid;
9039b50d902SRodney W. Grimes 
904ea92232aSPoul-Henning Kamp 	gname = nextarg(option, argvp);
9059b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
9069b50d902SRodney W. Grimes 
9079b50d902SRodney W. Grimes 	g = getgrnam(gname);
9089b50d902SRodney W. Grimes 	if (g == NULL) {
9099b50d902SRodney W. Grimes 		gid = atoi(gname);
9109b50d902SRodney W. Grimes 		if (gid == 0 && gname[0] != '0')
911ea92232aSPoul-Henning Kamp 			errx(1, "%s: %s: no such group", option->name, gname);
9129b50d902SRodney W. Grimes 	} else
9139b50d902SRodney W. Grimes 		gid = g->gr_gid;
9149b50d902SRodney W. Grimes 
915ea92232aSPoul-Henning Kamp 	new = palloc(option);
9169b50d902SRodney W. Grimes 	new->g_data = gid;
917ea92232aSPoul-Henning Kamp 	return new;
9189b50d902SRodney W. Grimes }
9199b50d902SRodney W. Grimes 
9209b50d902SRodney W. Grimes /*
9219b50d902SRodney W. Grimes  * -inum n functions --
9229b50d902SRodney W. Grimes  *
9239b50d902SRodney W. Grimes  *	True if the file has inode # n.
9249b50d902SRodney W. Grimes  */
9259b50d902SRodney W. Grimes int
926ef646f18SMark Murray f_inum(PLAN *plan, FTSENT *entry)
9279b50d902SRodney W. Grimes {
9289b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
9299b50d902SRodney W. Grimes }
9309b50d902SRodney W. Grimes 
9319b50d902SRodney W. Grimes PLAN *
932ef646f18SMark Murray c_inum(OPTION *option, char ***argvp)
9339b50d902SRodney W. Grimes {
934ea92232aSPoul-Henning Kamp 	char *inum_str;
9359b50d902SRodney W. Grimes 	PLAN *new;
9369b50d902SRodney W. Grimes 
937ea92232aSPoul-Henning Kamp 	inum_str = nextarg(option, argvp);
9389b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
9399b50d902SRodney W. Grimes 
940ea92232aSPoul-Henning Kamp 	new = palloc(option);
941ea92232aSPoul-Henning Kamp 	new->i_data = find_parsenum(new, option->name, inum_str, NULL);
942ea92232aSPoul-Henning Kamp 	return new;
9439b50d902SRodney W. Grimes }
9449b50d902SRodney W. Grimes 
9459b50d902SRodney W. Grimes /*
9469b50d902SRodney W. Grimes  * -links n functions --
9479b50d902SRodney W. Grimes  *
9489b50d902SRodney W. Grimes  *	True if the file has n links.
9499b50d902SRodney W. Grimes  */
9509b50d902SRodney W. Grimes int
951ef646f18SMark Murray f_links(PLAN *plan, FTSENT *entry)
9529b50d902SRodney W. Grimes {
9539b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
9549b50d902SRodney W. Grimes }
9559b50d902SRodney W. Grimes 
9569b50d902SRodney W. Grimes PLAN *
957ef646f18SMark Murray c_links(OPTION *option, char ***argvp)
9589b50d902SRodney W. Grimes {
959ea92232aSPoul-Henning Kamp 	char *nlinks;
9609b50d902SRodney W. Grimes 	PLAN *new;
9619b50d902SRodney W. Grimes 
962ea92232aSPoul-Henning Kamp 	nlinks = nextarg(option, argvp);
9639b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
9649b50d902SRodney W. Grimes 
965ea92232aSPoul-Henning Kamp 	new = palloc(option);
966ea92232aSPoul-Henning Kamp 	new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
967ea92232aSPoul-Henning Kamp 	return new;
9689b50d902SRodney W. Grimes }
9699b50d902SRodney W. Grimes 
9709b50d902SRodney W. Grimes /*
9719b50d902SRodney W. Grimes  * -ls functions --
9729b50d902SRodney W. Grimes  *
9739b50d902SRodney W. Grimes  *	Always true - prints the current entry to stdout in "ls" format.
9749b50d902SRodney W. Grimes  */
9759b50d902SRodney W. Grimes int
976ef646f18SMark Murray f_ls(PLAN *plan __unused, FTSENT *entry)
9779b50d902SRodney W. Grimes {
9789b50d902SRodney W. Grimes 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
979ea92232aSPoul-Henning Kamp 	return 1;
9809b50d902SRodney W. Grimes }
9819b50d902SRodney W. Grimes 
9829b50d902SRodney W. Grimes PLAN *
983ef646f18SMark Murray c_ls(OPTION *option, char ***argvp __unused)
9849b50d902SRodney W. Grimes {
9859b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
9869b50d902SRodney W. Grimes 	isoutput = 1;
9879b50d902SRodney W. Grimes 
988ea92232aSPoul-Henning Kamp 	return palloc(option);
9899b50d902SRodney W. Grimes }
9909b50d902SRodney W. Grimes 
9919b50d902SRodney W. Grimes /*
9929b50d902SRodney W. Grimes  * -name functions --
9939b50d902SRodney W. Grimes  *
9949b50d902SRodney W. Grimes  *	True if the basename of the filename being examined
9959b50d902SRodney W. Grimes  *	matches pattern using Pattern Matching Notation S3.14
9969b50d902SRodney W. Grimes  */
9979b50d902SRodney W. Grimes int
998ef646f18SMark Murray f_name(PLAN *plan, FTSENT *entry)
9999b50d902SRodney W. Grimes {
1000ea92232aSPoul-Henning Kamp 	return !fnmatch(plan->c_data, entry->fts_name,
1001ea92232aSPoul-Henning Kamp 	    plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
10029b50d902SRodney W. Grimes }
10039b50d902SRodney W. Grimes 
10049b50d902SRodney W. Grimes PLAN *
1005ef646f18SMark Murray c_name(OPTION *option, char ***argvp)
10069b50d902SRodney W. Grimes {
1007ea92232aSPoul-Henning Kamp 	char *pattern;
10089b50d902SRodney W. Grimes 	PLAN *new;
10099b50d902SRodney W. Grimes 
1010ea92232aSPoul-Henning Kamp 	pattern = nextarg(option, argvp);
1011ea92232aSPoul-Henning Kamp 	new = palloc(option);
10129b50d902SRodney W. Grimes 	new->c_data = pattern;
1013ea92232aSPoul-Henning Kamp 	return new;
10149b50d902SRodney W. Grimes }
10159b50d902SRodney W. Grimes 
10167c1d4b3aSAkinori MUSHA /*
1017ea92232aSPoul-Henning Kamp  * -newer file functions --
10187c1d4b3aSAkinori MUSHA  *
1019ea92232aSPoul-Henning Kamp  *	True if the current file has been modified more recently
1020ea92232aSPoul-Henning Kamp  *	then the modification time of the file named by the pathname
1021ea92232aSPoul-Henning Kamp  *	file.
10227c1d4b3aSAkinori MUSHA  */
10237c1d4b3aSAkinori MUSHA int
1024ef646f18SMark Murray f_newer(PLAN *plan, FTSENT *entry)
10257c1d4b3aSAkinori MUSHA {
1026ea92232aSPoul-Henning Kamp 	if (plan->flags & F_TIME_C)
1027ea92232aSPoul-Henning Kamp 		return entry->fts_statp->st_ctime > plan->t_data;
1028ea92232aSPoul-Henning Kamp 	else if (plan->flags & F_TIME_A)
1029ea92232aSPoul-Henning Kamp 		return entry->fts_statp->st_atime > plan->t_data;
1030ea92232aSPoul-Henning Kamp 	else
1031ea92232aSPoul-Henning Kamp 		return entry->fts_statp->st_mtime > plan->t_data;
10327c1d4b3aSAkinori MUSHA }
10337c1d4b3aSAkinori MUSHA 
10347c1d4b3aSAkinori MUSHA PLAN *
1035ef646f18SMark Murray c_newer(OPTION *option, char ***argvp)
10367c1d4b3aSAkinori MUSHA {
1037ea92232aSPoul-Henning Kamp 	char *fn_or_tspec;
10387c1d4b3aSAkinori MUSHA 	PLAN *new;
1039ea92232aSPoul-Henning Kamp 	struct stat sb;
10407c1d4b3aSAkinori MUSHA 
1041ea92232aSPoul-Henning Kamp 	fn_or_tspec = nextarg(option, argvp);
1042ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
1043ea92232aSPoul-Henning Kamp 
1044ea92232aSPoul-Henning Kamp 	new = palloc(option);
1045ea92232aSPoul-Henning Kamp 	/* compare against what */
1046ea92232aSPoul-Henning Kamp 	if (option->flags & F_TIME2_T) {
104748d09ba6SMark Murray 		new->t_data = get_date(fn_or_tspec, (struct timeb *) 0);
1048ea92232aSPoul-Henning Kamp 		if (new->t_data == (time_t) -1)
1049ea92232aSPoul-Henning Kamp 			errx(1, "Can't parse date/time: %s", fn_or_tspec);
1050ea92232aSPoul-Henning Kamp 	} else {
1051ea92232aSPoul-Henning Kamp 		if (stat(fn_or_tspec, &sb))
1052ea92232aSPoul-Henning Kamp 			err(1, "%s", fn_or_tspec);
1053ea92232aSPoul-Henning Kamp 		if (option->flags & F_TIME2_C)
1054ea92232aSPoul-Henning Kamp 			new->t_data = sb.st_ctime;
1055ea92232aSPoul-Henning Kamp 		else if (option->flags & F_TIME2_A)
1056ea92232aSPoul-Henning Kamp 			new->t_data = sb.st_atime;
1057ea92232aSPoul-Henning Kamp 		else
1058ea92232aSPoul-Henning Kamp 			new->t_data = sb.st_mtime;
1059ea92232aSPoul-Henning Kamp 	}
1060ea92232aSPoul-Henning Kamp 	return new;
10617c1d4b3aSAkinori MUSHA }
10627c1d4b3aSAkinori MUSHA 
1063ea92232aSPoul-Henning Kamp /*
1064ea92232aSPoul-Henning Kamp  * -nogroup functions --
1065ea92232aSPoul-Henning Kamp  *
1066ea92232aSPoul-Henning Kamp  *	True if file belongs to a user ID for which the equivalent
1067ea92232aSPoul-Henning Kamp  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1068ea92232aSPoul-Henning Kamp  */
1069ea92232aSPoul-Henning Kamp int
1070ef646f18SMark Murray f_nogroup(PLAN *plan __unused, FTSENT *entry)
1071ea92232aSPoul-Henning Kamp {
1072ea92232aSPoul-Henning Kamp 	return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1073ea92232aSPoul-Henning Kamp }
1074ea92232aSPoul-Henning Kamp 
1075ea92232aSPoul-Henning Kamp PLAN *
1076ef646f18SMark Murray c_nogroup(OPTION *option, char ***argvp __unused)
1077ea92232aSPoul-Henning Kamp {
1078ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
1079ea92232aSPoul-Henning Kamp 
1080ea92232aSPoul-Henning Kamp 	return palloc(option);
1081ea92232aSPoul-Henning Kamp }
1082ea92232aSPoul-Henning Kamp 
1083ea92232aSPoul-Henning Kamp /*
1084ea92232aSPoul-Henning Kamp  * -nouser functions --
1085ea92232aSPoul-Henning Kamp  *
1086ea92232aSPoul-Henning Kamp  *	True if file belongs to a user ID for which the equivalent
1087ea92232aSPoul-Henning Kamp  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1088ea92232aSPoul-Henning Kamp  */
1089ea92232aSPoul-Henning Kamp int
1090ef646f18SMark Murray f_nouser(PLAN *plan __unused, FTSENT *entry)
1091ea92232aSPoul-Henning Kamp {
1092ea92232aSPoul-Henning Kamp 	return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1093ea92232aSPoul-Henning Kamp }
1094ea92232aSPoul-Henning Kamp 
1095ea92232aSPoul-Henning Kamp PLAN *
1096ef646f18SMark Murray c_nouser(OPTION *option, char ***argvp __unused)
1097ea92232aSPoul-Henning Kamp {
1098ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
1099ea92232aSPoul-Henning Kamp 
1100ea92232aSPoul-Henning Kamp 	return palloc(option);
1101ea92232aSPoul-Henning Kamp }
1102ea92232aSPoul-Henning Kamp 
1103ea92232aSPoul-Henning Kamp /*
1104ea92232aSPoul-Henning Kamp  * -path functions --
1105ea92232aSPoul-Henning Kamp  *
1106ea92232aSPoul-Henning Kamp  *	True if the path of the filename being examined
1107ea92232aSPoul-Henning Kamp  *	matches pattern using Pattern Matching Notation S3.14
1108ea92232aSPoul-Henning Kamp  */
1109ea92232aSPoul-Henning Kamp int
1110ef646f18SMark Murray f_path(PLAN *plan, FTSENT *entry)
1111ea92232aSPoul-Henning Kamp {
1112ea92232aSPoul-Henning Kamp 	return !fnmatch(plan->c_data, entry->fts_path,
1113ea92232aSPoul-Henning Kamp 	    plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1114ea92232aSPoul-Henning Kamp }
1115ea92232aSPoul-Henning Kamp 
1116ea92232aSPoul-Henning Kamp /* c_path is the same as c_name */
1117ea92232aSPoul-Henning Kamp 
1118ea92232aSPoul-Henning Kamp /*
1119ea92232aSPoul-Henning Kamp  * -perm functions --
1120ea92232aSPoul-Henning Kamp  *
1121ea92232aSPoul-Henning Kamp  *	The mode argument is used to represent file mode bits.  If it starts
1122ea92232aSPoul-Henning Kamp  *	with a leading digit, it's treated as an octal mode, otherwise as a
1123ea92232aSPoul-Henning Kamp  *	symbolic mode.
1124ea92232aSPoul-Henning Kamp  */
1125ea92232aSPoul-Henning Kamp int
1126ef646f18SMark Murray f_perm(PLAN *plan, FTSENT *entry)
1127ea92232aSPoul-Henning Kamp {
1128ea92232aSPoul-Henning Kamp 	mode_t mode;
1129ea92232aSPoul-Henning Kamp 
1130ea92232aSPoul-Henning Kamp 	mode = entry->fts_statp->st_mode &
1131ea92232aSPoul-Henning Kamp 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1132ea92232aSPoul-Henning Kamp 	if (plan->flags & F_ATLEAST)
1133ea92232aSPoul-Henning Kamp 		return (plan->m_data | mode) == mode;
1134c0ff9709SRuslan Ermilov 	else if (plan->flags & F_ANY)
1135c0ff9709SRuslan Ermilov 		return (mode & plan->m_data);
1136ea92232aSPoul-Henning Kamp 	else
1137ea92232aSPoul-Henning Kamp 		return mode == plan->m_data;
1138ea92232aSPoul-Henning Kamp 	/* NOTREACHED */
1139ea92232aSPoul-Henning Kamp }
1140ea92232aSPoul-Henning Kamp 
1141ea92232aSPoul-Henning Kamp PLAN *
1142ef646f18SMark Murray c_perm(OPTION *option, char ***argvp)
1143ea92232aSPoul-Henning Kamp {
1144ea92232aSPoul-Henning Kamp 	char *perm;
1145ea92232aSPoul-Henning Kamp 	PLAN *new;
1146ea92232aSPoul-Henning Kamp 	mode_t *set;
1147ea92232aSPoul-Henning Kamp 
1148ea92232aSPoul-Henning Kamp 	perm = nextarg(option, argvp);
1149ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
1150ea92232aSPoul-Henning Kamp 
1151ea92232aSPoul-Henning Kamp 	new = palloc(option);
1152ea92232aSPoul-Henning Kamp 
1153ea92232aSPoul-Henning Kamp 	if (*perm == '-') {
1154ea92232aSPoul-Henning Kamp 		new->flags |= F_ATLEAST;
1155ea92232aSPoul-Henning Kamp 		++perm;
1156ea92232aSPoul-Henning Kamp 	} else if (*perm == '+') {
1157ea92232aSPoul-Henning Kamp 		new->flags |= F_ANY;
1158ea92232aSPoul-Henning Kamp 		++perm;
1159ea92232aSPoul-Henning Kamp 	}
1160ea92232aSPoul-Henning Kamp 
1161ea92232aSPoul-Henning Kamp 	if ((set = setmode(perm)) == NULL)
1162ea92232aSPoul-Henning Kamp 		errx(1, "%s: %s: illegal mode string", option->name, perm);
1163ea92232aSPoul-Henning Kamp 
1164ea92232aSPoul-Henning Kamp 	new->m_data = getmode(set, 0);
1165ea92232aSPoul-Henning Kamp 	free(set);
1166ea92232aSPoul-Henning Kamp 	return new;
1167ea92232aSPoul-Henning Kamp }
1168ea92232aSPoul-Henning Kamp 
1169ea92232aSPoul-Henning Kamp /*
1170ea92232aSPoul-Henning Kamp  * -print functions --
1171ea92232aSPoul-Henning Kamp  *
11729725a7b9SPhilippe Charnier  *	Always true, causes the current pathname to be written to
1173ea92232aSPoul-Henning Kamp  *	standard output.
1174ea92232aSPoul-Henning Kamp  */
1175ea92232aSPoul-Henning Kamp int
1176ef646f18SMark Murray f_print(PLAN *plan __unused, FTSENT *entry)
1177ea92232aSPoul-Henning Kamp {
1178ea92232aSPoul-Henning Kamp 	(void)puts(entry->fts_path);
1179ea92232aSPoul-Henning Kamp 	return 1;
1180ea92232aSPoul-Henning Kamp }
1181ea92232aSPoul-Henning Kamp 
1182ea92232aSPoul-Henning Kamp PLAN *
1183ef646f18SMark Murray c_print(OPTION *option, char ***argvp __unused)
1184ea92232aSPoul-Henning Kamp {
1185ea92232aSPoul-Henning Kamp 	isoutput = 1;
1186ea92232aSPoul-Henning Kamp 
1187ea92232aSPoul-Henning Kamp 	return palloc(option);
1188ea92232aSPoul-Henning Kamp }
1189ea92232aSPoul-Henning Kamp 
1190ea92232aSPoul-Henning Kamp /*
1191ea92232aSPoul-Henning Kamp  * -print0 functions --
1192ea92232aSPoul-Henning Kamp  *
11939725a7b9SPhilippe Charnier  *	Always true, causes the current pathname to be written to
1194ea92232aSPoul-Henning Kamp  *	standard output followed by a NUL character
1195ea92232aSPoul-Henning Kamp  */
1196ea92232aSPoul-Henning Kamp int
1197ef646f18SMark Murray f_print0(PLAN *plan __unused, FTSENT *entry)
1198ea92232aSPoul-Henning Kamp {
1199ea92232aSPoul-Henning Kamp 	fputs(entry->fts_path, stdout);
1200ea92232aSPoul-Henning Kamp 	fputc('\0', stdout);
1201ea92232aSPoul-Henning Kamp 	return 1;
1202ea92232aSPoul-Henning Kamp }
1203ea92232aSPoul-Henning Kamp 
1204ea92232aSPoul-Henning Kamp /* c_print0 is the same as c_print */
1205ea92232aSPoul-Henning Kamp 
1206ea92232aSPoul-Henning Kamp /*
1207ea92232aSPoul-Henning Kamp  * -prune functions --
1208ea92232aSPoul-Henning Kamp  *
1209ea92232aSPoul-Henning Kamp  *	Prune a portion of the hierarchy.
1210ea92232aSPoul-Henning Kamp  */
1211ea92232aSPoul-Henning Kamp int
1212ef646f18SMark Murray f_prune(PLAN *plan __unused, FTSENT *entry)
1213ea92232aSPoul-Henning Kamp {
1214ea92232aSPoul-Henning Kamp 	if (fts_set(tree, entry, FTS_SKIP))
1215ea92232aSPoul-Henning Kamp 		err(1, "%s", entry->fts_path);
1216ea92232aSPoul-Henning Kamp 	return 1;
1217ea92232aSPoul-Henning Kamp }
1218ea92232aSPoul-Henning Kamp 
1219ea92232aSPoul-Henning Kamp /* c_prune == c_simple */
12207c1d4b3aSAkinori MUSHA 
12217c1d4b3aSAkinori MUSHA /*
12227c1d4b3aSAkinori MUSHA  * -regex functions --
12237c1d4b3aSAkinori MUSHA  *
12247c1d4b3aSAkinori MUSHA  *	True if the whole path of the file matches pattern using
12257c1d4b3aSAkinori MUSHA  *	regular expression.
12267c1d4b3aSAkinori MUSHA  */
12277c1d4b3aSAkinori MUSHA int
1228ef646f18SMark Murray f_regex(PLAN *plan, FTSENT *entry)
12297c1d4b3aSAkinori MUSHA {
12307c1d4b3aSAkinori MUSHA 	char *str;
123147bca8b0SJuli Mallett 	int len;
12327c1d4b3aSAkinori MUSHA 	regex_t *pre;
12337c1d4b3aSAkinori MUSHA 	regmatch_t pmatch;
12347c1d4b3aSAkinori MUSHA 	int errcode;
12357c1d4b3aSAkinori MUSHA 	char errbuf[LINE_MAX];
12367c1d4b3aSAkinori MUSHA 	int matched;
12377c1d4b3aSAkinori MUSHA 
12387c1d4b3aSAkinori MUSHA 	pre = plan->re_data;
12397c1d4b3aSAkinori MUSHA 	str = entry->fts_path;
12407c1d4b3aSAkinori MUSHA 	len = strlen(str);
12417c1d4b3aSAkinori MUSHA 	matched = 0;
12427c1d4b3aSAkinori MUSHA 
12437c1d4b3aSAkinori MUSHA 	pmatch.rm_so = 0;
12447c1d4b3aSAkinori MUSHA 	pmatch.rm_eo = len;
12457c1d4b3aSAkinori MUSHA 
12467c1d4b3aSAkinori MUSHA 	errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
12477c1d4b3aSAkinori MUSHA 
12487c1d4b3aSAkinori MUSHA 	if (errcode != 0 && errcode != REG_NOMATCH) {
12497c1d4b3aSAkinori MUSHA 		regerror(errcode, pre, errbuf, sizeof errbuf);
12507c1d4b3aSAkinori MUSHA 		errx(1, "%s: %s",
1251ea92232aSPoul-Henning Kamp 		     plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
12527c1d4b3aSAkinori MUSHA 	}
12537c1d4b3aSAkinori MUSHA 
12547c1d4b3aSAkinori MUSHA 	if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
12557c1d4b3aSAkinori MUSHA 		matched = 1;
12567c1d4b3aSAkinori MUSHA 
1257ea92232aSPoul-Henning Kamp 	return matched;
12587c1d4b3aSAkinori MUSHA }
12597c1d4b3aSAkinori MUSHA 
12607c1d4b3aSAkinori MUSHA PLAN *
1261ef646f18SMark Murray c_regex(OPTION *option, char ***argvp)
12627c1d4b3aSAkinori MUSHA {
12637c1d4b3aSAkinori MUSHA 	PLAN *new;
1264ea92232aSPoul-Henning Kamp 	char *pattern;
12657c1d4b3aSAkinori MUSHA 	regex_t *pre;
12667c1d4b3aSAkinori MUSHA 	int errcode;
12677c1d4b3aSAkinori MUSHA 	char errbuf[LINE_MAX];
12687c1d4b3aSAkinori MUSHA 
12697c1d4b3aSAkinori MUSHA 	if ((pre = malloc(sizeof(regex_t))) == NULL)
12707c1d4b3aSAkinori MUSHA 		err(1, NULL);
12717c1d4b3aSAkinori MUSHA 
1272ea92232aSPoul-Henning Kamp 	pattern = nextarg(option, argvp);
1273ea92232aSPoul-Henning Kamp 
1274ea92232aSPoul-Henning Kamp 	if ((errcode = regcomp(pre, pattern,
1275ea92232aSPoul-Henning Kamp 	    regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
12767c1d4b3aSAkinori MUSHA 		regerror(errcode, pre, errbuf, sizeof errbuf);
12777c1d4b3aSAkinori MUSHA 		errx(1, "%s: %s: %s",
1278ea92232aSPoul-Henning Kamp 		     option->flags & F_IGNCASE ? "-iregex" : "-regex",
1279ea92232aSPoul-Henning Kamp 		     pattern, errbuf);
12807c1d4b3aSAkinori MUSHA 	}
12817c1d4b3aSAkinori MUSHA 
1282ea92232aSPoul-Henning Kamp 	new = palloc(option);
12837c1d4b3aSAkinori MUSHA 	new->re_data = pre;
12847c1d4b3aSAkinori MUSHA 
1285567664c4SOllivier Robert 	return new;
1286567664c4SOllivier Robert }
1287567664c4SOllivier Robert 
1288ea92232aSPoul-Henning Kamp /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */
12899b50d902SRodney W. Grimes 
12909b50d902SRodney W. Grimes PLAN *
1291ef646f18SMark Murray c_simple(OPTION *option, char ***argvp __unused)
12929b50d902SRodney W. Grimes {
1293ea92232aSPoul-Henning Kamp 	return palloc(option);
12949b50d902SRodney W. Grimes }
12959b50d902SRodney W. Grimes 
12969b50d902SRodney W. Grimes /*
12979b50d902SRodney W. Grimes  * -size n[c] functions --
12989b50d902SRodney W. Grimes  *
12999b50d902SRodney W. Grimes  *	True if the file size in bytes, divided by an implementation defined
13009b50d902SRodney W. Grimes  *	value and rounded up to the next integer, is n.  If n is followed by
13019b50d902SRodney W. Grimes  *	a c, the size is in bytes.
13029b50d902SRodney W. Grimes  */
13039b50d902SRodney W. Grimes #define	FIND_SIZE	512
13049b50d902SRodney W. Grimes static int divsize = 1;
13059b50d902SRodney W. Grimes 
13069b50d902SRodney W. Grimes int
1307ef646f18SMark Murray f_size(PLAN *plan, FTSENT *entry)
13089b50d902SRodney W. Grimes {
13099b50d902SRodney W. Grimes 	off_t size;
13109b50d902SRodney W. Grimes 
13119b50d902SRodney W. Grimes 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
13129b50d902SRodney W. Grimes 	    FIND_SIZE : entry->fts_statp->st_size;
13139b50d902SRodney W. Grimes 	COMPARE(size, plan->o_data);
13149b50d902SRodney W. Grimes }
13159b50d902SRodney W. Grimes 
13169b50d902SRodney W. Grimes PLAN *
1317ef646f18SMark Murray c_size(OPTION *option, char ***argvp)
13189b50d902SRodney W. Grimes {
1319ea92232aSPoul-Henning Kamp 	char *size_str;
13209b50d902SRodney W. Grimes 	PLAN *new;
13219b50d902SRodney W. Grimes 	char endch;
13229b50d902SRodney W. Grimes 
1323ea92232aSPoul-Henning Kamp 	size_str = nextarg(option, argvp);
13249b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
13259b50d902SRodney W. Grimes 
1326ea92232aSPoul-Henning Kamp 	new = palloc(option);
13279b50d902SRodney W. Grimes 	endch = 'c';
1328ea92232aSPoul-Henning Kamp 	new->o_data = find_parsenum(new, option->name, size_str, &endch);
13299b50d902SRodney W. Grimes 	if (endch == 'c')
13309b50d902SRodney W. Grimes 		divsize = 0;
1331ea92232aSPoul-Henning Kamp 	return new;
13329b50d902SRodney W. Grimes }
13339b50d902SRodney W. Grimes 
13349b50d902SRodney W. Grimes /*
13359b50d902SRodney W. Grimes  * -type c functions --
13369b50d902SRodney W. Grimes  *
1337841484cdSPeter Wemm  *	True if the type of the file is c, where c is b, c, d, p, f or w
1338841484cdSPeter Wemm  *	for block special file, character special file, directory, FIFO,
1339841484cdSPeter Wemm  *	regular file or whiteout respectively.
13409b50d902SRodney W. Grimes  */
13419b50d902SRodney W. Grimes int
1342ef646f18SMark Murray f_type(PLAN *plan, FTSENT *entry)
13439b50d902SRodney W. Grimes {
1344ea92232aSPoul-Henning Kamp 	return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
13459b50d902SRodney W. Grimes }
13469b50d902SRodney W. Grimes 
13479b50d902SRodney W. Grimes PLAN *
1348ef646f18SMark Murray c_type(OPTION *option, char ***argvp)
13499b50d902SRodney W. Grimes {
1350ea92232aSPoul-Henning Kamp 	char *typestring;
13519b50d902SRodney W. Grimes 	PLAN *new;
13529b50d902SRodney W. Grimes 	mode_t  mask;
13539b50d902SRodney W. Grimes 
1354ea92232aSPoul-Henning Kamp 	typestring = nextarg(option, argvp);
13559b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
13569b50d902SRodney W. Grimes 
13579b50d902SRodney W. Grimes 	switch (typestring[0]) {
13589b50d902SRodney W. Grimes 	case 'b':
13599b50d902SRodney W. Grimes 		mask = S_IFBLK;
13609b50d902SRodney W. Grimes 		break;
13619b50d902SRodney W. Grimes 	case 'c':
13629b50d902SRodney W. Grimes 		mask = S_IFCHR;
13639b50d902SRodney W. Grimes 		break;
13649b50d902SRodney W. Grimes 	case 'd':
13659b50d902SRodney W. Grimes 		mask = S_IFDIR;
13669b50d902SRodney W. Grimes 		break;
13679b50d902SRodney W. Grimes 	case 'f':
13689b50d902SRodney W. Grimes 		mask = S_IFREG;
13699b50d902SRodney W. Grimes 		break;
13709b50d902SRodney W. Grimes 	case 'l':
13719b50d902SRodney W. Grimes 		mask = S_IFLNK;
13729b50d902SRodney W. Grimes 		break;
13739b50d902SRodney W. Grimes 	case 'p':
13749b50d902SRodney W. Grimes 		mask = S_IFIFO;
13759b50d902SRodney W. Grimes 		break;
13769b50d902SRodney W. Grimes 	case 's':
13779b50d902SRodney W. Grimes 		mask = S_IFSOCK;
13789b50d902SRodney W. Grimes 		break;
1379841484cdSPeter Wemm #ifdef FTS_WHITEOUT
1380841484cdSPeter Wemm 	case 'w':
1381841484cdSPeter Wemm 		mask = S_IFWHT;
1382841484cdSPeter Wemm 		ftsoptions |= FTS_WHITEOUT;
1383841484cdSPeter Wemm 		break;
1384841484cdSPeter Wemm #endif /* FTS_WHITEOUT */
13859b50d902SRodney W. Grimes 	default:
1386ea92232aSPoul-Henning Kamp 		errx(1, "%s: %s: unknown type", option->name, typestring);
13879b50d902SRodney W. Grimes 	}
13889b50d902SRodney W. Grimes 
1389ea92232aSPoul-Henning Kamp 	new = palloc(option);
13909b50d902SRodney W. Grimes 	new->m_data = mask;
1391ea92232aSPoul-Henning Kamp 	return new;
1392abacbbbfSPeter Wemm }
1393abacbbbfSPeter Wemm 
1394abacbbbfSPeter Wemm /*
13959b50d902SRodney W. Grimes  * -user uname functions --
13969b50d902SRodney W. Grimes  *
13979b50d902SRodney W. Grimes  *	True if the file belongs to the user uname.  If uname is numeric and
13989b50d902SRodney W. Grimes  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
13999b50d902SRodney W. Grimes  *	return a valid user name, uname is taken as a user ID.
14009b50d902SRodney W. Grimes  */
14019b50d902SRodney W. Grimes int
1402ef646f18SMark Murray f_user(PLAN *plan, FTSENT *entry)
14039b50d902SRodney W. Grimes {
1404ea92232aSPoul-Henning Kamp 	return entry->fts_statp->st_uid == plan->u_data;
14059b50d902SRodney W. Grimes }
14069b50d902SRodney W. Grimes 
14079b50d902SRodney W. Grimes PLAN *
1408ef646f18SMark Murray c_user(OPTION *option, char ***argvp)
14099b50d902SRodney W. Grimes {
1410ea92232aSPoul-Henning Kamp 	char *username;
14119b50d902SRodney W. Grimes 	PLAN *new;
14129b50d902SRodney W. Grimes 	struct passwd *p;
14139b50d902SRodney W. Grimes 	uid_t uid;
14149b50d902SRodney W. Grimes 
1415ea92232aSPoul-Henning Kamp 	username = nextarg(option, argvp);
14169b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
14179b50d902SRodney W. Grimes 
14189b50d902SRodney W. Grimes 	p = getpwnam(username);
14199b50d902SRodney W. Grimes 	if (p == NULL) {
14209b50d902SRodney W. Grimes 		uid = atoi(username);
14219b50d902SRodney W. Grimes 		if (uid == 0 && username[0] != '0')
1422ea92232aSPoul-Henning Kamp 			errx(1, "%s: %s: no such user", option->name, username);
14239b50d902SRodney W. Grimes 	} else
14249b50d902SRodney W. Grimes 		uid = p->pw_uid;
14259b50d902SRodney W. Grimes 
1426ea92232aSPoul-Henning Kamp 	new = palloc(option);
14279b50d902SRodney W. Grimes 	new->u_data = uid;
1428ea92232aSPoul-Henning Kamp 	return new;
14299b50d902SRodney W. Grimes }
14309b50d902SRodney W. Grimes 
14319b50d902SRodney W. Grimes /*
14329b50d902SRodney W. Grimes  * -xdev functions --
14339b50d902SRodney W. Grimes  *
14349725a7b9SPhilippe Charnier  *	Always true, causes find not to descend past directories that have a
14359b50d902SRodney W. Grimes  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
14369b50d902SRodney W. Grimes  */
14379b50d902SRodney W. Grimes PLAN *
1438ef646f18SMark Murray c_xdev(OPTION *option, char ***argvp __unused)
14399b50d902SRodney W. Grimes {
14409b50d902SRodney W. Grimes 	ftsoptions |= FTS_XDEV;
14419b50d902SRodney W. Grimes 
1442ea92232aSPoul-Henning Kamp 	return palloc(option);
14439b50d902SRodney W. Grimes }
14449b50d902SRodney W. Grimes 
14459b50d902SRodney W. Grimes /*
14469b50d902SRodney W. Grimes  * ( expression ) functions --
14479b50d902SRodney W. Grimes  *
14489b50d902SRodney W. Grimes  *	True if expression is true.
14499b50d902SRodney W. Grimes  */
14509b50d902SRodney W. Grimes int
1451ef646f18SMark Murray f_expr(PLAN *plan, FTSENT *entry)
14529b50d902SRodney W. Grimes {
1453e98080b1SDavid Malone 	PLAN *p;
1454e98080b1SDavid Malone 	int state = 0;
14559b50d902SRodney W. Grimes 
14569b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
1457ea92232aSPoul-Henning Kamp 	    p && (state = (p->execute)(p, entry)); p = p->next);
1458ea92232aSPoul-Henning Kamp 	return state;
14599b50d902SRodney W. Grimes }
14609b50d902SRodney W. Grimes 
14619b50d902SRodney W. Grimes /*
1462ea92232aSPoul-Henning Kamp  * f_openparen and f_closeparen nodes are temporary place markers.  They are
14639b50d902SRodney W. Grimes  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1464ea92232aSPoul-Henning Kamp  * to a f_expr node containing the expression and the ')' node is discarded.
1465ea92232aSPoul-Henning Kamp  * The functions themselves are only used as constants.
14669b50d902SRodney W. Grimes  */
1467ea92232aSPoul-Henning Kamp 
1468ea92232aSPoul-Henning Kamp int
1469ef646f18SMark Murray f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
14709b50d902SRodney W. Grimes {
1471ea92232aSPoul-Henning Kamp 	abort();
14729b50d902SRodney W. Grimes }
14739b50d902SRodney W. Grimes 
1474ea92232aSPoul-Henning Kamp int
1475ef646f18SMark Murray f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
14769b50d902SRodney W. Grimes {
1477ea92232aSPoul-Henning Kamp 	abort();
1478ea92232aSPoul-Henning Kamp }
1479ea92232aSPoul-Henning Kamp 
1480ea92232aSPoul-Henning Kamp /* c_openparen == c_simple */
1481ea92232aSPoul-Henning Kamp /* c_closeparen == c_simple */
1482ea92232aSPoul-Henning Kamp 
1483ea92232aSPoul-Henning Kamp /*
1484ea92232aSPoul-Henning Kamp  * AND operator. Since AND is implicit, no node is allocated.
1485ea92232aSPoul-Henning Kamp  */
1486ea92232aSPoul-Henning Kamp PLAN *
1487ef646f18SMark Murray c_and(OPTION *option __unused, char ***argvp __unused)
1488ea92232aSPoul-Henning Kamp {
1489ea92232aSPoul-Henning Kamp 	return NULL;
14909b50d902SRodney W. Grimes }
14919b50d902SRodney W. Grimes 
14929b50d902SRodney W. Grimes /*
14939b50d902SRodney W. Grimes  * ! expression functions --
14949b50d902SRodney W. Grimes  *
14959b50d902SRodney W. Grimes  *	Negation of a primary; the unary NOT operator.
14969b50d902SRodney W. Grimes  */
14979b50d902SRodney W. Grimes int
1498ef646f18SMark Murray f_not(PLAN *plan, FTSENT *entry)
14999b50d902SRodney W. Grimes {
1500e98080b1SDavid Malone 	PLAN *p;
1501e98080b1SDavid Malone 	int state = 0;
15029b50d902SRodney W. Grimes 
15039b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
1504ea92232aSPoul-Henning Kamp 	    p && (state = (p->execute)(p, entry)); p = p->next);
1505ea92232aSPoul-Henning Kamp 	return !state;
15069b50d902SRodney W. Grimes }
15079b50d902SRodney W. Grimes 
1508ea92232aSPoul-Henning Kamp /* c_not == c_simple */
15099b50d902SRodney W. Grimes 
15109b50d902SRodney W. Grimes /*
15119b50d902SRodney W. Grimes  * expression -o expression functions --
15129b50d902SRodney W. Grimes  *
15139b50d902SRodney W. Grimes  *	Alternation of primaries; the OR operator.  The second expression is
15149b50d902SRodney W. Grimes  * not evaluated if the first expression is true.
15159b50d902SRodney W. Grimes  */
15169b50d902SRodney W. Grimes int
1517ef646f18SMark Murray f_or(PLAN *plan, FTSENT *entry)
15189b50d902SRodney W. Grimes {
1519e98080b1SDavid Malone 	PLAN *p;
1520e98080b1SDavid Malone 	int state = 0;
15219b50d902SRodney W. Grimes 
15229b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
1523ea92232aSPoul-Henning Kamp 	    p && (state = (p->execute)(p, entry)); p = p->next);
15249b50d902SRodney W. Grimes 
15259b50d902SRodney W. Grimes 	if (state)
1526ea92232aSPoul-Henning Kamp 		return 1;
15279b50d902SRodney W. Grimes 
15289b50d902SRodney W. Grimes 	for (p = plan->p_data[1];
1529ea92232aSPoul-Henning Kamp 	    p && (state = (p->execute)(p, entry)); p = p->next);
1530ea92232aSPoul-Henning Kamp 	return state;
15319b50d902SRodney W. Grimes }
15329b50d902SRodney W. Grimes 
1533ea92232aSPoul-Henning Kamp /* c_or == c_simple */
1534