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