82fb4ce28d199dd98ea2c61f0ce0dd67dd927b2b
[dragonfly.git] / usr.bin / find / function.c
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  * 4. 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.71 2011/06/13 05:22:07 avatar 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_flags;
776         static char fstype[sizeof(sb.f_fstypename)];
777         char *p, save[2] = {0,0};
778
779         if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
780                 return 0;
781
782         /* Only check when we cross mount point. */
783         if (first || curdev != entry->fts_statp->st_dev) {
784                 curdev = entry->fts_statp->st_dev;
785
786                 /*
787                  * Statfs follows symlinks; find wants the link's filesystem,
788                  * not where it points.
789                  */
790                 if (entry->fts_info == FTS_SL ||
791                     entry->fts_info == FTS_SLNONE) {
792                         if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
793                                 ++p;
794                         else
795                                 p = entry->fts_accpath;
796                         save[0] = p[0];
797                         p[0] = '.';
798                         save[1] = p[1];
799                         p[1] = '\0';
800                 } else
801                         p = NULL;
802
803                 if (statfs(entry->fts_accpath, &sb))
804                         err(1, "%s", entry->fts_accpath);
805
806                 if (p) {
807                         p[0] = save[0];
808                         p[1] = save[1];
809                 }
810
811                 first = 0;
812
813                 /*
814                  * Further tests may need both of these values, so
815                  * always copy both of them.
816                  */
817                 val_flags = sb.f_flags;
818                 strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
819         }
820         switch (plan->flags & F_MTMASK) {
821         case F_MTFLAG:
822                 return val_flags & plan->mt_data;
823         case F_MTTYPE:
824                 return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
825         default:
826                 abort();
827         }
828 }
829
830 PLAN *
831 c_fstype(OPTION *option, char ***argvp)
832 {
833         char *fsname;
834         PLAN *new;
835
836         fsname = nextarg(option, argvp);
837         ftsoptions &= ~FTS_NOSTAT;
838
839         new = palloc(option);
840         switch (*fsname) {
841         case 'l':
842                 if (!strcmp(fsname, "local")) {
843                         new->flags |= F_MTFLAG;
844                         new->mt_data = MNT_LOCAL;
845                         return new;
846                 }
847                 break;
848         case 'r':
849                 if (!strcmp(fsname, "rdonly")) {
850                         new->flags |= F_MTFLAG;
851                         new->mt_data = MNT_RDONLY;
852                         return new;
853                 }
854                 break;
855         }
856
857         new->flags |= F_MTTYPE;
858         new->c_data = fsname;
859         return new;
860 }
861
862 /*
863  * -group gname functions --
864  *
865  *      True if the file belongs to the group gname.  If gname is numeric and
866  *      an equivalent of the getgrnam() function does not return a valid group
867  *      name, gname is taken as a group ID.
868  */
869 int
870 f_group(PLAN *plan, FTSENT *entry)
871 {
872         COMPARE(entry->fts_statp->st_gid, plan->g_data);
873 }
874
875 PLAN *
876 c_group(OPTION *option, char ***argvp)
877 {
878         char *gname;
879         PLAN *new;
880         struct group *g;
881         gid_t gid;
882
883         gname = nextarg(option, argvp);
884         ftsoptions &= ~FTS_NOSTAT;
885
886         new = palloc(option);
887         g = getgrnam(gname);
888         if (g == NULL) {
889                 char* cp = gname;
890                 if (gname[0] == '-' || gname[0] == '+')
891                         gname++;
892                 gid = atoi(gname);
893                 if (gid == 0 && gname[0] != '0')
894                         errx(1, "%s: %s: no such group", option->name, gname);
895                 gid = find_parsenum(new, option->name, cp, NULL);
896         } else
897                 gid = g->gr_gid;
898
899         new->g_data = gid;
900         return new;
901 }
902
903 /*
904  * -inum n functions --
905  *
906  *      True if the file has inode # n.
907  */
908 int
909 f_inum(PLAN *plan, FTSENT *entry)
910 {
911         COMPARE(entry->fts_statp->st_ino, plan->i_data);
912 }
913
914 PLAN *
915 c_inum(OPTION *option, char ***argvp)
916 {
917         char *inum_str;
918         PLAN *new;
919
920         inum_str = nextarg(option, argvp);
921         ftsoptions &= ~FTS_NOSTAT;
922
923         new = palloc(option);
924         new->i_data = find_parsenum(new, option->name, inum_str, NULL);
925         return new;
926 }
927
928 /*
929  * -samefile FN
930  *
931  *      True if the file has the same inode (eg hard link) FN
932  */
933
934 /* f_samefile is just f_inum */
935 PLAN *
936 c_samefile(OPTION *option, char ***argvp)
937 {
938         char *fn;
939         PLAN *new;
940         struct stat sb;
941
942         fn = nextarg(option, argvp);
943         ftsoptions &= ~FTS_NOSTAT;
944
945         new = palloc(option);
946         if (stat(fn, &sb))
947                 err(1, "%s", fn);
948         new->i_data = sb.st_ino;
949         return new;
950 }
951
952 /*
953  * -links n functions --
954  *
955  *      True if the file has n links.
956  */
957 int
958 f_links(PLAN *plan, FTSENT *entry)
959 {
960         COMPARE(entry->fts_statp->st_nlink, plan->l_data);
961 }
962
963 PLAN *
964 c_links(OPTION *option, char ***argvp)
965 {
966         char *nlinks;
967         PLAN *new;
968
969         nlinks = nextarg(option, argvp);
970         ftsoptions &= ~FTS_NOSTAT;
971
972         new = palloc(option);
973         new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
974         return new;
975 }
976
977 /*
978  * -ls functions --
979  *
980  *      Always true - prints the current entry to stdout in "ls" format.
981  */
982 int
983 f_ls(PLAN *plan __unused, FTSENT *entry)
984 {
985         printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
986         return 1;
987 }
988
989 PLAN *
990 c_ls(OPTION *option, char ***argvp __unused)
991 {
992         ftsoptions &= ~FTS_NOSTAT;
993         isoutput = 1;
994
995         return palloc(option);
996 }
997
998 /*
999  * -name functions --
1000  *
1001  *      True if the basename of the filename being examined
1002  *      matches pattern using Pattern Matching Notation S3.14
1003  */
1004 int
1005 f_name(PLAN *plan, FTSENT *entry)
1006 {
1007         char fn[PATH_MAX];
1008         const char *name;
1009
1010         if (plan->flags & F_LINK) {
1011                 name = fn;
1012                 if (readlink(entry->fts_path, fn, sizeof(fn)) == -1)
1013                         return 0;
1014         } else
1015                 name = entry->fts_name;
1016         return !fnmatch(plan->c_data, name,
1017             plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1018 }
1019
1020 PLAN *
1021 c_name(OPTION *option, char ***argvp)
1022 {
1023         char *pattern;
1024         PLAN *new;
1025
1026         pattern = nextarg(option, argvp);
1027         new = palloc(option);
1028         new->c_data = pattern;
1029         return new;
1030 }
1031
1032 /*
1033  * -newer file functions --
1034  *
1035  *      True if the current file has been modified more recently
1036  *      then the modification time of the file named by the pathname
1037  *      file.
1038  */
1039 int
1040 f_newer(PLAN *plan, FTSENT *entry)
1041 {
1042         if (plan->flags & F_TIME_C)
1043                 return entry->fts_statp->st_ctime > plan->t_data;
1044         else if (plan->flags & F_TIME_A)
1045                 return entry->fts_statp->st_atime > plan->t_data;
1046         else
1047                 return entry->fts_statp->st_mtime > plan->t_data;
1048 }
1049
1050 PLAN *
1051 c_newer(OPTION *option, char ***argvp)
1052 {
1053         char *fn_or_tspec;
1054         PLAN *new;
1055         struct stat sb;
1056
1057         fn_or_tspec = nextarg(option, argvp);
1058         ftsoptions &= ~FTS_NOSTAT;
1059
1060         new = palloc(option);
1061         /* compare against what */
1062         if (option->flags & F_TIME2_T) {
1063                 new->t_data = get_date(fn_or_tspec);
1064                 if (new->t_data == (time_t) -1)
1065                         errx(1, "Can't parse date/time: %s", fn_or_tspec);
1066         } else {
1067                 if (stat(fn_or_tspec, &sb))
1068                         err(1, "%s", fn_or_tspec);
1069                 if (option->flags & F_TIME2_C)
1070                         new->t_data = sb.st_ctime;
1071                 else if (option->flags & F_TIME2_A)
1072                         new->t_data = sb.st_atime;
1073                 else
1074                         new->t_data = sb.st_mtime;
1075         }
1076         return new;
1077 }
1078
1079 /*
1080  * -nogroup functions --
1081  *
1082  *      True if file belongs to a user ID for which the equivalent
1083  *      of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1084  */
1085 int
1086 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1087 {
1088         return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1089 }
1090
1091 PLAN *
1092 c_nogroup(OPTION *option, char ***argvp __unused)
1093 {
1094         ftsoptions &= ~FTS_NOSTAT;
1095
1096         return palloc(option);
1097 }
1098
1099 /*
1100  * -nouser functions --
1101  *
1102  *      True if file belongs to a user ID for which the equivalent
1103  *      of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1104  */
1105 int
1106 f_nouser(PLAN *plan __unused, FTSENT *entry)
1107 {
1108         return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1109 }
1110
1111 PLAN *
1112 c_nouser(OPTION *option, char ***argvp __unused)
1113 {
1114         ftsoptions &= ~FTS_NOSTAT;
1115
1116         return palloc(option);
1117 }
1118
1119 /*
1120  * -path functions --
1121  *
1122  *      True if the path of the filename being examined
1123  *      matches pattern using Pattern Matching Notation S3.14
1124  */
1125 int
1126 f_path(PLAN *plan, FTSENT *entry)
1127 {
1128         return !fnmatch(plan->c_data, entry->fts_path,
1129             plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1130 }
1131
1132 /* c_path is the same as c_name */
1133
1134 /*
1135  * -perm functions --
1136  *
1137  *      The mode argument is used to represent file mode bits.  If it starts
1138  *      with a leading digit, it's treated as an octal mode, otherwise as a
1139  *      symbolic mode.
1140  */
1141 int
1142 f_perm(PLAN *plan, FTSENT *entry)
1143 {
1144         mode_t mode;
1145
1146         mode = entry->fts_statp->st_mode &
1147             (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1148         if (plan->flags & F_ATLEAST)
1149                 return (plan->m_data | mode) == mode;
1150         else if (plan->flags & F_ANY)
1151                 return (mode & plan->m_data);
1152         else
1153                 return mode == plan->m_data;
1154         /* NOTREACHED */
1155 }
1156
1157 PLAN *
1158 c_perm(OPTION *option, char ***argvp)
1159 {
1160         char *perm;
1161         PLAN *new;
1162         mode_t *set;
1163
1164         perm = nextarg(option, argvp);
1165         ftsoptions &= ~FTS_NOSTAT;
1166
1167         new = palloc(option);
1168
1169         if (*perm == '-') {
1170                 new->flags |= F_ATLEAST;
1171                 ++perm;
1172         } else if (*perm == '+') {
1173                 new->flags |= F_ANY;
1174                 ++perm;
1175         }
1176
1177         if ((set = setmode(perm)) == NULL)
1178                 errx(1, "%s: %s: illegal mode string", option->name, perm);
1179
1180         new->m_data = getmode(set, 0);
1181         free(set);
1182         return new;
1183 }
1184
1185 /*
1186  * -print functions --
1187  *
1188  *      Always true, causes the current pathname to be written to
1189  *      standard output.
1190  */
1191 int
1192 f_print(PLAN *plan __unused, FTSENT *entry)
1193 {
1194         puts(entry->fts_path);
1195         return 1;
1196 }
1197
1198 PLAN *
1199 c_print(OPTION *option, char ***argvp __unused)
1200 {
1201         isoutput = 1;
1202
1203         return palloc(option);
1204 }
1205
1206 /*
1207  * -print0 functions --
1208  *
1209  *      Always true, causes the current pathname to be written to
1210  *      standard output followed by a NUL character
1211  */
1212 int
1213 f_print0(PLAN *plan __unused, FTSENT *entry)
1214 {
1215         fputs(entry->fts_path, stdout);
1216         fputc('\0', stdout);
1217         return 1;
1218 }
1219
1220 /* c_print0 is the same as c_print */
1221
1222 /*
1223  * -prune functions --
1224  *
1225  *      Prune a portion of the hierarchy.
1226  */
1227 int
1228 f_prune(PLAN *plan __unused, FTSENT *entry)
1229 {
1230         if (fts_set(tree, entry, FTS_SKIP))
1231                 err(1, "%s", entry->fts_path);
1232         return 1;
1233 }
1234
1235 /* c_prune == c_simple */
1236
1237 /*
1238  * -regex functions --
1239  *
1240  *      True if the whole path of the file matches pattern using
1241  *      regular expression.
1242  */
1243 int
1244 f_regex(PLAN *plan, FTSENT *entry)
1245 {
1246         char *str;
1247         int len;
1248         regex_t *pre;
1249         regmatch_t pmatch;
1250         int errcode;
1251         char errbuf[LINE_MAX];
1252         int matched;
1253
1254         pre = plan->re_data;
1255         str = entry->fts_path;
1256         len = strlen(str);
1257         matched = 0;
1258
1259         pmatch.rm_so = 0;
1260         pmatch.rm_eo = len;
1261
1262         errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1263
1264         if (errcode != 0 && errcode != REG_NOMATCH) {
1265                 regerror(errcode, pre, errbuf, sizeof errbuf);
1266                 errx(1, "%s: %s",
1267                      plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1268         }
1269
1270         if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1271                 matched = 1;
1272
1273         return matched;
1274 }
1275
1276 PLAN *
1277 c_regex(OPTION *option, char ***argvp)
1278 {
1279         PLAN *new;
1280         char *pattern;
1281         regex_t *pre;
1282         int errcode;
1283         char errbuf[LINE_MAX];
1284
1285         if ((pre = malloc(sizeof(regex_t))) == NULL)
1286                 err(1, NULL);
1287
1288         pattern = nextarg(option, argvp);
1289
1290         if ((errcode = regcomp(pre, pattern,
1291             regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1292                 regerror(errcode, pre, errbuf, sizeof errbuf);
1293                 errx(1, "%s: %s: %s",
1294                      option->flags & F_IGNCASE ? "-iregex" : "-regex",
1295                      pattern, errbuf);
1296         }
1297
1298         new = palloc(option);
1299         new->re_data = pre;
1300
1301         return new;
1302 }
1303
1304 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1305
1306 PLAN *
1307 c_simple(OPTION *option, char ***argvp __unused)
1308 {
1309         return palloc(option);
1310 }
1311
1312 /*
1313  * -size n[c] functions --
1314  *
1315  *      True if the file size in bytes, divided by an implementation defined
1316  *      value and rounded up to the next integer, is n.  If n is followed by
1317  *      one of c k M G T P, the size is in bytes, kilobytes,
1318  *      megabytes, gigabytes, terabytes or petabytes respectively.
1319  */
1320 #define FIND_SIZE       512
1321 static int divsize = 1;
1322
1323 int
1324 f_size(PLAN *plan, FTSENT *entry)
1325 {
1326         off_t size;
1327
1328         size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1329             FIND_SIZE : entry->fts_statp->st_size;
1330         COMPARE(size, plan->o_data);
1331 }
1332
1333 PLAN *
1334 c_size(OPTION *option, char ***argvp)
1335 {
1336         char *size_str;
1337         PLAN *new;
1338         char endch;
1339         off_t scale;
1340
1341         size_str = nextarg(option, argvp);
1342         ftsoptions &= ~FTS_NOSTAT;
1343
1344         new = palloc(option);
1345         endch = 'c';
1346         new->o_data = find_parsenum(new, option->name, size_str, &endch);
1347         if (endch != '\0') {
1348                 divsize = 0;
1349
1350                 switch (endch) {
1351                 case 'c':                       /* characters */
1352                         scale = 0x1LL;
1353                         break;
1354                 case 'k':                       /* kilobytes 1<<10 */
1355                         scale = 0x400LL;
1356                         break;
1357                 case 'M':                       /* megabytes 1<<20 */
1358                         scale = 0x100000LL;
1359                         break;
1360                 case 'G':                       /* gigabytes 1<<30 */
1361                         scale = 0x40000000LL;
1362                         break;
1363                 case 'T':                       /* terabytes 1<<40 */
1364                         scale = 0x1000000000LL;
1365                         break;
1366                 case 'P':                       /* petabytes 1<<50 */
1367                         scale = 0x4000000000000LL;
1368                         break;
1369                 default:
1370                         errx(1, "%s: %s: illegal trailing character",
1371                                 option->name, size_str);
1372                         break;
1373                 }
1374                 if (new->o_data > QUAD_MAX / scale)
1375                         errx(1, "%s: %s: value too large",
1376                                 option->name, size_str);
1377                 new->o_data *= scale;
1378         }
1379         return new;
1380 }
1381
1382 /*
1383  * -type c functions --
1384  *
1385  *      True if the type of the file is c, where c is b, c, d, p, f or w
1386  *      for block special file, character special file, directory, FIFO,
1387  *      regular file or whiteout respectively.
1388  */
1389 int
1390 f_type(PLAN *plan, FTSENT *entry)
1391 {
1392         return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1393 }
1394
1395 PLAN *
1396 c_type(OPTION *option, char ***argvp)
1397 {
1398         char *typestring;
1399         PLAN *new;
1400         mode_t  mask;
1401
1402         typestring = nextarg(option, argvp);
1403         ftsoptions &= ~FTS_NOSTAT;
1404
1405         switch (typestring[0]) {
1406         case 'b':
1407                 mask = S_IFBLK;
1408                 break;
1409         case 'c':
1410                 mask = S_IFCHR;
1411                 break;
1412         case 'd':
1413                 mask = S_IFDIR;
1414                 break;
1415         case 'f':
1416                 mask = S_IFREG;
1417                 break;
1418         case 'l':
1419                 mask = S_IFLNK;
1420                 break;
1421         case 'p':
1422                 mask = S_IFIFO;
1423                 break;
1424         case 's':
1425                 mask = S_IFSOCK;
1426                 break;
1427 #ifdef FTS_WHITEOUT
1428         case 'w':
1429                 mask = S_IFWHT;
1430                 ftsoptions |= FTS_WHITEOUT;
1431                 break;
1432 #endif /* FTS_WHITEOUT */
1433         default:
1434                 errx(1, "%s: %s: unknown type", option->name, typestring);
1435         }
1436
1437         new = palloc(option);
1438         new->m_data = mask;
1439         return new;
1440 }
1441
1442 /*
1443  * -user uname functions --
1444  *
1445  *      True if the file belongs to the user uname.  If uname is numeric and
1446  *      an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1447  *      return a valid user name, uname is taken as a user ID.
1448  */
1449 int
1450 f_user(PLAN *plan, FTSENT *entry)
1451 {
1452         COMPARE(entry->fts_statp->st_uid, plan->u_data);
1453 }
1454
1455 PLAN *
1456 c_user(OPTION *option, char ***argvp)
1457 {
1458         char *username;
1459         PLAN *new;
1460         struct passwd *p;
1461         uid_t uid;
1462
1463         username = nextarg(option, argvp);
1464         ftsoptions &= ~FTS_NOSTAT;
1465
1466         new = palloc(option);
1467         p = getpwnam(username);
1468         if (p == NULL) {
1469                 char* cp = username;
1470                 if( username[0] == '-' || username[0] == '+' )
1471                         username++;
1472                 uid = atoi(username);
1473                 if (uid == 0 && username[0] != '0')
1474                         errx(1, "%s: %s: no such user", option->name, username);
1475                 uid = find_parsenum(new, option->name, cp, NULL);
1476         } else
1477                 uid = p->pw_uid;
1478
1479         new->u_data = uid;
1480         return new;
1481 }
1482
1483 /*
1484  * -xdev functions --
1485  *
1486  *      Always true, causes find not to descend past directories that have a
1487  *      different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1488  */
1489 PLAN *
1490 c_xdev(OPTION *option, char ***argvp __unused)
1491 {
1492         ftsoptions |= FTS_XDEV;
1493
1494         return palloc(option);
1495 }
1496
1497 /*
1498  * ( expression ) functions --
1499  *
1500  *      True if expression is true.
1501  */
1502 int
1503 f_expr(PLAN *plan, FTSENT *entry)
1504 {
1505         PLAN *p;
1506         int state = 0;
1507
1508         for (p = plan->p_data[0];
1509             p && (state = (p->execute)(p, entry)); p = p->next);
1510         return state;
1511 }
1512
1513 /*
1514  * f_openparen and f_closeparen nodes are temporary place markers.  They are
1515  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1516  * to a f_expr node containing the expression and the ')' node is discarded.
1517  * The functions themselves are only used as constants.
1518  */
1519
1520 int
1521 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1522 {
1523         abort();
1524 }
1525
1526 int
1527 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1528 {
1529         abort();
1530 }
1531
1532 /* c_openparen == c_simple */
1533 /* c_closeparen == c_simple */
1534
1535 /*
1536  * AND operator. Since AND is implicit, no node is allocated.
1537  */
1538 PLAN *
1539 c_and(OPTION *option __unused, char ***argvp __unused)
1540 {
1541         return NULL;
1542 }
1543
1544 /*
1545  * ! expression functions --
1546  *
1547  *      Negation of a primary; the unary NOT operator.
1548  */
1549 int
1550 f_not(PLAN *plan, FTSENT *entry)
1551 {
1552         PLAN *p;
1553         int state = 0;
1554
1555         for (p = plan->p_data[0];
1556             p && (state = (p->execute)(p, entry)); p = p->next);
1557         return !state;
1558 }
1559
1560 /* c_not == c_simple */
1561
1562 /*
1563  * expression -o expression functions --
1564  *
1565  *      Alternation of primaries; the OR operator.  The second expression is
1566  * not evaluated if the first expression is true.
1567  */
1568 int
1569 f_or(PLAN *plan, FTSENT *entry)
1570 {
1571         PLAN *p;
1572         int state = 0;
1573
1574         for (p = plan->p_data[0];
1575             p && (state = (p->execute)(p, entry)); p = p->next);
1576
1577         if (state)
1578                 return 1;
1579
1580         for (p = plan->p_data[1];
1581             p && (state = (p->execute)(p, entry)); p = p->next);
1582         return state;
1583 }
1584
1585 /* c_or == c_simple */
1586
1587 /*
1588  * -false
1589  *
1590  *      Always false.
1591  */
1592 int
1593 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1594 {
1595         return 0;
1596 }
1597
1598 /* c_false == c_simple */
1599
1600 /*
1601  * -quit
1602  *
1603  *      Exits the program
1604  */
1605 int
1606 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1607 {
1608         exit(0);
1609 }
1610
1611 /* c_quit == c_simple */