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