xref: /freebsd/usr.bin/find/function.c (revision 05e605b7649eaa47028b444ece1800b5ed11e6e5)
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>
681c832963SOliver Eikemeier #include <ctype.h>
699b50d902SRodney W. Grimes 
709b50d902SRodney W. Grimes #include "find.h"
719b50d902SRodney W. Grimes 
72ecca1f1cSMark Murray static PLAN *palloc(OPTION *);
73ecca1f1cSMark Murray static long long find_parsenum(PLAN *, const char *, char *, char *);
74ecca1f1cSMark Murray static long long find_parsetime(PLAN *, const char *, char *);
75ecca1f1cSMark Murray static char *nextarg(OPTION *, char ***);
76ea92232aSPoul-Henning Kamp 
77a07af811STim J. Robbins extern char **environ;
78a07af811STim J. Robbins 
7922170420SKirill Ponomarev static PLAN *lastexecplus = NULL;
8022170420SKirill Ponomarev 
81adff4fcaSRuslan Ermilov #define	COMPARE(a, b) do {						\
82ea92232aSPoul-Henning Kamp 	switch (plan->flags & F_ELG_MASK) {				\
839b50d902SRodney W. Grimes 	case F_EQUAL:							\
849b50d902SRodney W. Grimes 		return (a == b);					\
859b50d902SRodney W. Grimes 	case F_LESSTHAN:						\
869b50d902SRodney W. Grimes 		return (a < b);						\
879b50d902SRodney W. Grimes 	case F_GREATER:							\
889b50d902SRodney W. Grimes 		return (a > b);						\
899b50d902SRodney W. Grimes 	default:							\
909b50d902SRodney W. Grimes 		abort();						\
919b50d902SRodney W. Grimes 	}								\
92adff4fcaSRuslan Ermilov } while(0)
939b50d902SRodney W. Grimes 
94ea92232aSPoul-Henning Kamp static PLAN *
95ef646f18SMark Murray palloc(OPTION *option)
96ea92232aSPoul-Henning Kamp {
97ea92232aSPoul-Henning Kamp 	PLAN *new;
98ea92232aSPoul-Henning Kamp 
99ea92232aSPoul-Henning Kamp 	if ((new = malloc(sizeof(PLAN))) == NULL)
100ea92232aSPoul-Henning Kamp 		err(1, NULL);
101ea92232aSPoul-Henning Kamp 	new->execute = option->execute;
102ea92232aSPoul-Henning Kamp 	new->flags = option->flags;
103ea92232aSPoul-Henning Kamp 	new->next = NULL;
104ea92232aSPoul-Henning Kamp 	return new;
105ea92232aSPoul-Henning Kamp }
1069b50d902SRodney W. Grimes 
1079b50d902SRodney W. Grimes /*
1089b50d902SRodney W. Grimes  * find_parsenum --
1099b50d902SRodney W. Grimes  *	Parse a string of the form [+-]# and return the value.
1109b50d902SRodney W. Grimes  */
1119192bbf4SBruce Evans static long long
112ef646f18SMark Murray find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
1139b50d902SRodney W. Grimes {
1149192bbf4SBruce Evans 	long long value;
1159b50d902SRodney W. Grimes 	char *endchar, *str;	/* Pointer to character ending conversion. */
1169b50d902SRodney W. Grimes 
1179b50d902SRodney W. Grimes 	/* Determine comparison from leading + or -. */
1189b50d902SRodney W. Grimes 	str = vp;
1199b50d902SRodney W. Grimes 	switch (*str) {
1209b50d902SRodney W. Grimes 	case '+':
1219b50d902SRodney W. Grimes 		++str;
122ea92232aSPoul-Henning Kamp 		plan->flags |= F_GREATER;
1239b50d902SRodney W. Grimes 		break;
1249b50d902SRodney W. Grimes 	case '-':
1259b50d902SRodney W. Grimes 		++str;
126ea92232aSPoul-Henning Kamp 		plan->flags |= F_LESSTHAN;
1279b50d902SRodney W. Grimes 		break;
1289b50d902SRodney W. Grimes 	default:
129ea92232aSPoul-Henning Kamp 		plan->flags |= F_EQUAL;
1309b50d902SRodney W. Grimes 		break;
1319b50d902SRodney W. Grimes 	}
1329b50d902SRodney W. Grimes 
1339b50d902SRodney W. Grimes 	/*
1349192bbf4SBruce Evans 	 * Convert the string with strtoq().  Note, if strtoq() returns zero
1359b50d902SRodney W. Grimes 	 * and endchar points to the beginning of the string we know we have
1369b50d902SRodney W. Grimes 	 * a syntax error.
1379b50d902SRodney W. Grimes 	 */
1389192bbf4SBruce Evans 	value = strtoq(str, &endchar, 10);
1399b50d902SRodney W. Grimes 	if (value == 0 && endchar == str)
1409b50d902SRodney W. Grimes 		errx(1, "%s: %s: illegal numeric value", option, vp);
1415a890aacSKirill Ponomarev 	if (endchar[0] && endch == NULL)
1429b50d902SRodney W. Grimes 		errx(1, "%s: %s: illegal trailing character", option, vp);
1439b50d902SRodney W. Grimes 	if (endch)
1449b50d902SRodney W. Grimes 		*endch = endchar[0];
145ea92232aSPoul-Henning Kamp 	return value;
1469b50d902SRodney W. Grimes }
1479b50d902SRodney W. Grimes 
1489b50d902SRodney W. Grimes /*
149adff4fcaSRuslan Ermilov  * find_parsetime --
150adff4fcaSRuslan Ermilov  *	Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
151adff4fcaSRuslan Ermilov  */
152adff4fcaSRuslan Ermilov static long long
153ef646f18SMark Murray find_parsetime(PLAN *plan, const char *option, char *vp)
154adff4fcaSRuslan Ermilov {
155adff4fcaSRuslan Ermilov 	long long secs, value;
156adff4fcaSRuslan Ermilov 	char *str, *unit;	/* Pointer to character ending conversion. */
157adff4fcaSRuslan Ermilov 
158adff4fcaSRuslan Ermilov 	/* Determine comparison from leading + or -. */
159adff4fcaSRuslan Ermilov 	str = vp;
160adff4fcaSRuslan Ermilov 	switch (*str) {
161adff4fcaSRuslan Ermilov 	case '+':
162adff4fcaSRuslan Ermilov 		++str;
163adff4fcaSRuslan Ermilov 		plan->flags |= F_GREATER;
164adff4fcaSRuslan Ermilov 		break;
165adff4fcaSRuslan Ermilov 	case '-':
166adff4fcaSRuslan Ermilov 		++str;
167adff4fcaSRuslan Ermilov 		plan->flags |= F_LESSTHAN;
168adff4fcaSRuslan Ermilov 		break;
169adff4fcaSRuslan Ermilov 	default:
170adff4fcaSRuslan Ermilov 		plan->flags |= F_EQUAL;
171adff4fcaSRuslan Ermilov 		break;
172adff4fcaSRuslan Ermilov 	}
173adff4fcaSRuslan Ermilov 
174adff4fcaSRuslan Ermilov 	value = strtoq(str, &unit, 10);
175adff4fcaSRuslan Ermilov 	if (value == 0 && unit == str) {
176adff4fcaSRuslan Ermilov 		errx(1, "%s: %s: illegal time value", option, vp);
177adff4fcaSRuslan Ermilov 		/* NOTREACHED */
178adff4fcaSRuslan Ermilov 	}
179adff4fcaSRuslan Ermilov 	if (*unit == '\0')
180adff4fcaSRuslan Ermilov 		return value;
181adff4fcaSRuslan Ermilov 
182adff4fcaSRuslan Ermilov 	/* Units syntax. */
183adff4fcaSRuslan Ermilov 	secs = 0;
184adff4fcaSRuslan Ermilov 	for (;;) {
185adff4fcaSRuslan Ermilov 		switch(*unit) {
186adff4fcaSRuslan Ermilov 		case 's':	/* seconds */
187adff4fcaSRuslan Ermilov 			secs += value;
188adff4fcaSRuslan Ermilov 			break;
189adff4fcaSRuslan Ermilov 		case 'm':	/* minutes */
190adff4fcaSRuslan Ermilov 			secs += value * 60;
191adff4fcaSRuslan Ermilov 			break;
192adff4fcaSRuslan Ermilov 		case 'h':	/* hours */
193adff4fcaSRuslan Ermilov 			secs += value * 3600;
194adff4fcaSRuslan Ermilov 			break;
195adff4fcaSRuslan Ermilov 		case 'd':	/* days */
196adff4fcaSRuslan Ermilov 			secs += value * 86400;
197adff4fcaSRuslan Ermilov 			break;
198adff4fcaSRuslan Ermilov 		case 'w':	/* weeks */
199adff4fcaSRuslan Ermilov 			secs += value * 604800;
200adff4fcaSRuslan Ermilov 			break;
201adff4fcaSRuslan Ermilov 		default:
202adff4fcaSRuslan Ermilov 			errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
203adff4fcaSRuslan Ermilov 			/* NOTREACHED */
204adff4fcaSRuslan Ermilov 		}
205adff4fcaSRuslan Ermilov 		str = unit + 1;
206adff4fcaSRuslan Ermilov 		if (*str == '\0')	/* EOS */
207adff4fcaSRuslan Ermilov 			break;
208adff4fcaSRuslan Ermilov 		value = strtoq(str, &unit, 10);
209adff4fcaSRuslan Ermilov 		if (value == 0 && unit == str) {
210adff4fcaSRuslan Ermilov 			errx(1, "%s: %s: illegal time value", option, vp);
211adff4fcaSRuslan Ermilov 			/* NOTREACHED */
212adff4fcaSRuslan Ermilov 		}
213adff4fcaSRuslan Ermilov 		if (*unit == '\0') {
214adff4fcaSRuslan Ermilov 			errx(1, "%s: %s: missing trailing unit", option, vp);
215adff4fcaSRuslan Ermilov 			/* NOTREACHED */
216adff4fcaSRuslan Ermilov 		}
217adff4fcaSRuslan Ermilov 	}
218adff4fcaSRuslan Ermilov 	plan->flags |= F_EXACTTIME;
219adff4fcaSRuslan Ermilov 	return secs;
220adff4fcaSRuslan Ermilov }
221adff4fcaSRuslan Ermilov 
222adff4fcaSRuslan Ermilov /*
223ea92232aSPoul-Henning Kamp  * nextarg --
224ea92232aSPoul-Henning Kamp  *	Check that another argument still exists, return a pointer to it,
225ea92232aSPoul-Henning Kamp  *	and increment the argument vector pointer.
226ea92232aSPoul-Henning Kamp  */
227ea92232aSPoul-Henning Kamp static char *
228ef646f18SMark Murray nextarg(OPTION *option, char ***argvp)
229ea92232aSPoul-Henning Kamp {
230ea92232aSPoul-Henning Kamp 	char *arg;
231ea92232aSPoul-Henning Kamp 
232ea92232aSPoul-Henning Kamp 	if ((arg = **argvp) == 0)
233ea92232aSPoul-Henning Kamp 		errx(1, "%s: requires additional arguments", option->name);
234ea92232aSPoul-Henning Kamp 	(*argvp)++;
235ea92232aSPoul-Henning Kamp 	return arg;
236ea92232aSPoul-Henning Kamp } /* nextarg() */
237ea92232aSPoul-Henning Kamp 
238ea92232aSPoul-Henning Kamp /*
23931d53425SCeri Davies  * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
24031d53425SCeri Davies  * range, i.e. n matches from (n - 1) to n 24 hour periods.  This interacts
24131d53425SCeri Davies  * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
24231d53425SCeri Davies  * the user wanted.  Correct so that -1 is "less than 1".
2439b50d902SRodney W. Grimes  */
244ea92232aSPoul-Henning Kamp #define	TIME_CORRECT(p) \
245ea92232aSPoul-Henning Kamp 	if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
2469b50d902SRodney W. Grimes 		++((p)->t_data);
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes /*
249ea92232aSPoul-Henning Kamp  * -[acm]min n functions --
2503f5223f8SWolfram Schneider  *
251ea92232aSPoul-Henning Kamp  *    True if the difference between the
252ea92232aSPoul-Henning Kamp  *		file access time (-amin)
25331d53425SCeri Davies  *		file birth time (-Bmin)
254ea92232aSPoul-Henning Kamp  *		last change of file status information (-cmin)
255ea92232aSPoul-Henning Kamp  *		file modification time (-mmin)
256ea92232aSPoul-Henning Kamp  *    and the current time is n min periods.
2573f5223f8SWolfram Schneider  */
2583f5223f8SWolfram Schneider int
259ef646f18SMark Murray f_Xmin(PLAN *plan, FTSENT *entry)
2603f5223f8SWolfram Schneider {
261ea92232aSPoul-Henning Kamp 	if (plan->flags & F_TIME_C) {
2623f5223f8SWolfram Schneider 		COMPARE((now - entry->fts_statp->st_ctime +
2633f5223f8SWolfram Schneider 		    60 - 1) / 60, plan->t_data);
264ea92232aSPoul-Henning Kamp 	} else if (plan->flags & F_TIME_A) {
265ea92232aSPoul-Henning Kamp 		COMPARE((now - entry->fts_statp->st_atime +
266ea92232aSPoul-Henning Kamp 		    60 - 1) / 60, plan->t_data);
26731d53425SCeri Davies 	} else if (plan->flags & F_TIME_B) {
26831d53425SCeri Davies 		COMPARE((now - entry->fts_statp->st_birthtime +
26931d53425SCeri Davies 		    60 - 1) / 60, plan->t_data);
270ea92232aSPoul-Henning Kamp 	} else {
271ea92232aSPoul-Henning Kamp 		COMPARE((now - entry->fts_statp->st_mtime +
272ea92232aSPoul-Henning Kamp 		    60 - 1) / 60, plan->t_data);
273ea92232aSPoul-Henning Kamp 	}
2743f5223f8SWolfram Schneider }
2753f5223f8SWolfram Schneider 
2763f5223f8SWolfram Schneider PLAN *
277ef646f18SMark Murray c_Xmin(OPTION *option, char ***argvp)
2783f5223f8SWolfram Schneider {
279ea92232aSPoul-Henning Kamp 	char *nmins;
2803f5223f8SWolfram Schneider 	PLAN *new;
2813f5223f8SWolfram Schneider 
282ea92232aSPoul-Henning Kamp 	nmins = nextarg(option, argvp);
2833f5223f8SWolfram Schneider 	ftsoptions &= ~FTS_NOSTAT;
2843f5223f8SWolfram Schneider 
285ea92232aSPoul-Henning Kamp 	new = palloc(option);
286ea92232aSPoul-Henning Kamp 	new->t_data = find_parsenum(new, option->name, nmins, NULL);
287ea92232aSPoul-Henning Kamp 	TIME_CORRECT(new);
288ea92232aSPoul-Henning Kamp 	return new;
2893f5223f8SWolfram Schneider }
2903f5223f8SWolfram Schneider 
2919b50d902SRodney W. Grimes /*
292ea92232aSPoul-Henning Kamp  * -[acm]time n functions --
2939b50d902SRodney W. Grimes  *
294ea92232aSPoul-Henning Kamp  *	True if the difference between the
295ea92232aSPoul-Henning Kamp  *		file access time (-atime)
29631d53425SCeri Davies  *		file birth time (-Btime)
297ea92232aSPoul-Henning Kamp  *		last change of file status information (-ctime)
298ea92232aSPoul-Henning Kamp  *		file modification time (-mtime)
299ea92232aSPoul-Henning Kamp  *	and the current time is n 24 hour periods.
3009b50d902SRodney W. Grimes  */
301ea92232aSPoul-Henning Kamp 
3029b50d902SRodney W. Grimes int
303ef646f18SMark Murray f_Xtime(PLAN *plan, FTSENT *entry)
3049b50d902SRodney W. Grimes {
305631a8765SRuslan Ermilov 	time_t xtime;
306adff4fcaSRuslan Ermilov 
307631a8765SRuslan Ermilov 	if (plan->flags & F_TIME_A)
308631a8765SRuslan Ermilov 		xtime = entry->fts_statp->st_atime;
30931d53425SCeri Davies 	else if (plan->flags & F_TIME_B)
31031d53425SCeri Davies 		xtime = entry->fts_statp->st_birthtime;
311631a8765SRuslan Ermilov 	else if (plan->flags & F_TIME_C)
312631a8765SRuslan Ermilov 		xtime = entry->fts_statp->st_ctime;
313631a8765SRuslan Ermilov 	else
314631a8765SRuslan Ermilov 		xtime = entry->fts_statp->st_mtime;
3159b50d902SRodney W. Grimes 
316631a8765SRuslan Ermilov 	if (plan->flags & F_EXACTTIME)
317631a8765SRuslan Ermilov 		COMPARE(now - xtime, plan->t_data);
318adff4fcaSRuslan Ermilov 	else
319631a8765SRuslan Ermilov 		COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data);
3209b50d902SRodney W. Grimes }
3219b50d902SRodney W. Grimes 
3229b50d902SRodney W. Grimes PLAN *
323ef646f18SMark Murray c_Xtime(OPTION *option, char ***argvp)
3249b50d902SRodney W. Grimes {
325adff4fcaSRuslan Ermilov 	char *value;
3269b50d902SRodney W. Grimes 	PLAN *new;
3279b50d902SRodney W. Grimes 
328adff4fcaSRuslan Ermilov 	value = nextarg(option, argvp);
3299b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
3309b50d902SRodney W. Grimes 
331ea92232aSPoul-Henning Kamp 	new = palloc(option);
332adff4fcaSRuslan Ermilov 	new->t_data = find_parsetime(new, option->name, value);
333adff4fcaSRuslan Ermilov 	if (!(new->flags & F_EXACTTIME))
334ea92232aSPoul-Henning Kamp 		TIME_CORRECT(new);
335ea92232aSPoul-Henning Kamp 	return new;
336ea92232aSPoul-Henning Kamp }
337ea92232aSPoul-Henning Kamp 
338ea92232aSPoul-Henning Kamp /*
339ea92232aSPoul-Henning Kamp  * -maxdepth/-mindepth n functions --
340ea92232aSPoul-Henning Kamp  *
341ea92232aSPoul-Henning Kamp  *        Does the same as -prune if the level of the current file is
342ea92232aSPoul-Henning Kamp  *        greater/less than the specified maximum/minimum depth.
343ea92232aSPoul-Henning Kamp  *
344ea92232aSPoul-Henning Kamp  *        Note that -maxdepth and -mindepth are handled specially in
345ea92232aSPoul-Henning Kamp  *        find_execute() so their f_* functions are set to f_always_true().
346ea92232aSPoul-Henning Kamp  */
347ea92232aSPoul-Henning Kamp PLAN *
348ef646f18SMark Murray c_mXXdepth(OPTION *option, char ***argvp)
349ea92232aSPoul-Henning Kamp {
350ea92232aSPoul-Henning Kamp 	char *dstr;
351ea92232aSPoul-Henning Kamp 	PLAN *new;
352ea92232aSPoul-Henning Kamp 
353ea92232aSPoul-Henning Kamp 	dstr = nextarg(option, argvp);
354ea92232aSPoul-Henning Kamp 	if (dstr[0] == '-')
355ea92232aSPoul-Henning Kamp 		/* all other errors handled by find_parsenum() */
356ea92232aSPoul-Henning Kamp 		errx(1, "%s: %s: value must be positive", option->name, dstr);
357ea92232aSPoul-Henning Kamp 
358ea92232aSPoul-Henning Kamp 	new = palloc(option);
359ea92232aSPoul-Henning Kamp 	if (option->flags & F_MAXDEPTH)
360ea92232aSPoul-Henning Kamp 		maxdepth = find_parsenum(new, option->name, dstr, NULL);
361ea92232aSPoul-Henning Kamp 	else
362ea92232aSPoul-Henning Kamp 		mindepth = find_parsenum(new, option->name, dstr, NULL);
363ea92232aSPoul-Henning Kamp 	return new;
364ea92232aSPoul-Henning Kamp }
365ea92232aSPoul-Henning Kamp 
366ea92232aSPoul-Henning Kamp /*
3679c5d31dfSBosko Milekic  * -acl function --
3689c5d31dfSBosko Milekic  *
3699c5d31dfSBosko Milekic  *	Show files with EXTENDED ACL attributes.
3709c5d31dfSBosko Milekic  */
3719c5d31dfSBosko Milekic int
3729c5d31dfSBosko Milekic f_acl(PLAN *plan __unused, FTSENT *entry)
3739c5d31dfSBosko Milekic {
3749c5d31dfSBosko Milekic 	int match, entries;
3759c5d31dfSBosko Milekic 	acl_entry_t ae;
3769c5d31dfSBosko Milekic 	acl_t facl;
3779c5d31dfSBosko Milekic 
3789c5d31dfSBosko Milekic 	if (S_ISLNK(entry->fts_statp->st_mode))
3799c5d31dfSBosko Milekic 		return 0;
3809c5d31dfSBosko Milekic 	if ((match = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED)) <= 0) {
3819c5d31dfSBosko Milekic 		if (match < 0 && errno != EINVAL)
3829c5d31dfSBosko Milekic 			warn("%s", entry->fts_accpath);
3839c5d31dfSBosko Milekic 	else
3849c5d31dfSBosko Milekic 		return 0;
3859c5d31dfSBosko Milekic 	}
3869c5d31dfSBosko Milekic 	match = 0;
3879c5d31dfSBosko Milekic 	if ((facl = acl_get_file(entry->fts_accpath,ACL_TYPE_ACCESS)) != NULL) {
3889c5d31dfSBosko Milekic 		if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 1) {
3899c5d31dfSBosko Milekic 			/*
3909c5d31dfSBosko Milekic 			 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS
3919c5d31dfSBosko Milekic 			 * must have at least three entries (owner, group,
3929c5d31dfSBosko Milekic 			 * other).
3939c5d31dfSBosko Milekic 			 */
3949c5d31dfSBosko Milekic 			entries = 1;
3959c5d31dfSBosko Milekic 			while (acl_get_entry(facl, ACL_NEXT_ENTRY, &ae) == 1) {
3969c5d31dfSBosko Milekic 				if (++entries > 3) {
3979c5d31dfSBosko Milekic 					match = 1;
3989c5d31dfSBosko Milekic 					break;
3999c5d31dfSBosko Milekic 				}
4009c5d31dfSBosko Milekic 			}
4019c5d31dfSBosko Milekic 		}
4029c5d31dfSBosko Milekic 		acl_free(facl);
4039c5d31dfSBosko Milekic 	} else
4049c5d31dfSBosko Milekic 		warn("%s", entry->fts_accpath);
4059c5d31dfSBosko Milekic 	return match;
4069c5d31dfSBosko Milekic }
4079c5d31dfSBosko Milekic 
4089c5d31dfSBosko Milekic PLAN *
4099c5d31dfSBosko Milekic c_acl(OPTION *option, char ***argvp __unused)
4109c5d31dfSBosko Milekic {
4119c5d31dfSBosko Milekic 	ftsoptions &= ~FTS_NOSTAT;
4129c5d31dfSBosko Milekic 	return (palloc(option));
4139c5d31dfSBosko Milekic }
4149c5d31dfSBosko Milekic 
4159c5d31dfSBosko Milekic /*
416ea92232aSPoul-Henning Kamp  * -delete functions --
417ea92232aSPoul-Henning Kamp  *
418ea92232aSPoul-Henning Kamp  *	True always.  Makes its best shot and continues on regardless.
419ea92232aSPoul-Henning Kamp  */
420ea92232aSPoul-Henning Kamp int
421ef646f18SMark Murray f_delete(PLAN *plan __unused, FTSENT *entry)
422ea92232aSPoul-Henning Kamp {
423ea92232aSPoul-Henning Kamp 	/* ignore these from fts */
424ea92232aSPoul-Henning Kamp 	if (strcmp(entry->fts_accpath, ".") == 0 ||
425ea92232aSPoul-Henning Kamp 	    strcmp(entry->fts_accpath, "..") == 0)
426ea92232aSPoul-Henning Kamp 		return 1;
427ea92232aSPoul-Henning Kamp 
428ea92232aSPoul-Henning Kamp 	/* sanity check */
429ea92232aSPoul-Henning Kamp 	if (isdepth == 0 ||			/* depth off */
43005e605b7SAndriy Gapon 	    (ftsoptions & FTS_NOSTAT))		/* not stat()ing */
431ea92232aSPoul-Henning Kamp 		errx(1, "-delete: insecure options got turned on");
432ea92232aSPoul-Henning Kamp 
43305e605b7SAndriy Gapon 	if (!(ftsoptions & FTS_PHYSICAL) ||	/* physical off */
43405e605b7SAndriy Gapon 	    (ftsoptions & FTS_LOGICAL))		/* or finally, logical on */
43505e605b7SAndriy Gapon 		errx(1, "-delete: forbidden when symlinks are followed");
43605e605b7SAndriy Gapon 
437ea92232aSPoul-Henning Kamp 	/* Potentially unsafe - do not accept relative paths whatsoever */
438ea92232aSPoul-Henning Kamp 	if (strchr(entry->fts_accpath, '/') != NULL)
439ea92232aSPoul-Henning Kamp 		errx(1, "-delete: %s: relative path potentially not safe",
440ea92232aSPoul-Henning Kamp 			entry->fts_accpath);
441ea92232aSPoul-Henning Kamp 
442ea92232aSPoul-Henning Kamp 	/* Turn off user immutable bits if running as root */
443ea92232aSPoul-Henning Kamp 	if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
444ea92232aSPoul-Henning Kamp 	    !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
445ea92232aSPoul-Henning Kamp 	    geteuid() == 0)
446ea92232aSPoul-Henning Kamp 		chflags(entry->fts_accpath,
447ea92232aSPoul-Henning Kamp 		       entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
448ea92232aSPoul-Henning Kamp 
449ea92232aSPoul-Henning Kamp 	/* rmdir directories, unlink everything else */
450ea92232aSPoul-Henning Kamp 	if (S_ISDIR(entry->fts_statp->st_mode)) {
451ea92232aSPoul-Henning Kamp 		if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
452ea92232aSPoul-Henning Kamp 			warn("-delete: rmdir(%s)", entry->fts_path);
453ea92232aSPoul-Henning Kamp 	} else {
454ea92232aSPoul-Henning Kamp 		if (unlink(entry->fts_accpath) < 0)
455ea92232aSPoul-Henning Kamp 			warn("-delete: unlink(%s)", entry->fts_path);
456ea92232aSPoul-Henning Kamp 	}
457ea92232aSPoul-Henning Kamp 
458ea92232aSPoul-Henning Kamp 	/* "succeed" */
459ea92232aSPoul-Henning Kamp 	return 1;
460ea92232aSPoul-Henning Kamp }
461ea92232aSPoul-Henning Kamp 
462ea92232aSPoul-Henning Kamp PLAN *
463ef646f18SMark Murray c_delete(OPTION *option, char ***argvp __unused)
464ea92232aSPoul-Henning Kamp {
465ea92232aSPoul-Henning Kamp 
466ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;	/* no optimise */
467ea92232aSPoul-Henning Kamp 	isoutput = 1;			/* possible output */
468ea92232aSPoul-Henning Kamp 	isdepth = 1;			/* -depth implied */
469ea92232aSPoul-Henning Kamp 
470ea92232aSPoul-Henning Kamp 	return palloc(option);
4719b50d902SRodney W. Grimes }
4729b50d902SRodney W. Grimes 
4733f5223f8SWolfram Schneider 
4749b50d902SRodney W. Grimes /*
4751c832963SOliver Eikemeier  * always_true --
4769b50d902SRodney W. Grimes  *
47746b993ffSWarner Losh  *	Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
4789b50d902SRodney W. Grimes  */
4799b50d902SRodney W. Grimes int
480ef646f18SMark Murray f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
4819b50d902SRodney W. Grimes {
482ea92232aSPoul-Henning Kamp 	return 1;
4839b50d902SRodney W. Grimes }
4849b50d902SRodney W. Grimes 
4851c832963SOliver Eikemeier /*
4861c832963SOliver Eikemeier  * -depth functions --
4871c832963SOliver Eikemeier  *
4881c832963SOliver Eikemeier  *	With argument: True if the file is at level n.
4891c832963SOliver Eikemeier  *	Without argument: Always true, causes descent of the directory hierarchy
4901c832963SOliver Eikemeier  *	to be done so that all entries in a directory are acted on before the
4911c832963SOliver Eikemeier  *	directory itself.
4921c832963SOliver Eikemeier  */
4931c832963SOliver Eikemeier int
4941c832963SOliver Eikemeier f_depth(PLAN *plan, FTSENT *entry)
4959b50d902SRodney W. Grimes {
4961c832963SOliver Eikemeier 	if (plan->flags & F_DEPTH)
4971c832963SOliver Eikemeier 		COMPARE(entry->fts_level, plan->d_data);
4981c832963SOliver Eikemeier 	else
4991c832963SOliver Eikemeier 		return 1;
5001c832963SOliver Eikemeier }
5019b50d902SRodney W. Grimes 
5021c832963SOliver Eikemeier PLAN *
5031c832963SOliver Eikemeier c_depth(OPTION *option, char ***argvp)
5041c832963SOliver Eikemeier {
5051c832963SOliver Eikemeier 	PLAN *new;
5061c832963SOliver Eikemeier 	char *str;
5071c832963SOliver Eikemeier 
5081c832963SOliver Eikemeier 	new = palloc(option);
5091c832963SOliver Eikemeier 
5101c832963SOliver Eikemeier 	str = **argvp;
5111c832963SOliver Eikemeier 	if (str && !(new->flags & F_DEPTH)) {
5121c832963SOliver Eikemeier 		/* skip leading + or - */
5131c832963SOliver Eikemeier 		if (*str == '+' || *str == '-')
5141c832963SOliver Eikemeier 			str++;
5151c832963SOliver Eikemeier 		/* skip sign */
5161c832963SOliver Eikemeier 		if (*str == '+' || *str == '-')
5171c832963SOliver Eikemeier 			str++;
5181c832963SOliver Eikemeier 		if (isdigit(*str))
5191c832963SOliver Eikemeier 			new->flags |= F_DEPTH;
5201c832963SOliver Eikemeier 	}
5211c832963SOliver Eikemeier 
5221c832963SOliver Eikemeier 	if (new->flags & F_DEPTH) {	/* -depth n */
5231c832963SOliver Eikemeier 		char *ndepth;
5241c832963SOliver Eikemeier 
5251c832963SOliver Eikemeier 		ndepth = nextarg(option, argvp);
5261c832963SOliver Eikemeier 		new->d_data = find_parsenum(new, option->name, ndepth, NULL);
5271c832963SOliver Eikemeier 	} else {			/* -d */
5281c832963SOliver Eikemeier 		isdepth = 1;
5291c832963SOliver Eikemeier 	}
5301c832963SOliver Eikemeier 
5311c832963SOliver Eikemeier 	return new;
5329b50d902SRodney W. Grimes }
533127d7563SWarner Losh 
534127d7563SWarner Losh /*
535ed1a4621SPeter Wemm  * -empty functions --
536ed1a4621SPeter Wemm  *
537ed1a4621SPeter Wemm  *	True if the file or directory is empty
538ed1a4621SPeter Wemm  */
539ed1a4621SPeter Wemm int
540ef646f18SMark Murray f_empty(PLAN *plan __unused, FTSENT *entry)
541ed1a4621SPeter Wemm {
542ea92232aSPoul-Henning Kamp 	if (S_ISREG(entry->fts_statp->st_mode) &&
543ea92232aSPoul-Henning Kamp 	    entry->fts_statp->st_size == 0)
544ea92232aSPoul-Henning Kamp 		return 1;
545ed1a4621SPeter Wemm 	if (S_ISDIR(entry->fts_statp->st_mode)) {
546ed1a4621SPeter Wemm 		struct dirent *dp;
547ed1a4621SPeter Wemm 		int empty;
548ed1a4621SPeter Wemm 		DIR *dir;
549ed1a4621SPeter Wemm 
550ed1a4621SPeter Wemm 		empty = 1;
551ed1a4621SPeter Wemm 		dir = opendir(entry->fts_accpath);
552ed1a4621SPeter Wemm 		if (dir == NULL)
553ed1a4621SPeter Wemm 			err(1, "%s", entry->fts_accpath);
554ed1a4621SPeter Wemm 		for (dp = readdir(dir); dp; dp = readdir(dir))
555ed1a4621SPeter Wemm 			if (dp->d_name[0] != '.' ||
556ed1a4621SPeter Wemm 			    (dp->d_name[1] != '\0' &&
557ed1a4621SPeter Wemm 			     (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
558ed1a4621SPeter Wemm 				empty = 0;
559ed1a4621SPeter Wemm 				break;
560ed1a4621SPeter Wemm 			}
561ed1a4621SPeter Wemm 		closedir(dir);
562ea92232aSPoul-Henning Kamp 		return empty;
563ed1a4621SPeter Wemm 	}
564ea92232aSPoul-Henning Kamp 	return 0;
565ed1a4621SPeter Wemm }
566ed1a4621SPeter Wemm 
567ed1a4621SPeter Wemm PLAN *
568ef646f18SMark Murray c_empty(OPTION *option, char ***argvp __unused)
569ed1a4621SPeter Wemm {
570ed1a4621SPeter Wemm 	ftsoptions &= ~FTS_NOSTAT;
571ed1a4621SPeter Wemm 
572ea92232aSPoul-Henning Kamp 	return palloc(option);
573ed1a4621SPeter Wemm }
574ed1a4621SPeter Wemm 
575ed1a4621SPeter Wemm /*
576ea92232aSPoul-Henning Kamp  * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
577127d7563SWarner Losh  *
578127d7563SWarner Losh  *	True if the executed utility returns a zero value as exit status.
579127d7563SWarner Losh  *	The end of the primary expression is delimited by a semicolon.  If
580ea92232aSPoul-Henning Kamp  *	"{}" occurs anywhere, it gets replaced by the current pathname,
581ea92232aSPoul-Henning Kamp  *	or, in the case of -execdir, the current basename (filename
582ea92232aSPoul-Henning Kamp  *	without leading directory prefix). For -exec and -ok,
583ea92232aSPoul-Henning Kamp  *	the current directory for the execution of utility is the same as
584ea92232aSPoul-Henning Kamp  *	the current directory when the find utility was started, whereas
585ea92232aSPoul-Henning Kamp  *	for -execdir, it is the directory the file resides in.
586ea92232aSPoul-Henning Kamp  *
587ea92232aSPoul-Henning Kamp  *	The primary -ok differs from -exec in that it requests affirmation
588ea92232aSPoul-Henning Kamp  *	of the user before executing the utility.
589127d7563SWarner Losh  */
590127d7563SWarner Losh int
591ef646f18SMark Murray f_exec(PLAN *plan, FTSENT *entry)
592127d7563SWarner Losh {
593e98080b1SDavid Malone 	int cnt;
594127d7563SWarner Losh 	pid_t pid;
595127d7563SWarner Losh 	int status;
596127d7563SWarner Losh 	char *file;
597127d7563SWarner Losh 
5985e25d888STim J. Robbins 	if (entry == NULL && plan->flags & F_EXECPLUS) {
5995e25d888STim J. Robbins 		if (plan->e_ppos == plan->e_pbnum)
6005e25d888STim J. Robbins 			return (1);
6015e25d888STim J. Robbins 		plan->e_argv[plan->e_ppos] = NULL;
6025e25d888STim J. Robbins 		goto doexec;
6035e25d888STim J. Robbins 	}
6045e25d888STim J. Robbins 
605127d7563SWarner Losh 	/* XXX - if file/dir ends in '/' this will not work -- can it? */
606ea92232aSPoul-Henning Kamp 	if ((plan->flags & F_EXECDIR) && \
607ea92232aSPoul-Henning Kamp 	    (file = strrchr(entry->fts_path, '/')))
608127d7563SWarner Losh 		file++;
609127d7563SWarner Losh 	else
610127d7563SWarner Losh 		file = entry->fts_path;
611127d7563SWarner Losh 
6125e25d888STim J. Robbins 	if (plan->flags & F_EXECPLUS) {
6135e25d888STim J. Robbins 		if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
6145e25d888STim J. Robbins 			err(1, NULL);
6155e25d888STim J. Robbins 		plan->e_len[plan->e_ppos] = strlen(file);
6165e25d888STim J. Robbins 		plan->e_psize += plan->e_len[plan->e_ppos];
6175e25d888STim J. Robbins 		if (++plan->e_ppos < plan->e_pnummax &&
6185e25d888STim J. Robbins 		    plan->e_psize < plan->e_psizemax)
6195e25d888STim J. Robbins 			return (1);
6205e25d888STim J. Robbins 		plan->e_argv[plan->e_ppos] = NULL;
6215e25d888STim J. Robbins 	} else {
622127d7563SWarner Losh 		for (cnt = 0; plan->e_argv[cnt]; ++cnt)
623127d7563SWarner Losh 			if (plan->e_len[cnt])
6245e25d888STim J. Robbins 				brace_subst(plan->e_orig[cnt],
6255e25d888STim J. Robbins 				    &plan->e_argv[cnt], file,
6265e25d888STim J. Robbins 				    plan->e_len[cnt]);
6275e25d888STim J. Robbins 	}
628127d7563SWarner Losh 
6295e25d888STim J. Robbins doexec:	if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
630ea92232aSPoul-Henning Kamp 		return 0;
631ea92232aSPoul-Henning Kamp 
632ea92232aSPoul-Henning Kamp 	/* make sure find output is interspersed correctly with subprocesses */
633127d7563SWarner Losh 	fflush(stdout);
634127d7563SWarner Losh 	fflush(stderr);
635127d7563SWarner Losh 
6361fd98d7dSDag-Erling Smørgrav 	switch (pid = fork()) {
637127d7563SWarner Losh 	case -1:
638127d7563SWarner Losh 		err(1, "fork");
639127d7563SWarner Losh 		/* NOTREACHED */
640127d7563SWarner Losh 	case 0:
641ea92232aSPoul-Henning Kamp 		/* change dir back from where we started */
642ea92232aSPoul-Henning Kamp 		if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
643ea92232aSPoul-Henning Kamp 			warn("chdir");
644ea92232aSPoul-Henning Kamp 			_exit(1);
645ea92232aSPoul-Henning Kamp 		}
646127d7563SWarner Losh 		execvp(plan->e_argv[0], plan->e_argv);
647127d7563SWarner Losh 		warn("%s", plan->e_argv[0]);
648127d7563SWarner Losh 		_exit(1);
649127d7563SWarner Losh 	}
6505e25d888STim J. Robbins 	if (plan->flags & F_EXECPLUS) {
6515e25d888STim J. Robbins 		while (--plan->e_ppos >= plan->e_pbnum)
6525e25d888STim J. Robbins 			free(plan->e_argv[plan->e_ppos]);
6535e25d888STim J. Robbins 		plan->e_ppos = plan->e_pbnum;
6545e25d888STim J. Robbins 		plan->e_psize = plan->e_pbsize;
6555e25d888STim J. Robbins 	}
656127d7563SWarner Losh 	pid = waitpid(pid, &status, 0);
657127d7563SWarner Losh 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
658127d7563SWarner Losh }
659127d7563SWarner Losh 
660127d7563SWarner Losh /*
661ea92232aSPoul-Henning Kamp  * c_exec, c_execdir, c_ok --
662127d7563SWarner Losh  *	build three parallel arrays, one with pointers to the strings passed
663127d7563SWarner Losh  *	on the command line, one with (possibly duplicated) pointers to the
664127d7563SWarner Losh  *	argv array, and one with integer values that are lengths of the
665127d7563SWarner Losh  *	strings, but also flags meaning that the string has to be massaged.
666127d7563SWarner Losh  */
667127d7563SWarner Losh PLAN *
668ef646f18SMark Murray c_exec(OPTION *option, char ***argvp)
669127d7563SWarner Losh {
670127d7563SWarner Losh 	PLAN *new;			/* node returned */
6715e25d888STim J. Robbins 	long argmax;
6725e25d888STim J. Robbins 	int cnt, i;
673a07af811STim J. Robbins 	char **argv, **ap, **ep, *p;
674127d7563SWarner Losh 
675ea92232aSPoul-Henning Kamp 	/* XXX - was in c_execdir, but seems unnecessary!?
676127d7563SWarner Losh 	ftsoptions &= ~FTS_NOSTAT;
677ea92232aSPoul-Henning Kamp 	*/
678127d7563SWarner Losh 	isoutput = 1;
679127d7563SWarner Losh 
680ea92232aSPoul-Henning Kamp 	/* XXX - this is a change from the previous coding */
681ea92232aSPoul-Henning Kamp 	new = palloc(option);
682127d7563SWarner Losh 
683127d7563SWarner Losh 	for (ap = argv = *argvp;; ++ap) {
684127d7563SWarner Losh 		if (!*ap)
685127d7563SWarner Losh 			errx(1,
686e22bb9dbSTim J. Robbins 			    "%s: no terminating \";\" or \"+\"", option->name);
687127d7563SWarner Losh 		if (**ap == ';')
688127d7563SWarner Losh 			break;
6895e25d888STim J. Robbins 		if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
6905e25d888STim J. Robbins 			new->flags |= F_EXECPLUS;
6915e25d888STim J. Robbins 			break;
6925e25d888STim J. Robbins 		}
693127d7563SWarner Losh 	}
694127d7563SWarner Losh 
69551b0534fSJuli Mallett 	if (ap == argv)
69651b0534fSJuli Mallett 		errx(1, "%s: no command specified", option->name);
69751b0534fSJuli Mallett 
698127d7563SWarner Losh 	cnt = ap - *argvp + 1;
6995e25d888STim J. Robbins 	if (new->flags & F_EXECPLUS) {
7005e25d888STim J. Robbins 		new->e_ppos = new->e_pbnum = cnt - 2;
7015e25d888STim J. Robbins 		if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
7025e25d888STim J. Robbins 			warn("sysconf(_SC_ARG_MAX)");
7035e25d888STim J. Robbins 			argmax = _POSIX_ARG_MAX;
7045e25d888STim J. Robbins 		}
705a07af811STim J. Robbins 		argmax -= 1024;
706a07af811STim J. Robbins 		for (ep = environ; *ep != NULL; ep++)
707a07af811STim J. Robbins 			argmax -= strlen(*ep) + 1 + sizeof(*ep);
708a07af811STim J. Robbins 		argmax -= 1 + sizeof(*ep);
709a07af811STim J. Robbins 		new->e_pnummax = argmax / 16;
710a07af811STim J. Robbins 		argmax -= sizeof(char *) * new->e_pnummax;
711a07af811STim J. Robbins 		if (argmax <= 0)
712a07af811STim J. Robbins 			errx(1, "no space for arguments");
713a07af811STim J. Robbins 		new->e_psizemax = argmax;
7145e25d888STim J. Robbins 		new->e_pbsize = 0;
7155e25d888STim J. Robbins 		cnt += new->e_pnummax + 1;
71622170420SKirill Ponomarev 		new->e_next = lastexecplus;
71722170420SKirill Ponomarev 		lastexecplus = new;
7185e25d888STim J. Robbins 	}
71947bca8b0SJuli Mallett 	if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
72047bca8b0SJuli Mallett 		err(1, NULL);
72147bca8b0SJuli Mallett 	if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
72247bca8b0SJuli Mallett 		err(1, NULL);
72347bca8b0SJuli Mallett 	if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
72447bca8b0SJuli Mallett 		err(1, NULL);
725127d7563SWarner Losh 
726127d7563SWarner Losh 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
727127d7563SWarner Losh 		new->e_orig[cnt] = *argv;
7285e25d888STim J. Robbins 		if (new->flags & F_EXECPLUS)
7295e25d888STim J. Robbins 			new->e_pbsize += strlen(*argv) + 1;
730127d7563SWarner Losh 		for (p = *argv; *p; ++p)
7315e25d888STim J. Robbins 			if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
7325e25d888STim J. Robbins 			    p[1] == '}') {
733f0cb9537SDavid E. O'Brien 				if ((new->e_argv[cnt] =
73447bca8b0SJuli Mallett 				    malloc(MAXPATHLEN)) == NULL)
73547bca8b0SJuli Mallett 					err(1, NULL);
736127d7563SWarner Losh 				new->e_len[cnt] = MAXPATHLEN;
737127d7563SWarner Losh 				break;
738127d7563SWarner Losh 			}
739127d7563SWarner Losh 		if (!*p) {
740127d7563SWarner Losh 			new->e_argv[cnt] = *argv;
741127d7563SWarner Losh 			new->e_len[cnt] = 0;
742127d7563SWarner Losh 		}
743127d7563SWarner Losh 	}
7445e25d888STim J. Robbins 	if (new->flags & F_EXECPLUS) {
7455e25d888STim J. Robbins 		new->e_psize = new->e_pbsize;
7465e25d888STim J. Robbins 		cnt--;
7475e25d888STim J. Robbins 		for (i = 0; i < new->e_pnummax; i++) {
7485e25d888STim J. Robbins 			new->e_argv[cnt] = NULL;
7495e25d888STim J. Robbins 			new->e_len[cnt] = 0;
7505e25d888STim J. Robbins 			cnt++;
7515e25d888STim J. Robbins 		}
7525e25d888STim J. Robbins 		argv = ap;
7535e25d888STim J. Robbins 		goto done;
7545e25d888STim J. Robbins 	}
755127d7563SWarner Losh 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
756127d7563SWarner Losh 
7575e25d888STim J. Robbins done:	*argvp = argv + 1;
758ea92232aSPoul-Henning Kamp 	return new;
759ea92232aSPoul-Henning Kamp }
760ea92232aSPoul-Henning Kamp 
76122170420SKirill Ponomarev /* Finish any pending -exec ... {} + functions. */
76222170420SKirill Ponomarev void
76322170420SKirill Ponomarev finish_execplus()
76422170420SKirill Ponomarev {
76522170420SKirill Ponomarev 	PLAN *p;
76622170420SKirill Ponomarev 
76722170420SKirill Ponomarev 	p = lastexecplus;
76822170420SKirill Ponomarev 	while (p != NULL) {
76922170420SKirill Ponomarev 		(p->execute)(p, NULL);
77022170420SKirill Ponomarev 		p = p->e_next;
77122170420SKirill Ponomarev 	}
77222170420SKirill Ponomarev }
77322170420SKirill Ponomarev 
774ea92232aSPoul-Henning Kamp int
775ef646f18SMark Murray f_flags(PLAN *plan, FTSENT *entry)
776ea92232aSPoul-Henning Kamp {
777ea92232aSPoul-Henning Kamp 	u_long flags;
778ea92232aSPoul-Henning Kamp 
7797fd5ee41SRuslan Ermilov 	flags = entry->fts_statp->st_flags;
780ea92232aSPoul-Henning Kamp 	if (plan->flags & F_ATLEAST)
7817fd5ee41SRuslan Ermilov 		return (flags | plan->fl_flags) == flags &&
7827fd5ee41SRuslan Ermilov 		    !(flags & plan->fl_notflags);
7837fd5ee41SRuslan Ermilov 	else if (plan->flags & F_ANY)
7847fd5ee41SRuslan Ermilov 		return (flags & plan->fl_flags) ||
7857fd5ee41SRuslan Ermilov 		    (flags | plan->fl_notflags) != flags;
786ea92232aSPoul-Henning Kamp 	else
7877fd5ee41SRuslan Ermilov 		return flags == plan->fl_flags &&
7887fd5ee41SRuslan Ermilov 		    !(plan->fl_flags & plan->fl_notflags);
789ea92232aSPoul-Henning Kamp }
790ea92232aSPoul-Henning Kamp 
791ea92232aSPoul-Henning Kamp PLAN *
792ef646f18SMark Murray c_flags(OPTION *option, char ***argvp)
793ea92232aSPoul-Henning Kamp {
794ea92232aSPoul-Henning Kamp 	char *flags_str;
795ea92232aSPoul-Henning Kamp 	PLAN *new;
796ea92232aSPoul-Henning Kamp 	u_long flags, notflags;
797ea92232aSPoul-Henning Kamp 
798ea92232aSPoul-Henning Kamp 	flags_str = nextarg(option, argvp);
799ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
800ea92232aSPoul-Henning Kamp 
801ea92232aSPoul-Henning Kamp 	new = palloc(option);
802ea92232aSPoul-Henning Kamp 
803ea92232aSPoul-Henning Kamp 	if (*flags_str == '-') {
804ea92232aSPoul-Henning Kamp 		new->flags |= F_ATLEAST;
805ea92232aSPoul-Henning Kamp 		flags_str++;
8067fd5ee41SRuslan Ermilov 	} else if (*flags_str == '+') {
8077fd5ee41SRuslan Ermilov 		new->flags |= F_ANY;
8087fd5ee41SRuslan Ermilov 		flags_str++;
809ea92232aSPoul-Henning Kamp 	}
810ea92232aSPoul-Henning Kamp 	if (strtofflags(&flags_str, &flags, &notflags) == 1)
811ea92232aSPoul-Henning Kamp 		errx(1, "%s: %s: illegal flags string", option->name, flags_str);
812ea92232aSPoul-Henning Kamp 
813ea92232aSPoul-Henning Kamp 	new->fl_flags = flags;
8147fd5ee41SRuslan Ermilov 	new->fl_notflags = notflags;
815ea92232aSPoul-Henning Kamp 	return new;
816127d7563SWarner Losh }
8179b50d902SRodney W. Grimes 
8189b50d902SRodney W. Grimes /*
8199b50d902SRodney W. Grimes  * -follow functions --
8209b50d902SRodney W. Grimes  *
8219b50d902SRodney W. Grimes  *	Always true, causes symbolic links to be followed on a global
8229b50d902SRodney W. Grimes  *	basis.
8239b50d902SRodney W. Grimes  */
8249b50d902SRodney W. Grimes PLAN *
825ef646f18SMark Murray c_follow(OPTION *option, char ***argvp __unused)
8269b50d902SRodney W. Grimes {
8279b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_PHYSICAL;
8289b50d902SRodney W. Grimes 	ftsoptions |= FTS_LOGICAL;
8299b50d902SRodney W. Grimes 
830ea92232aSPoul-Henning Kamp 	return palloc(option);
8319b50d902SRodney W. Grimes }
8329b50d902SRodney W. Grimes 
8339b50d902SRodney W. Grimes /*
8349b50d902SRodney W. Grimes  * -fstype functions --
8359b50d902SRodney W. Grimes  *
8369b50d902SRodney W. Grimes  *	True if the file is of a certain type.
8379b50d902SRodney W. Grimes  */
8389b50d902SRodney W. Grimes int
839ef646f18SMark Murray f_fstype(PLAN *plan, FTSENT *entry)
8409b50d902SRodney W. Grimes {
8419b50d902SRodney W. Grimes 	static dev_t curdev;	/* need a guaranteed illegal dev value */
8429b50d902SRodney W. Grimes 	static int first = 1;
8439b50d902SRodney W. Grimes 	struct statfs sb;
8449d08e419SPeter Wemm 	static int val_type, val_flags;
8458a0a76b8SOllivier Robert 	char *p, save[2] = {0,0};
8469b50d902SRodney W. Grimes 
847ea92232aSPoul-Henning Kamp 	if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
848ea92232aSPoul-Henning Kamp 		return 0;
849ea92232aSPoul-Henning Kamp 
8509b50d902SRodney W. Grimes 	/* Only check when we cross mount point. */
8519b50d902SRodney W. Grimes 	if (first || curdev != entry->fts_statp->st_dev) {
8529b50d902SRodney W. Grimes 		curdev = entry->fts_statp->st_dev;
8539b50d902SRodney W. Grimes 
8549b50d902SRodney W. Grimes 		/*
8559b50d902SRodney W. Grimes 		 * Statfs follows symlinks; find wants the link's filesystem,
8569b50d902SRodney W. Grimes 		 * not where it points.
8579b50d902SRodney W. Grimes 		 */
8589b50d902SRodney W. Grimes 		if (entry->fts_info == FTS_SL ||
8599b50d902SRodney W. Grimes 		    entry->fts_info == FTS_SLNONE) {
8609b50d902SRodney W. Grimes 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
8619b50d902SRodney W. Grimes 				++p;
8629b50d902SRodney W. Grimes 			else
8639b50d902SRodney W. Grimes 				p = entry->fts_accpath;
8649b50d902SRodney W. Grimes 			save[0] = p[0];
8659b50d902SRodney W. Grimes 			p[0] = '.';
8669b50d902SRodney W. Grimes 			save[1] = p[1];
8679b50d902SRodney W. Grimes 			p[1] = '\0';
8689b50d902SRodney W. Grimes 		} else
8699b50d902SRodney W. Grimes 			p = NULL;
8709b50d902SRodney W. Grimes 
8719b50d902SRodney W. Grimes 		if (statfs(entry->fts_accpath, &sb))
8729b50d902SRodney W. Grimes 			err(1, "%s", entry->fts_accpath);
8739b50d902SRodney W. Grimes 
8749b50d902SRodney W. Grimes 		if (p) {
8759b50d902SRodney W. Grimes 			p[0] = save[0];
8769b50d902SRodney W. Grimes 			p[1] = save[1];
8779b50d902SRodney W. Grimes 		}
8789b50d902SRodney W. Grimes 
8799b50d902SRodney W. Grimes 		first = 0;
880841484cdSPeter Wemm 
881841484cdSPeter Wemm 		/*
882841484cdSPeter Wemm 		 * Further tests may need both of these values, so
883841484cdSPeter Wemm 		 * always copy both of them.
884841484cdSPeter Wemm 		 */
8859d08e419SPeter Wemm 		val_flags = sb.f_flags;
8869d08e419SPeter Wemm 		val_type = sb.f_type;
8879b50d902SRodney W. Grimes 	}
888ea92232aSPoul-Henning Kamp 	switch (plan->flags & F_MTMASK) {
8899b50d902SRodney W. Grimes 	case F_MTFLAG:
890ea92232aSPoul-Henning Kamp 		return val_flags & plan->mt_data;
8919b50d902SRodney W. Grimes 	case F_MTTYPE:
892ea92232aSPoul-Henning Kamp 		return val_type == plan->mt_data;
8939b50d902SRodney W. Grimes 	default:
8949b50d902SRodney W. Grimes 		abort();
8959b50d902SRodney W. Grimes 	}
8969b50d902SRodney W. Grimes }
8979b50d902SRodney W. Grimes 
8989b50d902SRodney W. Grimes PLAN *
899ef646f18SMark Murray c_fstype(OPTION *option, char ***argvp)
9009b50d902SRodney W. Grimes {
901ea92232aSPoul-Henning Kamp 	char *fsname;
902e98080b1SDavid Malone 	PLAN *new;
9035965373eSMaxime Henrion 	struct xvfsconf vfc;
9049b50d902SRodney W. Grimes 
905ea92232aSPoul-Henning Kamp 	fsname = nextarg(option, argvp);
9069b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
9079b50d902SRodney W. Grimes 
908ea92232aSPoul-Henning Kamp 	new = palloc(option);
909841484cdSPeter Wemm 
910841484cdSPeter Wemm 	/*
911841484cdSPeter Wemm 	 * Check first for a filesystem name.
912841484cdSPeter Wemm 	 */
913ea92232aSPoul-Henning Kamp 	if (getvfsbyname(fsname, &vfc) == 0) {
914ea92232aSPoul-Henning Kamp 		new->flags |= F_MTTYPE;
915841484cdSPeter Wemm 		new->mt_data = vfc.vfc_typenum;
916ea92232aSPoul-Henning Kamp 		return new;
917841484cdSPeter Wemm 	}
918841484cdSPeter Wemm 
919ea92232aSPoul-Henning Kamp 	switch (*fsname) {
9209b50d902SRodney W. Grimes 	case 'l':
921ea92232aSPoul-Henning Kamp 		if (!strcmp(fsname, "local")) {
922ea92232aSPoul-Henning Kamp 			new->flags |= F_MTFLAG;
9239b50d902SRodney W. Grimes 			new->mt_data = MNT_LOCAL;
924ea92232aSPoul-Henning Kamp 			return new;
9259b50d902SRodney W. Grimes 		}
9269b50d902SRodney W. Grimes 		break;
9279b50d902SRodney W. Grimes 	case 'r':
928ea92232aSPoul-Henning Kamp 		if (!strcmp(fsname, "rdonly")) {
929ea92232aSPoul-Henning Kamp 			new->flags |= F_MTFLAG;
9309b50d902SRodney W. Grimes 			new->mt_data = MNT_RDONLY;
931ea92232aSPoul-Henning Kamp 			return new;
9329b50d902SRodney W. Grimes 		}
9339b50d902SRodney W. Grimes 		break;
9349b50d902SRodney W. Grimes 	}
935ea92232aSPoul-Henning Kamp 
9361e2f8412SEivind Eklund 	/*
9371e2f8412SEivind Eklund 	 * We need to make filesystem checks for filesystems
9381e2f8412SEivind Eklund 	 * that exists but aren't in the kernel work.
9391e2f8412SEivind Eklund 	 */
940ea92232aSPoul-Henning Kamp 	fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname);
941ea92232aSPoul-Henning Kamp 	new->flags |= F_MTUNKNOWN;
942ea92232aSPoul-Henning Kamp 	return new;
9439b50d902SRodney W. Grimes }
9449b50d902SRodney W. Grimes 
9459b50d902SRodney W. Grimes /*
9469b50d902SRodney W. Grimes  * -group gname functions --
9479b50d902SRodney W. Grimes  *
9489b50d902SRodney W. Grimes  *	True if the file belongs to the group gname.  If gname is numeric and
9499b50d902SRodney W. Grimes  *	an equivalent of the getgrnam() function does not return a valid group
9509b50d902SRodney W. Grimes  *	name, gname is taken as a group ID.
9519b50d902SRodney W. Grimes  */
9529b50d902SRodney W. Grimes int
953ef646f18SMark Murray f_group(PLAN *plan, FTSENT *entry)
9549b50d902SRodney W. Grimes {
9554ba3b38bSKirill Ponomarev 	COMPARE(entry->fts_statp->st_gid, plan->g_data);
9569b50d902SRodney W. Grimes }
9579b50d902SRodney W. Grimes 
9589b50d902SRodney W. Grimes PLAN *
959ef646f18SMark Murray c_group(OPTION *option, char ***argvp)
9609b50d902SRodney W. Grimes {
961ea92232aSPoul-Henning Kamp 	char *gname;
9629b50d902SRodney W. Grimes 	PLAN *new;
9639b50d902SRodney W. Grimes 	struct group *g;
9649b50d902SRodney W. Grimes 	gid_t gid;
9659b50d902SRodney W. Grimes 
966ea92232aSPoul-Henning Kamp 	gname = nextarg(option, argvp);
9679b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
9689b50d902SRodney W. Grimes 
9694ba3b38bSKirill Ponomarev 	new = palloc(option);
9709b50d902SRodney W. Grimes 	g = getgrnam(gname);
9719b50d902SRodney W. Grimes 	if (g == NULL) {
9724ba3b38bSKirill Ponomarev 		char* cp = gname;
9734ba3b38bSKirill Ponomarev 		if (gname[0] == '-' || gname[0] == '+')
9744ba3b38bSKirill Ponomarev 			gname++;
9759b50d902SRodney W. Grimes 		gid = atoi(gname);
9769b50d902SRodney W. Grimes 		if (gid == 0 && gname[0] != '0')
977ea92232aSPoul-Henning Kamp 			errx(1, "%s: %s: no such group", option->name, gname);
9784ba3b38bSKirill Ponomarev 		gid = find_parsenum(new, option->name, cp, NULL);
9799b50d902SRodney W. Grimes 	} else
9809b50d902SRodney W. Grimes 		gid = g->gr_gid;
9819b50d902SRodney W. Grimes 
9829b50d902SRodney W. Grimes 	new->g_data = gid;
983ea92232aSPoul-Henning Kamp 	return new;
9849b50d902SRodney W. Grimes }
9859b50d902SRodney W. Grimes 
9869b50d902SRodney W. Grimes /*
9879b50d902SRodney W. Grimes  * -inum n functions --
9889b50d902SRodney W. Grimes  *
9899b50d902SRodney W. Grimes  *	True if the file has inode # n.
9909b50d902SRodney W. Grimes  */
9919b50d902SRodney W. Grimes int
992ef646f18SMark Murray f_inum(PLAN *plan, FTSENT *entry)
9939b50d902SRodney W. Grimes {
9949b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
9959b50d902SRodney W. Grimes }
9969b50d902SRodney W. Grimes 
9979b50d902SRodney W. Grimes PLAN *
998ef646f18SMark Murray c_inum(OPTION *option, char ***argvp)
9999b50d902SRodney W. Grimes {
1000ea92232aSPoul-Henning Kamp 	char *inum_str;
10019b50d902SRodney W. Grimes 	PLAN *new;
10029b50d902SRodney W. Grimes 
1003ea92232aSPoul-Henning Kamp 	inum_str = nextarg(option, argvp);
10049b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
10059b50d902SRodney W. Grimes 
1006ea92232aSPoul-Henning Kamp 	new = palloc(option);
1007ea92232aSPoul-Henning Kamp 	new->i_data = find_parsenum(new, option->name, inum_str, NULL);
1008ea92232aSPoul-Henning Kamp 	return new;
10099b50d902SRodney W. Grimes }
10109b50d902SRodney W. Grimes 
10119b50d902SRodney W. Grimes /*
101246b993ffSWarner Losh  * -samefile FN
101346b993ffSWarner Losh  *
101446b993ffSWarner Losh  *	True if the file has the same inode (eg hard link) FN
101546b993ffSWarner Losh  */
101646b993ffSWarner Losh 
101746b993ffSWarner Losh /* f_samefile is just f_inum */
101846b993ffSWarner Losh PLAN *
101946b993ffSWarner Losh c_samefile(OPTION *option, char ***argvp)
102046b993ffSWarner Losh {
102146b993ffSWarner Losh 	char *fn;
102246b993ffSWarner Losh 	PLAN *new;
102346b993ffSWarner Losh 	struct stat sb;
102446b993ffSWarner Losh 
102546b993ffSWarner Losh 	fn = nextarg(option, argvp);
102646b993ffSWarner Losh 	ftsoptions &= ~FTS_NOSTAT;
102746b993ffSWarner Losh 
102846b993ffSWarner Losh 	new = palloc(option);
102946b993ffSWarner Losh 	if (stat(fn, &sb))
103046b993ffSWarner Losh 		err(1, "%s", fn);
103146b993ffSWarner Losh 	new->i_data = sb.st_ino;
103246b993ffSWarner Losh 	return new;
103346b993ffSWarner Losh }
103446b993ffSWarner Losh 
103546b993ffSWarner Losh /*
10369b50d902SRodney W. Grimes  * -links n functions --
10379b50d902SRodney W. Grimes  *
10389b50d902SRodney W. Grimes  *	True if the file has n links.
10399b50d902SRodney W. Grimes  */
10409b50d902SRodney W. Grimes int
1041ef646f18SMark Murray f_links(PLAN *plan, FTSENT *entry)
10429b50d902SRodney W. Grimes {
10439b50d902SRodney W. Grimes 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
10449b50d902SRodney W. Grimes }
10459b50d902SRodney W. Grimes 
10469b50d902SRodney W. Grimes PLAN *
1047ef646f18SMark Murray c_links(OPTION *option, char ***argvp)
10489b50d902SRodney W. Grimes {
1049ea92232aSPoul-Henning Kamp 	char *nlinks;
10509b50d902SRodney W. Grimes 	PLAN *new;
10519b50d902SRodney W. Grimes 
1052ea92232aSPoul-Henning Kamp 	nlinks = nextarg(option, argvp);
10539b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
10549b50d902SRodney W. Grimes 
1055ea92232aSPoul-Henning Kamp 	new = palloc(option);
1056ea92232aSPoul-Henning Kamp 	new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1057ea92232aSPoul-Henning Kamp 	return new;
10589b50d902SRodney W. Grimes }
10599b50d902SRodney W. Grimes 
10609b50d902SRodney W. Grimes /*
10619b50d902SRodney W. Grimes  * -ls functions --
10629b50d902SRodney W. Grimes  *
10639b50d902SRodney W. Grimes  *	Always true - prints the current entry to stdout in "ls" format.
10649b50d902SRodney W. Grimes  */
10659b50d902SRodney W. Grimes int
1066ef646f18SMark Murray f_ls(PLAN *plan __unused, FTSENT *entry)
10679b50d902SRodney W. Grimes {
10689b50d902SRodney W. Grimes 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1069ea92232aSPoul-Henning Kamp 	return 1;
10709b50d902SRodney W. Grimes }
10719b50d902SRodney W. Grimes 
10729b50d902SRodney W. Grimes PLAN *
1073ef646f18SMark Murray c_ls(OPTION *option, char ***argvp __unused)
10749b50d902SRodney W. Grimes {
10759b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
10769b50d902SRodney W. Grimes 	isoutput = 1;
10779b50d902SRodney W. Grimes 
1078ea92232aSPoul-Henning Kamp 	return palloc(option);
10799b50d902SRodney W. Grimes }
10809b50d902SRodney W. Grimes 
10819b50d902SRodney W. Grimes /*
10829b50d902SRodney W. Grimes  * -name functions --
10839b50d902SRodney W. Grimes  *
10849b50d902SRodney W. Grimes  *	True if the basename of the filename being examined
10859b50d902SRodney W. Grimes  *	matches pattern using Pattern Matching Notation S3.14
10869b50d902SRodney W. Grimes  */
10879b50d902SRodney W. Grimes int
1088ef646f18SMark Murray f_name(PLAN *plan, FTSENT *entry)
10899b50d902SRodney W. Grimes {
1090acebb585SWarner Losh 	char fn[PATH_MAX];
1091acebb585SWarner Losh 	const char *name;
1092acebb585SWarner Losh 
1093acebb585SWarner Losh 	if (plan->flags & F_LINK) {
1094acebb585SWarner Losh 		name = fn;
1095acebb585SWarner Losh 		if (readlink(entry->fts_path, fn, sizeof(fn)) == -1)
109646b993ffSWarner Losh 			return 0;
1097acebb585SWarner Losh 	} else
1098acebb585SWarner Losh 		name = entry->fts_name;
1099acebb585SWarner Losh 	return !fnmatch(plan->c_data, name,
1100ea92232aSPoul-Henning Kamp 	    plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
11019b50d902SRodney W. Grimes }
11029b50d902SRodney W. Grimes 
11039b50d902SRodney W. Grimes PLAN *
1104ef646f18SMark Murray c_name(OPTION *option, char ***argvp)
11059b50d902SRodney W. Grimes {
1106ea92232aSPoul-Henning Kamp 	char *pattern;
11079b50d902SRodney W. Grimes 	PLAN *new;
11089b50d902SRodney W. Grimes 
1109ea92232aSPoul-Henning Kamp 	pattern = nextarg(option, argvp);
1110ea92232aSPoul-Henning Kamp 	new = palloc(option);
11119b50d902SRodney W. Grimes 	new->c_data = pattern;
1112ea92232aSPoul-Henning Kamp 	return new;
11139b50d902SRodney W. Grimes }
11149b50d902SRodney W. Grimes 
11157c1d4b3aSAkinori MUSHA /*
1116ea92232aSPoul-Henning Kamp  * -newer file functions --
11177c1d4b3aSAkinori MUSHA  *
1118ea92232aSPoul-Henning Kamp  *	True if the current file has been modified more recently
1119ea92232aSPoul-Henning Kamp  *	then the modification time of the file named by the pathname
1120ea92232aSPoul-Henning Kamp  *	file.
11217c1d4b3aSAkinori MUSHA  */
11227c1d4b3aSAkinori MUSHA int
1123ef646f18SMark Murray f_newer(PLAN *plan, FTSENT *entry)
11247c1d4b3aSAkinori MUSHA {
1125ea92232aSPoul-Henning Kamp 	if (plan->flags & F_TIME_C)
1126ea92232aSPoul-Henning Kamp 		return entry->fts_statp->st_ctime > plan->t_data;
1127ea92232aSPoul-Henning Kamp 	else if (plan->flags & F_TIME_A)
1128ea92232aSPoul-Henning Kamp 		return entry->fts_statp->st_atime > plan->t_data;
112931d53425SCeri Davies 	else if (plan->flags & F_TIME_B)
113031d53425SCeri Davies 		return entry->fts_statp->st_birthtime > plan->t_data;
1131ea92232aSPoul-Henning Kamp 	else
1132ea92232aSPoul-Henning Kamp 		return entry->fts_statp->st_mtime > plan->t_data;
11337c1d4b3aSAkinori MUSHA }
11347c1d4b3aSAkinori MUSHA 
11357c1d4b3aSAkinori MUSHA PLAN *
1136ef646f18SMark Murray c_newer(OPTION *option, char ***argvp)
11377c1d4b3aSAkinori MUSHA {
1138ea92232aSPoul-Henning Kamp 	char *fn_or_tspec;
11397c1d4b3aSAkinori MUSHA 	PLAN *new;
1140ea92232aSPoul-Henning Kamp 	struct stat sb;
11417c1d4b3aSAkinori MUSHA 
1142ea92232aSPoul-Henning Kamp 	fn_or_tspec = nextarg(option, argvp);
1143ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
1144ea92232aSPoul-Henning Kamp 
1145ea92232aSPoul-Henning Kamp 	new = palloc(option);
1146ea92232aSPoul-Henning Kamp 	/* compare against what */
1147ea92232aSPoul-Henning Kamp 	if (option->flags & F_TIME2_T) {
114848d09ba6SMark Murray 		new->t_data = get_date(fn_or_tspec, (struct timeb *) 0);
1149ea92232aSPoul-Henning Kamp 		if (new->t_data == (time_t) -1)
1150ea92232aSPoul-Henning Kamp 			errx(1, "Can't parse date/time: %s", fn_or_tspec);
1151ea92232aSPoul-Henning Kamp 	} else {
1152ea92232aSPoul-Henning Kamp 		if (stat(fn_or_tspec, &sb))
1153ea92232aSPoul-Henning Kamp 			err(1, "%s", fn_or_tspec);
1154ea92232aSPoul-Henning Kamp 		if (option->flags & F_TIME2_C)
1155ea92232aSPoul-Henning Kamp 			new->t_data = sb.st_ctime;
1156ea92232aSPoul-Henning Kamp 		else if (option->flags & F_TIME2_A)
1157ea92232aSPoul-Henning Kamp 			new->t_data = sb.st_atime;
1158ea92232aSPoul-Henning Kamp 		else
1159ea92232aSPoul-Henning Kamp 			new->t_data = sb.st_mtime;
1160ea92232aSPoul-Henning Kamp 	}
1161ea92232aSPoul-Henning Kamp 	return new;
11627c1d4b3aSAkinori MUSHA }
11637c1d4b3aSAkinori MUSHA 
1164ea92232aSPoul-Henning Kamp /*
1165ea92232aSPoul-Henning Kamp  * -nogroup functions --
1166ea92232aSPoul-Henning Kamp  *
1167ea92232aSPoul-Henning Kamp  *	True if file belongs to a user ID for which the equivalent
1168ea92232aSPoul-Henning Kamp  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1169ea92232aSPoul-Henning Kamp  */
1170ea92232aSPoul-Henning Kamp int
1171ef646f18SMark Murray f_nogroup(PLAN *plan __unused, FTSENT *entry)
1172ea92232aSPoul-Henning Kamp {
1173ea92232aSPoul-Henning Kamp 	return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1174ea92232aSPoul-Henning Kamp }
1175ea92232aSPoul-Henning Kamp 
1176ea92232aSPoul-Henning Kamp PLAN *
1177ef646f18SMark Murray c_nogroup(OPTION *option, char ***argvp __unused)
1178ea92232aSPoul-Henning Kamp {
1179ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
1180ea92232aSPoul-Henning Kamp 
1181ea92232aSPoul-Henning Kamp 	return palloc(option);
1182ea92232aSPoul-Henning Kamp }
1183ea92232aSPoul-Henning Kamp 
1184ea92232aSPoul-Henning Kamp /*
1185ea92232aSPoul-Henning Kamp  * -nouser functions --
1186ea92232aSPoul-Henning Kamp  *
1187ea92232aSPoul-Henning Kamp  *	True if file belongs to a user ID for which the equivalent
1188ea92232aSPoul-Henning Kamp  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1189ea92232aSPoul-Henning Kamp  */
1190ea92232aSPoul-Henning Kamp int
1191ef646f18SMark Murray f_nouser(PLAN *plan __unused, FTSENT *entry)
1192ea92232aSPoul-Henning Kamp {
1193ea92232aSPoul-Henning Kamp 	return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1194ea92232aSPoul-Henning Kamp }
1195ea92232aSPoul-Henning Kamp 
1196ea92232aSPoul-Henning Kamp PLAN *
1197ef646f18SMark Murray c_nouser(OPTION *option, char ***argvp __unused)
1198ea92232aSPoul-Henning Kamp {
1199ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
1200ea92232aSPoul-Henning Kamp 
1201ea92232aSPoul-Henning Kamp 	return palloc(option);
1202ea92232aSPoul-Henning Kamp }
1203ea92232aSPoul-Henning Kamp 
1204ea92232aSPoul-Henning Kamp /*
1205ea92232aSPoul-Henning Kamp  * -path functions --
1206ea92232aSPoul-Henning Kamp  *
1207ea92232aSPoul-Henning Kamp  *	True if the path of the filename being examined
1208ea92232aSPoul-Henning Kamp  *	matches pattern using Pattern Matching Notation S3.14
1209ea92232aSPoul-Henning Kamp  */
1210ea92232aSPoul-Henning Kamp int
1211ef646f18SMark Murray f_path(PLAN *plan, FTSENT *entry)
1212ea92232aSPoul-Henning Kamp {
1213ea92232aSPoul-Henning Kamp 	return !fnmatch(plan->c_data, entry->fts_path,
1214ea92232aSPoul-Henning Kamp 	    plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1215ea92232aSPoul-Henning Kamp }
1216ea92232aSPoul-Henning Kamp 
1217ea92232aSPoul-Henning Kamp /* c_path is the same as c_name */
1218ea92232aSPoul-Henning Kamp 
1219ea92232aSPoul-Henning Kamp /*
1220ea92232aSPoul-Henning Kamp  * -perm functions --
1221ea92232aSPoul-Henning Kamp  *
1222ea92232aSPoul-Henning Kamp  *	The mode argument is used to represent file mode bits.  If it starts
1223ea92232aSPoul-Henning Kamp  *	with a leading digit, it's treated as an octal mode, otherwise as a
1224ea92232aSPoul-Henning Kamp  *	symbolic mode.
1225ea92232aSPoul-Henning Kamp  */
1226ea92232aSPoul-Henning Kamp int
1227ef646f18SMark Murray f_perm(PLAN *plan, FTSENT *entry)
1228ea92232aSPoul-Henning Kamp {
1229ea92232aSPoul-Henning Kamp 	mode_t mode;
1230ea92232aSPoul-Henning Kamp 
1231ea92232aSPoul-Henning Kamp 	mode = entry->fts_statp->st_mode &
1232ea92232aSPoul-Henning Kamp 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1233ea92232aSPoul-Henning Kamp 	if (plan->flags & F_ATLEAST)
1234ea92232aSPoul-Henning Kamp 		return (plan->m_data | mode) == mode;
1235c0ff9709SRuslan Ermilov 	else if (plan->flags & F_ANY)
1236c0ff9709SRuslan Ermilov 		return (mode & plan->m_data);
1237ea92232aSPoul-Henning Kamp 	else
1238ea92232aSPoul-Henning Kamp 		return mode == plan->m_data;
1239ea92232aSPoul-Henning Kamp 	/* NOTREACHED */
1240ea92232aSPoul-Henning Kamp }
1241ea92232aSPoul-Henning Kamp 
1242ea92232aSPoul-Henning Kamp PLAN *
1243ef646f18SMark Murray c_perm(OPTION *option, char ***argvp)
1244ea92232aSPoul-Henning Kamp {
1245ea92232aSPoul-Henning Kamp 	char *perm;
1246ea92232aSPoul-Henning Kamp 	PLAN *new;
1247ea92232aSPoul-Henning Kamp 	mode_t *set;
1248ea92232aSPoul-Henning Kamp 
1249ea92232aSPoul-Henning Kamp 	perm = nextarg(option, argvp);
1250ea92232aSPoul-Henning Kamp 	ftsoptions &= ~FTS_NOSTAT;
1251ea92232aSPoul-Henning Kamp 
1252ea92232aSPoul-Henning Kamp 	new = palloc(option);
1253ea92232aSPoul-Henning Kamp 
1254ea92232aSPoul-Henning Kamp 	if (*perm == '-') {
1255ea92232aSPoul-Henning Kamp 		new->flags |= F_ATLEAST;
1256ea92232aSPoul-Henning Kamp 		++perm;
1257ea92232aSPoul-Henning Kamp 	} else if (*perm == '+') {
1258ea92232aSPoul-Henning Kamp 		new->flags |= F_ANY;
1259ea92232aSPoul-Henning Kamp 		++perm;
1260ea92232aSPoul-Henning Kamp 	}
1261ea92232aSPoul-Henning Kamp 
1262ea92232aSPoul-Henning Kamp 	if ((set = setmode(perm)) == NULL)
1263ea92232aSPoul-Henning Kamp 		errx(1, "%s: %s: illegal mode string", option->name, perm);
1264ea92232aSPoul-Henning Kamp 
1265ea92232aSPoul-Henning Kamp 	new->m_data = getmode(set, 0);
1266ea92232aSPoul-Henning Kamp 	free(set);
1267ea92232aSPoul-Henning Kamp 	return new;
1268ea92232aSPoul-Henning Kamp }
1269ea92232aSPoul-Henning Kamp 
1270ea92232aSPoul-Henning Kamp /*
1271ea92232aSPoul-Henning Kamp  * -print functions --
1272ea92232aSPoul-Henning Kamp  *
12739725a7b9SPhilippe Charnier  *	Always true, causes the current pathname to be written to
1274ea92232aSPoul-Henning Kamp  *	standard output.
1275ea92232aSPoul-Henning Kamp  */
1276ea92232aSPoul-Henning Kamp int
1277ef646f18SMark Murray f_print(PLAN *plan __unused, FTSENT *entry)
1278ea92232aSPoul-Henning Kamp {
1279ea92232aSPoul-Henning Kamp 	(void)puts(entry->fts_path);
1280ea92232aSPoul-Henning Kamp 	return 1;
1281ea92232aSPoul-Henning Kamp }
1282ea92232aSPoul-Henning Kamp 
1283ea92232aSPoul-Henning Kamp PLAN *
1284ef646f18SMark Murray c_print(OPTION *option, char ***argvp __unused)
1285ea92232aSPoul-Henning Kamp {
1286ea92232aSPoul-Henning Kamp 	isoutput = 1;
1287ea92232aSPoul-Henning Kamp 
1288ea92232aSPoul-Henning Kamp 	return palloc(option);
1289ea92232aSPoul-Henning Kamp }
1290ea92232aSPoul-Henning Kamp 
1291ea92232aSPoul-Henning Kamp /*
1292ea92232aSPoul-Henning Kamp  * -print0 functions --
1293ea92232aSPoul-Henning Kamp  *
12949725a7b9SPhilippe Charnier  *	Always true, causes the current pathname to be written to
1295ea92232aSPoul-Henning Kamp  *	standard output followed by a NUL character
1296ea92232aSPoul-Henning Kamp  */
1297ea92232aSPoul-Henning Kamp int
1298ef646f18SMark Murray f_print0(PLAN *plan __unused, FTSENT *entry)
1299ea92232aSPoul-Henning Kamp {
1300ea92232aSPoul-Henning Kamp 	fputs(entry->fts_path, stdout);
1301ea92232aSPoul-Henning Kamp 	fputc('\0', stdout);
1302ea92232aSPoul-Henning Kamp 	return 1;
1303ea92232aSPoul-Henning Kamp }
1304ea92232aSPoul-Henning Kamp 
1305ea92232aSPoul-Henning Kamp /* c_print0 is the same as c_print */
1306ea92232aSPoul-Henning Kamp 
1307ea92232aSPoul-Henning Kamp /*
1308ea92232aSPoul-Henning Kamp  * -prune functions --
1309ea92232aSPoul-Henning Kamp  *
1310ea92232aSPoul-Henning Kamp  *	Prune a portion of the hierarchy.
1311ea92232aSPoul-Henning Kamp  */
1312ea92232aSPoul-Henning Kamp int
1313ef646f18SMark Murray f_prune(PLAN *plan __unused, FTSENT *entry)
1314ea92232aSPoul-Henning Kamp {
1315ea92232aSPoul-Henning Kamp 	if (fts_set(tree, entry, FTS_SKIP))
1316ea92232aSPoul-Henning Kamp 		err(1, "%s", entry->fts_path);
1317ea92232aSPoul-Henning Kamp 	return 1;
1318ea92232aSPoul-Henning Kamp }
1319ea92232aSPoul-Henning Kamp 
1320ea92232aSPoul-Henning Kamp /* c_prune == c_simple */
13217c1d4b3aSAkinori MUSHA 
13227c1d4b3aSAkinori MUSHA /*
13237c1d4b3aSAkinori MUSHA  * -regex functions --
13247c1d4b3aSAkinori MUSHA  *
13257c1d4b3aSAkinori MUSHA  *	True if the whole path of the file matches pattern using
13267c1d4b3aSAkinori MUSHA  *	regular expression.
13277c1d4b3aSAkinori MUSHA  */
13287c1d4b3aSAkinori MUSHA int
1329ef646f18SMark Murray f_regex(PLAN *plan, FTSENT *entry)
13307c1d4b3aSAkinori MUSHA {
13317c1d4b3aSAkinori MUSHA 	char *str;
133247bca8b0SJuli Mallett 	int len;
13337c1d4b3aSAkinori MUSHA 	regex_t *pre;
13347c1d4b3aSAkinori MUSHA 	regmatch_t pmatch;
13357c1d4b3aSAkinori MUSHA 	int errcode;
13367c1d4b3aSAkinori MUSHA 	char errbuf[LINE_MAX];
13377c1d4b3aSAkinori MUSHA 	int matched;
13387c1d4b3aSAkinori MUSHA 
13397c1d4b3aSAkinori MUSHA 	pre = plan->re_data;
13407c1d4b3aSAkinori MUSHA 	str = entry->fts_path;
13417c1d4b3aSAkinori MUSHA 	len = strlen(str);
13427c1d4b3aSAkinori MUSHA 	matched = 0;
13437c1d4b3aSAkinori MUSHA 
13447c1d4b3aSAkinori MUSHA 	pmatch.rm_so = 0;
13457c1d4b3aSAkinori MUSHA 	pmatch.rm_eo = len;
13467c1d4b3aSAkinori MUSHA 
13477c1d4b3aSAkinori MUSHA 	errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
13487c1d4b3aSAkinori MUSHA 
13497c1d4b3aSAkinori MUSHA 	if (errcode != 0 && errcode != REG_NOMATCH) {
13507c1d4b3aSAkinori MUSHA 		regerror(errcode, pre, errbuf, sizeof errbuf);
13517c1d4b3aSAkinori MUSHA 		errx(1, "%s: %s",
1352ea92232aSPoul-Henning Kamp 		     plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
13537c1d4b3aSAkinori MUSHA 	}
13547c1d4b3aSAkinori MUSHA 
13557c1d4b3aSAkinori MUSHA 	if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
13567c1d4b3aSAkinori MUSHA 		matched = 1;
13577c1d4b3aSAkinori MUSHA 
1358ea92232aSPoul-Henning Kamp 	return matched;
13597c1d4b3aSAkinori MUSHA }
13607c1d4b3aSAkinori MUSHA 
13617c1d4b3aSAkinori MUSHA PLAN *
1362ef646f18SMark Murray c_regex(OPTION *option, char ***argvp)
13637c1d4b3aSAkinori MUSHA {
13647c1d4b3aSAkinori MUSHA 	PLAN *new;
1365ea92232aSPoul-Henning Kamp 	char *pattern;
13667c1d4b3aSAkinori MUSHA 	regex_t *pre;
13677c1d4b3aSAkinori MUSHA 	int errcode;
13687c1d4b3aSAkinori MUSHA 	char errbuf[LINE_MAX];
13697c1d4b3aSAkinori MUSHA 
13707c1d4b3aSAkinori MUSHA 	if ((pre = malloc(sizeof(regex_t))) == NULL)
13717c1d4b3aSAkinori MUSHA 		err(1, NULL);
13727c1d4b3aSAkinori MUSHA 
1373ea92232aSPoul-Henning Kamp 	pattern = nextarg(option, argvp);
1374ea92232aSPoul-Henning Kamp 
1375ea92232aSPoul-Henning Kamp 	if ((errcode = regcomp(pre, pattern,
1376ea92232aSPoul-Henning Kamp 	    regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
13777c1d4b3aSAkinori MUSHA 		regerror(errcode, pre, errbuf, sizeof errbuf);
13787c1d4b3aSAkinori MUSHA 		errx(1, "%s: %s: %s",
1379ea92232aSPoul-Henning Kamp 		     option->flags & F_IGNCASE ? "-iregex" : "-regex",
1380ea92232aSPoul-Henning Kamp 		     pattern, errbuf);
13817c1d4b3aSAkinori MUSHA 	}
13827c1d4b3aSAkinori MUSHA 
1383ea92232aSPoul-Henning Kamp 	new = palloc(option);
13847c1d4b3aSAkinori MUSHA 	new->re_data = pre;
13857c1d4b3aSAkinori MUSHA 
1386567664c4SOllivier Robert 	return new;
1387567664c4SOllivier Robert }
1388567664c4SOllivier Robert 
138946b993ffSWarner Losh /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
13909b50d902SRodney W. Grimes 
13919b50d902SRodney W. Grimes PLAN *
1392ef646f18SMark Murray c_simple(OPTION *option, char ***argvp __unused)
13939b50d902SRodney W. Grimes {
1394ea92232aSPoul-Henning Kamp 	return palloc(option);
13959b50d902SRodney W. Grimes }
13969b50d902SRodney W. Grimes 
13979b50d902SRodney W. Grimes /*
13989b50d902SRodney W. Grimes  * -size n[c] functions --
13999b50d902SRodney W. Grimes  *
14009b50d902SRodney W. Grimes  *	True if the file size in bytes, divided by an implementation defined
14019b50d902SRodney W. Grimes  *	value and rounded up to the next integer, is n.  If n is followed by
14025a890aacSKirill Ponomarev  *      one of c k M G T P, the size is in bytes, kilobytes,
14035a890aacSKirill Ponomarev  *      megabytes, gigabytes, terabytes or petabytes respectively.
14049b50d902SRodney W. Grimes  */
14059b50d902SRodney W. Grimes #define	FIND_SIZE	512
14069b50d902SRodney W. Grimes static int divsize = 1;
14079b50d902SRodney W. Grimes 
14089b50d902SRodney W. Grimes int
1409ef646f18SMark Murray f_size(PLAN *plan, FTSENT *entry)
14109b50d902SRodney W. Grimes {
14119b50d902SRodney W. Grimes 	off_t size;
14129b50d902SRodney W. Grimes 
14139b50d902SRodney W. Grimes 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
14149b50d902SRodney W. Grimes 	    FIND_SIZE : entry->fts_statp->st_size;
14159b50d902SRodney W. Grimes 	COMPARE(size, plan->o_data);
14169b50d902SRodney W. Grimes }
14179b50d902SRodney W. Grimes 
14189b50d902SRodney W. Grimes PLAN *
1419ef646f18SMark Murray c_size(OPTION *option, char ***argvp)
14209b50d902SRodney W. Grimes {
1421ea92232aSPoul-Henning Kamp 	char *size_str;
14229b50d902SRodney W. Grimes 	PLAN *new;
14239b50d902SRodney W. Grimes 	char endch;
14245a890aacSKirill Ponomarev 	off_t scale;
14259b50d902SRodney W. Grimes 
1426ea92232aSPoul-Henning Kamp 	size_str = nextarg(option, argvp);
14279b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
14289b50d902SRodney W. Grimes 
1429ea92232aSPoul-Henning Kamp 	new = palloc(option);
14309b50d902SRodney W. Grimes 	endch = 'c';
1431ea92232aSPoul-Henning Kamp 	new->o_data = find_parsenum(new, option->name, size_str, &endch);
14325a890aacSKirill Ponomarev 	if (endch != '\0') {
14339b50d902SRodney W. Grimes 		divsize = 0;
14345a890aacSKirill Ponomarev 
14355a890aacSKirill Ponomarev 		switch (endch) {
14365a890aacSKirill Ponomarev 		case 'c':                       /* characters */
14375a890aacSKirill Ponomarev 			scale = 0x1LL;
14385a890aacSKirill Ponomarev 			break;
14395a890aacSKirill Ponomarev 		case 'k':                       /* kilobytes 1<<10 */
14405a890aacSKirill Ponomarev 			scale = 0x400LL;
14415a890aacSKirill Ponomarev 			break;
14425a890aacSKirill Ponomarev 		case 'M':                       /* megabytes 1<<20 */
14435a890aacSKirill Ponomarev 			scale = 0x100000LL;
14445a890aacSKirill Ponomarev 			break;
14455a890aacSKirill Ponomarev 		case 'G':                       /* gigabytes 1<<30 */
14465a890aacSKirill Ponomarev 			scale = 0x40000000LL;
14475a890aacSKirill Ponomarev 			break;
14485a890aacSKirill Ponomarev 		case 'T':                       /* terabytes 1<<40 */
14495a890aacSKirill Ponomarev 			scale = 0x1000000000LL;
14505a890aacSKirill Ponomarev 			break;
14515a890aacSKirill Ponomarev 		case 'P':                       /* petabytes 1<<50 */
14525a890aacSKirill Ponomarev 			scale = 0x4000000000000LL;
14535a890aacSKirill Ponomarev 			break;
14545a890aacSKirill Ponomarev 		default:
14555a890aacSKirill Ponomarev 			errx(1, "%s: %s: illegal trailing character",
14565a890aacSKirill Ponomarev 				option->name, size_str);
14575a890aacSKirill Ponomarev 			break;
14585a890aacSKirill Ponomarev 		}
14595a890aacSKirill Ponomarev 		if (new->o_data > QUAD_MAX / scale)
14605a890aacSKirill Ponomarev 			errx(1, "%s: %s: value too large",
14615a890aacSKirill Ponomarev 				option->name, size_str);
14625a890aacSKirill Ponomarev 		new->o_data *= scale;
14635a890aacSKirill Ponomarev 	}
1464ea92232aSPoul-Henning Kamp 	return new;
14659b50d902SRodney W. Grimes }
14669b50d902SRodney W. Grimes 
14679b50d902SRodney W. Grimes /*
14689b50d902SRodney W. Grimes  * -type c functions --
14699b50d902SRodney W. Grimes  *
1470841484cdSPeter Wemm  *	True if the type of the file is c, where c is b, c, d, p, f or w
1471841484cdSPeter Wemm  *	for block special file, character special file, directory, FIFO,
1472841484cdSPeter Wemm  *	regular file or whiteout respectively.
14739b50d902SRodney W. Grimes  */
14749b50d902SRodney W. Grimes int
1475ef646f18SMark Murray f_type(PLAN *plan, FTSENT *entry)
14769b50d902SRodney W. Grimes {
1477ea92232aSPoul-Henning Kamp 	return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
14789b50d902SRodney W. Grimes }
14799b50d902SRodney W. Grimes 
14809b50d902SRodney W. Grimes PLAN *
1481ef646f18SMark Murray c_type(OPTION *option, char ***argvp)
14829b50d902SRodney W. Grimes {
1483ea92232aSPoul-Henning Kamp 	char *typestring;
14849b50d902SRodney W. Grimes 	PLAN *new;
14859b50d902SRodney W. Grimes 	mode_t  mask;
14869b50d902SRodney W. Grimes 
1487ea92232aSPoul-Henning Kamp 	typestring = nextarg(option, argvp);
14889b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
14899b50d902SRodney W. Grimes 
14909b50d902SRodney W. Grimes 	switch (typestring[0]) {
14919b50d902SRodney W. Grimes 	case 'b':
14929b50d902SRodney W. Grimes 		mask = S_IFBLK;
14939b50d902SRodney W. Grimes 		break;
14949b50d902SRodney W. Grimes 	case 'c':
14959b50d902SRodney W. Grimes 		mask = S_IFCHR;
14969b50d902SRodney W. Grimes 		break;
14979b50d902SRodney W. Grimes 	case 'd':
14989b50d902SRodney W. Grimes 		mask = S_IFDIR;
14999b50d902SRodney W. Grimes 		break;
15009b50d902SRodney W. Grimes 	case 'f':
15019b50d902SRodney W. Grimes 		mask = S_IFREG;
15029b50d902SRodney W. Grimes 		break;
15039b50d902SRodney W. Grimes 	case 'l':
15049b50d902SRodney W. Grimes 		mask = S_IFLNK;
15059b50d902SRodney W. Grimes 		break;
15069b50d902SRodney W. Grimes 	case 'p':
15079b50d902SRodney W. Grimes 		mask = S_IFIFO;
15089b50d902SRodney W. Grimes 		break;
15099b50d902SRodney W. Grimes 	case 's':
15109b50d902SRodney W. Grimes 		mask = S_IFSOCK;
15119b50d902SRodney W. Grimes 		break;
1512841484cdSPeter Wemm #ifdef FTS_WHITEOUT
1513841484cdSPeter Wemm 	case 'w':
1514841484cdSPeter Wemm 		mask = S_IFWHT;
1515841484cdSPeter Wemm 		ftsoptions |= FTS_WHITEOUT;
1516841484cdSPeter Wemm 		break;
1517841484cdSPeter Wemm #endif /* FTS_WHITEOUT */
15189b50d902SRodney W. Grimes 	default:
1519ea92232aSPoul-Henning Kamp 		errx(1, "%s: %s: unknown type", option->name, typestring);
15209b50d902SRodney W. Grimes 	}
15219b50d902SRodney W. Grimes 
1522ea92232aSPoul-Henning Kamp 	new = palloc(option);
15239b50d902SRodney W. Grimes 	new->m_data = mask;
1524ea92232aSPoul-Henning Kamp 	return new;
1525abacbbbfSPeter Wemm }
1526abacbbbfSPeter Wemm 
1527abacbbbfSPeter Wemm /*
15289b50d902SRodney W. Grimes  * -user uname functions --
15299b50d902SRodney W. Grimes  *
15309b50d902SRodney W. Grimes  *	True if the file belongs to the user uname.  If uname is numeric and
15319b50d902SRodney W. Grimes  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
15329b50d902SRodney W. Grimes  *	return a valid user name, uname is taken as a user ID.
15339b50d902SRodney W. Grimes  */
15349b50d902SRodney W. Grimes int
1535ef646f18SMark Murray f_user(PLAN *plan, FTSENT *entry)
15369b50d902SRodney W. Grimes {
15374ba3b38bSKirill Ponomarev 	COMPARE(entry->fts_statp->st_uid, plan->u_data);
15389b50d902SRodney W. Grimes }
15399b50d902SRodney W. Grimes 
15409b50d902SRodney W. Grimes PLAN *
1541ef646f18SMark Murray c_user(OPTION *option, char ***argvp)
15429b50d902SRodney W. Grimes {
1543ea92232aSPoul-Henning Kamp 	char *username;
15449b50d902SRodney W. Grimes 	PLAN *new;
15459b50d902SRodney W. Grimes 	struct passwd *p;
15469b50d902SRodney W. Grimes 	uid_t uid;
15479b50d902SRodney W. Grimes 
1548ea92232aSPoul-Henning Kamp 	username = nextarg(option, argvp);
15499b50d902SRodney W. Grimes 	ftsoptions &= ~FTS_NOSTAT;
15509b50d902SRodney W. Grimes 
15514ba3b38bSKirill Ponomarev 	new = palloc(option);
15529b50d902SRodney W. Grimes 	p = getpwnam(username);
15539b50d902SRodney W. Grimes 	if (p == NULL) {
15544ba3b38bSKirill Ponomarev 		char* cp = username;
15554ba3b38bSKirill Ponomarev 		if( username[0] == '-' || username[0] == '+' )
15564ba3b38bSKirill Ponomarev 			username++;
15579b50d902SRodney W. Grimes 		uid = atoi(username);
15589b50d902SRodney W. Grimes 		if (uid == 0 && username[0] != '0')
1559ea92232aSPoul-Henning Kamp 			errx(1, "%s: %s: no such user", option->name, username);
15604ba3b38bSKirill Ponomarev 		uid = find_parsenum(new, option->name, cp, NULL);
15619b50d902SRodney W. Grimes 	} else
15629b50d902SRodney W. Grimes 		uid = p->pw_uid;
15639b50d902SRodney W. Grimes 
15649b50d902SRodney W. Grimes 	new->u_data = uid;
1565ea92232aSPoul-Henning Kamp 	return new;
15669b50d902SRodney W. Grimes }
15679b50d902SRodney W. Grimes 
15689b50d902SRodney W. Grimes /*
15699b50d902SRodney W. Grimes  * -xdev functions --
15709b50d902SRodney W. Grimes  *
15719725a7b9SPhilippe Charnier  *	Always true, causes find not to descend past directories that have a
15729b50d902SRodney W. Grimes  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
15739b50d902SRodney W. Grimes  */
15749b50d902SRodney W. Grimes PLAN *
1575ef646f18SMark Murray c_xdev(OPTION *option, char ***argvp __unused)
15769b50d902SRodney W. Grimes {
15779b50d902SRodney W. Grimes 	ftsoptions |= FTS_XDEV;
15789b50d902SRodney W. Grimes 
1579ea92232aSPoul-Henning Kamp 	return palloc(option);
15809b50d902SRodney W. Grimes }
15819b50d902SRodney W. Grimes 
15829b50d902SRodney W. Grimes /*
15839b50d902SRodney W. Grimes  * ( expression ) functions --
15849b50d902SRodney W. Grimes  *
15859b50d902SRodney W. Grimes  *	True if expression is true.
15869b50d902SRodney W. Grimes  */
15879b50d902SRodney W. Grimes int
1588ef646f18SMark Murray f_expr(PLAN *plan, FTSENT *entry)
15899b50d902SRodney W. Grimes {
1590e98080b1SDavid Malone 	PLAN *p;
1591e98080b1SDavid Malone 	int state = 0;
15929b50d902SRodney W. Grimes 
15939b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
1594ea92232aSPoul-Henning Kamp 	    p && (state = (p->execute)(p, entry)); p = p->next);
1595ea92232aSPoul-Henning Kamp 	return state;
15969b50d902SRodney W. Grimes }
15979b50d902SRodney W. Grimes 
15989b50d902SRodney W. Grimes /*
1599ea92232aSPoul-Henning Kamp  * f_openparen and f_closeparen nodes are temporary place markers.  They are
16009b50d902SRodney W. Grimes  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1601ea92232aSPoul-Henning Kamp  * to a f_expr node containing the expression and the ')' node is discarded.
1602ea92232aSPoul-Henning Kamp  * The functions themselves are only used as constants.
16039b50d902SRodney W. Grimes  */
1604ea92232aSPoul-Henning Kamp 
1605ea92232aSPoul-Henning Kamp int
1606ef646f18SMark Murray f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
16079b50d902SRodney W. Grimes {
1608ea92232aSPoul-Henning Kamp 	abort();
16099b50d902SRodney W. Grimes }
16109b50d902SRodney W. Grimes 
1611ea92232aSPoul-Henning Kamp int
1612ef646f18SMark Murray f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
16139b50d902SRodney W. Grimes {
1614ea92232aSPoul-Henning Kamp 	abort();
1615ea92232aSPoul-Henning Kamp }
1616ea92232aSPoul-Henning Kamp 
1617ea92232aSPoul-Henning Kamp /* c_openparen == c_simple */
1618ea92232aSPoul-Henning Kamp /* c_closeparen == c_simple */
1619ea92232aSPoul-Henning Kamp 
1620ea92232aSPoul-Henning Kamp /*
1621ea92232aSPoul-Henning Kamp  * AND operator. Since AND is implicit, no node is allocated.
1622ea92232aSPoul-Henning Kamp  */
1623ea92232aSPoul-Henning Kamp PLAN *
1624ef646f18SMark Murray c_and(OPTION *option __unused, char ***argvp __unused)
1625ea92232aSPoul-Henning Kamp {
1626ea92232aSPoul-Henning Kamp 	return NULL;
16279b50d902SRodney W. Grimes }
16289b50d902SRodney W. Grimes 
16299b50d902SRodney W. Grimes /*
16309b50d902SRodney W. Grimes  * ! expression functions --
16319b50d902SRodney W. Grimes  *
16329b50d902SRodney W. Grimes  *	Negation of a primary; the unary NOT operator.
16339b50d902SRodney W. Grimes  */
16349b50d902SRodney W. Grimes int
1635ef646f18SMark Murray f_not(PLAN *plan, FTSENT *entry)
16369b50d902SRodney W. Grimes {
1637e98080b1SDavid Malone 	PLAN *p;
1638e98080b1SDavid Malone 	int state = 0;
16399b50d902SRodney W. Grimes 
16409b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
1641ea92232aSPoul-Henning Kamp 	    p && (state = (p->execute)(p, entry)); p = p->next);
1642ea92232aSPoul-Henning Kamp 	return !state;
16439b50d902SRodney W. Grimes }
16449b50d902SRodney W. Grimes 
1645ea92232aSPoul-Henning Kamp /* c_not == c_simple */
16469b50d902SRodney W. Grimes 
16479b50d902SRodney W. Grimes /*
16489b50d902SRodney W. Grimes  * expression -o expression functions --
16499b50d902SRodney W. Grimes  *
16509b50d902SRodney W. Grimes  *	Alternation of primaries; the OR operator.  The second expression is
16519b50d902SRodney W. Grimes  * not evaluated if the first expression is true.
16529b50d902SRodney W. Grimes  */
16539b50d902SRodney W. Grimes int
1654ef646f18SMark Murray f_or(PLAN *plan, FTSENT *entry)
16559b50d902SRodney W. Grimes {
1656e98080b1SDavid Malone 	PLAN *p;
1657e98080b1SDavid Malone 	int state = 0;
16589b50d902SRodney W. Grimes 
16599b50d902SRodney W. Grimes 	for (p = plan->p_data[0];
1660ea92232aSPoul-Henning Kamp 	    p && (state = (p->execute)(p, entry)); p = p->next);
16619b50d902SRodney W. Grimes 
16629b50d902SRodney W. Grimes 	if (state)
1663ea92232aSPoul-Henning Kamp 		return 1;
16649b50d902SRodney W. Grimes 
16659b50d902SRodney W. Grimes 	for (p = plan->p_data[1];
1666ea92232aSPoul-Henning Kamp 	    p && (state = (p->execute)(p, entry)); p = p->next);
1667ea92232aSPoul-Henning Kamp 	return state;
16689b50d902SRodney W. Grimes }
16699b50d902SRodney W. Grimes 
1670ea92232aSPoul-Henning Kamp /* c_or == c_simple */
167146b993ffSWarner Losh 
167246b993ffSWarner Losh /*
167346b993ffSWarner Losh  * -false
167446b993ffSWarner Losh  *
167546b993ffSWarner Losh  *	Always false.
167646b993ffSWarner Losh  */
167746b993ffSWarner Losh int
167846b993ffSWarner Losh f_false(PLAN *plan __unused, FTSENT *entry __unused)
167946b993ffSWarner Losh {
168046b993ffSWarner Losh 	return 0;
168146b993ffSWarner Losh }
168246b993ffSWarner Losh 
168346b993ffSWarner Losh /* c_false == c_simple */
168446b993ffSWarner Losh 
168546b993ffSWarner Losh /*
168646b993ffSWarner Losh  * -quit
168746b993ffSWarner Losh  *
168846b993ffSWarner Losh  *	Exits the program
168946b993ffSWarner Losh  */
169046b993ffSWarner Losh int
169146b993ffSWarner Losh f_quit(PLAN *plan __unused, FTSENT *entry __unused)
169246b993ffSWarner Losh {
169346b993ffSWarner Losh 	exit(0);
169446b993ffSWarner Losh }
169546b993ffSWarner Losh 
169646b993ffSWarner Losh /* c_quit == c_simple */
1697