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 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)function.c 8.1 (Berkeley) 06/06/93"; 13 #endif /* not lint */ 14 15 #include <sys/param.h> 16 #include <sys/ucred.h> 17 #include <sys/stat.h> 18 #include <sys/wait.h> 19 #include <sys/mount.h> 20 21 #include <err.h> 22 #include <errno.h> 23 #include <fnmatch.h> 24 #include <fts.h> 25 #include <grp.h> 26 #include <pwd.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <tzfile.h> 31 #include <unistd.h> 32 33 #include "find.h" 34 35 #define COMPARE(a, b) { \ 36 switch (plan->flags) { \ 37 case F_EQUAL: \ 38 return (a == b); \ 39 case F_LESSTHAN: \ 40 return (a < b); \ 41 case F_GREATER: \ 42 return (a > b); \ 43 default: \ 44 abort(); \ 45 } \ 46 } 47 48 static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *)))); 49 50 /* 51 * find_parsenum -- 52 * Parse a string of the form [+-]# and return the value. 53 */ 54 static long 55 find_parsenum(plan, option, vp, endch) 56 PLAN *plan; 57 char *option, *vp, *endch; 58 { 59 long value; 60 char *endchar, *str; /* Pointer to character ending conversion. */ 61 62 /* Determine comparison from leading + or -. */ 63 str = vp; 64 switch (*str) { 65 case '+': 66 ++str; 67 plan->flags = F_GREATER; 68 break; 69 case '-': 70 ++str; 71 plan->flags = F_LESSTHAN; 72 break; 73 default: 74 plan->flags = F_EQUAL; 75 break; 76 } 77 78 /* 79 * Convert the string with strtol(). Note, if strtol() returns zero 80 * and endchar points to the beginning of the string we know we have 81 * a syntax error. 82 */ 83 value = strtol(str, &endchar, 10); 84 if (value == 0 && endchar == str) 85 errx(1, "%s: %s: illegal numeric value", option, vp); 86 if (endchar[0] && (endch == NULL || endchar[0] != *endch)) 87 errx(1, "%s: %s: illegal trailing character", option, vp); 88 if (endch) 89 *endch = endchar[0]; 90 return (value); 91 } 92 93 /* 94 * The value of n for the inode times (atime, ctime, and mtime) is a range, 95 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with 96 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the 97 * user wanted. Correct so that -1 is "less than 1". 98 */ 99 #define TIME_CORRECT(p, ttype) \ 100 if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \ 101 ++((p)->t_data); 102 103 /* 104 * -atime n functions -- 105 * 106 * True if the difference between the file access time and the 107 * current time is n 24 hour periods. 108 */ 109 int 110 f_atime(plan, entry) 111 PLAN *plan; 112 FTSENT *entry; 113 { 114 extern time_t now; 115 116 COMPARE((now - entry->fts_statp->st_atime + 117 SECSPERDAY - 1) / SECSPERDAY, plan->t_data); 118 } 119 120 PLAN * 121 c_atime(arg) 122 char *arg; 123 { 124 PLAN *new; 125 126 ftsoptions &= ~FTS_NOSTAT; 127 128 new = palloc(N_ATIME, f_atime); 129 new->t_data = find_parsenum(new, "-atime", arg, NULL); 130 TIME_CORRECT(new, N_ATIME); 131 return (new); 132 } 133 /* 134 * -ctime n functions -- 135 * 136 * True if the difference between the last change of file 137 * status information and the current time is n 24 hour periods. 138 */ 139 int 140 f_ctime(plan, entry) 141 PLAN *plan; 142 FTSENT *entry; 143 { 144 extern time_t now; 145 146 COMPARE((now - entry->fts_statp->st_ctime + 147 SECSPERDAY - 1) / SECSPERDAY, plan->t_data); 148 } 149 150 PLAN * 151 c_ctime(arg) 152 char *arg; 153 { 154 PLAN *new; 155 156 ftsoptions &= ~FTS_NOSTAT; 157 158 new = palloc(N_CTIME, f_ctime); 159 new->t_data = find_parsenum(new, "-ctime", arg, NULL); 160 TIME_CORRECT(new, N_CTIME); 161 return (new); 162 } 163 164 /* 165 * -depth functions -- 166 * 167 * Always true, causes descent of the directory hierarchy to be done 168 * so that all entries in a directory are acted on before the directory 169 * itself. 170 */ 171 int 172 f_always_true(plan, entry) 173 PLAN *plan; 174 FTSENT *entry; 175 { 176 return (1); 177 } 178 179 PLAN * 180 c_depth() 181 { 182 isdepth = 1; 183 184 return (palloc(N_DEPTH, f_always_true)); 185 } 186 187 /* 188 * [-exec | -ok] utility [arg ... ] ; functions -- 189 * 190 * True if the executed utility returns a zero value as exit status. 191 * The end of the primary expression is delimited by a semicolon. If 192 * "{}" occurs anywhere, it gets replaced by the current pathname. 193 * The current directory for the execution of utility is the same as 194 * the current directory when the find utility was started. 195 * 196 * The primary -ok is different in that it requests affirmation of the 197 * user before executing the utility. 198 */ 199 int 200 f_exec(plan, entry) 201 register PLAN *plan; 202 FTSENT *entry; 203 { 204 extern int dotfd; 205 register int cnt; 206 pid_t pid; 207 int status; 208 209 for (cnt = 0; plan->e_argv[cnt]; ++cnt) 210 if (plan->e_len[cnt]) 211 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt], 212 entry->fts_path, plan->e_len[cnt]); 213 214 if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv)) 215 return (0); 216 217 switch (pid = vfork()) { 218 case -1: 219 err(1, "fork"); 220 /* NOTREACHED */ 221 case 0: 222 if (fchdir(dotfd)) { 223 warn("chdir"); 224 _exit(1); 225 } 226 execvp(plan->e_argv[0], plan->e_argv); 227 warn("%s", plan->e_argv[0]); 228 _exit(1); 229 } 230 pid = waitpid(pid, &status, 0); 231 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); 232 } 233 234 /* 235 * c_exec -- 236 * build three parallel arrays, one with pointers to the strings passed 237 * on the command line, one with (possibly duplicated) pointers to the 238 * argv array, and one with integer values that are lengths of the 239 * strings, but also flags meaning that the string has to be massaged. 240 */ 241 PLAN * 242 c_exec(argvp, isok) 243 char ***argvp; 244 int isok; 245 { 246 PLAN *new; /* node returned */ 247 register int cnt; 248 register char **argv, **ap, *p; 249 250 isoutput = 1; 251 252 new = palloc(N_EXEC, f_exec); 253 if (isok) 254 new->flags = F_NEEDOK; 255 256 for (ap = argv = *argvp;; ++ap) { 257 if (!*ap) 258 errx(1, 259 "%s: no terminating \";\"", isok ? "-ok" : "-exec"); 260 if (**ap == ';') 261 break; 262 } 263 264 cnt = ap - *argvp + 1; 265 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *)); 266 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *)); 267 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int)); 268 269 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 270 new->e_orig[cnt] = *argv; 271 for (p = *argv; *p; ++p) 272 if (p[0] == '{' && p[1] == '}') { 273 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN); 274 new->e_len[cnt] = MAXPATHLEN; 275 break; 276 } 277 if (!*p) { 278 new->e_argv[cnt] = *argv; 279 new->e_len[cnt] = 0; 280 } 281 } 282 new->e_argv[cnt] = new->e_orig[cnt] = NULL; 283 284 *argvp = argv + 1; 285 return (new); 286 } 287 288 /* 289 * -follow functions -- 290 * 291 * Always true, causes symbolic links to be followed on a global 292 * basis. 293 */ 294 PLAN * 295 c_follow() 296 { 297 ftsoptions &= ~FTS_PHYSICAL; 298 ftsoptions |= FTS_LOGICAL; 299 300 return (palloc(N_FOLLOW, f_always_true)); 301 } 302 303 /* 304 * -fstype functions -- 305 * 306 * True if the file is of a certain type. 307 */ 308 int 309 f_fstype(plan, entry) 310 PLAN *plan; 311 FTSENT *entry; 312 { 313 static dev_t curdev; /* need a guaranteed illegal dev value */ 314 static int first = 1; 315 struct statfs sb; 316 static short val; 317 char *p, save[2]; 318 319 /* Only check when we cross mount point. */ 320 if (first || curdev != entry->fts_statp->st_dev) { 321 curdev = entry->fts_statp->st_dev; 322 323 /* 324 * Statfs follows symlinks; find wants the link's file system, 325 * not where it points. 326 */ 327 if (entry->fts_info == FTS_SL || 328 entry->fts_info == FTS_SLNONE) { 329 if (p = strrchr(entry->fts_accpath, '/')) 330 ++p; 331 else 332 p = entry->fts_accpath; 333 save[0] = p[0]; 334 p[0] = '.'; 335 save[1] = p[1]; 336 p[1] = '\0'; 337 338 } else 339 p = NULL; 340 341 if (statfs(entry->fts_accpath, &sb)) 342 err(1, "%s", entry->fts_accpath); 343 344 if (p) { 345 p[0] = save[0]; 346 p[1] = save[1]; 347 } 348 349 first = 0; 350 switch (plan->flags) { 351 case F_MTFLAG: 352 val = sb.f_flags; 353 break; 354 case F_MTTYPE: 355 val = sb.f_type; 356 break; 357 default: 358 abort(); 359 } 360 } 361 switch(plan->flags) { 362 case F_MTFLAG: 363 return (val & plan->mt_data); 364 case F_MTTYPE: 365 return (val == plan->mt_data); 366 default: 367 abort(); 368 } 369 } 370 371 PLAN * 372 c_fstype(arg) 373 char *arg; 374 { 375 register PLAN *new; 376 377 ftsoptions &= ~FTS_NOSTAT; 378 379 new = palloc(N_FSTYPE, f_fstype); 380 switch (*arg) { 381 case 'l': 382 if (!strcmp(arg, "local")) { 383 new->flags = F_MTFLAG; 384 new->mt_data = MNT_LOCAL; 385 return (new); 386 } 387 break; 388 case 'm': 389 if (!strcmp(arg, "mfs")) { 390 new->flags = F_MTTYPE; 391 new->mt_data = MOUNT_MFS; 392 return (new); 393 } 394 break; 395 case 'n': 396 if (!strcmp(arg, "nfs")) { 397 new->flags = F_MTTYPE; 398 new->mt_data = MOUNT_NFS; 399 return (new); 400 } 401 break; 402 case 'p': 403 if (!strcmp(arg, "pc")) { 404 new->flags = F_MTTYPE; 405 new->mt_data = MOUNT_PC; 406 return (new); 407 } 408 break; 409 case 'r': 410 if (!strcmp(arg, "rdonly")) { 411 new->flags = F_MTFLAG; 412 new->mt_data = MNT_RDONLY; 413 return (new); 414 } 415 break; 416 case 'u': 417 if (!strcmp(arg, "ufs")) { 418 new->flags = F_MTTYPE; 419 new->mt_data = MOUNT_UFS; 420 return (new); 421 } 422 break; 423 } 424 errx(1, "%s: unknown file type", arg); 425 /* NOTREACHED */ 426 } 427 428 /* 429 * -group gname functions -- 430 * 431 * True if the file belongs to the group gname. If gname is numeric and 432 * an equivalent of the getgrnam() function does not return a valid group 433 * name, gname is taken as a group ID. 434 */ 435 int 436 f_group(plan, entry) 437 PLAN *plan; 438 FTSENT *entry; 439 { 440 return (entry->fts_statp->st_gid == plan->g_data); 441 } 442 443 PLAN * 444 c_group(gname) 445 char *gname; 446 { 447 PLAN *new; 448 struct group *g; 449 gid_t gid; 450 451 ftsoptions &= ~FTS_NOSTAT; 452 453 g = getgrnam(gname); 454 if (g == NULL) { 455 gid = atoi(gname); 456 if (gid == 0 && gname[0] != '0') 457 errx(1, "-group: %s: no such group", gname); 458 } else 459 gid = g->gr_gid; 460 461 new = palloc(N_GROUP, f_group); 462 new->g_data = gid; 463 return (new); 464 } 465 466 /* 467 * -inum n functions -- 468 * 469 * True if the file has inode # n. 470 */ 471 int 472 f_inum(plan, entry) 473 PLAN *plan; 474 FTSENT *entry; 475 { 476 COMPARE(entry->fts_statp->st_ino, plan->i_data); 477 } 478 479 PLAN * 480 c_inum(arg) 481 char *arg; 482 { 483 PLAN *new; 484 485 ftsoptions &= ~FTS_NOSTAT; 486 487 new = palloc(N_INUM, f_inum); 488 new->i_data = find_parsenum(new, "-inum", arg, NULL); 489 return (new); 490 } 491 492 /* 493 * -links n functions -- 494 * 495 * True if the file has n links. 496 */ 497 int 498 f_links(plan, entry) 499 PLAN *plan; 500 FTSENT *entry; 501 { 502 COMPARE(entry->fts_statp->st_nlink, plan->l_data); 503 } 504 505 PLAN * 506 c_links(arg) 507 char *arg; 508 { 509 PLAN *new; 510 511 ftsoptions &= ~FTS_NOSTAT; 512 513 new = palloc(N_LINKS, f_links); 514 new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL); 515 return (new); 516 } 517 518 /* 519 * -ls functions -- 520 * 521 * Always true - prints the current entry to stdout in "ls" format. 522 */ 523 int 524 f_ls(plan, entry) 525 PLAN *plan; 526 FTSENT *entry; 527 { 528 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); 529 return (1); 530 } 531 532 PLAN * 533 c_ls() 534 { 535 ftsoptions &= ~FTS_NOSTAT; 536 isoutput = 1; 537 538 return (palloc(N_LS, f_ls)); 539 } 540 541 /* 542 * -mtime n functions -- 543 * 544 * True if the difference between the file modification time and the 545 * current time is n 24 hour periods. 546 */ 547 int 548 f_mtime(plan, entry) 549 PLAN *plan; 550 FTSENT *entry; 551 { 552 extern time_t now; 553 554 COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) / 555 SECSPERDAY, plan->t_data); 556 } 557 558 PLAN * 559 c_mtime(arg) 560 char *arg; 561 { 562 PLAN *new; 563 564 ftsoptions &= ~FTS_NOSTAT; 565 566 new = palloc(N_MTIME, f_mtime); 567 new->t_data = find_parsenum(new, "-mtime", arg, NULL); 568 TIME_CORRECT(new, N_MTIME); 569 return (new); 570 } 571 572 /* 573 * -name functions -- 574 * 575 * True if the basename of the filename being examined 576 * matches pattern using Pattern Matching Notation S3.14 577 */ 578 int 579 f_name(plan, entry) 580 PLAN *plan; 581 FTSENT *entry; 582 { 583 return (!fnmatch(plan->c_data, entry->fts_name, 0)); 584 } 585 586 PLAN * 587 c_name(pattern) 588 char *pattern; 589 { 590 PLAN *new; 591 592 new = palloc(N_NAME, f_name); 593 new->c_data = pattern; 594 return (new); 595 } 596 597 /* 598 * -newer file functions -- 599 * 600 * True if the current file has been modified more recently 601 * then the modification time of the file named by the pathname 602 * file. 603 */ 604 int 605 f_newer(plan, entry) 606 PLAN *plan; 607 FTSENT *entry; 608 { 609 return (entry->fts_statp->st_mtime > plan->t_data); 610 } 611 612 PLAN * 613 c_newer(filename) 614 char *filename; 615 { 616 PLAN *new; 617 struct stat sb; 618 619 ftsoptions &= ~FTS_NOSTAT; 620 621 if (stat(filename, &sb)) 622 err(1, "%s", filename); 623 new = palloc(N_NEWER, f_newer); 624 new->t_data = sb.st_mtime; 625 return (new); 626 } 627 628 /* 629 * -nogroup functions -- 630 * 631 * True if file belongs to a user ID for which the equivalent 632 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 633 */ 634 int 635 f_nogroup(plan, entry) 636 PLAN *plan; 637 FTSENT *entry; 638 { 639 char *group_from_gid(); 640 641 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 1 : 0); 642 } 643 644 PLAN * 645 c_nogroup() 646 { 647 ftsoptions &= ~FTS_NOSTAT; 648 649 return (palloc(N_NOGROUP, f_nogroup)); 650 } 651 652 /* 653 * -nouser functions -- 654 * 655 * True if file belongs to a user ID for which the equivalent 656 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 657 */ 658 int 659 f_nouser(plan, entry) 660 PLAN *plan; 661 FTSENT *entry; 662 { 663 char *user_from_uid(); 664 665 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 1 : 0); 666 } 667 668 PLAN * 669 c_nouser() 670 { 671 ftsoptions &= ~FTS_NOSTAT; 672 673 return (palloc(N_NOUSER, f_nouser)); 674 } 675 676 /* 677 * -path functions -- 678 * 679 * True if the path of the filename being examined 680 * matches pattern using Pattern Matching Notation S3.14 681 */ 682 int 683 f_path(plan, entry) 684 PLAN *plan; 685 FTSENT *entry; 686 { 687 return (!fnmatch(plan->c_data, entry->fts_path, 0)); 688 } 689 690 PLAN * 691 c_path(pattern) 692 char *pattern; 693 { 694 PLAN *new; 695 696 new = palloc(N_NAME, f_path); 697 new->c_data = pattern; 698 return (new); 699 } 700 701 /* 702 * -perm functions -- 703 * 704 * The mode argument is used to represent file mode bits. If it starts 705 * with a leading digit, it's treated as an octal mode, otherwise as a 706 * symbolic mode. 707 */ 708 int 709 f_perm(plan, entry) 710 PLAN *plan; 711 FTSENT *entry; 712 { 713 mode_t mode; 714 715 mode = entry->fts_statp->st_mode & 716 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 717 if (plan->flags == F_ATLEAST) 718 return ((plan->m_data | mode) == mode); 719 else 720 return (mode == plan->m_data); 721 /* NOTREACHED */ 722 } 723 724 PLAN * 725 c_perm(perm) 726 char *perm; 727 { 728 PLAN *new; 729 mode_t *set; 730 731 ftsoptions &= ~FTS_NOSTAT; 732 733 new = palloc(N_PERM, f_perm); 734 735 if (*perm == '-') { 736 new->flags = F_ATLEAST; 737 ++perm; 738 } 739 740 if ((set = setmode(perm)) == NULL) 741 err(1, "-perm: %s: illegal mode string", perm); 742 743 new->m_data = getmode(set, 0); 744 return (new); 745 } 746 747 /* 748 * -print functions -- 749 * 750 * Always true, causes the current pathame to be written to 751 * standard output. 752 */ 753 int 754 f_print(plan, entry) 755 PLAN *plan; 756 FTSENT *entry; 757 { 758 (void)printf("%s\n", entry->fts_path); 759 return (1); 760 } 761 762 PLAN * 763 c_print() 764 { 765 isoutput = 1; 766 767 return (palloc(N_PRINT, f_print)); 768 } 769 770 /* 771 * -prune functions -- 772 * 773 * Prune a portion of the hierarchy. 774 */ 775 int 776 f_prune(plan, entry) 777 PLAN *plan; 778 FTSENT *entry; 779 { 780 extern FTS *tree; 781 782 if (fts_set(tree, entry, FTS_SKIP)) 783 err(1, "%s", entry->fts_path); 784 return (1); 785 } 786 787 PLAN * 788 c_prune() 789 { 790 return (palloc(N_PRUNE, f_prune)); 791 } 792 793 /* 794 * -size n[c] functions -- 795 * 796 * True if the file size in bytes, divided by an implementation defined 797 * value and rounded up to the next integer, is n. If n is followed by 798 * a c, the size is in bytes. 799 */ 800 #define FIND_SIZE 512 801 static int divsize = 1; 802 803 int 804 f_size(plan, entry) 805 PLAN *plan; 806 FTSENT *entry; 807 { 808 off_t size; 809 810 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 811 FIND_SIZE : entry->fts_statp->st_size; 812 COMPARE(size, plan->o_data); 813 } 814 815 PLAN * 816 c_size(arg) 817 char *arg; 818 { 819 PLAN *new; 820 char endch; 821 822 ftsoptions &= ~FTS_NOSTAT; 823 824 new = palloc(N_SIZE, f_size); 825 endch = 'c'; 826 new->o_data = find_parsenum(new, "-size", arg, &endch); 827 if (endch == 'c') 828 divsize = 0; 829 return (new); 830 } 831 832 /* 833 * -type c functions -- 834 * 835 * True if the type of the file is c, where c is b, c, d, p, or f for 836 * block special file, character special file, directory, FIFO, or 837 * regular file, respectively. 838 */ 839 int 840 f_type(plan, entry) 841 PLAN *plan; 842 FTSENT *entry; 843 { 844 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data); 845 } 846 847 PLAN * 848 c_type(typestring) 849 char *typestring; 850 { 851 PLAN *new; 852 mode_t mask; 853 854 ftsoptions &= ~FTS_NOSTAT; 855 856 switch (typestring[0]) { 857 case 'b': 858 mask = S_IFBLK; 859 break; 860 case 'c': 861 mask = S_IFCHR; 862 break; 863 case 'd': 864 mask = S_IFDIR; 865 break; 866 case 'f': 867 mask = S_IFREG; 868 break; 869 case 'l': 870 mask = S_IFLNK; 871 break; 872 case 'p': 873 mask = S_IFIFO; 874 break; 875 case 's': 876 mask = S_IFSOCK; 877 break; 878 default: 879 errx(1, "-type: %s: unknown type", typestring); 880 } 881 882 new = palloc(N_TYPE, f_type); 883 new->m_data = mask; 884 return (new); 885 } 886 887 /* 888 * -user uname functions -- 889 * 890 * True if the file belongs to the user uname. If uname is numeric and 891 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 892 * return a valid user name, uname is taken as a user ID. 893 */ 894 int 895 f_user(plan, entry) 896 PLAN *plan; 897 FTSENT *entry; 898 { 899 return (entry->fts_statp->st_uid == plan->u_data); 900 } 901 902 PLAN * 903 c_user(username) 904 char *username; 905 { 906 PLAN *new; 907 struct passwd *p; 908 uid_t uid; 909 910 ftsoptions &= ~FTS_NOSTAT; 911 912 p = getpwnam(username); 913 if (p == NULL) { 914 uid = atoi(username); 915 if (uid == 0 && username[0] != '0') 916 errx(1, "-user: %s: no such user", username); 917 } else 918 uid = p->pw_uid; 919 920 new = palloc(N_USER, f_user); 921 new->u_data = uid; 922 return (new); 923 } 924 925 /* 926 * -xdev functions -- 927 * 928 * Always true, causes find not to decend past directories that have a 929 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 930 */ 931 PLAN * 932 c_xdev() 933 { 934 ftsoptions |= FTS_XDEV; 935 936 return (palloc(N_XDEV, f_always_true)); 937 } 938 939 /* 940 * ( expression ) functions -- 941 * 942 * True if expression is true. 943 */ 944 int 945 f_expr(plan, entry) 946 PLAN *plan; 947 FTSENT *entry; 948 { 949 register PLAN *p; 950 register int state; 951 952 for (p = plan->p_data[0]; 953 p && (state = (p->eval)(p, entry)); p = p->next); 954 return (state); 955 } 956 957 /* 958 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are 959 * eliminated during phase 2 of find_formplan() --- the '(' node is converted 960 * to a N_EXPR node containing the expression and the ')' node is discarded. 961 */ 962 PLAN * 963 c_openparen() 964 { 965 return (palloc(N_OPENPAREN, (int (*)())-1)); 966 } 967 968 PLAN * 969 c_closeparen() 970 { 971 return (palloc(N_CLOSEPAREN, (int (*)())-1)); 972 } 973 974 /* 975 * ! expression functions -- 976 * 977 * Negation of a primary; the unary NOT operator. 978 */ 979 int 980 f_not(plan, entry) 981 PLAN *plan; 982 FTSENT *entry; 983 { 984 register PLAN *p; 985 register int state; 986 987 for (p = plan->p_data[0]; 988 p && (state = (p->eval)(p, entry)); p = p->next); 989 return (!state); 990 } 991 992 PLAN * 993 c_not() 994 { 995 return (palloc(N_NOT, f_not)); 996 } 997 998 /* 999 * expression -o expression functions -- 1000 * 1001 * Alternation of primaries; the OR operator. The second expression is 1002 * not evaluated if the first expression is true. 1003 */ 1004 int 1005 f_or(plan, entry) 1006 PLAN *plan; 1007 FTSENT *entry; 1008 { 1009 register PLAN *p; 1010 register int state; 1011 1012 for (p = plan->p_data[0]; 1013 p && (state = (p->eval)(p, entry)); p = p->next); 1014 1015 if (state) 1016 return (1); 1017 1018 for (p = plan->p_data[1]; 1019 p && (state = (p->eval)(p, entry)); p = p->next); 1020 return (state); 1021 } 1022 1023 PLAN * 1024 c_or() 1025 { 1026 return (palloc(N_OR, f_or)); 1027 } 1028 1029 static PLAN * 1030 palloc(t, f) 1031 enum ntype t; 1032 int (*f) __P((PLAN *, FTSENT *)); 1033 { 1034 PLAN *new; 1035 1036 if (new = malloc(sizeof(PLAN))) { 1037 new->type = t; 1038 new->eval = f; 1039 new->flags = 0; 1040 new->next = NULL; 1041 return (new); 1042 } 1043 err(1, NULL); 1044 /* NOTREACHED */ 1045 } 1046