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