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