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