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