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