xref: /freebsd/usr.bin/find/function.c (revision b547523eb468d33e37873ed99df3cb0046985b71)
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  * 4. Neither the name of the University nor the names of its contributors
179b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
189b50d902SRodney W. Grimes  *    without specific prior written permission.
199b50d902SRodney W. Grimes  *
209b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
219b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
249b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309b50d902SRodney W. Grimes  * SUCH DAMAGE.
319b50d902SRodney W. Grimes  */
329b50d902SRodney W. Grimes 
333549ef2fSXin LI #ifndef lint
343549ef2fSXin LI #if 0
353549ef2fSXin LI static const char sccsid[] = "@(#)function.c	8.10 (Berkeley) 5/4/95";
363549ef2fSXin LI #endif
373549ef2fSXin LI #endif /* not lint */
383549ef2fSXin LI 
393077469eSDavid E. O'Brien #include <sys/cdefs.h>
403077469eSDavid E. O'Brien __FBSDID("$FreeBSD$");
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes #include <sys/param.h>
439b50d902SRodney W. Grimes #include <sys/ucred.h>
449b50d902SRodney W. Grimes #include <sys/stat.h>
459c5d31dfSBosko Milekic #include <sys/types.h>
469c5d31dfSBosko Milekic #include <sys/acl.h>
479b50d902SRodney W. Grimes #include <sys/wait.h>
489b50d902SRodney W. Grimes #include <sys/mount.h>
499b50d902SRodney W. Grimes 
50ed1a4621SPeter Wemm #include <dirent.h>
519b50d902SRodney W. Grimes #include <err.h>
529b50d902SRodney W. Grimes #include <errno.h>
539b50d902SRodney W. Grimes #include <fnmatch.h>
549b50d902SRodney W. Grimes #include <fts.h>
559b50d902SRodney W. Grimes #include <grp.h>
565e25d888STim J. Robbins #include <limits.h>
579b50d902SRodney W. Grimes #include <pwd.h>
587c1d4b3aSAkinori MUSHA #include <regex.h>
599b50d902SRodney W. Grimes #include <stdio.h>
609b50d902SRodney W. Grimes #include <stdlib.h>
619b50d902SRodney W. Grimes #include <string.h>
629b50d902SRodney W. Grimes #include <unistd.h>
631c832963SOliver Eikemeier #include <ctype.h>
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes #include "find.h"
669b50d902SRodney W. Grimes 
67ecca1f1cSMark Murray static PLAN *palloc(OPTION *);
68ecca1f1cSMark Murray static long long find_parsenum(PLAN *, const char *, char *, char *);
69ecca1f1cSMark Murray static long long find_parsetime(PLAN *, const char *, char *);
70ecca1f1cSMark Murray static char *nextarg(OPTION *, char ***);
71ea92232aSPoul-Henning Kamp 
72a07af811STim J. Robbins extern char **environ;
73a07af811STim J. Robbins 
7422170420SKirill Ponomarev static PLAN *lastexecplus = NULL;
7522170420SKirill Ponomarev 
76adff4fcaSRuslan Ermilov #define	COMPARE(a, b) do {						\
77ea92232aSPoul-Henning Kamp 	switch (plan->flags & F_ELG_MASK) {				\
789b50d902SRodney W. Grimes 	case F_EQUAL:							\
799b50d902SRodney W. Grimes 		return (a == b);					\
809b50d902SRodney W. Grimes 	case F_LESSTHAN:						\
819b50d902SRodney W. Grimes 		return (a < b);						\
829b50d902SRodney W. Grimes 	case F_GREATER:							\
839b50d902SRodney W. Grimes 		return (a > b);						\
849b50d902SRodney W. Grimes 	default:							\
859b50d902SRodney W. Grimes 		abort();						\
869b50d902SRodney W. Grimes 	}								\
87adff4fcaSRuslan Ermilov } while(0)
889b50d902SRodney W. Grimes 
89ea92232aSPoul-Henning Kamp static PLAN *
90ef646f18SMark Murray palloc(OPTION *option)
91ea92232aSPoul-Henning Kamp {
92ea92232aSPoul-Henning Kamp 	PLAN *new;
93ea92232aSPoul-Henning Kamp 
94ea92232aSPoul-Henning Kamp 	if ((new = malloc(sizeof(PLAN))) == NULL)
95ea92232aSPoul-Henning Kamp 		err(1, NULL);
96ea92232aSPoul-Henning Kamp 	new->execute = option->execute;
97ea92232aSPoul-Henning Kamp 	new->flags = option->flags;
98ea92232aSPoul-Henning Kamp 	new->next = NULL;
99ea92232aSPoul-Henning Kamp 	return new;
100ea92232aSPoul-Henning Kamp }
1019b50d902SRodney W. Grimes 
1029b50d902SRodney W. Grimes /*
1039b50d902SRodney W. Grimes  * find_parsenum --
1049b50d902SRodney W. Grimes  *	Parse a string of the form [+-]# and return the value.
1059b50d902SRodney W. Grimes  */
1069192bbf4SBruce Evans static long long
107ef646f18SMark Murray find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
1089b50d902SRodney W. Grimes {
1099192bbf4SBruce Evans 	long long value;
1109b50d902SRodney W. Grimes 	char *endchar, *str;	/* Pointer to character ending conversion. */
1119b50d902SRodney W. Grimes 
1129b50d902SRodney W. Grimes 	/* Determine comparison from leading + or -. */
1139b50d902SRodney W. Grimes 	str = vp;
1149b50d902SRodney W. Grimes 	switch (*str) {
1159b50d902SRodney W. Grimes 	case '+':
1169b50d902SRodney W. Grimes 		++str;
117ea92232aSPoul-Henning Kamp 		plan->flags |= F_GREATER;
1189b50d902SRodney W. Grimes 		break;
1199b50d902SRodney W. Grimes 	case '-':
1209b50d902SRodney W. Grimes 		++str;
121ea92232aSPoul-Henning Kamp 		plan->flags |= F_LESSTHAN;
1229b50d902SRodney W. Grimes 		break;
1239b50d902SRodney W. Grimes 	default:
124ea92232aSPoul-Henning Kamp 		plan->flags |= F_EQUAL;
1259b50d902SRodney W. Grimes 		break;
1269b50d902SRodney W. Grimes 	}
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes 	/*
1299192bbf4SBruce Evans 	 * Convert the string with strtoq().  Note, if strtoq() returns zero
1309b50d902SRodney W. Grimes 	 * and endchar points to the beginning of the string we know we have
1319b50d902SRodney W. Grimes 	 * a syntax error.
1329b50d902SRodney W. Grimes 	 */
1339192bbf4SBruce Evans 	value = strtoq(str, &endchar, 10);
1349b50d902SRodney W. Grimes 	if (value == 0 && endchar == str)
1359b50d902SRodney W. Grimes 		errx(1, "%s: %s: illegal numeric value", option, vp);
1365a890aacSKirill Ponomarev 	if (endchar[0] && endch == NULL)
1379b50d902SRodney W. Grimes 		errx(1, "%s: %s: illegal trailing character", option, vp);
1389b50d902SRodney W. Grimes 	if (endch)
1399b50d902SRodney W. Grimes 		*endch = endchar[0];
140ea92232aSPoul-Henning Kamp 	return value;
1419b50d902SRodney W. Grimes }
1429b50d902SRodney W. Grimes 
1439b50d902SRodney W. Grimes /*
144adff4fcaSRuslan Ermilov  * find_parsetime --
145adff4fcaSRuslan Ermilov  *	Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
146adff4fcaSRuslan Ermilov  */
147adff4fcaSRuslan Ermilov static long long
148ef646f18SMark Murray find_parsetime(PLAN *plan, const char *option, char *vp)
149adff4fcaSRuslan Ermilov {
150adff4fcaSRuslan Ermilov 	long long secs, value;
151adff4fcaSRuslan Ermilov 	char *str, *unit;	/* Pointer to character ending conversion. */
152adff4fcaSRuslan Ermilov 
153adff4fcaSRuslan Ermilov 	/* Determine comparison from leading + or -. */
154adff4fcaSRuslan Ermilov 	str = vp;
155adff4fcaSRuslan Ermilov 	switch (*str) {
156adff4fcaSRuslan Ermilov 	case '+':
157adff4fcaSRuslan Ermilov 		++str;
158adff4fcaSRuslan Ermilov 		plan->flags |= F_GREATER;
159adff4fcaSRuslan Ermilov 		break;
160adff4fcaSRuslan Ermilov 	case '-':
161adff4fcaSRuslan Ermilov 		++str;
162adff4fcaSRuslan Ermilov 		plan->flags |= F_LESSTHAN;
163adff4fcaSRuslan Ermilov 		break;
164adff4fcaSRuslan Ermilov 	default:
165adff4fcaSRuslan Ermilov 		plan->flags |= F_EQUAL;
166adff4fcaSRuslan Ermilov 		break;
167adff4fcaSRuslan Ermilov 	}
168adff4fcaSRuslan Ermilov 
169adff4fcaSRuslan Ermilov 	value = strtoq(str, &unit, 10);
170adff4fcaSRuslan Ermilov 	if (value == 0 && unit == str) {
171adff4fcaSRuslan Ermilov 		errx(1, "%s: %s: illegal time value", option, vp);
172adff4fcaSRuslan Ermilov 		/* NOTREACHED */
173adff4fcaSRuslan Ermilov 	}
174adff4fcaSRuslan Ermilov 	if (*unit == '\0')
175adff4fcaSRuslan Ermilov 		return value;
176adff4fcaSRuslan Ermilov 
177adff4fcaSRuslan Ermilov 	/* Units syntax. */
178adff4fcaSRuslan Ermilov 	secs = 0;
179adff4fcaSRuslan Ermilov 	for (;;) {
180adff4fcaSRuslan Ermilov 		switch(*unit) {
181adff4fcaSRuslan Ermilov 		case 's':	/* seconds */
182adff4fcaSRuslan Ermilov 			secs += value;
183adff4fcaSRuslan Ermilov 			break;
184adff4fcaSRuslan Ermilov 		case 'm':	/* minutes */
185adff4fcaSRuslan Ermilov 			secs += value * 60;
186adff4fcaSRuslan Ermilov 			break;
187adff4fcaSRuslan Ermilov 		case 'h':	/* hours */
188adff4fcaSRuslan Ermilov 			secs += value * 3600;
189adff4fcaSRuslan Ermilov 			break;
190adff4fcaSRuslan Ermilov 		case 'd':	/* days */
191adff4fcaSRuslan Ermilov 			secs += value * 86400;
192adff4fcaSRuslan Ermilov 			break;
193adff4fcaSRuslan Ermilov 		case 'w':	/* weeks */
194adff4fcaSRuslan Ermilov 			secs += value * 604800;
195adff4fcaSRuslan Ermilov 			break;
196adff4fcaSRuslan Ermilov 		default:
197adff4fcaSRuslan Ermilov 			errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
198adff4fcaSRuslan Ermilov 			/* NOTREACHED */
199adff4fcaSRuslan Ermilov 		}
200adff4fcaSRuslan Ermilov 		str = unit + 1;
201adff4fcaSRuslan Ermilov 		if (*str == '\0')	/* EOS */
202adff4fcaSRuslan Ermilov 			break;
203adff4fcaSRuslan Ermilov 		value = strtoq(str, &unit, 10);
204adff4fcaSRuslan Ermilov 		if (value == 0 && unit == str) {
205adff4fcaSRuslan Ermilov 			errx(1, "%s: %s: illegal time value", option, vp);
206adff4fcaSRuslan Ermilov 			/* NOTREACHED */
207adff4fcaSRuslan Ermilov 		}
208adff4fcaSRuslan Ermilov 		if (*unit == '\0') {
209adff4fcaSRuslan Ermilov 			errx(1, "%s: %s: missing trailing unit", option, vp);
210adff4fcaSRuslan Ermilov 			/* NOTREACHED */
211adff4fcaSRuslan Ermilov 		}
212adff4fcaSRuslan Ermilov 	}
213adff4fcaSRuslan Ermilov 	plan->flags |= F_EXACTTIME;
214adff4fcaSRuslan Ermilov 	return secs;
215adff4fcaSRuslan Ermilov }
216adff4fcaSRuslan Ermilov 
217adff4fcaSRuslan Ermilov /*
218ea92232aSPoul-Henning Kamp  * nextarg --
219ea92232aSPoul-Henning Kamp  *	Check that another argument still exists, return a pointer to it,
220ea92232aSPoul-Henning Kamp  *	and increment the argument vector pointer.
221ea92232aSPoul-Henning Kamp  */
222ea92232aSPoul-Henning Kamp static char *
223ef646f18SMark Murray nextarg(OPTION *option, char ***argvp)
224ea92232aSPoul-Henning Kamp {
225ea92232aSPoul-Henning Kamp 	char *arg;
226ea92232aSPoul-Henning Kamp 
227ea92232aSPoul-Henning Kamp 	if ((arg = **argvp) == 0)
228ea92232aSPoul-Henning Kamp 		errx(1, "%s: requires additional arguments", option->name);
229ea92232aSPoul-Henning Kamp 	(*argvp)++;
230ea92232aSPoul-Henning Kamp 	return arg;
231ea92232aSPoul-Henning Kamp } /* nextarg() */
232ea92232aSPoul-Henning Kamp 
233ea92232aSPoul-Henning Kamp /*
23431d53425SCeri Davies  * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
23531d53425SCeri Davies  * range, i.e. n matches from (n - 1) to n 24 hour periods.  This interacts
23631d53425SCeri Davies  * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
23731d53425SCeri Davies  * the user wanted.  Correct so that -1 is "less than 1".
2389b50d902SRodney W. Grimes  */
239ea92232aSPoul-Henning Kamp #define	TIME_CORRECT(p) \
240ea92232aSPoul-Henning Kamp 	if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
241ddd956b0SJilles Tjoelker 		++((p)->t_data.tv_sec);
2429b50d902SRodney W. Grimes 
2439b50d902SRodney W. Grimes /*
244ea92232aSPoul-Henning Kamp  * -[acm]min n functions --
2453f5223f8SWolfram Schneider  *
246ea92232aSPoul-Henning Kamp  *    True if the difference between the
247ea92232aSPoul-Henning Kamp  *		file access time (-amin)
24831d53425SCeri Davies  *		file birth time (-Bmin)
249ea92232aSPoul-Henning Kamp  *		last change of file status information (-cmin)
250ea92232aSPoul-Henning Kamp  *		file modification time (-mmin)
251ea92232aSPoul-Henning Kamp  *    and the current time is n min periods.
2523f5223f8SWolfram Schneider  */
2533f5223f8SWolfram Schneider int
254ef646f18SMark Murray f_Xmin(PLAN *plan, FTSENT *entry)
2553f5223f8SWolfram Schneider {
256ea92232aSPoul-Henning Kamp 	if (plan->flags & F_TIME_C) {
2573f5223f8SWolfram Schneider 		COMPARE((now - entry->fts_statp->st_ctime +
258ddd956b0SJilles Tjoelker 		    60 - 1) / 60, plan->t_data.tv_sec);
259ea92232aSPoul-Henning Kamp 	} else if (plan->flags & F_TIME_A) {
260ea92232aSPoul-Henning Kamp 		COMPARE((now - entry->fts_statp->st_atime +
261ddd956b0SJilles Tjoelker 		    60 - 1) / 60, plan->t_data.tv_sec);
26231d53425SCeri Davies 	} else if (plan->flags & F_TIME_B) {
26331d53425SCeri Davies 		COMPARE((now - entry->fts_statp->st_birthtime +
264ddd956b0SJilles Tjoelker 		    60 - 1) / 60, plan->t_data.tv_sec);
265ea92232aSPoul-Henning Kamp 	} else {
266ea92232aSPoul-Henning Kamp 		COMPARE((now - entry->fts_statp->st_mtime +
267ddd956b0SJilles Tjoelker 		    60 - 1) / 60, plan->t_data.tv_sec);
268ea92232aSPoul-Henning Kamp 	}
2693f5223f8SWolfram Schneider }
2703f5223f8SWolfram Schneider 
2713f5223f8SWolfram Schneider PLAN *
272ef646f18SMark Murray c_Xmin(OPTION *option, char ***argvp)
2733f5223f8SWolfram Schneider {
274ea92232aSPoul-Henning Kamp 	char *nmins;
2753f5223f8SWolfram Schneider 	PLAN *new;
2763f5223f8SWolfram Schneider 
277ea92232aSPoul-Henning Kamp 	nmins = nextarg(option, argvp);
2783f5223f8SWolfram Schneider 	ftsoptions &= ~FTS_NOSTAT;
2793f5223f8SWolfram Schneider 
280ea92232aSPoul-Henning Kamp 	new = palloc(option);
281ddd956b0SJilles Tjoelker 	new->t_data.tv_sec = find_parsenum(new, option->name, nmins, NULL);
282ddd956b0SJilles Tjoelker 	new->t_data.tv_nsec = 0;
283ea92232aSPoul-Henning Kamp 	TIME_CORRECT(new);
284ea92232aSPoul-Henning Kamp 	return new;
2853f5223f8SWolfram Schneider }
2863f5223f8SWolfram Schneider 
2879b50d902SRodney W. Grimes /*
288ea92232aSPoul-Henning Kamp  * -[acm]time n functions --
2899b50d902SRodney W. Grimes  *
290ea92232aSPoul-Henning Kamp  *	True if the difference between the
291ea92232aSPoul-Henning Kamp  *		file access time (-atime)
29231d53425SCeri Davies  *		file birth time (-Btime)
293ea92232aSPoul-Henning Kamp  *		last change of file status information (-ctime)
294ea92232aSPoul-Henning Kamp  *		file modification time (-mtime)
295ea92232aSPoul-Henning Kamp  *	and the current time is n 24 hour periods.
2969b50d902SRodney W. Grimes  */
297ea92232aSPoul-Henning Kamp 
2989b50d902SRodney W. Grimes int
299ef646f18SMark Murray f_Xtime(PLAN *plan, FTSENT *entry)
3009b50d902SRodney W. Grimes {
301631a8765SRuslan Ermilov 	time_t xtime;
302adff4fcaSRuslan Ermilov 
303631a8765SRuslan Ermilov 	if (plan->flags & F_TIME_A)
304631a8765SRuslan Ermilov 		xtime = entry->fts_statp->st_atime;
30531d53425SCeri Davies 	else if (plan->flags & F_TIME_B)
30631d53425SCeri Davies 		xtime = entry->fts_statp->st_birthtime;
307631a8765SRuslan Ermilov 	else if (plan->flags & F_TIME_C)
308631a8765SRuslan Ermilov 		xtime = entry->fts_statp->st_ctime;
309631a8765SRuslan Ermilov 	else
310631a8765SRuslan Ermilov 		xtime = entry->fts_statp->st_mtime;
3119b50d902SRodney W. Grimes 
312631a8765SRuslan Ermilov 	if (plan->flags & F_EXACTTIME)
313ddd956b0SJilles Tjoelker 		COMPARE(now - xtime, plan->t_data.tv_sec);
314adff4fcaSRuslan Ermilov 	else
315ddd956b0SJilles Tjoelker 		COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data.tv_sec);
3169b50d902SRodney W. Grimes }
3179b50d902SRodney W. Grimes 
3189b50d902SRodney W. Grimes PLAN *
319ef646f18SMark Murray c_Xtime(OPTION *option, char ***argvp)
3209b50d902SRodney W. Grimes {
321adff4fcaSRuslan Ermilov 	char *value;
3229b50d902SRodney W. Grimes 	PLAN *new;
3239b50d902SRodney W. Grimes 
324adff4fcaSRuslan Ermilov 	value = nextarg(option, argvp);
3259b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
3269b50d902SRodney W. Grimes 
327ea92232aSPoul-Henning Kamp 	new = palloc(option);
328ddd956b0SJilles Tjoelker 	new->t_data.tv_sec = find_parsetime(new, option->name, value);
329ddd956b0SJilles Tjoelker 	new->t_data.tv_nsec = 0;
330adff4fcaSRuslan Ermilov 	if (!(new->flags & F_EXACTTIME))
331ea92232aSPoul-Henning Kamp 		TIME_CORRECT(new);
332ea92232aSPoul-Henning Kamp 	return new;
333ea92232aSPoul-Henning Kamp }
334ea92232aSPoul-Henning Kamp 
335ea92232aSPoul-Henning Kamp /*
336ea92232aSPoul-Henning Kamp  * -maxdepth/-mindepth n functions --
337ea92232aSPoul-Henning Kamp  *
338ea92232aSPoul-Henning Kamp  *        Does the same as -prune if the level of the current file is
339ea92232aSPoul-Henning Kamp  *        greater/less than the specified maximum/minimum depth.
340ea92232aSPoul-Henning Kamp  *
341ea92232aSPoul-Henning Kamp  *        Note that -maxdepth and -mindepth are handled specially in
342ea92232aSPoul-Henning Kamp  *        find_execute() so their f_* functions are set to f_always_true().
343ea92232aSPoul-Henning Kamp  */
344ea92232aSPoul-Henning Kamp PLAN *
345ef646f18SMark Murray c_mXXdepth(OPTION *option, char ***argvp)
346ea92232aSPoul-Henning Kamp {
347ea92232aSPoul-Henning Kamp 	char *dstr;
348ea92232aSPoul-Henning Kamp 	PLAN *new;
349ea92232aSPoul-Henning Kamp 
350ea92232aSPoul-Henning Kamp 	dstr = nextarg(option, argvp);
351ea92232aSPoul-Henning Kamp 	if (dstr[0] == '-')
352ea92232aSPoul-Henning Kamp 		/* all other errors handled by find_parsenum() */
353ea92232aSPoul-Henning Kamp 		errx(1, "%s: %s: value must be positive", option->name, dstr);
354ea92232aSPoul-Henning Kamp 
355ea92232aSPoul-Henning Kamp 	new = palloc(option);
356ea92232aSPoul-Henning Kamp 	if (option->flags & F_MAXDEPTH)
357ea92232aSPoul-Henning Kamp 		maxdepth = find_parsenum(new, option->name, dstr, NULL);
358ea92232aSPoul-Henning Kamp 	else
359ea92232aSPoul-Henning Kamp 		mindepth = find_parsenum(new, option->name, dstr, NULL);
360ea92232aSPoul-Henning Kamp 	return new;
361ea92232aSPoul-Henning Kamp }
362ea92232aSPoul-Henning Kamp 
363ea92232aSPoul-Henning Kamp /*
3649c5d31dfSBosko Milekic  * -acl function --
3659c5d31dfSBosko Milekic  *
3669c5d31dfSBosko Milekic  *	Show files with EXTENDED ACL attributes.
3679c5d31dfSBosko Milekic  */
3689c5d31dfSBosko Milekic int
3699c5d31dfSBosko Milekic f_acl(PLAN *plan __unused, FTSENT *entry)
3709c5d31dfSBosko Milekic {
3719c5d31dfSBosko Milekic 	acl_t facl;
372fa2db914SEdward Tomasz Napierala 	acl_type_t acl_type;
373fa2db914SEdward Tomasz Napierala 	int acl_supported = 0, ret, trivial;
3749c5d31dfSBosko Milekic 
3759c5d31dfSBosko Milekic 	if (S_ISLNK(entry->fts_statp->st_mode))
3769c5d31dfSBosko Milekic 		return 0;
377fa2db914SEdward Tomasz Napierala 	ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4);
378fa2db914SEdward Tomasz Napierala 	if (ret > 0) {
379fa2db914SEdward Tomasz Napierala 		acl_supported = 1;
380fa2db914SEdward Tomasz Napierala 		acl_type = ACL_TYPE_NFS4;
381fa2db914SEdward Tomasz Napierala 	} else if (ret < 0 && errno != EINVAL) {
3829c5d31dfSBosko Milekic 		warn("%s", entry->fts_accpath);
383fa2db914SEdward Tomasz Napierala 		return (0);
3849c5d31dfSBosko Milekic 	}
385fa2db914SEdward Tomasz Napierala 	if (acl_supported == 0) {
386fa2db914SEdward Tomasz Napierala 		ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED);
387fa2db914SEdward Tomasz Napierala 		if (ret > 0) {
388fa2db914SEdward Tomasz Napierala 			acl_supported = 1;
389fa2db914SEdward Tomasz Napierala 			acl_type = ACL_TYPE_ACCESS;
390fa2db914SEdward Tomasz Napierala 		} else if (ret < 0 && errno != EINVAL) {
391fa2db914SEdward Tomasz Napierala 			warn("%s", entry->fts_accpath);
392fa2db914SEdward Tomasz Napierala 			return (0);
3939c5d31dfSBosko Milekic 		}
3949c5d31dfSBosko Milekic 	}
395fa2db914SEdward Tomasz Napierala 	if (acl_supported == 0)
396fa2db914SEdward Tomasz Napierala 		return (0);
397fa2db914SEdward Tomasz Napierala 
398fa2db914SEdward Tomasz Napierala 	facl = acl_get_file(entry->fts_accpath, acl_type);
399fa2db914SEdward Tomasz Napierala 	if (facl == NULL) {
400fa2db914SEdward Tomasz Napierala 		warn("%s", entry->fts_accpath);
401fa2db914SEdward Tomasz Napierala 		return (0);
4029c5d31dfSBosko Milekic 	}
403fa2db914SEdward Tomasz Napierala 	ret = acl_is_trivial_np(facl, &trivial);
4049c5d31dfSBosko Milekic 	acl_free(facl);
405fa2db914SEdward Tomasz Napierala 	if (ret) {
4069c5d31dfSBosko Milekic 		warn("%s", entry->fts_accpath);
407fa2db914SEdward Tomasz Napierala 		acl_free(facl);
408fa2db914SEdward Tomasz Napierala 		return (0);
409fa2db914SEdward Tomasz Napierala 	}
410fa2db914SEdward Tomasz Napierala 	if (trivial)
411fa2db914SEdward Tomasz Napierala 		return (0);
412fa2db914SEdward Tomasz Napierala 	return (1);
4139c5d31dfSBosko Milekic }
4149c5d31dfSBosko Milekic 
4159c5d31dfSBosko Milekic PLAN *
4169c5d31dfSBosko Milekic c_acl(OPTION *option, char ***argvp __unused)
4179c5d31dfSBosko Milekic {
4189c5d31dfSBosko Milekic 	ftsoptions &= ~FTS_NOSTAT;
4199c5d31dfSBosko Milekic 	return (palloc(option));
4209c5d31dfSBosko Milekic }
4219c5d31dfSBosko Milekic 
4229c5d31dfSBosko Milekic /*
423ea92232aSPoul-Henning Kamp  * -delete functions --
424ea92232aSPoul-Henning Kamp  *
425ea92232aSPoul-Henning Kamp  *	True always.  Makes its best shot and continues on regardless.
426ea92232aSPoul-Henning Kamp  */
427ea92232aSPoul-Henning Kamp int
428ef646f18SMark Murray f_delete(PLAN *plan __unused, FTSENT *entry)
429ea92232aSPoul-Henning Kamp {
430ea92232aSPoul-Henning Kamp 	/* ignore these from fts */
431ea92232aSPoul-Henning Kamp 	if (strcmp(entry->fts_accpath, ".") == 0 ||
432ea92232aSPoul-Henning Kamp 	    strcmp(entry->fts_accpath, "..") == 0)
433ea92232aSPoul-Henning Kamp 		return 1;
434ea92232aSPoul-Henning Kamp 
435ea92232aSPoul-Henning Kamp 	/* sanity check */
436ea92232aSPoul-Henning Kamp 	if (isdepth == 0 ||			/* depth off */
43705e605b7SAndriy Gapon 	    (ftsoptions & FTS_NOSTAT))		/* not stat()ing */
438ea92232aSPoul-Henning Kamp 		errx(1, "-delete: insecure options got turned on");
439ea92232aSPoul-Henning Kamp 
44005e605b7SAndriy Gapon 	if (!(ftsoptions & FTS_PHYSICAL) ||	/* physical off */
44105e605b7SAndriy Gapon 	    (ftsoptions & FTS_LOGICAL))		/* or finally, logical on */
44205e605b7SAndriy Gapon 		errx(1, "-delete: forbidden when symlinks are followed");
44305e605b7SAndriy Gapon 
444ea92232aSPoul-Henning Kamp 	/* Potentially unsafe - do not accept relative paths whatsoever */
4459d6d5a71SJilles Tjoelker 	if (entry->fts_level > FTS_ROOTLEVEL &&
4469d6d5a71SJilles Tjoelker 	    strchr(entry->fts_accpath, '/') != NULL)
447ea92232aSPoul-Henning Kamp 		errx(1, "-delete: %s: relative path potentially not safe",
448ea92232aSPoul-Henning Kamp 			entry->fts_accpath);
449ea92232aSPoul-Henning Kamp 
450ea92232aSPoul-Henning Kamp 	/* Turn off user immutable bits if running as root */
451ea92232aSPoul-Henning Kamp 	if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
452ea92232aSPoul-Henning Kamp 	    !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
453ea92232aSPoul-Henning Kamp 	    geteuid() == 0)
4546911f596SJilles Tjoelker 		lchflags(entry->fts_accpath,
455ea92232aSPoul-Henning Kamp 		       entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
456ea92232aSPoul-Henning Kamp 
457ea92232aSPoul-Henning Kamp 	/* rmdir directories, unlink everything else */
458ea92232aSPoul-Henning Kamp 	if (S_ISDIR(entry->fts_statp->st_mode)) {
459ea92232aSPoul-Henning Kamp 		if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
460ea92232aSPoul-Henning Kamp 			warn("-delete: rmdir(%s)", entry->fts_path);
461ea92232aSPoul-Henning Kamp 	} else {
462ea92232aSPoul-Henning Kamp 		if (unlink(entry->fts_accpath) < 0)
463ea92232aSPoul-Henning Kamp 			warn("-delete: unlink(%s)", entry->fts_path);
464ea92232aSPoul-Henning Kamp 	}
465ea92232aSPoul-Henning Kamp 
466ea92232aSPoul-Henning Kamp 	/* "succeed" */
467ea92232aSPoul-Henning Kamp 	return 1;
468ea92232aSPoul-Henning Kamp }
469ea92232aSPoul-Henning Kamp 
470ea92232aSPoul-Henning Kamp PLAN *
471ef646f18SMark Murray c_delete(OPTION *option, char ***argvp __unused)
472ea92232aSPoul-Henning Kamp {
473ea92232aSPoul-Henning Kamp 
474ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;	/* no optimise */
475ea92232aSPoul-Henning Kamp 	isoutput = 1;			/* possible output */
476ea92232aSPoul-Henning Kamp 	isdepth = 1;			/* -depth implied */
477ea92232aSPoul-Henning Kamp 
47817ef6d3aSJilles Tjoelker 	/*
47917ef6d3aSJilles Tjoelker 	 * Try to avoid the confusing error message about relative paths
48017ef6d3aSJilles Tjoelker 	 * being potentially not safe.
48117ef6d3aSJilles Tjoelker 	 */
48217ef6d3aSJilles Tjoelker 	if (ftsoptions & FTS_NOCHDIR)
48317ef6d3aSJilles Tjoelker 		errx(1, "%s: forbidden when the current directory cannot be opened",
48417ef6d3aSJilles Tjoelker 		    "-delete");
48517ef6d3aSJilles Tjoelker 
486ea92232aSPoul-Henning Kamp 	return palloc(option);
4879b50d902SRodney W. Grimes }
4889b50d902SRodney W. Grimes 
4893f5223f8SWolfram Schneider 
4909b50d902SRodney W. Grimes /*
4911c832963SOliver Eikemeier  * always_true --
4929b50d902SRodney W. Grimes  *
49346b993ffSWarner Losh  *	Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
4949b50d902SRodney W. Grimes  */
4959b50d902SRodney W. Grimes int
496ef646f18SMark Murray f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
4979b50d902SRodney W. Grimes {
498ea92232aSPoul-Henning Kamp 	return 1;
4999b50d902SRodney W. Grimes }
5009b50d902SRodney W. Grimes 
5011c832963SOliver Eikemeier /*
5021c832963SOliver Eikemeier  * -depth functions --
5031c832963SOliver Eikemeier  *
5041c832963SOliver Eikemeier  *	With argument: True if the file is at level n.
5051c832963SOliver Eikemeier  *	Without argument: Always true, causes descent of the directory hierarchy
5061c832963SOliver Eikemeier  *	to be done so that all entries in a directory are acted on before the
5071c832963SOliver Eikemeier  *	directory itself.
5081c832963SOliver Eikemeier  */
5091c832963SOliver Eikemeier int
5101c832963SOliver Eikemeier f_depth(PLAN *plan, FTSENT *entry)
5119b50d902SRodney W. Grimes {
5121c832963SOliver Eikemeier 	if (plan->flags & F_DEPTH)
5131c832963SOliver Eikemeier 		COMPARE(entry->fts_level, plan->d_data);
5141c832963SOliver Eikemeier 	else
5151c832963SOliver Eikemeier 		return 1;
5161c832963SOliver Eikemeier }
5179b50d902SRodney W. Grimes 
5181c832963SOliver Eikemeier PLAN *
5191c832963SOliver Eikemeier c_depth(OPTION *option, char ***argvp)
5201c832963SOliver Eikemeier {
5211c832963SOliver Eikemeier 	PLAN *new;
5221c832963SOliver Eikemeier 	char *str;
5231c832963SOliver Eikemeier 
5241c832963SOliver Eikemeier 	new = palloc(option);
5251c832963SOliver Eikemeier 
5261c832963SOliver Eikemeier 	str = **argvp;
5271c832963SOliver Eikemeier 	if (str && !(new->flags & F_DEPTH)) {
5281c832963SOliver Eikemeier 		/* skip leading + or - */
5291c832963SOliver Eikemeier 		if (*str == '+' || *str == '-')
5301c832963SOliver Eikemeier 			str++;
5311c832963SOliver Eikemeier 		/* skip sign */
5321c832963SOliver Eikemeier 		if (*str == '+' || *str == '-')
5331c832963SOliver Eikemeier 			str++;
5341c832963SOliver Eikemeier 		if (isdigit(*str))
5351c832963SOliver Eikemeier 			new->flags |= F_DEPTH;
5361c832963SOliver Eikemeier 	}
5371c832963SOliver Eikemeier 
5381c832963SOliver Eikemeier 	if (new->flags & F_DEPTH) {	/* -depth n */
5391c832963SOliver Eikemeier 		char *ndepth;
5401c832963SOliver Eikemeier 
5411c832963SOliver Eikemeier 		ndepth = nextarg(option, argvp);
5421c832963SOliver Eikemeier 		new->d_data = find_parsenum(new, option->name, ndepth, NULL);
5431c832963SOliver Eikemeier 	} else {			/* -d */
5441c832963SOliver Eikemeier 		isdepth = 1;
5451c832963SOliver Eikemeier 	}
5461c832963SOliver Eikemeier 
5471c832963SOliver Eikemeier 	return new;
5489b50d902SRodney W. Grimes }
549127d7563SWarner Losh 
550127d7563SWarner Losh /*
551ed1a4621SPeter Wemm  * -empty functions --
552ed1a4621SPeter Wemm  *
553ed1a4621SPeter Wemm  *	True if the file or directory is empty
554ed1a4621SPeter Wemm  */
555ed1a4621SPeter Wemm int
556ef646f18SMark Murray f_empty(PLAN *plan __unused, FTSENT *entry)
557ed1a4621SPeter Wemm {
558ea92232aSPoul-Henning Kamp 	if (S_ISREG(entry->fts_statp->st_mode) &&
559ea92232aSPoul-Henning Kamp 	    entry->fts_statp->st_size == 0)
560ea92232aSPoul-Henning Kamp 		return 1;
561ed1a4621SPeter Wemm 	if (S_ISDIR(entry->fts_statp->st_mode)) {
562ed1a4621SPeter Wemm 		struct dirent *dp;
563ed1a4621SPeter Wemm 		int empty;
564ed1a4621SPeter Wemm 		DIR *dir;
565ed1a4621SPeter Wemm 
566ed1a4621SPeter Wemm 		empty = 1;
567ed1a4621SPeter Wemm 		dir = opendir(entry->fts_accpath);
568ed1a4621SPeter Wemm 		if (dir == NULL)
569e66a677bSKevin Lo 			return 0;
570ed1a4621SPeter Wemm 		for (dp = readdir(dir); dp; dp = readdir(dir))
571ed1a4621SPeter Wemm 			if (dp->d_name[0] != '.' ||
572ed1a4621SPeter Wemm 			    (dp->d_name[1] != '\0' &&
573ed1a4621SPeter Wemm 			     (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
574ed1a4621SPeter Wemm 				empty = 0;
575ed1a4621SPeter Wemm 				break;
576ed1a4621SPeter Wemm 			}
577ed1a4621SPeter Wemm 		closedir(dir);
578ea92232aSPoul-Henning Kamp 		return empty;
579ed1a4621SPeter Wemm 	}
580ea92232aSPoul-Henning Kamp 	return 0;
581ed1a4621SPeter Wemm }
582ed1a4621SPeter Wemm 
583ed1a4621SPeter Wemm PLAN *
584ef646f18SMark Murray c_empty(OPTION *option, char ***argvp __unused)
585ed1a4621SPeter Wemm {
586ed1a4621SPeter Wemm 	ftsoptions &= ~FTS_NOSTAT;
587ed1a4621SPeter Wemm 
588ea92232aSPoul-Henning Kamp 	return palloc(option);
589ed1a4621SPeter Wemm }
590ed1a4621SPeter Wemm 
591ed1a4621SPeter Wemm /*
592ea92232aSPoul-Henning Kamp  * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
593127d7563SWarner Losh  *
594127d7563SWarner Losh  *	True if the executed utility returns a zero value as exit status.
595127d7563SWarner Losh  *	The end of the primary expression is delimited by a semicolon.  If
596ea92232aSPoul-Henning Kamp  *	"{}" occurs anywhere, it gets replaced by the current pathname,
597ea92232aSPoul-Henning Kamp  *	or, in the case of -execdir, the current basename (filename
598ea92232aSPoul-Henning Kamp  *	without leading directory prefix). For -exec and -ok,
599ea92232aSPoul-Henning Kamp  *	the current directory for the execution of utility is the same as
600ea92232aSPoul-Henning Kamp  *	the current directory when the find utility was started, whereas
601ea92232aSPoul-Henning Kamp  *	for -execdir, it is the directory the file resides in.
602ea92232aSPoul-Henning Kamp  *
603ea92232aSPoul-Henning Kamp  *	The primary -ok differs from -exec in that it requests affirmation
604ea92232aSPoul-Henning Kamp  *	of the user before executing the utility.
605127d7563SWarner Losh  */
606127d7563SWarner Losh int
607ef646f18SMark Murray f_exec(PLAN *plan, FTSENT *entry)
608127d7563SWarner Losh {
609e98080b1SDavid Malone 	int cnt;
610127d7563SWarner Losh 	pid_t pid;
611127d7563SWarner Losh 	int status;
612127d7563SWarner Losh 	char *file;
613127d7563SWarner Losh 
6145e25d888STim J. Robbins 	if (entry == NULL && plan->flags & F_EXECPLUS) {
6155e25d888STim J. Robbins 		if (plan->e_ppos == plan->e_pbnum)
6165e25d888STim J. Robbins 			return (1);
6175e25d888STim J. Robbins 		plan->e_argv[plan->e_ppos] = NULL;
6185e25d888STim J. Robbins 		goto doexec;
6195e25d888STim J. Robbins 	}
6205e25d888STim J. Robbins 
621127d7563SWarner Losh 	/* XXX - if file/dir ends in '/' this will not work -- can it? */
622ea92232aSPoul-Henning Kamp 	if ((plan->flags & F_EXECDIR) && \
623ea92232aSPoul-Henning Kamp 	    (file = strrchr(entry->fts_path, '/')))
624127d7563SWarner Losh 		file++;
625127d7563SWarner Losh 	else
626127d7563SWarner Losh 		file = entry->fts_path;
627127d7563SWarner Losh 
6285e25d888STim J. Robbins 	if (plan->flags & F_EXECPLUS) {
6295e25d888STim J. Robbins 		if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
6305e25d888STim J. Robbins 			err(1, NULL);
6315e25d888STim J. Robbins 		plan->e_len[plan->e_ppos] = strlen(file);
6325e25d888STim J. Robbins 		plan->e_psize += plan->e_len[plan->e_ppos];
6335e25d888STim J. Robbins 		if (++plan->e_ppos < plan->e_pnummax &&
6345e25d888STim J. Robbins 		    plan->e_psize < plan->e_psizemax)
6355e25d888STim J. Robbins 			return (1);
6365e25d888STim J. Robbins 		plan->e_argv[plan->e_ppos] = NULL;
6375e25d888STim J. Robbins 	} else {
638127d7563SWarner Losh 		for (cnt = 0; plan->e_argv[cnt]; ++cnt)
639127d7563SWarner Losh 			if (plan->e_len[cnt])
6405e25d888STim J. Robbins 				brace_subst(plan->e_orig[cnt],
6415e25d888STim J. Robbins 				    &plan->e_argv[cnt], file,
6425e25d888STim J. Robbins 				    plan->e_len[cnt]);
6435e25d888STim J. Robbins 	}
644127d7563SWarner Losh 
6455e25d888STim J. Robbins doexec:	if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
646ea92232aSPoul-Henning Kamp 		return 0;
647ea92232aSPoul-Henning Kamp 
648ea92232aSPoul-Henning Kamp 	/* make sure find output is interspersed correctly with subprocesses */
649127d7563SWarner Losh 	fflush(stdout);
650127d7563SWarner Losh 	fflush(stderr);
651127d7563SWarner Losh 
6521fd98d7dSDag-Erling Smørgrav 	switch (pid = fork()) {
653127d7563SWarner Losh 	case -1:
654127d7563SWarner Losh 		err(1, "fork");
655127d7563SWarner Losh 		/* NOTREACHED */
656127d7563SWarner Losh 	case 0:
657ea92232aSPoul-Henning Kamp 		/* change dir back from where we started */
65817ef6d3aSJilles Tjoelker 		if (!(plan->flags & F_EXECDIR) &&
65917ef6d3aSJilles Tjoelker 		    !(ftsoptions & FTS_NOCHDIR) && fchdir(dotfd)) {
660ea92232aSPoul-Henning Kamp 			warn("chdir");
661ea92232aSPoul-Henning Kamp 			_exit(1);
662ea92232aSPoul-Henning Kamp 		}
663127d7563SWarner Losh 		execvp(plan->e_argv[0], plan->e_argv);
664127d7563SWarner Losh 		warn("%s", plan->e_argv[0]);
665127d7563SWarner Losh 		_exit(1);
666127d7563SWarner Losh 	}
6675e25d888STim J. Robbins 	if (plan->flags & F_EXECPLUS) {
6685e25d888STim J. Robbins 		while (--plan->e_ppos >= plan->e_pbnum)
6695e25d888STim J. Robbins 			free(plan->e_argv[plan->e_ppos]);
6705e25d888STim J. Robbins 		plan->e_ppos = plan->e_pbnum;
6715e25d888STim J. Robbins 		plan->e_psize = plan->e_pbsize;
6725e25d888STim J. Robbins 	}
673127d7563SWarner Losh 	pid = waitpid(pid, &status, 0);
6747a79617cSJilles Tjoelker 	if (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status))
6757a79617cSJilles Tjoelker 		return (1);
6767a79617cSJilles Tjoelker 	if (plan->flags & F_EXECPLUS) {
6777a79617cSJilles Tjoelker 		exitstatus = 1;
6787a79617cSJilles Tjoelker 		return (1);
6797a79617cSJilles Tjoelker 	}
6807a79617cSJilles Tjoelker 	return (0);
681127d7563SWarner Losh }
682127d7563SWarner Losh 
683127d7563SWarner Losh /*
684ea92232aSPoul-Henning Kamp  * c_exec, c_execdir, c_ok --
685127d7563SWarner Losh  *	build three parallel arrays, one with pointers to the strings passed
686127d7563SWarner Losh  *	on the command line, one with (possibly duplicated) pointers to the
687127d7563SWarner Losh  *	argv array, and one with integer values that are lengths of the
688127d7563SWarner Losh  *	strings, but also flags meaning that the string has to be massaged.
689127d7563SWarner Losh  */
690127d7563SWarner Losh PLAN *
691ef646f18SMark Murray c_exec(OPTION *option, char ***argvp)
692127d7563SWarner Losh {
693127d7563SWarner Losh 	PLAN *new;			/* node returned */
6945e25d888STim J. Robbins 	long argmax;
6955e25d888STim J. Robbins 	int cnt, i;
696a07af811STim J. Robbins 	char **argv, **ap, **ep, *p;
697127d7563SWarner Losh 
69817ef6d3aSJilles Tjoelker 	/* This would defeat -execdir's intended security. */
69917ef6d3aSJilles Tjoelker 	if (option->flags & F_EXECDIR && ftsoptions & FTS_NOCHDIR)
70017ef6d3aSJilles Tjoelker 		errx(1, "%s: forbidden when the current directory cannot be opened",
70117ef6d3aSJilles Tjoelker 		    "-execdir");
70217ef6d3aSJilles Tjoelker 
703ea92232aSPoul-Henning Kamp 	/* XXX - was in c_execdir, but seems unnecessary!?
704127d7563SWarner Losh 	ftsoptions &= ~FTS_NOSTAT;
705ea92232aSPoul-Henning Kamp 	*/
706127d7563SWarner Losh 	isoutput = 1;
707127d7563SWarner Losh 
708ea92232aSPoul-Henning Kamp 	/* XXX - this is a change from the previous coding */
709ea92232aSPoul-Henning Kamp 	new = palloc(option);
710127d7563SWarner Losh 
711127d7563SWarner Losh 	for (ap = argv = *argvp;; ++ap) {
712127d7563SWarner Losh 		if (!*ap)
713127d7563SWarner Losh 			errx(1,
714e22bb9dbSTim J. Robbins 			    "%s: no terminating \";\" or \"+\"", option->name);
715127d7563SWarner Losh 		if (**ap == ';')
716127d7563SWarner Losh 			break;
7175e25d888STim J. Robbins 		if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
7185e25d888STim J. Robbins 			new->flags |= F_EXECPLUS;
7195e25d888STim J. Robbins 			break;
7205e25d888STim J. Robbins 		}
721127d7563SWarner Losh 	}
722127d7563SWarner Losh 
72351b0534fSJuli Mallett 	if (ap == argv)
72451b0534fSJuli Mallett 		errx(1, "%s: no command specified", option->name);
72551b0534fSJuli Mallett 
726127d7563SWarner Losh 	cnt = ap - *argvp + 1;
7275e25d888STim J. Robbins 	if (new->flags & F_EXECPLUS) {
7285e25d888STim J. Robbins 		new->e_ppos = new->e_pbnum = cnt - 2;
7295e25d888STim J. Robbins 		if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
7305e25d888STim J. Robbins 			warn("sysconf(_SC_ARG_MAX)");
7315e25d888STim J. Robbins 			argmax = _POSIX_ARG_MAX;
7325e25d888STim J. Robbins 		}
733a07af811STim J. Robbins 		argmax -= 1024;
734a07af811STim J. Robbins 		for (ep = environ; *ep != NULL; ep++)
735a07af811STim J. Robbins 			argmax -= strlen(*ep) + 1 + sizeof(*ep);
736a07af811STim J. Robbins 		argmax -= 1 + sizeof(*ep);
737bc626176SJilles Tjoelker 		/*
738bc626176SJilles Tjoelker 		 * Ensure that -execdir ... {} + does not mix files
739bc626176SJilles Tjoelker 		 * from different directories in one invocation.
740bc626176SJilles Tjoelker 		 * Files from the same directory should be handled
741bc626176SJilles Tjoelker 		 * in one invocation but there is no code for it.
742bc626176SJilles Tjoelker 		 */
743bc626176SJilles Tjoelker 		new->e_pnummax = new->flags & F_EXECDIR ? 1 : argmax / 16;
744a07af811STim J. Robbins 		argmax -= sizeof(char *) * new->e_pnummax;
745a07af811STim J. Robbins 		if (argmax <= 0)
746a07af811STim J. Robbins 			errx(1, "no space for arguments");
747a07af811STim J. Robbins 		new->e_psizemax = argmax;
7485e25d888STim J. Robbins 		new->e_pbsize = 0;
7495e25d888STim J. Robbins 		cnt += new->e_pnummax + 1;
75022170420SKirill Ponomarev 		new->e_next = lastexecplus;
75122170420SKirill Ponomarev 		lastexecplus = new;
7525e25d888STim J. Robbins 	}
75347bca8b0SJuli Mallett 	if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
75447bca8b0SJuli Mallett 		err(1, NULL);
75547bca8b0SJuli Mallett 	if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
75647bca8b0SJuli Mallett 		err(1, NULL);
75747bca8b0SJuli Mallett 	if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
75847bca8b0SJuli Mallett 		err(1, NULL);
759127d7563SWarner Losh 
760127d7563SWarner Losh 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
761127d7563SWarner Losh 		new->e_orig[cnt] = *argv;
7625e25d888STim J. Robbins 		if (new->flags & F_EXECPLUS)
7635e25d888STim J. Robbins 			new->e_pbsize += strlen(*argv) + 1;
764127d7563SWarner Losh 		for (p = *argv; *p; ++p)
7655e25d888STim J. Robbins 			if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
7665e25d888STim J. Robbins 			    p[1] == '}') {
767f0cb9537SDavid E. O'Brien 				if ((new->e_argv[cnt] =
76847bca8b0SJuli Mallett 				    malloc(MAXPATHLEN)) == NULL)
76947bca8b0SJuli Mallett 					err(1, NULL);
770127d7563SWarner Losh 				new->e_len[cnt] = MAXPATHLEN;
771127d7563SWarner Losh 				break;
772127d7563SWarner Losh 			}
773127d7563SWarner Losh 		if (!*p) {
774127d7563SWarner Losh 			new->e_argv[cnt] = *argv;
775127d7563SWarner Losh 			new->e_len[cnt] = 0;
776127d7563SWarner Losh 		}
777127d7563SWarner Losh 	}
7785e25d888STim J. Robbins 	if (new->flags & F_EXECPLUS) {
7795e25d888STim J. Robbins 		new->e_psize = new->e_pbsize;
7805e25d888STim J. Robbins 		cnt--;
7815e25d888STim J. Robbins 		for (i = 0; i < new->e_pnummax; i++) {
7825e25d888STim J. Robbins 			new->e_argv[cnt] = NULL;
7835e25d888STim J. Robbins 			new->e_len[cnt] = 0;
7845e25d888STim J. Robbins 			cnt++;
7855e25d888STim J. Robbins 		}
7865e25d888STim J. Robbins 		argv = ap;
7875e25d888STim J. Robbins 		goto done;
7885e25d888STim J. Robbins 	}
789127d7563SWarner Losh 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
790127d7563SWarner Losh 
7915e25d888STim J. Robbins done:	*argvp = argv + 1;
792ea92232aSPoul-Henning Kamp 	return new;
793ea92232aSPoul-Henning Kamp }
794ea92232aSPoul-Henning Kamp 
79522170420SKirill Ponomarev /* Finish any pending -exec ... {} + functions. */
79622170420SKirill Ponomarev void
79710bc3a7fSEd Schouten finish_execplus(void)
79822170420SKirill Ponomarev {
79922170420SKirill Ponomarev 	PLAN *p;
80022170420SKirill Ponomarev 
80122170420SKirill Ponomarev 	p = lastexecplus;
80222170420SKirill Ponomarev 	while (p != NULL) {
80322170420SKirill Ponomarev 		(p->execute)(p, NULL);
80422170420SKirill Ponomarev 		p = p->e_next;
80522170420SKirill Ponomarev 	}
80622170420SKirill Ponomarev }
80722170420SKirill Ponomarev 
808ea92232aSPoul-Henning Kamp int
809ef646f18SMark Murray f_flags(PLAN *plan, FTSENT *entry)
810ea92232aSPoul-Henning Kamp {
811ea92232aSPoul-Henning Kamp 	u_long flags;
812ea92232aSPoul-Henning Kamp 
8137fd5ee41SRuslan Ermilov 	flags = entry->fts_statp->st_flags;
814ea92232aSPoul-Henning Kamp 	if (plan->flags & F_ATLEAST)
8157fd5ee41SRuslan Ermilov 		return (flags | plan->fl_flags) == flags &&
8167fd5ee41SRuslan Ermilov 		    !(flags & plan->fl_notflags);
8177fd5ee41SRuslan Ermilov 	else if (plan->flags & F_ANY)
8187fd5ee41SRuslan Ermilov 		return (flags & plan->fl_flags) ||
8197fd5ee41SRuslan Ermilov 		    (flags | plan->fl_notflags) != flags;
820ea92232aSPoul-Henning Kamp 	else
8217fd5ee41SRuslan Ermilov 		return flags == plan->fl_flags &&
8227fd5ee41SRuslan Ermilov 		    !(plan->fl_flags & plan->fl_notflags);
823ea92232aSPoul-Henning Kamp }
824ea92232aSPoul-Henning Kamp 
825ea92232aSPoul-Henning Kamp PLAN *
826ef646f18SMark Murray c_flags(OPTION *option, char ***argvp)
827ea92232aSPoul-Henning Kamp {
828ea92232aSPoul-Henning Kamp 	char *flags_str;
829ea92232aSPoul-Henning Kamp 	PLAN *new;
830ea92232aSPoul-Henning Kamp 	u_long flags, notflags;
831ea92232aSPoul-Henning Kamp 
832ea92232aSPoul-Henning Kamp 	flags_str = nextarg(option, argvp);
833ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
834ea92232aSPoul-Henning Kamp 
835ea92232aSPoul-Henning Kamp 	new = palloc(option);
836ea92232aSPoul-Henning Kamp 
837ea92232aSPoul-Henning Kamp 	if (*flags_str == '-') {
838ea92232aSPoul-Henning Kamp 		new->flags |= F_ATLEAST;
839ea92232aSPoul-Henning Kamp 		flags_str++;
8407fd5ee41SRuslan Ermilov 	} else if (*flags_str == '+') {
8417fd5ee41SRuslan Ermilov 		new->flags |= F_ANY;
8427fd5ee41SRuslan Ermilov 		flags_str++;
843ea92232aSPoul-Henning Kamp 	}
844ea92232aSPoul-Henning Kamp 	if (strtofflags(&flags_str, &flags, &notflags) == 1)
845ea92232aSPoul-Henning Kamp 		errx(1, "%s: %s: illegal flags string", option->name, flags_str);
846ea92232aSPoul-Henning Kamp 
847ea92232aSPoul-Henning Kamp 	new->fl_flags = flags;
8487fd5ee41SRuslan Ermilov 	new->fl_notflags = notflags;
849ea92232aSPoul-Henning Kamp 	return new;
850127d7563SWarner Losh }
8519b50d902SRodney W. Grimes 
8529b50d902SRodney W. Grimes /*
8539b50d902SRodney W. Grimes  * -follow functions --
8549b50d902SRodney W. Grimes  *
8559b50d902SRodney W. Grimes  *	Always true, causes symbolic links to be followed on a global
8569b50d902SRodney W. Grimes  *	basis.
8579b50d902SRodney W. Grimes  */
8589b50d902SRodney W. Grimes PLAN *
859ef646f18SMark Murray c_follow(OPTION *option, char ***argvp __unused)
8609b50d902SRodney W. Grimes {
8619b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_PHYSICAL;
8629b50d902SRodney W. Grimes 	ftsoptions |= FTS_LOGICAL;
8639b50d902SRodney W. Grimes 
864ea92232aSPoul-Henning Kamp 	return palloc(option);
8659b50d902SRodney W. Grimes }
8669b50d902SRodney W. Grimes 
8679b50d902SRodney W. Grimes /*
8689b50d902SRodney W. Grimes  * -fstype functions --
8699b50d902SRodney W. Grimes  *
8709b50d902SRodney W. Grimes  *	True if the file is of a certain type.
8719b50d902SRodney W. Grimes  */
8729b50d902SRodney W. Grimes int
873ef646f18SMark Murray f_fstype(PLAN *plan, FTSENT *entry)
8749b50d902SRodney W. Grimes {
8759b50d902SRodney W. Grimes 	static dev_t curdev;	/* need a guaranteed illegal dev value */
8769b50d902SRodney W. Grimes 	static int first = 1;
8779b50d902SRodney W. Grimes 	struct statfs sb;
8786ab780e5STai-hwa Liang 	static int val_flags;
8796ab780e5STai-hwa Liang 	static char fstype[sizeof(sb.f_fstypename)];
8808a0a76b8SOllivier Robert 	char *p, save[2] = {0,0};
8819b50d902SRodney W. Grimes 
882ea92232aSPoul-Henning Kamp 	if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
883ea92232aSPoul-Henning Kamp 		return 0;
884ea92232aSPoul-Henning Kamp 
8859b50d902SRodney W. Grimes 	/* Only check when we cross mount point. */
8869b50d902SRodney W. Grimes 	if (first || curdev != entry->fts_statp->st_dev) {
8879b50d902SRodney W. Grimes 		curdev = entry->fts_statp->st_dev;
8889b50d902SRodney W. Grimes 
8899b50d902SRodney W. Grimes 		/*
8909b50d902SRodney W. Grimes 		 * Statfs follows symlinks; find wants the link's filesystem,
8919b50d902SRodney W. Grimes 		 * not where it points.
8929b50d902SRodney W. Grimes 		 */
8939b50d902SRodney W. Grimes 		if (entry->fts_info == FTS_SL ||
8949b50d902SRodney W. Grimes 		    entry->fts_info == FTS_SLNONE) {
8959b50d902SRodney W. Grimes 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
8969b50d902SRodney W. Grimes 				++p;
8979b50d902SRodney W. Grimes 			else
8989b50d902SRodney W. Grimes 				p = entry->fts_accpath;
8999b50d902SRodney W. Grimes 			save[0] = p[0];
9009b50d902SRodney W. Grimes 			p[0] = '.';
9019b50d902SRodney W. Grimes 			save[1] = p[1];
9029b50d902SRodney W. Grimes 			p[1] = '\0';
9039b50d902SRodney W. Grimes 		} else
9049b50d902SRodney W. Grimes 			p = NULL;
9059b50d902SRodney W. Grimes 
9069b50d902SRodney W. Grimes 		if (statfs(entry->fts_accpath, &sb))
9079b50d902SRodney W. Grimes 			err(1, "%s", entry->fts_accpath);
9089b50d902SRodney W. Grimes 
9099b50d902SRodney W. Grimes 		if (p) {
9109b50d902SRodney W. Grimes 			p[0] = save[0];
9119b50d902SRodney W. Grimes 			p[1] = save[1];
9129b50d902SRodney W. Grimes 		}
9139b50d902SRodney W. Grimes 
9149b50d902SRodney W. Grimes 		first = 0;
915841484cdSPeter Wemm 
916841484cdSPeter Wemm 		/*
917841484cdSPeter Wemm 		 * Further tests may need both of these values, so
918841484cdSPeter Wemm 		 * always copy both of them.
919841484cdSPeter Wemm 		 */
9209d08e419SPeter Wemm 		val_flags = sb.f_flags;
9216ab780e5STai-hwa Liang 		strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
9229b50d902SRodney W. Grimes 	}
923ea92232aSPoul-Henning Kamp 	switch (plan->flags & F_MTMASK) {
9249b50d902SRodney W. Grimes 	case F_MTFLAG:
925ea92232aSPoul-Henning Kamp 		return val_flags & plan->mt_data;
9269b50d902SRodney W. Grimes 	case F_MTTYPE:
9276ab780e5STai-hwa Liang 		return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
9289b50d902SRodney W. Grimes 	default:
9299b50d902SRodney W. Grimes 		abort();
9309b50d902SRodney W. Grimes 	}
9319b50d902SRodney W. Grimes }
9329b50d902SRodney W. Grimes 
9339b50d902SRodney W. Grimes PLAN *
934ef646f18SMark Murray c_fstype(OPTION *option, char ***argvp)
9359b50d902SRodney W. Grimes {
936ea92232aSPoul-Henning Kamp 	char *fsname;
937e98080b1SDavid Malone 	PLAN *new;
9389b50d902SRodney W. Grimes 
939ea92232aSPoul-Henning Kamp 	fsname = nextarg(option, argvp);
9409b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
9419b50d902SRodney W. Grimes 
942ea92232aSPoul-Henning Kamp 	new = palloc(option);
943ea92232aSPoul-Henning Kamp 	switch (*fsname) {
9449b50d902SRodney W. Grimes 	case 'l':
945ea92232aSPoul-Henning Kamp 		if (!strcmp(fsname, "local")) {
946ea92232aSPoul-Henning Kamp 			new->flags |= F_MTFLAG;
9479b50d902SRodney W. Grimes 			new->mt_data = MNT_LOCAL;
948ea92232aSPoul-Henning Kamp 			return new;
9499b50d902SRodney W. Grimes 		}
9509b50d902SRodney W. Grimes 		break;
9519b50d902SRodney W. Grimes 	case 'r':
952ea92232aSPoul-Henning Kamp 		if (!strcmp(fsname, "rdonly")) {
953ea92232aSPoul-Henning Kamp 			new->flags |= F_MTFLAG;
9549b50d902SRodney W. Grimes 			new->mt_data = MNT_RDONLY;
955ea92232aSPoul-Henning Kamp 			return new;
9569b50d902SRodney W. Grimes 		}
9579b50d902SRodney W. Grimes 		break;
9589b50d902SRodney W. Grimes 	}
959ea92232aSPoul-Henning Kamp 
9606ab780e5STai-hwa Liang 	new->flags |= F_MTTYPE;
9616ab780e5STai-hwa Liang 	new->c_data = fsname;
962ea92232aSPoul-Henning Kamp 	return new;
9639b50d902SRodney W. Grimes }
9649b50d902SRodney W. Grimes 
9659b50d902SRodney W. Grimes /*
9669b50d902SRodney W. Grimes  * -group gname functions --
9679b50d902SRodney W. Grimes  *
9689b50d902SRodney W. Grimes  *	True if the file belongs to the group gname.  If gname is numeric and
9699b50d902SRodney W. Grimes  *	an equivalent of the getgrnam() function does not return a valid group
9709b50d902SRodney W. Grimes  *	name, gname is taken as a group ID.
9719b50d902SRodney W. Grimes  */
9729b50d902SRodney W. Grimes int
973ef646f18SMark Murray f_group(PLAN *plan, FTSENT *entry)
9749b50d902SRodney W. Grimes {
9754ba3b38bSKirill Ponomarev 	COMPARE(entry->fts_statp->st_gid, plan->g_data);
9769b50d902SRodney W. Grimes }
9779b50d902SRodney W. Grimes 
9789b50d902SRodney W. Grimes PLAN *
979ef646f18SMark Murray c_group(OPTION *option, char ***argvp)
9809b50d902SRodney W. Grimes {
981ea92232aSPoul-Henning Kamp 	char *gname;
9829b50d902SRodney W. Grimes 	PLAN *new;
9839b50d902SRodney W. Grimes 	struct group *g;
9849b50d902SRodney W. Grimes 	gid_t gid;
9859b50d902SRodney W. Grimes 
986ea92232aSPoul-Henning Kamp 	gname = nextarg(option, argvp);
9879b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
9889b50d902SRodney W. Grimes 
9894ba3b38bSKirill Ponomarev 	new = palloc(option);
9909b50d902SRodney W. Grimes 	g = getgrnam(gname);
9919b50d902SRodney W. Grimes 	if (g == NULL) {
9924ba3b38bSKirill Ponomarev 		char* cp = gname;
9934ba3b38bSKirill Ponomarev 		if (gname[0] == '-' || gname[0] == '+')
9944ba3b38bSKirill Ponomarev 			gname++;
9959b50d902SRodney W. Grimes 		gid = atoi(gname);
9969b50d902SRodney W. Grimes 		if (gid == 0 && gname[0] != '0')
997ea92232aSPoul-Henning Kamp 			errx(1, "%s: %s: no such group", option->name, gname);
9984ba3b38bSKirill Ponomarev 		gid = find_parsenum(new, option->name, cp, NULL);
9999b50d902SRodney W. Grimes 	} else
10009b50d902SRodney W. Grimes 		gid = g->gr_gid;
10019b50d902SRodney W. Grimes 
10029b50d902SRodney W. Grimes 	new->g_data = gid;
1003ea92232aSPoul-Henning Kamp 	return new;
10049b50d902SRodney W. Grimes }
10059b50d902SRodney W. Grimes 
10069b50d902SRodney W. Grimes /*
100740072dc2SJilles Tjoelker  * -ignore_readdir_race functions --
100840072dc2SJilles Tjoelker  *
100940072dc2SJilles Tjoelker  *	Always true. Ignore errors which occur if a file or a directory
101040072dc2SJilles Tjoelker  *	in a starting point gets deleted between reading the name and calling
101140072dc2SJilles Tjoelker  *	stat on it while find is traversing the starting point.
101240072dc2SJilles Tjoelker  */
101340072dc2SJilles Tjoelker 
101440072dc2SJilles Tjoelker PLAN *
101540072dc2SJilles Tjoelker c_ignore_readdir_race(OPTION *option, char ***argvp __unused)
101640072dc2SJilles Tjoelker {
101740072dc2SJilles Tjoelker 	if (strcmp(option->name, "-ignore_readdir_race") == 0)
101840072dc2SJilles Tjoelker 		ignore_readdir_race = 1;
101940072dc2SJilles Tjoelker 	else
102040072dc2SJilles Tjoelker 		ignore_readdir_race = 0;
102140072dc2SJilles Tjoelker 
102240072dc2SJilles Tjoelker 	return palloc(option);
102340072dc2SJilles Tjoelker }
102440072dc2SJilles Tjoelker 
102540072dc2SJilles Tjoelker /*
10269b50d902SRodney W. Grimes  * -inum n functions --
10279b50d902SRodney W. Grimes  *
10289b50d902SRodney W. Grimes  *	True if the file has inode # n.
10299b50d902SRodney W. Grimes  */
10309b50d902SRodney W. Grimes int
1031ef646f18SMark Murray f_inum(PLAN *plan, FTSENT *entry)
10329b50d902SRodney W. Grimes {
10339b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
10349b50d902SRodney W. Grimes }
10359b50d902SRodney W. Grimes 
10369b50d902SRodney W. Grimes PLAN *
1037ef646f18SMark Murray c_inum(OPTION *option, char ***argvp)
10389b50d902SRodney W. Grimes {
1039ea92232aSPoul-Henning Kamp 	char *inum_str;
10409b50d902SRodney W. Grimes 	PLAN *new;
10419b50d902SRodney W. Grimes 
1042ea92232aSPoul-Henning Kamp 	inum_str = nextarg(option, argvp);
10439b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
10449b50d902SRodney W. Grimes 
1045ea92232aSPoul-Henning Kamp 	new = palloc(option);
1046ea92232aSPoul-Henning Kamp 	new->i_data = find_parsenum(new, option->name, inum_str, NULL);
1047ea92232aSPoul-Henning Kamp 	return new;
10489b50d902SRodney W. Grimes }
10499b50d902SRodney W. Grimes 
10509b50d902SRodney W. Grimes /*
105146b993ffSWarner Losh  * -samefile FN
105246b993ffSWarner Losh  *
105346b993ffSWarner Losh  *	True if the file has the same inode (eg hard link) FN
105446b993ffSWarner Losh  */
105546b993ffSWarner Losh 
105646b993ffSWarner Losh /* f_samefile is just f_inum */
105746b993ffSWarner Losh PLAN *
105846b993ffSWarner Losh c_samefile(OPTION *option, char ***argvp)
105946b993ffSWarner Losh {
106046b993ffSWarner Losh 	char *fn;
106146b993ffSWarner Losh 	PLAN *new;
106246b993ffSWarner Losh 	struct stat sb;
106346b993ffSWarner Losh 
106446b993ffSWarner Losh 	fn = nextarg(option, argvp);
106546b993ffSWarner Losh 	ftsoptions &= ~FTS_NOSTAT;
106646b993ffSWarner Losh 
106746b993ffSWarner Losh 	new = palloc(option);
106846b993ffSWarner Losh 	if (stat(fn, &sb))
106946b993ffSWarner Losh 		err(1, "%s", fn);
107046b993ffSWarner Losh 	new->i_data = sb.st_ino;
107146b993ffSWarner Losh 	return new;
107246b993ffSWarner Losh }
107346b993ffSWarner Losh 
107446b993ffSWarner Losh /*
10759b50d902SRodney W. Grimes  * -links n functions --
10769b50d902SRodney W. Grimes  *
10779b50d902SRodney W. Grimes  *	True if the file has n links.
10789b50d902SRodney W. Grimes  */
10799b50d902SRodney W. Grimes int
1080ef646f18SMark Murray f_links(PLAN *plan, FTSENT *entry)
10819b50d902SRodney W. Grimes {
10829b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
10839b50d902SRodney W. Grimes }
10849b50d902SRodney W. Grimes 
10859b50d902SRodney W. Grimes PLAN *
1086ef646f18SMark Murray c_links(OPTION *option, char ***argvp)
10879b50d902SRodney W. Grimes {
1088ea92232aSPoul-Henning Kamp 	char *nlinks;
10899b50d902SRodney W. Grimes 	PLAN *new;
10909b50d902SRodney W. Grimes 
1091ea92232aSPoul-Henning Kamp 	nlinks = nextarg(option, argvp);
10929b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
10939b50d902SRodney W. Grimes 
1094ea92232aSPoul-Henning Kamp 	new = palloc(option);
1095ea92232aSPoul-Henning Kamp 	new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1096ea92232aSPoul-Henning Kamp 	return new;
10979b50d902SRodney W. Grimes }
10989b50d902SRodney W. Grimes 
10999b50d902SRodney W. Grimes /*
11009b50d902SRodney W. Grimes  * -ls functions --
11019b50d902SRodney W. Grimes  *
11029b50d902SRodney W. Grimes  *	Always true - prints the current entry to stdout in "ls" format.
11039b50d902SRodney W. Grimes  */
11049b50d902SRodney W. Grimes int
1105ef646f18SMark Murray f_ls(PLAN *plan __unused, FTSENT *entry)
11069b50d902SRodney W. Grimes {
11079b50d902SRodney W. Grimes 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1108ea92232aSPoul-Henning Kamp 	return 1;
11099b50d902SRodney W. Grimes }
11109b50d902SRodney W. Grimes 
11119b50d902SRodney W. Grimes PLAN *
1112ef646f18SMark Murray c_ls(OPTION *option, char ***argvp __unused)
11139b50d902SRodney W. Grimes {
11149b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
11159b50d902SRodney W. Grimes 	isoutput = 1;
11169b50d902SRodney W. Grimes 
1117ea92232aSPoul-Henning Kamp 	return palloc(option);
11189b50d902SRodney W. Grimes }
11199b50d902SRodney W. Grimes 
11209b50d902SRodney W. Grimes /*
11219b50d902SRodney W. Grimes  * -name functions --
11229b50d902SRodney W. Grimes  *
11239b50d902SRodney W. Grimes  *	True if the basename of the filename being examined
11249b50d902SRodney W. Grimes  *	matches pattern using Pattern Matching Notation S3.14
11259b50d902SRodney W. Grimes  */
11269b50d902SRodney W. Grimes int
1127ef646f18SMark Murray f_name(PLAN *plan, FTSENT *entry)
11289b50d902SRodney W. Grimes {
1129acebb585SWarner Losh 	char fn[PATH_MAX];
1130acebb585SWarner Losh 	const char *name;
11313e02329aSJilles Tjoelker 	ssize_t len;
1132acebb585SWarner Losh 
1133acebb585SWarner Losh 	if (plan->flags & F_LINK) {
1134e810bef7SJilles Tjoelker 		/*
1135e810bef7SJilles Tjoelker 		 * The below test both avoids obviously useless readlink()
1136e810bef7SJilles Tjoelker 		 * calls and ensures that symlinks with existent target do
1137e810bef7SJilles Tjoelker 		 * not match if symlinks are being followed.
1138e810bef7SJilles Tjoelker 		 * Assumption: fts will stat all symlinks that are to be
1139e810bef7SJilles Tjoelker 		 * followed and will return the stat information.
1140e810bef7SJilles Tjoelker 		 */
1141e810bef7SJilles Tjoelker 		if (entry->fts_info != FTS_NSOK && entry->fts_info != FTS_SL &&
1142e810bef7SJilles Tjoelker 		    entry->fts_info != FTS_SLNONE)
1143e810bef7SJilles Tjoelker 			return 0;
1144e810bef7SJilles Tjoelker 		len = readlink(entry->fts_accpath, fn, sizeof(fn) - 1);
11453e02329aSJilles Tjoelker 		if (len == -1)
114646b993ffSWarner Losh 			return 0;
11473e02329aSJilles Tjoelker 		fn[len] = '\0';
11483e02329aSJilles Tjoelker 		name = fn;
1149acebb585SWarner Losh 	} else
1150acebb585SWarner Losh 		name = entry->fts_name;
1151acebb585SWarner Losh 	return !fnmatch(plan->c_data, name,
1152ea92232aSPoul-Henning Kamp 	    plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
11539b50d902SRodney W. Grimes }
11549b50d902SRodney W. Grimes 
11559b50d902SRodney W. Grimes PLAN *
1156ef646f18SMark Murray c_name(OPTION *option, char ***argvp)
11579b50d902SRodney W. Grimes {
1158ea92232aSPoul-Henning Kamp 	char *pattern;
11599b50d902SRodney W. Grimes 	PLAN *new;
11609b50d902SRodney W. Grimes 
1161ea92232aSPoul-Henning Kamp 	pattern = nextarg(option, argvp);
1162ea92232aSPoul-Henning Kamp 	new = palloc(option);
11639b50d902SRodney W. Grimes 	new->c_data = pattern;
1164ea92232aSPoul-Henning Kamp 	return new;
11659b50d902SRodney W. Grimes }
11669b50d902SRodney W. Grimes 
11677c1d4b3aSAkinori MUSHA /*
1168ea92232aSPoul-Henning Kamp  * -newer file functions --
11697c1d4b3aSAkinori MUSHA  *
1170ea92232aSPoul-Henning Kamp  *	True if the current file has been modified more recently
1171ea92232aSPoul-Henning Kamp  *	then the modification time of the file named by the pathname
1172ea92232aSPoul-Henning Kamp  *	file.
11737c1d4b3aSAkinori MUSHA  */
11747c1d4b3aSAkinori MUSHA int
1175ef646f18SMark Murray f_newer(PLAN *plan, FTSENT *entry)
11767c1d4b3aSAkinori MUSHA {
1177ddd956b0SJilles Tjoelker 	struct timespec ft;
1178ddd956b0SJilles Tjoelker 
1179ea92232aSPoul-Henning Kamp 	if (plan->flags & F_TIME_C)
1180ddd956b0SJilles Tjoelker 		ft = entry->fts_statp->st_ctim;
1181ea92232aSPoul-Henning Kamp 	else if (plan->flags & F_TIME_A)
1182ddd956b0SJilles Tjoelker 		ft = entry->fts_statp->st_atim;
118331d53425SCeri Davies 	else if (plan->flags & F_TIME_B)
1184ddd956b0SJilles Tjoelker 		ft = entry->fts_statp->st_birthtim;
1185ea92232aSPoul-Henning Kamp 	else
1186ddd956b0SJilles Tjoelker 		ft = entry->fts_statp->st_mtim;
1187ddd956b0SJilles Tjoelker 	return (ft.tv_sec > plan->t_data.tv_sec ||
1188ddd956b0SJilles Tjoelker 	    (ft.tv_sec == plan->t_data.tv_sec &&
1189ddd956b0SJilles Tjoelker 	    ft.tv_nsec > plan->t_data.tv_nsec));
11907c1d4b3aSAkinori MUSHA }
11917c1d4b3aSAkinori MUSHA 
11927c1d4b3aSAkinori MUSHA PLAN *
1193ef646f18SMark Murray c_newer(OPTION *option, char ***argvp)
11947c1d4b3aSAkinori MUSHA {
1195ea92232aSPoul-Henning Kamp 	char *fn_or_tspec;
11967c1d4b3aSAkinori MUSHA 	PLAN *new;
1197ea92232aSPoul-Henning Kamp 	struct stat sb;
11987c1d4b3aSAkinori MUSHA 
1199ea92232aSPoul-Henning Kamp 	fn_or_tspec = nextarg(option, argvp);
1200ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
1201ea92232aSPoul-Henning Kamp 
1202ea92232aSPoul-Henning Kamp 	new = palloc(option);
1203ea92232aSPoul-Henning Kamp 	/* compare against what */
1204ea92232aSPoul-Henning Kamp 	if (option->flags & F_TIME2_T) {
1205ddd956b0SJilles Tjoelker 		new->t_data.tv_sec = get_date(fn_or_tspec);
1206ddd956b0SJilles Tjoelker 		if (new->t_data.tv_sec == (time_t) -1)
1207ea92232aSPoul-Henning Kamp 			errx(1, "Can't parse date/time: %s", fn_or_tspec);
1208ddd956b0SJilles Tjoelker 		/* Use the seconds only in the comparison. */
1209ddd956b0SJilles Tjoelker 		new->t_data.tv_nsec = 999999999;
1210ea92232aSPoul-Henning Kamp 	} else {
1211ea92232aSPoul-Henning Kamp 		if (stat(fn_or_tspec, &sb))
1212ea92232aSPoul-Henning Kamp 			err(1, "%s", fn_or_tspec);
1213ea92232aSPoul-Henning Kamp 		if (option->flags & F_TIME2_C)
1214ddd956b0SJilles Tjoelker 			new->t_data = sb.st_ctim;
1215ea92232aSPoul-Henning Kamp 		else if (option->flags & F_TIME2_A)
1216ddd956b0SJilles Tjoelker 			new->t_data = sb.st_atim;
12178310a1a2SGavin Atkinson 		else if (option->flags & F_TIME2_B)
1218ddd956b0SJilles Tjoelker 			new->t_data = sb.st_birthtim;
1219ea92232aSPoul-Henning Kamp 		else
1220ddd956b0SJilles Tjoelker 			new->t_data = sb.st_mtim;
1221ea92232aSPoul-Henning Kamp 	}
1222ea92232aSPoul-Henning Kamp 	return new;
12237c1d4b3aSAkinori MUSHA }
12247c1d4b3aSAkinori MUSHA 
1225ea92232aSPoul-Henning Kamp /*
1226ea92232aSPoul-Henning Kamp  * -nogroup functions --
1227ea92232aSPoul-Henning Kamp  *
1228ea92232aSPoul-Henning Kamp  *	True if file belongs to a user ID for which the equivalent
1229ea92232aSPoul-Henning Kamp  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1230ea92232aSPoul-Henning Kamp  */
1231ea92232aSPoul-Henning Kamp int
1232ef646f18SMark Murray f_nogroup(PLAN *plan __unused, FTSENT *entry)
1233ea92232aSPoul-Henning Kamp {
1234ea92232aSPoul-Henning Kamp 	return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1235ea92232aSPoul-Henning Kamp }
1236ea92232aSPoul-Henning Kamp 
1237ea92232aSPoul-Henning Kamp PLAN *
1238ef646f18SMark Murray c_nogroup(OPTION *option, char ***argvp __unused)
1239ea92232aSPoul-Henning Kamp {
1240ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
1241ea92232aSPoul-Henning Kamp 
1242ea92232aSPoul-Henning Kamp 	return palloc(option);
1243ea92232aSPoul-Henning Kamp }
1244ea92232aSPoul-Henning Kamp 
1245ea92232aSPoul-Henning Kamp /*
1246ea92232aSPoul-Henning Kamp  * -nouser functions --
1247ea92232aSPoul-Henning Kamp  *
1248ea92232aSPoul-Henning Kamp  *	True if file belongs to a user ID for which the equivalent
1249ea92232aSPoul-Henning Kamp  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1250ea92232aSPoul-Henning Kamp  */
1251ea92232aSPoul-Henning Kamp int
1252ef646f18SMark Murray f_nouser(PLAN *plan __unused, FTSENT *entry)
1253ea92232aSPoul-Henning Kamp {
1254ea92232aSPoul-Henning Kamp 	return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1255ea92232aSPoul-Henning Kamp }
1256ea92232aSPoul-Henning Kamp 
1257ea92232aSPoul-Henning Kamp PLAN *
1258ef646f18SMark Murray c_nouser(OPTION *option, char ***argvp __unused)
1259ea92232aSPoul-Henning Kamp {
1260ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
1261ea92232aSPoul-Henning Kamp 
1262ea92232aSPoul-Henning Kamp 	return palloc(option);
1263ea92232aSPoul-Henning Kamp }
1264ea92232aSPoul-Henning Kamp 
1265ea92232aSPoul-Henning Kamp /*
1266ea92232aSPoul-Henning Kamp  * -path functions --
1267ea92232aSPoul-Henning Kamp  *
1268ea92232aSPoul-Henning Kamp  *	True if the path of the filename being examined
1269ea92232aSPoul-Henning Kamp  *	matches pattern using Pattern Matching Notation S3.14
1270ea92232aSPoul-Henning Kamp  */
1271ea92232aSPoul-Henning Kamp int
1272ef646f18SMark Murray f_path(PLAN *plan, FTSENT *entry)
1273ea92232aSPoul-Henning Kamp {
1274ea92232aSPoul-Henning Kamp 	return !fnmatch(plan->c_data, entry->fts_path,
1275ea92232aSPoul-Henning Kamp 	    plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1276ea92232aSPoul-Henning Kamp }
1277ea92232aSPoul-Henning Kamp 
1278ea92232aSPoul-Henning Kamp /* c_path is the same as c_name */
1279ea92232aSPoul-Henning Kamp 
1280ea92232aSPoul-Henning Kamp /*
1281ea92232aSPoul-Henning Kamp  * -perm functions --
1282ea92232aSPoul-Henning Kamp  *
1283ea92232aSPoul-Henning Kamp  *	The mode argument is used to represent file mode bits.  If it starts
1284ea92232aSPoul-Henning Kamp  *	with a leading digit, it's treated as an octal mode, otherwise as a
1285ea92232aSPoul-Henning Kamp  *	symbolic mode.
1286ea92232aSPoul-Henning Kamp  */
1287ea92232aSPoul-Henning Kamp int
1288ef646f18SMark Murray f_perm(PLAN *plan, FTSENT *entry)
1289ea92232aSPoul-Henning Kamp {
1290ea92232aSPoul-Henning Kamp 	mode_t mode;
1291ea92232aSPoul-Henning Kamp 
1292ea92232aSPoul-Henning Kamp 	mode = entry->fts_statp->st_mode &
1293ea92232aSPoul-Henning Kamp 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1294ea92232aSPoul-Henning Kamp 	if (plan->flags & F_ATLEAST)
1295ea92232aSPoul-Henning Kamp 		return (plan->m_data | mode) == mode;
1296c0ff9709SRuslan Ermilov 	else if (plan->flags & F_ANY)
1297c0ff9709SRuslan Ermilov 		return (mode & plan->m_data);
1298ea92232aSPoul-Henning Kamp 	else
1299ea92232aSPoul-Henning Kamp 		return mode == plan->m_data;
1300ea92232aSPoul-Henning Kamp 	/* NOTREACHED */
1301ea92232aSPoul-Henning Kamp }
1302ea92232aSPoul-Henning Kamp 
1303ea92232aSPoul-Henning Kamp PLAN *
1304ef646f18SMark Murray c_perm(OPTION *option, char ***argvp)
1305ea92232aSPoul-Henning Kamp {
1306ea92232aSPoul-Henning Kamp 	char *perm;
1307ea92232aSPoul-Henning Kamp 	PLAN *new;
1308ea92232aSPoul-Henning Kamp 	mode_t *set;
1309ea92232aSPoul-Henning Kamp 
1310ea92232aSPoul-Henning Kamp 	perm = nextarg(option, argvp);
1311ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
1312ea92232aSPoul-Henning Kamp 
1313ea92232aSPoul-Henning Kamp 	new = palloc(option);
1314ea92232aSPoul-Henning Kamp 
1315ea92232aSPoul-Henning Kamp 	if (*perm == '-') {
1316ea92232aSPoul-Henning Kamp 		new->flags |= F_ATLEAST;
1317ea92232aSPoul-Henning Kamp 		++perm;
1318ea92232aSPoul-Henning Kamp 	} else if (*perm == '+') {
1319ea92232aSPoul-Henning Kamp 		new->flags |= F_ANY;
1320ea92232aSPoul-Henning Kamp 		++perm;
1321ea92232aSPoul-Henning Kamp 	}
1322ea92232aSPoul-Henning Kamp 
1323ea92232aSPoul-Henning Kamp 	if ((set = setmode(perm)) == NULL)
1324ea92232aSPoul-Henning Kamp 		errx(1, "%s: %s: illegal mode string", option->name, perm);
1325ea92232aSPoul-Henning Kamp 
1326ea92232aSPoul-Henning Kamp 	new->m_data = getmode(set, 0);
1327ea92232aSPoul-Henning Kamp 	free(set);
1328ea92232aSPoul-Henning Kamp 	return new;
1329ea92232aSPoul-Henning Kamp }
1330ea92232aSPoul-Henning Kamp 
1331ea92232aSPoul-Henning Kamp /*
1332ea92232aSPoul-Henning Kamp  * -print functions --
1333ea92232aSPoul-Henning Kamp  *
13349725a7b9SPhilippe Charnier  *	Always true, causes the current pathname to be written to
1335ea92232aSPoul-Henning Kamp  *	standard output.
1336ea92232aSPoul-Henning Kamp  */
1337ea92232aSPoul-Henning Kamp int
1338ef646f18SMark Murray f_print(PLAN *plan __unused, FTSENT *entry)
1339ea92232aSPoul-Henning Kamp {
1340ea92232aSPoul-Henning Kamp 	(void)puts(entry->fts_path);
1341ea92232aSPoul-Henning Kamp 	return 1;
1342ea92232aSPoul-Henning Kamp }
1343ea92232aSPoul-Henning Kamp 
1344ea92232aSPoul-Henning Kamp PLAN *
1345ef646f18SMark Murray c_print(OPTION *option, char ***argvp __unused)
1346ea92232aSPoul-Henning Kamp {
1347ea92232aSPoul-Henning Kamp 	isoutput = 1;
1348ea92232aSPoul-Henning Kamp 
1349ea92232aSPoul-Henning Kamp 	return palloc(option);
1350ea92232aSPoul-Henning Kamp }
1351ea92232aSPoul-Henning Kamp 
1352ea92232aSPoul-Henning Kamp /*
1353ea92232aSPoul-Henning Kamp  * -print0 functions --
1354ea92232aSPoul-Henning Kamp  *
13559725a7b9SPhilippe Charnier  *	Always true, causes the current pathname to be written to
1356ea92232aSPoul-Henning Kamp  *	standard output followed by a NUL character
1357ea92232aSPoul-Henning Kamp  */
1358ea92232aSPoul-Henning Kamp int
1359ef646f18SMark Murray f_print0(PLAN *plan __unused, FTSENT *entry)
1360ea92232aSPoul-Henning Kamp {
1361ea92232aSPoul-Henning Kamp 	fputs(entry->fts_path, stdout);
1362ea92232aSPoul-Henning Kamp 	fputc('\0', stdout);
1363ea92232aSPoul-Henning Kamp 	return 1;
1364ea92232aSPoul-Henning Kamp }
1365ea92232aSPoul-Henning Kamp 
1366ea92232aSPoul-Henning Kamp /* c_print0 is the same as c_print */
1367ea92232aSPoul-Henning Kamp 
1368ea92232aSPoul-Henning Kamp /*
1369ea92232aSPoul-Henning Kamp  * -prune functions --
1370ea92232aSPoul-Henning Kamp  *
1371ea92232aSPoul-Henning Kamp  *	Prune a portion of the hierarchy.
1372ea92232aSPoul-Henning Kamp  */
1373ea92232aSPoul-Henning Kamp int
1374ef646f18SMark Murray f_prune(PLAN *plan __unused, FTSENT *entry)
1375ea92232aSPoul-Henning Kamp {
1376ea92232aSPoul-Henning Kamp 	if (fts_set(tree, entry, FTS_SKIP))
1377ea92232aSPoul-Henning Kamp 		err(1, "%s", entry->fts_path);
1378ea92232aSPoul-Henning Kamp 	return 1;
1379ea92232aSPoul-Henning Kamp }
1380ea92232aSPoul-Henning Kamp 
1381ea92232aSPoul-Henning Kamp /* c_prune == c_simple */
13827c1d4b3aSAkinori MUSHA 
13837c1d4b3aSAkinori MUSHA /*
13847c1d4b3aSAkinori MUSHA  * -regex functions --
13857c1d4b3aSAkinori MUSHA  *
13867c1d4b3aSAkinori MUSHA  *	True if the whole path of the file matches pattern using
13877c1d4b3aSAkinori MUSHA  *	regular expression.
13887c1d4b3aSAkinori MUSHA  */
13897c1d4b3aSAkinori MUSHA int
1390ef646f18SMark Murray f_regex(PLAN *plan, FTSENT *entry)
13917c1d4b3aSAkinori MUSHA {
13927c1d4b3aSAkinori MUSHA 	char *str;
139347bca8b0SJuli Mallett 	int len;
13947c1d4b3aSAkinori MUSHA 	regex_t *pre;
13957c1d4b3aSAkinori MUSHA 	regmatch_t pmatch;
13967c1d4b3aSAkinori MUSHA 	int errcode;
13977c1d4b3aSAkinori MUSHA 	char errbuf[LINE_MAX];
13987c1d4b3aSAkinori MUSHA 	int matched;
13997c1d4b3aSAkinori MUSHA 
14007c1d4b3aSAkinori MUSHA 	pre = plan->re_data;
14017c1d4b3aSAkinori MUSHA 	str = entry->fts_path;
14027c1d4b3aSAkinori MUSHA 	len = strlen(str);
14037c1d4b3aSAkinori MUSHA 	matched = 0;
14047c1d4b3aSAkinori MUSHA 
14057c1d4b3aSAkinori MUSHA 	pmatch.rm_so = 0;
14067c1d4b3aSAkinori MUSHA 	pmatch.rm_eo = len;
14077c1d4b3aSAkinori MUSHA 
14087c1d4b3aSAkinori MUSHA 	errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
14097c1d4b3aSAkinori MUSHA 
14107c1d4b3aSAkinori MUSHA 	if (errcode != 0 && errcode != REG_NOMATCH) {
14117c1d4b3aSAkinori MUSHA 		regerror(errcode, pre, errbuf, sizeof errbuf);
14127c1d4b3aSAkinori MUSHA 		errx(1, "%s: %s",
1413ea92232aSPoul-Henning Kamp 		     plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
14147c1d4b3aSAkinori MUSHA 	}
14157c1d4b3aSAkinori MUSHA 
14167c1d4b3aSAkinori MUSHA 	if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
14177c1d4b3aSAkinori MUSHA 		matched = 1;
14187c1d4b3aSAkinori MUSHA 
1419ea92232aSPoul-Henning Kamp 	return matched;
14207c1d4b3aSAkinori MUSHA }
14217c1d4b3aSAkinori MUSHA 
14227c1d4b3aSAkinori MUSHA PLAN *
1423ef646f18SMark Murray c_regex(OPTION *option, char ***argvp)
14247c1d4b3aSAkinori MUSHA {
14257c1d4b3aSAkinori MUSHA 	PLAN *new;
1426ea92232aSPoul-Henning Kamp 	char *pattern;
14277c1d4b3aSAkinori MUSHA 	regex_t *pre;
14287c1d4b3aSAkinori MUSHA 	int errcode;
14297c1d4b3aSAkinori MUSHA 	char errbuf[LINE_MAX];
14307c1d4b3aSAkinori MUSHA 
14317c1d4b3aSAkinori MUSHA 	if ((pre = malloc(sizeof(regex_t))) == NULL)
14327c1d4b3aSAkinori MUSHA 		err(1, NULL);
14337c1d4b3aSAkinori MUSHA 
1434ea92232aSPoul-Henning Kamp 	pattern = nextarg(option, argvp);
1435ea92232aSPoul-Henning Kamp 
1436ea92232aSPoul-Henning Kamp 	if ((errcode = regcomp(pre, pattern,
1437ea92232aSPoul-Henning Kamp 	    regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
14387c1d4b3aSAkinori MUSHA 		regerror(errcode, pre, errbuf, sizeof errbuf);
14397c1d4b3aSAkinori MUSHA 		errx(1, "%s: %s: %s",
1440ea92232aSPoul-Henning Kamp 		     option->flags & F_IGNCASE ? "-iregex" : "-regex",
1441ea92232aSPoul-Henning Kamp 		     pattern, errbuf);
14427c1d4b3aSAkinori MUSHA 	}
14437c1d4b3aSAkinori MUSHA 
1444ea92232aSPoul-Henning Kamp 	new = palloc(option);
14457c1d4b3aSAkinori MUSHA 	new->re_data = pre;
14467c1d4b3aSAkinori MUSHA 
1447567664c4SOllivier Robert 	return new;
1448567664c4SOllivier Robert }
1449567664c4SOllivier Robert 
145046b993ffSWarner Losh /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
14519b50d902SRodney W. Grimes 
14529b50d902SRodney W. Grimes PLAN *
1453ef646f18SMark Murray c_simple(OPTION *option, char ***argvp __unused)
14549b50d902SRodney W. Grimes {
1455ea92232aSPoul-Henning Kamp 	return palloc(option);
14569b50d902SRodney W. Grimes }
14579b50d902SRodney W. Grimes 
14589b50d902SRodney W. Grimes /*
14599b50d902SRodney W. Grimes  * -size n[c] functions --
14609b50d902SRodney W. Grimes  *
14619b50d902SRodney W. Grimes  *	True if the file size in bytes, divided by an implementation defined
14629b50d902SRodney W. Grimes  *	value and rounded up to the next integer, is n.  If n is followed by
14635a890aacSKirill Ponomarev  *      one of c k M G T P, the size is in bytes, kilobytes,
14645a890aacSKirill Ponomarev  *      megabytes, gigabytes, terabytes or petabytes respectively.
14659b50d902SRodney W. Grimes  */
14669b50d902SRodney W. Grimes #define	FIND_SIZE	512
14679b50d902SRodney W. Grimes static int divsize = 1;
14689b50d902SRodney W. Grimes 
14699b50d902SRodney W. Grimes int
1470ef646f18SMark Murray f_size(PLAN *plan, FTSENT *entry)
14719b50d902SRodney W. Grimes {
14729b50d902SRodney W. Grimes 	off_t size;
14739b50d902SRodney W. Grimes 
14749b50d902SRodney W. Grimes 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
14759b50d902SRodney W. Grimes 	    FIND_SIZE : entry->fts_statp->st_size;
14769b50d902SRodney W. Grimes 	COMPARE(size, plan->o_data);
14779b50d902SRodney W. Grimes }
14789b50d902SRodney W. Grimes 
14799b50d902SRodney W. Grimes PLAN *
1480ef646f18SMark Murray c_size(OPTION *option, char ***argvp)
14819b50d902SRodney W. Grimes {
1482ea92232aSPoul-Henning Kamp 	char *size_str;
14839b50d902SRodney W. Grimes 	PLAN *new;
14849b50d902SRodney W. Grimes 	char endch;
14855a890aacSKirill Ponomarev 	off_t scale;
14869b50d902SRodney W. Grimes 
1487ea92232aSPoul-Henning Kamp 	size_str = nextarg(option, argvp);
14889b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
14899b50d902SRodney W. Grimes 
1490ea92232aSPoul-Henning Kamp 	new = palloc(option);
14919b50d902SRodney W. Grimes 	endch = 'c';
1492ea92232aSPoul-Henning Kamp 	new->o_data = find_parsenum(new, option->name, size_str, &endch);
14935a890aacSKirill Ponomarev 	if (endch != '\0') {
14949b50d902SRodney W. Grimes 		divsize = 0;
14955a890aacSKirill Ponomarev 
14965a890aacSKirill Ponomarev 		switch (endch) {
14975a890aacSKirill Ponomarev 		case 'c':                       /* characters */
14985a890aacSKirill Ponomarev 			scale = 0x1LL;
14995a890aacSKirill Ponomarev 			break;
15005a890aacSKirill Ponomarev 		case 'k':                       /* kilobytes 1<<10 */
15015a890aacSKirill Ponomarev 			scale = 0x400LL;
15025a890aacSKirill Ponomarev 			break;
15035a890aacSKirill Ponomarev 		case 'M':                       /* megabytes 1<<20 */
15045a890aacSKirill Ponomarev 			scale = 0x100000LL;
15055a890aacSKirill Ponomarev 			break;
15065a890aacSKirill Ponomarev 		case 'G':                       /* gigabytes 1<<30 */
15075a890aacSKirill Ponomarev 			scale = 0x40000000LL;
15085a890aacSKirill Ponomarev 			break;
15095a890aacSKirill Ponomarev 		case 'T':                       /* terabytes 1<<40 */
15105a890aacSKirill Ponomarev 			scale = 0x1000000000LL;
15115a890aacSKirill Ponomarev 			break;
15125a890aacSKirill Ponomarev 		case 'P':                       /* petabytes 1<<50 */
15135a890aacSKirill Ponomarev 			scale = 0x4000000000000LL;
15145a890aacSKirill Ponomarev 			break;
15155a890aacSKirill Ponomarev 		default:
15165a890aacSKirill Ponomarev 			errx(1, "%s: %s: illegal trailing character",
15175a890aacSKirill Ponomarev 				option->name, size_str);
15185a890aacSKirill Ponomarev 			break;
15195a890aacSKirill Ponomarev 		}
15205a890aacSKirill Ponomarev 		if (new->o_data > QUAD_MAX / scale)
15215a890aacSKirill Ponomarev 			errx(1, "%s: %s: value too large",
15225a890aacSKirill Ponomarev 				option->name, size_str);
15235a890aacSKirill Ponomarev 		new->o_data *= scale;
15245a890aacSKirill Ponomarev 	}
1525ea92232aSPoul-Henning Kamp 	return new;
15269b50d902SRodney W. Grimes }
15279b50d902SRodney W. Grimes 
15289b50d902SRodney W. Grimes /*
15299ed0c92cSDavid Malone  * -sparse functions --
15309ed0c92cSDavid Malone  *
15319ed0c92cSDavid Malone  *      Check if a file is sparse by finding if it occupies fewer blocks
15329ed0c92cSDavid Malone  *      than we expect based on its size.
15339ed0c92cSDavid Malone  */
15349ed0c92cSDavid Malone int
15359ed0c92cSDavid Malone f_sparse(PLAN *plan __unused, FTSENT *entry)
15369ed0c92cSDavid Malone {
15379ed0c92cSDavid Malone 	off_t expected_blocks;
15389ed0c92cSDavid Malone 
15399ed0c92cSDavid Malone 	expected_blocks = (entry->fts_statp->st_size + 511) / 512;
15409ed0c92cSDavid Malone 	return entry->fts_statp->st_blocks < expected_blocks;
15419ed0c92cSDavid Malone }
15429ed0c92cSDavid Malone 
15439ed0c92cSDavid Malone PLAN *
15449ed0c92cSDavid Malone c_sparse(OPTION *option, char ***argvp __unused)
15459ed0c92cSDavid Malone {
15469ed0c92cSDavid Malone 	ftsoptions &= ~FTS_NOSTAT;
15479ed0c92cSDavid Malone 
15489ed0c92cSDavid Malone 	return palloc(option);
15499ed0c92cSDavid Malone }
15509ed0c92cSDavid Malone 
15519ed0c92cSDavid Malone /*
15529b50d902SRodney W. Grimes  * -type c functions --
15539b50d902SRodney W. Grimes  *
1554841484cdSPeter Wemm  *	True if the type of the file is c, where c is b, c, d, p, f or w
1555841484cdSPeter Wemm  *	for block special file, character special file, directory, FIFO,
1556841484cdSPeter Wemm  *	regular file or whiteout respectively.
15579b50d902SRodney W. Grimes  */
15589b50d902SRodney W. Grimes int
1559ef646f18SMark Murray f_type(PLAN *plan, FTSENT *entry)
15609b50d902SRodney W. Grimes {
1561b95de98aSJilles Tjoelker 	if (plan->m_data == S_IFDIR)
1562b95de98aSJilles Tjoelker 		return (entry->fts_info == FTS_D || entry->fts_info == FTS_DC ||
1563b95de98aSJilles Tjoelker 		    entry->fts_info == FTS_DNR || entry->fts_info == FTS_DOT ||
1564b95de98aSJilles Tjoelker 		    entry->fts_info == FTS_DP);
1565b95de98aSJilles Tjoelker 	else
1566ea92232aSPoul-Henning Kamp 		return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
15679b50d902SRodney W. Grimes }
15689b50d902SRodney W. Grimes 
15699b50d902SRodney W. Grimes PLAN *
1570ef646f18SMark Murray c_type(OPTION *option, char ***argvp)
15719b50d902SRodney W. Grimes {
1572ea92232aSPoul-Henning Kamp 	char *typestring;
15739b50d902SRodney W. Grimes 	PLAN *new;
15749b50d902SRodney W. Grimes 	mode_t  mask;
15759b50d902SRodney W. Grimes 
1576ea92232aSPoul-Henning Kamp 	typestring = nextarg(option, argvp);
1577b95de98aSJilles Tjoelker 	if (typestring[0] != 'd')
15789b50d902SRodney W. Grimes 		ftsoptions &= ~FTS_NOSTAT;
15799b50d902SRodney W. Grimes 
15809b50d902SRodney W. Grimes 	switch (typestring[0]) {
15819b50d902SRodney W. Grimes 	case 'b':
15829b50d902SRodney W. Grimes 		mask = S_IFBLK;
15839b50d902SRodney W. Grimes 		break;
15849b50d902SRodney W. Grimes 	case 'c':
15859b50d902SRodney W. Grimes 		mask = S_IFCHR;
15869b50d902SRodney W. Grimes 		break;
15879b50d902SRodney W. Grimes 	case 'd':
15889b50d902SRodney W. Grimes 		mask = S_IFDIR;
15899b50d902SRodney W. Grimes 		break;
15909b50d902SRodney W. Grimes 	case 'f':
15919b50d902SRodney W. Grimes 		mask = S_IFREG;
15929b50d902SRodney W. Grimes 		break;
15939b50d902SRodney W. Grimes 	case 'l':
15949b50d902SRodney W. Grimes 		mask = S_IFLNK;
15959b50d902SRodney W. Grimes 		break;
15969b50d902SRodney W. Grimes 	case 'p':
15979b50d902SRodney W. Grimes 		mask = S_IFIFO;
15989b50d902SRodney W. Grimes 		break;
15999b50d902SRodney W. Grimes 	case 's':
16009b50d902SRodney W. Grimes 		mask = S_IFSOCK;
16019b50d902SRodney W. Grimes 		break;
1602841484cdSPeter Wemm #ifdef FTS_WHITEOUT
1603841484cdSPeter Wemm 	case 'w':
1604841484cdSPeter Wemm 		mask = S_IFWHT;
1605841484cdSPeter Wemm 		ftsoptions |= FTS_WHITEOUT;
1606841484cdSPeter Wemm 		break;
1607841484cdSPeter Wemm #endif /* FTS_WHITEOUT */
16089b50d902SRodney W. Grimes 	default:
1609ea92232aSPoul-Henning Kamp 		errx(1, "%s: %s: unknown type", option->name, typestring);
16109b50d902SRodney W. Grimes 	}
16119b50d902SRodney W. Grimes 
1612ea92232aSPoul-Henning Kamp 	new = palloc(option);
16139b50d902SRodney W. Grimes 	new->m_data = mask;
1614ea92232aSPoul-Henning Kamp 	return new;
1615abacbbbfSPeter Wemm }
1616abacbbbfSPeter Wemm 
1617abacbbbfSPeter Wemm /*
16189b50d902SRodney W. Grimes  * -user uname functions --
16199b50d902SRodney W. Grimes  *
16209b50d902SRodney W. Grimes  *	True if the file belongs to the user uname.  If uname is numeric and
16219b50d902SRodney W. Grimes  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
16229b50d902SRodney W. Grimes  *	return a valid user name, uname is taken as a user ID.
16239b50d902SRodney W. Grimes  */
16249b50d902SRodney W. Grimes int
1625ef646f18SMark Murray f_user(PLAN *plan, FTSENT *entry)
16269b50d902SRodney W. Grimes {
16274ba3b38bSKirill Ponomarev 	COMPARE(entry->fts_statp->st_uid, plan->u_data);
16289b50d902SRodney W. Grimes }
16299b50d902SRodney W. Grimes 
16309b50d902SRodney W. Grimes PLAN *
1631ef646f18SMark Murray c_user(OPTION *option, char ***argvp)
16329b50d902SRodney W. Grimes {
1633ea92232aSPoul-Henning Kamp 	char *username;
16349b50d902SRodney W. Grimes 	PLAN *new;
16359b50d902SRodney W. Grimes 	struct passwd *p;
16369b50d902SRodney W. Grimes 	uid_t uid;
16379b50d902SRodney W. Grimes 
1638ea92232aSPoul-Henning Kamp 	username = nextarg(option, argvp);
16399b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
16409b50d902SRodney W. Grimes 
16414ba3b38bSKirill Ponomarev 	new = palloc(option);
16429b50d902SRodney W. Grimes 	p = getpwnam(username);
16439b50d902SRodney W. Grimes 	if (p == NULL) {
16444ba3b38bSKirill Ponomarev 		char* cp = username;
16454ba3b38bSKirill Ponomarev 		if( username[0] == '-' || username[0] == '+' )
16464ba3b38bSKirill Ponomarev 			username++;
16479b50d902SRodney W. Grimes 		uid = atoi(username);
16489b50d902SRodney W. Grimes 		if (uid == 0 && username[0] != '0')
1649ea92232aSPoul-Henning Kamp 			errx(1, "%s: %s: no such user", option->name, username);
16504ba3b38bSKirill Ponomarev 		uid = find_parsenum(new, option->name, cp, NULL);
16519b50d902SRodney W. Grimes 	} else
16529b50d902SRodney W. Grimes 		uid = p->pw_uid;
16539b50d902SRodney W. Grimes 
16549b50d902SRodney W. Grimes 	new->u_data = uid;
1655ea92232aSPoul-Henning Kamp 	return new;
16569b50d902SRodney W. Grimes }
16579b50d902SRodney W. Grimes 
16589b50d902SRodney W. Grimes /*
16599b50d902SRodney W. Grimes  * -xdev functions --
16609b50d902SRodney W. Grimes  *
16619725a7b9SPhilippe Charnier  *	Always true, causes find not to descend past directories that have a
16629b50d902SRodney W. Grimes  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
16639b50d902SRodney W. Grimes  */
16649b50d902SRodney W. Grimes PLAN *
1665ef646f18SMark Murray c_xdev(OPTION *option, char ***argvp __unused)
16669b50d902SRodney W. Grimes {
16679b50d902SRodney W. Grimes 	ftsoptions |= FTS_XDEV;
16689b50d902SRodney W. Grimes 
1669ea92232aSPoul-Henning Kamp 	return palloc(option);
16709b50d902SRodney W. Grimes }
16719b50d902SRodney W. Grimes 
16729b50d902SRodney W. Grimes /*
16739b50d902SRodney W. Grimes  * ( expression ) functions --
16749b50d902SRodney W. Grimes  *
16759b50d902SRodney W. Grimes  *	True if expression is true.
16769b50d902SRodney W. Grimes  */
16779b50d902SRodney W. Grimes int
1678ef646f18SMark Murray f_expr(PLAN *plan, FTSENT *entry)
16799b50d902SRodney W. Grimes {
1680e98080b1SDavid Malone 	PLAN *p;
1681e98080b1SDavid Malone 	int state = 0;
16829b50d902SRodney W. Grimes 
16839b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
1684ea92232aSPoul-Henning Kamp 	    p && (state = (p->execute)(p, entry)); p = p->next);
1685ea92232aSPoul-Henning Kamp 	return state;
16869b50d902SRodney W. Grimes }
16879b50d902SRodney W. Grimes 
16889b50d902SRodney W. Grimes /*
1689ea92232aSPoul-Henning Kamp  * f_openparen and f_closeparen nodes are temporary place markers.  They are
16909b50d902SRodney W. Grimes  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1691ea92232aSPoul-Henning Kamp  * to a f_expr node containing the expression and the ')' node is discarded.
1692ea92232aSPoul-Henning Kamp  * The functions themselves are only used as constants.
16939b50d902SRodney W. Grimes  */
1694ea92232aSPoul-Henning Kamp 
1695ea92232aSPoul-Henning Kamp int
1696ef646f18SMark Murray f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
16979b50d902SRodney W. Grimes {
1698ea92232aSPoul-Henning Kamp 	abort();
16999b50d902SRodney W. Grimes }
17009b50d902SRodney W. Grimes 
1701ea92232aSPoul-Henning Kamp int
1702ef646f18SMark Murray f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
17039b50d902SRodney W. Grimes {
1704ea92232aSPoul-Henning Kamp 	abort();
1705ea92232aSPoul-Henning Kamp }
1706ea92232aSPoul-Henning Kamp 
1707ea92232aSPoul-Henning Kamp /* c_openparen == c_simple */
1708ea92232aSPoul-Henning Kamp /* c_closeparen == c_simple */
1709ea92232aSPoul-Henning Kamp 
1710ea92232aSPoul-Henning Kamp /*
1711ea92232aSPoul-Henning Kamp  * AND operator. Since AND is implicit, no node is allocated.
1712ea92232aSPoul-Henning Kamp  */
1713ea92232aSPoul-Henning Kamp PLAN *
1714ef646f18SMark Murray c_and(OPTION *option __unused, char ***argvp __unused)
1715ea92232aSPoul-Henning Kamp {
1716ea92232aSPoul-Henning Kamp 	return NULL;
17179b50d902SRodney W. Grimes }
17189b50d902SRodney W. Grimes 
17199b50d902SRodney W. Grimes /*
17209b50d902SRodney W. Grimes  * ! expression functions --
17219b50d902SRodney W. Grimes  *
17229b50d902SRodney W. Grimes  *	Negation of a primary; the unary NOT operator.
17239b50d902SRodney W. Grimes  */
17249b50d902SRodney W. Grimes int
1725ef646f18SMark Murray f_not(PLAN *plan, FTSENT *entry)
17269b50d902SRodney W. Grimes {
1727e98080b1SDavid Malone 	PLAN *p;
1728e98080b1SDavid Malone 	int state = 0;
17299b50d902SRodney W. Grimes 
17309b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
1731ea92232aSPoul-Henning Kamp 	    p && (state = (p->execute)(p, entry)); p = p->next);
1732ea92232aSPoul-Henning Kamp 	return !state;
17339b50d902SRodney W. Grimes }
17349b50d902SRodney W. Grimes 
1735ea92232aSPoul-Henning Kamp /* c_not == c_simple */
17369b50d902SRodney W. Grimes 
17379b50d902SRodney W. Grimes /*
17389b50d902SRodney W. Grimes  * expression -o expression functions --
17399b50d902SRodney W. Grimes  *
17409b50d902SRodney W. Grimes  *	Alternation of primaries; the OR operator.  The second expression is
17419b50d902SRodney W. Grimes  * not evaluated if the first expression is true.
17429b50d902SRodney W. Grimes  */
17439b50d902SRodney W. Grimes int
1744ef646f18SMark Murray f_or(PLAN *plan, FTSENT *entry)
17459b50d902SRodney W. Grimes {
1746e98080b1SDavid Malone 	PLAN *p;
1747e98080b1SDavid Malone 	int state = 0;
17489b50d902SRodney W. Grimes 
17499b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
1750ea92232aSPoul-Henning Kamp 	    p && (state = (p->execute)(p, entry)); p = p->next);
17519b50d902SRodney W. Grimes 
17529b50d902SRodney W. Grimes 	if (state)
1753ea92232aSPoul-Henning Kamp 		return 1;
17549b50d902SRodney W. Grimes 
17559b50d902SRodney W. Grimes 	for (p = plan->p_data[1];
1756ea92232aSPoul-Henning Kamp 	    p && (state = (p->execute)(p, entry)); p = p->next);
1757ea92232aSPoul-Henning Kamp 	return state;
17589b50d902SRodney W. Grimes }
17599b50d902SRodney W. Grimes 
1760ea92232aSPoul-Henning Kamp /* c_or == c_simple */
176146b993ffSWarner Losh 
176246b993ffSWarner Losh /*
176346b993ffSWarner Losh  * -false
176446b993ffSWarner Losh  *
176546b993ffSWarner Losh  *	Always false.
176646b993ffSWarner Losh  */
176746b993ffSWarner Losh int
176846b993ffSWarner Losh f_false(PLAN *plan __unused, FTSENT *entry __unused)
176946b993ffSWarner Losh {
177046b993ffSWarner Losh 	return 0;
177146b993ffSWarner Losh }
177246b993ffSWarner Losh 
177346b993ffSWarner Losh /* c_false == c_simple */
177446b993ffSWarner Losh 
177546b993ffSWarner Losh /*
177646b993ffSWarner Losh  * -quit
177746b993ffSWarner Losh  *
177846b993ffSWarner Losh  *	Exits the program
177946b993ffSWarner Losh  */
178046b993ffSWarner Losh int
178146b993ffSWarner Losh f_quit(PLAN *plan __unused, FTSENT *entry __unused)
178246b993ffSWarner Losh {
1783cf599562SJilles Tjoelker 	finish_execplus();
1784*b547523eSJilles Tjoelker 	exit(exitstatus);
178546b993ffSWarner Losh }
178646b993ffSWarner Losh 
178746b993ffSWarner Losh /* c_quit == c_simple */
1788