function.c revision 18681
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.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 73static 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 */ 79static long long 80find_parsenum(plan, option, vp, endch) 81 PLAN *plan; 82 char *option, *vp, *endch; 83{ 84 long 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 strtoq(). Note, if strtoq() returns zero 105 * and endchar points to the beginning of the string we know we have 106 * a syntax error. 107 */ 108 value = strtoq(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 */ 134int 135f_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 145PLAN * 146c_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 */ 164int 165f_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 175PLAN * 176c_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 */ 196int 197f_always_true(plan, entry) 198 PLAN *plan; 199 FTSENT *entry; 200{ 201 return (1); 202} 203 204PLAN * 205c_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 */ 224int 225f_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 */ 269PLAN * 270c_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 */ 322PLAN * 323c_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 */ 336int 337f_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 399PLAN * 400c_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 */ 463int 464f_group(plan, entry) 465 PLAN *plan; 466 FTSENT *entry; 467{ 468 return (entry->fts_statp->st_gid == plan->g_data); 469} 470 471PLAN * 472c_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 */ 499int 500f_inum(plan, entry) 501 PLAN *plan; 502 FTSENT *entry; 503{ 504 COMPARE(entry->fts_statp->st_ino, plan->i_data); 505} 506 507PLAN * 508c_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 */ 525int 526f_links(plan, entry) 527 PLAN *plan; 528 FTSENT *entry; 529{ 530 COMPARE(entry->fts_statp->st_nlink, plan->l_data); 531} 532 533PLAN * 534c_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 */ 551int 552f_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 560PLAN * 561c_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 */ 575int 576f_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 586PLAN * 587c_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 */ 606int 607f_name(plan, entry) 608 PLAN *plan; 609 FTSENT *entry; 610{ 611 return (!fnmatch(plan->c_data, entry->fts_name, 0)); 612} 613 614PLAN * 615c_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 */ 632int 633f_newer(plan, entry) 634 PLAN *plan; 635 FTSENT *entry; 636{ 637 return (entry->fts_statp->st_mtime > plan->t_data); 638} 639 640PLAN * 641c_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 */ 662int 663f_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 672PLAN * 673c_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 */ 686int 687f_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 696PLAN * 697c_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 */ 710int 711f_path(plan, entry) 712 PLAN *plan; 713 FTSENT *entry; 714{ 715 return (!fnmatch(plan->c_data, entry->fts_path, 0)); 716} 717 718PLAN * 719c_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 */ 736int 737f_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 752PLAN * 753c_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 */ 781int 782f_print(plan, entry) 783 PLAN *plan; 784 FTSENT *entry; 785{ 786 (void)puts(entry->fts_path); 787 return (1); 788} 789 790PLAN * 791c_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 */ 804int 805f_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 814PLAN * 815c_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 */ 827int 828f_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 839PLAN * 840c_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 853static int divsize = 1; 854 855int 856f_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 867PLAN * 868c_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 */ 891int 892f_type(plan, entry) 893 PLAN *plan; 894 FTSENT *entry; 895{ 896 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data); 897} 898 899PLAN * 900c_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 * -delete functions -- 941 * 942 * True always. Makes it's best shot and continues on regardless. 943 */ 944int 945f_delete(plan, entry) 946 PLAN *plan; 947 FTSENT *entry; 948{ 949 /* ignore these from fts */ 950 if (strcmp(entry->fts_accpath, ".") == 0 || 951 strcmp(entry->fts_accpath, "..") == 0) 952 return (1); 953 954 /* sanity check */ 955 if (isdepth == 0 || /* depth off */ 956 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */ 957 !(ftsoptions & FTS_PHYSICAL) || /* physical off */ 958 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */ 959 errx(1, "-delete: insecure options got turned on"); 960 961 /* Potentially unsafe - do not accept relative paths whatsoever */ 962 if (strchr(entry->fts_accpath, '/') != NULL) 963 errx(1, "-delete: %s: relative path potentially not safe", 964 entry->fts_accpath); 965 966 /* rmdir directories, unlink everything else */ 967 if (S_ISDIR(entry->fts_statp->st_mode)) { 968 if (rmdir(entry->fts_accpath) < 0) 969 warn("-delete: rmdir(%s)", entry->fts_path); 970 } else { 971 if (unlink(entry->fts_accpath) < 0) 972 warn("-delete: unlink(%s)", entry->fts_path); 973 } 974 975 /* "succeed" */ 976 return (1); 977} 978 979PLAN * 980c_delete() 981{ 982 983 ftsoptions &= ~FTS_NOSTAT; /* no optimise */ 984 ftsoptions |= FTS_PHYSICAL; /* disable -follow */ 985 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */ 986 isoutput = 1; /* possible output */ 987 isdepth = 1; /* -depth implied */ 988 989 return (palloc(N_DELETE, f_delete)); 990} 991 992/* 993 * -user uname functions -- 994 * 995 * True if the file belongs to the user uname. If uname is numeric and 996 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 997 * return a valid user name, uname is taken as a user ID. 998 */ 999int 1000f_user(plan, entry) 1001 PLAN *plan; 1002 FTSENT *entry; 1003{ 1004 return (entry->fts_statp->st_uid == plan->u_data); 1005} 1006 1007PLAN * 1008c_user(username) 1009 char *username; 1010{ 1011 PLAN *new; 1012 struct passwd *p; 1013 uid_t uid; 1014 1015 ftsoptions &= ~FTS_NOSTAT; 1016 1017 p = getpwnam(username); 1018 if (p == NULL) { 1019 uid = atoi(username); 1020 if (uid == 0 && username[0] != '0') 1021 errx(1, "-user: %s: no such user", username); 1022 } else 1023 uid = p->pw_uid; 1024 1025 new = palloc(N_USER, f_user); 1026 new->u_data = uid; 1027 return (new); 1028} 1029 1030/* 1031 * -xdev functions -- 1032 * 1033 * Always true, causes find not to decend past directories that have a 1034 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 1035 */ 1036PLAN * 1037c_xdev() 1038{ 1039 ftsoptions |= FTS_XDEV; 1040 1041 return (palloc(N_XDEV, f_always_true)); 1042} 1043 1044/* 1045 * ( expression ) functions -- 1046 * 1047 * True if expression is true. 1048 */ 1049int 1050f_expr(plan, entry) 1051 PLAN *plan; 1052 FTSENT *entry; 1053{ 1054 register PLAN *p; 1055 register int state; 1056 1057 for (p = plan->p_data[0]; 1058 p && (state = (p->eval)(p, entry)); p = p->next); 1059 return (state); 1060} 1061 1062/* 1063 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are 1064 * eliminated during phase 2 of find_formplan() --- the '(' node is converted 1065 * to a N_EXPR node containing the expression and the ')' node is discarded. 1066 */ 1067PLAN * 1068c_openparen() 1069{ 1070 return (palloc(N_OPENPAREN, (int (*)())-1)); 1071} 1072 1073PLAN * 1074c_closeparen() 1075{ 1076 return (palloc(N_CLOSEPAREN, (int (*)())-1)); 1077} 1078 1079/* 1080 * ! expression functions -- 1081 * 1082 * Negation of a primary; the unary NOT operator. 1083 */ 1084int 1085f_not(plan, entry) 1086 PLAN *plan; 1087 FTSENT *entry; 1088{ 1089 register PLAN *p; 1090 register int state; 1091 1092 for (p = plan->p_data[0]; 1093 p && (state = (p->eval)(p, entry)); p = p->next); 1094 return (!state); 1095} 1096 1097PLAN * 1098c_not() 1099{ 1100 return (palloc(N_NOT, f_not)); 1101} 1102 1103/* 1104 * expression -o expression functions -- 1105 * 1106 * Alternation of primaries; the OR operator. The second expression is 1107 * not evaluated if the first expression is true. 1108 */ 1109int 1110f_or(plan, entry) 1111 PLAN *plan; 1112 FTSENT *entry; 1113{ 1114 register PLAN *p; 1115 register int state; 1116 1117 for (p = plan->p_data[0]; 1118 p && (state = (p->eval)(p, entry)); p = p->next); 1119 1120 if (state) 1121 return (1); 1122 1123 for (p = plan->p_data[1]; 1124 p && (state = (p->eval)(p, entry)); p = p->next); 1125 return (state); 1126} 1127 1128PLAN * 1129c_or() 1130{ 1131 return (palloc(N_OR, f_or)); 1132} 1133 1134static PLAN * 1135palloc(t, f) 1136 enum ntype t; 1137 int (*f) __P((PLAN *, FTSENT *)); 1138{ 1139 PLAN *new; 1140 1141 if ((new = malloc(sizeof(PLAN))) == NULL) 1142 err(1, NULL); 1143 new->type = t; 1144 new->eval = f; 1145 new->flags = 0; 1146 new->next = NULL; 1147 return (new); 1148} 1149