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