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