function.c revision 61575
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 61575 2000-06-12 11:12:41Z 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 char *group_from_gid(); 915 916 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1); 917} 918 919PLAN * 920c_nogroup() 921{ 922 ftsoptions &= ~FTS_NOSTAT; 923 924 return (palloc(N_NOGROUP, f_nogroup)); 925} 926 927/* 928 * -nouser functions -- 929 * 930 * True if file belongs to a user ID for which the equivalent 931 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 932 */ 933int 934f_nouser(plan, entry) 935 PLAN *plan; 936 FTSENT *entry; 937{ 938 char *user_from_uid(); 939 940 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1); 941} 942 943PLAN * 944c_nouser() 945{ 946 ftsoptions &= ~FTS_NOSTAT; 947 948 return (palloc(N_NOUSER, f_nouser)); 949} 950 951/* 952 * -path functions -- 953 * 954 * True if the path of the filename being examined 955 * matches pattern using Pattern Matching Notation S3.14 956 */ 957int 958f_path(plan, entry) 959 PLAN *plan; 960 FTSENT *entry; 961{ 962 return (!fnmatch(plan->c_data, entry->fts_path, 0)); 963} 964 965PLAN * 966c_path(pattern) 967 char *pattern; 968{ 969 PLAN *new; 970 971 new = palloc(N_NAME, f_path); 972 new->c_data = pattern; 973 return (new); 974} 975 976/* 977 * -perm functions -- 978 * 979 * The mode argument is used to represent file mode bits. If it starts 980 * with a leading digit, it's treated as an octal mode, otherwise as a 981 * symbolic mode. 982 */ 983int 984f_perm(plan, entry) 985 PLAN *plan; 986 FTSENT *entry; 987{ 988 mode_t mode; 989 990 mode = entry->fts_statp->st_mode & 991 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 992 if (plan->flags == F_ATLEAST) 993 return ((plan->m_data | mode) == mode); 994 else if (plan->flags == F_ANY ) 995 return (plan->m_data & mode); 996 else 997 return (mode == plan->m_data); 998 /* NOTREACHED */ 999} 1000 1001PLAN * 1002c_perm(perm) 1003 char *perm; 1004{ 1005 PLAN *new; 1006 mode_t *set; 1007 1008 ftsoptions &= ~FTS_NOSTAT; 1009 1010 new = palloc(N_PERM, f_perm); 1011 1012 if (*perm == '-') { 1013 new->flags = F_ATLEAST; 1014 ++perm; 1015 } else if (*perm == '+') { 1016 new->flags = F_ANY; 1017 ++perm; 1018 } 1019 1020 if ((set = setmode(perm)) == NULL) 1021 errx(1, "-perm: %s: illegal mode string", perm); 1022 1023 new->m_data = getmode(set, 0); 1024 free(set); 1025 return (new); 1026} 1027 1028/* 1029 * -flags functions -- 1030 * 1031 * The flags argument is used to represent file flags bits. 1032 */ 1033int 1034f_flags(plan, entry) 1035 PLAN *plan; 1036 FTSENT *entry; 1037{ 1038 u_long flags; 1039 1040 flags = entry->fts_statp->st_flags & 1041 (UF_NODUMP | UF_IMMUTABLE | UF_APPEND | UF_OPAQUE | 1042 SF_ARCHIVED | SF_IMMUTABLE | SF_APPEND); 1043 if (plan->flags == F_ATLEAST) 1044 /* note that plan->fl_flags always is a subset of 1045 plan->fl_mask */ 1046 return (flags & plan->fl_mask) == plan->fl_flags; 1047 else 1048 return flags == plan->fl_flags; 1049 /* NOTREACHED */ 1050} 1051 1052PLAN * 1053c_flags(flags_str) 1054 char *flags_str; 1055{ 1056 PLAN *new; 1057 u_long flags, notflags; 1058 1059 ftsoptions &= ~FTS_NOSTAT; 1060 1061 new = palloc(N_FLAGS, f_flags); 1062 1063 if (*flags_str == '-') { 1064 new->flags = F_ATLEAST; 1065 flags_str++; 1066 } 1067 if (setflags(&flags_str, &flags, ¬flags) == 1) 1068 errx(1, "-flags: %s: illegal flags string", flags_str); 1069 1070 new->fl_flags = flags; 1071 new->fl_mask = flags | notflags; 1072#if 0 1073 printf("flags = %08x, mask = %08x (%08x, %08x)\n", 1074 new->fl_flags, new->fl_mask, flags, notflags); 1075#endif 1076 return new; 1077} 1078 1079/* 1080 * -print functions -- 1081 * 1082 * Always true, causes the current pathame to be written to 1083 * standard output. 1084 */ 1085int 1086f_print(plan, entry) 1087 PLAN *plan; 1088 FTSENT *entry; 1089{ 1090 (void)puts(entry->fts_path); 1091 return (1); 1092} 1093 1094PLAN * 1095c_print() 1096{ 1097 isoutput = 1; 1098 1099 return (palloc(N_PRINT, f_print)); 1100} 1101 1102/* 1103 * -print0 functions -- 1104 * 1105 * Always true, causes the current pathame to be written to 1106 * standard output followed by a NUL character 1107 */ 1108int 1109f_print0(plan, entry) 1110 PLAN *plan; 1111 FTSENT *entry; 1112{ 1113 fputs(entry->fts_path, stdout); 1114 fputc('\0', stdout); 1115 return (1); 1116} 1117 1118PLAN * 1119c_print0() 1120{ 1121 isoutput = 1; 1122 1123 return (palloc(N_PRINT0, f_print0)); 1124} 1125 1126/* 1127 * -prune functions -- 1128 * 1129 * Prune a portion of the hierarchy. 1130 */ 1131int 1132f_prune(plan, entry) 1133 PLAN *plan; 1134 FTSENT *entry; 1135{ 1136 extern FTS *tree; 1137 1138 if (fts_set(tree, entry, FTS_SKIP)) 1139 err(1, "%s", entry->fts_path); 1140 return (1); 1141} 1142 1143PLAN * 1144c_prune() 1145{ 1146 return (palloc(N_PRUNE, f_prune)); 1147} 1148 1149/* 1150 * -size n[c] functions -- 1151 * 1152 * True if the file size in bytes, divided by an implementation defined 1153 * value and rounded up to the next integer, is n. If n is followed by 1154 * a c, the size is in bytes. 1155 */ 1156#define FIND_SIZE 512 1157static int divsize = 1; 1158 1159int 1160f_size(plan, entry) 1161 PLAN *plan; 1162 FTSENT *entry; 1163{ 1164 off_t size; 1165 1166 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 1167 FIND_SIZE : entry->fts_statp->st_size; 1168 COMPARE(size, plan->o_data); 1169} 1170 1171PLAN * 1172c_size(arg) 1173 char *arg; 1174{ 1175 PLAN *new; 1176 char endch; 1177 1178 ftsoptions &= ~FTS_NOSTAT; 1179 1180 new = palloc(N_SIZE, f_size); 1181 endch = 'c'; 1182 new->o_data = find_parsenum(new, "-size", arg, &endch); 1183 if (endch == 'c') 1184 divsize = 0; 1185 return (new); 1186} 1187 1188/* 1189 * -type c functions -- 1190 * 1191 * True if the type of the file is c, where c is b, c, d, p, f or w 1192 * for block special file, character special file, directory, FIFO, 1193 * regular file or whiteout respectively. 1194 */ 1195int 1196f_type(plan, entry) 1197 PLAN *plan; 1198 FTSENT *entry; 1199{ 1200 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data); 1201} 1202 1203PLAN * 1204c_type(typestring) 1205 char *typestring; 1206{ 1207 PLAN *new; 1208 mode_t mask; 1209 1210 ftsoptions &= ~FTS_NOSTAT; 1211 1212 switch (typestring[0]) { 1213 case 'b': 1214 mask = S_IFBLK; 1215 break; 1216 case 'c': 1217 mask = S_IFCHR; 1218 break; 1219 case 'd': 1220 mask = S_IFDIR; 1221 break; 1222 case 'f': 1223 mask = S_IFREG; 1224 break; 1225 case 'l': 1226 mask = S_IFLNK; 1227 break; 1228 case 'p': 1229 mask = S_IFIFO; 1230 break; 1231 case 's': 1232 mask = S_IFSOCK; 1233 break; 1234#ifdef FTS_WHITEOUT 1235 case 'w': 1236 mask = S_IFWHT; 1237 ftsoptions |= FTS_WHITEOUT; 1238 break; 1239#endif /* FTS_WHITEOUT */ 1240 default: 1241 errx(1, "-type: %s: unknown type", typestring); 1242 } 1243 1244 new = palloc(N_TYPE, f_type); 1245 new->m_data = mask; 1246 return (new); 1247} 1248 1249/* 1250 * -delete functions -- 1251 * 1252 * True always. Makes it's best shot and continues on regardless. 1253 */ 1254int 1255f_delete(plan, entry) 1256 PLAN *plan; 1257 FTSENT *entry; 1258{ 1259 /* ignore these from fts */ 1260 if (strcmp(entry->fts_accpath, ".") == 0 || 1261 strcmp(entry->fts_accpath, "..") == 0) 1262 return (1); 1263 1264 /* sanity check */ 1265 if (isdepth == 0 || /* depth off */ 1266 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */ 1267 !(ftsoptions & FTS_PHYSICAL) || /* physical off */ 1268 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */ 1269 errx(1, "-delete: insecure options got turned on"); 1270 1271 /* Potentially unsafe - do not accept relative paths whatsoever */ 1272 if (strchr(entry->fts_accpath, '/') != NULL) 1273 errx(1, "-delete: %s: relative path potentially not safe", 1274 entry->fts_accpath); 1275 1276 /* Turn off user immutable bits if running as root */ 1277 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 1278 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 1279 geteuid() == 0) 1280 chflags(entry->fts_accpath, 1281 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 1282 1283 /* rmdir directories, unlink everything else */ 1284 if (S_ISDIR(entry->fts_statp->st_mode)) { 1285 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY) 1286 warn("-delete: rmdir(%s)", entry->fts_path); 1287 } else { 1288 if (unlink(entry->fts_accpath) < 0) 1289 warn("-delete: unlink(%s)", entry->fts_path); 1290 } 1291 1292 /* "succeed" */ 1293 return (1); 1294} 1295 1296PLAN * 1297c_delete() 1298{ 1299 1300 ftsoptions &= ~FTS_NOSTAT; /* no optimise */ 1301 ftsoptions |= FTS_PHYSICAL; /* disable -follow */ 1302 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */ 1303 isoutput = 1; /* possible output */ 1304 isdepth = 1; /* -depth implied */ 1305 1306 return (palloc(N_DELETE, f_delete)); 1307} 1308 1309/* 1310 * -user uname functions -- 1311 * 1312 * True if the file belongs to the user uname. If uname is numeric and 1313 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 1314 * return a valid user name, uname is taken as a user ID. 1315 */ 1316int 1317f_user(plan, entry) 1318 PLAN *plan; 1319 FTSENT *entry; 1320{ 1321 return (entry->fts_statp->st_uid == plan->u_data); 1322} 1323 1324PLAN * 1325c_user(username) 1326 char *username; 1327{ 1328 PLAN *new; 1329 struct passwd *p; 1330 uid_t uid; 1331 1332 ftsoptions &= ~FTS_NOSTAT; 1333 1334 p = getpwnam(username); 1335 if (p == NULL) { 1336 uid = atoi(username); 1337 if (uid == 0 && username[0] != '0') 1338 errx(1, "-user: %s: no such user", username); 1339 } else 1340 uid = p->pw_uid; 1341 1342 new = palloc(N_USER, f_user); 1343 new->u_data = uid; 1344 return (new); 1345} 1346 1347/* 1348 * -xdev functions -- 1349 * 1350 * Always true, causes find not to decend past directories that have a 1351 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 1352 */ 1353PLAN * 1354c_xdev() 1355{ 1356 ftsoptions |= FTS_XDEV; 1357 1358 return (palloc(N_XDEV, f_always_true)); 1359} 1360 1361/* 1362 * ( expression ) functions -- 1363 * 1364 * True if expression is true. 1365 */ 1366int 1367f_expr(plan, entry) 1368 PLAN *plan; 1369 FTSENT *entry; 1370{ 1371 register PLAN *p; 1372 register int state; 1373 1374 state = 0; 1375 for (p = plan->p_data[0]; 1376 p && (state = (p->eval)(p, entry)); p = p->next); 1377 return (state); 1378} 1379 1380/* 1381 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are 1382 * eliminated during phase 2 of find_formplan() --- the '(' node is converted 1383 * to a N_EXPR node containing the expression and the ')' node is discarded. 1384 */ 1385PLAN * 1386c_openparen() 1387{ 1388 return (palloc(N_OPENPAREN, (int (*)())-1)); 1389} 1390 1391PLAN * 1392c_closeparen() 1393{ 1394 return (palloc(N_CLOSEPAREN, (int (*)())-1)); 1395} 1396 1397/* 1398 * ! expression functions -- 1399 * 1400 * Negation of a primary; the unary NOT operator. 1401 */ 1402int 1403f_not(plan, entry) 1404 PLAN *plan; 1405 FTSENT *entry; 1406{ 1407 register PLAN *p; 1408 register int state; 1409 1410 state = 0; 1411 for (p = plan->p_data[0]; 1412 p && (state = (p->eval)(p, entry)); p = p->next); 1413 return (!state); 1414} 1415 1416PLAN * 1417c_not() 1418{ 1419 return (palloc(N_NOT, f_not)); 1420} 1421 1422/* 1423 * expression -o expression functions -- 1424 * 1425 * Alternation of primaries; the OR operator. The second expression is 1426 * not evaluated if the first expression is true. 1427 */ 1428int 1429f_or(plan, entry) 1430 PLAN *plan; 1431 FTSENT *entry; 1432{ 1433 register PLAN *p; 1434 register int state; 1435 1436 state = 0; 1437 for (p = plan->p_data[0]; 1438 p && (state = (p->eval)(p, entry)); p = p->next); 1439 1440 if (state) 1441 return (1); 1442 1443 for (p = plan->p_data[1]; 1444 p && (state = (p->eval)(p, entry)); p = p->next); 1445 return (state); 1446} 1447 1448PLAN * 1449c_or() 1450{ 1451 return (palloc(N_OR, f_or)); 1452} 1453 1454static PLAN * 1455palloc(t, f) 1456 enum ntype t; 1457 int (*f) __P((PLAN *, FTSENT *)); 1458{ 1459 PLAN *new; 1460 1461 if ((new = malloc(sizeof(PLAN))) == NULL) 1462 err(1, NULL); 1463 new->type = t; 1464 new->eval = f; 1465 new->flags = 0; 1466 new->next = NULL; 1467 return (new); 1468} 1469