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