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 static char sccsid[] = "@(#)function.c 8.6 (Berkeley) 4/1/94"; 39 #endif /* not lint */ 40 41 #include <sys/param.h> 42 #include <sys/ucred.h> 43 #include <sys/stat.h> 44 #include <sys/wait.h> 45 #include <sys/mount.h> 46 47 #include <err.h> 48 #include <errno.h> 49 #include <fnmatch.h> 50 #include <fts.h> 51 #include <grp.h> 52 #include <pwd.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 58 #include "find.h" 59 60 #define COMPARE(a, b) { \ 61 switch (plan->flags) { \ 62 case F_EQUAL: \ 63 return (a == b); \ 64 case F_LESSTHAN: \ 65 return (a < b); \ 66 case F_GREATER: \ 67 return (a > b); \ 68 default: \ 69 abort(); \ 70 } \ 71 } 72 73 static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *)))); 74 75 /* 76 * find_parsenum -- 77 * Parse a string of the form [+-]# and return the value. 78 */ 79 static long 80 find_parsenum(plan, option, vp, endch) 81 PLAN *plan; 82 char *option, *vp, *endch; 83 { 84 long value; 85 char *endchar, *str; /* Pointer to character ending conversion. */ 86 87 /* Determine comparison from leading + or -. */ 88 str = vp; 89 switch (*str) { 90 case '+': 91 ++str; 92 plan->flags = F_GREATER; 93 break; 94 case '-': 95 ++str; 96 plan->flags = F_LESSTHAN; 97 break; 98 default: 99 plan->flags = F_EQUAL; 100 break; 101 } 102 103 /* 104 * Convert the string with strtol(). Note, if strtol() returns zero 105 * and endchar points to the beginning of the string we know we have 106 * a syntax error. 107 */ 108 value = strtol(str, &endchar, 10); 109 if (value == 0 && endchar == str) 110 errx(1, "%s: %s: illegal numeric value", option, vp); 111 if (endchar[0] && (endch == NULL || endchar[0] != *endch)) 112 errx(1, "%s: %s: illegal trailing character", option, vp); 113 if (endch) 114 *endch = endchar[0]; 115 return (value); 116 } 117 118 /* 119 * The value of n for the inode times (atime, ctime, and mtime) is a range, 120 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with 121 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the 122 * user wanted. Correct so that -1 is "less than 1". 123 */ 124 #define TIME_CORRECT(p, ttype) \ 125 if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \ 126 ++((p)->t_data); 127 128 /* 129 * -atime n functions -- 130 * 131 * True if the difference between the file access time and the 132 * current time is n 24 hour periods. 133 */ 134 int 135 f_atime(plan, entry) 136 PLAN *plan; 137 FTSENT *entry; 138 { 139 extern time_t now; 140 141 COMPARE((now - entry->fts_statp->st_atime + 142 86400 - 1) / 86400, plan->t_data); 143 } 144 145 PLAN * 146 c_atime(arg) 147 char *arg; 148 { 149 PLAN *new; 150 151 ftsoptions &= ~FTS_NOSTAT; 152 153 new = palloc(N_ATIME, f_atime); 154 new->t_data = find_parsenum(new, "-atime", arg, NULL); 155 TIME_CORRECT(new, N_ATIME); 156 return (new); 157 } 158 /* 159 * -ctime n functions -- 160 * 161 * True if the difference between the last change of file 162 * status information and the current time is n 24 hour periods. 163 */ 164 int 165 f_ctime(plan, entry) 166 PLAN *plan; 167 FTSENT *entry; 168 { 169 extern time_t now; 170 171 COMPARE((now - entry->fts_statp->st_ctime + 172 86400 - 1) / 86400, plan->t_data); 173 } 174 175 PLAN * 176 c_ctime(arg) 177 char *arg; 178 { 179 PLAN *new; 180 181 ftsoptions &= ~FTS_NOSTAT; 182 183 new = palloc(N_CTIME, f_ctime); 184 new->t_data = find_parsenum(new, "-ctime", arg, NULL); 185 TIME_CORRECT(new, N_CTIME); 186 return (new); 187 } 188 189 /* 190 * -depth functions -- 191 * 192 * Always true, causes descent of the directory hierarchy to be done 193 * so that all entries in a directory are acted on before the directory 194 * itself. 195 */ 196 int 197 f_always_true(plan, entry) 198 PLAN *plan; 199 FTSENT *entry; 200 { 201 return (1); 202 } 203 204 PLAN * 205 c_depth() 206 { 207 isdepth = 1; 208 209 return (palloc(N_DEPTH, f_always_true)); 210 } 211 212 /* 213 * [-exec | -ok] utility [arg ... ] ; functions -- 214 * 215 * True if the executed utility returns a zero value as exit status. 216 * The end of the primary expression is delimited by a semicolon. If 217 * "{}" occurs anywhere, it gets replaced by the current pathname. 218 * The current directory for the execution of utility is the same as 219 * the current directory when the find utility was started. 220 * 221 * The primary -ok is different in that it requests affirmation of the 222 * user before executing the utility. 223 */ 224 int 225 f_exec(plan, entry) 226 register PLAN *plan; 227 FTSENT *entry; 228 { 229 extern int dotfd; 230 register int cnt; 231 pid_t pid; 232 int status; 233 234 for (cnt = 0; plan->e_argv[cnt]; ++cnt) 235 if (plan->e_len[cnt]) 236 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt], 237 entry->fts_path, plan->e_len[cnt]); 238 239 if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv)) 240 return (0); 241 242 /* make sure find output is interspersed correctly with subprocesses */ 243 fflush(stdout); 244 245 switch (pid = vfork()) { 246 case -1: 247 err(1, "fork"); 248 /* NOTREACHED */ 249 case 0: 250 if (fchdir(dotfd)) { 251 warn("chdir"); 252 _exit(1); 253 } 254 execvp(plan->e_argv[0], plan->e_argv); 255 warn("%s", plan->e_argv[0]); 256 _exit(1); 257 } 258 pid = waitpid(pid, &status, 0); 259 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); 260 } 261 262 /* 263 * c_exec -- 264 * build three parallel arrays, one with pointers to the strings passed 265 * on the command line, one with (possibly duplicated) pointers to the 266 * argv array, and one with integer values that are lengths of the 267 * strings, but also flags meaning that the string has to be massaged. 268 */ 269 PLAN * 270 c_exec(argvp, isok) 271 char ***argvp; 272 int isok; 273 { 274 PLAN *new; /* node returned */ 275 register int cnt; 276 register char **argv, **ap, *p; 277 278 isoutput = 1; 279 280 new = palloc(N_EXEC, f_exec); 281 if (isok) 282 new->flags = F_NEEDOK; 283 284 for (ap = argv = *argvp;; ++ap) { 285 if (!*ap) 286 errx(1, 287 "%s: no terminating \";\"", isok ? "-ok" : "-exec"); 288 if (**ap == ';') 289 break; 290 } 291 292 cnt = ap - *argvp + 1; 293 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *)); 294 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *)); 295 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int)); 296 297 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 298 new->e_orig[cnt] = *argv; 299 for (p = *argv; *p; ++p) 300 if (p[0] == '{' && p[1] == '}') { 301 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN); 302 new->e_len[cnt] = MAXPATHLEN; 303 break; 304 } 305 if (!*p) { 306 new->e_argv[cnt] = *argv; 307 new->e_len[cnt] = 0; 308 } 309 } 310 new->e_argv[cnt] = new->e_orig[cnt] = NULL; 311 312 *argvp = argv + 1; 313 return (new); 314 } 315 316 /* 317 * -follow functions -- 318 * 319 * Always true, causes symbolic links to be followed on a global 320 * basis. 321 */ 322 PLAN * 323 c_follow() 324 { 325 ftsoptions &= ~FTS_PHYSICAL; 326 ftsoptions |= FTS_LOGICAL; 327 328 return (palloc(N_FOLLOW, f_always_true)); 329 } 330 331 /* 332 * -fstype functions -- 333 * 334 * True if the file is of a certain type. 335 */ 336 int 337 f_fstype(plan, entry) 338 PLAN *plan; 339 FTSENT *entry; 340 { 341 static dev_t curdev; /* need a guaranteed illegal dev value */ 342 static int first = 1; 343 struct statfs sb; 344 static short val; 345 char *p, save[2]; 346 347 /* Only check when we cross mount point. */ 348 if (first || curdev != entry->fts_statp->st_dev) { 349 curdev = entry->fts_statp->st_dev; 350 351 /* 352 * Statfs follows symlinks; find wants the link's file system, 353 * not where it points. 354 */ 355 if (entry->fts_info == FTS_SL || 356 entry->fts_info == FTS_SLNONE) { 357 if ((p = strrchr(entry->fts_accpath, '/')) != NULL) 358 ++p; 359 else 360 p = entry->fts_accpath; 361 save[0] = p[0]; 362 p[0] = '.'; 363 save[1] = p[1]; 364 p[1] = '\0'; 365 366 } else 367 p = NULL; 368 369 if (statfs(entry->fts_accpath, &sb)) 370 err(1, "%s", entry->fts_accpath); 371 372 if (p) { 373 p[0] = save[0]; 374 p[1] = save[1]; 375 } 376 377 first = 0; 378 switch (plan->flags) { 379 case F_MTFLAG: 380 val = sb.f_flags; 381 break; 382 case F_MTTYPE: 383 val = sb.f_type; 384 break; 385 default: 386 abort(); 387 } 388 } 389 switch(plan->flags) { 390 case F_MTFLAG: 391 return (val & plan->mt_data); 392 case F_MTTYPE: 393 return (val == plan->mt_data); 394 default: 395 abort(); 396 } 397 } 398 399 PLAN * 400 c_fstype(arg) 401 char *arg; 402 { 403 register PLAN *new; 404 405 ftsoptions &= ~FTS_NOSTAT; 406 407 new = palloc(N_FSTYPE, f_fstype); 408 switch (*arg) { 409 case 'l': 410 if (!strcmp(arg, "local")) { 411 new->flags = F_MTFLAG; 412 new->mt_data = MNT_LOCAL; 413 return (new); 414 } 415 break; 416 case 'm': 417 if (!strcmp(arg, "mfs")) { 418 new->flags = F_MTTYPE; 419 new->mt_data = MOUNT_MFS; 420 return (new); 421 } 422 break; 423 case 'n': 424 if (!strcmp(arg, "nfs")) { 425 new->flags = F_MTTYPE; 426 new->mt_data = MOUNT_NFS; 427 return (new); 428 } 429 break; 430 case 'p': 431 if (!strcmp(arg, "msdos")) { 432 new->flags = F_MTTYPE; 433 new->mt_data = MOUNT_MSDOS; 434 return (new); 435 } 436 break; 437 case 'r': 438 if (!strcmp(arg, "rdonly")) { 439 new->flags = F_MTFLAG; 440 new->mt_data = MNT_RDONLY; 441 return (new); 442 } 443 break; 444 case 'u': 445 if (!strcmp(arg, "ufs")) { 446 new->flags = F_MTTYPE; 447 new->mt_data = MOUNT_UFS; 448 return (new); 449 } 450 break; 451 } 452 errx(1, "%s: unknown file type", arg); 453 /* NOTREACHED */ 454 } 455 456 /* 457 * -group gname functions -- 458 * 459 * True if the file belongs to the group gname. If gname is numeric and 460 * an equivalent of the getgrnam() function does not return a valid group 461 * name, gname is taken as a group ID. 462 */ 463 int 464 f_group(plan, entry) 465 PLAN *plan; 466 FTSENT *entry; 467 { 468 return (entry->fts_statp->st_gid == plan->g_data); 469 } 470 471 PLAN * 472 c_group(gname) 473 char *gname; 474 { 475 PLAN *new; 476 struct group *g; 477 gid_t gid; 478 479 ftsoptions &= ~FTS_NOSTAT; 480 481 g = getgrnam(gname); 482 if (g == NULL) { 483 gid = atoi(gname); 484 if (gid == 0 && gname[0] != '0') 485 errx(1, "-group: %s: no such group", gname); 486 } else 487 gid = g->gr_gid; 488 489 new = palloc(N_GROUP, f_group); 490 new->g_data = gid; 491 return (new); 492 } 493 494 /* 495 * -inum n functions -- 496 * 497 * True if the file has inode # n. 498 */ 499 int 500 f_inum(plan, entry) 501 PLAN *plan; 502 FTSENT *entry; 503 { 504 COMPARE(entry->fts_statp->st_ino, plan->i_data); 505 } 506 507 PLAN * 508 c_inum(arg) 509 char *arg; 510 { 511 PLAN *new; 512 513 ftsoptions &= ~FTS_NOSTAT; 514 515 new = palloc(N_INUM, f_inum); 516 new->i_data = find_parsenum(new, "-inum", arg, NULL); 517 return (new); 518 } 519 520 /* 521 * -links n functions -- 522 * 523 * True if the file has n links. 524 */ 525 int 526 f_links(plan, entry) 527 PLAN *plan; 528 FTSENT *entry; 529 { 530 COMPARE(entry->fts_statp->st_nlink, plan->l_data); 531 } 532 533 PLAN * 534 c_links(arg) 535 char *arg; 536 { 537 PLAN *new; 538 539 ftsoptions &= ~FTS_NOSTAT; 540 541 new = palloc(N_LINKS, f_links); 542 new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL); 543 return (new); 544 } 545 546 /* 547 * -ls functions -- 548 * 549 * Always true - prints the current entry to stdout in "ls" format. 550 */ 551 int 552 f_ls(plan, entry) 553 PLAN *plan; 554 FTSENT *entry; 555 { 556 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); 557 return (1); 558 } 559 560 PLAN * 561 c_ls() 562 { 563 ftsoptions &= ~FTS_NOSTAT; 564 isoutput = 1; 565 566 return (palloc(N_LS, f_ls)); 567 } 568 569 /* 570 * -mtime n functions -- 571 * 572 * True if the difference between the file modification time and the 573 * current time is n 24 hour periods. 574 */ 575 int 576 f_mtime(plan, entry) 577 PLAN *plan; 578 FTSENT *entry; 579 { 580 extern time_t now; 581 582 COMPARE((now - entry->fts_statp->st_mtime + 86400 - 1) / 583 86400, plan->t_data); 584 } 585 586 PLAN * 587 c_mtime(arg) 588 char *arg; 589 { 590 PLAN *new; 591 592 ftsoptions &= ~FTS_NOSTAT; 593 594 new = palloc(N_MTIME, f_mtime); 595 new->t_data = find_parsenum(new, "-mtime", arg, NULL); 596 TIME_CORRECT(new, N_MTIME); 597 return (new); 598 } 599 600 /* 601 * -name functions -- 602 * 603 * True if the basename of the filename being examined 604 * matches pattern using Pattern Matching Notation S3.14 605 */ 606 int 607 f_name(plan, entry) 608 PLAN *plan; 609 FTSENT *entry; 610 { 611 return (!fnmatch(plan->c_data, entry->fts_name, 0)); 612 } 613 614 PLAN * 615 c_name(pattern) 616 char *pattern; 617 { 618 PLAN *new; 619 620 new = palloc(N_NAME, f_name); 621 new->c_data = pattern; 622 return (new); 623 } 624 625 /* 626 * -newer file functions -- 627 * 628 * True if the current file has been modified more recently 629 * then the modification time of the file named by the pathname 630 * file. 631 */ 632 int 633 f_newer(plan, entry) 634 PLAN *plan; 635 FTSENT *entry; 636 { 637 return (entry->fts_statp->st_mtime > plan->t_data); 638 } 639 640 PLAN * 641 c_newer(filename) 642 char *filename; 643 { 644 PLAN *new; 645 struct stat sb; 646 647 ftsoptions &= ~FTS_NOSTAT; 648 649 if (stat(filename, &sb)) 650 err(1, "%s", filename); 651 new = palloc(N_NEWER, f_newer); 652 new->t_data = sb.st_mtime; 653 return (new); 654 } 655 656 /* 657 * -nogroup functions -- 658 * 659 * True if file belongs to a user ID for which the equivalent 660 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 661 */ 662 int 663 f_nogroup(plan, entry) 664 PLAN *plan; 665 FTSENT *entry; 666 { 667 char *group_from_gid(); 668 669 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1); 670 } 671 672 PLAN * 673 c_nogroup() 674 { 675 ftsoptions &= ~FTS_NOSTAT; 676 677 return (palloc(N_NOGROUP, f_nogroup)); 678 } 679 680 /* 681 * -nouser functions -- 682 * 683 * True if file belongs to a user ID for which the equivalent 684 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 685 */ 686 int 687 f_nouser(plan, entry) 688 PLAN *plan; 689 FTSENT *entry; 690 { 691 char *user_from_uid(); 692 693 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1); 694 } 695 696 PLAN * 697 c_nouser() 698 { 699 ftsoptions &= ~FTS_NOSTAT; 700 701 return (palloc(N_NOUSER, f_nouser)); 702 } 703 704 /* 705 * -path functions -- 706 * 707 * True if the path of the filename being examined 708 * matches pattern using Pattern Matching Notation S3.14 709 */ 710 int 711 f_path(plan, entry) 712 PLAN *plan; 713 FTSENT *entry; 714 { 715 return (!fnmatch(plan->c_data, entry->fts_path, 0)); 716 } 717 718 PLAN * 719 c_path(pattern) 720 char *pattern; 721 { 722 PLAN *new; 723 724 new = palloc(N_NAME, f_path); 725 new->c_data = pattern; 726 return (new); 727 } 728 729 /* 730 * -perm functions -- 731 * 732 * The mode argument is used to represent file mode bits. If it starts 733 * with a leading digit, it's treated as an octal mode, otherwise as a 734 * symbolic mode. 735 */ 736 int 737 f_perm(plan, entry) 738 PLAN *plan; 739 FTSENT *entry; 740 { 741 mode_t mode; 742 743 mode = entry->fts_statp->st_mode & 744 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 745 if (plan->flags == F_ATLEAST) 746 return ((plan->m_data | mode) == mode); 747 else 748 return (mode == plan->m_data); 749 /* NOTREACHED */ 750 } 751 752 PLAN * 753 c_perm(perm) 754 char *perm; 755 { 756 PLAN *new; 757 mode_t *set; 758 759 ftsoptions &= ~FTS_NOSTAT; 760 761 new = palloc(N_PERM, f_perm); 762 763 if (*perm == '-') { 764 new->flags = F_ATLEAST; 765 ++perm; 766 } 767 768 if ((set = setmode(perm)) == NULL) 769 err(1, "-perm: %s: illegal mode string", perm); 770 771 new->m_data = getmode(set, 0); 772 return (new); 773 } 774 775 /* 776 * -print functions -- 777 * 778 * Always true, causes the current pathame to be written to 779 * standard output. 780 */ 781 int 782 f_print(plan, entry) 783 PLAN *plan; 784 FTSENT *entry; 785 { 786 (void)puts(entry->fts_path); 787 return (1); 788 } 789 790 PLAN * 791 c_print() 792 { 793 isoutput = 1; 794 795 return (palloc(N_PRINT, f_print)); 796 } 797 798 /* 799 * -print0 functions -- 800 * 801 * Always true, causes the current pathame to be written to 802 * standard output followed by a NUL character 803 */ 804 int 805 f_print0(plan, entry) 806 PLAN *plan; 807 FTSENT *entry; 808 { 809 fputs(entry->fts_path, stdout); 810 fputc('\0', stdout); 811 return (1); 812 } 813 814 PLAN * 815 c_print0() 816 { 817 isoutput = 1; 818 819 return (palloc(N_PRINT0, f_print0)); 820 } 821 822 /* 823 * -prune functions -- 824 * 825 * Prune a portion of the hierarchy. 826 */ 827 int 828 f_prune(plan, entry) 829 PLAN *plan; 830 FTSENT *entry; 831 { 832 extern FTS *tree; 833 834 if (fts_set(tree, entry, FTS_SKIP)) 835 err(1, "%s", entry->fts_path); 836 return (1); 837 } 838 839 PLAN * 840 c_prune() 841 { 842 return (palloc(N_PRUNE, f_prune)); 843 } 844 845 /* 846 * -size n[c] functions -- 847 * 848 * True if the file size in bytes, divided by an implementation defined 849 * value and rounded up to the next integer, is n. If n is followed by 850 * a c, the size is in bytes. 851 */ 852 #define FIND_SIZE 512 853 static int divsize = 1; 854 855 int 856 f_size(plan, entry) 857 PLAN *plan; 858 FTSENT *entry; 859 { 860 off_t size; 861 862 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 863 FIND_SIZE : entry->fts_statp->st_size; 864 COMPARE(size, plan->o_data); 865 } 866 867 PLAN * 868 c_size(arg) 869 char *arg; 870 { 871 PLAN *new; 872 char endch; 873 874 ftsoptions &= ~FTS_NOSTAT; 875 876 new = palloc(N_SIZE, f_size); 877 endch = 'c'; 878 new->o_data = find_parsenum(new, "-size", arg, &endch); 879 if (endch == 'c') 880 divsize = 0; 881 return (new); 882 } 883 884 /* 885 * -type c functions -- 886 * 887 * True if the type of the file is c, where c is b, c, d, p, or f for 888 * block special file, character special file, directory, FIFO, or 889 * regular file, respectively. 890 */ 891 int 892 f_type(plan, entry) 893 PLAN *plan; 894 FTSENT *entry; 895 { 896 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data); 897 } 898 899 PLAN * 900 c_type(typestring) 901 char *typestring; 902 { 903 PLAN *new; 904 mode_t mask; 905 906 ftsoptions &= ~FTS_NOSTAT; 907 908 switch (typestring[0]) { 909 case 'b': 910 mask = S_IFBLK; 911 break; 912 case 'c': 913 mask = S_IFCHR; 914 break; 915 case 'd': 916 mask = S_IFDIR; 917 break; 918 case 'f': 919 mask = S_IFREG; 920 break; 921 case 'l': 922 mask = S_IFLNK; 923 break; 924 case 'p': 925 mask = S_IFIFO; 926 break; 927 case 's': 928 mask = S_IFSOCK; 929 break; 930 default: 931 errx(1, "-type: %s: unknown type", typestring); 932 } 933 934 new = palloc(N_TYPE, f_type); 935 new->m_data = mask; 936 return (new); 937 } 938 939 /* 940 * -user uname functions -- 941 * 942 * True if the file belongs to the user uname. If uname is numeric and 943 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 944 * return a valid user name, uname is taken as a user ID. 945 */ 946 int 947 f_user(plan, entry) 948 PLAN *plan; 949 FTSENT *entry; 950 { 951 return (entry->fts_statp->st_uid == plan->u_data); 952 } 953 954 PLAN * 955 c_user(username) 956 char *username; 957 { 958 PLAN *new; 959 struct passwd *p; 960 uid_t uid; 961 962 ftsoptions &= ~FTS_NOSTAT; 963 964 p = getpwnam(username); 965 if (p == NULL) { 966 uid = atoi(username); 967 if (uid == 0 && username[0] != '0') 968 errx(1, "-user: %s: no such user", username); 969 } else 970 uid = p->pw_uid; 971 972 new = palloc(N_USER, f_user); 973 new->u_data = uid; 974 return (new); 975 } 976 977 /* 978 * -xdev functions -- 979 * 980 * Always true, causes find not to decend past directories that have a 981 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 982 */ 983 PLAN * 984 c_xdev() 985 { 986 ftsoptions |= FTS_XDEV; 987 988 return (palloc(N_XDEV, f_always_true)); 989 } 990 991 /* 992 * ( expression ) functions -- 993 * 994 * True if expression is true. 995 */ 996 int 997 f_expr(plan, entry) 998 PLAN *plan; 999 FTSENT *entry; 1000 { 1001 register PLAN *p; 1002 register int state; 1003 1004 for (p = plan->p_data[0]; 1005 p && (state = (p->eval)(p, entry)); p = p->next); 1006 return (state); 1007 } 1008 1009 /* 1010 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are 1011 * eliminated during phase 2 of find_formplan() --- the '(' node is converted 1012 * to a N_EXPR node containing the expression and the ')' node is discarded. 1013 */ 1014 PLAN * 1015 c_openparen() 1016 { 1017 return (palloc(N_OPENPAREN, (int (*)())-1)); 1018 } 1019 1020 PLAN * 1021 c_closeparen() 1022 { 1023 return (palloc(N_CLOSEPAREN, (int (*)())-1)); 1024 } 1025 1026 /* 1027 * ! expression functions -- 1028 * 1029 * Negation of a primary; the unary NOT operator. 1030 */ 1031 int 1032 f_not(plan, entry) 1033 PLAN *plan; 1034 FTSENT *entry; 1035 { 1036 register PLAN *p; 1037 register int state; 1038 1039 for (p = plan->p_data[0]; 1040 p && (state = (p->eval)(p, entry)); p = p->next); 1041 return (!state); 1042 } 1043 1044 PLAN * 1045 c_not() 1046 { 1047 return (palloc(N_NOT, f_not)); 1048 } 1049 1050 /* 1051 * expression -o expression functions -- 1052 * 1053 * Alternation of primaries; the OR operator. The second expression is 1054 * not evaluated if the first expression is true. 1055 */ 1056 int 1057 f_or(plan, entry) 1058 PLAN *plan; 1059 FTSENT *entry; 1060 { 1061 register PLAN *p; 1062 register int state; 1063 1064 for (p = plan->p_data[0]; 1065 p && (state = (p->eval)(p, entry)); p = p->next); 1066 1067 if (state) 1068 return (1); 1069 1070 for (p = plan->p_data[1]; 1071 p && (state = (p->eval)(p, entry)); p = p->next); 1072 return (state); 1073 } 1074 1075 PLAN * 1076 c_or() 1077 { 1078 return (palloc(N_OR, f_or)); 1079 } 1080 1081 static PLAN * 1082 palloc(t, f) 1083 enum ntype t; 1084 int (*f) __P((PLAN *, FTSENT *)); 1085 { 1086 PLAN *new; 1087 1088 if ((new = malloc(sizeof(PLAN))) == NULL) 1089 err(1, NULL); 1090 new->type = t; 1091 new->eval = f; 1092 new->flags = 0; 1093 new->next = NULL; 1094 return (new); 1095 } 1096