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