function.c revision 1.51 1 /* $NetBSD: function.c,v 1.51 2005/10/12 20:03:59 reed Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Cimarron D. Taylor of the University of California, Berkeley.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "from: @(#)function.c 8.10 (Berkeley) 5/4/95";
39 #else
40 __RCSID("$NetBSD: function.c,v 1.51 2005/10/12 20:03:59 reed Exp $");
41 #endif
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <sys/wait.h>
47 #include <sys/mount.h>
48
49 #include <dirent.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fnmatch.h>
53 #include <fts.h>
54 #include <grp.h>
55 #include <inttypes.h>
56 #include <pwd.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <tzfile.h>
61 #include <unistd.h>
62
63 #include "find.h"
64 #include "stat_flags.h"
65
66 #define COMPARE(a, b) { \
67 switch (plan->flags) { \
68 case F_EQUAL: \
69 return (a == b); \
70 case F_LESSTHAN: \
71 return (a < b); \
72 case F_GREATER: \
73 return (a > b); \
74 default: \
75 abort(); \
76 } \
77 }
78
79 static int64_t find_parsenum __P((PLAN *, char *, char *, char *));
80 int f_always_true __P((PLAN *, FTSENT *));
81 int f_amin __P((PLAN *, FTSENT *));
82 int f_anewer __P((PLAN *, FTSENT *));
83 int f_atime __P((PLAN *, FTSENT *));
84 int f_cmin __P((PLAN *, FTSENT *));
85 int f_cnewer __P((PLAN *, FTSENT *));
86 int f_ctime __P((PLAN *, FTSENT *));
87 int f_empty __P((PLAN *, FTSENT *));
88 int f_exec __P((PLAN *, FTSENT *));
89 int f_execdir __P((PLAN *, FTSENT *));
90 int f_flags __P((PLAN *, FTSENT *));
91 int f_fprint __P((PLAN *, FTSENT *));
92 int f_fstype __P((PLAN *, FTSENT *));
93 int f_group __P((PLAN *, FTSENT *));
94 int f_iname __P((PLAN *, FTSENT *));
95 int f_inum __P((PLAN *, FTSENT *));
96 int f_links __P((PLAN *, FTSENT *));
97 int f_ls __P((PLAN *, FTSENT *));
98 int f_mindepth __P((PLAN *, FTSENT *));
99 int f_maxdepth __P((PLAN *, FTSENT *));
100 int f_mmin __P((PLAN *, FTSENT *));
101 int f_mtime __P((PLAN *, FTSENT *));
102 int f_name __P((PLAN *, FTSENT *));
103 int f_newer __P((PLAN *, FTSENT *));
104 int f_nogroup __P((PLAN *, FTSENT *));
105 int f_nouser __P((PLAN *, FTSENT *));
106 int f_path __P((PLAN *, FTSENT *));
107 int f_perm __P((PLAN *, FTSENT *));
108 int f_print __P((PLAN *, FTSENT *));
109 int f_print0 __P((PLAN *, FTSENT *));
110 int f_printx __P((PLAN *, FTSENT *));
111 int f_prune __P((PLAN *, FTSENT *));
112 int f_regex __P((PLAN *, FTSENT *));
113 int f_size __P((PLAN *, FTSENT *));
114 int f_type __P((PLAN *, FTSENT *));
115 int f_user __P((PLAN *, FTSENT *));
116 int f_not __P((PLAN *, FTSENT *));
117 int f_or __P((PLAN *, FTSENT *));
118 static PLAN *c_regex_common __P((char ***, int, enum ntype, int));
119 static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
120
121 extern int dotfd;
122 extern FTS *tree;
123 extern time_t now;
124
125 /*
126 * find_parsenum --
127 * Parse a string of the form [+-]# and return the value.
128 */
129 static int64_t
130 find_parsenum(plan, option, vp, endch)
131 PLAN *plan;
132 char *option, *vp, *endch;
133 {
134 int64_t value;
135 char *endchar, *str; /* Pointer to character ending conversion. */
136
137 /* Determine comparison from leading + or -. */
138 str = vp;
139 switch (*str) {
140 case '+':
141 ++str;
142 plan->flags = F_GREATER;
143 break;
144 case '-':
145 ++str;
146 plan->flags = F_LESSTHAN;
147 break;
148 default:
149 plan->flags = F_EQUAL;
150 break;
151 }
152
153 /*
154 * Convert the string with strtol(). Note, if strtol() returns zero
155 * and endchar points to the beginning of the string we know we have
156 * a syntax error.
157 */
158 value = strtoq(str, &endchar, 10);
159 if (value == 0 && endchar == str)
160 errx(1, "%s: %s: illegal numeric value", option, vp);
161 if (endchar[0] && (endch == NULL || endchar[0] != *endch))
162 errx(1, "%s: %s: illegal trailing character", option, vp);
163 if (endch)
164 *endch = endchar[0];
165 return (value);
166 }
167
168 /*
169 * The value of n for the inode times (atime, ctime, and mtime) is a range,
170 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
171 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
172 * user wanted. Correct so that -1 is "less than 1".
173 */
174 #define TIME_CORRECT(p, ttype) \
175 if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \
176 ++((p)->t_data);
177
178 /*
179 * -amin n functions --
180 *
181 * True if the difference between the file access time and the
182 * current time is n 1 minute periods.
183 */
184 int
185 f_amin(plan, entry)
186 PLAN *plan;
187 FTSENT *entry;
188 {
189 COMPARE((now - entry->fts_statp->st_atime +
190 SECSPERMIN - 1) / SECSPERMIN, plan->t_data);
191 }
192
193 PLAN *
194 c_amin(argvp, isok)
195 char ***argvp;
196 int isok;
197 {
198 char *arg = **argvp;
199 PLAN *new;
200
201 (*argvp)++;
202 ftsoptions &= ~FTS_NOSTAT;
203
204 new = palloc(N_AMIN, f_amin);
205 new->t_data = find_parsenum(new, "-amin", arg, NULL);
206 TIME_CORRECT(new, N_AMIN);
207 return (new);
208 }
209
210 /*
211 * -anewer file functions --
212 *
213 * True if the current file has been accessed more recently
214 * than the access time of the file named by the pathname
215 * file.
216 */
217 int
218 f_anewer(plan, entry)
219 PLAN *plan;
220 FTSENT *entry;
221 {
222
223 return (entry->fts_statp->st_atime > plan->t_data);
224 }
225
226 PLAN *
227 c_anewer(argvp, isok)
228 char ***argvp;
229 int isok;
230 {
231 char *filename = **argvp;
232 PLAN *new;
233 struct stat sb;
234
235 (*argvp)++;
236 ftsoptions &= ~FTS_NOSTAT;
237
238 if (stat(filename, &sb))
239 err(1, "%s", filename);
240 new = palloc(N_ANEWER, f_anewer);
241 new->t_data = sb.st_atime;
242 return (new);
243 }
244
245 /*
246 * -atime n functions --
247 *
248 * True if the difference between the file access time and the
249 * current time is n 24 hour periods.
250 */
251 int
252 f_atime(plan, entry)
253 PLAN *plan;
254 FTSENT *entry;
255 {
256 COMPARE((now - entry->fts_statp->st_atime +
257 SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
258 }
259
260 PLAN *
261 c_atime(argvp, isok)
262 char ***argvp;
263 int isok;
264 {
265 char *arg = **argvp;
266 PLAN *new;
267
268 (*argvp)++;
269 ftsoptions &= ~FTS_NOSTAT;
270
271 new = palloc(N_ATIME, f_atime);
272 new->t_data = find_parsenum(new, "-atime", arg, NULL);
273 TIME_CORRECT(new, N_ATIME);
274 return (new);
275 }
276 /*
277 * -cmin n functions --
278 *
279 * True if the difference between the last change of file
280 * status information and the current time is n 24 hour periods.
281 */
282 int
283 f_cmin(plan, entry)
284 PLAN *plan;
285 FTSENT *entry;
286 {
287 COMPARE((now - entry->fts_statp->st_ctime +
288 SECSPERMIN - 1) / SECSPERMIN, plan->t_data);
289 }
290
291 PLAN *
292 c_cmin(argvp, isok)
293 char ***argvp;
294 int isok;
295 {
296 char *arg = **argvp;
297 PLAN *new;
298
299 (*argvp)++;
300 ftsoptions &= ~FTS_NOSTAT;
301
302 new = palloc(N_CMIN, f_cmin);
303 new->t_data = find_parsenum(new, "-cmin", arg, NULL);
304 TIME_CORRECT(new, N_CMIN);
305 return (new);
306 }
307
308 /*
309 * -cnewer file functions --
310 *
311 * True if the current file has been changed more recently
312 * than the changed time of the file named by the pathname
313 * file.
314 */
315 int
316 f_cnewer(plan, entry)
317 PLAN *plan;
318 FTSENT *entry;
319 {
320
321 return (entry->fts_statp->st_ctime > plan->t_data);
322 }
323
324 PLAN *
325 c_cnewer(argvp, isok)
326 char ***argvp;
327 int isok;
328 {
329 char *filename = **argvp;
330 PLAN *new;
331 struct stat sb;
332
333 (*argvp)++;
334 ftsoptions &= ~FTS_NOSTAT;
335
336 if (stat(filename, &sb))
337 err(1, "%s", filename);
338 new = palloc(N_CNEWER, f_cnewer);
339 new->t_data = sb.st_ctime;
340 return (new);
341 }
342
343 /*
344 * -ctime n functions --
345 *
346 * True if the difference between the last change of file
347 * status information and the current time is n 24 hour periods.
348 */
349 int
350 f_ctime(plan, entry)
351 PLAN *plan;
352 FTSENT *entry;
353 {
354 COMPARE((now - entry->fts_statp->st_ctime +
355 SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
356 }
357
358 PLAN *
359 c_ctime(argvp, isok)
360 char ***argvp;
361 int isok;
362 {
363 char *arg = **argvp;
364 PLAN *new;
365
366 (*argvp)++;
367 ftsoptions &= ~FTS_NOSTAT;
368
369 new = palloc(N_CTIME, f_ctime);
370 new->t_data = find_parsenum(new, "-ctime", arg, NULL);
371 TIME_CORRECT(new, N_CTIME);
372 return (new);
373 }
374
375 /*
376 * -depth functions --
377 *
378 * Always true, causes descent of the directory hierarchy to be done
379 * so that all entries in a directory are acted on before the directory
380 * itself.
381 */
382 int
383 f_always_true(plan, entry)
384 PLAN *plan;
385 FTSENT *entry;
386 {
387
388 return (1);
389 }
390
391 PLAN *
392 c_depth(argvp, isok)
393 char ***argvp;
394 int isok;
395 {
396 isdepth = 1;
397
398 return (palloc(N_DEPTH, f_always_true));
399 }
400
401 /*
402 * -empty functions --
403 *
404 * True if the file or directory is empty
405 */
406 int
407 f_empty(plan, entry)
408 PLAN *plan;
409 FTSENT *entry;
410 {
411 if (S_ISREG(entry->fts_statp->st_mode) &&
412 entry->fts_statp->st_size == 0)
413 return (1);
414 if (S_ISDIR(entry->fts_statp->st_mode)) {
415 struct dirent *dp;
416 int empty;
417 DIR *dir;
418
419 empty = 1;
420 dir = opendir(entry->fts_accpath);
421 if (dir == NULL)
422 err(1, "%s", entry->fts_accpath);
423 for (dp = readdir(dir); dp; dp = readdir(dir))
424 if (dp->d_name[0] != '.' ||
425 (dp->d_name[1] != '\0' &&
426 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
427 empty = 0;
428 break;
429 }
430 closedir(dir);
431 return (empty);
432 }
433 return (0);
434 }
435
436 PLAN *
437 c_empty(argvp, isok)
438 char ***argvp;
439 int isok;
440 {
441 ftsoptions &= ~FTS_NOSTAT;
442
443 return (palloc(N_EMPTY, f_empty));
444 }
445
446 /*
447 * [-exec | -ok] utility [arg ... ] ; functions --
448 *
449 * True if the executed utility returns a zero value as exit status.
450 * The end of the primary expression is delimited by a semicolon. If
451 * "{}" occurs anywhere, it gets replaced by the current pathname.
452 * The current directory for the execution of utility is the same as
453 * the current directory when the find utility was started.
454 *
455 * The primary -ok is different in that it requests affirmation of the
456 * user before executing the utility.
457 */
458 int
459 f_exec(plan, entry)
460 PLAN *plan;
461 FTSENT *entry;
462 {
463 int cnt;
464 pid_t pid;
465 int status;
466
467 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
468 if (plan->e_len[cnt])
469 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
470 entry->fts_path, &plan->e_len[cnt]);
471
472 if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
473 return (0);
474
475 /* don't mix output of command with find output */
476 fflush(stdout);
477 fflush(stderr);
478
479 switch (pid = vfork()) {
480 case -1:
481 err(1, "vfork");
482 /* NOTREACHED */
483 case 0:
484 if (fchdir(dotfd)) {
485 warn("chdir");
486 _exit(1);
487 }
488 execvp(plan->e_argv[0], plan->e_argv);
489 warn("%s", plan->e_argv[0]);
490 _exit(1);
491 }
492 pid = waitpid(pid, &status, 0);
493 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
494 }
495
496 /*
497 * c_exec --
498 * build three parallel arrays, one with pointers to the strings passed
499 * on the command line, one with (possibly duplicated) pointers to the
500 * argv array, and one with integer values that are lengths of the
501 * strings, but also flags meaning that the string has to be massaged.
502 */
503 PLAN *
504 c_exec(argvp, isok)
505 char ***argvp;
506 int isok;
507 {
508 PLAN *new; /* node returned */
509 int cnt;
510 char **argv, **ap, *p;
511
512 isoutput = 1;
513
514 new = palloc(N_EXEC, f_exec);
515 if (isok)
516 new->flags = F_NEEDOK;
517
518 for (ap = argv = *argvp;; ++ap) {
519 if (!*ap)
520 errx(1,
521 "%s: no terminating \";\"", isok ? "-ok" : "-exec");
522 if (**ap == ';')
523 break;
524 }
525
526 cnt = ap - *argvp + 1;
527 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
528 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
529 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
530
531 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
532 new->e_orig[cnt] = *argv;
533 for (p = *argv; *p; ++p)
534 if (p[0] == '{' && p[1] == '}') {
535 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
536 new->e_len[cnt] = MAXPATHLEN;
537 break;
538 }
539 if (!*p) {
540 new->e_argv[cnt] = *argv;
541 new->e_len[cnt] = 0;
542 }
543 }
544 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
545
546 *argvp = argv + 1;
547 return (new);
548 }
549
550 /*
551 * -execdir utility [arg ... ] ; functions --
552 *
553 * True if the executed utility returns a zero value as exit status.
554 * The end of the primary expression is delimited by a semicolon. If
555 * "{}" occurs anywhere, it gets replaced by the unqualified pathname.
556 * The current directory for the execution of utility is the same as
557 * the directory where the file lives.
558 */
559 int
560 f_execdir(plan, entry)
561 PLAN *plan;
562 FTSENT *entry;
563 {
564 int cnt;
565 pid_t pid;
566 int status;
567 char *file;
568
569 /* XXX - if file/dir ends in '/' this will not work -- can it? */
570 if ((file = strrchr(entry->fts_path, '/')))
571 file++;
572 else
573 file = entry->fts_path;
574
575 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
576 if (plan->e_len[cnt])
577 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
578 file, &plan->e_len[cnt]);
579
580 /* don't mix output of command with find output */
581 fflush(stdout);
582 fflush(stderr);
583
584 switch (pid = vfork()) {
585 case -1:
586 err(1, "fork");
587 /* NOTREACHED */
588 case 0:
589 execvp(plan->e_argv[0], plan->e_argv);
590 warn("%s", plan->e_argv[0]);
591 _exit(1);
592 }
593 pid = waitpid(pid, &status, 0);
594 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
595 }
596
597 /*
598 * c_execdir --
599 * build three parallel arrays, one with pointers to the strings passed
600 * on the command line, one with (possibly duplicated) pointers to the
601 * argv array, and one with integer values that are lengths of the
602 * strings, but also flags meaning that the string has to be massaged.
603 */
604 PLAN *
605 c_execdir(argvp, isok)
606 char ***argvp;
607 int isok;
608 {
609 PLAN *new; /* node returned */
610 int cnt;
611 char **argv, **ap, *p;
612
613 ftsoptions &= ~FTS_NOSTAT;
614 isoutput = 1;
615
616 new = palloc(N_EXECDIR, f_execdir);
617
618 for (ap = argv = *argvp;; ++ap) {
619 if (!*ap)
620 errx(1,
621 "-execdir: no terminating \";\"");
622 if (**ap == ';')
623 break;
624 }
625
626 cnt = ap - *argvp + 1;
627 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
628 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
629 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
630
631 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
632 new->e_orig[cnt] = *argv;
633 for (p = *argv; *p; ++p)
634 if (p[0] == '{' && p[1] == '}') {
635 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
636 new->e_len[cnt] = MAXPATHLEN;
637 break;
638 }
639 if (!*p) {
640 new->e_argv[cnt] = *argv;
641 new->e_len[cnt] = 0;
642 }
643 }
644 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
645
646 *argvp = argv + 1;
647 return (new);
648 }
649
650 /*
651 * -flags [-]flags functions --
652 */
653 int
654 f_flags(plan, entry)
655 PLAN *plan;
656 FTSENT *entry;
657 {
658 u_int32_t flags;
659
660 flags = entry->fts_statp->st_flags;
661 if (plan->flags == F_ATLEAST)
662 return ((plan->f_data | flags) == flags);
663 else
664 return (flags == plan->f_data);
665 /* NOTREACHED */
666 }
667
668 PLAN *
669 c_flags(argvp, isok)
670 char ***argvp;
671 int isok;
672 {
673 char *flags = **argvp;
674 PLAN *new;
675 u_long flagset;
676
677 (*argvp)++;
678 ftsoptions &= ~FTS_NOSTAT;
679
680 new = palloc(N_FLAGS, f_flags);
681
682 if (*flags == '-') {
683 new->flags = F_ATLEAST;
684 ++flags;
685 }
686
687 flagset = 0;
688 if ((strcmp(flags, "none") != 0) &&
689 (string_to_flags(&flags, &flagset, NULL) != 0))
690 errx(1, "-flags: %s: illegal flags string", flags);
691 new->f_data = flagset;
692 return (new);
693 }
694
695 /*
696 * -follow functions --
697 *
698 * Always true, causes symbolic links to be followed on a global
699 * basis.
700 */
701 PLAN *
702 c_follow(argvp, isok)
703 char ***argvp;
704 int isok;
705 {
706 ftsoptions &= ~FTS_PHYSICAL;
707 ftsoptions |= FTS_LOGICAL;
708
709 return (palloc(N_FOLLOW, f_always_true));
710 }
711
712 /* -fprint functions --
713 *
714 * Causes the current pathame to be written to the defined output file.
715 */
716 int
717 f_fprint(plan, entry)
718 PLAN *plan;
719 FTSENT *entry;
720 {
721
722 if (-1 == fprintf(plan->fprint_file, "%s\n", entry->fts_path))
723 warn("fprintf");
724
725 return(1);
726
727 /* no descriptors are closed; they will be closed by
728 operating system when this find command exits. */
729 }
730
731 PLAN *
732 c_fprint(argvp, isok)
733 char ***argvp;
734 int isok;
735 {
736 PLAN *new;
737
738 isoutput = 1; /* do not assume -print */
739
740 new = palloc(N_FPRINT, f_fprint);
741
742 if (NULL == (new->fprint_file = fopen(**argvp, "w")))
743 err(1, "-fprint: %s: cannot create file", **argvp);
744
745 (*argvp)++;
746 return (new);
747 }
748
749 /*
750 * -fstype functions --
751 *
752 * True if the file is of a certain type.
753 */
754 int
755 f_fstype(plan, entry)
756 PLAN *plan;
757 FTSENT *entry;
758 {
759 static dev_t curdev; /* need a guaranteed illegal dev value */
760 static int first = 1;
761 struct statvfs sb;
762 static short val;
763 static char fstype[MFSNAMELEN];
764 char *p, save[2];
765
766 /* Only check when we cross mount point. */
767 if (first || curdev != entry->fts_statp->st_dev) {
768 curdev = entry->fts_statp->st_dev;
769
770 /*
771 * Statfs follows symlinks; find wants the link's file system,
772 * not where it points.
773 */
774 if (entry->fts_info == FTS_SL ||
775 entry->fts_info == FTS_SLNONE) {
776 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
777 ++p;
778 else
779 p = entry->fts_accpath;
780 save[0] = p[0];
781 p[0] = '.';
782 save[1] = p[1];
783 p[1] = '\0';
784
785 } else
786 p = NULL;
787
788 if (statvfs(entry->fts_accpath, &sb))
789 err(1, "%s", entry->fts_accpath);
790
791 if (p) {
792 p[0] = save[0];
793 p[1] = save[1];
794 }
795
796 first = 0;
797
798 /*
799 * Further tests may need both of these values, so
800 * always copy both of them.
801 */
802 val = sb.f_flag;
803 strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
804 }
805 switch (plan->flags) {
806 case F_MTFLAG:
807 return (val & plan->mt_data);
808 case F_MTTYPE:
809 return (strncmp(fstype, plan->c_data, MFSNAMELEN) == 0);
810 default:
811 abort();
812 }
813 }
814
815 PLAN *
816 c_fstype(argvp, isok)
817 char ***argvp;
818 int isok;
819 {
820 char *arg = **argvp;
821 PLAN *new;
822
823 (*argvp)++;
824 ftsoptions &= ~FTS_NOSTAT;
825
826 new = palloc(N_FSTYPE, f_fstype);
827
828 switch (*arg) {
829 case 'l':
830 if (!strcmp(arg, "local")) {
831 new->flags = F_MTFLAG;
832 new->mt_data = MNT_LOCAL;
833 return (new);
834 }
835 break;
836 case 'r':
837 if (!strcmp(arg, "rdonly")) {
838 new->flags = F_MTFLAG;
839 new->mt_data = MNT_RDONLY;
840 return (new);
841 }
842 break;
843 }
844
845 new->flags = F_MTTYPE;
846 new->c_data = arg;
847 return (new);
848 }
849
850 /*
851 * -group gname functions --
852 *
853 * True if the file belongs to the group gname. If gname is numeric and
854 * an equivalent of the getgrnam() function does not return a valid group
855 * name, gname is taken as a group ID.
856 */
857 int
858 f_group(plan, entry)
859 PLAN *plan;
860 FTSENT *entry;
861 {
862
863 return (entry->fts_statp->st_gid == plan->g_data);
864 }
865
866 PLAN *
867 c_group(argvp, isok)
868 char ***argvp;
869 int isok;
870 {
871 char *gname = **argvp;
872 PLAN *new;
873 struct group *g;
874 gid_t gid;
875
876 (*argvp)++;
877 ftsoptions &= ~FTS_NOSTAT;
878
879 g = getgrnam(gname);
880 if (g == NULL) {
881 gid = atoi(gname);
882 if (gid == 0 && gname[0] != '0')
883 errx(1, "-group: %s: no such group", gname);
884 } else
885 gid = g->gr_gid;
886
887 new = palloc(N_GROUP, f_group);
888 new->g_data = gid;
889 return (new);
890 }
891
892 /*
893 * -inum n functions --
894 *
895 * True if the file has inode # n.
896 */
897 int
898 f_inum(plan, entry)
899 PLAN *plan;
900 FTSENT *entry;
901 {
902
903 COMPARE(entry->fts_statp->st_ino, plan->i_data);
904 }
905
906 PLAN *
907 c_inum(argvp, isok)
908 char ***argvp;
909 int isok;
910 {
911 char *arg = **argvp;
912 PLAN *new;
913
914 (*argvp)++;
915 ftsoptions &= ~FTS_NOSTAT;
916
917 new = palloc(N_INUM, f_inum);
918 new->i_data = find_parsenum(new, "-inum", arg, NULL);
919 return (new);
920 }
921
922 /*
923 * -links n functions --
924 *
925 * True if the file has n links.
926 */
927 int
928 f_links(plan, entry)
929 PLAN *plan;
930 FTSENT *entry;
931 {
932
933 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
934 }
935
936 PLAN *
937 c_links(argvp, isok)
938 char ***argvp;
939 int isok;
940 {
941 char *arg = **argvp;
942 PLAN *new;
943
944 (*argvp)++;
945 ftsoptions &= ~FTS_NOSTAT;
946
947 new = palloc(N_LINKS, f_links);
948 new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
949 return (new);
950 }
951
952 /*
953 * -ls functions --
954 *
955 * Always true - prints the current entry to stdout in "ls" format.
956 */
957 int
958 f_ls(plan, entry)
959 PLAN *plan;
960 FTSENT *entry;
961 {
962
963 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
964 return (1);
965 }
966
967 PLAN *
968 c_ls(argvp, isok)
969 char ***argvp;
970 int isok;
971 {
972
973 ftsoptions &= ~FTS_NOSTAT;
974 isoutput = 1;
975
976 return (palloc(N_LS, f_ls));
977 }
978
979 /*
980 * - maxdepth n functions --
981 *
982 * True if the current search depth is less than or equal to the
983 * maximum depth specified
984 */
985 int
986 f_maxdepth(plan, entry)
987 PLAN *plan;
988 FTSENT *entry;
989 {
990 extern FTS *tree;
991
992 if (entry->fts_level >= plan->max_data)
993 fts_set(tree, entry, FTS_SKIP);
994 return (entry->fts_level <= plan->max_data);
995 }
996
997 PLAN *
998 c_maxdepth(argvp, isok)
999 char ***argvp;
1000 int isok;
1001 {
1002 char *arg = **argvp;
1003 PLAN *new;
1004
1005 (*argvp)++;
1006 new = palloc(N_MAXDEPTH, f_maxdepth);
1007 new->max_data = atoi(arg);
1008 return (new);
1009 }
1010
1011 /*
1012 * - mindepth n functions --
1013 *
1014 * True if the current search depth is greater than or equal to the
1015 * minimum depth specified
1016 */
1017 int
1018 f_mindepth(plan, entry)
1019 PLAN *plan;
1020 FTSENT *entry;
1021 {
1022 return (entry->fts_level >= plan->min_data);
1023 }
1024
1025 PLAN *
1026 c_mindepth(argvp, isok)
1027 char ***argvp;
1028 int isok;
1029 {
1030 char *arg = **argvp;
1031 PLAN *new;
1032
1033 (*argvp)++;
1034 new = palloc(N_MINDEPTH, f_mindepth);
1035 new->min_data = atoi(arg);
1036 return (new);
1037 }
1038 /*
1039 * -mmin n functions --
1040 *
1041 * True if the difference between the file modification time and the
1042 * current time is n 24 hour periods.
1043 */
1044 int
1045 f_mmin(plan, entry)
1046 PLAN *plan;
1047 FTSENT *entry;
1048 {
1049 COMPARE((now - entry->fts_statp->st_mtime + SECSPERMIN - 1) /
1050 SECSPERMIN, plan->t_data);
1051 }
1052
1053 PLAN *
1054 c_mmin(argvp, isok)
1055 char ***argvp;
1056 int isok;
1057 {
1058 char *arg = **argvp;
1059 PLAN *new;
1060
1061 (*argvp)++;
1062 ftsoptions &= ~FTS_NOSTAT;
1063
1064 new = palloc(N_MMIN, f_mmin);
1065 new->t_data = find_parsenum(new, "-mmin", arg, NULL);
1066 TIME_CORRECT(new, N_MMIN);
1067 return (new);
1068 }
1069 /*
1070 * -mtime n functions --
1071 *
1072 * True if the difference between the file modification time and the
1073 * current time is n 24 hour periods.
1074 */
1075 int
1076 f_mtime(plan, entry)
1077 PLAN *plan;
1078 FTSENT *entry;
1079 {
1080 COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
1081 SECSPERDAY, plan->t_data);
1082 }
1083
1084 PLAN *
1085 c_mtime(argvp, isok)
1086 char ***argvp;
1087 int isok;
1088 {
1089 char *arg = **argvp;
1090 PLAN *new;
1091
1092 (*argvp)++;
1093 ftsoptions &= ~FTS_NOSTAT;
1094
1095 new = palloc(N_MTIME, f_mtime);
1096 new->t_data = find_parsenum(new, "-mtime", arg, NULL);
1097 TIME_CORRECT(new, N_MTIME);
1098 return (new);
1099 }
1100
1101 /*
1102 * -name functions --
1103 *
1104 * True if the basename of the filename being examined
1105 * matches pattern using Pattern Matching Notation S3.14
1106 */
1107 int
1108 f_name(plan, entry)
1109 PLAN *plan;
1110 FTSENT *entry;
1111 {
1112
1113 return (!fnmatch(plan->c_data, entry->fts_name, 0));
1114 }
1115
1116 PLAN *
1117 c_name(argvp, isok)
1118 char ***argvp;
1119 int isok;
1120 {
1121 char *pattern = **argvp;
1122 PLAN *new;
1123
1124 (*argvp)++;
1125 new = palloc(N_NAME, f_name);
1126 new->c_data = pattern;
1127 return (new);
1128 }
1129
1130 /*
1131 * -iname functions --
1132 *
1133 * Similar to -name, but does case insensitive matching
1134 *
1135 */
1136 int
1137 f_iname(plan, entry)
1138 PLAN *plan;
1139 FTSENT *entry;
1140 {
1141 return (!fnmatch(plan->c_data, entry->fts_name, FNM_CASEFOLD));
1142 }
1143
1144 PLAN *
1145 c_iname(argvp, isok)
1146 char ***argvp;
1147 int isok;
1148 {
1149 char *pattern = **argvp;
1150 PLAN *new;
1151
1152 (*argvp)++;
1153 new = palloc(N_INAME, f_iname);
1154 new->c_data = pattern;
1155 return (new);
1156 }
1157
1158 /*
1159 * -newer file functions --
1160 *
1161 * True if the current file has been modified more recently
1162 * than the modification time of the file named by the pathname
1163 * file.
1164 */
1165 int
1166 f_newer(plan, entry)
1167 PLAN *plan;
1168 FTSENT *entry;
1169 {
1170
1171 return (entry->fts_statp->st_mtime > plan->t_data);
1172 }
1173
1174 PLAN *
1175 c_newer(argvp, isok)
1176 char ***argvp;
1177 int isok;
1178 {
1179 char *filename = **argvp;
1180 PLAN *new;
1181 struct stat sb;
1182
1183 (*argvp)++;
1184 ftsoptions &= ~FTS_NOSTAT;
1185
1186 if (stat(filename, &sb))
1187 err(1, "%s", filename);
1188 new = palloc(N_NEWER, f_newer);
1189 new->t_data = sb.st_mtime;
1190 return (new);
1191 }
1192
1193 /*
1194 * -nogroup functions --
1195 *
1196 * True if file belongs to a user ID for which the equivalent
1197 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1198 */
1199 int
1200 f_nogroup(plan, entry)
1201 PLAN *plan;
1202 FTSENT *entry;
1203 {
1204
1205 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
1206 }
1207
1208 PLAN *
1209 c_nogroup(argvp, isok)
1210 char ***argvp;
1211 int isok;
1212 {
1213 ftsoptions &= ~FTS_NOSTAT;
1214
1215 return (palloc(N_NOGROUP, f_nogroup));
1216 }
1217
1218 /*
1219 * -nouser functions --
1220 *
1221 * True if file belongs to a user ID for which the equivalent
1222 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1223 */
1224 int
1225 f_nouser(plan, entry)
1226 PLAN *plan;
1227 FTSENT *entry;
1228 {
1229
1230 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
1231 }
1232
1233 PLAN *
1234 c_nouser(argvp, isok)
1235 char ***argvp;
1236 int isok;
1237 {
1238 ftsoptions &= ~FTS_NOSTAT;
1239
1240 return (palloc(N_NOUSER, f_nouser));
1241 }
1242
1243 /*
1244 * -path functions --
1245 *
1246 * True if the path of the filename being examined
1247 * matches pattern using Pattern Matching Notation S3.14
1248 */
1249 int
1250 f_path(plan, entry)
1251 PLAN *plan;
1252 FTSENT *entry;
1253 {
1254
1255 return (!fnmatch(plan->c_data, entry->fts_path, 0));
1256 }
1257
1258 PLAN *
1259 c_path(argvp, isok)
1260 char ***argvp;
1261 int isok;
1262 {
1263 char *pattern = **argvp;
1264 PLAN *new;
1265
1266 (*argvp)++;
1267 new = palloc(N_NAME, f_path);
1268 new->c_data = pattern;
1269 return (new);
1270 }
1271
1272 /*
1273 * -perm functions --
1274 *
1275 * The mode argument is used to represent file mode bits. If it starts
1276 * with a leading digit, it's treated as an octal mode, otherwise as a
1277 * symbolic mode.
1278 */
1279 int
1280 f_perm(plan, entry)
1281 PLAN *plan;
1282 FTSENT *entry;
1283 {
1284 mode_t mode;
1285
1286 mode = entry->fts_statp->st_mode &
1287 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1288 if (plan->flags == F_ATLEAST)
1289 return ((plan->m_data | mode) == mode);
1290 else
1291 return (mode == plan->m_data);
1292 /* NOTREACHED */
1293 }
1294
1295 PLAN *
1296 c_perm(argvp, isok)
1297 char ***argvp;
1298 int isok;
1299 {
1300 char *perm = **argvp;
1301 PLAN *new;
1302 mode_t *set;
1303
1304 (*argvp)++;
1305 ftsoptions &= ~FTS_NOSTAT;
1306
1307 new = palloc(N_PERM, f_perm);
1308
1309 if (*perm == '-') {
1310 new->flags = F_ATLEAST;
1311 ++perm;
1312 }
1313
1314 if ((set = setmode(perm)) == NULL)
1315 err(1, "-perm: Cannot set file mode `%s'", perm);
1316
1317 new->m_data = getmode(set, 0);
1318 free(set);
1319 return (new);
1320 }
1321
1322 /*
1323 * -print functions --
1324 *
1325 * Always true, causes the current pathame to be written to
1326 * standard output.
1327 */
1328 int
1329 f_print(plan, entry)
1330 PLAN *plan;
1331 FTSENT *entry;
1332 {
1333
1334 (void)printf("%s\n", entry->fts_path);
1335 return (1);
1336 }
1337
1338 int
1339 f_print0(plan, entry)
1340 PLAN *plan;
1341 FTSENT *entry;
1342 {
1343
1344 (void)fputs(entry->fts_path, stdout);
1345 (void)fputc('\0', stdout);
1346 return (1);
1347 }
1348
1349 int
1350 f_printx(plan, entry)
1351 PLAN *plan;
1352 FTSENT *entry;
1353 {
1354 char *cp;
1355
1356 for (cp = entry->fts_path; *cp; cp++) {
1357 if (*cp == '\'' || *cp == '\"' || *cp == ' ' ||
1358 *cp == '$' || *cp == '`' ||
1359 *cp == '\t' || *cp == '\n' || *cp == '\\')
1360 fputc('\\', stdout);
1361
1362 fputc(*cp, stdout);
1363 }
1364
1365 fputc('\n', stdout);
1366 return (1);
1367 }
1368
1369 PLAN *
1370 c_print(argvp, isok)
1371 char ***argvp;
1372 int isok;
1373 {
1374
1375 isoutput = 1;
1376
1377 return (palloc(N_PRINT, f_print));
1378 }
1379
1380 PLAN *
1381 c_print0(argvp, isok)
1382 char ***argvp;
1383 int isok;
1384 {
1385
1386 isoutput = 1;
1387
1388 return (palloc(N_PRINT0, f_print0));
1389 }
1390
1391 PLAN *
1392 c_printx(argvp, isok)
1393 char ***argvp;
1394 int isok;
1395 {
1396
1397 isoutput = 1;
1398
1399 return (palloc(N_PRINTX, f_printx));
1400 }
1401
1402 /*
1403 * -prune functions --
1404 *
1405 * Prune a portion of the hierarchy.
1406 */
1407 int
1408 f_prune(plan, entry)
1409 PLAN *plan;
1410 FTSENT *entry;
1411 {
1412 if (fts_set(tree, entry, FTS_SKIP))
1413 err(1, "%s", entry->fts_path);
1414 return (1);
1415 }
1416
1417 PLAN *
1418 c_prune(argvp, isok)
1419 char ***argvp;
1420 int isok;
1421 {
1422
1423 return (palloc(N_PRUNE, f_prune));
1424 }
1425
1426 /*
1427 * -regex regexp (and related) functions --
1428 *
1429 * True if the complete file path matches the regular expression regexp.
1430 * For -regex, regexp is a case-sensitive (basic) regular expression.
1431 * For -iregex, regexp is a case-insensitive (basic) regular expression.
1432 */
1433 int
1434 f_regex(plan, entry)
1435 PLAN *plan;
1436 FTSENT *entry;
1437 {
1438
1439 return (regexec(&plan->regexp_data, entry->fts_path, 0, NULL, 0) == 0);
1440 }
1441
1442 static PLAN *
1443 c_regex_common(argvp, isok, type, regcomp_flags)
1444 char ***argvp;
1445 int isok, regcomp_flags;
1446 enum ntype type;
1447 {
1448 char errbuf[LINE_MAX];
1449 regex_t reg;
1450 char *regexp = **argvp;
1451 char *lineregexp;
1452 PLAN *new;
1453 int rv;
1454
1455 (*argvp)++;
1456
1457 lineregexp = alloca(strlen(regexp) + 1 + 6); /* max needed */
1458 sprintf(lineregexp, "^%s(%s%s)$",
1459 (regcomp_flags & REG_EXTENDED) ? "" : "\\", regexp,
1460 (regcomp_flags & REG_EXTENDED) ? "" : "\\");
1461 rv = regcomp(®, lineregexp, REG_NOSUB|regcomp_flags);
1462 if (rv != 0) {
1463 regerror(rv, ®, errbuf, sizeof errbuf);
1464 errx(1, "regexp %s: %s", regexp, errbuf);
1465 }
1466
1467 new = palloc(type, f_regex);
1468 new->regexp_data = reg;
1469 return (new);
1470 }
1471
1472 PLAN *
1473 c_regex(argvp, isok)
1474 char ***argvp;
1475 int isok;
1476 {
1477
1478 return (c_regex_common(argvp, isok, N_REGEX, REG_BASIC));
1479 }
1480
1481 PLAN *
1482 c_iregex(argvp, isok)
1483 char ***argvp;
1484 int isok;
1485 {
1486
1487 return (c_regex_common(argvp, isok, N_IREGEX, REG_BASIC|REG_ICASE));
1488 }
1489
1490 /*
1491 * -size n[c] functions --
1492 *
1493 * True if the file size in bytes, divided by an implementation defined
1494 * value and rounded up to the next integer, is n. If n is followed by
1495 * a c, the size is in bytes.
1496 */
1497 #define FIND_SIZE 512
1498 static int divsize = 1;
1499
1500 int
1501 f_size(plan, entry)
1502 PLAN *plan;
1503 FTSENT *entry;
1504 {
1505 off_t size;
1506
1507 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1508 FIND_SIZE : entry->fts_statp->st_size;
1509 COMPARE(size, plan->o_data);
1510 }
1511
1512 PLAN *
1513 c_size(argvp, isok)
1514 char ***argvp;
1515 int isok;
1516 {
1517 char *arg = **argvp;
1518 PLAN *new;
1519 char endch;
1520
1521 (*argvp)++;
1522 ftsoptions &= ~FTS_NOSTAT;
1523
1524 new = palloc(N_SIZE, f_size);
1525 endch = 'c';
1526 new->o_data = find_parsenum(new, "-size", arg, &endch);
1527 if (endch == 'c')
1528 divsize = 0;
1529 return (new);
1530 }
1531
1532 /*
1533 * -type c functions --
1534 *
1535 * True if the type of the file is c, where c is b, c, d, p, f or w
1536 * for block special file, character special file, directory, FIFO,
1537 * regular file or whiteout respectively.
1538 */
1539 int
1540 f_type(plan, entry)
1541 PLAN *plan;
1542 FTSENT *entry;
1543 {
1544
1545 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
1546 }
1547
1548 PLAN *
1549 c_type(argvp, isok)
1550 char ***argvp;
1551 int isok;
1552 {
1553 char *typestring = **argvp;
1554 PLAN *new;
1555 mode_t mask = (mode_t)0;
1556
1557 (*argvp)++;
1558 ftsoptions &= ~FTS_NOSTAT;
1559
1560 switch (typestring[0]) {
1561 case 'b':
1562 mask = S_IFBLK;
1563 break;
1564 case 'c':
1565 mask = S_IFCHR;
1566 break;
1567 case 'd':
1568 mask = S_IFDIR;
1569 break;
1570 case 'f':
1571 mask = S_IFREG;
1572 break;
1573 case 'l':
1574 mask = S_IFLNK;
1575 break;
1576 case 'p':
1577 mask = S_IFIFO;
1578 break;
1579 case 's':
1580 mask = S_IFSOCK;
1581 break;
1582 #ifdef S_IFWHT
1583 case 'W':
1584 case 'w':
1585 mask = S_IFWHT;
1586 #ifdef FTS_WHITEOUT
1587 ftsoptions |= FTS_WHITEOUT;
1588 #endif
1589 break;
1590 #endif /* S_IFWHT */
1591 default:
1592 errx(1, "-type: %s: unknown type", typestring);
1593 }
1594
1595 new = palloc(N_TYPE, f_type);
1596 new->m_data = mask;
1597 return (new);
1598 }
1599
1600 /*
1601 * -user uname functions --
1602 *
1603 * True if the file belongs to the user uname. If uname is numeric and
1604 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1605 * return a valid user name, uname is taken as a user ID.
1606 */
1607 int
1608 f_user(plan, entry)
1609 PLAN *plan;
1610 FTSENT *entry;
1611 {
1612
1613 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1614 }
1615
1616 PLAN *
1617 c_user(argvp, isok)
1618 char ***argvp;
1619 int isok;
1620 {
1621 char *username = **argvp;
1622 PLAN *new;
1623 struct passwd *p;
1624 uid_t uid;
1625
1626 (*argvp)++;
1627 ftsoptions &= ~FTS_NOSTAT;
1628
1629 new = palloc(N_USER, f_user);
1630 p = getpwnam(username);
1631 if (p == NULL) {
1632 if (atoi(username) == 0 && username[0] != '0' &&
1633 strcmp(username, "+0") && strcmp(username, "-0"))
1634 errx(1, "-user: %s: no such user", username);
1635 uid = find_parsenum(new, "-user", username, NULL);
1636
1637 } else {
1638 new->flags = F_EQUAL;
1639 uid = p->pw_uid;
1640 }
1641
1642 new->u_data = uid;
1643 return (new);
1644 }
1645
1646 /*
1647 * -xdev functions --
1648 *
1649 * Always true, causes find not to decend past directories that have a
1650 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1651 */
1652 PLAN *
1653 c_xdev(argvp, isok)
1654 char ***argvp;
1655 int isok;
1656 {
1657 ftsoptions |= FTS_XDEV;
1658
1659 return (palloc(N_XDEV, f_always_true));
1660 }
1661
1662 /*
1663 * ( expression ) functions --
1664 *
1665 * True if expression is true.
1666 */
1667 int
1668 f_expr(plan, entry)
1669 PLAN *plan;
1670 FTSENT *entry;
1671 {
1672 PLAN *p;
1673 int state;
1674
1675 state = 0;
1676 for (p = plan->p_data[0];
1677 p && (state = (p->eval)(p, entry)); p = p->next);
1678 return (state);
1679 }
1680
1681 /*
1682 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are
1683 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1684 * to a N_EXPR node containing the expression and the ')' node is discarded.
1685 */
1686 PLAN *
1687 c_openparen(argvp, isok)
1688 char ***argvp;
1689 int isok;
1690 {
1691
1692 return (palloc(N_OPENPAREN, (int (*) __P((PLAN *, FTSENT *)))-1));
1693 }
1694
1695 PLAN *
1696 c_closeparen(argvp, isok)
1697 char ***argvp;
1698 int isok;
1699 {
1700
1701 return (palloc(N_CLOSEPAREN, (int (*) __P((PLAN *, FTSENT *)))-1));
1702 }
1703
1704 /*
1705 * ! expression functions --
1706 *
1707 * Negation of a primary; the unary NOT operator.
1708 */
1709 int
1710 f_not(plan, entry)
1711 PLAN *plan;
1712 FTSENT *entry;
1713 {
1714 PLAN *p;
1715 int state;
1716
1717 state = 0;
1718 for (p = plan->p_data[0];
1719 p && (state = (p->eval)(p, entry)); p = p->next);
1720 return (!state);
1721 }
1722
1723 PLAN *
1724 c_not(argvp, isok)
1725 char ***argvp;
1726 int isok;
1727 {
1728
1729 return (palloc(N_NOT, f_not));
1730 }
1731
1732 /*
1733 * expression -o expression functions --
1734 *
1735 * Alternation of primaries; the OR operator. The second expression is
1736 * not evaluated if the first expression is true.
1737 */
1738 int
1739 f_or(plan, entry)
1740 PLAN *plan;
1741 FTSENT *entry;
1742 {
1743 PLAN *p;
1744 int state;
1745
1746 state = 0;
1747 for (p = plan->p_data[0];
1748 p && (state = (p->eval)(p, entry)); p = p->next);
1749
1750 if (state)
1751 return (1);
1752
1753 for (p = plan->p_data[1];
1754 p && (state = (p->eval)(p, entry)); p = p->next);
1755 return (state);
1756 }
1757
1758 PLAN *
1759 c_or(argvp, isok)
1760 char ***argvp;
1761 int isok;
1762 {
1763
1764 return (palloc(N_OR, f_or));
1765 }
1766
1767 PLAN *
1768 c_null(argvp, isok)
1769 char ***argvp;
1770 int isok;
1771 {
1772
1773 return (NULL);
1774 }
1775
1776 static PLAN *
1777 palloc(t, f)
1778 enum ntype t;
1779 int (*f) __P((PLAN *, FTSENT *));
1780 {
1781 PLAN *new;
1782
1783 if ((new = malloc(sizeof(PLAN))) == NULL)
1784 err(1, NULL);
1785 new->type = t;
1786 new->eval = f;
1787 new->flags = 0;
1788 new->next = NULL;
1789 return (new);
1790 }
1791