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