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