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 \";\"", 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 #if !defined(__NetBSD__) 875 PLAN * 876 c_fstype(OPTION *option, char ***argvp) 877 { 878 char *fsname; 879 PLAN *new; 880 struct xvfsconf vfc; 881 882 fsname = nextarg(option, argvp); 883 ftsoptions &= ~FTS_NOSTAT; 884 885 new = palloc(option); 886 887 /* 888 * Check first for a filesystem name. 889 */ 890 if (getvfsbyname(fsname, &vfc) == 0) { 891 new->flags |= F_MTTYPE; 892 new->mt_data = vfc.vfc_typenum; 893 return new; 894 } 895 896 switch (*fsname) { 897 case 'l': 898 if (!strcmp(fsname, "local")) { 899 new->flags |= F_MTFLAG; 900 new->mt_data = MNT_LOCAL; 901 return new; 902 } 903 break; 904 case 'r': 905 if (!strcmp(fsname, "rdonly")) { 906 new->flags |= F_MTFLAG; 907 new->mt_data = MNT_RDONLY; 908 return new; 909 } 910 break; 911 } 912 913 /* 914 * We need to make filesystem checks for filesystems 915 * that exists but aren't in the kernel work. 916 */ 917 fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname); 918 new->flags |= F_MTUNKNOWN; 919 return new; 920 } 921 #endif /* __NetBSD__ */ 922 923 /* 924 * -group gname functions -- 925 * 926 * True if the file belongs to the group gname. If gname is numeric and 927 * an equivalent of the getgrnam() function does not return a valid group 928 * name, gname is taken as a group ID. 929 */ 930 int 931 f_group(PLAN *plan, FTSENT *entry) 932 { 933 return entry->fts_statp->st_gid == plan->g_data; 934 } 935 936 PLAN * 937 c_group(OPTION *option, char ***argvp) 938 { 939 char *gname; 940 PLAN *new; 941 struct group *g; 942 gid_t gid; 943 944 gname = nextarg(option, argvp); 945 ftsoptions &= ~FTS_NOSTAT; 946 947 g = getgrnam(gname); 948 if (g == NULL) { 949 gid = atoi(gname); 950 if (gid == 0 && gname[0] != '0') 951 errx(1, "%s: %s: no such group", option->name, gname); 952 } else 953 gid = g->gr_gid; 954 955 new = palloc(option); 956 new->g_data = gid; 957 return new; 958 } 959 960 /* 961 * -inum n functions -- 962 * 963 * True if the file has inode # n. 964 */ 965 int 966 f_inum(PLAN *plan, FTSENT *entry) 967 { 968 COMPARE(entry->fts_statp->st_ino, plan->i_data); 969 } 970 971 PLAN * 972 c_inum(OPTION *option, char ***argvp) 973 { 974 char *inum_str; 975 PLAN *new; 976 977 inum_str = nextarg(option, argvp); 978 ftsoptions &= ~FTS_NOSTAT; 979 980 new = palloc(option); 981 new->i_data = find_parsenum(new, option->name, inum_str, NULL); 982 return new; 983 } 984 985 /* 986 * -links n functions -- 987 * 988 * True if the file has n links. 989 */ 990 int 991 f_links(PLAN *plan, FTSENT *entry) 992 { 993 COMPARE(entry->fts_statp->st_nlink, plan->l_data); 994 } 995 996 PLAN * 997 c_links(OPTION *option, char ***argvp) 998 { 999 char *nlinks; 1000 PLAN *new; 1001 1002 nlinks = nextarg(option, argvp); 1003 ftsoptions &= ~FTS_NOSTAT; 1004 1005 new = palloc(option); 1006 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL); 1007 return new; 1008 } 1009 1010 /* 1011 * -ls functions -- 1012 * 1013 * Always true - prints the current entry to stdout in "ls" format. 1014 */ 1015 int 1016 f_ls(PLAN *plan __unused, FTSENT *entry) 1017 { 1018 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); 1019 return 1; 1020 } 1021 1022 PLAN * 1023 c_ls(OPTION *option, char ***argvp __unused) 1024 { 1025 ftsoptions &= ~FTS_NOSTAT; 1026 isoutput = 1; 1027 1028 return palloc(option); 1029 } 1030 1031 /* 1032 * -name functions -- 1033 * 1034 * True if the basename of the filename being examined 1035 * matches pattern using Pattern Matching Notation S3.14 1036 */ 1037 int 1038 f_name(PLAN *plan, FTSENT *entry) 1039 { 1040 return !fnmatch(plan->c_data, entry->fts_name, 1041 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 1042 } 1043 1044 PLAN * 1045 c_name(OPTION *option, char ***argvp) 1046 { 1047 char *pattern; 1048 PLAN *new; 1049 1050 pattern = nextarg(option, argvp); 1051 new = palloc(option); 1052 new->c_data = pattern; 1053 return new; 1054 } 1055 1056 /* 1057 * -newer file functions -- 1058 * 1059 * True if the current file has been modified more recently 1060 * then the modification time of the file named by the pathname 1061 * file. 1062 */ 1063 int 1064 f_newer(PLAN *plan, FTSENT *entry) 1065 { 1066 if (plan->flags & F_TIME_C) 1067 return entry->fts_statp->st_ctime > plan->t_data; 1068 else if (plan->flags & F_TIME_A) 1069 return entry->fts_statp->st_atime > plan->t_data; 1070 else 1071 return entry->fts_statp->st_mtime > plan->t_data; 1072 } 1073 1074 PLAN * 1075 c_newer(OPTION *option, char ***argvp) 1076 { 1077 char *fn_or_tspec; 1078 PLAN *new; 1079 struct stat sb; 1080 1081 fn_or_tspec = nextarg(option, argvp); 1082 ftsoptions &= ~FTS_NOSTAT; 1083 1084 new = palloc(option); 1085 /* compare against what */ 1086 if (option->flags & F_TIME2_T) { 1087 new->t_data = get_date(fn_or_tspec, (struct timeb *) 0); 1088 if (new->t_data == (time_t) -1) 1089 errx(1, "Can't parse date/time: %s", fn_or_tspec); 1090 } else { 1091 if (stat(fn_or_tspec, &sb)) 1092 err(1, "%s", fn_or_tspec); 1093 if (option->flags & F_TIME2_C) 1094 new->t_data = sb.st_ctime; 1095 else if (option->flags & F_TIME2_A) 1096 new->t_data = sb.st_atime; 1097 else 1098 new->t_data = sb.st_mtime; 1099 } 1100 return new; 1101 } 1102 1103 /* 1104 * -nogroup functions -- 1105 * 1106 * True if file belongs to a user ID for which the equivalent 1107 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 1108 */ 1109 int 1110 f_nogroup(PLAN *plan __unused, FTSENT *entry) 1111 { 1112 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL; 1113 } 1114 1115 PLAN * 1116 c_nogroup(OPTION *option, char ***argvp __unused) 1117 { 1118 ftsoptions &= ~FTS_NOSTAT; 1119 1120 return palloc(option); 1121 } 1122 1123 /* 1124 * -nouser functions -- 1125 * 1126 * True if file belongs to a user ID for which the equivalent 1127 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 1128 */ 1129 int 1130 f_nouser(PLAN *plan __unused, FTSENT *entry) 1131 { 1132 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL; 1133 } 1134 1135 PLAN * 1136 c_nouser(OPTION *option, char ***argvp __unused) 1137 { 1138 ftsoptions &= ~FTS_NOSTAT; 1139 1140 return palloc(option); 1141 } 1142 1143 /* 1144 * -path functions -- 1145 * 1146 * True if the path of the filename being examined 1147 * matches pattern using Pattern Matching Notation S3.14 1148 */ 1149 int 1150 f_path(PLAN *plan, FTSENT *entry) 1151 { 1152 return !fnmatch(plan->c_data, entry->fts_path, 1153 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 1154 } 1155 1156 /* c_path is the same as c_name */ 1157 1158 /* 1159 * -perm functions -- 1160 * 1161 * The mode argument is used to represent file mode bits. If it starts 1162 * with a leading digit, it's treated as an octal mode, otherwise as a 1163 * symbolic mode. 1164 */ 1165 int 1166 f_perm(PLAN *plan, FTSENT *entry) 1167 { 1168 mode_t mode; 1169 1170 mode = entry->fts_statp->st_mode & 1171 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 1172 if (plan->flags & F_ATLEAST) 1173 return (plan->m_data | mode) == mode; 1174 else if (plan->flags & F_ANY) 1175 return (mode & plan->m_data); 1176 else 1177 return mode == plan->m_data; 1178 /* NOTREACHED */ 1179 } 1180 1181 PLAN * 1182 c_perm(OPTION *option, char ***argvp) 1183 { 1184 char *perm; 1185 PLAN *new; 1186 mode_t *set; 1187 1188 perm = nextarg(option, argvp); 1189 ftsoptions &= ~FTS_NOSTAT; 1190 1191 new = palloc(option); 1192 1193 if (*perm == '-') { 1194 new->flags |= F_ATLEAST; 1195 ++perm; 1196 } else if (*perm == '+') { 1197 new->flags |= F_ANY; 1198 ++perm; 1199 } 1200 1201 if ((set = setmode(perm)) == NULL) 1202 errx(1, "%s: %s: illegal mode string", option->name, perm); 1203 1204 new->m_data = getmode(set, 0); 1205 free(set); 1206 return new; 1207 } 1208 1209 /* 1210 * -print functions -- 1211 * 1212 * Always true, causes the current pathname to be written to 1213 * standard output. 1214 */ 1215 int 1216 f_print(PLAN *plan __unused, FTSENT *entry) 1217 { 1218 (void)puts(entry->fts_path); 1219 return 1; 1220 } 1221 1222 PLAN * 1223 c_print(OPTION *option, char ***argvp __unused) 1224 { 1225 isoutput = 1; 1226 1227 return palloc(option); 1228 } 1229 1230 /* 1231 * -print0 functions -- 1232 * 1233 * Always true, causes the current pathname to be written to 1234 * standard output followed by a NUL character 1235 */ 1236 int 1237 f_print0(PLAN *plan __unused, FTSENT *entry) 1238 { 1239 fputs(entry->fts_path, stdout); 1240 fputc('\0', stdout); 1241 return 1; 1242 } 1243 1244 /* c_print0 is the same as c_print */ 1245 1246 /* 1247 * -prune functions -- 1248 * 1249 * Prune a portion of the hierarchy. 1250 */ 1251 int 1252 f_prune(PLAN *plan __unused, FTSENT *entry) 1253 { 1254 if (fts_set(tree, entry, FTS_SKIP)) 1255 err(1, "%s", entry->fts_path); 1256 return 1; 1257 } 1258 1259 /* c_prune == c_simple */ 1260 1261 /* 1262 * -regex functions -- 1263 * 1264 * True if the whole path of the file matches pattern using 1265 * regular expression. 1266 */ 1267 int 1268 f_regex(PLAN *plan, FTSENT *entry) 1269 { 1270 char *str; 1271 int len; 1272 regex_t *pre; 1273 regmatch_t pmatch; 1274 int errcode; 1275 char errbuf[LINE_MAX]; 1276 int matched; 1277 1278 pre = plan->re_data; 1279 str = entry->fts_path; 1280 len = strlen(str); 1281 matched = 0; 1282 1283 pmatch.rm_so = 0; 1284 pmatch.rm_eo = len; 1285 1286 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND); 1287 1288 if (errcode != 0 && errcode != REG_NOMATCH) { 1289 regerror(errcode, pre, errbuf, sizeof errbuf); 1290 errx(1, "%s: %s", 1291 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf); 1292 } 1293 1294 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len) 1295 matched = 1; 1296 1297 return matched; 1298 } 1299 1300 PLAN * 1301 c_regex(OPTION *option, char ***argvp) 1302 { 1303 PLAN *new; 1304 char *pattern; 1305 regex_t *pre; 1306 int errcode; 1307 char errbuf[LINE_MAX]; 1308 1309 if ((pre = malloc(sizeof(regex_t))) == NULL) 1310 err(1, NULL); 1311 1312 pattern = nextarg(option, argvp); 1313 1314 if ((errcode = regcomp(pre, pattern, 1315 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) { 1316 regerror(errcode, pre, errbuf, sizeof errbuf); 1317 errx(1, "%s: %s: %s", 1318 option->flags & F_IGNCASE ? "-iregex" : "-regex", 1319 pattern, errbuf); 1320 } 1321 1322 new = palloc(option); 1323 new->re_data = pre; 1324 1325 return new; 1326 } 1327 1328 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */ 1329 1330 PLAN * 1331 c_simple(OPTION *option, char ***argvp __unused) 1332 { 1333 return palloc(option); 1334 } 1335 1336 /* 1337 * -size n[c] functions -- 1338 * 1339 * True if the file size in bytes, divided by an implementation defined 1340 * value and rounded up to the next integer, is n. If n is followed by 1341 * a c, the size is in bytes. 1342 */ 1343 #define FIND_SIZE 512 1344 static int divsize = 1; 1345 1346 int 1347 f_size(PLAN *plan, FTSENT *entry) 1348 { 1349 off_t size; 1350 1351 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 1352 FIND_SIZE : entry->fts_statp->st_size; 1353 COMPARE(size, plan->o_data); 1354 } 1355 1356 PLAN * 1357 c_size(OPTION *option, char ***argvp) 1358 { 1359 char *size_str; 1360 PLAN *new; 1361 char endch; 1362 1363 size_str = nextarg(option, argvp); 1364 ftsoptions &= ~FTS_NOSTAT; 1365 1366 new = palloc(option); 1367 endch = 'c'; 1368 new->o_data = find_parsenum(new, option->name, size_str, &endch); 1369 if (endch == 'c') 1370 divsize = 0; 1371 return new; 1372 } 1373 1374 /* 1375 * -type c functions -- 1376 * 1377 * True if the type of the file is c, where c is b, c, d, p, f or w 1378 * for block special file, character special file, directory, FIFO, 1379 * regular file or whiteout respectively. 1380 */ 1381 int 1382 f_type(PLAN *plan, FTSENT *entry) 1383 { 1384 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data; 1385 } 1386 1387 PLAN * 1388 c_type(OPTION *option, char ***argvp) 1389 { 1390 char *typestring; 1391 PLAN *new; 1392 mode_t mask; 1393 1394 typestring = nextarg(option, argvp); 1395 ftsoptions &= ~FTS_NOSTAT; 1396 1397 switch (typestring[0]) { 1398 case 'b': 1399 mask = S_IFBLK; 1400 break; 1401 case 'c': 1402 mask = S_IFCHR; 1403 break; 1404 case 'd': 1405 mask = S_IFDIR; 1406 break; 1407 case 'f': 1408 mask = S_IFREG; 1409 break; 1410 case 'l': 1411 mask = S_IFLNK; 1412 break; 1413 case 'p': 1414 mask = S_IFIFO; 1415 break; 1416 case 's': 1417 mask = S_IFSOCK; 1418 break; 1419 #ifdef FTS_WHITEOUT 1420 case 'w': 1421 mask = S_IFWHT; 1422 ftsoptions |= FTS_WHITEOUT; 1423 break; 1424 #endif /* FTS_WHITEOUT */ 1425 default: 1426 errx(1, "%s: %s: unknown type", option->name, typestring); 1427 } 1428 1429 new = palloc(option); 1430 new->m_data = mask; 1431 return new; 1432 } 1433 1434 /* 1435 * -user uname functions -- 1436 * 1437 * True if the file belongs to the user uname. If uname is numeric and 1438 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 1439 * return a valid user name, uname is taken as a user ID. 1440 */ 1441 int 1442 f_user(PLAN *plan, FTSENT *entry) 1443 { 1444 return entry->fts_statp->st_uid == plan->u_data; 1445 } 1446 1447 PLAN * 1448 c_user(OPTION *option, char ***argvp) 1449 { 1450 char *username; 1451 PLAN *new; 1452 struct passwd *p; 1453 uid_t uid; 1454 1455 username = nextarg(option, argvp); 1456 ftsoptions &= ~FTS_NOSTAT; 1457 1458 p = getpwnam(username); 1459 if (p == NULL) { 1460 uid = atoi(username); 1461 if (uid == 0 && username[0] != '0') 1462 errx(1, "%s: %s: no such user", option->name, username); 1463 } else 1464 uid = p->pw_uid; 1465 1466 new = palloc(option); 1467 new->u_data = uid; 1468 return new; 1469 } 1470 1471 /* 1472 * -xdev functions -- 1473 * 1474 * Always true, causes find not to descend past directories that have a 1475 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 1476 */ 1477 PLAN * 1478 c_xdev(OPTION *option, char ***argvp __unused) 1479 { 1480 ftsoptions |= FTS_XDEV; 1481 1482 return palloc(option); 1483 } 1484 1485 /* 1486 * ( expression ) functions -- 1487 * 1488 * True if expression is true. 1489 */ 1490 int 1491 f_expr(PLAN *plan, FTSENT *entry) 1492 { 1493 PLAN *p; 1494 int state = 0; 1495 1496 for (p = plan->p_data[0]; 1497 p && (state = (p->execute)(p, entry)); p = p->next); 1498 return state; 1499 } 1500 1501 /* 1502 * f_openparen and f_closeparen nodes are temporary place markers. They are 1503 * eliminated during phase 2 of find_formplan() --- the '(' node is converted 1504 * to a f_expr node containing the expression and the ')' node is discarded. 1505 * The functions themselves are only used as constants. 1506 */ 1507 1508 int 1509 f_openparen(PLAN *plan __unused, FTSENT *entry __unused) 1510 { 1511 abort(); 1512 } 1513 1514 int 1515 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused) 1516 { 1517 abort(); 1518 } 1519 1520 /* c_openparen == c_simple */ 1521 /* c_closeparen == c_simple */ 1522 1523 /* 1524 * AND operator. Since AND is implicit, no node is allocated. 1525 */ 1526 PLAN * 1527 c_and(OPTION *option __unused, char ***argvp __unused) 1528 { 1529 return NULL; 1530 } 1531 1532 /* 1533 * ! expression functions -- 1534 * 1535 * Negation of a primary; the unary NOT operator. 1536 */ 1537 int 1538 f_not(PLAN *plan, FTSENT *entry) 1539 { 1540 PLAN *p; 1541 int state = 0; 1542 1543 for (p = plan->p_data[0]; 1544 p && (state = (p->execute)(p, entry)); p = p->next); 1545 return !state; 1546 } 1547 1548 /* c_not == c_simple */ 1549 1550 /* 1551 * expression -o expression functions -- 1552 * 1553 * Alternation of primaries; the OR operator. The second expression is 1554 * not evaluated if the first expression is true. 1555 */ 1556 int 1557 f_or(PLAN *plan, FTSENT *entry) 1558 { 1559 PLAN *p; 1560 int state = 0; 1561 1562 for (p = plan->p_data[0]; 1563 p && (state = (p->execute)(p, entry)); p = p->next); 1564 1565 if (state) 1566 return 1; 1567 1568 for (p = plan->p_data[1]; 1569 p && (state = (p->execute)(p, entry)); p = p->next); 1570 return state; 1571 } 1572 1573 /* c_or == c_simple */ 1574