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