function.c revision 22066
11573Srgrimes/*- 21573Srgrimes * Copyright (c) 1990, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * This code is derived from software contributed to Berkeley by 61573Srgrimes * Cimarron D. Taylor of the University of California, Berkeley. 71573Srgrimes * 81573Srgrimes * Redistribution and use in source and binary forms, with or without 91573Srgrimes * modification, are permitted provided that the following conditions 101573Srgrimes * are met: 111573Srgrimes * 1. Redistributions of source code must retain the above copyright 121573Srgrimes * notice, this list of conditions and the following disclaimer. 131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141573Srgrimes * notice, this list of conditions and the following disclaimer in the 151573Srgrimes * documentation and/or other materials provided with the distribution. 161573Srgrimes * 3. All advertising materials mentioning features or use of this software 171573Srgrimes * must display the following acknowledgement: 181573Srgrimes * This product includes software developed by the University of 191573Srgrimes * California, Berkeley and its contributors. 201573Srgrimes * 4. Neither the name of the University nor the names of its contributors 211573Srgrimes * may be used to endorse or promote products derived from this software 221573Srgrimes * without specific prior written permission. 231573Srgrimes * 241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2814699Sbde * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2950476Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3379531Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341573Srgrimes * SUCH DAMAGE. 351573Srgrimes */ 361573Srgrimes 3759460Sphantom#ifndef lint 3859460Sphantomstatic char sccsid[] = "@(#)function.c 8.6 (Berkeley) 4/1/94"; 391573Srgrimes#endif /* not lint */ 4084306Sru 41108380Smike#include <sys/param.h> 42108380Smike#include <sys/ucred.h> 431573Srgrimes#include <sys/stat.h> 441573Srgrimes#include <sys/wait.h> 4581285Sru#include <sys/mount.h> 4681285Sru 471573Srgrimes#include <err.h> 481573Srgrimes#include <errno.h> 491573Srgrimes#include <fnmatch.h> 501573Srgrimes#include <fts.h> 511573Srgrimes#include <grp.h> 521573Srgrimes#include <pwd.h> 53108040Sru#include <stdio.h> 541573Srgrimes#include <stdlib.h> 551573Srgrimes#include <string.h> 561573Srgrimes#include <unistd.h> 571573Srgrimes 581573Srgrimes#include "find.h" 591573Srgrimes 601573Srgrimes#define COMPARE(a, b) { \ 611573Srgrimes switch (plan->flags) { \ 621573Srgrimes case F_EQUAL: \ 631573Srgrimes return (a == b); \ 641573Srgrimes case F_LESSTHAN: \ 651573Srgrimes return (a < b); \ 66131504Sru case F_GREATER: \ 67129064Sbrueffer return (a > b); \ 68129064Sbrueffer default: \ 6928209Sache abort(); \ 7028209Sache } \ 7128209Sache} 7228209Sache 73108040Srustatic PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *)))); 7428209Sache 75108040Sru/* 7628209Sache * find_parsenum -- 77129064Sbrueffer * Parse a string of the form [+-]# and return the value. 781573Srgrimes */ 791573Srgrimesstatic long long 8014699Sbdefind_parsenum(plan, option, vp, endch) 811573Srgrimes PLAN *plan; 8228193Sache char *option, *vp, *endch; 8328193Sache{ 841573Srgrimes long long value; 851573Srgrimes char *endchar, *str; /* Pointer to character ending conversion. */ 861573Srgrimes 87207735Sjilles /* Determine comparison from leading + or -. */ 88207735Sjilles str = vp; 8929988Swosch switch (*str) { 901573Srgrimes case '+': 911573Srgrimes ++str; 921573Srgrimes plan->flags = F_GREATER; 931573Srgrimes break; 941573Srgrimes case '-': 951573Srgrimes ++str; 9667967Sasmodai plan->flags = F_LESSTHAN; 971573Srgrimes break; 98 default: 99 plan->flags = F_EQUAL; 100 break; 101 } 102 103 /* 104 * Convert the string with strtoq(). Note, if strtoq() returns zero 105 * and endchar points to the beginning of the string we know we have 106 * a syntax error. 107 */ 108 value = strtoq(str, &endchar, 10); 109 if (value == 0 && endchar == str) 110 errx(1, "%s: %s: illegal numeric value", option, vp); 111 if (endchar[0] && (endch == NULL || endchar[0] != *endch)) 112 errx(1, "%s: %s: illegal trailing character", option, vp); 113 if (endch) 114 *endch = endchar[0]; 115 return (value); 116} 117 118/* 119 * The value of n for the inode times (atime, ctime, and mtime) is a range, 120 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with 121 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the 122 * user wanted. Correct so that -1 is "less than 1". 123 */ 124#define TIME_CORRECT(p, ttype) \ 125 if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \ 126 ++((p)->t_data); 127 128/* 129 * -atime n functions -- 130 * 131 * True if the difference between the file access time and the 132 * current time is n 24 hour periods. 133 */ 134int 135f_atime(plan, entry) 136 PLAN *plan; 137 FTSENT *entry; 138{ 139 extern time_t now; 140 141 COMPARE((now - entry->fts_statp->st_atime + 142 86400 - 1) / 86400, plan->t_data); 143} 144 145PLAN * 146c_atime(arg) 147 char *arg; 148{ 149 PLAN *new; 150 151 ftsoptions &= ~FTS_NOSTAT; 152 153 new = palloc(N_ATIME, f_atime); 154 new->t_data = find_parsenum(new, "-atime", arg, NULL); 155 TIME_CORRECT(new, N_ATIME); 156 return (new); 157} 158/* 159 * -ctime n functions -- 160 * 161 * True if the difference between the last change of file 162 * status information and the current time is n 24 hour periods. 163 */ 164int 165f_ctime(plan, entry) 166 PLAN *plan; 167 FTSENT *entry; 168{ 169 extern time_t now; 170 171 COMPARE((now - entry->fts_statp->st_ctime + 172 86400 - 1) / 86400, plan->t_data); 173} 174 175PLAN * 176c_ctime(arg) 177 char *arg; 178{ 179 PLAN *new; 180 181 ftsoptions &= ~FTS_NOSTAT; 182 183 new = palloc(N_CTIME, f_ctime); 184 new->t_data = find_parsenum(new, "-ctime", arg, NULL); 185 TIME_CORRECT(new, N_CTIME); 186 return (new); 187} 188 189/* 190 * -depth functions -- 191 * 192 * Always true, causes descent of the directory hierarchy to be done 193 * so that all entries in a directory are acted on before the directory 194 * itself. 195 */ 196int 197f_always_true(plan, entry) 198 PLAN *plan; 199 FTSENT *entry; 200{ 201 return (1); 202} 203 204PLAN * 205c_depth() 206{ 207 isdepth = 1; 208 209 return (palloc(N_DEPTH, f_always_true)); 210} 211 212/* 213 * [-exec | -ok] utility [arg ... ] ; functions -- 214 * 215 * True if the executed utility returns a zero value as exit status. 216 * The end of the primary expression is delimited by a semicolon. If 217 * "{}" occurs anywhere, it gets replaced by the current pathname. 218 * The current directory for the execution of utility is the same as 219 * the current directory when the find utility was started. 220 * 221 * The primary -ok is different in that it requests affirmation of the 222 * user before executing the utility. 223 */ 224int 225f_exec(plan, entry) 226 register PLAN *plan; 227 FTSENT *entry; 228{ 229 extern int dotfd; 230 register int cnt; 231 pid_t pid; 232 int status; 233 234 for (cnt = 0; plan->e_argv[cnt]; ++cnt) 235 if (plan->e_len[cnt]) 236 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt], 237 entry->fts_path, plan->e_len[cnt]); 238 239 if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv)) 240 return (0); 241 242 /* make sure find output is interspersed correctly with subprocesses */ 243 fflush(stdout); 244 245 switch (pid = vfork()) { 246 case -1: 247 err(1, "fork"); 248 /* NOTREACHED */ 249 case 0: 250 if (fchdir(dotfd)) { 251 warn("chdir"); 252 _exit(1); 253 } 254 execvp(plan->e_argv[0], plan->e_argv); 255 warn("%s", plan->e_argv[0]); 256 _exit(1); 257 } 258 pid = waitpid(pid, &status, 0); 259 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); 260} 261 262/* 263 * c_exec -- 264 * build three parallel arrays, one with pointers to the strings passed 265 * on the command line, one with (possibly duplicated) pointers to the 266 * argv array, and one with integer values that are lengths of the 267 * strings, but also flags meaning that the string has to be massaged. 268 */ 269PLAN * 270c_exec(argvp, isok) 271 char ***argvp; 272 int isok; 273{ 274 PLAN *new; /* node returned */ 275 register int cnt; 276 register char **argv, **ap, *p; 277 278 isoutput = 1; 279 280 new = palloc(N_EXEC, f_exec); 281 if (isok) 282 new->flags = F_NEEDOK; 283 284 for (ap = argv = *argvp;; ++ap) { 285 if (!*ap) 286 errx(1, 287 "%s: no terminating \";\"", isok ? "-ok" : "-exec"); 288 if (**ap == ';') 289 break; 290 } 291 292 cnt = ap - *argvp + 1; 293 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *)); 294 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *)); 295 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int)); 296 297 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 298 new->e_orig[cnt] = *argv; 299 for (p = *argv; *p; ++p) 300 if (p[0] == '{' && p[1] == '}') { 301 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN); 302 new->e_len[cnt] = MAXPATHLEN; 303 break; 304 } 305 if (!*p) { 306 new->e_argv[cnt] = *argv; 307 new->e_len[cnt] = 0; 308 } 309 } 310 new->e_argv[cnt] = new->e_orig[cnt] = NULL; 311 312 *argvp = argv + 1; 313 return (new); 314} 315 316/* 317 * -follow functions -- 318 * 319 * Always true, causes symbolic links to be followed on a global 320 * basis. 321 */ 322PLAN * 323c_follow() 324{ 325 ftsoptions &= ~FTS_PHYSICAL; 326 ftsoptions |= FTS_LOGICAL; 327 328 return (palloc(N_FOLLOW, f_always_true)); 329} 330 331/* 332 * -fstype functions -- 333 * 334 * True if the file is of a certain type. 335 */ 336int 337f_fstype(plan, entry) 338 PLAN *plan; 339 FTSENT *entry; 340{ 341 static dev_t curdev; /* need a guaranteed illegal dev value */ 342 static int first = 1; 343 struct statfs sb; 344 static short val; 345 char *p, save[2]; 346 347 /* Only check when we cross mount point. */ 348 if (first || curdev != entry->fts_statp->st_dev) { 349 curdev = entry->fts_statp->st_dev; 350 351 /* 352 * Statfs follows symlinks; find wants the link's file system, 353 * not where it points. 354 */ 355 if (entry->fts_info == FTS_SL || 356 entry->fts_info == FTS_SLNONE) { 357 if ((p = strrchr(entry->fts_accpath, '/')) != NULL) 358 ++p; 359 else 360 p = entry->fts_accpath; 361 save[0] = p[0]; 362 p[0] = '.'; 363 save[1] = p[1]; 364 p[1] = '\0'; 365 366 } else 367 p = NULL; 368 369 if (statfs(entry->fts_accpath, &sb)) 370 err(1, "%s", entry->fts_accpath); 371 372 if (p) { 373 p[0] = save[0]; 374 p[1] = save[1]; 375 } 376 377 first = 0; 378 switch (plan->flags) { 379 case F_MTFLAG: 380 val = sb.f_flags; 381 break; 382 case F_MTTYPE: 383 val = sb.f_type; 384 break; 385 default: 386 abort(); 387 } 388 } 389 switch(plan->flags) { 390 case F_MTFLAG: 391 return (val & plan->mt_data); 392 case F_MTTYPE: 393 return (val == plan->mt_data); 394 default: 395 abort(); 396 } 397} 398 399PLAN * 400c_fstype(arg) 401 char *arg; 402{ 403 register PLAN *new; 404 405 ftsoptions &= ~FTS_NOSTAT; 406 407 new = palloc(N_FSTYPE, f_fstype); 408 switch (*arg) { 409 case 'l': 410 if (!strcmp(arg, "local")) { 411 new->flags = F_MTFLAG; 412 new->mt_data = MNT_LOCAL; 413 return (new); 414 } 415 break; 416 case 'm': 417 if (!strcmp(arg, "mfs")) { 418 new->flags = F_MTTYPE; 419 new->mt_data = MOUNT_MFS; 420 return (new); 421 } else if (!strcmp(arg, "msdos")) { 422 new->flags = F_MTTYPE; 423 new->mt_data = MOUNT_MSDOS; 424 return (new); 425 } 426 break; 427 case 'n': 428 if (!strcmp(arg, "nfs")) { 429 new->flags = F_MTTYPE; 430 new->mt_data = MOUNT_NFS; 431 return (new); 432 } 433 break; 434 case 'r': 435 if (!strcmp(arg, "rdonly")) { 436 new->flags = F_MTFLAG; 437 new->mt_data = MNT_RDONLY; 438 return (new); 439 } 440 break; 441 case 'u': 442 if (!strcmp(arg, "ufs")) { 443 new->flags = F_MTTYPE; 444 new->mt_data = MOUNT_UFS; 445 return (new); 446 } 447 break; 448 } 449 errx(1, "%s: unknown file type", arg); 450 /* NOTREACHED */ 451} 452 453/* 454 * -group gname functions -- 455 * 456 * True if the file belongs to the group gname. If gname is numeric and 457 * an equivalent of the getgrnam() function does not return a valid group 458 * name, gname is taken as a group ID. 459 */ 460int 461f_group(plan, entry) 462 PLAN *plan; 463 FTSENT *entry; 464{ 465 return (entry->fts_statp->st_gid == plan->g_data); 466} 467 468PLAN * 469c_group(gname) 470 char *gname; 471{ 472 PLAN *new; 473 struct group *g; 474 gid_t gid; 475 476 ftsoptions &= ~FTS_NOSTAT; 477 478 g = getgrnam(gname); 479 if (g == NULL) { 480 gid = atoi(gname); 481 if (gid == 0 && gname[0] != '0') 482 errx(1, "-group: %s: no such group", gname); 483 } else 484 gid = g->gr_gid; 485 486 new = palloc(N_GROUP, f_group); 487 new->g_data = gid; 488 return (new); 489} 490 491/* 492 * -inum n functions -- 493 * 494 * True if the file has inode # n. 495 */ 496int 497f_inum(plan, entry) 498 PLAN *plan; 499 FTSENT *entry; 500{ 501 COMPARE(entry->fts_statp->st_ino, plan->i_data); 502} 503 504PLAN * 505c_inum(arg) 506 char *arg; 507{ 508 PLAN *new; 509 510 ftsoptions &= ~FTS_NOSTAT; 511 512 new = palloc(N_INUM, f_inum); 513 new->i_data = find_parsenum(new, "-inum", arg, NULL); 514 return (new); 515} 516 517/* 518 * -links n functions -- 519 * 520 * True if the file has n links. 521 */ 522int 523f_links(plan, entry) 524 PLAN *plan; 525 FTSENT *entry; 526{ 527 COMPARE(entry->fts_statp->st_nlink, plan->l_data); 528} 529 530PLAN * 531c_links(arg) 532 char *arg; 533{ 534 PLAN *new; 535 536 ftsoptions &= ~FTS_NOSTAT; 537 538 new = palloc(N_LINKS, f_links); 539 new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL); 540 return (new); 541} 542 543/* 544 * -ls functions -- 545 * 546 * Always true - prints the current entry to stdout in "ls" format. 547 */ 548int 549f_ls(plan, entry) 550 PLAN *plan; 551 FTSENT *entry; 552{ 553 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); 554 return (1); 555} 556 557PLAN * 558c_ls() 559{ 560 ftsoptions &= ~FTS_NOSTAT; 561 isoutput = 1; 562 563 return (palloc(N_LS, f_ls)); 564} 565 566/* 567 * -mtime n functions -- 568 * 569 * True if the difference between the file modification time and the 570 * current time is n 24 hour periods. 571 */ 572int 573f_mtime(plan, entry) 574 PLAN *plan; 575 FTSENT *entry; 576{ 577 extern time_t now; 578 579 COMPARE((now - entry->fts_statp->st_mtime + 86400 - 1) / 580 86400, plan->t_data); 581} 582 583PLAN * 584c_mtime(arg) 585 char *arg; 586{ 587 PLAN *new; 588 589 ftsoptions &= ~FTS_NOSTAT; 590 591 new = palloc(N_MTIME, f_mtime); 592 new->t_data = find_parsenum(new, "-mtime", arg, NULL); 593 TIME_CORRECT(new, N_MTIME); 594 return (new); 595} 596 597/* 598 * -name functions -- 599 * 600 * True if the basename of the filename being examined 601 * matches pattern using Pattern Matching Notation S3.14 602 */ 603int 604f_name(plan, entry) 605 PLAN *plan; 606 FTSENT *entry; 607{ 608 return (!fnmatch(plan->c_data, entry->fts_name, 0)); 609} 610 611PLAN * 612c_name(pattern) 613 char *pattern; 614{ 615 PLAN *new; 616 617 new = palloc(N_NAME, f_name); 618 new->c_data = pattern; 619 return (new); 620} 621 622/* 623 * -newer file functions -- 624 * 625 * True if the current file has been modified more recently 626 * then the modification time of the file named by the pathname 627 * file. 628 */ 629int 630f_newer(plan, entry) 631 PLAN *plan; 632 FTSENT *entry; 633{ 634 return (entry->fts_statp->st_mtime > plan->t_data); 635} 636 637PLAN * 638c_newer(filename) 639 char *filename; 640{ 641 PLAN *new; 642 struct stat sb; 643 644 ftsoptions &= ~FTS_NOSTAT; 645 646 if (stat(filename, &sb)) 647 err(1, "%s", filename); 648 new = palloc(N_NEWER, f_newer); 649 new->t_data = sb.st_mtime; 650 return (new); 651} 652 653/* 654 * -nogroup functions -- 655 * 656 * True if file belongs to a user ID for which the equivalent 657 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 658 */ 659int 660f_nogroup(plan, entry) 661 PLAN *plan; 662 FTSENT *entry; 663{ 664 char *group_from_gid(); 665 666 return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1); 667} 668 669PLAN * 670c_nogroup() 671{ 672 ftsoptions &= ~FTS_NOSTAT; 673 674 return (palloc(N_NOGROUP, f_nogroup)); 675} 676 677/* 678 * -nouser functions -- 679 * 680 * True if file belongs to a user ID for which the equivalent 681 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 682 */ 683int 684f_nouser(plan, entry) 685 PLAN *plan; 686 FTSENT *entry; 687{ 688 char *user_from_uid(); 689 690 return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1); 691} 692 693PLAN * 694c_nouser() 695{ 696 ftsoptions &= ~FTS_NOSTAT; 697 698 return (palloc(N_NOUSER, f_nouser)); 699} 700 701/* 702 * -path functions -- 703 * 704 * True if the path of the filename being examined 705 * matches pattern using Pattern Matching Notation S3.14 706 */ 707int 708f_path(plan, entry) 709 PLAN *plan; 710 FTSENT *entry; 711{ 712 return (!fnmatch(plan->c_data, entry->fts_path, 0)); 713} 714 715PLAN * 716c_path(pattern) 717 char *pattern; 718{ 719 PLAN *new; 720 721 new = palloc(N_NAME, f_path); 722 new->c_data = pattern; 723 return (new); 724} 725 726/* 727 * -perm functions -- 728 * 729 * The mode argument is used to represent file mode bits. If it starts 730 * with a leading digit, it's treated as an octal mode, otherwise as a 731 * symbolic mode. 732 */ 733int 734f_perm(plan, entry) 735 PLAN *plan; 736 FTSENT *entry; 737{ 738 mode_t mode; 739 740 mode = entry->fts_statp->st_mode & 741 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 742 if (plan->flags == F_ATLEAST) 743 return ((plan->m_data | mode) == mode); 744 else 745 return (mode == plan->m_data); 746 /* NOTREACHED */ 747} 748 749PLAN * 750c_perm(perm) 751 char *perm; 752{ 753 PLAN *new; 754 mode_t *set; 755 756 ftsoptions &= ~FTS_NOSTAT; 757 758 new = palloc(N_PERM, f_perm); 759 760 if (*perm == '-') { 761 new->flags = F_ATLEAST; 762 ++perm; 763 } 764 765 if ((set = setmode(perm)) == NULL) 766 err(1, "-perm: %s: illegal mode string", perm); 767 768 new->m_data = getmode(set, 0); 769 return (new); 770} 771 772/* 773 * -print functions -- 774 * 775 * Always true, causes the current pathame to be written to 776 * standard output. 777 */ 778int 779f_print(plan, entry) 780 PLAN *plan; 781 FTSENT *entry; 782{ 783 (void)puts(entry->fts_path); 784 return (1); 785} 786 787PLAN * 788c_print() 789{ 790 isoutput = 1; 791 792 return (palloc(N_PRINT, f_print)); 793} 794 795/* 796 * -print0 functions -- 797 * 798 * Always true, causes the current pathame to be written to 799 * standard output followed by a NUL character 800 */ 801int 802f_print0(plan, entry) 803 PLAN *plan; 804 FTSENT *entry; 805{ 806 fputs(entry->fts_path, stdout); 807 fputc('\0', stdout); 808 return (1); 809} 810 811PLAN * 812c_print0() 813{ 814 isoutput = 1; 815 816 return (palloc(N_PRINT0, f_print0)); 817} 818 819/* 820 * -prune functions -- 821 * 822 * Prune a portion of the hierarchy. 823 */ 824int 825f_prune(plan, entry) 826 PLAN *plan; 827 FTSENT *entry; 828{ 829 extern FTS *tree; 830 831 if (fts_set(tree, entry, FTS_SKIP)) 832 err(1, "%s", entry->fts_path); 833 return (1); 834} 835 836PLAN * 837c_prune() 838{ 839 return (palloc(N_PRUNE, f_prune)); 840} 841 842/* 843 * -size n[c] functions -- 844 * 845 * True if the file size in bytes, divided by an implementation defined 846 * value and rounded up to the next integer, is n. If n is followed by 847 * a c, the size is in bytes. 848 */ 849#define FIND_SIZE 512 850static int divsize = 1; 851 852int 853f_size(plan, entry) 854 PLAN *plan; 855 FTSENT *entry; 856{ 857 off_t size; 858 859 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 860 FIND_SIZE : entry->fts_statp->st_size; 861 COMPARE(size, plan->o_data); 862} 863 864PLAN * 865c_size(arg) 866 char *arg; 867{ 868 PLAN *new; 869 char endch; 870 871 ftsoptions &= ~FTS_NOSTAT; 872 873 new = palloc(N_SIZE, f_size); 874 endch = 'c'; 875 new->o_data = find_parsenum(new, "-size", arg, &endch); 876 if (endch == 'c') 877 divsize = 0; 878 return (new); 879} 880 881/* 882 * -type c functions -- 883 * 884 * True if the type of the file is c, where c is b, c, d, p, or f for 885 * block special file, character special file, directory, FIFO, or 886 * regular file, respectively. 887 */ 888int 889f_type(plan, entry) 890 PLAN *plan; 891 FTSENT *entry; 892{ 893 return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data); 894} 895 896PLAN * 897c_type(typestring) 898 char *typestring; 899{ 900 PLAN *new; 901 mode_t mask; 902 903 ftsoptions &= ~FTS_NOSTAT; 904 905 switch (typestring[0]) { 906 case 'b': 907 mask = S_IFBLK; 908 break; 909 case 'c': 910 mask = S_IFCHR; 911 break; 912 case 'd': 913 mask = S_IFDIR; 914 break; 915 case 'f': 916 mask = S_IFREG; 917 break; 918 case 'l': 919 mask = S_IFLNK; 920 break; 921 case 'p': 922 mask = S_IFIFO; 923 break; 924 case 's': 925 mask = S_IFSOCK; 926 break; 927 default: 928 errx(1, "-type: %s: unknown type", typestring); 929 } 930 931 new = palloc(N_TYPE, f_type); 932 new->m_data = mask; 933 return (new); 934} 935 936/* 937 * -delete functions -- 938 * 939 * True always. Makes it's best shot and continues on regardless. 940 */ 941int 942f_delete(plan, entry) 943 PLAN *plan; 944 FTSENT *entry; 945{ 946 /* ignore these from fts */ 947 if (strcmp(entry->fts_accpath, ".") == 0 || 948 strcmp(entry->fts_accpath, "..") == 0) 949 return (1); 950 951 /* sanity check */ 952 if (isdepth == 0 || /* depth off */ 953 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */ 954 !(ftsoptions & FTS_PHYSICAL) || /* physical off */ 955 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */ 956 errx(1, "-delete: insecure options got turned on"); 957 958 /* Potentially unsafe - do not accept relative paths whatsoever */ 959 if (strchr(entry->fts_accpath, '/') != NULL) 960 errx(1, "-delete: %s: relative path potentially not safe", 961 entry->fts_accpath); 962 963 /* Turn off user immutable bits if running as root */ 964 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 965 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 966 geteuid() == 0) 967 chflags(entry->fts_accpath, 968 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 969 970 /* rmdir directories, unlink everything else */ 971 if (S_ISDIR(entry->fts_statp->st_mode)) { 972 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY) 973 warn("-delete: rmdir(%s)", entry->fts_path); 974 } else { 975 if (unlink(entry->fts_accpath) < 0) 976 warn("-delete: unlink(%s)", entry->fts_path); 977 } 978 979 /* "succeed" */ 980 return (1); 981} 982 983PLAN * 984c_delete() 985{ 986 987 ftsoptions &= ~FTS_NOSTAT; /* no optimise */ 988 ftsoptions |= FTS_PHYSICAL; /* disable -follow */ 989 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */ 990 isoutput = 1; /* possible output */ 991 isdepth = 1; /* -depth implied */ 992 993 return (palloc(N_DELETE, f_delete)); 994} 995 996/* 997 * -user uname functions -- 998 * 999 * True if the file belongs to the user uname. If uname is numeric and 1000 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 1001 * return a valid user name, uname is taken as a user ID. 1002 */ 1003int 1004f_user(plan, entry) 1005 PLAN *plan; 1006 FTSENT *entry; 1007{ 1008 return (entry->fts_statp->st_uid == plan->u_data); 1009} 1010 1011PLAN * 1012c_user(username) 1013 char *username; 1014{ 1015 PLAN *new; 1016 struct passwd *p; 1017 uid_t uid; 1018 1019 ftsoptions &= ~FTS_NOSTAT; 1020 1021 p = getpwnam(username); 1022 if (p == NULL) { 1023 uid = atoi(username); 1024 if (uid == 0 && username[0] != '0') 1025 errx(1, "-user: %s: no such user", username); 1026 } else 1027 uid = p->pw_uid; 1028 1029 new = palloc(N_USER, f_user); 1030 new->u_data = uid; 1031 return (new); 1032} 1033 1034/* 1035 * -xdev functions -- 1036 * 1037 * Always true, causes find not to decend past directories that have a 1038 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 1039 */ 1040PLAN * 1041c_xdev() 1042{ 1043 ftsoptions |= FTS_XDEV; 1044 1045 return (palloc(N_XDEV, f_always_true)); 1046} 1047 1048/* 1049 * ( expression ) functions -- 1050 * 1051 * True if expression is true. 1052 */ 1053int 1054f_expr(plan, entry) 1055 PLAN *plan; 1056 FTSENT *entry; 1057{ 1058 register PLAN *p; 1059 register int state; 1060 1061 for (p = plan->p_data[0]; 1062 p && (state = (p->eval)(p, entry)); p = p->next); 1063 return (state); 1064} 1065 1066/* 1067 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are 1068 * eliminated during phase 2 of find_formplan() --- the '(' node is converted 1069 * to a N_EXPR node containing the expression and the ')' node is discarded. 1070 */ 1071PLAN * 1072c_openparen() 1073{ 1074 return (palloc(N_OPENPAREN, (int (*)())-1)); 1075} 1076 1077PLAN * 1078c_closeparen() 1079{ 1080 return (palloc(N_CLOSEPAREN, (int (*)())-1)); 1081} 1082 1083/* 1084 * ! expression functions -- 1085 * 1086 * Negation of a primary; the unary NOT operator. 1087 */ 1088int 1089f_not(plan, entry) 1090 PLAN *plan; 1091 FTSENT *entry; 1092{ 1093 register PLAN *p; 1094 register int state; 1095 1096 for (p = plan->p_data[0]; 1097 p && (state = (p->eval)(p, entry)); p = p->next); 1098 return (!state); 1099} 1100 1101PLAN * 1102c_not() 1103{ 1104 return (palloc(N_NOT, f_not)); 1105} 1106 1107/* 1108 * expression -o expression functions -- 1109 * 1110 * Alternation of primaries; the OR operator. The second expression is 1111 * not evaluated if the first expression is true. 1112 */ 1113int 1114f_or(plan, entry) 1115 PLAN *plan; 1116 FTSENT *entry; 1117{ 1118 register PLAN *p; 1119 register int state; 1120 1121 for (p = plan->p_data[0]; 1122 p && (state = (p->eval)(p, entry)); p = p->next); 1123 1124 if (state) 1125 return (1); 1126 1127 for (p = plan->p_data[1]; 1128 p && (state = (p->eval)(p, entry)); p = p->next); 1129 return (state); 1130} 1131 1132PLAN * 1133c_or() 1134{ 1135 return (palloc(N_OR, f_or)); 1136} 1137 1138static PLAN * 1139palloc(t, f) 1140 enum ntype t; 1141 int (*f) __P((PLAN *, FTSENT *)); 1142{ 1143 PLAN *new; 1144 1145 if ((new = malloc(sizeof(PLAN))) == NULL) 1146 err(1, NULL); 1147 new->type = t; 1148 new->eval = f; 1149 new->flags = 0; 1150 new->next = NULL; 1151 return (new); 1152} 1153