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