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 /* 476 * Try to avoid the confusing error message about relative paths 477 * being potentially not safe. 478 */ 479 if (ftsoptions & FTS_NOCHDIR) 480 errx(1, "%s: forbidden when the current directory cannot be opened", 481 "-delete"); 482 483 return palloc(option); 484 } 485 486 487 /* 488 * always_true -- 489 * 490 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true 491 */ 492 int 493 f_always_true(PLAN *plan __unused, FTSENT *entry __unused) 494 { 495 return 1; 496 } 497 498 /* 499 * -depth functions -- 500 * 501 * With argument: True if the file is at level n. 502 * Without argument: Always true, causes descent of the directory hierarchy 503 * to be done so that all entries in a directory are acted on before the 504 * directory itself. 505 */ 506 int 507 f_depth(PLAN *plan, FTSENT *entry) 508 { 509 if (plan->flags & F_DEPTH) 510 COMPARE(entry->fts_level, plan->d_data); 511 else 512 return 1; 513 } 514 515 PLAN * 516 c_depth(OPTION *option, char ***argvp) 517 { 518 PLAN *new; 519 char *str; 520 521 new = palloc(option); 522 523 str = **argvp; 524 if (str && !(new->flags & F_DEPTH)) { 525 /* skip leading + or - */ 526 if (*str == '+' || *str == '-') 527 str++; 528 /* skip sign */ 529 if (*str == '+' || *str == '-') 530 str++; 531 if (isdigit(*str)) 532 new->flags |= F_DEPTH; 533 } 534 535 if (new->flags & F_DEPTH) { /* -depth n */ 536 char *ndepth; 537 538 ndepth = nextarg(option, argvp); 539 new->d_data = find_parsenum(new, option->name, ndepth, NULL); 540 } else { /* -d */ 541 isdepth = 1; 542 } 543 544 return new; 545 } 546 547 /* 548 * -empty functions -- 549 * 550 * True if the file or directory is empty 551 */ 552 int 553 f_empty(PLAN *plan __unused, FTSENT *entry) 554 { 555 if (S_ISREG(entry->fts_statp->st_mode) && 556 entry->fts_statp->st_size == 0) 557 return 1; 558 if (S_ISDIR(entry->fts_statp->st_mode)) { 559 struct dirent *dp; 560 int empty; 561 DIR *dir; 562 563 empty = 1; 564 dir = opendir(entry->fts_accpath); 565 if (dir == NULL) 566 return 0; 567 for (dp = readdir(dir); dp; dp = readdir(dir)) 568 if (dp->d_name[0] != '.' || 569 (dp->d_name[1] != '\0' && 570 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) { 571 empty = 0; 572 break; 573 } 574 closedir(dir); 575 return empty; 576 } 577 return 0; 578 } 579 580 PLAN * 581 c_empty(OPTION *option, char ***argvp __unused) 582 { 583 ftsoptions &= ~FTS_NOSTAT; 584 585 return palloc(option); 586 } 587 588 /* 589 * [-exec | -execdir | -ok] utility [arg ... ] ; functions -- 590 * 591 * True if the executed utility returns a zero value as exit status. 592 * The end of the primary expression is delimited by a semicolon. If 593 * "{}" occurs anywhere, it gets replaced by the current pathname, 594 * or, in the case of -execdir, the current basename (filename 595 * without leading directory prefix). For -exec and -ok, 596 * the current directory for the execution of utility is the same as 597 * the current directory when the find utility was started, whereas 598 * for -execdir, it is the directory the file resides in. 599 * 600 * The primary -ok differs from -exec in that it requests affirmation 601 * of the user before executing the utility. 602 */ 603 int 604 f_exec(PLAN *plan, FTSENT *entry) 605 { 606 int cnt; 607 pid_t pid; 608 int status; 609 char *file; 610 611 if (entry == NULL && plan->flags & F_EXECPLUS) { 612 if (plan->e_ppos == plan->e_pbnum) 613 return (1); 614 plan->e_argv[plan->e_ppos] = NULL; 615 goto doexec; 616 } 617 618 /* XXX - if file/dir ends in '/' this will not work -- can it? */ 619 if ((plan->flags & F_EXECDIR) && \ 620 (file = strrchr(entry->fts_path, '/'))) 621 file++; 622 else 623 file = entry->fts_path; 624 625 if (plan->flags & F_EXECPLUS) { 626 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL) 627 err(1, NULL); 628 plan->e_len[plan->e_ppos] = strlen(file); 629 plan->e_psize += plan->e_len[plan->e_ppos]; 630 if (++plan->e_ppos < plan->e_pnummax && 631 plan->e_psize < plan->e_psizemax) 632 return (1); 633 plan->e_argv[plan->e_ppos] = NULL; 634 } else { 635 for (cnt = 0; plan->e_argv[cnt]; ++cnt) 636 if (plan->e_len[cnt]) 637 brace_subst(plan->e_orig[cnt], 638 &plan->e_argv[cnt], file, 639 plan->e_len[cnt]); 640 } 641 642 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv)) 643 return 0; 644 645 /* make sure find output is interspersed correctly with subprocesses */ 646 fflush(stdout); 647 fflush(stderr); 648 649 switch (pid = fork()) { 650 case -1: 651 err(1, "fork"); 652 /* NOTREACHED */ 653 case 0: 654 /* change dir back from where we started */ 655 if (!(plan->flags & F_EXECDIR) && 656 !(ftsoptions & FTS_NOCHDIR) && fchdir(dotfd)) { 657 warn("chdir"); 658 _exit(1); 659 } 660 execvp(plan->e_argv[0], plan->e_argv); 661 warn("%s", plan->e_argv[0]); 662 _exit(1); 663 } 664 if (plan->flags & F_EXECPLUS) { 665 while (--plan->e_ppos >= plan->e_pbnum) 666 free(plan->e_argv[plan->e_ppos]); 667 plan->e_ppos = plan->e_pbnum; 668 plan->e_psize = plan->e_pbsize; 669 } 670 pid = waitpid(pid, &status, 0); 671 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); 672 } 673 674 /* 675 * c_exec, c_execdir, c_ok -- 676 * build three parallel arrays, one with pointers to the strings passed 677 * on the command line, one with (possibly duplicated) pointers to the 678 * argv array, and one with integer values that are lengths of the 679 * strings, but also flags meaning that the string has to be massaged. 680 */ 681 PLAN * 682 c_exec(OPTION *option, char ***argvp) 683 { 684 PLAN *new; /* node returned */ 685 long argmax; 686 int cnt, i; 687 char **argv, **ap, **ep, *p; 688 689 /* This would defeat -execdir's intended security. */ 690 if (option->flags & F_EXECDIR && ftsoptions & FTS_NOCHDIR) 691 errx(1, "%s: forbidden when the current directory cannot be opened", 692 "-execdir"); 693 694 /* XXX - was in c_execdir, but seems unnecessary!? 695 ftsoptions &= ~FTS_NOSTAT; 696 */ 697 isoutput = 1; 698 699 /* XXX - this is a change from the previous coding */ 700 new = palloc(option); 701 702 for (ap = argv = *argvp;; ++ap) { 703 if (!*ap) 704 errx(1, 705 "%s: no terminating \";\" or \"+\"", option->name); 706 if (**ap == ';') 707 break; 708 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) { 709 new->flags |= F_EXECPLUS; 710 break; 711 } 712 } 713 714 if (ap == argv) 715 errx(1, "%s: no command specified", option->name); 716 717 cnt = ap - *argvp + 1; 718 if (new->flags & F_EXECPLUS) { 719 new->e_ppos = new->e_pbnum = cnt - 2; 720 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) { 721 warn("sysconf(_SC_ARG_MAX)"); 722 argmax = _POSIX_ARG_MAX; 723 } 724 argmax -= 1024; 725 for (ep = environ; *ep != NULL; ep++) 726 argmax -= strlen(*ep) + 1 + sizeof(*ep); 727 argmax -= 1 + sizeof(*ep); 728 /* 729 * Ensure that -execdir ... {} + does not mix files 730 * from different directories in one invocation. 731 * Files from the same directory should be handled 732 * in one invocation but there is no code for it. 733 */ 734 new->e_pnummax = new->flags & F_EXECDIR ? 1 : argmax / 16; 735 argmax -= sizeof(char *) * new->e_pnummax; 736 if (argmax <= 0) 737 errx(1, "no space for arguments"); 738 new->e_psizemax = argmax; 739 new->e_pbsize = 0; 740 cnt += new->e_pnummax + 1; 741 new->e_next = lastexecplus; 742 lastexecplus = new; 743 } 744 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL) 745 err(1, NULL); 746 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL) 747 err(1, NULL); 748 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL) 749 err(1, NULL); 750 751 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 752 new->e_orig[cnt] = *argv; 753 if (new->flags & F_EXECPLUS) 754 new->e_pbsize += strlen(*argv) + 1; 755 for (p = *argv; *p; ++p) 756 if (!(new->flags & F_EXECPLUS) && p[0] == '{' && 757 p[1] == '}') { 758 if ((new->e_argv[cnt] = 759 malloc(MAXPATHLEN)) == NULL) 760 err(1, NULL); 761 new->e_len[cnt] = MAXPATHLEN; 762 break; 763 } 764 if (!*p) { 765 new->e_argv[cnt] = *argv; 766 new->e_len[cnt] = 0; 767 } 768 } 769 if (new->flags & F_EXECPLUS) { 770 new->e_psize = new->e_pbsize; 771 cnt--; 772 for (i = 0; i < new->e_pnummax; i++) { 773 new->e_argv[cnt] = NULL; 774 new->e_len[cnt] = 0; 775 cnt++; 776 } 777 argv = ap; 778 goto done; 779 } 780 new->e_argv[cnt] = new->e_orig[cnt] = NULL; 781 782 done: *argvp = argv + 1; 783 return new; 784 } 785 786 /* Finish any pending -exec ... {} + functions. */ 787 void 788 finish_execplus(void) 789 { 790 PLAN *p; 791 792 p = lastexecplus; 793 while (p != NULL) { 794 (p->execute)(p, NULL); 795 p = p->e_next; 796 } 797 } 798 799 int 800 f_flags(PLAN *plan, FTSENT *entry) 801 { 802 u_long flags; 803 804 flags = entry->fts_statp->st_flags; 805 if (plan->flags & F_ATLEAST) 806 return (flags | plan->fl_flags) == flags && 807 !(flags & plan->fl_notflags); 808 else if (plan->flags & F_ANY) 809 return (flags & plan->fl_flags) || 810 (flags | plan->fl_notflags) != flags; 811 else 812 return flags == plan->fl_flags && 813 !(plan->fl_flags & plan->fl_notflags); 814 } 815 816 PLAN * 817 c_flags(OPTION *option, char ***argvp) 818 { 819 char *flags_str; 820 PLAN *new; 821 u_long flags, notflags; 822 823 flags_str = nextarg(option, argvp); 824 ftsoptions &= ~FTS_NOSTAT; 825 826 new = palloc(option); 827 828 if (*flags_str == '-') { 829 new->flags |= F_ATLEAST; 830 flags_str++; 831 } else if (*flags_str == '+') { 832 new->flags |= F_ANY; 833 flags_str++; 834 } 835 if (strtofflags(&flags_str, &flags, ¬flags) == 1) 836 errx(1, "%s: %s: illegal flags string", option->name, flags_str); 837 838 new->fl_flags = flags; 839 new->fl_notflags = notflags; 840 return new; 841 } 842 843 /* 844 * -follow functions -- 845 * 846 * Always true, causes symbolic links to be followed on a global 847 * basis. 848 */ 849 PLAN * 850 c_follow(OPTION *option, char ***argvp __unused) 851 { 852 ftsoptions &= ~FTS_PHYSICAL; 853 ftsoptions |= FTS_LOGICAL; 854 855 return palloc(option); 856 } 857 858 /* 859 * -fstype functions -- 860 * 861 * True if the file is of a certain type. 862 */ 863 int 864 f_fstype(PLAN *plan, FTSENT *entry) 865 { 866 static dev_t curdev; /* need a guaranteed illegal dev value */ 867 static int first = 1; 868 struct statfs sb; 869 static int val_flags; 870 static char fstype[sizeof(sb.f_fstypename)]; 871 char *p, save[2] = {0,0}; 872 873 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN) 874 return 0; 875 876 /* Only check when we cross mount point. */ 877 if (first || curdev != entry->fts_statp->st_dev) { 878 curdev = entry->fts_statp->st_dev; 879 880 /* 881 * Statfs follows symlinks; find wants the link's filesystem, 882 * not where it points. 883 */ 884 if (entry->fts_info == FTS_SL || 885 entry->fts_info == FTS_SLNONE) { 886 if ((p = strrchr(entry->fts_accpath, '/')) != NULL) 887 ++p; 888 else 889 p = entry->fts_accpath; 890 save[0] = p[0]; 891 p[0] = '.'; 892 save[1] = p[1]; 893 p[1] = '\0'; 894 } else 895 p = NULL; 896 897 if (statfs(entry->fts_accpath, &sb)) 898 err(1, "%s", entry->fts_accpath); 899 900 if (p) { 901 p[0] = save[0]; 902 p[1] = save[1]; 903 } 904 905 first = 0; 906 907 /* 908 * Further tests may need both of these values, so 909 * always copy both of them. 910 */ 911 val_flags = sb.f_flags; 912 strlcpy(fstype, sb.f_fstypename, sizeof(fstype)); 913 } 914 switch (plan->flags & F_MTMASK) { 915 case F_MTFLAG: 916 return val_flags & plan->mt_data; 917 case F_MTTYPE: 918 return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0); 919 default: 920 abort(); 921 } 922 } 923 924 PLAN * 925 c_fstype(OPTION *option, char ***argvp) 926 { 927 char *fsname; 928 PLAN *new; 929 930 fsname = nextarg(option, argvp); 931 ftsoptions &= ~FTS_NOSTAT; 932 933 new = palloc(option); 934 switch (*fsname) { 935 case 'l': 936 if (!strcmp(fsname, "local")) { 937 new->flags |= F_MTFLAG; 938 new->mt_data = MNT_LOCAL; 939 return new; 940 } 941 break; 942 case 'r': 943 if (!strcmp(fsname, "rdonly")) { 944 new->flags |= F_MTFLAG; 945 new->mt_data = MNT_RDONLY; 946 return new; 947 } 948 break; 949 } 950 951 new->flags |= F_MTTYPE; 952 new->c_data = fsname; 953 return new; 954 } 955 956 /* 957 * -group gname functions -- 958 * 959 * True if the file belongs to the group gname. If gname is numeric and 960 * an equivalent of the getgrnam() function does not return a valid group 961 * name, gname is taken as a group ID. 962 */ 963 int 964 f_group(PLAN *plan, FTSENT *entry) 965 { 966 COMPARE(entry->fts_statp->st_gid, plan->g_data); 967 } 968 969 PLAN * 970 c_group(OPTION *option, char ***argvp) 971 { 972 char *gname; 973 PLAN *new; 974 struct group *g; 975 gid_t gid; 976 977 gname = nextarg(option, argvp); 978 ftsoptions &= ~FTS_NOSTAT; 979 980 new = palloc(option); 981 g = getgrnam(gname); 982 if (g == NULL) { 983 char* cp = gname; 984 if (gname[0] == '-' || gname[0] == '+') 985 gname++; 986 gid = atoi(gname); 987 if (gid == 0 && gname[0] != '0') 988 errx(1, "%s: %s: no such group", option->name, gname); 989 gid = find_parsenum(new, option->name, cp, NULL); 990 } else 991 gid = g->gr_gid; 992 993 new->g_data = gid; 994 return new; 995 } 996 997 /* 998 * -ignore_readdir_race functions -- 999 * 1000 * Always true. Ignore errors which occur if a file or a directory 1001 * in a starting point gets deleted between reading the name and calling 1002 * stat on it while find is traversing the starting point. 1003 */ 1004 1005 PLAN * 1006 c_ignore_readdir_race(OPTION *option, char ***argvp __unused) 1007 { 1008 if (strcmp(option->name, "-ignore_readdir_race") == 0) 1009 ignore_readdir_race = 1; 1010 else 1011 ignore_readdir_race = 0; 1012 1013 return palloc(option); 1014 } 1015 1016 /* 1017 * -inum n functions -- 1018 * 1019 * True if the file has inode # n. 1020 */ 1021 int 1022 f_inum(PLAN *plan, FTSENT *entry) 1023 { 1024 COMPARE(entry->fts_statp->st_ino, plan->i_data); 1025 } 1026 1027 PLAN * 1028 c_inum(OPTION *option, char ***argvp) 1029 { 1030 char *inum_str; 1031 PLAN *new; 1032 1033 inum_str = nextarg(option, argvp); 1034 ftsoptions &= ~FTS_NOSTAT; 1035 1036 new = palloc(option); 1037 new->i_data = find_parsenum(new, option->name, inum_str, NULL); 1038 return new; 1039 } 1040 1041 /* 1042 * -samefile FN 1043 * 1044 * True if the file has the same inode (eg hard link) FN 1045 */ 1046 1047 /* f_samefile is just f_inum */ 1048 PLAN * 1049 c_samefile(OPTION *option, char ***argvp) 1050 { 1051 char *fn; 1052 PLAN *new; 1053 struct stat sb; 1054 1055 fn = nextarg(option, argvp); 1056 ftsoptions &= ~FTS_NOSTAT; 1057 1058 new = palloc(option); 1059 if (stat(fn, &sb)) 1060 err(1, "%s", fn); 1061 new->i_data = sb.st_ino; 1062 return new; 1063 } 1064 1065 /* 1066 * -links n functions -- 1067 * 1068 * True if the file has n links. 1069 */ 1070 int 1071 f_links(PLAN *plan, FTSENT *entry) 1072 { 1073 COMPARE(entry->fts_statp->st_nlink, plan->l_data); 1074 } 1075 1076 PLAN * 1077 c_links(OPTION *option, char ***argvp) 1078 { 1079 char *nlinks; 1080 PLAN *new; 1081 1082 nlinks = nextarg(option, argvp); 1083 ftsoptions &= ~FTS_NOSTAT; 1084 1085 new = palloc(option); 1086 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL); 1087 return new; 1088 } 1089 1090 /* 1091 * -ls functions -- 1092 * 1093 * Always true - prints the current entry to stdout in "ls" format. 1094 */ 1095 int 1096 f_ls(PLAN *plan __unused, FTSENT *entry) 1097 { 1098 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); 1099 return 1; 1100 } 1101 1102 PLAN * 1103 c_ls(OPTION *option, char ***argvp __unused) 1104 { 1105 ftsoptions &= ~FTS_NOSTAT; 1106 isoutput = 1; 1107 1108 return palloc(option); 1109 } 1110 1111 /* 1112 * -name functions -- 1113 * 1114 * True if the basename of the filename being examined 1115 * matches pattern using Pattern Matching Notation S3.14 1116 */ 1117 int 1118 f_name(PLAN *plan, FTSENT *entry) 1119 { 1120 char fn[PATH_MAX]; 1121 const char *name; 1122 1123 if (plan->flags & F_LINK) { 1124 name = fn; 1125 if (readlink(entry->fts_path, fn, sizeof(fn)) == -1) 1126 return 0; 1127 } else 1128 name = entry->fts_name; 1129 return !fnmatch(plan->c_data, name, 1130 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 1131 } 1132 1133 PLAN * 1134 c_name(OPTION *option, char ***argvp) 1135 { 1136 char *pattern; 1137 PLAN *new; 1138 1139 pattern = nextarg(option, argvp); 1140 new = palloc(option); 1141 new->c_data = pattern; 1142 return new; 1143 } 1144 1145 /* 1146 * -newer file functions -- 1147 * 1148 * True if the current file has been modified more recently 1149 * then the modification time of the file named by the pathname 1150 * file. 1151 */ 1152 int 1153 f_newer(PLAN *plan, FTSENT *entry) 1154 { 1155 if (plan->flags & F_TIME_C) 1156 return entry->fts_statp->st_ctime > plan->t_data; 1157 else if (plan->flags & F_TIME_A) 1158 return entry->fts_statp->st_atime > plan->t_data; 1159 else if (plan->flags & F_TIME_B) 1160 return entry->fts_statp->st_birthtime > plan->t_data; 1161 else 1162 return entry->fts_statp->st_mtime > plan->t_data; 1163 } 1164 1165 PLAN * 1166 c_newer(OPTION *option, char ***argvp) 1167 { 1168 char *fn_or_tspec; 1169 PLAN *new; 1170 struct stat sb; 1171 1172 fn_or_tspec = nextarg(option, argvp); 1173 ftsoptions &= ~FTS_NOSTAT; 1174 1175 new = palloc(option); 1176 /* compare against what */ 1177 if (option->flags & F_TIME2_T) { 1178 new->t_data = get_date(fn_or_tspec); 1179 if (new->t_data == (time_t) -1) 1180 errx(1, "Can't parse date/time: %s", fn_or_tspec); 1181 } else { 1182 if (stat(fn_or_tspec, &sb)) 1183 err(1, "%s", fn_or_tspec); 1184 if (option->flags & F_TIME2_C) 1185 new->t_data = sb.st_ctime; 1186 else if (option->flags & F_TIME2_A) 1187 new->t_data = sb.st_atime; 1188 else if (option->flags & F_TIME2_B) 1189 new->t_data = sb.st_birthtime; 1190 else 1191 new->t_data = sb.st_mtime; 1192 } 1193 return new; 1194 } 1195 1196 /* 1197 * -nogroup functions -- 1198 * 1199 * True if file belongs to a user ID for which the equivalent 1200 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 1201 */ 1202 int 1203 f_nogroup(PLAN *plan __unused, FTSENT *entry) 1204 { 1205 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL; 1206 } 1207 1208 PLAN * 1209 c_nogroup(OPTION *option, char ***argvp __unused) 1210 { 1211 ftsoptions &= ~FTS_NOSTAT; 1212 1213 return palloc(option); 1214 } 1215 1216 /* 1217 * -nouser functions -- 1218 * 1219 * True if file belongs to a user ID for which the equivalent 1220 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 1221 */ 1222 int 1223 f_nouser(PLAN *plan __unused, FTSENT *entry) 1224 { 1225 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL; 1226 } 1227 1228 PLAN * 1229 c_nouser(OPTION *option, char ***argvp __unused) 1230 { 1231 ftsoptions &= ~FTS_NOSTAT; 1232 1233 return palloc(option); 1234 } 1235 1236 /* 1237 * -path functions -- 1238 * 1239 * True if the path of the filename being examined 1240 * matches pattern using Pattern Matching Notation S3.14 1241 */ 1242 int 1243 f_path(PLAN *plan, FTSENT *entry) 1244 { 1245 return !fnmatch(plan->c_data, entry->fts_path, 1246 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 1247 } 1248 1249 /* c_path is the same as c_name */ 1250 1251 /* 1252 * -perm functions -- 1253 * 1254 * The mode argument is used to represent file mode bits. If it starts 1255 * with a leading digit, it's treated as an octal mode, otherwise as a 1256 * symbolic mode. 1257 */ 1258 int 1259 f_perm(PLAN *plan, FTSENT *entry) 1260 { 1261 mode_t mode; 1262 1263 mode = entry->fts_statp->st_mode & 1264 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 1265 if (plan->flags & F_ATLEAST) 1266 return (plan->m_data | mode) == mode; 1267 else if (plan->flags & F_ANY) 1268 return (mode & plan->m_data); 1269 else 1270 return mode == plan->m_data; 1271 /* NOTREACHED */ 1272 } 1273 1274 PLAN * 1275 c_perm(OPTION *option, char ***argvp) 1276 { 1277 char *perm; 1278 PLAN *new; 1279 mode_t *set; 1280 1281 perm = nextarg(option, argvp); 1282 ftsoptions &= ~FTS_NOSTAT; 1283 1284 new = palloc(option); 1285 1286 if (*perm == '-') { 1287 new->flags |= F_ATLEAST; 1288 ++perm; 1289 } else if (*perm == '+') { 1290 new->flags |= F_ANY; 1291 ++perm; 1292 } 1293 1294 if ((set = setmode(perm)) == NULL) 1295 errx(1, "%s: %s: illegal mode string", option->name, perm); 1296 1297 new->m_data = getmode(set, 0); 1298 free(set); 1299 return new; 1300 } 1301 1302 /* 1303 * -print functions -- 1304 * 1305 * Always true, causes the current pathname to be written to 1306 * standard output. 1307 */ 1308 int 1309 f_print(PLAN *plan __unused, FTSENT *entry) 1310 { 1311 (void)puts(entry->fts_path); 1312 return 1; 1313 } 1314 1315 PLAN * 1316 c_print(OPTION *option, char ***argvp __unused) 1317 { 1318 isoutput = 1; 1319 1320 return palloc(option); 1321 } 1322 1323 /* 1324 * -print0 functions -- 1325 * 1326 * Always true, causes the current pathname to be written to 1327 * standard output followed by a NUL character 1328 */ 1329 int 1330 f_print0(PLAN *plan __unused, FTSENT *entry) 1331 { 1332 fputs(entry->fts_path, stdout); 1333 fputc('\0', stdout); 1334 return 1; 1335 } 1336 1337 /* c_print0 is the same as c_print */ 1338 1339 /* 1340 * -prune functions -- 1341 * 1342 * Prune a portion of the hierarchy. 1343 */ 1344 int 1345 f_prune(PLAN *plan __unused, FTSENT *entry) 1346 { 1347 if (fts_set(tree, entry, FTS_SKIP)) 1348 err(1, "%s", entry->fts_path); 1349 return 1; 1350 } 1351 1352 /* c_prune == c_simple */ 1353 1354 /* 1355 * -regex functions -- 1356 * 1357 * True if the whole path of the file matches pattern using 1358 * regular expression. 1359 */ 1360 int 1361 f_regex(PLAN *plan, FTSENT *entry) 1362 { 1363 char *str; 1364 int len; 1365 regex_t *pre; 1366 regmatch_t pmatch; 1367 int errcode; 1368 char errbuf[LINE_MAX]; 1369 int matched; 1370 1371 pre = plan->re_data; 1372 str = entry->fts_path; 1373 len = strlen(str); 1374 matched = 0; 1375 1376 pmatch.rm_so = 0; 1377 pmatch.rm_eo = len; 1378 1379 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND); 1380 1381 if (errcode != 0 && errcode != REG_NOMATCH) { 1382 regerror(errcode, pre, errbuf, sizeof errbuf); 1383 errx(1, "%s: %s", 1384 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf); 1385 } 1386 1387 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len) 1388 matched = 1; 1389 1390 return matched; 1391 } 1392 1393 PLAN * 1394 c_regex(OPTION *option, char ***argvp) 1395 { 1396 PLAN *new; 1397 char *pattern; 1398 regex_t *pre; 1399 int errcode; 1400 char errbuf[LINE_MAX]; 1401 1402 if ((pre = malloc(sizeof(regex_t))) == NULL) 1403 err(1, NULL); 1404 1405 pattern = nextarg(option, argvp); 1406 1407 if ((errcode = regcomp(pre, pattern, 1408 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) { 1409 regerror(errcode, pre, errbuf, sizeof errbuf); 1410 errx(1, "%s: %s: %s", 1411 option->flags & F_IGNCASE ? "-iregex" : "-regex", 1412 pattern, errbuf); 1413 } 1414 1415 new = palloc(option); 1416 new->re_data = pre; 1417 1418 return new; 1419 } 1420 1421 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */ 1422 1423 PLAN * 1424 c_simple(OPTION *option, char ***argvp __unused) 1425 { 1426 return palloc(option); 1427 } 1428 1429 /* 1430 * -size n[c] functions -- 1431 * 1432 * True if the file size in bytes, divided by an implementation defined 1433 * value and rounded up to the next integer, is n. If n is followed by 1434 * one of c k M G T P, the size is in bytes, kilobytes, 1435 * megabytes, gigabytes, terabytes or petabytes respectively. 1436 */ 1437 #define FIND_SIZE 512 1438 static int divsize = 1; 1439 1440 int 1441 f_size(PLAN *plan, FTSENT *entry) 1442 { 1443 off_t size; 1444 1445 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 1446 FIND_SIZE : entry->fts_statp->st_size; 1447 COMPARE(size, plan->o_data); 1448 } 1449 1450 PLAN * 1451 c_size(OPTION *option, char ***argvp) 1452 { 1453 char *size_str; 1454 PLAN *new; 1455 char endch; 1456 off_t scale; 1457 1458 size_str = nextarg(option, argvp); 1459 ftsoptions &= ~FTS_NOSTAT; 1460 1461 new = palloc(option); 1462 endch = 'c'; 1463 new->o_data = find_parsenum(new, option->name, size_str, &endch); 1464 if (endch != '\0') { 1465 divsize = 0; 1466 1467 switch (endch) { 1468 case 'c': /* characters */ 1469 scale = 0x1LL; 1470 break; 1471 case 'k': /* kilobytes 1<<10 */ 1472 scale = 0x400LL; 1473 break; 1474 case 'M': /* megabytes 1<<20 */ 1475 scale = 0x100000LL; 1476 break; 1477 case 'G': /* gigabytes 1<<30 */ 1478 scale = 0x40000000LL; 1479 break; 1480 case 'T': /* terabytes 1<<40 */ 1481 scale = 0x1000000000LL; 1482 break; 1483 case 'P': /* petabytes 1<<50 */ 1484 scale = 0x4000000000000LL; 1485 break; 1486 default: 1487 errx(1, "%s: %s: illegal trailing character", 1488 option->name, size_str); 1489 break; 1490 } 1491 if (new->o_data > QUAD_MAX / scale) 1492 errx(1, "%s: %s: value too large", 1493 option->name, size_str); 1494 new->o_data *= scale; 1495 } 1496 return new; 1497 } 1498 1499 /* 1500 * -type c functions -- 1501 * 1502 * True if the type of the file is c, where c is b, c, d, p, f or w 1503 * for block special file, character special file, directory, FIFO, 1504 * regular file or whiteout respectively. 1505 */ 1506 int 1507 f_type(PLAN *plan, FTSENT *entry) 1508 { 1509 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data; 1510 } 1511 1512 PLAN * 1513 c_type(OPTION *option, char ***argvp) 1514 { 1515 char *typestring; 1516 PLAN *new; 1517 mode_t mask; 1518 1519 typestring = nextarg(option, argvp); 1520 ftsoptions &= ~FTS_NOSTAT; 1521 1522 switch (typestring[0]) { 1523 case 'b': 1524 mask = S_IFBLK; 1525 break; 1526 case 'c': 1527 mask = S_IFCHR; 1528 break; 1529 case 'd': 1530 mask = S_IFDIR; 1531 break; 1532 case 'f': 1533 mask = S_IFREG; 1534 break; 1535 case 'l': 1536 mask = S_IFLNK; 1537 break; 1538 case 'p': 1539 mask = S_IFIFO; 1540 break; 1541 case 's': 1542 mask = S_IFSOCK; 1543 break; 1544 #ifdef FTS_WHITEOUT 1545 case 'w': 1546 mask = S_IFWHT; 1547 ftsoptions |= FTS_WHITEOUT; 1548 break; 1549 #endif /* FTS_WHITEOUT */ 1550 default: 1551 errx(1, "%s: %s: unknown type", option->name, typestring); 1552 } 1553 1554 new = palloc(option); 1555 new->m_data = mask; 1556 return new; 1557 } 1558 1559 /* 1560 * -user uname functions -- 1561 * 1562 * True if the file belongs to the user uname. If uname is numeric and 1563 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 1564 * return a valid user name, uname is taken as a user ID. 1565 */ 1566 int 1567 f_user(PLAN *plan, FTSENT *entry) 1568 { 1569 COMPARE(entry->fts_statp->st_uid, plan->u_data); 1570 } 1571 1572 PLAN * 1573 c_user(OPTION *option, char ***argvp) 1574 { 1575 char *username; 1576 PLAN *new; 1577 struct passwd *p; 1578 uid_t uid; 1579 1580 username = nextarg(option, argvp); 1581 ftsoptions &= ~FTS_NOSTAT; 1582 1583 new = palloc(option); 1584 p = getpwnam(username); 1585 if (p == NULL) { 1586 char* cp = username; 1587 if( username[0] == '-' || username[0] == '+' ) 1588 username++; 1589 uid = atoi(username); 1590 if (uid == 0 && username[0] != '0') 1591 errx(1, "%s: %s: no such user", option->name, username); 1592 uid = find_parsenum(new, option->name, cp, NULL); 1593 } else 1594 uid = p->pw_uid; 1595 1596 new->u_data = uid; 1597 return new; 1598 } 1599 1600 /* 1601 * -xdev functions -- 1602 * 1603 * Always true, causes find not to descend past directories that have a 1604 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 1605 */ 1606 PLAN * 1607 c_xdev(OPTION *option, char ***argvp __unused) 1608 { 1609 ftsoptions |= FTS_XDEV; 1610 1611 return palloc(option); 1612 } 1613 1614 /* 1615 * ( expression ) functions -- 1616 * 1617 * True if expression is true. 1618 */ 1619 int 1620 f_expr(PLAN *plan, FTSENT *entry) 1621 { 1622 PLAN *p; 1623 int state = 0; 1624 1625 for (p = plan->p_data[0]; 1626 p && (state = (p->execute)(p, entry)); p = p->next); 1627 return state; 1628 } 1629 1630 /* 1631 * f_openparen and f_closeparen nodes are temporary place markers. They are 1632 * eliminated during phase 2 of find_formplan() --- the '(' node is converted 1633 * to a f_expr node containing the expression and the ')' node is discarded. 1634 * The functions themselves are only used as constants. 1635 */ 1636 1637 int 1638 f_openparen(PLAN *plan __unused, FTSENT *entry __unused) 1639 { 1640 abort(); 1641 } 1642 1643 int 1644 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused) 1645 { 1646 abort(); 1647 } 1648 1649 /* c_openparen == c_simple */ 1650 /* c_closeparen == c_simple */ 1651 1652 /* 1653 * AND operator. Since AND is implicit, no node is allocated. 1654 */ 1655 PLAN * 1656 c_and(OPTION *option __unused, char ***argvp __unused) 1657 { 1658 return NULL; 1659 } 1660 1661 /* 1662 * ! expression functions -- 1663 * 1664 * Negation of a primary; the unary NOT operator. 1665 */ 1666 int 1667 f_not(PLAN *plan, FTSENT *entry) 1668 { 1669 PLAN *p; 1670 int state = 0; 1671 1672 for (p = plan->p_data[0]; 1673 p && (state = (p->execute)(p, entry)); p = p->next); 1674 return !state; 1675 } 1676 1677 /* c_not == c_simple */ 1678 1679 /* 1680 * expression -o expression functions -- 1681 * 1682 * Alternation of primaries; the OR operator. The second expression is 1683 * not evaluated if the first expression is true. 1684 */ 1685 int 1686 f_or(PLAN *plan, FTSENT *entry) 1687 { 1688 PLAN *p; 1689 int state = 0; 1690 1691 for (p = plan->p_data[0]; 1692 p && (state = (p->execute)(p, entry)); p = p->next); 1693 1694 if (state) 1695 return 1; 1696 1697 for (p = plan->p_data[1]; 1698 p && (state = (p->execute)(p, entry)); p = p->next); 1699 return state; 1700 } 1701 1702 /* c_or == c_simple */ 1703 1704 /* 1705 * -false 1706 * 1707 * Always false. 1708 */ 1709 int 1710 f_false(PLAN *plan __unused, FTSENT *entry __unused) 1711 { 1712 return 0; 1713 } 1714 1715 /* c_false == c_simple */ 1716 1717 /* 1718 * -quit 1719 * 1720 * Exits the program 1721 */ 1722 int 1723 f_quit(PLAN *plan __unused, FTSENT *entry __unused) 1724 { 1725 exit(0); 1726 } 1727 1728 /* c_quit == c_simple */ 1729