1 /* $NetBSD: function.c,v 1.53 2006/02/20 16:31:02 jschauma Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Cimarron D. Taylor of the University of California, Berkeley. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "from: @(#)function.c 8.10 (Berkeley) 5/4/95"; 39 #else 40 __RCSID("$NetBSD: function.c,v 1.53 2006/02/20 16:31:02 jschauma Exp $"); 41 #endif 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/stat.h> 46 #include <sys/wait.h> 47 #include <sys/mount.h> 48 49 #include <dirent.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <fnmatch.h> 53 #include <fts.h> 54 #include <grp.h> 55 #include <inttypes.h> 56 #include <pwd.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <tzfile.h> 61 #include <unistd.h> 62 63 #include "find.h" 64 #include "stat_flags.h" 65 66 #define COMPARE(a, b) { \ 67 switch (plan->flags) { \ 68 case F_EQUAL: \ 69 return (a == b); \ 70 case F_LESSTHAN: \ 71 return (a < b); \ 72 case F_GREATER: \ 73 return (a > b); \ 74 default: \ 75 abort(); \ 76 } \ 77 } 78 79 static int64_t find_parsenum __P((PLAN *, char *, char *, char *)); 80 int f_always_true __P((PLAN *, FTSENT *)); 81 int f_amin __P((PLAN *, FTSENT *)); 82 int f_anewer __P((PLAN *, FTSENT *)); 83 int f_atime __P((PLAN *, FTSENT *)); 84 int f_cmin __P((PLAN *, FTSENT *)); 85 int f_cnewer __P((PLAN *, FTSENT *)); 86 int f_ctime __P((PLAN *, FTSENT *)); 87 int f_empty __P((PLAN *, FTSENT *)); 88 int f_exec __P((PLAN *, FTSENT *)); 89 int f_execdir __P((PLAN *, FTSENT *)); 90 int f_false __P((PLAN *, FTSENT *)); 91 int f_flags __P((PLAN *, FTSENT *)); 92 int f_fprint __P((PLAN *, FTSENT *)); 93 int f_fstype __P((PLAN *, FTSENT *)); 94 int f_group __P((PLAN *, FTSENT *)); 95 int f_iname __P((PLAN *, FTSENT *)); 96 int f_inum __P((PLAN *, FTSENT *)); 97 int f_links __P((PLAN *, FTSENT *)); 98 int f_ls __P((PLAN *, FTSENT *)); 99 int f_mindepth __P((PLAN *, FTSENT *)); 100 int f_maxdepth __P((PLAN *, FTSENT *)); 101 int f_mmin __P((PLAN *, FTSENT *)); 102 int f_mtime __P((PLAN *, FTSENT *)); 103 int f_name __P((PLAN *, FTSENT *)); 104 int f_newer __P((PLAN *, FTSENT *)); 105 int f_nogroup __P((PLAN *, FTSENT *)); 106 int f_nouser __P((PLAN *, FTSENT *)); 107 int f_path __P((PLAN *, FTSENT *)); 108 int f_perm __P((PLAN *, FTSENT *)); 109 int f_print __P((PLAN *, FTSENT *)); 110 int f_print0 __P((PLAN *, FTSENT *)); 111 int f_printx __P((PLAN *, FTSENT *)); 112 int f_prune __P((PLAN *, FTSENT *)); 113 int f_regex __P((PLAN *, FTSENT *)); 114 int f_size __P((PLAN *, FTSENT *)); 115 int f_type __P((PLAN *, FTSENT *)); 116 int f_user __P((PLAN *, FTSENT *)); 117 int f_not __P((PLAN *, FTSENT *)); 118 int f_or __P((PLAN *, FTSENT *)); 119 static PLAN *c_regex_common __P((char ***, int, enum ntype, int)); 120 static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *)))); 121 122 extern int dotfd; 123 extern FTS *tree; 124 extern time_t now; 125 126 /* 127 * find_parsenum -- 128 * Parse a string of the form [+-]# and return the value. 129 */ 130 static int64_t 131 find_parsenum(plan, option, vp, endch) 132 PLAN *plan; 133 char *option, *vp, *endch; 134 { 135 int64_t value; 136 char *endchar, *str; /* Pointer to character ending conversion. */ 137 138 /* Determine comparison from leading + or -. */ 139 str = vp; 140 switch (*str) { 141 case '+': 142 ++str; 143 plan->flags = F_GREATER; 144 break; 145 case '-': 146 ++str; 147 plan->flags = F_LESSTHAN; 148 break; 149 default: 150 plan->flags = F_EQUAL; 151 break; 152 } 153 154 /* 155 * Convert the string with strtol(). Note, if strtol() returns zero 156 * and endchar points to the beginning of the string we know we have 157 * a syntax error. 158 */ 159 value = strtoq(str, &endchar, 10); 160 if (value == 0 && endchar == str) 161 errx(1, "%s: %s: illegal numeric value", option, vp); 162 if (endchar[0] && (endch == NULL || endchar[0] != *endch)) 163 errx(1, "%s: %s: illegal trailing character", option, vp); 164 if (endch) 165 *endch = endchar[0]; 166 return (value); 167 } 168 169 /* 170 * The value of n for the inode times (atime, ctime, and mtime) is a range, 171 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with 172 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the 173 * user wanted. Correct so that -1 is "less than 1". 174 */ 175 #define TIME_CORRECT(p, ttype) \ 176 if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \ 177 ++((p)->t_data); 178 179 /* 180 * -amin n functions -- 181 * 182 * True if the difference between the file access time and the 183 * current time is n 1 minute periods. 184 */ 185 int 186 f_amin(plan, entry) 187 PLAN *plan; 188 FTSENT *entry; 189 { 190 COMPARE((now - entry->fts_statp->st_atime + 191 SECSPERMIN - 1) / SECSPERMIN, plan->t_data); 192 } 193 194 PLAN * 195 c_amin(argvp, isok) 196 char ***argvp; 197 int isok; 198 { 199 char *arg = **argvp; 200 PLAN *new; 201 202 (*argvp)++; 203 ftsoptions &= ~FTS_NOSTAT; 204 205 new = palloc(N_AMIN, f_amin); 206 new->t_data = find_parsenum(new, "-amin", arg, NULL); 207 TIME_CORRECT(new, N_AMIN); 208 return (new); 209 } 210 211 /* 212 * -anewer file functions -- 213 * 214 * True if the current file has been accessed more recently 215 * than the access time of the file named by the pathname 216 * file. 217 */ 218 int 219 f_anewer(plan, entry) 220 PLAN *plan; 221 FTSENT *entry; 222 { 223 224 return (entry->fts_statp->st_atime > plan->t_data); 225 } 226 227 PLAN * 228 c_anewer(argvp, isok) 229 char ***argvp; 230 int isok; 231 { 232 char *filename = **argvp; 233 PLAN *new; 234 struct stat sb; 235 236 (*argvp)++; 237 ftsoptions &= ~FTS_NOSTAT; 238 239 if (stat(filename, &sb)) 240 err(1, "%s", filename); 241 new = palloc(N_ANEWER, f_anewer); 242 new->t_data = sb.st_atime; 243 return (new); 244 } 245 246 /* 247 * -atime n functions -- 248 * 249 * True if the difference between the file access time and the 250 * current time is n 24 hour periods. 251 */ 252 int 253 f_atime(plan, entry) 254 PLAN *plan; 255 FTSENT *entry; 256 { 257 COMPARE((now - entry->fts_statp->st_atime + 258 SECSPERDAY - 1) / SECSPERDAY, plan->t_data); 259 } 260 261 PLAN * 262 c_atime(argvp, isok) 263 char ***argvp; 264 int isok; 265 { 266 char *arg = **argvp; 267 PLAN *new; 268 269 (*argvp)++; 270 ftsoptions &= ~FTS_NOSTAT; 271 272 new = palloc(N_ATIME, f_atime); 273 new->t_data = find_parsenum(new, "-atime", arg, NULL); 274 TIME_CORRECT(new, N_ATIME); 275 return (new); 276 } 277 /* 278 * -cmin n functions -- 279 * 280 * True if the difference between the last change of file 281 * status information and the current time is n 24 hour periods. 282 */ 283 int 284 f_cmin(plan, entry) 285 PLAN *plan; 286 FTSENT *entry; 287 { 288 COMPARE((now - entry->fts_statp->st_ctime + 289 SECSPERMIN - 1) / SECSPERMIN, plan->t_data); 290 } 291 292 PLAN * 293 c_cmin(argvp, isok) 294 char ***argvp; 295 int isok; 296 { 297 char *arg = **argvp; 298 PLAN *new; 299 300 (*argvp)++; 301 ftsoptions &= ~FTS_NOSTAT; 302 303 new = palloc(N_CMIN, f_cmin); 304 new->t_data = find_parsenum(new, "-cmin", arg, NULL); 305 TIME_CORRECT(new, N_CMIN); 306 return (new); 307 } 308 309 /* 310 * -cnewer file functions -- 311 * 312 * True if the current file has been changed more recently 313 * than the changed time of the file named by the pathname 314 * file. 315 */ 316 int 317 f_cnewer(plan, entry) 318 PLAN *plan; 319 FTSENT *entry; 320 { 321 322 return (entry->fts_statp->st_ctime > plan->t_data); 323 } 324 325 PLAN * 326 c_cnewer(argvp, isok) 327 char ***argvp; 328 int isok; 329 { 330 char *filename = **argvp; 331 PLAN *new; 332 struct stat sb; 333 334 (*argvp)++; 335 ftsoptions &= ~FTS_NOSTAT; 336 337 if (stat(filename, &sb)) 338 err(1, "%s", filename); 339 new = palloc(N_CNEWER, f_cnewer); 340 new->t_data = sb.st_ctime; 341 return (new); 342 } 343 344 /* 345 * -ctime n functions -- 346 * 347 * True if the difference between the last change of file 348 * status information and the current time is n 24 hour periods. 349 */ 350 int 351 f_ctime(plan, entry) 352 PLAN *plan; 353 FTSENT *entry; 354 { 355 COMPARE((now - entry->fts_statp->st_ctime + 356 SECSPERDAY - 1) / SECSPERDAY, plan->t_data); 357 } 358 359 PLAN * 360 c_ctime(argvp, isok) 361 char ***argvp; 362 int isok; 363 { 364 char *arg = **argvp; 365 PLAN *new; 366 367 (*argvp)++; 368 ftsoptions &= ~FTS_NOSTAT; 369 370 new = palloc(N_CTIME, f_ctime); 371 new->t_data = find_parsenum(new, "-ctime", arg, NULL); 372 TIME_CORRECT(new, N_CTIME); 373 return (new); 374 } 375 376 /* 377 * -depth functions -- 378 * 379 * Always true, causes descent of the directory hierarchy to be done 380 * so that all entries in a directory are acted on before the directory 381 * itself. 382 */ 383 int 384 f_always_true(plan, entry) 385 PLAN *plan; 386 FTSENT *entry; 387 { 388 389 return (1); 390 } 391 392 PLAN * 393 c_depth(argvp, isok) 394 char ***argvp; 395 int isok; 396 { 397 isdepth = 1; 398 399 return (palloc(N_DEPTH, f_always_true)); 400 } 401 402 /* 403 * -empty functions -- 404 * 405 * True if the file or directory is empty 406 */ 407 int 408 f_empty(plan, entry) 409 PLAN *plan; 410 FTSENT *entry; 411 { 412 if (S_ISREG(entry->fts_statp->st_mode) && 413 entry->fts_statp->st_size == 0) 414 return (1); 415 if (S_ISDIR(entry->fts_statp->st_mode)) { 416 struct dirent *dp; 417 int empty; 418 DIR *dir; 419 420 empty = 1; 421 dir = opendir(entry->fts_accpath); 422 if (dir == NULL) 423 err(1, "%s", entry->fts_accpath); 424 for (dp = readdir(dir); dp; dp = readdir(dir)) 425 if (dp->d_name[0] != '.' || 426 (dp->d_name[1] != '\0' && 427 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) { 428 empty = 0; 429 break; 430 } 431 closedir(dir); 432 return (empty); 433 } 434 return (0); 435 } 436 437 PLAN * 438 c_empty(argvp, isok) 439 char ***argvp; 440 int isok; 441 { 442 ftsoptions &= ~FTS_NOSTAT; 443 444 return (palloc(N_EMPTY, f_empty)); 445 } 446 447 /* 448 * [-exec | -ok] utility [arg ... ] ; functions -- 449 * 450 * True if the executed utility returns a zero value as exit status. 451 * The end of the primary expression is delimited by a semicolon. If 452 * "{}" occurs anywhere, it gets replaced by the current pathname. 453 * The current directory for the execution of utility is the same as 454 * the current directory when the find utility was started. 455 * 456 * The primary -ok is different in that it requests affirmation of the 457 * user before executing the utility. 458 */ 459 int 460 f_exec(plan, entry) 461 PLAN *plan; 462 FTSENT *entry; 463 { 464 int cnt; 465 pid_t pid; 466 int status; 467 468 for (cnt = 0; plan->e_argv[cnt]; ++cnt) 469 if (plan->e_len[cnt]) 470 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt], 471 entry->fts_path, &plan->e_len[cnt]); 472 473 if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv)) 474 return (0); 475 476 /* don't mix output of command with find output */ 477 fflush(stdout); 478 fflush(stderr); 479 480 switch (pid = vfork()) { 481 case -1: 482 err(1, "vfork"); 483 /* NOTREACHED */ 484 case 0: 485 if (fchdir(dotfd)) { 486 warn("chdir"); 487 _exit(1); 488 } 489 execvp(plan->e_argv[0], plan->e_argv); 490 warn("%s", plan->e_argv[0]); 491 _exit(1); 492 } 493 pid = waitpid(pid, &status, 0); 494 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); 495 } 496 497 /* 498 * c_exec -- 499 * build three parallel arrays, one with pointers to the strings passed 500 * on the command line, one with (possibly duplicated) pointers to the 501 * argv array, and one with integer values that are lengths of the 502 * strings, but also flags meaning that the string has to be massaged. 503 */ 504 PLAN * 505 c_exec(argvp, isok) 506 char ***argvp; 507 int isok; 508 { 509 PLAN *new; /* node returned */ 510 int cnt; 511 char **argv, **ap, *p; 512 513 isoutput = 1; 514 515 new = palloc(N_EXEC, f_exec); 516 if (isok) 517 new->flags = F_NEEDOK; 518 519 for (ap = argv = *argvp;; ++ap) { 520 if (!*ap) 521 errx(1, 522 "%s: no terminating \";\"", isok ? "-ok" : "-exec"); 523 if (**ap == ';') 524 break; 525 } 526 527 cnt = ap - *argvp + 1; 528 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *)); 529 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *)); 530 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int)); 531 532 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 533 new->e_orig[cnt] = *argv; 534 for (p = *argv; *p; ++p) 535 if (p[0] == '{' && p[1] == '}') { 536 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN); 537 new->e_len[cnt] = MAXPATHLEN; 538 break; 539 } 540 if (!*p) { 541 new->e_argv[cnt] = *argv; 542 new->e_len[cnt] = 0; 543 } 544 } 545 new->e_argv[cnt] = new->e_orig[cnt] = NULL; 546 547 *argvp = argv + 1; 548 return (new); 549 } 550 551 /* 552 * -execdir utility [arg ... ] ; functions -- 553 * 554 * True if the executed utility returns a zero value as exit status. 555 * The end of the primary expression is delimited by a semicolon. If 556 * "{}" occurs anywhere, it gets replaced by the unqualified pathname. 557 * The current directory for the execution of utility is the same as 558 * the directory where the file lives. 559 */ 560 int 561 f_execdir(plan, entry) 562 PLAN *plan; 563 FTSENT *entry; 564 { 565 int cnt; 566 pid_t pid; 567 int status; 568 char *file; 569 570 /* XXX - if file/dir ends in '/' this will not work -- can it? */ 571 if ((file = strrchr(entry->fts_path, '/'))) 572 file++; 573 else 574 file = entry->fts_path; 575 576 for (cnt = 0; plan->e_argv[cnt]; ++cnt) 577 if (plan->e_len[cnt]) 578 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt], 579 file, &plan->e_len[cnt]); 580 581 /* don't mix output of command with find output */ 582 fflush(stdout); 583 fflush(stderr); 584 585 switch (pid = vfork()) { 586 case -1: 587 err(1, "fork"); 588 /* NOTREACHED */ 589 case 0: 590 execvp(plan->e_argv[0], plan->e_argv); 591 warn("%s", plan->e_argv[0]); 592 _exit(1); 593 } 594 pid = waitpid(pid, &status, 0); 595 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); 596 } 597 598 /* 599 * c_execdir -- 600 * build three parallel arrays, one with pointers to the strings passed 601 * on the command line, one with (possibly duplicated) pointers to the 602 * argv array, and one with integer values that are lengths of the 603 * strings, but also flags meaning that the string has to be massaged. 604 */ 605 PLAN * 606 c_execdir(argvp, isok) 607 char ***argvp; 608 int isok; 609 { 610 PLAN *new; /* node returned */ 611 int cnt; 612 char **argv, **ap, *p; 613 614 ftsoptions &= ~FTS_NOSTAT; 615 isoutput = 1; 616 617 new = palloc(N_EXECDIR, f_execdir); 618 619 for (ap = argv = *argvp;; ++ap) { 620 if (!*ap) 621 errx(1, 622 "-execdir: no terminating \";\""); 623 if (**ap == ';') 624 break; 625 } 626 627 cnt = ap - *argvp + 1; 628 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *)); 629 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *)); 630 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int)); 631 632 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 633 new->e_orig[cnt] = *argv; 634 for (p = *argv; *p; ++p) 635 if (p[0] == '{' && p[1] == '}') { 636 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN); 637 new->e_len[cnt] = MAXPATHLEN; 638 break; 639 } 640 if (!*p) { 641 new->e_argv[cnt] = *argv; 642 new->e_len[cnt] = 0; 643 } 644 } 645 new->e_argv[cnt] = new->e_orig[cnt] = NULL; 646 647 *argvp = argv + 1; 648 return (new); 649 } 650 651 PLAN * 652 c_exit(argvp, isok) 653 char ***argvp; 654 int isok; 655 { 656 char *arg = **argvp; 657 PLAN *new; 658 659 /* not technically true, but otherwise '-print' is implied */ 660 isoutput = 1; 661 662 new = palloc(N_EXIT, f_always_true); 663 664 if (arg) { 665 (*argvp)++; 666 new->exit_val = find_parsenum(new, "-exit", arg, NULL); 667 } else 668 new->exit_val = 0; 669 670 return (new); 671 } 672 673 674 /* 675 * -false function 676 */ 677 int 678 f_false(plan, entry) 679 PLAN *plan; 680 FTSENT *entry; 681 { 682 683 return (0); 684 } 685 686 PLAN * 687 c_false(argvp, isok) 688 char ***argvp; 689 int isok; 690 { 691 return (palloc(N_FALSE, f_false)); 692 } 693 694 695 /* 696 * -flags [-]flags functions -- 697 */ 698 int 699 f_flags(plan, entry) 700 PLAN *plan; 701 FTSENT *entry; 702 { 703 u_int32_t flags; 704 705 flags = entry->fts_statp->st_flags; 706 if (plan->flags == F_ATLEAST) 707 return ((plan->f_data | flags) == flags); 708 else 709 return (flags == plan->f_data); 710 /* NOTREACHED */ 711 } 712 713 PLAN * 714 c_flags(argvp, isok) 715 char ***argvp; 716 int isok; 717 { 718 char *flags = **argvp; 719 PLAN *new; 720 u_long flagset; 721 722 (*argvp)++; 723 ftsoptions &= ~FTS_NOSTAT; 724 725 new = palloc(N_FLAGS, f_flags); 726 727 if (*flags == '-') { 728 new->flags = F_ATLEAST; 729 ++flags; 730 } 731 732 flagset = 0; 733 if ((strcmp(flags, "none") != 0) && 734 (string_to_flags(&flags, &flagset, NULL) != 0)) 735 errx(1, "-flags: %s: illegal flags string", flags); 736 new->f_data = flagset; 737 return (new); 738 } 739 740 /* 741 * -follow functions -- 742 * 743 * Always true, causes symbolic links to be followed on a global 744 * basis. 745 */ 746 PLAN * 747 c_follow(argvp, isok) 748 char ***argvp; 749 int isok; 750 { 751 ftsoptions &= ~FTS_PHYSICAL; 752 ftsoptions |= FTS_LOGICAL; 753 754 return (palloc(N_FOLLOW, f_always_true)); 755 } 756 757 /* -fprint functions -- 758 * 759 * Causes the current pathame to be written to the defined output file. 760 */ 761 int 762 f_fprint(plan, entry) 763 PLAN *plan; 764 FTSENT *entry; 765 { 766 767 if (-1 == fprintf(plan->fprint_file, "%s\n", entry->fts_path)) 768 warn("fprintf"); 769 770 return(1); 771 772 /* no descriptors are closed; they will be closed by 773 operating system when this find command exits. */ 774 } 775 776 PLAN * 777 c_fprint(argvp, isok) 778 char ***argvp; 779 int isok; 780 { 781 PLAN *new; 782 783 isoutput = 1; /* do not assume -print */ 784 785 new = palloc(N_FPRINT, f_fprint); 786 787 if (NULL == (new->fprint_file = fopen(**argvp, "w"))) 788 err(1, "-fprint: %s: cannot create file", **argvp); 789 790 (*argvp)++; 791 return (new); 792 } 793 794 /* 795 * -fstype functions -- 796 * 797 * True if the file is of a certain type. 798 */ 799 int 800 f_fstype(plan, entry) 801 PLAN *plan; 802 FTSENT *entry; 803 { 804 static dev_t curdev; /* need a guaranteed illegal dev value */ 805 static int first = 1; 806 struct statvfs sb; 807 static short val; 808 static char fstype[MFSNAMELEN]; 809 char *p, save[2]; 810 811 /* Only check when we cross mount point. */ 812 if (first || curdev != entry->fts_statp->st_dev) { 813 curdev = entry->fts_statp->st_dev; 814 815 /* 816 * Statfs follows symlinks; find wants the link's file system, 817 * not where it points. 818 */ 819 if (entry->fts_info == FTS_SL || 820 entry->fts_info == FTS_SLNONE) { 821 if ((p = strrchr(entry->fts_accpath, '/')) != NULL) 822 ++p; 823 else 824 p = entry->fts_accpath; 825 save[0] = p[0]; 826 p[0] = '.'; 827 save[1] = p[1]; 828 p[1] = '\0'; 829 830 } else 831 p = NULL; 832 833 if (statvfs(entry->fts_accpath, &sb)) 834 err(1, "%s", entry->fts_accpath); 835 836 if (p) { 837 p[0] = save[0]; 838 p[1] = save[1]; 839 } 840 841 first = 0; 842 843 /* 844 * Further tests may need both of these values, so 845 * always copy both of them. 846 */ 847 val = sb.f_flag; 848 strlcpy(fstype, sb.f_fstypename, sizeof(fstype)); 849 } 850 switch (plan->flags) { 851 case F_MTFLAG: 852 return (val & plan->mt_data); 853 case F_MTTYPE: 854 return (strncmp(fstype, plan->c_data, MFSNAMELEN) == 0); 855 default: 856 abort(); 857 } 858 } 859 860 PLAN * 861 c_fstype(argvp, isok) 862 char ***argvp; 863 int isok; 864 { 865 char *arg = **argvp; 866 PLAN *new; 867 868 (*argvp)++; 869 ftsoptions &= ~FTS_NOSTAT; 870 871 new = palloc(N_FSTYPE, f_fstype); 872 873 switch (*arg) { 874 case 'l': 875 if (!strcmp(arg, "local")) { 876 new->flags = F_MTFLAG; 877 new->mt_data = MNT_LOCAL; 878 return (new); 879 } 880 break; 881 case 'r': 882 if (!strcmp(arg, "rdonly")) { 883 new->flags = F_MTFLAG; 884 new->mt_data = MNT_RDONLY; 885 return (new); 886 } 887 break; 888 } 889 890 new->flags = F_MTTYPE; 891 new->c_data = arg; 892 return (new); 893 } 894 895 /* 896 * -group gname functions -- 897 * 898 * True if the file belongs to the group gname. If gname is numeric and 899 * an equivalent of the getgrnam() function does not return a valid group 900 * name, gname is taken as a group ID. 901 */ 902 int 903 f_group(plan, entry) 904 PLAN *plan; 905 FTSENT *entry; 906 { 907 908 return (entry->fts_statp->st_gid == plan->g_data); 909 } 910 911 PLAN * 912 c_group(argvp, isok) 913 char ***argvp; 914 int isok; 915 { 916 char *gname = **argvp; 917 PLAN *new; 918 struct group *g; 919 gid_t gid; 920 921 (*argvp)++; 922 ftsoptions &= ~FTS_NOSTAT; 923 924 g = getgrnam(gname); 925 if (g == NULL) { 926 gid = atoi(gname); 927 if (gid == 0 && gname[0] != '0') 928 errx(1, "-group: %s: no such group", gname); 929 } else 930 gid = g->gr_gid; 931 932 new = palloc(N_GROUP, f_group); 933 new->g_data = gid; 934 return (new); 935 } 936 937 /* 938 * -inum n functions -- 939 * 940 * True if the file has inode # n. 941 */ 942 int 943 f_inum(plan, entry) 944 PLAN *plan; 945 FTSENT *entry; 946 { 947 948 COMPARE(entry->fts_statp->st_ino, plan->i_data); 949 } 950 951 PLAN * 952 c_inum(argvp, isok) 953 char ***argvp; 954 int isok; 955 { 956 char *arg = **argvp; 957 PLAN *new; 958 959 (*argvp)++; 960 ftsoptions &= ~FTS_NOSTAT; 961 962 new = palloc(N_INUM, f_inum); 963 new->i_data = find_parsenum(new, "-inum", arg, NULL); 964 return (new); 965 } 966 967 /* 968 * -links n functions -- 969 * 970 * True if the file has n links. 971 */ 972 int 973 f_links(plan, entry) 974 PLAN *plan; 975 FTSENT *entry; 976 { 977 978 COMPARE(entry->fts_statp->st_nlink, plan->l_data); 979 } 980 981 PLAN * 982 c_links(argvp, isok) 983 char ***argvp; 984 int isok; 985 { 986 char *arg = **argvp; 987 PLAN *new; 988 989 (*argvp)++; 990 ftsoptions &= ~FTS_NOSTAT; 991 992 new = palloc(N_LINKS, f_links); 993 new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL); 994 return (new); 995 } 996 997 /* 998 * -ls functions -- 999 * 1000 * Always true - prints the current entry to stdout in "ls" format. 1001 */ 1002 int 1003 f_ls(plan, entry) 1004 PLAN *plan; 1005 FTSENT *entry; 1006 { 1007 1008 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); 1009 return (1); 1010 } 1011 1012 PLAN * 1013 c_ls(argvp, isok) 1014 char ***argvp; 1015 int isok; 1016 { 1017 1018 ftsoptions &= ~FTS_NOSTAT; 1019 isoutput = 1; 1020 1021 return (palloc(N_LS, f_ls)); 1022 } 1023 1024 /* 1025 * - maxdepth n functions -- 1026 * 1027 * True if the current search depth is less than or equal to the 1028 * maximum depth specified 1029 */ 1030 int 1031 f_maxdepth(plan, entry) 1032 PLAN *plan; 1033 FTSENT *entry; 1034 { 1035 extern FTS *tree; 1036 1037 if (entry->fts_level >= plan->max_data) 1038 fts_set(tree, entry, FTS_SKIP); 1039 return (entry->fts_level <= plan->max_data); 1040 } 1041 1042 PLAN * 1043 c_maxdepth(argvp, isok) 1044 char ***argvp; 1045 int isok; 1046 { 1047 char *arg = **argvp; 1048 PLAN *new; 1049 1050 (*argvp)++; 1051 new = palloc(N_MAXDEPTH, f_maxdepth); 1052 new->max_data = atoi(arg); 1053 return (new); 1054 } 1055 1056 /* 1057 * - mindepth n functions -- 1058 * 1059 * True if the current search depth is greater than or equal to the 1060 * minimum depth specified 1061 */ 1062 int 1063 f_mindepth(plan, entry) 1064 PLAN *plan; 1065 FTSENT *entry; 1066 { 1067 return (entry->fts_level >= plan->min_data); 1068 } 1069 1070 PLAN * 1071 c_mindepth(argvp, isok) 1072 char ***argvp; 1073 int isok; 1074 { 1075 char *arg = **argvp; 1076 PLAN *new; 1077 1078 (*argvp)++; 1079 new = palloc(N_MINDEPTH, f_mindepth); 1080 new->min_data = atoi(arg); 1081 return (new); 1082 } 1083 /* 1084 * -mmin n functions -- 1085 * 1086 * True if the difference between the file modification time and the 1087 * current time is n 24 hour periods. 1088 */ 1089 int 1090 f_mmin(plan, entry) 1091 PLAN *plan; 1092 FTSENT *entry; 1093 { 1094 COMPARE((now - entry->fts_statp->st_mtime + SECSPERMIN - 1) / 1095 SECSPERMIN, plan->t_data); 1096 } 1097 1098 PLAN * 1099 c_mmin(argvp, isok) 1100 char ***argvp; 1101 int isok; 1102 { 1103 char *arg = **argvp; 1104 PLAN *new; 1105 1106 (*argvp)++; 1107 ftsoptions &= ~FTS_NOSTAT; 1108 1109 new = palloc(N_MMIN, f_mmin); 1110 new->t_data = find_parsenum(new, "-mmin", arg, NULL); 1111 TIME_CORRECT(new, N_MMIN); 1112 return (new); 1113 } 1114 /* 1115 * -mtime n functions -- 1116 * 1117 * True if the difference between the file modification time and the 1118 * current time is n 24 hour periods. 1119 */ 1120 int 1121 f_mtime(plan, entry) 1122 PLAN *plan; 1123 FTSENT *entry; 1124 { 1125 COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) / 1126 SECSPERDAY, plan->t_data); 1127 } 1128 1129 PLAN * 1130 c_mtime(argvp, isok) 1131 char ***argvp; 1132 int isok; 1133 { 1134 char *arg = **argvp; 1135 PLAN *new; 1136 1137 (*argvp)++; 1138 ftsoptions &= ~FTS_NOSTAT; 1139 1140 new = palloc(N_MTIME, f_mtime); 1141 new->t_data = find_parsenum(new, "-mtime", arg, NULL); 1142 TIME_CORRECT(new, N_MTIME); 1143 return (new); 1144 } 1145 1146 /* 1147 * -name functions -- 1148 * 1149 * True if the basename of the filename being examined 1150 * matches pattern using Pattern Matching Notation S3.14 1151 */ 1152 int 1153 f_name(plan, entry) 1154 PLAN *plan; 1155 FTSENT *entry; 1156 { 1157 1158 return (!fnmatch(plan->c_data, entry->fts_name, 0)); 1159 } 1160 1161 PLAN * 1162 c_name(argvp, isok) 1163 char ***argvp; 1164 int isok; 1165 { 1166 char *pattern = **argvp; 1167 PLAN *new; 1168 1169 (*argvp)++; 1170 new = palloc(N_NAME, f_name); 1171 new->c_data = pattern; 1172 return (new); 1173 } 1174 1175 /* 1176 * -iname functions -- 1177 * 1178 * Similar to -name, but does case insensitive matching 1179 * 1180 */ 1181 int 1182 f_iname(plan, entry) 1183 PLAN *plan; 1184 FTSENT *entry; 1185 { 1186 return (!fnmatch(plan->c_data, entry->fts_name, FNM_CASEFOLD)); 1187 } 1188 1189 PLAN * 1190 c_iname(argvp, isok) 1191 char ***argvp; 1192 int isok; 1193 { 1194 char *pattern = **argvp; 1195 PLAN *new; 1196 1197 (*argvp)++; 1198 new = palloc(N_INAME, f_iname); 1199 new->c_data = pattern; 1200 return (new); 1201 } 1202 1203 /* 1204 * -newer file functions -- 1205 * 1206 * True if the current file has been modified more recently 1207 * than the modification time of the file named by the pathname 1208 * file. 1209 */ 1210 int 1211 f_newer(plan, entry) 1212 PLAN *plan; 1213 FTSENT *entry; 1214 { 1215 1216 return (entry->fts_statp->st_mtime > plan->t_data); 1217 } 1218 1219 PLAN * 1220 c_newer(argvp, isok) 1221 char ***argvp; 1222 int isok; 1223 { 1224 char *filename = **argvp; 1225 PLAN *new; 1226 struct stat sb; 1227 1228 (*argvp)++; 1229 ftsoptions &= ~FTS_NOSTAT; 1230 1231 if (stat(filename, &sb)) 1232 err(1, "%s", filename); 1233 new = palloc(N_NEWER, f_newer); 1234 new->t_data = sb.st_mtime; 1235 return (new); 1236 } 1237 1238 /* 1239 * -nogroup functions -- 1240 * 1241 * True if file belongs to a user ID for which the equivalent 1242 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 1243 */ 1244 int 1245 f_nogroup(plan, entry) 1246 PLAN *plan; 1247 FTSENT *entry; 1248 { 1249 1250 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1); 1251 } 1252 1253 PLAN * 1254 c_nogroup(argvp, isok) 1255 char ***argvp; 1256 int isok; 1257 { 1258 ftsoptions &= ~FTS_NOSTAT; 1259 1260 return (palloc(N_NOGROUP, f_nogroup)); 1261 } 1262 1263 /* 1264 * -nouser functions -- 1265 * 1266 * True if file belongs to a user ID for which the equivalent 1267 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 1268 */ 1269 int 1270 f_nouser(plan, entry) 1271 PLAN *plan; 1272 FTSENT *entry; 1273 { 1274 1275 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1); 1276 } 1277 1278 PLAN * 1279 c_nouser(argvp, isok) 1280 char ***argvp; 1281 int isok; 1282 { 1283 ftsoptions &= ~FTS_NOSTAT; 1284 1285 return (palloc(N_NOUSER, f_nouser)); 1286 } 1287 1288 /* 1289 * -path functions -- 1290 * 1291 * True if the path of the filename being examined 1292 * matches pattern using Pattern Matching Notation S3.14 1293 */ 1294 int 1295 f_path(plan, entry) 1296 PLAN *plan; 1297 FTSENT *entry; 1298 { 1299 1300 return (!fnmatch(plan->c_data, entry->fts_path, 0)); 1301 } 1302 1303 PLAN * 1304 c_path(argvp, isok) 1305 char ***argvp; 1306 int isok; 1307 { 1308 char *pattern = **argvp; 1309 PLAN *new; 1310 1311 (*argvp)++; 1312 new = palloc(N_NAME, f_path); 1313 new->c_data = pattern; 1314 return (new); 1315 } 1316 1317 /* 1318 * -perm functions -- 1319 * 1320 * The mode argument is used to represent file mode bits. If it starts 1321 * with a leading digit, it's treated as an octal mode, otherwise as a 1322 * symbolic mode. 1323 */ 1324 int 1325 f_perm(plan, entry) 1326 PLAN *plan; 1327 FTSENT *entry; 1328 { 1329 mode_t mode; 1330 1331 mode = entry->fts_statp->st_mode & 1332 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 1333 if (plan->flags == F_ATLEAST) 1334 return ((plan->m_data | mode) == mode); 1335 else 1336 return (mode == plan->m_data); 1337 /* NOTREACHED */ 1338 } 1339 1340 PLAN * 1341 c_perm(argvp, isok) 1342 char ***argvp; 1343 int isok; 1344 { 1345 char *perm = **argvp; 1346 PLAN *new; 1347 mode_t *set; 1348 1349 (*argvp)++; 1350 ftsoptions &= ~FTS_NOSTAT; 1351 1352 new = palloc(N_PERM, f_perm); 1353 1354 if (*perm == '-') { 1355 new->flags = F_ATLEAST; 1356 ++perm; 1357 } 1358 1359 if ((set = setmode(perm)) == NULL) 1360 err(1, "-perm: Cannot set file mode `%s'", perm); 1361 1362 new->m_data = getmode(set, 0); 1363 free(set); 1364 return (new); 1365 } 1366 1367 /* 1368 * -print functions -- 1369 * 1370 * Always true, causes the current pathame to be written to 1371 * standard output. 1372 */ 1373 int 1374 f_print(plan, entry) 1375 PLAN *plan; 1376 FTSENT *entry; 1377 { 1378 1379 (void)printf("%s\n", entry->fts_path); 1380 return (1); 1381 } 1382 1383 int 1384 f_print0(plan, entry) 1385 PLAN *plan; 1386 FTSENT *entry; 1387 { 1388 1389 (void)fputs(entry->fts_path, stdout); 1390 (void)fputc('\0', stdout); 1391 return (1); 1392 } 1393 1394 int 1395 f_printx(plan, entry) 1396 PLAN *plan; 1397 FTSENT *entry; 1398 { 1399 char *cp; 1400 1401 for (cp = entry->fts_path; *cp; cp++) { 1402 if (*cp == '\'' || *cp == '\"' || *cp == ' ' || 1403 *cp == '$' || *cp == '`' || 1404 *cp == '\t' || *cp == '\n' || *cp == '\\') 1405 fputc('\\', stdout); 1406 1407 fputc(*cp, stdout); 1408 } 1409 1410 fputc('\n', stdout); 1411 return (1); 1412 } 1413 1414 PLAN * 1415 c_print(argvp, isok) 1416 char ***argvp; 1417 int isok; 1418 { 1419 1420 isoutput = 1; 1421 1422 return (palloc(N_PRINT, f_print)); 1423 } 1424 1425 PLAN * 1426 c_print0(argvp, isok) 1427 char ***argvp; 1428 int isok; 1429 { 1430 1431 isoutput = 1; 1432 1433 return (palloc(N_PRINT0, f_print0)); 1434 } 1435 1436 PLAN * 1437 c_printx(argvp, isok) 1438 char ***argvp; 1439 int isok; 1440 { 1441 1442 isoutput = 1; 1443 1444 return (palloc(N_PRINTX, f_printx)); 1445 } 1446 1447 /* 1448 * -prune functions -- 1449 * 1450 * Prune a portion of the hierarchy. 1451 */ 1452 int 1453 f_prune(plan, entry) 1454 PLAN *plan; 1455 FTSENT *entry; 1456 { 1457 if (fts_set(tree, entry, FTS_SKIP)) 1458 err(1, "%s", entry->fts_path); 1459 return (1); 1460 } 1461 1462 PLAN * 1463 c_prune(argvp, isok) 1464 char ***argvp; 1465 int isok; 1466 { 1467 1468 return (palloc(N_PRUNE, f_prune)); 1469 } 1470 1471 /* 1472 * -regex regexp (and related) functions -- 1473 * 1474 * True if the complete file path matches the regular expression regexp. 1475 * For -regex, regexp is a case-sensitive (basic) regular expression. 1476 * For -iregex, regexp is a case-insensitive (basic) regular expression. 1477 */ 1478 int 1479 f_regex(plan, entry) 1480 PLAN *plan; 1481 FTSENT *entry; 1482 { 1483 1484 return (regexec(&plan->regexp_data, entry->fts_path, 0, NULL, 0) == 0); 1485 } 1486 1487 static PLAN * 1488 c_regex_common(argvp, isok, type, regcomp_flags) 1489 char ***argvp; 1490 int isok, regcomp_flags; 1491 enum ntype type; 1492 { 1493 char errbuf[LINE_MAX]; 1494 regex_t reg; 1495 char *regexp = **argvp; 1496 char *lineregexp; 1497 PLAN *new; 1498 int rv; 1499 1500 (*argvp)++; 1501 1502 lineregexp = alloca(strlen(regexp) + 1 + 6); /* max needed */ 1503 sprintf(lineregexp, "^%s(%s%s)$", 1504 (regcomp_flags & REG_EXTENDED) ? "" : "\\", regexp, 1505 (regcomp_flags & REG_EXTENDED) ? "" : "\\"); 1506 rv = regcomp(®, lineregexp, REG_NOSUB|regcomp_flags); 1507 if (rv != 0) { 1508 regerror(rv, ®, errbuf, sizeof errbuf); 1509 errx(1, "regexp %s: %s", regexp, errbuf); 1510 } 1511 1512 new = palloc(type, f_regex); 1513 new->regexp_data = reg; 1514 return (new); 1515 } 1516 1517 PLAN * 1518 c_regex(argvp, isok) 1519 char ***argvp; 1520 int isok; 1521 { 1522 1523 return (c_regex_common(argvp, isok, N_REGEX, REG_BASIC)); 1524 } 1525 1526 PLAN * 1527 c_iregex(argvp, isok) 1528 char ***argvp; 1529 int isok; 1530 { 1531 1532 return (c_regex_common(argvp, isok, N_IREGEX, REG_BASIC|REG_ICASE)); 1533 } 1534 1535 /* 1536 * -size n[c] functions -- 1537 * 1538 * True if the file size in bytes, divided by an implementation defined 1539 * value and rounded up to the next integer, is n. If n is followed by 1540 * a c, the size is in bytes. 1541 */ 1542 #define FIND_SIZE 512 1543 static int divsize = 1; 1544 1545 int 1546 f_size(plan, entry) 1547 PLAN *plan; 1548 FTSENT *entry; 1549 { 1550 off_t size; 1551 1552 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 1553 FIND_SIZE : entry->fts_statp->st_size; 1554 COMPARE(size, plan->o_data); 1555 } 1556 1557 PLAN * 1558 c_size(argvp, isok) 1559 char ***argvp; 1560 int isok; 1561 { 1562 char *arg = **argvp; 1563 PLAN *new; 1564 char endch; 1565 1566 (*argvp)++; 1567 ftsoptions &= ~FTS_NOSTAT; 1568 1569 new = palloc(N_SIZE, f_size); 1570 endch = 'c'; 1571 new->o_data = find_parsenum(new, "-size", arg, &endch); 1572 if (endch == 'c') 1573 divsize = 0; 1574 return (new); 1575 } 1576 1577 /* 1578 * -type c functions -- 1579 * 1580 * True if the type of the file is c, where c is b, c, d, p, f or w 1581 * for block special file, character special file, directory, FIFO, 1582 * regular file or whiteout respectively. 1583 */ 1584 int 1585 f_type(plan, entry) 1586 PLAN *plan; 1587 FTSENT *entry; 1588 { 1589 1590 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data); 1591 } 1592 1593 PLAN * 1594 c_type(argvp, isok) 1595 char ***argvp; 1596 int isok; 1597 { 1598 char *typestring = **argvp; 1599 PLAN *new; 1600 mode_t mask = (mode_t)0; 1601 1602 (*argvp)++; 1603 ftsoptions &= ~FTS_NOSTAT; 1604 1605 switch (typestring[0]) { 1606 case 'b': 1607 mask = S_IFBLK; 1608 break; 1609 case 'c': 1610 mask = S_IFCHR; 1611 break; 1612 case 'd': 1613 mask = S_IFDIR; 1614 break; 1615 case 'f': 1616 mask = S_IFREG; 1617 break; 1618 case 'l': 1619 mask = S_IFLNK; 1620 break; 1621 case 'p': 1622 mask = S_IFIFO; 1623 break; 1624 case 's': 1625 mask = S_IFSOCK; 1626 break; 1627 #ifdef S_IFWHT 1628 case 'W': 1629 case 'w': 1630 mask = S_IFWHT; 1631 #ifdef FTS_WHITEOUT 1632 ftsoptions |= FTS_WHITEOUT; 1633 #endif 1634 break; 1635 #endif /* S_IFWHT */ 1636 default: 1637 errx(1, "-type: %s: unknown type", typestring); 1638 } 1639 1640 new = palloc(N_TYPE, f_type); 1641 new->m_data = mask; 1642 return (new); 1643 } 1644 1645 /* 1646 * -user uname functions -- 1647 * 1648 * True if the file belongs to the user uname. If uname is numeric and 1649 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 1650 * return a valid user name, uname is taken as a user ID. 1651 */ 1652 int 1653 f_user(plan, entry) 1654 PLAN *plan; 1655 FTSENT *entry; 1656 { 1657 1658 COMPARE(entry->fts_statp->st_uid, plan->u_data); 1659 } 1660 1661 PLAN * 1662 c_user(argvp, isok) 1663 char ***argvp; 1664 int isok; 1665 { 1666 char *username = **argvp; 1667 PLAN *new; 1668 struct passwd *p; 1669 uid_t uid; 1670 1671 (*argvp)++; 1672 ftsoptions &= ~FTS_NOSTAT; 1673 1674 new = palloc(N_USER, f_user); 1675 p = getpwnam(username); 1676 if (p == NULL) { 1677 if (atoi(username) == 0 && username[0] != '0' && 1678 strcmp(username, "+0") && strcmp(username, "-0")) 1679 errx(1, "-user: %s: no such user", username); 1680 uid = find_parsenum(new, "-user", username, NULL); 1681 1682 } else { 1683 new->flags = F_EQUAL; 1684 uid = p->pw_uid; 1685 } 1686 1687 new->u_data = uid; 1688 return (new); 1689 } 1690 1691 /* 1692 * -xdev functions -- 1693 * 1694 * Always true, causes find not to decend past directories that have a 1695 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 1696 */ 1697 PLAN * 1698 c_xdev(argvp, isok) 1699 char ***argvp; 1700 int isok; 1701 { 1702 ftsoptions |= FTS_XDEV; 1703 1704 return (palloc(N_XDEV, f_always_true)); 1705 } 1706 1707 /* 1708 * ( expression ) functions -- 1709 * 1710 * True if expression is true. 1711 */ 1712 int 1713 f_expr(plan, entry) 1714 PLAN *plan; 1715 FTSENT *entry; 1716 { 1717 PLAN *p; 1718 int state; 1719 1720 state = 0; 1721 for (p = plan->p_data[0]; 1722 p && (state = (p->eval)(p, entry)); p = p->next); 1723 return (state); 1724 } 1725 1726 /* 1727 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are 1728 * eliminated during phase 2 of find_formplan() --- the '(' node is converted 1729 * to a N_EXPR node containing the expression and the ')' node is discarded. 1730 */ 1731 PLAN * 1732 c_openparen(argvp, isok) 1733 char ***argvp; 1734 int isok; 1735 { 1736 1737 return (palloc(N_OPENPAREN, (int (*) __P((PLAN *, FTSENT *)))-1)); 1738 } 1739 1740 PLAN * 1741 c_closeparen(argvp, isok) 1742 char ***argvp; 1743 int isok; 1744 { 1745 1746 return (palloc(N_CLOSEPAREN, (int (*) __P((PLAN *, FTSENT *)))-1)); 1747 } 1748 1749 /* 1750 * ! expression functions -- 1751 * 1752 * Negation of a primary; the unary NOT operator. 1753 */ 1754 int 1755 f_not(plan, entry) 1756 PLAN *plan; 1757 FTSENT *entry; 1758 { 1759 PLAN *p; 1760 int state; 1761 1762 state = 0; 1763 for (p = plan->p_data[0]; 1764 p && (state = (p->eval)(p, entry)); p = p->next); 1765 return (!state); 1766 } 1767 1768 PLAN * 1769 c_not(argvp, isok) 1770 char ***argvp; 1771 int isok; 1772 { 1773 1774 return (palloc(N_NOT, f_not)); 1775 } 1776 1777 /* 1778 * expression -o expression functions -- 1779 * 1780 * Alternation of primaries; the OR operator. The second expression is 1781 * not evaluated if the first expression is true. 1782 */ 1783 int 1784 f_or(plan, entry) 1785 PLAN *plan; 1786 FTSENT *entry; 1787 { 1788 PLAN *p; 1789 int state; 1790 1791 state = 0; 1792 for (p = plan->p_data[0]; 1793 p && (state = (p->eval)(p, entry)); p = p->next); 1794 1795 if (state) 1796 return (1); 1797 1798 for (p = plan->p_data[1]; 1799 p && (state = (p->eval)(p, entry)); p = p->next); 1800 return (state); 1801 } 1802 1803 PLAN * 1804 c_or(argvp, isok) 1805 char ***argvp; 1806 int isok; 1807 { 1808 1809 return (palloc(N_OR, f_or)); 1810 } 1811 1812 PLAN * 1813 c_null(argvp, isok) 1814 char ***argvp; 1815 int isok; 1816 { 1817 1818 return (NULL); 1819 } 1820 1821 static PLAN * 1822 palloc(t, f) 1823 enum ntype t; 1824 int (*f) __P((PLAN *, FTSENT *)); 1825 { 1826 PLAN *new; 1827 1828 if ((new = malloc(sizeof(PLAN))) == NULL) 1829 err(1, NULL); 1830 new->type = t; 1831 new->eval = f; 1832 new->flags = 0; 1833 new->next = NULL; 1834 return (new); 1835 } 1836