function.c revision 264699
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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#ifndef lint 34#if 0 35static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95"; 36#endif 37#endif /* not lint */ 38 39#include <sys/cdefs.h> 40__FBSDID("$FreeBSD: stable/9/usr.bin/find/function.c 264699 2014-04-20 16:41:30Z jilles $"); 41 42#include <sys/param.h> 43#include <sys/ucred.h> 44#include <sys/stat.h> 45#include <sys/types.h> 46#include <sys/acl.h> 47#include <sys/wait.h> 48#include <sys/mount.h> 49 50#include <dirent.h> 51#include <err.h> 52#include <errno.h> 53#include <fnmatch.h> 54#include <fts.h> 55#include <grp.h> 56#include <limits.h> 57#include <pwd.h> 58#include <regex.h> 59#include <stdio.h> 60#include <stdlib.h> 61#include <string.h> 62#include <unistd.h> 63#include <ctype.h> 64 65#include "find.h" 66 67static PLAN *palloc(OPTION *); 68static long long find_parsenum(PLAN *, const char *, char *, char *); 69static long long find_parsetime(PLAN *, const char *, char *); 70static char *nextarg(OPTION *, char ***); 71 72extern char **environ; 73 74static PLAN *lastexecplus = NULL; 75 76#define COMPARE(a, b) do { \ 77 switch (plan->flags & F_ELG_MASK) { \ 78 case F_EQUAL: \ 79 return (a == b); \ 80 case F_LESSTHAN: \ 81 return (a < b); \ 82 case F_GREATER: \ 83 return (a > b); \ 84 default: \ 85 abort(); \ 86 } \ 87} while(0) 88 89static PLAN * 90palloc(OPTION *option) 91{ 92 PLAN *new; 93 94 if ((new = malloc(sizeof(PLAN))) == NULL) 95 err(1, NULL); 96 new->execute = option->execute; 97 new->flags = option->flags; 98 new->next = NULL; 99 return new; 100} 101 102/* 103 * find_parsenum -- 104 * Parse a string of the form [+-]# and return the value. 105 */ 106static long long 107find_parsenum(PLAN *plan, const char *option, char *vp, char *endch) 108{ 109 long long value; 110 char *endchar, *str; /* Pointer to character ending conversion. */ 111 112 /* Determine comparison from leading + or -. */ 113 str = vp; 114 switch (*str) { 115 case '+': 116 ++str; 117 plan->flags |= F_GREATER; 118 break; 119 case '-': 120 ++str; 121 plan->flags |= F_LESSTHAN; 122 break; 123 default: 124 plan->flags |= F_EQUAL; 125 break; 126 } 127 128 /* 129 * Convert the string with strtoq(). Note, if strtoq() returns zero 130 * and endchar points to the beginning of the string we know we have 131 * a syntax error. 132 */ 133 value = strtoq(str, &endchar, 10); 134 if (value == 0 && endchar == str) 135 errx(1, "%s: %s: illegal numeric value", option, vp); 136 if (endchar[0] && endch == NULL) 137 errx(1, "%s: %s: illegal trailing character", option, vp); 138 if (endch) 139 *endch = endchar[0]; 140 return value; 141} 142 143/* 144 * find_parsetime -- 145 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value. 146 */ 147static long long 148find_parsetime(PLAN *plan, const char *option, char *vp) 149{ 150 long long secs, value; 151 char *str, *unit; /* Pointer to character ending conversion. */ 152 153 /* Determine comparison from leading + or -. */ 154 str = vp; 155 switch (*str) { 156 case '+': 157 ++str; 158 plan->flags |= F_GREATER; 159 break; 160 case '-': 161 ++str; 162 plan->flags |= F_LESSTHAN; 163 break; 164 default: 165 plan->flags |= F_EQUAL; 166 break; 167 } 168 169 value = strtoq(str, &unit, 10); 170 if (value == 0 && unit == str) { 171 errx(1, "%s: %s: illegal time value", option, vp); 172 /* NOTREACHED */ 173 } 174 if (*unit == '\0') 175 return value; 176 177 /* Units syntax. */ 178 secs = 0; 179 for (;;) { 180 switch(*unit) { 181 case 's': /* seconds */ 182 secs += value; 183 break; 184 case 'm': /* minutes */ 185 secs += value * 60; 186 break; 187 case 'h': /* hours */ 188 secs += value * 3600; 189 break; 190 case 'd': /* days */ 191 secs += value * 86400; 192 break; 193 case 'w': /* weeks */ 194 secs += value * 604800; 195 break; 196 default: 197 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit); 198 /* NOTREACHED */ 199 } 200 str = unit + 1; 201 if (*str == '\0') /* EOS */ 202 break; 203 value = strtoq(str, &unit, 10); 204 if (value == 0 && unit == str) { 205 errx(1, "%s: %s: illegal time value", option, vp); 206 /* NOTREACHED */ 207 } 208 if (*unit == '\0') { 209 errx(1, "%s: %s: missing trailing unit", option, vp); 210 /* NOTREACHED */ 211 } 212 } 213 plan->flags |= F_EXACTTIME; 214 return secs; 215} 216 217/* 218 * nextarg -- 219 * Check that another argument still exists, return a pointer to it, 220 * and increment the argument vector pointer. 221 */ 222static char * 223nextarg(OPTION *option, char ***argvp) 224{ 225 char *arg; 226 227 if ((arg = **argvp) == 0) 228 errx(1, "%s: requires additional arguments", option->name); 229 (*argvp)++; 230 return arg; 231} /* nextarg() */ 232 233/* 234 * The value of n for the inode times (atime, birthtime, ctime, mtime) is a 235 * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts 236 * with -n, such that "-mtime -1" would be less than 0 days, which isn't what 237 * the user wanted. Correct so that -1 is "less than 1". 238 */ 239#define TIME_CORRECT(p) \ 240 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \ 241 ++((p)->t_data.tv_sec); 242 243/* 244 * -[acm]min n functions -- 245 * 246 * True if the difference between the 247 * file access time (-amin) 248 * file birth time (-Bmin) 249 * last change of file status information (-cmin) 250 * file modification time (-mmin) 251 * and the current time is n min periods. 252 */ 253int 254f_Xmin(PLAN *plan, FTSENT *entry) 255{ 256 if (plan->flags & F_TIME_C) { 257 COMPARE((now - entry->fts_statp->st_ctime + 258 60 - 1) / 60, plan->t_data.tv_sec); 259 } else if (plan->flags & F_TIME_A) { 260 COMPARE((now - entry->fts_statp->st_atime + 261 60 - 1) / 60, plan->t_data.tv_sec); 262 } else if (plan->flags & F_TIME_B) { 263 COMPARE((now - entry->fts_statp->st_birthtime + 264 60 - 1) / 60, plan->t_data.tv_sec); 265 } else { 266 COMPARE((now - entry->fts_statp->st_mtime + 267 60 - 1) / 60, plan->t_data.tv_sec); 268 } 269} 270 271PLAN * 272c_Xmin(OPTION *option, char ***argvp) 273{ 274 char *nmins; 275 PLAN *new; 276 277 nmins = nextarg(option, argvp); 278 ftsoptions &= ~FTS_NOSTAT; 279 280 new = palloc(option); 281 new->t_data.tv_sec = find_parsenum(new, option->name, nmins, NULL); 282 new->t_data.tv_nsec = 0; 283 TIME_CORRECT(new); 284 return new; 285} 286 287/* 288 * -[acm]time n functions -- 289 * 290 * True if the difference between the 291 * file access time (-atime) 292 * file birth time (-Btime) 293 * last change of file status information (-ctime) 294 * file modification time (-mtime) 295 * and the current time is n 24 hour periods. 296 */ 297 298int 299f_Xtime(PLAN *plan, FTSENT *entry) 300{ 301 time_t xtime; 302 303 if (plan->flags & F_TIME_A) 304 xtime = entry->fts_statp->st_atime; 305 else if (plan->flags & F_TIME_B) 306 xtime = entry->fts_statp->st_birthtime; 307 else if (plan->flags & F_TIME_C) 308 xtime = entry->fts_statp->st_ctime; 309 else 310 xtime = entry->fts_statp->st_mtime; 311 312 if (plan->flags & F_EXACTTIME) 313 COMPARE(now - xtime, plan->t_data.tv_sec); 314 else 315 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data.tv_sec); 316} 317 318PLAN * 319c_Xtime(OPTION *option, char ***argvp) 320{ 321 char *value; 322 PLAN *new; 323 324 value = nextarg(option, argvp); 325 ftsoptions &= ~FTS_NOSTAT; 326 327 new = palloc(option); 328 new->t_data.tv_sec = find_parsetime(new, option->name, value); 329 new->t_data.tv_nsec = 0; 330 if (!(new->flags & F_EXACTTIME)) 331 TIME_CORRECT(new); 332 return new; 333} 334 335/* 336 * -maxdepth/-mindepth n functions -- 337 * 338 * Does the same as -prune if the level of the current file is 339 * greater/less than the specified maximum/minimum depth. 340 * 341 * Note that -maxdepth and -mindepth are handled specially in 342 * find_execute() so their f_* functions are set to f_always_true(). 343 */ 344PLAN * 345c_mXXdepth(OPTION *option, char ***argvp) 346{ 347 char *dstr; 348 PLAN *new; 349 350 dstr = nextarg(option, argvp); 351 if (dstr[0] == '-') 352 /* all other errors handled by find_parsenum() */ 353 errx(1, "%s: %s: value must be positive", option->name, dstr); 354 355 new = palloc(option); 356 if (option->flags & F_MAXDEPTH) 357 maxdepth = find_parsenum(new, option->name, dstr, NULL); 358 else 359 mindepth = find_parsenum(new, option->name, dstr, NULL); 360 return new; 361} 362 363/* 364 * -acl function -- 365 * 366 * Show files with EXTENDED ACL attributes. 367 */ 368int 369f_acl(PLAN *plan __unused, FTSENT *entry) 370{ 371 acl_t facl; 372 acl_type_t acl_type; 373 int acl_supported = 0, ret, trivial; 374 375 if (S_ISLNK(entry->fts_statp->st_mode)) 376 return 0; 377 ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4); 378 if (ret > 0) { 379 acl_supported = 1; 380 acl_type = ACL_TYPE_NFS4; 381 } else if (ret < 0 && errno != EINVAL) { 382 warn("%s", entry->fts_accpath); 383 return (0); 384 } 385 if (acl_supported == 0) { 386 ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED); 387 if (ret > 0) { 388 acl_supported = 1; 389 acl_type = ACL_TYPE_ACCESS; 390 } else if (ret < 0 && errno != EINVAL) { 391 warn("%s", entry->fts_accpath); 392 return (0); 393 } 394 } 395 if (acl_supported == 0) 396 return (0); 397 398 facl = acl_get_file(entry->fts_accpath, acl_type); 399 if (facl == NULL) { 400 warn("%s", entry->fts_accpath); 401 return (0); 402 } 403 ret = acl_is_trivial_np(facl, &trivial); 404 acl_free(facl); 405 if (ret) { 406 warn("%s", entry->fts_accpath); 407 acl_free(facl); 408 return (0); 409 } 410 if (trivial) 411 return (0); 412 return (1); 413} 414 415PLAN * 416c_acl(OPTION *option, char ***argvp __unused) 417{ 418 ftsoptions &= ~FTS_NOSTAT; 419 return (palloc(option)); 420} 421 422/* 423 * -delete functions -- 424 * 425 * True always. Makes its best shot and continues on regardless. 426 */ 427int 428f_delete(PLAN *plan __unused, FTSENT *entry) 429{ 430 /* ignore these from fts */ 431 if (strcmp(entry->fts_accpath, ".") == 0 || 432 strcmp(entry->fts_accpath, "..") == 0) 433 return 1; 434 435 /* sanity check */ 436 if (isdepth == 0 || /* depth off */ 437 (ftsoptions & FTS_NOSTAT)) /* not stat()ing */ 438 errx(1, "-delete: insecure options got turned on"); 439 440 if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */ 441 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */ 442 errx(1, "-delete: forbidden when symlinks are followed"); 443 444 /* Potentially unsafe - do not accept relative paths whatsoever */ 445 if (strchr(entry->fts_accpath, '/') != NULL) 446 errx(1, "-delete: %s: relative path potentially not safe", 447 entry->fts_accpath); 448 449 /* Turn off user immutable bits if running as root */ 450 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 451 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 452 geteuid() == 0) 453 lchflags(entry->fts_accpath, 454 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 455 456 /* rmdir directories, unlink everything else */ 457 if (S_ISDIR(entry->fts_statp->st_mode)) { 458 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY) 459 warn("-delete: rmdir(%s)", entry->fts_path); 460 } else { 461 if (unlink(entry->fts_accpath) < 0) 462 warn("-delete: unlink(%s)", entry->fts_path); 463 } 464 465 /* "succeed" */ 466 return 1; 467} 468 469PLAN * 470c_delete(OPTION *option, char ***argvp __unused) 471{ 472 473 ftsoptions &= ~FTS_NOSTAT; /* no optimise */ 474 isoutput = 1; /* possible output */ 475 isdepth = 1; /* -depth implied */ 476 477 return palloc(option); 478} 479 480 481/* 482 * always_true -- 483 * 484 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true 485 */ 486int 487f_always_true(PLAN *plan __unused, FTSENT *entry __unused) 488{ 489 return 1; 490} 491 492/* 493 * -depth functions -- 494 * 495 * With argument: True if the file is at level n. 496 * Without argument: Always true, causes descent of the directory hierarchy 497 * to be done so that all entries in a directory are acted on before the 498 * directory itself. 499 */ 500int 501f_depth(PLAN *plan, FTSENT *entry) 502{ 503 if (plan->flags & F_DEPTH) 504 COMPARE(entry->fts_level, plan->d_data); 505 else 506 return 1; 507} 508 509PLAN * 510c_depth(OPTION *option, char ***argvp) 511{ 512 PLAN *new; 513 char *str; 514 515 new = palloc(option); 516 517 str = **argvp; 518 if (str && !(new->flags & F_DEPTH)) { 519 /* skip leading + or - */ 520 if (*str == '+' || *str == '-') 521 str++; 522 /* skip sign */ 523 if (*str == '+' || *str == '-') 524 str++; 525 if (isdigit(*str)) 526 new->flags |= F_DEPTH; 527 } 528 529 if (new->flags & F_DEPTH) { /* -depth n */ 530 char *ndepth; 531 532 ndepth = nextarg(option, argvp); 533 new->d_data = find_parsenum(new, option->name, ndepth, NULL); 534 } else { /* -d */ 535 isdepth = 1; 536 } 537 538 return new; 539} 540 541/* 542 * -empty functions -- 543 * 544 * True if the file or directory is empty 545 */ 546int 547f_empty(PLAN *plan __unused, FTSENT *entry) 548{ 549 if (S_ISREG(entry->fts_statp->st_mode) && 550 entry->fts_statp->st_size == 0) 551 return 1; 552 if (S_ISDIR(entry->fts_statp->st_mode)) { 553 struct dirent *dp; 554 int empty; 555 DIR *dir; 556 557 empty = 1; 558 dir = opendir(entry->fts_accpath); 559 if (dir == NULL) 560 return 0; 561 for (dp = readdir(dir); dp; dp = readdir(dir)) 562 if (dp->d_name[0] != '.' || 563 (dp->d_name[1] != '\0' && 564 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) { 565 empty = 0; 566 break; 567 } 568 closedir(dir); 569 return empty; 570 } 571 return 0; 572} 573 574PLAN * 575c_empty(OPTION *option, char ***argvp __unused) 576{ 577 ftsoptions &= ~FTS_NOSTAT; 578 579 return palloc(option); 580} 581 582/* 583 * [-exec | -execdir | -ok] utility [arg ... ] ; functions -- 584 * 585 * True if the executed utility returns a zero value as exit status. 586 * The end of the primary expression is delimited by a semicolon. If 587 * "{}" occurs anywhere, it gets replaced by the current pathname, 588 * or, in the case of -execdir, the current basename (filename 589 * without leading directory prefix). For -exec and -ok, 590 * the current directory for the execution of utility is the same as 591 * the current directory when the find utility was started, whereas 592 * for -execdir, it is the directory the file resides in. 593 * 594 * The primary -ok differs from -exec in that it requests affirmation 595 * of the user before executing the utility. 596 */ 597int 598f_exec(PLAN *plan, FTSENT *entry) 599{ 600 int cnt; 601 pid_t pid; 602 int status; 603 char *file; 604 605 if (entry == NULL && plan->flags & F_EXECPLUS) { 606 if (plan->e_ppos == plan->e_pbnum) 607 return (1); 608 plan->e_argv[plan->e_ppos] = NULL; 609 goto doexec; 610 } 611 612 /* XXX - if file/dir ends in '/' this will not work -- can it? */ 613 if ((plan->flags & F_EXECDIR) && \ 614 (file = strrchr(entry->fts_path, '/'))) 615 file++; 616 else 617 file = entry->fts_path; 618 619 if (plan->flags & F_EXECPLUS) { 620 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL) 621 err(1, NULL); 622 plan->e_len[plan->e_ppos] = strlen(file); 623 plan->e_psize += plan->e_len[plan->e_ppos]; 624 if (++plan->e_ppos < plan->e_pnummax && 625 plan->e_psize < plan->e_psizemax) 626 return (1); 627 plan->e_argv[plan->e_ppos] = NULL; 628 } else { 629 for (cnt = 0; plan->e_argv[cnt]; ++cnt) 630 if (plan->e_len[cnt]) 631 brace_subst(plan->e_orig[cnt], 632 &plan->e_argv[cnt], file, 633 plan->e_len[cnt]); 634 } 635 636doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv)) 637 return 0; 638 639 /* make sure find output is interspersed correctly with subprocesses */ 640 fflush(stdout); 641 fflush(stderr); 642 643 switch (pid = fork()) { 644 case -1: 645 err(1, "fork"); 646 /* NOTREACHED */ 647 case 0: 648 /* change dir back from where we started */ 649 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) { 650 warn("chdir"); 651 _exit(1); 652 } 653 execvp(plan->e_argv[0], plan->e_argv); 654 warn("%s", plan->e_argv[0]); 655 _exit(1); 656 } 657 if (plan->flags & F_EXECPLUS) { 658 while (--plan->e_ppos >= plan->e_pbnum) 659 free(plan->e_argv[plan->e_ppos]); 660 plan->e_ppos = plan->e_pbnum; 661 plan->e_psize = plan->e_pbsize; 662 } 663 pid = waitpid(pid, &status, 0); 664 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); 665} 666 667/* 668 * c_exec, c_execdir, c_ok -- 669 * build three parallel arrays, one with pointers to the strings passed 670 * on the command line, one with (possibly duplicated) pointers to the 671 * argv array, and one with integer values that are lengths of the 672 * strings, but also flags meaning that the string has to be massaged. 673 */ 674PLAN * 675c_exec(OPTION *option, char ***argvp) 676{ 677 PLAN *new; /* node returned */ 678 long argmax; 679 int cnt, i; 680 char **argv, **ap, **ep, *p; 681 682 /* XXX - was in c_execdir, but seems unnecessary!? 683 ftsoptions &= ~FTS_NOSTAT; 684 */ 685 isoutput = 1; 686 687 /* XXX - this is a change from the previous coding */ 688 new = palloc(option); 689 690 for (ap = argv = *argvp;; ++ap) { 691 if (!*ap) 692 errx(1, 693 "%s: no terminating \";\" or \"+\"", option->name); 694 if (**ap == ';') 695 break; 696 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) { 697 new->flags |= F_EXECPLUS; 698 break; 699 } 700 } 701 702 if (ap == argv) 703 errx(1, "%s: no command specified", option->name); 704 705 cnt = ap - *argvp + 1; 706 if (new->flags & F_EXECPLUS) { 707 new->e_ppos = new->e_pbnum = cnt - 2; 708 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) { 709 warn("sysconf(_SC_ARG_MAX)"); 710 argmax = _POSIX_ARG_MAX; 711 } 712 argmax -= 1024; 713 for (ep = environ; *ep != NULL; ep++) 714 argmax -= strlen(*ep) + 1 + sizeof(*ep); 715 argmax -= 1 + sizeof(*ep); 716 new->e_pnummax = argmax / 16; 717 argmax -= sizeof(char *) * new->e_pnummax; 718 if (argmax <= 0) 719 errx(1, "no space for arguments"); 720 new->e_psizemax = argmax; 721 new->e_pbsize = 0; 722 cnt += new->e_pnummax + 1; 723 new->e_next = lastexecplus; 724 lastexecplus = new; 725 } 726 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL) 727 err(1, NULL); 728 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL) 729 err(1, NULL); 730 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL) 731 err(1, NULL); 732 733 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 734 new->e_orig[cnt] = *argv; 735 if (new->flags & F_EXECPLUS) 736 new->e_pbsize += strlen(*argv) + 1; 737 for (p = *argv; *p; ++p) 738 if (!(new->flags & F_EXECPLUS) && p[0] == '{' && 739 p[1] == '}') { 740 if ((new->e_argv[cnt] = 741 malloc(MAXPATHLEN)) == NULL) 742 err(1, NULL); 743 new->e_len[cnt] = MAXPATHLEN; 744 break; 745 } 746 if (!*p) { 747 new->e_argv[cnt] = *argv; 748 new->e_len[cnt] = 0; 749 } 750 } 751 if (new->flags & F_EXECPLUS) { 752 new->e_psize = new->e_pbsize; 753 cnt--; 754 for (i = 0; i < new->e_pnummax; i++) { 755 new->e_argv[cnt] = NULL; 756 new->e_len[cnt] = 0; 757 cnt++; 758 } 759 argv = ap; 760 goto done; 761 } 762 new->e_argv[cnt] = new->e_orig[cnt] = NULL; 763 764done: *argvp = argv + 1; 765 return new; 766} 767 768/* Finish any pending -exec ... {} + functions. */ 769void 770finish_execplus(void) 771{ 772 PLAN *p; 773 774 p = lastexecplus; 775 while (p != NULL) { 776 (p->execute)(p, NULL); 777 p = p->e_next; 778 } 779} 780 781int 782f_flags(PLAN *plan, FTSENT *entry) 783{ 784 u_long flags; 785 786 flags = entry->fts_statp->st_flags; 787 if (plan->flags & F_ATLEAST) 788 return (flags | plan->fl_flags) == flags && 789 !(flags & plan->fl_notflags); 790 else if (plan->flags & F_ANY) 791 return (flags & plan->fl_flags) || 792 (flags | plan->fl_notflags) != flags; 793 else 794 return flags == plan->fl_flags && 795 !(plan->fl_flags & plan->fl_notflags); 796} 797 798PLAN * 799c_flags(OPTION *option, char ***argvp) 800{ 801 char *flags_str; 802 PLAN *new; 803 u_long flags, notflags; 804 805 flags_str = nextarg(option, argvp); 806 ftsoptions &= ~FTS_NOSTAT; 807 808 new = palloc(option); 809 810 if (*flags_str == '-') { 811 new->flags |= F_ATLEAST; 812 flags_str++; 813 } else if (*flags_str == '+') { 814 new->flags |= F_ANY; 815 flags_str++; 816 } 817 if (strtofflags(&flags_str, &flags, ¬flags) == 1) 818 errx(1, "%s: %s: illegal flags string", option->name, flags_str); 819 820 new->fl_flags = flags; 821 new->fl_notflags = notflags; 822 return new; 823} 824 825/* 826 * -follow functions -- 827 * 828 * Always true, causes symbolic links to be followed on a global 829 * basis. 830 */ 831PLAN * 832c_follow(OPTION *option, char ***argvp __unused) 833{ 834 ftsoptions &= ~FTS_PHYSICAL; 835 ftsoptions |= FTS_LOGICAL; 836 837 return palloc(option); 838} 839 840/* 841 * -fstype functions -- 842 * 843 * True if the file is of a certain type. 844 */ 845int 846f_fstype(PLAN *plan, FTSENT *entry) 847{ 848 static dev_t curdev; /* need a guaranteed illegal dev value */ 849 static int first = 1; 850 struct statfs sb; 851 static int val_flags; 852 static char fstype[sizeof(sb.f_fstypename)]; 853 char *p, save[2] = {0,0}; 854 855 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN) 856 return 0; 857 858 /* Only check when we cross mount point. */ 859 if (first || curdev != entry->fts_statp->st_dev) { 860 curdev = entry->fts_statp->st_dev; 861 862 /* 863 * Statfs follows symlinks; find wants the link's filesystem, 864 * not where it points. 865 */ 866 if (entry->fts_info == FTS_SL || 867 entry->fts_info == FTS_SLNONE) { 868 if ((p = strrchr(entry->fts_accpath, '/')) != NULL) 869 ++p; 870 else 871 p = entry->fts_accpath; 872 save[0] = p[0]; 873 p[0] = '.'; 874 save[1] = p[1]; 875 p[1] = '\0'; 876 } else 877 p = NULL; 878 879 if (statfs(entry->fts_accpath, &sb)) 880 err(1, "%s", entry->fts_accpath); 881 882 if (p) { 883 p[0] = save[0]; 884 p[1] = save[1]; 885 } 886 887 first = 0; 888 889 /* 890 * Further tests may need both of these values, so 891 * always copy both of them. 892 */ 893 val_flags = sb.f_flags; 894 strlcpy(fstype, sb.f_fstypename, sizeof(fstype)); 895 } 896 switch (plan->flags & F_MTMASK) { 897 case F_MTFLAG: 898 return val_flags & plan->mt_data; 899 case F_MTTYPE: 900 return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0); 901 default: 902 abort(); 903 } 904} 905 906PLAN * 907c_fstype(OPTION *option, char ***argvp) 908{ 909 char *fsname; 910 PLAN *new; 911 912 fsname = nextarg(option, argvp); 913 ftsoptions &= ~FTS_NOSTAT; 914 915 new = palloc(option); 916 switch (*fsname) { 917 case 'l': 918 if (!strcmp(fsname, "local")) { 919 new->flags |= F_MTFLAG; 920 new->mt_data = MNT_LOCAL; 921 return new; 922 } 923 break; 924 case 'r': 925 if (!strcmp(fsname, "rdonly")) { 926 new->flags |= F_MTFLAG; 927 new->mt_data = MNT_RDONLY; 928 return new; 929 } 930 break; 931 } 932 933 new->flags |= F_MTTYPE; 934 new->c_data = fsname; 935 return new; 936} 937 938/* 939 * -group gname functions -- 940 * 941 * True if the file belongs to the group gname. If gname is numeric and 942 * an equivalent of the getgrnam() function does not return a valid group 943 * name, gname is taken as a group ID. 944 */ 945int 946f_group(PLAN *plan, FTSENT *entry) 947{ 948 COMPARE(entry->fts_statp->st_gid, plan->g_data); 949} 950 951PLAN * 952c_group(OPTION *option, char ***argvp) 953{ 954 char *gname; 955 PLAN *new; 956 struct group *g; 957 gid_t gid; 958 959 gname = nextarg(option, argvp); 960 ftsoptions &= ~FTS_NOSTAT; 961 962 new = palloc(option); 963 g = getgrnam(gname); 964 if (g == NULL) { 965 char* cp = gname; 966 if (gname[0] == '-' || gname[0] == '+') 967 gname++; 968 gid = atoi(gname); 969 if (gid == 0 && gname[0] != '0') 970 errx(1, "%s: %s: no such group", option->name, gname); 971 gid = find_parsenum(new, option->name, cp, NULL); 972 } else 973 gid = g->gr_gid; 974 975 new->g_data = gid; 976 return new; 977} 978 979/* 980 * -ignore_readdir_race functions -- 981 * 982 * Always true. Ignore errors which occur if a file or a directory 983 * in a starting point gets deleted between reading the name and calling 984 * stat on it while find is traversing the starting point. 985 */ 986 987PLAN * 988c_ignore_readdir_race(OPTION *option, char ***argvp __unused) 989{ 990 if (strcmp(option->name, "-ignore_readdir_race") == 0) 991 ignore_readdir_race = 1; 992 else 993 ignore_readdir_race = 0; 994 995 return palloc(option); 996} 997 998/* 999 * -inum n functions -- 1000 * 1001 * True if the file has inode # n. 1002 */ 1003int 1004f_inum(PLAN *plan, FTSENT *entry) 1005{ 1006 COMPARE(entry->fts_statp->st_ino, plan->i_data); 1007} 1008 1009PLAN * 1010c_inum(OPTION *option, char ***argvp) 1011{ 1012 char *inum_str; 1013 PLAN *new; 1014 1015 inum_str = nextarg(option, argvp); 1016 ftsoptions &= ~FTS_NOSTAT; 1017 1018 new = palloc(option); 1019 new->i_data = find_parsenum(new, option->name, inum_str, NULL); 1020 return new; 1021} 1022 1023/* 1024 * -samefile FN 1025 * 1026 * True if the file has the same inode (eg hard link) FN 1027 */ 1028 1029/* f_samefile is just f_inum */ 1030PLAN * 1031c_samefile(OPTION *option, char ***argvp) 1032{ 1033 char *fn; 1034 PLAN *new; 1035 struct stat sb; 1036 1037 fn = nextarg(option, argvp); 1038 ftsoptions &= ~FTS_NOSTAT; 1039 1040 new = palloc(option); 1041 if (stat(fn, &sb)) 1042 err(1, "%s", fn); 1043 new->i_data = sb.st_ino; 1044 return new; 1045} 1046 1047/* 1048 * -links n functions -- 1049 * 1050 * True if the file has n links. 1051 */ 1052int 1053f_links(PLAN *plan, FTSENT *entry) 1054{ 1055 COMPARE(entry->fts_statp->st_nlink, plan->l_data); 1056} 1057 1058PLAN * 1059c_links(OPTION *option, char ***argvp) 1060{ 1061 char *nlinks; 1062 PLAN *new; 1063 1064 nlinks = nextarg(option, argvp); 1065 ftsoptions &= ~FTS_NOSTAT; 1066 1067 new = palloc(option); 1068 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL); 1069 return new; 1070} 1071 1072/* 1073 * -ls functions -- 1074 * 1075 * Always true - prints the current entry to stdout in "ls" format. 1076 */ 1077int 1078f_ls(PLAN *plan __unused, FTSENT *entry) 1079{ 1080 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); 1081 return 1; 1082} 1083 1084PLAN * 1085c_ls(OPTION *option, char ***argvp __unused) 1086{ 1087 ftsoptions &= ~FTS_NOSTAT; 1088 isoutput = 1; 1089 1090 return palloc(option); 1091} 1092 1093/* 1094 * -name functions -- 1095 * 1096 * True if the basename of the filename being examined 1097 * matches pattern using Pattern Matching Notation S3.14 1098 */ 1099int 1100f_name(PLAN *plan, FTSENT *entry) 1101{ 1102 char fn[PATH_MAX]; 1103 const char *name; 1104 ssize_t len; 1105 1106 if (plan->flags & F_LINK) { 1107 /* 1108 * The below test both avoids obviously useless readlink() 1109 * calls and ensures that symlinks with existent target do 1110 * not match if symlinks are being followed. 1111 * Assumption: fts will stat all symlinks that are to be 1112 * followed and will return the stat information. 1113 */ 1114 if (entry->fts_info != FTS_NSOK && entry->fts_info != FTS_SL && 1115 entry->fts_info != FTS_SLNONE) 1116 return 0; 1117 len = readlink(entry->fts_accpath, fn, sizeof(fn) - 1); 1118 if (len == -1) 1119 return 0; 1120 fn[len] = '\0'; 1121 name = fn; 1122 } else 1123 name = entry->fts_name; 1124 return !fnmatch(plan->c_data, name, 1125 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 1126} 1127 1128PLAN * 1129c_name(OPTION *option, char ***argvp) 1130{ 1131 char *pattern; 1132 PLAN *new; 1133 1134 pattern = nextarg(option, argvp); 1135 new = palloc(option); 1136 new->c_data = pattern; 1137 return new; 1138} 1139 1140/* 1141 * -newer file functions -- 1142 * 1143 * True if the current file has been modified more recently 1144 * then the modification time of the file named by the pathname 1145 * file. 1146 */ 1147int 1148f_newer(PLAN *plan, FTSENT *entry) 1149{ 1150 struct timespec ft; 1151 1152 if (plan->flags & F_TIME_C) 1153 ft = entry->fts_statp->st_ctim; 1154 else if (plan->flags & F_TIME_A) 1155 ft = entry->fts_statp->st_atim; 1156 else if (plan->flags & F_TIME_B) 1157 ft = entry->fts_statp->st_birthtim; 1158 else 1159 ft = entry->fts_statp->st_mtim; 1160 return (ft.tv_sec > plan->t_data.tv_sec || 1161 (ft.tv_sec == plan->t_data.tv_sec && 1162 ft.tv_nsec > plan->t_data.tv_nsec)); 1163} 1164 1165PLAN * 1166c_newer(OPTION *option, char ***argvp) 1167{ 1168 char *fn_or_tspec; 1169 PLAN *new; 1170 struct stat sb; 1171 1172 fn_or_tspec = nextarg(option, argvp); 1173 ftsoptions &= ~FTS_NOSTAT; 1174 1175 new = palloc(option); 1176 /* compare against what */ 1177 if (option->flags & F_TIME2_T) { 1178 new->t_data.tv_sec = get_date(fn_or_tspec); 1179 if (new->t_data.tv_sec == (time_t) -1) 1180 errx(1, "Can't parse date/time: %s", fn_or_tspec); 1181 /* Use the seconds only in the comparison. */ 1182 new->t_data.tv_nsec = 999999999; 1183 } else { 1184 if (stat(fn_or_tspec, &sb)) 1185 err(1, "%s", fn_or_tspec); 1186 if (option->flags & F_TIME2_C) 1187 new->t_data = sb.st_ctim; 1188 else if (option->flags & F_TIME2_A) 1189 new->t_data = sb.st_atim; 1190 else if (option->flags & F_TIME2_B) 1191 new->t_data = sb.st_birthtim; 1192 else 1193 new->t_data = sb.st_mtim; 1194 } 1195 return new; 1196} 1197 1198/* 1199 * -nogroup functions -- 1200 * 1201 * True if file belongs to a user ID for which the equivalent 1202 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 1203 */ 1204int 1205f_nogroup(PLAN *plan __unused, FTSENT *entry) 1206{ 1207 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL; 1208} 1209 1210PLAN * 1211c_nogroup(OPTION *option, char ***argvp __unused) 1212{ 1213 ftsoptions &= ~FTS_NOSTAT; 1214 1215 return palloc(option); 1216} 1217 1218/* 1219 * -nouser functions -- 1220 * 1221 * True if file belongs to a user ID for which the equivalent 1222 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 1223 */ 1224int 1225f_nouser(PLAN *plan __unused, FTSENT *entry) 1226{ 1227 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL; 1228} 1229 1230PLAN * 1231c_nouser(OPTION *option, char ***argvp __unused) 1232{ 1233 ftsoptions &= ~FTS_NOSTAT; 1234 1235 return palloc(option); 1236} 1237 1238/* 1239 * -path functions -- 1240 * 1241 * True if the path of the filename being examined 1242 * matches pattern using Pattern Matching Notation S3.14 1243 */ 1244int 1245f_path(PLAN *plan, FTSENT *entry) 1246{ 1247 return !fnmatch(plan->c_data, entry->fts_path, 1248 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0); 1249} 1250 1251/* c_path is the same as c_name */ 1252 1253/* 1254 * -perm functions -- 1255 * 1256 * The mode argument is used to represent file mode bits. If it starts 1257 * with a leading digit, it's treated as an octal mode, otherwise as a 1258 * symbolic mode. 1259 */ 1260int 1261f_perm(PLAN *plan, FTSENT *entry) 1262{ 1263 mode_t mode; 1264 1265 mode = entry->fts_statp->st_mode & 1266 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 1267 if (plan->flags & F_ATLEAST) 1268 return (plan->m_data | mode) == mode; 1269 else if (plan->flags & F_ANY) 1270 return (mode & plan->m_data); 1271 else 1272 return mode == plan->m_data; 1273 /* NOTREACHED */ 1274} 1275 1276PLAN * 1277c_perm(OPTION *option, char ***argvp) 1278{ 1279 char *perm; 1280 PLAN *new; 1281 mode_t *set; 1282 1283 perm = nextarg(option, argvp); 1284 ftsoptions &= ~FTS_NOSTAT; 1285 1286 new = palloc(option); 1287 1288 if (*perm == '-') { 1289 new->flags |= F_ATLEAST; 1290 ++perm; 1291 } else if (*perm == '+') { 1292 new->flags |= F_ANY; 1293 ++perm; 1294 } 1295 1296 if ((set = setmode(perm)) == NULL) 1297 errx(1, "%s: %s: illegal mode string", option->name, perm); 1298 1299 new->m_data = getmode(set, 0); 1300 free(set); 1301 return new; 1302} 1303 1304/* 1305 * -print functions -- 1306 * 1307 * Always true, causes the current pathname to be written to 1308 * standard output. 1309 */ 1310int 1311f_print(PLAN *plan __unused, FTSENT *entry) 1312{ 1313 (void)puts(entry->fts_path); 1314 return 1; 1315} 1316 1317PLAN * 1318c_print(OPTION *option, char ***argvp __unused) 1319{ 1320 isoutput = 1; 1321 1322 return palloc(option); 1323} 1324 1325/* 1326 * -print0 functions -- 1327 * 1328 * Always true, causes the current pathname to be written to 1329 * standard output followed by a NUL character 1330 */ 1331int 1332f_print0(PLAN *plan __unused, FTSENT *entry) 1333{ 1334 fputs(entry->fts_path, stdout); 1335 fputc('\0', stdout); 1336 return 1; 1337} 1338 1339/* c_print0 is the same as c_print */ 1340 1341/* 1342 * -prune functions -- 1343 * 1344 * Prune a portion of the hierarchy. 1345 */ 1346int 1347f_prune(PLAN *plan __unused, FTSENT *entry) 1348{ 1349 if (fts_set(tree, entry, FTS_SKIP)) 1350 err(1, "%s", entry->fts_path); 1351 return 1; 1352} 1353 1354/* c_prune == c_simple */ 1355 1356/* 1357 * -regex functions -- 1358 * 1359 * True if the whole path of the file matches pattern using 1360 * regular expression. 1361 */ 1362int 1363f_regex(PLAN *plan, FTSENT *entry) 1364{ 1365 char *str; 1366 int len; 1367 regex_t *pre; 1368 regmatch_t pmatch; 1369 int errcode; 1370 char errbuf[LINE_MAX]; 1371 int matched; 1372 1373 pre = plan->re_data; 1374 str = entry->fts_path; 1375 len = strlen(str); 1376 matched = 0; 1377 1378 pmatch.rm_so = 0; 1379 pmatch.rm_eo = len; 1380 1381 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND); 1382 1383 if (errcode != 0 && errcode != REG_NOMATCH) { 1384 regerror(errcode, pre, errbuf, sizeof errbuf); 1385 errx(1, "%s: %s", 1386 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf); 1387 } 1388 1389 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len) 1390 matched = 1; 1391 1392 return matched; 1393} 1394 1395PLAN * 1396c_regex(OPTION *option, char ***argvp) 1397{ 1398 PLAN *new; 1399 char *pattern; 1400 regex_t *pre; 1401 int errcode; 1402 char errbuf[LINE_MAX]; 1403 1404 if ((pre = malloc(sizeof(regex_t))) == NULL) 1405 err(1, NULL); 1406 1407 pattern = nextarg(option, argvp); 1408 1409 if ((errcode = regcomp(pre, pattern, 1410 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) { 1411 regerror(errcode, pre, errbuf, sizeof errbuf); 1412 errx(1, "%s: %s: %s", 1413 option->flags & F_IGNCASE ? "-iregex" : "-regex", 1414 pattern, errbuf); 1415 } 1416 1417 new = palloc(option); 1418 new->re_data = pre; 1419 1420 return new; 1421} 1422 1423/* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */ 1424 1425PLAN * 1426c_simple(OPTION *option, char ***argvp __unused) 1427{ 1428 return palloc(option); 1429} 1430 1431/* 1432 * -size n[c] functions -- 1433 * 1434 * True if the file size in bytes, divided by an implementation defined 1435 * value and rounded up to the next integer, is n. If n is followed by 1436 * one of c k M G T P, the size is in bytes, kilobytes, 1437 * megabytes, gigabytes, terabytes or petabytes respectively. 1438 */ 1439#define FIND_SIZE 512 1440static int divsize = 1; 1441 1442int 1443f_size(PLAN *plan, FTSENT *entry) 1444{ 1445 off_t size; 1446 1447 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 1448 FIND_SIZE : entry->fts_statp->st_size; 1449 COMPARE(size, plan->o_data); 1450} 1451 1452PLAN * 1453c_size(OPTION *option, char ***argvp) 1454{ 1455 char *size_str; 1456 PLAN *new; 1457 char endch; 1458 off_t scale; 1459 1460 size_str = nextarg(option, argvp); 1461 ftsoptions &= ~FTS_NOSTAT; 1462 1463 new = palloc(option); 1464 endch = 'c'; 1465 new->o_data = find_parsenum(new, option->name, size_str, &endch); 1466 if (endch != '\0') { 1467 divsize = 0; 1468 1469 switch (endch) { 1470 case 'c': /* characters */ 1471 scale = 0x1LL; 1472 break; 1473 case 'k': /* kilobytes 1<<10 */ 1474 scale = 0x400LL; 1475 break; 1476 case 'M': /* megabytes 1<<20 */ 1477 scale = 0x100000LL; 1478 break; 1479 case 'G': /* gigabytes 1<<30 */ 1480 scale = 0x40000000LL; 1481 break; 1482 case 'T': /* terabytes 1<<40 */ 1483 scale = 0x1000000000LL; 1484 break; 1485 case 'P': /* petabytes 1<<50 */ 1486 scale = 0x4000000000000LL; 1487 break; 1488 default: 1489 errx(1, "%s: %s: illegal trailing character", 1490 option->name, size_str); 1491 break; 1492 } 1493 if (new->o_data > QUAD_MAX / scale) 1494 errx(1, "%s: %s: value too large", 1495 option->name, size_str); 1496 new->o_data *= scale; 1497 } 1498 return new; 1499} 1500 1501/* 1502 * -type c functions -- 1503 * 1504 * True if the type of the file is c, where c is b, c, d, p, f or w 1505 * for block special file, character special file, directory, FIFO, 1506 * regular file or whiteout respectively. 1507 */ 1508int 1509f_type(PLAN *plan, FTSENT *entry) 1510{ 1511 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data; 1512} 1513 1514PLAN * 1515c_type(OPTION *option, char ***argvp) 1516{ 1517 char *typestring; 1518 PLAN *new; 1519 mode_t mask; 1520 1521 typestring = nextarg(option, argvp); 1522 ftsoptions &= ~FTS_NOSTAT; 1523 1524 switch (typestring[0]) { 1525 case 'b': 1526 mask = S_IFBLK; 1527 break; 1528 case 'c': 1529 mask = S_IFCHR; 1530 break; 1531 case 'd': 1532 mask = S_IFDIR; 1533 break; 1534 case 'f': 1535 mask = S_IFREG; 1536 break; 1537 case 'l': 1538 mask = S_IFLNK; 1539 break; 1540 case 'p': 1541 mask = S_IFIFO; 1542 break; 1543 case 's': 1544 mask = S_IFSOCK; 1545 break; 1546#ifdef FTS_WHITEOUT 1547 case 'w': 1548 mask = S_IFWHT; 1549 ftsoptions |= FTS_WHITEOUT; 1550 break; 1551#endif /* FTS_WHITEOUT */ 1552 default: 1553 errx(1, "%s: %s: unknown type", option->name, typestring); 1554 } 1555 1556 new = palloc(option); 1557 new->m_data = mask; 1558 return new; 1559} 1560 1561/* 1562 * -user uname functions -- 1563 * 1564 * True if the file belongs to the user uname. If uname is numeric and 1565 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 1566 * return a valid user name, uname is taken as a user ID. 1567 */ 1568int 1569f_user(PLAN *plan, FTSENT *entry) 1570{ 1571 COMPARE(entry->fts_statp->st_uid, plan->u_data); 1572} 1573 1574PLAN * 1575c_user(OPTION *option, char ***argvp) 1576{ 1577 char *username; 1578 PLAN *new; 1579 struct passwd *p; 1580 uid_t uid; 1581 1582 username = nextarg(option, argvp); 1583 ftsoptions &= ~FTS_NOSTAT; 1584 1585 new = palloc(option); 1586 p = getpwnam(username); 1587 if (p == NULL) { 1588 char* cp = username; 1589 if( username[0] == '-' || username[0] == '+' ) 1590 username++; 1591 uid = atoi(username); 1592 if (uid == 0 && username[0] != '0') 1593 errx(1, "%s: %s: no such user", option->name, username); 1594 uid = find_parsenum(new, option->name, cp, NULL); 1595 } else 1596 uid = p->pw_uid; 1597 1598 new->u_data = uid; 1599 return new; 1600} 1601 1602/* 1603 * -xdev functions -- 1604 * 1605 * Always true, causes find not to descend past directories that have a 1606 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 1607 */ 1608PLAN * 1609c_xdev(OPTION *option, char ***argvp __unused) 1610{ 1611 ftsoptions |= FTS_XDEV; 1612 1613 return palloc(option); 1614} 1615 1616/* 1617 * ( expression ) functions -- 1618 * 1619 * True if expression is true. 1620 */ 1621int 1622f_expr(PLAN *plan, FTSENT *entry) 1623{ 1624 PLAN *p; 1625 int state = 0; 1626 1627 for (p = plan->p_data[0]; 1628 p && (state = (p->execute)(p, entry)); p = p->next); 1629 return state; 1630} 1631 1632/* 1633 * f_openparen and f_closeparen nodes are temporary place markers. They are 1634 * eliminated during phase 2 of find_formplan() --- the '(' node is converted 1635 * to a f_expr node containing the expression and the ')' node is discarded. 1636 * The functions themselves are only used as constants. 1637 */ 1638 1639int 1640f_openparen(PLAN *plan __unused, FTSENT *entry __unused) 1641{ 1642 abort(); 1643} 1644 1645int 1646f_closeparen(PLAN *plan __unused, FTSENT *entry __unused) 1647{ 1648 abort(); 1649} 1650 1651/* c_openparen == c_simple */ 1652/* c_closeparen == c_simple */ 1653 1654/* 1655 * AND operator. Since AND is implicit, no node is allocated. 1656 */ 1657PLAN * 1658c_and(OPTION *option __unused, char ***argvp __unused) 1659{ 1660 return NULL; 1661} 1662 1663/* 1664 * ! expression functions -- 1665 * 1666 * Negation of a primary; the unary NOT operator. 1667 */ 1668int 1669f_not(PLAN *plan, FTSENT *entry) 1670{ 1671 PLAN *p; 1672 int state = 0; 1673 1674 for (p = plan->p_data[0]; 1675 p && (state = (p->execute)(p, entry)); p = p->next); 1676 return !state; 1677} 1678 1679/* c_not == c_simple */ 1680 1681/* 1682 * expression -o expression functions -- 1683 * 1684 * Alternation of primaries; the OR operator. The second expression is 1685 * not evaluated if the first expression is true. 1686 */ 1687int 1688f_or(PLAN *plan, FTSENT *entry) 1689{ 1690 PLAN *p; 1691 int state = 0; 1692 1693 for (p = plan->p_data[0]; 1694 p && (state = (p->execute)(p, entry)); p = p->next); 1695 1696 if (state) 1697 return 1; 1698 1699 for (p = plan->p_data[1]; 1700 p && (state = (p->execute)(p, entry)); p = p->next); 1701 return state; 1702} 1703 1704/* c_or == c_simple */ 1705 1706/* 1707 * -false 1708 * 1709 * Always false. 1710 */ 1711int 1712f_false(PLAN *plan __unused, FTSENT *entry __unused) 1713{ 1714 return 0; 1715} 1716 1717/* c_false == c_simple */ 1718 1719/* 1720 * -quit 1721 * 1722 * Exits the program 1723 */ 1724int 1725f_quit(PLAN *plan __unused, FTSENT *entry __unused) 1726{ 1727 finish_execplus(); 1728 exit(0); 1729} 1730 1731/* c_quit == c_simple */ 1732