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