1 /*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Cimarron D. Taylor of the University of California, Berkeley. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95"; 40 #endif 41 #endif /* not lint */ 42 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include <sys/param.h> 47 #include <sys/ucred.h> 48 #include <sys/stat.h> 49 #include <sys/types.h> 50 #include <sys/acl.h> 51 #include <sys/wait.h> 52 #include <sys/mount.h> 53 #include <sys/timeb.h> 54 55 #include <dirent.h> 56 #include <err.h> 57 #include <errno.h> 58 #include <fnmatch.h> 59 #include <fts.h> 60 #include <grp.h> 61 #include <limits.h> 62 #include <pwd.h> 63 #include <regex.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <unistd.h> 68 #include <ctype.h> 69 70 #include "find.h" 71 72 static PLAN *palloc(OPTION *); 73 static long long find_parsenum(PLAN *, const char *, char *, char *); 74 static long long find_parsetime(PLAN *, const char *, char *); 75 static char *nextarg(OPTION *, char ***); 76 77 extern char **environ; 78 79 static PLAN *lastexecplus = NULL; 80 81 #define COMPARE(a, b) do { \ 82 switch (plan->flags & F_ELG_MASK) { \ 83 case F_EQUAL: \ 84 return (a == b); \ 85 case F_LESSTHAN: \ 86 return (a < b); \ 87 case F_GREATER: \ 88 return (a > b); \ 89 default: \ 90 abort(); \ 91 } \ 92 } while(0) 93 94 static PLAN * 95 palloc(OPTION *option) 96 { 97 PLAN *new; 98 99 if ((new = malloc(sizeof(PLAN))) == NULL) 100 err(1, NULL); 101 new->execute = option->execute; 102 new->flags = option->flags; 103 new->next = NULL; 104 return new; 105 } 106 107 /* 108 * find_parsenum -- 109 * Parse a string of the form [+-]# and return the value. 110 */ 111 static long long 112 find_parsenum(PLAN *plan, const char *option, char *vp, char *endch) 113 { 114 long long value; 115 char *endchar, *str; /* Pointer to character ending conversion. */ 116 117 /* Determine comparison from leading + or -. */ 118 str = vp; 119 switch (*str) { 120 case '+': 121 ++str; 122 plan->flags |= F_GREATER; 123 break; 124 case '-': 125 ++str; 126 plan->flags |= F_LESSTHAN; 127 break; 128 default: 129 plan->flags |= F_EQUAL; 130 break; 131 } 132 133 /* 134 * Convert the string with strtoq(). Note, if strtoq() returns zero 135 * and endchar points to the beginning of the string we know we have 136 * a syntax error. 137 */ 138 value = strtoq(str, &endchar, 10); 139 if (value == 0 && endchar == str) 140 errx(1, "%s: %s: illegal numeric value", option, vp); 141 if (endchar[0] && endch == NULL) 142 errx(1, "%s: %s: illegal trailing character", option, vp); 143 if (endch) 144 *endch = endchar[0]; 145 return value; 146 } 147 148 /* 149 * find_parsetime -- 150 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value. 151 */ 152 static long long 153 find_parsetime(PLAN *plan, const char *option, char *vp) 154 { 155 long long secs, value; 156 char *str, *unit; /* Pointer to character ending conversion. */ 157 158 /* Determine comparison from leading + or -. */ 159 str = vp; 160 switch (*str) { 161 case '+': 162 ++str; 163 plan->flags |= F_GREATER; 164 break; 165 case '-': 166 ++str; 167 plan->flags |= F_LESSTHAN; 168 break; 169 default: 170 plan->flags |= F_EQUAL; 171 break; 172 } 173 174 value = strtoq(str, &unit, 10); 175 if (value == 0 && unit == str) { 176 errx(1, "%s: %s: illegal time value", option, vp); 177 /* NOTREACHED */ 178 } 179 if (*unit == '\0') 180 return value; 181 182 /* Units syntax. */ 183 secs = 0; 184 for (;;) { 185 switch(*unit) { 186 case 's': /* seconds */ 187 secs += value; 188 break; 189 case 'm': /* minutes */ 190 secs += value * 60; 191 break; 192 case 'h': /* hours */ 193 secs += value * 3600; 194 break; 195 case 'd': /* days */ 196 secs += value * 86400; 197 break; 198 case 'w': /* weeks */ 199 secs += value * 604800; 200 break; 201 default: 202 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit); 203 /* NOTREACHED */ 204 } 205 str = unit + 1; 206 if (*str == '\0') /* EOS */ 207 break; 208 value = strtoq(str, &unit, 10); 209 if (value == 0 && unit == str) { 210 errx(1, "%s: %s: illegal time value", option, vp); 211 /* NOTREACHED */ 212 } 213 if (*unit == '\0') { 214 errx(1, "%s: %s: missing trailing unit", option, vp); 215 /* NOTREACHED */ 216 } 217 } 218 plan->flags |= F_EXACTTIME; 219 return secs; 220 } 221 222 /* 223 * nextarg -- 224 * Check that another argument still exists, return a pointer to it, 225 * and increment the argument vector pointer. 226 */ 227 static char * 228 nextarg(OPTION *option, char ***argvp) 229 { 230 char *arg; 231 232 if ((arg = **argvp) == 0) 233 errx(1, "%s: requires additional arguments", option->name); 234 (*argvp)++; 235 return arg; 236 } /* nextarg() */ 237 238 /* 239 * The value of n for the inode times (atime, birthtime, ctime, mtime) is a 240 * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts 241 * with -n, such that "-mtime -1" would be less than 0 days, which isn't what 242 * the user wanted. Correct so that -1 is "less than 1". 243 */ 244 #define TIME_CORRECT(p) \ 245 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \ 246 ++((p)->t_data); 247 248 /* 249 * -[acm]min n functions -- 250 * 251 * True if the difference between the 252 * file access time (-amin) 253 * file birth time (-Bmin) 254 * last change of file status information (-cmin) 255 * file modification time (-mmin) 256 * and the current time is n min periods. 257 */ 258 int 259 f_Xmin(PLAN *plan, FTSENT *entry) 260 { 261 if (plan->flags & F_TIME_C) { 262 COMPARE((now - entry->fts_statp->st_ctime + 263 60 - 1) / 60, plan->t_data); 264 } else if (plan->flags & F_TIME_A) { 265 COMPARE((now - entry->fts_statp->st_atime + 266 60 - 1) / 60, plan->t_data); 267 } else if (plan->flags & F_TIME_B) { 268 COMPARE((now - entry->fts_statp->st_birthtime + 269 60 - 1) / 60, plan->t_data); 270 } else { 271 COMPARE((now - entry->fts_statp->st_mtime + 272 60 - 1) / 60, plan->t_data); 273 } 274 } 275 276 PLAN * 277 c_Xmin(OPTION *option, char ***argvp) 278 { 279 char *nmins; 280 PLAN *new; 281 282 nmins = nextarg(option, argvp); 283 ftsoptions &= ~FTS_NOSTAT; 284 285 new = palloc(option); 286 new->t_data = find_parsenum(new, option->name, nmins, NULL); 287 TIME_CORRECT(new); 288 return new; 289 } 290 291 /* 292 * -[acm]time n functions -- 293 * 294 * True if the difference between the 295 * file access time (-atime) 296 * file birth time (-Btime) 297 * last change of file status information (-ctime) 298 * file modification time (-mtime) 299 * and the current time is n 24 hour periods. 300 */ 301 302 int 303 f_Xtime(PLAN *plan, FTSENT *entry) 304 { 305 time_t xtime; 306 307 if (plan->flags & F_TIME_A) 308 xtime = entry->fts_statp->st_atime; 309 else if (plan->flags & F_TIME_B) 310 xtime = entry->fts_statp->st_birthtime; 311 else if (plan->flags & F_TIME_C) 312 xtime = entry->fts_statp->st_ctime; 313 else 314 xtime = entry->fts_statp->st_mtime; 315 316 if (plan->flags & F_EXACTTIME) 317 COMPARE(now - xtime, plan->t_data); 318 else 319 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data); 320 } 321 322 PLAN * 323 c_Xtime(OPTION *option, char ***argvp) 324 { 325 char *value; 326 PLAN *new; 327 328 value = nextarg(option, argvp); 329 ftsoptions &= ~FTS_NOSTAT; 330 331 new = palloc(option); 332 new->t_data = find_parsetime(new, option->name, value); 333 if (!(new->flags & F_EXACTTIME)) 334 TIME_CORRECT(new); 335 return new; 336 } 337 338 /* 339 * -maxdepth/-mindepth n functions -- 340 * 341 * Does the same as -prune if the level of the current file is 342 * greater/less than the specified maximum/minimum depth. 343 * 344 * Note that -maxdepth and -mindepth are handled specially in 345 * find_execute() so their f_* functions are set to f_always_true(). 346 */ 347 PLAN * 348 c_mXXdepth(OPTION *option, char ***argvp) 349 { 350 char *dstr; 351 PLAN *new; 352 353 dstr = nextarg(option, argvp); 354 if (dstr[0] == '-') 355 /* all other errors handled by find_parsenum() */ 356 errx(1, "%s: %s: value must be positive", option->name, dstr); 357 358 new = palloc(option); 359 if (option->flags & F_MAXDEPTH) 360 maxdepth = find_parsenum(new, option->name, dstr, NULL); 361 else 362 mindepth = find_parsenum(new, option->name, dstr, NULL); 363 return new; 364 } 365 366 /* 367 * -acl function -- 368 * 369 * Show files with EXTENDED ACL attributes. 370 */ 371 int 372 f_acl(PLAN *plan __unused, FTSENT *entry) 373 { 374 int match, entries; 375 acl_entry_t ae; 376 acl_t facl; 377 378 if (S_ISLNK(entry->fts_statp->st_mode)) 379 return 0; 380 if ((match = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED)) <= 0) { 381 if (match < 0 && errno != EINVAL) 382 warn("%s", entry->fts_accpath); 383 else 384 return 0; 385 } 386 match = 0; 387 if ((facl = acl_get_file(entry->fts_accpath,ACL_TYPE_ACCESS)) != NULL) { 388 if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 1) { 389 /* 390 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS 391 * must have at least three entries (owner, group, 392 * other). 393 */ 394 entries = 1; 395 while (acl_get_entry(facl, ACL_NEXT_ENTRY, &ae) == 1) { 396 if (++entries > 3) { 397 match = 1; 398 break; 399 } 400 } 401 } 402 acl_free(facl); 403 } else 404 warn("%s", entry->fts_accpath); 405 return match; 406 } 407 408 PLAN * 409 c_acl(OPTION *option, char ***argvp __unused) 410 { 411 ftsoptions &= ~FTS_NOSTAT; 412 return (palloc(option)); 413 } 414 415 /* 416 * -delete functions -- 417 * 418 * True always. Makes its best shot and continues on regardless. 419 */ 420 int 421 f_delete(PLAN *plan __unused, FTSENT *entry) 422 { 423 /* ignore these from fts */ 424 if (strcmp(entry->fts_accpath, ".") == 0 || 425 strcmp(entry->fts_accpath, "..") == 0) 426 return 1; 427 428 /* sanity check */ 429 if (isdepth == 0 || /* depth off */ 430 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */ 431 !(ftsoptions & FTS_PHYSICAL) || /* physical off */ 432 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */ 433 errx(1, "-delete: insecure options got turned on"); 434 435 /* Potentially unsafe - do not accept relative paths whatsoever */ 436 if (strchr(entry->fts_accpath, '/') != NULL) 437 errx(1, "-delete: %s: relative path potentially not safe", 438 entry->fts_accpath); 439 440 /* Turn off user immutable bits if running as root */ 441 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 442 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 443 geteuid() == 0) 444 chflags(entry->fts_accpath, 445 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 446 447 /* rmdir directories, unlink everything else */ 448 if (S_ISDIR(entry->fts_statp->st_mode)) { 449 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY) 450 warn("-delete: rmdir(%s)", entry->fts_path); 451 } else { 452 if (unlink(entry->fts_accpath) < 0) 453 warn("-delete: unlink(%s)", entry->fts_path); 454 } 455 456 /* "succeed" */ 457 return 1; 458 } 459 460 PLAN * 461 c_delete(OPTION *option, char ***argvp __unused) 462 { 463 464 ftsoptions &= ~FTS_NOSTAT; /* no optimise */ 465 ftsoptions |= FTS_PHYSICAL; /* disable -follow */ 466 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */ 467 isoutput = 1; /* possible output */ 468 isdepth = 1; /* -depth implied */ 469 470 return palloc(option); 471 } 472 473 474 /* 475 * always_true -- 476 * 477 * Always true, used for -maxdepth, -mindepth, -xdev and -follow 478 */ 479 int 480 f_always_true(PLAN *plan __unused, FTSENT *entry __unused) 481 { 482 return 1; 483 } 484 485 /* 486 * -depth functions -- 487 * 488 * With argument: True if the file is at level n. 489 * Without argument: Always true, causes descent of the directory hierarchy 490 * to be done so that all entries in a directory are acted on before the 491 * directory itself. 492 */ 493 int 494 f_depth(PLAN *plan, FTSENT *entry) 495 { 496 if (plan->flags & F_DEPTH) 497 COMPARE(entry->fts_level, plan->d_data); 498 else 499 return 1; 500 } 501 502 PLAN * 503 c_depth(OPTION *option, char ***argvp) 504 { 505 PLAN *new; 506 char *str; 507 508 new = palloc(option); 509 510 str = **argvp; 511 if (str && !(new->flags & F_DEPTH)) { 512 /* skip leading + or - */ 513 if (*str == '+' || *str == '-') 514 str++; 515 /* skip sign */ 516 if (*str == '+' || *str == '-') 517 str++; 518 if (isdigit(*str)) 519 new->flags |= F_DEPTH; 520 } 521 522 if (new->flags & F_DEPTH) { /* -depth n */ 523 char *ndepth; 524 525 ndepth = nextarg(option, argvp); 526 new->d_data = find_parsenum(new, option->name, ndepth, NULL); 527 } else { /* -d */ 528 isdepth = 1; 529 } 530 531 return new; 532 } 533 534 /* 535 * -empty functions -- 536 * 537 * True if the file or directory is empty 538 */ 539 int 540 f_empty(PLAN *plan __unused, FTSENT *entry) 541 { 542 if (S_ISREG(entry->fts_statp->st_mode) && 543 entry->fts_statp->st_size == 0) 544 return 1; 545 if (S_ISDIR(entry->fts_statp->st_mode)) { 546 struct dirent *dp; 547 int empty; 548 DIR *dir; 549 550 empty = 1; 551 dir = opendir(entry->fts_accpath); 552 if (dir == NULL) 553 err(1, "%s", entry->fts_accpath); 554 for (dp = readdir(dir); dp; dp = readdir(dir)) 555 if (dp->d_name[0] != '.' || 556 (dp->d_name[1] != '\0' && 557 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) { 558 empty = 0; 559 break; 560 } 561 closedir(dir); 562 return empty; 563 } 564 return 0; 565 } 566 567 PLAN * 568 c_empty(OPTION *option, char ***argvp __unused) 569 { 570 ftsoptions &= ~FTS_NOSTAT; 571 572 return palloc(option); 573 } 574 575 /* 576 * [-exec | -execdir | -ok] utility [arg ... ] ; functions -- 577 * 578 * True if the executed utility returns a zero value as exit status. 579 * The end of the primary expression is delimited by a semicolon. If 580 * "{}" occurs anywhere, it gets replaced by the current pathname, 581 * or, in the case of -execdir, the current basename (filename 582 * without leading directory prefix). For -exec and -ok, 583 * the current directory for the execution of utility is the same as 584 * the current directory when the find utility was started, whereas 585 * for -execdir, it is the directory the file resides in. 586 * 587 * The primary -ok differs from -exec in that it requests affirmation 588 * of the user before executing the utility. 589 */ 590 int 591 f_exec(PLAN *plan, FTSENT *entry) 592 { 593 int cnt; 594 pid_t pid; 595 int status; 596 char *file; 597 598 if (entry == NULL && plan->flags & F_EXECPLUS) { 599 if (plan->e_ppos == plan->e_pbnum) 600 return (1); 601 plan->e_argv[plan->e_ppos] = NULL; 602 goto doexec; 603 } 604 605 /* XXX - if file/dir ends in '/' this will not work -- can it? */ 606 if ((plan->flags & F_EXECDIR) && \ 607 (file = strrchr(entry->fts_path, '/'))) 608 file++; 609 else 610 file = entry->fts_path; 611 612 if (plan->flags & F_EXECPLUS) { 613 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL) 614 err(1, NULL); 615 plan->e_len[plan->e_ppos] = strlen(file); 616 plan->e_psize += plan->e_len[plan->e_ppos]; 617 if (++plan->e_ppos < plan->e_pnummax && 618 plan->e_psize < plan->e_psizemax) 619 return (1); 620 plan->e_argv[plan->e_ppos] = NULL; 621 } else { 622 for (cnt = 0; plan->e_argv[cnt]; ++cnt) 623 if (plan->e_len[cnt]) 624 brace_subst(plan->e_orig[cnt], 625 &plan->e_argv[cnt], file, 626 plan->e_len[cnt]); 627 } 628 629 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv)) 630 return 0; 631 632 /* make sure find output is interspersed correctly with subprocesses */ 633 fflush(stdout); 634 fflush(stderr); 635 636 switch (pid = fork()) { 637 case -1: 638 err(1, "fork"); 639 /* NOTREACHED */ 640 case 0: 641 /* change dir back from where we started */ 642 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) { 643 warn("chdir"); 644 _exit(1); 645 } 646 execvp(plan->e_argv[0], plan->e_argv); 647 warn("%s", plan->e_argv[0]); 648 _exit(1); 649 } 650 if (plan->flags & F_EXECPLUS) { 651 while (--plan->e_ppos >= plan->e_pbnum) 652 free(plan->e_argv[plan->e_ppos]); 653 plan->e_ppos = plan->e_pbnum; 654 plan->e_psize = plan->e_pbsize; 655 } 656 pid = waitpid(pid, &status, 0); 657 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); 658 } 659 660 /* 661 * c_exec, c_execdir, c_ok -- 662 * build three parallel arrays, one with pointers to the strings passed 663 * on the command line, one with (possibly duplicated) pointers to the 664 * argv array, and one with integer values that are lengths of the 665 * strings, but also flags meaning that the string has to be massaged. 666 */ 667 PLAN * 668 c_exec(OPTION *option, char ***argvp) 669 { 670 PLAN *new; /* node returned */ 671 long argmax; 672 int cnt, i; 673 char **argv, **ap, **ep, *p; 674 675 /* XXX - was in c_execdir, but seems unnecessary!? 676 ftsoptions &= ~FTS_NOSTAT; 677 */ 678 isoutput = 1; 679 680 /* XXX - this is a change from the previous coding */ 681 new = palloc(option); 682 683 for (ap = argv = *argvp;; ++ap) { 684 if (!*ap) 685 errx(1, 686 "%s: no terminating \";\" or \"+\"", option->name); 687 if (**ap == ';') 688 break; 689 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) { 690 new->flags |= F_EXECPLUS; 691 break; 692 } 693 } 694 695 if (ap == argv) 696 errx(1, "%s: no command specified", option->name); 697 698 cnt = ap - *argvp + 1; 699 if (new->flags & F_EXECPLUS) { 700 new->e_ppos = new->e_pbnum = cnt - 2; 701 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) { 702 warn("sysconf(_SC_ARG_MAX)"); 703 argmax = _POSIX_ARG_MAX; 704 } 705 argmax -= 1024; 706 for (ep = environ; *ep != NULL; ep++) 707 argmax -= strlen(*ep) + 1 + sizeof(*ep); 708 argmax -= 1 + sizeof(*ep); 709 new->e_pnummax = argmax / 16; 710 argmax -= sizeof(char *) * new->e_pnummax; 711 if (argmax <= 0) 712 errx(1, "no space for arguments"); 713 new->e_psizemax = argmax; 714 new->e_pbsize = 0; 715 cnt += new->e_pnummax + 1; 716 new->e_next = lastexecplus; 717 lastexecplus = new; 718 } 719 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL) 720 err(1, NULL); 721 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL) 722 err(1, NULL); 723 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL) 724 err(1, NULL); 725 726 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 727 new->e_orig[cnt] = *argv; 728 if (new->flags & F_EXECPLUS) 729 new->e_pbsize += strlen(*argv) + 1; 730 for (p = *argv; *p; ++p) 731 if (!(new->flags & F_EXECPLUS) && p[0] == '{' && 732 p[1] == '}') { 733 if ((new->e_argv[cnt] = 734 malloc(MAXPATHLEN)) == NULL) 735 err(1, NULL); 736 new->e_len[cnt] = MAXPATHLEN; 737 break; 738 } 739 if (!*p) { 740 new->e_argv[cnt] = *argv; 741 new->e_len[cnt] = 0; 742 } 743 } 744 if (new->flags & F_EXECPLUS) { 745 new->e_psize = new->e_pbsize; 746 cnt--; 747 for (i = 0; i < new->e_pnummax; i++) { 748 new->e_argv[cnt] = NULL; 749 new->e_len[cnt] = 0; 750 cnt++; 751 } 752 argv = ap; 753 goto done; 754 } 755 new->e_argv[cnt] = new->e_orig[cnt] = NULL; 756 757 done: *argvp = argv + 1; 758 return new; 759 } 760 761 /* Finish any pending -exec ... {} + functions. */ 762 void 763 finish_execplus() 764 { 765 PLAN *p; 766 767 p = lastexecplus; 768 while (p != NULL) { 769 (p->execute)(p, NULL); 770 p = p->e_next; 771 } 772 } 773 774 int 775 f_flags(PLAN *plan, FTSENT *entry) 776 { 777 u_long flags; 778 779 flags = entry->fts_statp->st_flags; 780 if (plan->flags & F_ATLEAST) 781 return (flags | plan->fl_flags) == flags && 782 !(flags & plan->fl_notflags); 783 else if (plan->flags & F_ANY) 784 return (flags & plan->fl_flags) || 785 (flags | plan->fl_notflags) != flags; 786 else 787 return flags == plan->fl_flags && 788 !(plan->fl_flags & plan->fl_notflags); 789 } 790 791 PLAN * 792 c_flags(OPTION *option, char ***argvp) 793 { 794 char *flags_str; 795 PLAN *new; 796 u_long flags, notflags; 797 798 flags_str = nextarg(option, argvp); 799 ftsoptions &= ~FTS_NOSTAT; 800 801 new = palloc(option); 802 803 if (*flags_str == '-') { 804 new->flags |= F_ATLEAST; 805 flags_str++; 806 } else if (*flags_str == '+') { 807 new->flags |= F_ANY; 808 flags_str++; 809 } 810 if (strtofflags(&flags_str, &flags, ¬flags) == 1) 811 errx(1, "%s: %s: illegal flags string", option->name, flags_str); 812 813 new->fl_flags = flags; 814 new->fl_notflags = notflags; 815 return new; 816 } 817 818 /* 819 * -follow functions -- 820 * 821 * Always true, causes symbolic links to be followed on a global 822 * basis. 823 */ 824 PLAN * 825 c_follow(OPTION *option, char ***argvp __unused) 826 { 827 ftsoptions &= ~FTS_PHYSICAL; 828 ftsoptions |= FTS_LOGICAL; 829 830 return palloc(option); 831 } 832 833 /* 834 * -fstype functions -- 835 * 836 * True if the file is of a certain type. 837 */ 838 int 839 f_fstype(PLAN *plan, FTSENT *entry) 840 { 841 static dev_t curdev; /* need a guaranteed illegal dev value */ 842 static int first = 1; 843 struct statfs sb; 844 static int val_type, val_flags; 845 char *p, save[2] = {0,0}; 846 847 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN) 848 return 0; 849 850 /* Only check when we cross mount point. */ 851 if (first || curdev != entry->fts_statp->st_dev) { 852 curdev = entry->fts_statp->st_dev; 853 854 /* 855 * Statfs follows symlinks; find wants the link's filesystem, 856 * not where it points. 857 */ 858 if (entry->fts_info == FTS_SL || 859 entry->fts_info == FTS_SLNONE) { 860 if ((p = strrchr(entry->fts_accpath, '/')) != NULL) 861 ++p; 862 else 863 p = entry->fts_accpath; 864 save[0] = p[0]; 865 p[0] = '.'; 866 save[1] = p[1]; 867 p[1] = '\0'; 868 } else 869 p = NULL; 870 871 if (statfs(entry->fts_accpath, &sb)) 872 err(1, "%s", entry->fts_accpath); 873 874 if (p) { 875 p[0] = save[0]; 876 p[1] = save[1]; 877 } 878 879 first = 0; 880 881 /* 882 * Further tests may need both of these values, so 883 * always copy both of them. 884 */ 885 val_flags = sb.f_flags; 886 val_type = sb.f_type; 887 } 888 switch (plan->flags & F_MTMASK) { 889 case F_MTFLAG: 890 return val_flags & plan->mt_data; 891 case F_MTTYPE: 892 return val_type == plan->mt_data; 893 default: 894 abort(); 895 } 896 } 897 898 PLAN * 899 c_fstype(OPTION *option, char ***argvp) 900 { 901 char *fsname; 902 PLAN *new; 903 struct xvfsconf vfc; 904 905 fsname = nextarg(option, argvp); 906 ftsoptions &= ~FTS_NOSTAT; 907 908 new = palloc(option); 909 910 /* 911 * Check first for a filesystem name. 912 */ 913 if (getvfsbyname(fsname, &vfc) == 0) { 914 new->flags |= F_MTTYPE; 915 new->mt_data = vfc.vfc_typenum; 916 return new; 917 } 918 919 switch (*fsname) { 920 case 'l': 921 if (!strcmp(fsname, "local")) { 922 new->flags |= F_MTFLAG; 923 new->mt_data = MNT_LOCAL; 924 return new; 925 } 926 break; 927 case 'r': 928 if (!strcmp(fsname, "rdonly")) { 929 new->flags |= F_MTFLAG; 930 new->mt_data = MNT_RDONLY; 931 return new; 932 } 933 break; 934 } 935 936 /* 937 * We need to make filesystem checks for filesystems 938 * that exists but aren't in the kernel work. 939 */ 940 fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname); 941 new->flags |= F_MTUNKNOWN; 942 return new; 943 } 944 945 /* 946 * -group gname functions -- 947 * 948 * True if the file belongs to the group gname. If gname is numeric and 949 * an equivalent of the getgrnam() function does not return a valid group 950 * name, gname is taken as a group ID. 951 */ 952 int 953 f_group(PLAN *plan, FTSENT *entry) 954 { 955 COMPARE(entry->fts_statp->st_gid, plan->g_data); 956 } 957 958 PLAN * 959 c_group(OPTION *option, char ***argvp) 960 { 961 char *gname; 962 PLAN *new; 963 struct group *g; 964 gid_t gid; 965 966 gname = nextarg(option, argvp); 967 ftsoptions &= ~FTS_NOSTAT; 968 969 new = palloc(option); 970 g = getgrnam(gname); 971 if (g == NULL) { 972 char* cp = gname; 973 if( gname[0] == '-' || gname[0] == '+' ) 974 gname++; 975 gid = atoi(gname); 976 if (gid == 0 && gname[0] != '0') 977 errx(1, "%s: %s: no such group", option->name, gname); 978 gid = find_parsenum(new, option->name, cp, NULL); 979 } else 980 gid = g->gr_gid; 981 982 new->g_data = gid; 983 return new; 984 } 985 986 /* 987 * -inum n functions -- 988 * 989 * True if the file has inode # n. 990 */ 991 int 992 f_inum(PLAN *plan, FTSENT *entry) 993 { 994 COMPARE(entry->fts_statp->st_ino, plan->i_data); 995 } 996 997 PLAN * 998 c_inum(OPTION *option, char ***argvp) 999 { 1000 char *inum_str; 1001 PLAN *new; 1002 1003 inum_str = nextarg(option, argvp); 1004 ftsoptions &= ~FTS_NOSTAT; 1005 1006 new = palloc(option); 1007 new->i_data = find_parsenum(new, option->name, inum_str, NULL); 1008 return new; 1009 } 1010 1011 /* 1012 * -links n functions -- 1013 * 1014 * True if the file has n links. 1015 */ 1016 int 1017 f_links(PLAN *plan, FTSENT *entry) 1018 { 1019 COMPARE(entry->fts_statp->st_nlink, plan->l_data); 1020 } 1021 1022 PLAN * 1023 c_links(OPTION *option, char ***argvp) 1024 { 1025 char *nlinks; 1026 PLAN *new; 1027 1028 nlinks = nextarg(option, argvp); 1029 ftsoptions &= ~FTS_NOSTAT; 1030 1031 new = palloc(option); 1032 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL); 1033 return new; 1034 } 1035 1036 /* 1037 * -ls functions -- 1038 * 1039 * Always true - prints the current entry to stdout in "ls" format. 1040 */ 1041 int 1042 f_ls(PLAN *plan __unused, FTSENT *entry) 1043 { 1044 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); 1045 return 1; 1046 } 1047 1048 PLAN * 1049 c_ls(OPTION *option, char ***argvp __unused) 1050 { 1051 ftsoptions &= ~FTS_NOSTAT; 1052 isoutput = 1; 1053 1054 return palloc(option); 1055 } 1056 1057 /* 1058 * -name functions -- 1059 * 1060 * True if the basename of the filename being examined 1061 * matches pattern using Pattern Matching Notation S3.14 1062 */ 1063 int 1064 f_name(PLAN *plan, FTSENT *entry) 1065 { 1066 return !fnmatch(plan->c_data, entry->fts_name, 1067 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 1068 } 1069 1070 PLAN * 1071 c_name(OPTION *option, char ***argvp) 1072 { 1073 char *pattern; 1074 PLAN *new; 1075 1076 pattern = nextarg(option, argvp); 1077 new = palloc(option); 1078 new->c_data = pattern; 1079 return new; 1080 } 1081 1082 /* 1083 * -newer file functions -- 1084 * 1085 * True if the current file has been modified more recently 1086 * then the modification time of the file named by the pathname 1087 * file. 1088 */ 1089 int 1090 f_newer(PLAN *plan, FTSENT *entry) 1091 { 1092 if (plan->flags & F_TIME_C) 1093 return entry->fts_statp->st_ctime > plan->t_data; 1094 else if (plan->flags & F_TIME_A) 1095 return entry->fts_statp->st_atime > plan->t_data; 1096 else if (plan->flags & F_TIME_B) 1097 return entry->fts_statp->st_birthtime > plan->t_data; 1098 else 1099 return entry->fts_statp->st_mtime > plan->t_data; 1100 } 1101 1102 PLAN * 1103 c_newer(OPTION *option, char ***argvp) 1104 { 1105 char *fn_or_tspec; 1106 PLAN *new; 1107 struct stat sb; 1108 1109 fn_or_tspec = nextarg(option, argvp); 1110 ftsoptions &= ~FTS_NOSTAT; 1111 1112 new = palloc(option); 1113 /* compare against what */ 1114 if (option->flags & F_TIME2_T) { 1115 new->t_data = get_date(fn_or_tspec, (struct timeb *) 0); 1116 if (new->t_data == (time_t) -1) 1117 errx(1, "Can't parse date/time: %s", fn_or_tspec); 1118 } else { 1119 if (stat(fn_or_tspec, &sb)) 1120 err(1, "%s", fn_or_tspec); 1121 if (option->flags & F_TIME2_C) 1122 new->t_data = sb.st_ctime; 1123 else if (option->flags & F_TIME2_A) 1124 new->t_data = sb.st_atime; 1125 else 1126 new->t_data = sb.st_mtime; 1127 } 1128 return new; 1129 } 1130 1131 /* 1132 * -nogroup functions -- 1133 * 1134 * True if file belongs to a user ID for which the equivalent 1135 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 1136 */ 1137 int 1138 f_nogroup(PLAN *plan __unused, FTSENT *entry) 1139 { 1140 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL; 1141 } 1142 1143 PLAN * 1144 c_nogroup(OPTION *option, char ***argvp __unused) 1145 { 1146 ftsoptions &= ~FTS_NOSTAT; 1147 1148 return palloc(option); 1149 } 1150 1151 /* 1152 * -nouser functions -- 1153 * 1154 * True if file belongs to a user ID for which the equivalent 1155 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 1156 */ 1157 int 1158 f_nouser(PLAN *plan __unused, FTSENT *entry) 1159 { 1160 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL; 1161 } 1162 1163 PLAN * 1164 c_nouser(OPTION *option, char ***argvp __unused) 1165 { 1166 ftsoptions &= ~FTS_NOSTAT; 1167 1168 return palloc(option); 1169 } 1170 1171 /* 1172 * -path functions -- 1173 * 1174 * True if the path of the filename being examined 1175 * matches pattern using Pattern Matching Notation S3.14 1176 */ 1177 int 1178 f_path(PLAN *plan, FTSENT *entry) 1179 { 1180 return !fnmatch(plan->c_data, entry->fts_path, 1181 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 1182 } 1183 1184 /* c_path is the same as c_name */ 1185 1186 /* 1187 * -perm functions -- 1188 * 1189 * The mode argument is used to represent file mode bits. If it starts 1190 * with a leading digit, it's treated as an octal mode, otherwise as a 1191 * symbolic mode. 1192 */ 1193 int 1194 f_perm(PLAN *plan, FTSENT *entry) 1195 { 1196 mode_t mode; 1197 1198 mode = entry->fts_statp->st_mode & 1199 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 1200 if (plan->flags & F_ATLEAST) 1201 return (plan->m_data | mode) == mode; 1202 else if (plan->flags & F_ANY) 1203 return (mode & plan->m_data); 1204 else 1205 return mode == plan->m_data; 1206 /* NOTREACHED */ 1207 } 1208 1209 PLAN * 1210 c_perm(OPTION *option, char ***argvp) 1211 { 1212 char *perm; 1213 PLAN *new; 1214 mode_t *set; 1215 1216 perm = nextarg(option, argvp); 1217 ftsoptions &= ~FTS_NOSTAT; 1218 1219 new = palloc(option); 1220 1221 if (*perm == '-') { 1222 new->flags |= F_ATLEAST; 1223 ++perm; 1224 } else if (*perm == '+') { 1225 new->flags |= F_ANY; 1226 ++perm; 1227 } 1228 1229 if ((set = setmode(perm)) == NULL) 1230 errx(1, "%s: %s: illegal mode string", option->name, perm); 1231 1232 new->m_data = getmode(set, 0); 1233 free(set); 1234 return new; 1235 } 1236 1237 /* 1238 * -print functions -- 1239 * 1240 * Always true, causes the current pathname to be written to 1241 * standard output. 1242 */ 1243 int 1244 f_print(PLAN *plan __unused, FTSENT *entry) 1245 { 1246 (void)puts(entry->fts_path); 1247 return 1; 1248 } 1249 1250 PLAN * 1251 c_print(OPTION *option, char ***argvp __unused) 1252 { 1253 isoutput = 1; 1254 1255 return palloc(option); 1256 } 1257 1258 /* 1259 * -print0 functions -- 1260 * 1261 * Always true, causes the current pathname to be written to 1262 * standard output followed by a NUL character 1263 */ 1264 int 1265 f_print0(PLAN *plan __unused, FTSENT *entry) 1266 { 1267 fputs(entry->fts_path, stdout); 1268 fputc('\0', stdout); 1269 return 1; 1270 } 1271 1272 /* c_print0 is the same as c_print */ 1273 1274 /* 1275 * -prune functions -- 1276 * 1277 * Prune a portion of the hierarchy. 1278 */ 1279 int 1280 f_prune(PLAN *plan __unused, FTSENT *entry) 1281 { 1282 if (fts_set(tree, entry, FTS_SKIP)) 1283 err(1, "%s", entry->fts_path); 1284 return 1; 1285 } 1286 1287 /* c_prune == c_simple */ 1288 1289 /* 1290 * -regex functions -- 1291 * 1292 * True if the whole path of the file matches pattern using 1293 * regular expression. 1294 */ 1295 int 1296 f_regex(PLAN *plan, FTSENT *entry) 1297 { 1298 char *str; 1299 int len; 1300 regex_t *pre; 1301 regmatch_t pmatch; 1302 int errcode; 1303 char errbuf[LINE_MAX]; 1304 int matched; 1305 1306 pre = plan->re_data; 1307 str = entry->fts_path; 1308 len = strlen(str); 1309 matched = 0; 1310 1311 pmatch.rm_so = 0; 1312 pmatch.rm_eo = len; 1313 1314 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND); 1315 1316 if (errcode != 0 && errcode != REG_NOMATCH) { 1317 regerror(errcode, pre, errbuf, sizeof errbuf); 1318 errx(1, "%s: %s", 1319 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf); 1320 } 1321 1322 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len) 1323 matched = 1; 1324 1325 return matched; 1326 } 1327 1328 PLAN * 1329 c_regex(OPTION *option, char ***argvp) 1330 { 1331 PLAN *new; 1332 char *pattern; 1333 regex_t *pre; 1334 int errcode; 1335 char errbuf[LINE_MAX]; 1336 1337 if ((pre = malloc(sizeof(regex_t))) == NULL) 1338 err(1, NULL); 1339 1340 pattern = nextarg(option, argvp); 1341 1342 if ((errcode = regcomp(pre, pattern, 1343 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) { 1344 regerror(errcode, pre, errbuf, sizeof errbuf); 1345 errx(1, "%s: %s: %s", 1346 option->flags & F_IGNCASE ? "-iregex" : "-regex", 1347 pattern, errbuf); 1348 } 1349 1350 new = palloc(option); 1351 new->re_data = pre; 1352 1353 return new; 1354 } 1355 1356 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */ 1357 1358 PLAN * 1359 c_simple(OPTION *option, char ***argvp __unused) 1360 { 1361 return palloc(option); 1362 } 1363 1364 /* 1365 * -size n[c] functions -- 1366 * 1367 * True if the file size in bytes, divided by an implementation defined 1368 * value and rounded up to the next integer, is n. If n is followed by 1369 * one of c k M G T P, the size is in bytes, kilobytes, 1370 * megabytes, gigabytes, terabytes or petabytes respectively. 1371 */ 1372 #define FIND_SIZE 512 1373 static int divsize = 1; 1374 1375 int 1376 f_size(PLAN *plan, FTSENT *entry) 1377 { 1378 off_t size; 1379 1380 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 1381 FIND_SIZE : entry->fts_statp->st_size; 1382 COMPARE(size, plan->o_data); 1383 } 1384 1385 PLAN * 1386 c_size(OPTION *option, char ***argvp) 1387 { 1388 char *size_str; 1389 PLAN *new; 1390 char endch; 1391 off_t scale; 1392 1393 size_str = nextarg(option, argvp); 1394 ftsoptions &= ~FTS_NOSTAT; 1395 1396 new = palloc(option); 1397 endch = 'c'; 1398 new->o_data = find_parsenum(new, option->name, size_str, &endch); 1399 if (endch != '\0') { 1400 divsize = 0; 1401 1402 switch (endch) { 1403 case 'c': /* characters */ 1404 scale = 0x1LL; 1405 break; 1406 case 'k': /* kilobytes 1<<10 */ 1407 scale = 0x400LL; 1408 break; 1409 case 'M': /* megabytes 1<<20 */ 1410 scale = 0x100000LL; 1411 break; 1412 case 'G': /* gigabytes 1<<30 */ 1413 scale = 0x40000000LL; 1414 break; 1415 case 'T': /* terabytes 1<<40 */ 1416 scale = 0x1000000000LL; 1417 break; 1418 case 'P': /* petabytes 1<<50 */ 1419 scale = 0x4000000000000LL; 1420 break; 1421 default: 1422 errx(1, "%s: %s: illegal trailing character", 1423 option->name, size_str); 1424 break; 1425 } 1426 if (new->o_data > QUAD_MAX / scale) 1427 errx(1, "%s: %s: value too large", 1428 option->name, size_str); 1429 new->o_data *= scale; 1430 } 1431 return new; 1432 } 1433 1434 /* 1435 * -type c functions -- 1436 * 1437 * True if the type of the file is c, where c is b, c, d, p, f or w 1438 * for block special file, character special file, directory, FIFO, 1439 * regular file or whiteout respectively. 1440 */ 1441 int 1442 f_type(PLAN *plan, FTSENT *entry) 1443 { 1444 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data; 1445 } 1446 1447 PLAN * 1448 c_type(OPTION *option, char ***argvp) 1449 { 1450 char *typestring; 1451 PLAN *new; 1452 mode_t mask; 1453 1454 typestring = nextarg(option, argvp); 1455 ftsoptions &= ~FTS_NOSTAT; 1456 1457 switch (typestring[0]) { 1458 case 'b': 1459 mask = S_IFBLK; 1460 break; 1461 case 'c': 1462 mask = S_IFCHR; 1463 break; 1464 case 'd': 1465 mask = S_IFDIR; 1466 break; 1467 case 'f': 1468 mask = S_IFREG; 1469 break; 1470 case 'l': 1471 mask = S_IFLNK; 1472 break; 1473 case 'p': 1474 mask = S_IFIFO; 1475 break; 1476 case 's': 1477 mask = S_IFSOCK; 1478 break; 1479 #ifdef FTS_WHITEOUT 1480 case 'w': 1481 mask = S_IFWHT; 1482 ftsoptions |= FTS_WHITEOUT; 1483 break; 1484 #endif /* FTS_WHITEOUT */ 1485 default: 1486 errx(1, "%s: %s: unknown type", option->name, typestring); 1487 } 1488 1489 new = palloc(option); 1490 new->m_data = mask; 1491 return new; 1492 } 1493 1494 /* 1495 * -user uname functions -- 1496 * 1497 * True if the file belongs to the user uname. If uname is numeric and 1498 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 1499 * return a valid user name, uname is taken as a user ID. 1500 */ 1501 int 1502 f_user(PLAN *plan, FTSENT *entry) 1503 { 1504 COMPARE(entry->fts_statp->st_uid, plan->u_data); 1505 } 1506 1507 PLAN * 1508 c_user(OPTION *option, char ***argvp) 1509 { 1510 char *username; 1511 PLAN *new; 1512 struct passwd *p; 1513 uid_t uid; 1514 1515 username = nextarg(option, argvp); 1516 ftsoptions &= ~FTS_NOSTAT; 1517 1518 new = palloc(option); 1519 p = getpwnam(username); 1520 if (p == NULL) { 1521 char* cp = username; 1522 if( username[0] == '-' || username[0] == '+' ) 1523 username++; 1524 uid = atoi(username); 1525 if (uid == 0 && username[0] != '0') 1526 errx(1, "%s: %s: no such user", option->name, username); 1527 uid = find_parsenum(new, option->name, cp, NULL); 1528 } else 1529 uid = p->pw_uid; 1530 1531 new->u_data = uid; 1532 return new; 1533 } 1534 1535 /* 1536 * -xdev functions -- 1537 * 1538 * Always true, causes find not to descend past directories that have a 1539 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 1540 */ 1541 PLAN * 1542 c_xdev(OPTION *option, char ***argvp __unused) 1543 { 1544 ftsoptions |= FTS_XDEV; 1545 1546 return palloc(option); 1547 } 1548 1549 /* 1550 * ( expression ) functions -- 1551 * 1552 * True if expression is true. 1553 */ 1554 int 1555 f_expr(PLAN *plan, FTSENT *entry) 1556 { 1557 PLAN *p; 1558 int state = 0; 1559 1560 for (p = plan->p_data[0]; 1561 p && (state = (p->execute)(p, entry)); p = p->next); 1562 return state; 1563 } 1564 1565 /* 1566 * f_openparen and f_closeparen nodes are temporary place markers. They are 1567 * eliminated during phase 2 of find_formplan() --- the '(' node is converted 1568 * to a f_expr node containing the expression and the ')' node is discarded. 1569 * The functions themselves are only used as constants. 1570 */ 1571 1572 int 1573 f_openparen(PLAN *plan __unused, FTSENT *entry __unused) 1574 { 1575 abort(); 1576 } 1577 1578 int 1579 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused) 1580 { 1581 abort(); 1582 } 1583 1584 /* c_openparen == c_simple */ 1585 /* c_closeparen == c_simple */ 1586 1587 /* 1588 * AND operator. Since AND is implicit, no node is allocated. 1589 */ 1590 PLAN * 1591 c_and(OPTION *option __unused, char ***argvp __unused) 1592 { 1593 return NULL; 1594 } 1595 1596 /* 1597 * ! expression functions -- 1598 * 1599 * Negation of a primary; the unary NOT operator. 1600 */ 1601 int 1602 f_not(PLAN *plan, FTSENT *entry) 1603 { 1604 PLAN *p; 1605 int state = 0; 1606 1607 for (p = plan->p_data[0]; 1608 p && (state = (p->execute)(p, entry)); p = p->next); 1609 return !state; 1610 } 1611 1612 /* c_not == c_simple */ 1613 1614 /* 1615 * expression -o expression functions -- 1616 * 1617 * Alternation of primaries; the OR operator. The second expression is 1618 * not evaluated if the first expression is true. 1619 */ 1620 int 1621 f_or(PLAN *plan, FTSENT *entry) 1622 { 1623 PLAN *p; 1624 int state = 0; 1625 1626 for (p = plan->p_data[0]; 1627 p && (state = (p->execute)(p, entry)); p = p->next); 1628 1629 if (state) 1630 return 1; 1631 1632 for (p = plan->p_data[1]; 1633 p && (state = (p->execute)(p, entry)); p = p->next); 1634 return state; 1635 } 1636 1637 /* c_or == c_simple */ 1638