xref: /dragonfly/usr.bin/find/function.c (revision e8c03636)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Cimarron D. Taylor of the University of California, Berkeley.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#)function.c  8.10 (Berkeley) 5/4/95
33  * $FreeBSD: src/usr.bin/find/function.c,v 1.70 2010/12/11 08:32:16 joel Exp $
34  */
35 
36 #include <sys/param.h>
37 #include <sys/ucred.h>
38 #include <sys/stat.h>
39 #include <sys/wait.h>
40 #include <sys/mount.h>
41 
42 #include <dirent.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <fnmatch.h>
46 #include <fts.h>
47 #include <grp.h>
48 #include <limits.h>
49 #include <pwd.h>
50 #include <regex.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <ctype.h>
56 
57 #include "find.h"
58 
59 static PLAN *palloc(OPTION *);
60 static long long find_parsenum(PLAN *, const char *, char *, char *);
61 static long long find_parsetime(PLAN *, const char *, char *);
62 static char *nextarg(OPTION *, char ***);
63 
64 extern char **environ;
65 
66 static PLAN *lastexecplus = NULL;
67 
68 #define	COMPARE(a, b) do {						\
69 	switch (plan->flags & F_ELG_MASK) {				\
70 	case F_EQUAL:							\
71 		return (a == b);					\
72 	case F_LESSTHAN:						\
73 		return (a < b);						\
74 	case F_GREATER:							\
75 		return (a > b);						\
76 	default:							\
77 		abort();						\
78 	}								\
79 } while(0)
80 
81 static PLAN *
82 palloc(OPTION *option)
83 {
84 	PLAN *new;
85 
86 	if ((new = malloc(sizeof(PLAN))) == NULL)
87 		err(1, NULL);
88 	new->execute = option->execute;
89 	new->flags = option->flags;
90 	new->next = NULL;
91 	return new;
92 }
93 
94 /*
95  * find_parsenum --
96  *	Parse a string of the form [+-]# and return the value.
97  */
98 static long long
99 find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
100 {
101 	long long value;
102 	char *endchar, *str;	/* Pointer to character ending conversion. */
103 
104 	/* Determine comparison from leading + or -. */
105 	str = vp;
106 	switch (*str) {
107 	case '+':
108 		++str;
109 		plan->flags |= F_GREATER;
110 		break;
111 	case '-':
112 		++str;
113 		plan->flags |= F_LESSTHAN;
114 		break;
115 	default:
116 		plan->flags |= F_EQUAL;
117 		break;
118 	}
119 
120 	/*
121 	 * Convert the string with strtoq().  Note, if strtoq() returns zero
122 	 * and endchar points to the beginning of the string we know we have
123 	 * a syntax error.
124 	 */
125 	value = strtoq(str, &endchar, 10);
126 	if (value == 0 && endchar == str)
127 		errx(1, "%s: %s: illegal numeric value", option, vp);
128 	if (endchar[0] && endch == NULL)
129 		errx(1, "%s: %s: illegal trailing character", option, vp);
130 	if (endch)
131 		*endch = endchar[0];
132 	return value;
133 }
134 
135 /*
136  * find_parsetime --
137  *	Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
138  */
139 static long long
140 find_parsetime(PLAN *plan, const char *option, char *vp)
141 {
142 	long long secs, value;
143 	char *str, *unit;	/* Pointer to character ending conversion. */
144 
145 	/* Determine comparison from leading + or -. */
146 	str = vp;
147 	switch (*str) {
148 	case '+':
149 		++str;
150 		plan->flags |= F_GREATER;
151 		break;
152 	case '-':
153 		++str;
154 		plan->flags |= F_LESSTHAN;
155 		break;
156 	default:
157 		plan->flags |= F_EQUAL;
158 		break;
159 	}
160 
161 	value = strtoq(str, &unit, 10);
162 	if (value == 0 && unit == str) {
163 		errx(1, "%s: %s: illegal time value", option, vp);
164 		/* NOTREACHED */
165 	}
166 	if (*unit == '\0')
167 		return value;
168 
169 	/* Units syntax. */
170 	secs = 0;
171 	for (;;) {
172 		switch(*unit) {
173 		case 's':	/* seconds */
174 			secs += value;
175 			break;
176 		case 'm':	/* minutes */
177 			secs += value * 60;
178 			break;
179 		case 'h':	/* hours */
180 			secs += value * 3600;
181 			break;
182 		case 'd':	/* days */
183 			secs += value * 86400;
184 			break;
185 		case 'w':	/* weeks */
186 			secs += value * 604800;
187 			break;
188 		default:
189 			errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
190 			/* NOTREACHED */
191 		}
192 		str = unit + 1;
193 		if (*str == '\0')	/* EOS */
194 			break;
195 		value = strtoq(str, &unit, 10);
196 		if (value == 0 && unit == str) {
197 			errx(1, "%s: %s: illegal time value", option, vp);
198 			/* NOTREACHED */
199 		}
200 		if (*unit == '\0') {
201 			errx(1, "%s: %s: missing trailing unit", option, vp);
202 			/* NOTREACHED */
203 		}
204 	}
205 	plan->flags |= F_EXACTTIME;
206 	return secs;
207 }
208 
209 /*
210  * nextarg --
211  *	Check that another argument still exists, return a pointer to it,
212  *	and increment the argument vector pointer.
213  */
214 static char *
215 nextarg(OPTION *option, char ***argvp)
216 {
217 	char *arg;
218 
219 	if ((arg = **argvp) == NULL)
220 		errx(1, "%s: requires additional arguments", option->name);
221 	(*argvp)++;
222 	return arg;
223 } /* nextarg() */
224 
225 /*
226  * The value of n for the inode times (atime, ctime, and mtime) is a range,
227  * i.e. n matches from (n - 1) to n 24 hour periods.  This interacts with
228  * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
229  * user wanted.  Correct so that -1 is "less than 1".
230  */
231 #define	TIME_CORRECT(p) \
232 	if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
233 		++((p)->t_data);
234 
235 /*
236  * -[acm]min n functions --
237  *
238  *    True if the difference between the
239  *		file access time (-amin)
240  *		last change of file status information (-cmin)
241  *		file modification time (-mmin)
242  *    and the current time is n min periods.
243  */
244 int
245 f_Xmin(PLAN *plan, FTSENT *entry)
246 {
247 	if (plan->flags & F_TIME_C) {
248 		COMPARE((now - entry->fts_statp->st_ctime +
249 		    60 - 1) / 60, plan->t_data);
250 	} else if (plan->flags & F_TIME_A) {
251 		COMPARE((now - entry->fts_statp->st_atime +
252 		    60 - 1) / 60, plan->t_data);
253 	} else {
254 		COMPARE((now - entry->fts_statp->st_mtime +
255 		    60 - 1) / 60, plan->t_data);
256 	}
257 }
258 
259 PLAN *
260 c_Xmin(OPTION *option, char ***argvp)
261 {
262 	char *nmins;
263 	PLAN *new;
264 
265 	nmins = nextarg(option, argvp);
266 	ftsoptions &= ~FTS_NOSTAT;
267 
268 	new = palloc(option);
269 	new->t_data = find_parsenum(new, option->name, nmins, NULL);
270 	TIME_CORRECT(new);
271 	return new;
272 }
273 
274 /*
275  * -[acm]time n functions --
276  *
277  *	True if the difference between the
278  *		file access time (-atime)
279  *		last change of file status information (-ctime)
280  *		file modification time (-mtime)
281  *	and the current time is n 24 hour periods.
282  */
283 
284 int
285 f_Xtime(PLAN *plan, FTSENT *entry)
286 {
287 	time_t xtime;
288 
289 	if (plan->flags & F_TIME_A)
290 		xtime = entry->fts_statp->st_atime;
291 	else if (plan->flags & F_TIME_C)
292 		xtime = entry->fts_statp->st_ctime;
293 	else
294 		xtime = entry->fts_statp->st_mtime;
295 
296 	if (plan->flags & F_EXACTTIME)
297 		COMPARE(now - xtime, plan->t_data);
298 	else
299 		COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data);
300 }
301 
302 PLAN *
303 c_Xtime(OPTION *option, char ***argvp)
304 {
305 	char *value;
306 	PLAN *new;
307 
308 	value = nextarg(option, argvp);
309 	ftsoptions &= ~FTS_NOSTAT;
310 
311 	new = palloc(option);
312 	new->t_data = find_parsetime(new, option->name, value);
313 	if (!(new->flags & F_EXACTTIME))
314 		TIME_CORRECT(new);
315 	return new;
316 }
317 
318 /*
319  * -maxdepth/-mindepth n functions --
320  *
321  *        Does the same as -prune if the level of the current file is
322  *        greater/less than the specified maximum/minimum depth.
323  *
324  *        Note that -maxdepth and -mindepth are handled specially in
325  *        find_execute() so their f_* functions are set to f_always_true().
326  */
327 PLAN *
328 c_mXXdepth(OPTION *option, char ***argvp)
329 {
330 	char *dstr;
331 	PLAN *new;
332 
333 	dstr = nextarg(option, argvp);
334 	if (dstr[0] == '-')
335 		/* all other errors handled by find_parsenum() */
336 		errx(1, "%s: %s: value must be positive", option->name, dstr);
337 
338 	new = palloc(option);
339 	if (option->flags & F_MAXDEPTH)
340 		maxdepth = find_parsenum(new, option->name, dstr, NULL);
341 	else
342 		mindepth = find_parsenum(new, option->name, dstr, NULL);
343 	return new;
344 }
345 
346 /*
347  * -delete functions --
348  *
349  *	True always.  Makes its best shot and continues on regardless.
350  */
351 int
352 f_delete(PLAN *plan __unused, FTSENT *entry)
353 {
354 	/* ignore these from fts */
355 	if (strcmp(entry->fts_accpath, ".") == 0 ||
356 	    strcmp(entry->fts_accpath, "..") == 0)
357 		return 1;
358 
359 	/* sanity check */
360 	if (isdepth == 0 ||			/* depth off */
361 	    (ftsoptions & FTS_NOSTAT))		/* not stat()ing */
362 		errx(1, "-delete: insecure options got turned on");
363 
364 	if (!(ftsoptions & FTS_PHYSICAL) ||	/* physical off */
365 	    (ftsoptions & FTS_LOGICAL))		/* or finally, logical on */
366 		errx(1, "-delete: forbidden when symlinks are followed");
367 
368 	/* Potentially unsafe - do not accept relative paths whatsoever */
369 	if (strchr(entry->fts_accpath, '/') != NULL)
370 		errx(1, "-delete: %s: relative path potentially not safe",
371 			entry->fts_accpath);
372 
373 	/* Turn off user immutable bits if running as root */
374 	if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
375 	    !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
376 	    geteuid() == 0)
377 		lchflags(entry->fts_accpath,
378 		       entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
379 
380 	/* rmdir directories, unlink everything else */
381 	if (S_ISDIR(entry->fts_statp->st_mode)) {
382 		if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
383 			warn("-delete: rmdir(%s)", entry->fts_path);
384 	} else {
385 		if (unlink(entry->fts_accpath) < 0)
386 			warn("-delete: unlink(%s)", entry->fts_path);
387 	}
388 
389 	/* "succeed" */
390 	return 1;
391 }
392 
393 PLAN *
394 c_delete(OPTION *option, char ***argvp __unused)
395 {
396 
397 	ftsoptions &= ~FTS_NOSTAT;	/* no optimise */
398 	isoutput = 1;			/* possible output */
399 	isdepth = 1;			/* -depth implied */
400 
401 	return palloc(option);
402 }
403 
404 
405 /*
406  * always_true --
407  *
408  *	Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
409  */
410 int
411 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
412 {
413 	return 1;
414 }
415 
416 /*
417  * -depth functions --
418  *
419  *	With argument: True if the file is at level n.
420  *	Without argument: Always true, causes descent of the directory hierarchy
421  *	to be done so that all entries in a directory are acted on before the
422  *	directory itself.
423  */
424 int
425 f_depth(PLAN *plan, FTSENT *entry)
426 {
427 	if (plan->flags & F_DEPTH)
428 		COMPARE(entry->fts_level, plan->d_data);
429 	else
430 		return 1;
431 }
432 
433 PLAN *
434 c_depth(OPTION *option, char ***argvp)
435 {
436 	PLAN *new;
437 	char *str;
438 
439 	new = palloc(option);
440 
441 	str = **argvp;
442 	if (str && !(new->flags & F_DEPTH)) {
443 		/* skip leading + or - */
444 		if (*str == '+' || *str == '-')
445 			str++;
446 		/* skip sign */
447 		if (*str == '+' || *str == '-')
448 			str++;
449 		if (isdigit(*str))
450 			new->flags |= F_DEPTH;
451 	}
452 
453 	if (new->flags & F_DEPTH) {	/* -depth n */
454 		char *ndepth;
455 
456 		ndepth = nextarg(option, argvp);
457 		new->d_data = find_parsenum(new, option->name, ndepth, NULL);
458 	} else {			/* -d */
459 		isdepth = 1;
460 	}
461 
462 	return new;
463 }
464 
465 /*
466  * -empty functions --
467  *
468  *	True if the file or directory is empty
469  */
470 int
471 f_empty(PLAN *plan __unused, FTSENT *entry)
472 {
473 	if (S_ISREG(entry->fts_statp->st_mode) &&
474 	    entry->fts_statp->st_size == 0)
475 		return 1;
476 	if (S_ISDIR(entry->fts_statp->st_mode)) {
477 		struct dirent *dp;
478 		int empty;
479 		DIR *dir;
480 
481 		empty = 1;
482 		dir = opendir(entry->fts_accpath);
483 		if (dir == NULL)
484 			return 0;
485 		for (dp = readdir(dir); dp; dp = readdir(dir))
486 			if (dp->d_name[0] != '.' ||
487 			    (dp->d_name[1] != '\0' &&
488 			     (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
489 				empty = 0;
490 				break;
491 			}
492 		closedir(dir);
493 		return empty;
494 	}
495 	return 0;
496 }
497 
498 PLAN *
499 c_empty(OPTION *option, char ***argvp __unused)
500 {
501 	ftsoptions &= ~FTS_NOSTAT;
502 
503 	return palloc(option);
504 }
505 
506 /*
507  * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
508  *
509  *	True if the executed utility returns a zero value as exit status.
510  *	The end of the primary expression is delimited by a semicolon.  If
511  *	"{}" occurs anywhere, it gets replaced by the current pathname,
512  *	or, in the case of -execdir, the current basename (filename
513  *	without leading directory prefix). For -exec and -ok,
514  *	the current directory for the execution of utility is the same as
515  *	the current directory when the find utility was started, whereas
516  *	for -execdir, it is the directory the file resides in.
517  *
518  *	The primary -ok differs from -exec in that it requests affirmation
519  *	of the user before executing the utility.
520  */
521 int
522 f_exec(PLAN *plan, FTSENT *entry)
523 {
524 	int cnt;
525 	pid_t pid;
526 	int status;
527 	char *file;
528 
529 	if (entry == NULL && plan->flags & F_EXECPLUS) {
530 		if (plan->e_ppos == plan->e_pbnum)
531 			return (1);
532 		plan->e_argv[plan->e_ppos] = NULL;
533 		goto doexec;
534 	}
535 
536 	/* XXX - if file/dir ends in '/' this will not work -- can it? */
537 	if ((plan->flags & F_EXECDIR) && \
538 	    (file = strrchr(entry->fts_path, '/')))
539 		file++;
540 	else
541 		file = entry->fts_path;
542 
543 	if (plan->flags & F_EXECPLUS) {
544 		if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
545 			err(1, NULL);
546 		plan->e_len[plan->e_ppos] = strlen(file);
547 		plan->e_psize += plan->e_len[plan->e_ppos];
548 		if (++plan->e_ppos < plan->e_pnummax &&
549 		    plan->e_psize < plan->e_psizemax)
550 			return (1);
551 		plan->e_argv[plan->e_ppos] = NULL;
552 	} else {
553 		for (cnt = 0; plan->e_argv[cnt]; ++cnt)
554 			if (plan->e_len[cnt])
555 				brace_subst(plan->e_orig[cnt],
556 				    &plan->e_argv[cnt], file,
557 				    plan->e_len[cnt]);
558 	}
559 
560 doexec:	if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
561 		return 0;
562 
563 	/* make sure find output is interspersed correctly with subprocesses */
564 	fflush(stdout);
565 	fflush(stderr);
566 
567 	switch (pid = fork()) {
568 	case -1:
569 		err(1, "fork");
570 		/* NOTREACHED */
571 	case 0:
572 		/* change dir back from where we started */
573 		if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
574 			warn("chdir");
575 			_exit(1);
576 		}
577 		execvp(plan->e_argv[0], plan->e_argv);
578 		warn("%s", plan->e_argv[0]);
579 		_exit(1);
580 	}
581 	if (plan->flags & F_EXECPLUS) {
582 		while (--plan->e_ppos >= plan->e_pbnum)
583 			free(plan->e_argv[plan->e_ppos]);
584 		plan->e_ppos = plan->e_pbnum;
585 		plan->e_psize = plan->e_pbsize;
586 	}
587 	pid = waitpid(pid, &status, 0);
588 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
589 }
590 
591 /*
592  * c_exec, c_execdir, c_ok --
593  *	build three parallel arrays, one with pointers to the strings passed
594  *	on the command line, one with (possibly duplicated) pointers to the
595  *	argv array, and one with integer values that are lengths of the
596  *	strings, but also flags meaning that the string has to be massaged.
597  */
598 PLAN *
599 c_exec(OPTION *option, char ***argvp)
600 {
601 	PLAN *new;			/* node returned */
602 	long argmax;
603 	int cnt, i;
604 	char **argv, **ap, **ep, *p;
605 
606 	/* XXX - was in c_execdir, but seems unnecessary!?
607 	ftsoptions &= ~FTS_NOSTAT;
608 	*/
609 	isoutput = 1;
610 
611 	/* XXX - this is a change from the previous coding */
612 	new = palloc(option);
613 
614 	for (ap = argv = *argvp;; ++ap) {
615 		if (!*ap)
616 			errx(1,
617 			    "%s: no terminating \";\" or \"+\"", option->name);
618 		if (**ap == ';')
619 			break;
620 		if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
621 			new->flags |= F_EXECPLUS;
622 			break;
623 		}
624 	}
625 
626 	if (ap == argv)
627 		errx(1, "%s: no command specified", option->name);
628 
629 	cnt = ap - *argvp + 1;
630 	if (new->flags & F_EXECPLUS) {
631 		new->e_ppos = new->e_pbnum = cnt - 2;
632 		if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
633 			warn("sysconf(_SC_ARG_MAX)");
634 			argmax = _POSIX_ARG_MAX;
635 		}
636 		argmax -= 1024;
637 		for (ep = environ; *ep != NULL; ep++)
638 			argmax -= strlen(*ep) + 1 + sizeof(*ep);
639 		argmax -= 1 + sizeof(*ep);
640 		new->e_pnummax = argmax / 16;
641 		argmax -= sizeof(char *) * new->e_pnummax;
642 		if (argmax <= 0)
643 			errx(1, "no space for arguments");
644 		new->e_psizemax = argmax;
645 		new->e_pbsize = 0;
646 		cnt += new->e_pnummax + 1;
647 		new->e_next = lastexecplus;
648 		lastexecplus = new;
649 	}
650 	if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
651 		err(1, NULL);
652 	if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
653 		err(1, NULL);
654 	if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
655 		err(1, NULL);
656 
657 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
658 		new->e_orig[cnt] = *argv;
659 		if (new->flags & F_EXECPLUS)
660 			new->e_pbsize += strlen(*argv) + 1;
661 		for (p = *argv; *p; ++p)
662 			if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
663 			    p[1] == '}') {
664 				if ((new->e_argv[cnt] =
665 				    malloc(MAXPATHLEN)) == NULL)
666 					err(1, NULL);
667 				new->e_len[cnt] = MAXPATHLEN;
668 				break;
669 			}
670 		if (!*p) {
671 			new->e_argv[cnt] = *argv;
672 			new->e_len[cnt] = 0;
673 		}
674 	}
675 	if (new->flags & F_EXECPLUS) {
676 		new->e_psize = new->e_pbsize;
677 		cnt--;
678 		for (i = 0; i < new->e_pnummax; i++) {
679 			new->e_argv[cnt] = NULL;
680 			new->e_len[cnt] = 0;
681 			cnt++;
682 		}
683 		argv = ap;
684 		goto done;
685 	}
686 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
687 
688 done:	*argvp = argv + 1;
689 	return new;
690 }
691 
692 /* Finish any pending -exec ... {} + functions. */
693 void
694 finish_execplus(void)
695 {
696 	PLAN *p;
697 
698 	p = lastexecplus;
699 	while (p != NULL) {
700 		(p->execute)(p, NULL);
701 		p = p->e_next;
702 	}
703 }
704 
705 int
706 f_flags(PLAN *plan, FTSENT *entry)
707 {
708 	u_long flags;
709 
710 	flags = entry->fts_statp->st_flags;
711 	if (plan->flags & F_ATLEAST)
712 		return (flags | plan->fl_flags) == flags &&
713 		    !(flags & plan->fl_notflags);
714 	else if (plan->flags & F_ANY)
715 		return (flags & plan->fl_flags) ||
716 		    (flags | plan->fl_notflags) != flags;
717 	else
718 		return flags == plan->fl_flags &&
719 		    !(plan->fl_flags & plan->fl_notflags);
720 }
721 
722 PLAN *
723 c_flags(OPTION *option, char ***argvp)
724 {
725 	char *flags_str;
726 	PLAN *new;
727 	u_long flags, notflags;
728 
729 	flags_str = nextarg(option, argvp);
730 	ftsoptions &= ~FTS_NOSTAT;
731 
732 	new = palloc(option);
733 
734 	if (*flags_str == '-') {
735 		new->flags |= F_ATLEAST;
736 		flags_str++;
737 	} else if (*flags_str == '+') {
738 		new->flags |= F_ANY;
739 		flags_str++;
740 	}
741 	if (strtofflags(&flags_str, &flags, &notflags) == 1)
742 		errx(1, "%s: %s: illegal flags string", option->name, flags_str);
743 
744 	new->fl_flags = flags;
745 	new->fl_notflags = notflags;
746 	return new;
747 }
748 
749 /*
750  * -follow functions --
751  *
752  *	Always true, causes symbolic links to be followed on a global
753  *	basis.
754  */
755 PLAN *
756 c_follow(OPTION *option, char ***argvp __unused)
757 {
758 	ftsoptions &= ~FTS_PHYSICAL;
759 	ftsoptions |= FTS_LOGICAL;
760 
761 	return palloc(option);
762 }
763 
764 /*
765  * -fstype functions --
766  *
767  *	True if the file is of a certain type.
768  */
769 int
770 f_fstype(PLAN *plan, FTSENT *entry)
771 {
772 	static dev_t curdev;	/* need a guaranteed illegal dev value */
773 	static int first = 1;
774 	struct statfs sb;
775 	static int val_type, val_flags;
776 	char *p, save[2] = {0,0};
777 
778 	if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
779 		return 0;
780 
781 	/* Only check when we cross mount point. */
782 	if (first || curdev != entry->fts_statp->st_dev) {
783 		curdev = entry->fts_statp->st_dev;
784 
785 		/*
786 		 * Statfs follows symlinks; find wants the link's filesystem,
787 		 * not where it points.
788 		 */
789 		if (entry->fts_info == FTS_SL ||
790 		    entry->fts_info == FTS_SLNONE) {
791 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
792 				++p;
793 			else
794 				p = entry->fts_accpath;
795 			save[0] = p[0];
796 			p[0] = '.';
797 			save[1] = p[1];
798 			p[1] = '\0';
799 		} else
800 			p = NULL;
801 
802 		if (statfs(entry->fts_accpath, &sb)) {
803 			warn("%s", entry->fts_accpath);
804 			return 0;
805 		}
806 
807 		if (p) {
808 			p[0] = save[0];
809 			p[1] = save[1];
810 		}
811 
812 		first = 0;
813 
814 		/*
815 		 * Further tests may need both of these values, so
816 		 * always copy both of them.
817 		 */
818 		val_flags = sb.f_flags;
819 		val_type = sb.f_type;
820 	}
821 	switch (plan->flags & F_MTMASK) {
822 	case F_MTFLAG:
823 		return val_flags & plan->mt_data;
824 	case F_MTTYPE:
825 		return val_type == plan->mt_data;
826 	default:
827 		abort();
828 	}
829 }
830 
831 PLAN *
832 c_fstype(OPTION *option, char ***argvp)
833 {
834 	char *fsname;
835 	PLAN *new;
836 	struct vfsconf vfc;
837 
838 	fsname = nextarg(option, argvp);
839 	ftsoptions &= ~FTS_NOSTAT;
840 
841 	new = palloc(option);
842 
843 	/*
844 	 * Check first for a filesystem name.
845 	 */
846 	if (getvfsbyname(fsname, &vfc) == 0) {
847 		new->flags |= F_MTTYPE;
848 		new->mt_data = vfc.vfc_typenum;
849 		return new;
850 	}
851 
852 	switch (*fsname) {
853 	case 'l':
854 		if (!strcmp(fsname, "local")) {
855 			new->flags |= F_MTFLAG;
856 			new->mt_data = MNT_LOCAL;
857 			return new;
858 		}
859 		break;
860 	case 'r':
861 		if (!strcmp(fsname, "rdonly")) {
862 			new->flags |= F_MTFLAG;
863 			new->mt_data = MNT_RDONLY;
864 			return new;
865 		}
866 		break;
867 	}
868 
869 	/*
870 	 * We need to make filesystem checks for filesystems
871 	 * that exists but aren't in the kernel work.
872 	 */
873 	fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname);
874 	new->flags |= F_MTUNKNOWN;
875 	return new;
876 }
877 
878 /*
879  * -group gname functions --
880  *
881  *	True if the file belongs to the group gname.  If gname is numeric and
882  *	an equivalent of the getgrnam() function does not return a valid group
883  *	name, gname is taken as a group ID.
884  */
885 int
886 f_group(PLAN *plan, FTSENT *entry)
887 {
888 	COMPARE(entry->fts_statp->st_gid, plan->g_data);
889 }
890 
891 PLAN *
892 c_group(OPTION *option, char ***argvp)
893 {
894 	char *gname;
895 	PLAN *new;
896 	struct group *g;
897 	gid_t gid;
898 
899 	gname = nextarg(option, argvp);
900 	ftsoptions &= ~FTS_NOSTAT;
901 
902 	new = palloc(option);
903 	g = getgrnam(gname);
904 	if (g == NULL) {
905 		char* cp = gname;
906 		if (gname[0] == '-' || gname[0] == '+')
907 			gname++;
908 		gid = atoi(gname);
909 		if (gid == 0 && gname[0] != '0')
910 			errx(1, "%s: %s: no such group", option->name, gname);
911 		gid = find_parsenum(new, option->name, cp, NULL);
912 	} else
913 		gid = g->gr_gid;
914 
915 	new->g_data = gid;
916 	return new;
917 }
918 
919 /*
920  * -inum n functions --
921  *
922  *	True if the file has inode # n.
923  */
924 int
925 f_inum(PLAN *plan, FTSENT *entry)
926 {
927 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
928 }
929 
930 PLAN *
931 c_inum(OPTION *option, char ***argvp)
932 {
933 	char *inum_str;
934 	PLAN *new;
935 
936 	inum_str = nextarg(option, argvp);
937 	ftsoptions &= ~FTS_NOSTAT;
938 
939 	new = palloc(option);
940 	new->i_data = find_parsenum(new, option->name, inum_str, NULL);
941 	return new;
942 }
943 
944 /*
945  * -samefile FN
946  *
947  *	True if the file has the same inode (eg hard link) FN
948  */
949 
950 /* f_samefile is just f_inum */
951 PLAN *
952 c_samefile(OPTION *option, char ***argvp)
953 {
954 	char *fn;
955 	PLAN *new;
956 	struct stat sb;
957 
958 	fn = nextarg(option, argvp);
959 	ftsoptions &= ~FTS_NOSTAT;
960 
961 	new = palloc(option);
962 	if (stat(fn, &sb))
963 		err(1, "%s", fn);
964 	new->i_data = sb.st_ino;
965 	return new;
966 }
967 
968 /*
969  * -links n functions --
970  *
971  *	True if the file has n links.
972  */
973 int
974 f_links(PLAN *plan, FTSENT *entry)
975 {
976 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
977 }
978 
979 PLAN *
980 c_links(OPTION *option, char ***argvp)
981 {
982 	char *nlinks;
983 	PLAN *new;
984 
985 	nlinks = nextarg(option, argvp);
986 	ftsoptions &= ~FTS_NOSTAT;
987 
988 	new = palloc(option);
989 	new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
990 	return new;
991 }
992 
993 /*
994  * -ls functions --
995  *
996  *	Always true - prints the current entry to stdout in "ls" format.
997  */
998 int
999 f_ls(PLAN *plan __unused, FTSENT *entry)
1000 {
1001 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1002 	return 1;
1003 }
1004 
1005 PLAN *
1006 c_ls(OPTION *option, char ***argvp __unused)
1007 {
1008 	ftsoptions &= ~FTS_NOSTAT;
1009 	isoutput = 1;
1010 
1011 	return palloc(option);
1012 }
1013 
1014 /*
1015  * -name functions --
1016  *
1017  *	True if the basename of the filename being examined
1018  *	matches pattern using Pattern Matching Notation S3.14
1019  */
1020 int
1021 f_name(PLAN *plan, FTSENT *entry)
1022 {
1023 	char fn[PATH_MAX];
1024 	const char *name;
1025 
1026 	if (plan->flags & F_LINK) {
1027 		name = fn;
1028 		if (readlink(entry->fts_path, fn, sizeof(fn)) == -1)
1029 			return 0;
1030 	} else
1031 		name = entry->fts_name;
1032 	return !fnmatch(plan->c_data, name,
1033 	    plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1034 }
1035 
1036 PLAN *
1037 c_name(OPTION *option, char ***argvp)
1038 {
1039 	char *pattern;
1040 	PLAN *new;
1041 
1042 	pattern = nextarg(option, argvp);
1043 	new = palloc(option);
1044 	new->c_data = pattern;
1045 	return new;
1046 }
1047 
1048 /*
1049  * -newer file functions --
1050  *
1051  *	True if the current file has been modified more recently
1052  *	then the modification time of the file named by the pathname
1053  *	file.
1054  */
1055 int
1056 f_newer(PLAN *plan, FTSENT *entry)
1057 {
1058 	if (plan->flags & F_TIME_C)
1059 		return entry->fts_statp->st_ctime > plan->t_data;
1060 	else if (plan->flags & F_TIME_A)
1061 		return entry->fts_statp->st_atime > plan->t_data;
1062 	else
1063 		return entry->fts_statp->st_mtime > plan->t_data;
1064 }
1065 
1066 PLAN *
1067 c_newer(OPTION *option, char ***argvp)
1068 {
1069 	char *fn_or_tspec;
1070 	PLAN *new;
1071 	struct stat sb;
1072 
1073 	fn_or_tspec = nextarg(option, argvp);
1074 	ftsoptions &= ~FTS_NOSTAT;
1075 
1076 	new = palloc(option);
1077 	/* compare against what */
1078 	if (option->flags & F_TIME2_T) {
1079 		new->t_data = get_date(fn_or_tspec);
1080 		if (new->t_data == (time_t) -1)
1081 			errx(1, "Can't parse date/time: %s", fn_or_tspec);
1082 	} else {
1083 		if (stat(fn_or_tspec, &sb))
1084 			err(1, "%s", fn_or_tspec);
1085 		if (option->flags & F_TIME2_C)
1086 			new->t_data = sb.st_ctime;
1087 		else if (option->flags & F_TIME2_A)
1088 			new->t_data = sb.st_atime;
1089 		else
1090 			new->t_data = sb.st_mtime;
1091 	}
1092 	return new;
1093 }
1094 
1095 /*
1096  * -nogroup functions --
1097  *
1098  *	True if file belongs to a user ID for which the equivalent
1099  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1100  */
1101 int
1102 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1103 {
1104 	return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1105 }
1106 
1107 PLAN *
1108 c_nogroup(OPTION *option, char ***argvp __unused)
1109 {
1110 	ftsoptions &= ~FTS_NOSTAT;
1111 
1112 	return palloc(option);
1113 }
1114 
1115 /*
1116  * -nouser functions --
1117  *
1118  *	True if file belongs to a user ID for which the equivalent
1119  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1120  */
1121 int
1122 f_nouser(PLAN *plan __unused, FTSENT *entry)
1123 {
1124 	return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1125 }
1126 
1127 PLAN *
1128 c_nouser(OPTION *option, char ***argvp __unused)
1129 {
1130 	ftsoptions &= ~FTS_NOSTAT;
1131 
1132 	return palloc(option);
1133 }
1134 
1135 /*
1136  * -path functions --
1137  *
1138  *	True if the path of the filename being examined
1139  *	matches pattern using Pattern Matching Notation S3.14
1140  */
1141 int
1142 f_path(PLAN *plan, FTSENT *entry)
1143 {
1144 	return !fnmatch(plan->c_data, entry->fts_path,
1145 	    plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1146 }
1147 
1148 /* c_path is the same as c_name */
1149 
1150 /*
1151  * -perm functions --
1152  *
1153  *	The mode argument is used to represent file mode bits.  If it starts
1154  *	with a leading digit, it's treated as an octal mode, otherwise as a
1155  *	symbolic mode.
1156  */
1157 int
1158 f_perm(PLAN *plan, FTSENT *entry)
1159 {
1160 	mode_t mode;
1161 
1162 	mode = entry->fts_statp->st_mode &
1163 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1164 	if (plan->flags & F_ATLEAST)
1165 		return (plan->m_data | mode) == mode;
1166 	else if (plan->flags & F_ANY)
1167 		return (mode & plan->m_data);
1168 	else
1169 		return mode == plan->m_data;
1170 	/* NOTREACHED */
1171 }
1172 
1173 PLAN *
1174 c_perm(OPTION *option, char ***argvp)
1175 {
1176 	char *perm;
1177 	PLAN *new;
1178 	mode_t *set;
1179 
1180 	perm = nextarg(option, argvp);
1181 	ftsoptions &= ~FTS_NOSTAT;
1182 
1183 	new = palloc(option);
1184 
1185 	if (*perm == '-') {
1186 		new->flags |= F_ATLEAST;
1187 		++perm;
1188 	} else if (*perm == '+') {
1189 		new->flags |= F_ANY;
1190 		++perm;
1191 	}
1192 
1193 	if ((set = setmode(perm)) == NULL)
1194 		errx(1, "%s: %s: illegal mode string", option->name, perm);
1195 
1196 	new->m_data = getmode(set, 0);
1197 	free(set);
1198 	return new;
1199 }
1200 
1201 /*
1202  * -print functions --
1203  *
1204  *	Always true, causes the current pathname to be written to
1205  *	standard output.
1206  */
1207 int
1208 f_print(PLAN *plan __unused, FTSENT *entry)
1209 {
1210 	puts(entry->fts_path);
1211 	return 1;
1212 }
1213 
1214 PLAN *
1215 c_print(OPTION *option, char ***argvp __unused)
1216 {
1217 	isoutput = 1;
1218 
1219 	return palloc(option);
1220 }
1221 
1222 /*
1223  * -print0 functions --
1224  *
1225  *	Always true, causes the current pathname to be written to
1226  *	standard output followed by a NUL character
1227  */
1228 int
1229 f_print0(PLAN *plan __unused, FTSENT *entry)
1230 {
1231 	fputs(entry->fts_path, stdout);
1232 	fputc('\0', stdout);
1233 	return 1;
1234 }
1235 
1236 /* c_print0 is the same as c_print */
1237 
1238 /*
1239  * -prune functions --
1240  *
1241  *	Prune a portion of the hierarchy.
1242  */
1243 int
1244 f_prune(PLAN *plan __unused, FTSENT *entry)
1245 {
1246 	if (fts_set(tree, entry, FTS_SKIP))
1247 		err(1, "%s", entry->fts_path);
1248 	return 1;
1249 }
1250 
1251 /* c_prune == c_simple */
1252 
1253 /*
1254  * -regex functions --
1255  *
1256  *	True if the whole path of the file matches pattern using
1257  *	regular expression.
1258  */
1259 int
1260 f_regex(PLAN *plan, FTSENT *entry)
1261 {
1262 	char *str;
1263 	int len;
1264 	regex_t *pre;
1265 	regmatch_t pmatch;
1266 	int errcode;
1267 	char errbuf[LINE_MAX];
1268 	int matched;
1269 
1270 	pre = plan->re_data;
1271 	str = entry->fts_path;
1272 	len = strlen(str);
1273 	matched = 0;
1274 
1275 	pmatch.rm_so = 0;
1276 	pmatch.rm_eo = len;
1277 
1278 	errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1279 
1280 	if (errcode != 0 && errcode != REG_NOMATCH) {
1281 		regerror(errcode, pre, errbuf, sizeof errbuf);
1282 		errx(1, "%s: %s",
1283 		     plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1284 	}
1285 
1286 	if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1287 		matched = 1;
1288 
1289 	return matched;
1290 }
1291 
1292 PLAN *
1293 c_regex(OPTION *option, char ***argvp)
1294 {
1295 	PLAN *new;
1296 	char *pattern;
1297 	regex_t *pre;
1298 	int errcode;
1299 	char errbuf[LINE_MAX];
1300 
1301 	if ((pre = malloc(sizeof(regex_t))) == NULL)
1302 		err(1, NULL);
1303 
1304 	pattern = nextarg(option, argvp);
1305 
1306 	if ((errcode = regcomp(pre, pattern,
1307 	    regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1308 		regerror(errcode, pre, errbuf, sizeof errbuf);
1309 		errx(1, "%s: %s: %s",
1310 		     option->flags & F_IGNCASE ? "-iregex" : "-regex",
1311 		     pattern, errbuf);
1312 	}
1313 
1314 	new = palloc(option);
1315 	new->re_data = pre;
1316 
1317 	return new;
1318 }
1319 
1320 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1321 
1322 PLAN *
1323 c_simple(OPTION *option, char ***argvp __unused)
1324 {
1325 	return palloc(option);
1326 }
1327 
1328 /*
1329  * -size n[c] functions --
1330  *
1331  *	True if the file size in bytes, divided by an implementation defined
1332  *	value and rounded up to the next integer, is n.  If n is followed by
1333  *      one of c k M G T P, the size is in bytes, kilobytes,
1334  *      megabytes, gigabytes, terabytes or petabytes respectively.
1335  */
1336 #define	FIND_SIZE	512
1337 static int divsize = 1;
1338 
1339 int
1340 f_size(PLAN *plan, FTSENT *entry)
1341 {
1342 	off_t size;
1343 
1344 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1345 	    FIND_SIZE : entry->fts_statp->st_size;
1346 	COMPARE(size, plan->o_data);
1347 }
1348 
1349 PLAN *
1350 c_size(OPTION *option, char ***argvp)
1351 {
1352 	char *size_str;
1353 	PLAN *new;
1354 	char endch;
1355 	off_t scale;
1356 
1357 	size_str = nextarg(option, argvp);
1358 	ftsoptions &= ~FTS_NOSTAT;
1359 
1360 	new = palloc(option);
1361 	endch = 'c';
1362 	new->o_data = find_parsenum(new, option->name, size_str, &endch);
1363 	if (endch != '\0') {
1364 		divsize = 0;
1365 
1366 		switch (endch) {
1367 		case 'c':                       /* characters */
1368 			scale = 0x1LL;
1369 			break;
1370 		case 'k':                       /* kilobytes 1<<10 */
1371 			scale = 0x400LL;
1372 			break;
1373 		case 'M':                       /* megabytes 1<<20 */
1374 			scale = 0x100000LL;
1375 			break;
1376 		case 'G':                       /* gigabytes 1<<30 */
1377 			scale = 0x40000000LL;
1378 			break;
1379 		case 'T':                       /* terabytes 1<<40 */
1380 			scale = 0x1000000000LL;
1381 			break;
1382 		case 'P':                       /* petabytes 1<<50 */
1383 			scale = 0x4000000000000LL;
1384 			break;
1385 		default:
1386 			errx(1, "%s: %s: illegal trailing character",
1387 				option->name, size_str);
1388 			break;
1389 		}
1390 		if (new->o_data > QUAD_MAX / scale)
1391 			errx(1, "%s: %s: value too large",
1392 				option->name, size_str);
1393 		new->o_data *= scale;
1394 	}
1395 	return new;
1396 }
1397 
1398 /*
1399  * -type c functions --
1400  *
1401  *	True if the type of the file is c, where c is b, c, d, p, f or w
1402  *	for block special file, character special file, directory, FIFO,
1403  *	regular file or whiteout respectively.
1404  */
1405 int
1406 f_type(PLAN *plan, FTSENT *entry)
1407 {
1408 	return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1409 }
1410 
1411 PLAN *
1412 c_type(OPTION *option, char ***argvp)
1413 {
1414 	char *typestring;
1415 	PLAN *new;
1416 	mode_t  mask;
1417 
1418 	typestring = nextarg(option, argvp);
1419 	ftsoptions &= ~FTS_NOSTAT;
1420 
1421 	switch (typestring[0]) {
1422 	case 'b':
1423 		mask = S_IFBLK;
1424 		break;
1425 	case 'c':
1426 		mask = S_IFCHR;
1427 		break;
1428 	case 'd':
1429 		mask = S_IFDIR;
1430 		break;
1431 	case 'f':
1432 		mask = S_IFREG;
1433 		break;
1434 	case 'l':
1435 		mask = S_IFLNK;
1436 		break;
1437 	case 'p':
1438 		mask = S_IFIFO;
1439 		break;
1440 	case 's':
1441 		mask = S_IFSOCK;
1442 		break;
1443 #ifdef FTS_WHITEOUT
1444 	case 'w':
1445 		mask = S_IFWHT;
1446 		ftsoptions |= FTS_WHITEOUT;
1447 		break;
1448 #endif /* FTS_WHITEOUT */
1449 	default:
1450 		errx(1, "%s: %s: unknown type", option->name, typestring);
1451 	}
1452 
1453 	new = palloc(option);
1454 	new->m_data = mask;
1455 	return new;
1456 }
1457 
1458 /*
1459  * -user uname functions --
1460  *
1461  *	True if the file belongs to the user uname.  If uname is numeric and
1462  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1463  *	return a valid user name, uname is taken as a user ID.
1464  */
1465 int
1466 f_user(PLAN *plan, FTSENT *entry)
1467 {
1468 	COMPARE(entry->fts_statp->st_uid, plan->u_data);
1469 }
1470 
1471 PLAN *
1472 c_user(OPTION *option, char ***argvp)
1473 {
1474 	char *username;
1475 	PLAN *new;
1476 	struct passwd *p;
1477 	uid_t uid;
1478 
1479 	username = nextarg(option, argvp);
1480 	ftsoptions &= ~FTS_NOSTAT;
1481 
1482 	new = palloc(option);
1483 	p = getpwnam(username);
1484 	if (p == NULL) {
1485 		char* cp = username;
1486 		if( username[0] == '-' || username[0] == '+' )
1487 			username++;
1488 		uid = atoi(username);
1489 		if (uid == 0 && username[0] != '0')
1490 			errx(1, "%s: %s: no such user", option->name, username);
1491 		uid = find_parsenum(new, option->name, cp, NULL);
1492 	} else
1493 		uid = p->pw_uid;
1494 
1495 	new->u_data = uid;
1496 	return new;
1497 }
1498 
1499 /*
1500  * -xdev functions --
1501  *
1502  *	Always true, causes find not to descend past directories that have a
1503  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1504  */
1505 PLAN *
1506 c_xdev(OPTION *option, char ***argvp __unused)
1507 {
1508 	ftsoptions |= FTS_XDEV;
1509 
1510 	return palloc(option);
1511 }
1512 
1513 /*
1514  * ( expression ) functions --
1515  *
1516  *	True if expression is true.
1517  */
1518 int
1519 f_expr(PLAN *plan, FTSENT *entry)
1520 {
1521 	PLAN *p;
1522 	int state = 0;
1523 
1524 	for (p = plan->p_data[0];
1525 	    p && (state = (p->execute)(p, entry)); p = p->next);
1526 	return state;
1527 }
1528 
1529 /*
1530  * f_openparen and f_closeparen nodes are temporary place markers.  They are
1531  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1532  * to a f_expr node containing the expression and the ')' node is discarded.
1533  * The functions themselves are only used as constants.
1534  */
1535 
1536 int
1537 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1538 {
1539 	abort();
1540 }
1541 
1542 int
1543 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1544 {
1545 	abort();
1546 }
1547 
1548 /* c_openparen == c_simple */
1549 /* c_closeparen == c_simple */
1550 
1551 /*
1552  * AND operator. Since AND is implicit, no node is allocated.
1553  */
1554 PLAN *
1555 c_and(OPTION *option __unused, char ***argvp __unused)
1556 {
1557 	return NULL;
1558 }
1559 
1560 /*
1561  * ! expression functions --
1562  *
1563  *	Negation of a primary; the unary NOT operator.
1564  */
1565 int
1566 f_not(PLAN *plan, FTSENT *entry)
1567 {
1568 	PLAN *p;
1569 	int state = 0;
1570 
1571 	for (p = plan->p_data[0];
1572 	    p && (state = (p->execute)(p, entry)); p = p->next);
1573 	return !state;
1574 }
1575 
1576 /* c_not == c_simple */
1577 
1578 /*
1579  * expression -o expression functions --
1580  *
1581  *	Alternation of primaries; the OR operator.  The second expression is
1582  * not evaluated if the first expression is true.
1583  */
1584 int
1585 f_or(PLAN *plan, FTSENT *entry)
1586 {
1587 	PLAN *p;
1588 	int state = 0;
1589 
1590 	for (p = plan->p_data[0];
1591 	    p && (state = (p->execute)(p, entry)); p = p->next);
1592 
1593 	if (state)
1594 		return 1;
1595 
1596 	for (p = plan->p_data[1];
1597 	    p && (state = (p->execute)(p, entry)); p = p->next);
1598 	return state;
1599 }
1600 
1601 /* c_or == c_simple */
1602 
1603 /*
1604  * -false
1605  *
1606  *	Always false.
1607  */
1608 int
1609 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1610 {
1611 	return 0;
1612 }
1613 
1614 /* c_false == c_simple */
1615 
1616 /*
1617  * -quit
1618  *
1619  *	Exits the program
1620  */
1621 int
1622 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1623 {
1624 	exit(0);
1625 }
1626 
1627 /* c_quit == c_simple */
1628