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