Home | History | Annotate | Line # | Download | only in find
function.c revision 1.14
      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.14 1994/10/18 13:54:25 mycroft 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 	static char fstype[MFSNAMELEN+1];
    349 	char *p, save[2];
    350 
    351 	/* Only check when we cross mount point. */
    352 	if (first || curdev != entry->fts_statp->st_dev) {
    353 		curdev = entry->fts_statp->st_dev;
    354 
    355 		/*
    356 		 * Statfs follows symlinks; find wants the link's file system,
    357 		 * not where it points.
    358 		 */
    359 		if (entry->fts_info == FTS_SL ||
    360 		    entry->fts_info == FTS_SLNONE) {
    361 			if (p = strrchr(entry->fts_accpath, '/'))
    362 				++p;
    363 			else
    364 				p = entry->fts_accpath;
    365 			save[0] = p[0];
    366 			p[0] = '.';
    367 			save[1] = p[1];
    368 			p[1] = '\0';
    369 
    370 		} else
    371 			p = NULL;
    372 
    373 		if (statfs(entry->fts_accpath, &sb))
    374 			err(1, "%s", entry->fts_accpath);
    375 
    376 		if (p) {
    377 			p[0] = save[0];
    378 			p[1] = save[1];
    379 		}
    380 
    381 		first = 0;
    382 		switch (plan->flags) {
    383 		case F_MTFLAG:
    384 		case F_MTTYPE:
    385 			/*
    386 			 * Further tests may need both of these values, so
    387 			 * always copy both of them.
    388 			 */
    389 			val = sb.f_flags;
    390 			strncpy(fstype, sb.f_fstypename, MFSNAMELEN);
    391 			fstype[MFSNAMELEN] = '\0';
    392 			break;
    393 		default:
    394 			abort();
    395 		}
    396 	}
    397 	switch (plan->flags) {
    398 	case F_MTFLAG:
    399 		return (val & plan->mt_data);
    400 	case F_MTTYPE:
    401 		return (strncmp(fstype, plan->c_data, MFSNAMELEN) == 0);
    402 	default:
    403 		abort();
    404 	}
    405 }
    406 
    407 PLAN *
    408 c_fstype(arg)
    409 	char *arg;
    410 {
    411 	register PLAN *new;
    412 
    413 	ftsoptions &= ~FTS_NOSTAT;
    414 
    415 	new = palloc(N_FSTYPE, f_fstype);
    416 	switch (*arg) {
    417 	case 'l':
    418 		if (!strcmp(arg, "local")) {
    419 			new->flags = F_MTFLAG;
    420 			new->mt_data = MNT_LOCAL;
    421 			return (new);
    422 		}
    423 		break;
    424 	case 'r':
    425 		if (!strcmp(arg, "rdonly")) {
    426 			new->flags = F_MTFLAG;
    427 			new->mt_data = MNT_RDONLY;
    428 			return (new);
    429 		}
    430 		break;
    431 	}
    432 
    433 	new->flags = F_MTTYPE;
    434 	new->c_data = arg;
    435 	return (new);
    436 }
    437 
    438 /*
    439  * -group gname functions --
    440  *
    441  *	True if the file belongs to the group gname.  If gname is numeric and
    442  *	an equivalent of the getgrnam() function does not return a valid group
    443  *	name, gname is taken as a group ID.
    444  */
    445 int
    446 f_group(plan, entry)
    447 	PLAN *plan;
    448 	FTSENT *entry;
    449 {
    450 	return (entry->fts_statp->st_gid == plan->g_data);
    451 }
    452 
    453 PLAN *
    454 c_group(gname)
    455 	char *gname;
    456 {
    457 	PLAN *new;
    458 	struct group *g;
    459 	gid_t gid;
    460 
    461 	ftsoptions &= ~FTS_NOSTAT;
    462 
    463 	g = getgrnam(gname);
    464 	if (g == NULL) {
    465 		gid = atoi(gname);
    466 		if (gid == 0 && gname[0] != '0')
    467 			errx(1, "-group: %s: no such group", gname);
    468 	} else
    469 		gid = g->gr_gid;
    470 
    471 	new = palloc(N_GROUP, f_group);
    472 	new->g_data = gid;
    473 	return (new);
    474 }
    475 
    476 /*
    477  * -inum n functions --
    478  *
    479  *	True if the file has inode # n.
    480  */
    481 int
    482 f_inum(plan, entry)
    483 	PLAN *plan;
    484 	FTSENT *entry;
    485 {
    486 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
    487 }
    488 
    489 PLAN *
    490 c_inum(arg)
    491 	char *arg;
    492 {
    493 	PLAN *new;
    494 
    495 	ftsoptions &= ~FTS_NOSTAT;
    496 
    497 	new = palloc(N_INUM, f_inum);
    498 	new->i_data = find_parsenum(new, "-inum", arg, NULL);
    499 	return (new);
    500 }
    501 
    502 /*
    503  * -links n functions --
    504  *
    505  *	True if the file has n links.
    506  */
    507 int
    508 f_links(plan, entry)
    509 	PLAN *plan;
    510 	FTSENT *entry;
    511 {
    512 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
    513 }
    514 
    515 PLAN *
    516 c_links(arg)
    517 	char *arg;
    518 {
    519 	PLAN *new;
    520 
    521 	ftsoptions &= ~FTS_NOSTAT;
    522 
    523 	new = palloc(N_LINKS, f_links);
    524 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
    525 	return (new);
    526 }
    527 
    528 /*
    529  * -ls functions --
    530  *
    531  *	Always true - prints the current entry to stdout in "ls" format.
    532  */
    533 int
    534 f_ls(plan, entry)
    535 	PLAN *plan;
    536 	FTSENT *entry;
    537 {
    538 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
    539 	return (1);
    540 }
    541 
    542 PLAN *
    543 c_ls()
    544 {
    545 	ftsoptions &= ~FTS_NOSTAT;
    546 	isoutput = 1;
    547 
    548 	return (palloc(N_LS, f_ls));
    549 }
    550 
    551 /*
    552  * -mtime n functions --
    553  *
    554  *	True if the difference between the file modification time and the
    555  *	current time is n 24 hour periods.
    556  */
    557 int
    558 f_mtime(plan, entry)
    559 	PLAN *plan;
    560 	FTSENT *entry;
    561 {
    562 	extern time_t now;
    563 
    564 	COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
    565 	    SECSPERDAY, plan->t_data);
    566 }
    567 
    568 PLAN *
    569 c_mtime(arg)
    570 	char *arg;
    571 {
    572 	PLAN *new;
    573 
    574 	ftsoptions &= ~FTS_NOSTAT;
    575 
    576 	new = palloc(N_MTIME, f_mtime);
    577 	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
    578 	TIME_CORRECT(new, N_MTIME);
    579 	return (new);
    580 }
    581 
    582 /*
    583  * -name functions --
    584  *
    585  *	True if the basename of the filename being examined
    586  *	matches pattern using Pattern Matching Notation S3.14
    587  */
    588 int
    589 f_name(plan, entry)
    590 	PLAN *plan;
    591 	FTSENT *entry;
    592 {
    593 	return (!fnmatch(plan->c_data, entry->fts_name, 0));
    594 }
    595 
    596 PLAN *
    597 c_name(pattern)
    598 	char *pattern;
    599 {
    600 	PLAN *new;
    601 
    602 	new = palloc(N_NAME, f_name);
    603 	new->c_data = pattern;
    604 	return (new);
    605 }
    606 
    607 /*
    608  * -newer file functions --
    609  *
    610  *	True if the current file has been modified more recently
    611  *	then the modification time of the file named by the pathname
    612  *	file.
    613  */
    614 int
    615 f_newer(plan, entry)
    616 	PLAN *plan;
    617 	FTSENT *entry;
    618 {
    619 	return (entry->fts_statp->st_mtime > plan->t_data);
    620 }
    621 
    622 PLAN *
    623 c_newer(filename)
    624 	char *filename;
    625 {
    626 	PLAN *new;
    627 	struct stat sb;
    628 
    629 	ftsoptions &= ~FTS_NOSTAT;
    630 
    631 	if (stat(filename, &sb))
    632 		err(1, "%s", filename);
    633 	new = palloc(N_NEWER, f_newer);
    634 	new->t_data = sb.st_mtime;
    635 	return (new);
    636 }
    637 
    638 /*
    639  * -nogroup functions --
    640  *
    641  *	True if file belongs to a user ID for which the equivalent
    642  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
    643  */
    644 int
    645 f_nogroup(plan, entry)
    646 	PLAN *plan;
    647 	FTSENT *entry;
    648 {
    649 	char *group_from_gid();
    650 
    651 	return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
    652 }
    653 
    654 PLAN *
    655 c_nogroup()
    656 {
    657 	ftsoptions &= ~FTS_NOSTAT;
    658 
    659 	return (palloc(N_NOGROUP, f_nogroup));
    660 }
    661 
    662 /*
    663  * -nouser functions --
    664  *
    665  *	True if file belongs to a user ID for which the equivalent
    666  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
    667  */
    668 int
    669 f_nouser(plan, entry)
    670 	PLAN *plan;
    671 	FTSENT *entry;
    672 {
    673 	char *user_from_uid();
    674 
    675 	return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
    676 }
    677 
    678 PLAN *
    679 c_nouser()
    680 {
    681 	ftsoptions &= ~FTS_NOSTAT;
    682 
    683 	return (palloc(N_NOUSER, f_nouser));
    684 }
    685 
    686 /*
    687  * -path functions --
    688  *
    689  *	True if the path of the filename being examined
    690  *	matches pattern using Pattern Matching Notation S3.14
    691  */
    692 int
    693 f_path(plan, entry)
    694 	PLAN *plan;
    695 	FTSENT *entry;
    696 {
    697 	return (!fnmatch(plan->c_data, entry->fts_path, 0));
    698 }
    699 
    700 PLAN *
    701 c_path(pattern)
    702 	char *pattern;
    703 {
    704 	PLAN *new;
    705 
    706 	new = palloc(N_NAME, f_path);
    707 	new->c_data = pattern;
    708 	return (new);
    709 }
    710 
    711 /*
    712  * -perm functions --
    713  *
    714  *	The mode argument is used to represent file mode bits.  If it starts
    715  *	with a leading digit, it's treated as an octal mode, otherwise as a
    716  *	symbolic mode.
    717  */
    718 int
    719 f_perm(plan, entry)
    720 	PLAN *plan;
    721 	FTSENT *entry;
    722 {
    723 	mode_t mode;
    724 
    725 	mode = entry->fts_statp->st_mode &
    726 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
    727 	if (plan->flags == F_ATLEAST)
    728 		return ((plan->m_data | mode) == mode);
    729 	else
    730 		return (mode == plan->m_data);
    731 	/* NOTREACHED */
    732 }
    733 
    734 PLAN *
    735 c_perm(perm)
    736 	char *perm;
    737 {
    738 	PLAN *new;
    739 	mode_t *set;
    740 
    741 	ftsoptions &= ~FTS_NOSTAT;
    742 
    743 	new = palloc(N_PERM, f_perm);
    744 
    745 	if (*perm == '-') {
    746 		new->flags = F_ATLEAST;
    747 		++perm;
    748 	}
    749 
    750 	if ((set = setmode(perm)) == NULL)
    751 		err(1, "-perm: %s: illegal mode string", perm);
    752 
    753 	new->m_data = getmode(set, 0);
    754 	return (new);
    755 }
    756 
    757 /*
    758  * -print functions --
    759  *
    760  *	Always true, causes the current pathame to be written to
    761  *	standard output.
    762  */
    763 int
    764 f_print(plan, entry)
    765 	PLAN *plan;
    766 	FTSENT *entry;
    767 {
    768 	(void)printf("%s\n", entry->fts_path);
    769 	return(1);
    770 }
    771 
    772 /* ARGSUSED */
    773 f_print0(plan, entry)
    774 	PLAN *plan;
    775 	FTSENT *entry;
    776 {
    777 	(void)fputs(entry->fts_path, stdout);
    778 	(void)fputc('\0', stdout);
    779 	return(1);
    780 }
    781 
    782 PLAN *
    783 c_print()
    784 {
    785 	isoutput = 1;
    786 
    787 	return(palloc(N_PRINT, f_print));
    788 }
    789 
    790 PLAN *
    791 c_print0()
    792 {
    793 	isoutput = 1;
    794 
    795 	return(palloc(N_PRINT0, f_print0));
    796 }
    797 
    798 /*
    799  * -prune functions --
    800  *
    801  *	Prune a portion of the hierarchy.
    802  */
    803 int
    804 f_prune(plan, entry)
    805 	PLAN *plan;
    806 	FTSENT *entry;
    807 {
    808 	extern FTS *tree;
    809 
    810 	if (fts_set(tree, entry, FTS_SKIP))
    811 		err(1, "%s", entry->fts_path);
    812 	return (1);
    813 }
    814 
    815 PLAN *
    816 c_prune()
    817 {
    818 	return (palloc(N_PRUNE, f_prune));
    819 }
    820 
    821 /*
    822  * -size n[c] functions --
    823  *
    824  *	True if the file size in bytes, divided by an implementation defined
    825  *	value and rounded up to the next integer, is n.  If n is followed by
    826  *	a c, the size is in bytes.
    827  */
    828 #define	FIND_SIZE	512
    829 static int divsize = 1;
    830 
    831 int
    832 f_size(plan, entry)
    833 	PLAN *plan;
    834 	FTSENT *entry;
    835 {
    836 	off_t size;
    837 
    838 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
    839 	    FIND_SIZE : entry->fts_statp->st_size;
    840 	COMPARE(size, plan->o_data);
    841 }
    842 
    843 PLAN *
    844 c_size(arg)
    845 	char *arg;
    846 {
    847 	PLAN *new;
    848 	char endch;
    849 
    850 	ftsoptions &= ~FTS_NOSTAT;
    851 
    852 	new = palloc(N_SIZE, f_size);
    853 	endch = 'c';
    854 	new->o_data = find_parsenum(new, "-size", arg, &endch);
    855 	if (endch == 'c')
    856 		divsize = 0;
    857 	return (new);
    858 }
    859 
    860 /*
    861  * -type c functions --
    862  *
    863  *	True if the type of the file is c, where c is b, c, d, p, or f for
    864  *	block special file, character special file, directory, FIFO, or
    865  *	regular file, respectively.
    866  */
    867 int
    868 f_type(plan, entry)
    869 	PLAN *plan;
    870 	FTSENT *entry;
    871 {
    872 	return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
    873 }
    874 
    875 PLAN *
    876 c_type(typestring)
    877 	char *typestring;
    878 {
    879 	PLAN *new;
    880 	mode_t  mask;
    881 
    882 	ftsoptions &= ~FTS_NOSTAT;
    883 
    884 	switch (typestring[0]) {
    885 	case 'b':
    886 		mask = S_IFBLK;
    887 		break;
    888 	case 'c':
    889 		mask = S_IFCHR;
    890 		break;
    891 	case 'd':
    892 		mask = S_IFDIR;
    893 		break;
    894 	case 'f':
    895 		mask = S_IFREG;
    896 		break;
    897 	case 'l':
    898 		mask = S_IFLNK;
    899 		break;
    900 	case 'p':
    901 		mask = S_IFIFO;
    902 		break;
    903 	case 's':
    904 		mask = S_IFSOCK;
    905 		break;
    906 	default:
    907 		errx(1, "-type: %s: unknown type", typestring);
    908 	}
    909 
    910 	new = palloc(N_TYPE, f_type);
    911 	new->m_data = mask;
    912 	return (new);
    913 }
    914 
    915 /*
    916  * -user uname functions --
    917  *
    918  *	True if the file belongs to the user uname.  If uname is numeric and
    919  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
    920  *	return a valid user name, uname is taken as a user ID.
    921  */
    922 int
    923 f_user(plan, entry)
    924 	PLAN *plan;
    925 	FTSENT *entry;
    926 {
    927 	return (entry->fts_statp->st_uid == plan->u_data);
    928 }
    929 
    930 PLAN *
    931 c_user(username)
    932 	char *username;
    933 {
    934 	PLAN *new;
    935 	struct passwd *p;
    936 	uid_t uid;
    937 
    938 	ftsoptions &= ~FTS_NOSTAT;
    939 
    940 	p = getpwnam(username);
    941 	if (p == NULL) {
    942 		uid = atoi(username);
    943 		if (uid == 0 && username[0] != '0')
    944 			errx(1, "-user: %s: no such user", username);
    945 	} else
    946 		uid = p->pw_uid;
    947 
    948 	new = palloc(N_USER, f_user);
    949 	new->u_data = uid;
    950 	return (new);
    951 }
    952 
    953 /*
    954  * -xdev functions --
    955  *
    956  *	Always true, causes find not to decend past directories that have a
    957  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
    958  */
    959 PLAN *
    960 c_xdev()
    961 {
    962 	ftsoptions |= FTS_XDEV;
    963 
    964 	return (palloc(N_XDEV, f_always_true));
    965 }
    966 
    967 /*
    968  * ( expression ) functions --
    969  *
    970  *	True if expression is true.
    971  */
    972 int
    973 f_expr(plan, entry)
    974 	PLAN *plan;
    975 	FTSENT *entry;
    976 {
    977 	register PLAN *p;
    978 	register int state;
    979 
    980 	for (p = plan->p_data[0];
    981 	    p && (state = (p->eval)(p, entry)); p = p->next);
    982 	return (state);
    983 }
    984 
    985 /*
    986  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
    987  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
    988  * to a N_EXPR node containing the expression and the ')' node is discarded.
    989  */
    990 PLAN *
    991 c_openparen()
    992 {
    993 	return (palloc(N_OPENPAREN, (int (*)())-1));
    994 }
    995 
    996 PLAN *
    997 c_closeparen()
    998 {
    999 	return (palloc(N_CLOSEPAREN, (int (*)())-1));
   1000 }
   1001 
   1002 /*
   1003  * ! expression functions --
   1004  *
   1005  *	Negation of a primary; the unary NOT operator.
   1006  */
   1007 int
   1008 f_not(plan, entry)
   1009 	PLAN *plan;
   1010 	FTSENT *entry;
   1011 {
   1012 	register PLAN *p;
   1013 	register int state;
   1014 
   1015 	for (p = plan->p_data[0];
   1016 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1017 	return (!state);
   1018 }
   1019 
   1020 PLAN *
   1021 c_not()
   1022 {
   1023 	return (palloc(N_NOT, f_not));
   1024 }
   1025 
   1026 /*
   1027  * expression -o expression functions --
   1028  *
   1029  *	Alternation of primaries; the OR operator.  The second expression is
   1030  * not evaluated if the first expression is true.
   1031  */
   1032 int
   1033 f_or(plan, entry)
   1034 	PLAN *plan;
   1035 	FTSENT *entry;
   1036 {
   1037 	register PLAN *p;
   1038 	register int state;
   1039 
   1040 	for (p = plan->p_data[0];
   1041 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1042 
   1043 	if (state)
   1044 		return (1);
   1045 
   1046 	for (p = plan->p_data[1];
   1047 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1048 	return (state);
   1049 }
   1050 
   1051 PLAN *
   1052 c_or()
   1053 {
   1054 	return (palloc(N_OR, f_or));
   1055 }
   1056 
   1057 static PLAN *
   1058 palloc(t, f)
   1059 	enum ntype t;
   1060 	int (*f) __P((PLAN *, FTSENT *));
   1061 {
   1062 	PLAN *new;
   1063 
   1064 	if (new = malloc(sizeof(PLAN))) {
   1065 		new->type = t;
   1066 		new->eval = f;
   1067 		new->flags = 0;
   1068 		new->next = NULL;
   1069 		return (new);
   1070 	}
   1071 	err(1, NULL);
   1072 	/* NOTREACHED */
   1073 }
   1074