Home | History | Annotate | Line # | Download | only in find
function.c revision 1.66
      1 /*	$NetBSD: function.c,v 1.66 2011/02/24 02:55:18 jmcneill 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "from: @(#)function.c	8.10 (Berkeley) 5/4/95";
     39 #else
     40 __RCSID("$NetBSD: function.c,v 1.66 2011/02/24 02:55:18 jmcneill Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/stat.h>
     46 #include <sys/wait.h>
     47 #include <sys/mount.h>
     48 
     49 #include <dirent.h>
     50 #include <err.h>
     51 #include <errno.h>
     52 #include <fnmatch.h>
     53 #include <fts.h>
     54 #include <grp.h>
     55 #include <inttypes.h>
     56 #include <limits.h>
     57 #include <pwd.h>
     58 #include <stdbool.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <tzfile.h>
     63 #include <unistd.h>
     64 #include <util.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	int64_t	find_parsenum(PLAN *, const char *, const char *, char *);
     82 static	void	run_f_exec(PLAN *);
     83 	int	f_always_true(PLAN *, FTSENT *);
     84 	int	f_amin(PLAN *, FTSENT *);
     85 	int	f_anewer(PLAN *, FTSENT *);
     86 	int	f_atime(PLAN *, FTSENT *);
     87 	int	f_cmin(PLAN *, FTSENT *);
     88 	int	f_cnewer(PLAN *, FTSENT *);
     89 	int	f_ctime(PLAN *, FTSENT *);
     90 	int	f_delete(PLAN *, FTSENT *);
     91 	int	f_empty(PLAN *, FTSENT *);
     92 	int	f_exec(PLAN *, FTSENT *);
     93 	int	f_execdir(PLAN *, FTSENT *);
     94 	int	f_false(PLAN *, FTSENT *);
     95 	int	f_flags(PLAN *, FTSENT *);
     96 	int	f_fprint(PLAN *, FTSENT *);
     97 	int	f_fstype(PLAN *, FTSENT *);
     98 	int	f_group(PLAN *, FTSENT *);
     99 	int	f_iname(PLAN *, FTSENT *);
    100 	int	f_inum(PLAN *, FTSENT *);
    101 	int	f_links(PLAN *, FTSENT *);
    102 	int	f_ls(PLAN *, FTSENT *);
    103 	int	f_mindepth(PLAN *, FTSENT *);
    104 	int	f_maxdepth(PLAN *, FTSENT *);
    105 	int	f_mmin(PLAN *, FTSENT *);
    106 	int	f_mtime(PLAN *, FTSENT *);
    107 	int	f_name(PLAN *, FTSENT *);
    108 	int	f_newer(PLAN *, FTSENT *);
    109 	int	f_nogroup(PLAN *, FTSENT *);
    110 	int	f_nouser(PLAN *, FTSENT *);
    111 	int	f_path(PLAN *, FTSENT *);
    112 	int	f_perm(PLAN *, FTSENT *);
    113 	int	f_print(PLAN *, FTSENT *);
    114 	int	f_print0(PLAN *, FTSENT *);
    115 	int	f_printx(PLAN *, FTSENT *);
    116 	int	f_prune(PLAN *, FTSENT *);
    117 	int	f_regex(PLAN *, FTSENT *);
    118 	int	f_size(PLAN *, FTSENT *);
    119 	int	f_type(PLAN *, FTSENT *);
    120 	int	f_user(PLAN *, FTSENT *);
    121 	int	f_not(PLAN *, FTSENT *);
    122 	int	f_or(PLAN *, FTSENT *);
    123 static	PLAN   *c_regex_common(char ***, int, enum ntype, bool);
    124 static	PLAN   *palloc(enum ntype, int (*)(PLAN *, FTSENT *));
    125 
    126 extern int dotfd;
    127 extern FTS *tree;
    128 extern time_t now;
    129 
    130 /*
    131  * find_parsenum --
    132  *	Parse a string of the form [+-]# and return the value.
    133  */
    134 static int64_t
    135 find_parsenum(PLAN *plan, const char *option, const char *vp, char *endch)
    136 {
    137 	int64_t value;
    138 	const char *str;
    139 	char *endchar; /* Pointer to character ending conversion. */
    140 
    141 	/* Determine comparison from leading + or -. */
    142 	str = vp;
    143 	switch (*str) {
    144 	case '+':
    145 		++str;
    146 		plan->flags = F_GREATER;
    147 		break;
    148 	case '-':
    149 		++str;
    150 		plan->flags = F_LESSTHAN;
    151 		break;
    152 	default:
    153 		plan->flags = F_EQUAL;
    154 		break;
    155 	}
    156 
    157 	/*
    158 	 * Convert the string with strtol().  Note, if strtol() returns zero
    159 	 * and endchar points to the beginning of the string we know we have
    160 	 * a syntax error.
    161 	 */
    162 	value = strtoq(str, &endchar, 10);
    163 	if (value == 0 && endchar == str)
    164 		errx(1, "%s: %s: illegal numeric value", option, vp);
    165 	if (endchar[0] && (endch == NULL || endchar[0] != *endch))
    166 		errx(1, "%s: %s: illegal trailing character", option, vp);
    167 	if (endch)
    168 		*endch = endchar[0];
    169 	return (value);
    170 }
    171 
    172 /*
    173  * The value of n for the inode times (atime, ctime, and mtime) is a range,
    174  * i.e. n matches from (n - 1) to n 24 hour periods.  This interacts with
    175  * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
    176  * user wanted.  Correct so that -1 is "less than 1".
    177  */
    178 #define	TIME_CORRECT(p, ttype)						\
    179 	if ((p)->type == ttype && (p)->flags == F_LESSTHAN)		\
    180 		++((p)->t_data);
    181 
    182 /*
    183  * -amin n functions --
    184  *
    185  *	True if the difference between the file access time and the
    186  *	current time is n 1 minute periods.
    187  */
    188 int
    189 f_amin(PLAN *plan, FTSENT *entry)
    190 {
    191 	COMPARE((now - entry->fts_statp->st_atime +
    192 	    SECSPERMIN - 1) / SECSPERMIN, plan->t_data);
    193 }
    194 
    195 PLAN *
    196 c_amin(char ***argvp, int isok)
    197 {
    198 	char *arg = **argvp;
    199 	PLAN *new;
    200 
    201 	(*argvp)++;
    202 	ftsoptions &= ~FTS_NOSTAT;
    203 
    204 	new = palloc(N_AMIN, f_amin);
    205 	new->t_data = find_parsenum(new, "-amin", arg, NULL);
    206 	TIME_CORRECT(new, N_AMIN);
    207 	return (new);
    208 }
    209 
    210 /*
    211  * -anewer file functions --
    212  *
    213  *	True if the current file has been accessed more recently
    214  *	than the access time of the file named by the pathname
    215  *	file.
    216  */
    217 int
    218 f_anewer(plan, entry)
    219 	PLAN *plan;
    220 	FTSENT *entry;
    221 {
    222 
    223 	return (entry->fts_statp->st_atime > plan->t_data);
    224 }
    225 
    226 PLAN *
    227 c_anewer(char ***argvp, int isok)
    228 {
    229 	char *filename = **argvp;
    230 	PLAN *new;
    231 	struct stat sb;
    232 
    233 	(*argvp)++;
    234 	ftsoptions &= ~FTS_NOSTAT;
    235 
    236 	if (stat(filename, &sb))
    237 		err(1, "%s", filename);
    238 	new = palloc(N_ANEWER, f_anewer);
    239 	new->t_data = sb.st_atime;
    240 	return (new);
    241 }
    242 
    243 /*
    244  * -atime n functions --
    245  *
    246  *	True if the difference between the file access time and the
    247  *	current time is n 24 hour periods.
    248  */
    249 int
    250 f_atime(PLAN *plan, FTSENT *entry)
    251 {
    252 	COMPARE((now - entry->fts_statp->st_atime +
    253 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
    254 }
    255 
    256 PLAN *
    257 c_atime(char ***argvp, int isok)
    258 {
    259 	char *arg = **argvp;
    260 	PLAN *new;
    261 
    262 	(*argvp)++;
    263 	ftsoptions &= ~FTS_NOSTAT;
    264 
    265 	new = palloc(N_ATIME, f_atime);
    266 	new->t_data = find_parsenum(new, "-atime", arg, NULL);
    267 	TIME_CORRECT(new, N_ATIME);
    268 	return (new);
    269 }
    270 /*
    271  * -cmin n functions --
    272  *
    273  *	True if the difference between the last change of file
    274  *	status information and the current time is n 24 hour periods.
    275  */
    276 int
    277 f_cmin(PLAN *plan, FTSENT *entry)
    278 {
    279 	COMPARE((now - entry->fts_statp->st_ctime +
    280 	    SECSPERMIN - 1) / SECSPERMIN, plan->t_data);
    281 }
    282 
    283 PLAN *
    284 c_cmin(char ***argvp, int isok)
    285 {
    286 	char *arg = **argvp;
    287 	PLAN *new;
    288 
    289 	(*argvp)++;
    290 	ftsoptions &= ~FTS_NOSTAT;
    291 
    292 	new = palloc(N_CMIN, f_cmin);
    293 	new->t_data = find_parsenum(new, "-cmin", arg, NULL);
    294 	TIME_CORRECT(new, N_CMIN);
    295 	return (new);
    296 }
    297 
    298 /*
    299  * -cnewer file functions --
    300  *
    301  *	True if the current file has been changed more recently
    302  *	than the changed time of the file named by the pathname
    303  *	file.
    304  */
    305 int
    306 f_cnewer(PLAN *plan, FTSENT *entry)
    307 {
    308 
    309 	return (entry->fts_statp->st_ctime > plan->t_data);
    310 }
    311 
    312 PLAN *
    313 c_cnewer(char ***argvp, int isok)
    314 {
    315 	char *filename = **argvp;
    316 	PLAN *new;
    317 	struct stat sb;
    318 
    319 	(*argvp)++;
    320 	ftsoptions &= ~FTS_NOSTAT;
    321 
    322 	if (stat(filename, &sb))
    323 		err(1, "%s", filename);
    324 	new = palloc(N_CNEWER, f_cnewer);
    325 	new->t_data = sb.st_ctime;
    326 	return (new);
    327 }
    328 
    329 /*
    330  * -ctime n functions --
    331  *
    332  *	True if the difference between the last change of file
    333  *	status information and the current time is n 24 hour periods.
    334  */
    335 int
    336 f_ctime(PLAN *plan, FTSENT *entry)
    337 {
    338 	COMPARE((now - entry->fts_statp->st_ctime +
    339 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
    340 }
    341 
    342 PLAN *
    343 c_ctime(char ***argvp, int isok)
    344 {
    345 	char *arg = **argvp;
    346 	PLAN *new;
    347 
    348 	(*argvp)++;
    349 	ftsoptions &= ~FTS_NOSTAT;
    350 
    351 	new = palloc(N_CTIME, f_ctime);
    352 	new->t_data = find_parsenum(new, "-ctime", arg, NULL);
    353 	TIME_CORRECT(new, N_CTIME);
    354 	return (new);
    355 }
    356 
    357 /*
    358  * -delete functions --
    359  *
    360  *	True always.  Makes its best shot and continues on regardless.
    361  */
    362 int
    363 f_delete(PLAN *plan __unused, FTSENT *entry)
    364 {
    365 	/* ignore these from fts */
    366 	if (strcmp(entry->fts_accpath, ".") == 0 ||
    367 	    strcmp(entry->fts_accpath, "..") == 0)
    368 		return 1;
    369 
    370 	/* sanity check */
    371 	if (isdepth == 0 ||			/* depth off */
    372 	    (ftsoptions & FTS_NOSTAT) ||	/* not stat()ing */
    373 	    !(ftsoptions & FTS_PHYSICAL) ||	/* physical off */
    374 	    (ftsoptions & FTS_LOGICAL))		/* or finally, logical on */
    375 		errx(1, "-delete: insecure options got turned on");
    376 
    377 	/* Potentially unsafe - do not accept relative paths whatsoever */
    378 	if (strchr(entry->fts_accpath, '/') != NULL)
    379 		errx(1, "-delete: %s: relative path potentially not safe",
    380 			entry->fts_accpath);
    381 
    382 	/* Turn off user immutable bits if running as root */
    383 	if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
    384 	    !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
    385 	    geteuid() == 0)
    386 		chflags(entry->fts_accpath,
    387 		       entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
    388 
    389 	/* rmdir directories, unlink everything else */
    390 	if (S_ISDIR(entry->fts_statp->st_mode)) {
    391 		if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
    392 			warn("-delete: rmdir(%s)", entry->fts_path);
    393 	} else {
    394 		if (unlink(entry->fts_accpath) < 0)
    395 			warn("-delete: unlink(%s)", entry->fts_path);
    396 	}
    397 
    398 	/* "succeed" */
    399 	return 1;
    400 }
    401 
    402 PLAN *
    403 c_delete(char ***argvp __unused, int isok)
    404 {
    405 
    406 	ftsoptions &= ~FTS_NOSTAT;	/* no optimize */
    407 	ftsoptions |= FTS_PHYSICAL;	/* disable -follow */
    408 	ftsoptions &= ~FTS_LOGICAL;	/* disable -follow */
    409 	isoutput = 1;			/* possible output */
    410 	isdepth = 1;			/* -depth implied */
    411 
    412 	return palloc(N_DELETE, f_delete);
    413 }
    414 
    415 /*
    416  * -depth functions --
    417  *
    418  *	Always true, causes descent of the directory hierarchy to be done
    419  *	so that all entries in a directory are acted on before the directory
    420  *	itself.
    421  */
    422 int
    423 f_always_true(PLAN *plan, FTSENT *entry)
    424 {
    425 
    426 	return (1);
    427 }
    428 
    429 PLAN *
    430 c_depth(char ***argvp, int isok)
    431 {
    432 	isdepth = 1;
    433 
    434 	return (palloc(N_DEPTH, f_always_true));
    435 }
    436 
    437 /*
    438  * -empty functions --
    439  *
    440  *	True if the file or directory is empty
    441  */
    442 int
    443 f_empty(PLAN *plan, FTSENT *entry)
    444 {
    445 	if (S_ISREG(entry->fts_statp->st_mode) &&
    446 	    entry->fts_statp->st_size == 0)
    447 		return (1);
    448 	if (S_ISDIR(entry->fts_statp->st_mode)) {
    449 		struct dirent *dp;
    450 		int empty;
    451 		DIR *dir;
    452 
    453 		empty = 1;
    454 		dir = opendir(entry->fts_accpath);
    455 		if (dir == NULL)
    456 			return (0);
    457 		for (dp = readdir(dir); dp; dp = readdir(dir))
    458 			if (dp->d_name[0] != '.' ||
    459 			    (dp->d_name[1] != '\0' &&
    460 				(dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
    461 				empty = 0;
    462 				break;
    463 			}
    464 		closedir(dir);
    465 		return (empty);
    466 	}
    467 	return (0);
    468 }
    469 
    470 PLAN *
    471 c_empty(char ***argvp, int isok)
    472 {
    473 	ftsoptions &= ~FTS_NOSTAT;
    474 
    475 	return (palloc(N_EMPTY, f_empty));
    476 }
    477 
    478 /*
    479  * [-exec | -ok] utility [arg ... ] ; functions --
    480  * [-exec | -ok] utility [arg ... ] {} + functions --
    481  *
    482  *	If the end of the primary expression is delimited by a
    483  *	semicolon: true if the executed utility returns a zero value
    484  *	as exit status.  If "{}" occurs anywhere, it gets replaced by
    485  *	the current pathname.
    486  *
    487  *	If the end of the primary expression is delimited by a plus
    488  *	sign: always true. Pathnames for which the primary is
    489  *	evaluated shall be aggregated into sets. The utility will be
    490  *	executed once per set, with "{}" replaced by the entire set of
    491  *	pathnames (as if xargs). "{}" must appear last.
    492  *
    493  *	The current directory for the execution of utility is the same
    494  *	as the current directory when the find utility was started.
    495  *
    496  *	The primary -ok is different in that it requests affirmation
    497  *	of the user before executing the utility.
    498  */
    499 int
    500 f_exec(PLAN *plan, FTSENT *entry)
    501 {
    502 	int cnt, l;
    503 	pid_t pid;
    504 	int status;
    505 
    506 	if (plan->flags & F_PLUSSET) {
    507 		/*
    508 		 * Confirm sufficient buffer space, then copy the path
    509 		 * to the buffer.
    510 		 */
    511 		l = strlen(entry->fts_path);
    512 		if (plan->ep_p + l < plan->ep_ebp) {
    513 			plan->ep_bxp[plan->ep_narg++] =
    514 			    strcpy(plan->ep_p, entry->fts_path);
    515 			plan->ep_p += l + 1;
    516 
    517 			if (plan->ep_narg == plan->ep_maxargs)
    518 				run_f_exec(plan);
    519 		} else {
    520 			/*
    521 			 * Without sufficient space to copy in the next
    522 			 * argument, run the command to empty out the
    523 			 * buffer before re-attepting the copy.
    524 			 */
    525 			run_f_exec(plan);
    526 			if ((plan->ep_p + l < plan->ep_ebp)) {
    527 				plan->ep_bxp[plan->ep_narg++]
    528 				    = strcpy(plan->ep_p, entry->fts_path);
    529 				plan->ep_p += l + 1;
    530 			} else
    531 				errx(1, "insufficient space for argument");
    532 		}
    533 		return (1);
    534 	} else {
    535 		for (cnt = 0; plan->e_argv[cnt]; ++cnt)
    536 			if (plan->e_len[cnt])
    537 				brace_subst(plan->e_orig[cnt],
    538 				    &plan->e_argv[cnt],
    539 				    entry->fts_path,
    540 				    &plan->e_len[cnt]);
    541 		if (plan->flags & F_NEEDOK && !queryuser(plan->e_argv))
    542 			return (0);
    543 
    544 		/* Don't mix output of command with find output. */
    545 		fflush(stdout);
    546 		fflush(stderr);
    547 
    548 		switch (pid = vfork()) {
    549 		case -1:
    550 			err(1, "vfork");
    551 			/* NOTREACHED */
    552 		case 0:
    553 			if (fchdir(dotfd)) {
    554 				warn("chdir");
    555 				_exit(1);
    556 			}
    557 			execvp(plan->e_argv[0], plan->e_argv);
    558 			warn("%s", plan->e_argv[0]);
    559 			_exit(1);
    560 		}
    561 		pid = waitpid(pid, &status, 0);
    562 		return (pid != -1 && WIFEXITED(status)
    563 		    && !WEXITSTATUS(status));
    564 	}
    565 }
    566 
    567 static void
    568 run_f_exec(PLAN *plan)
    569 {
    570 	pid_t pid;
    571 	int rval, status;
    572 
    573 	/* Ensure arg list is null terminated. */
    574 	plan->ep_bxp[plan->ep_narg] = NULL;
    575 
    576 	/* Don't mix output of command with find output. */
    577 	fflush(stdout);
    578 	fflush(stderr);
    579 
    580 	switch (pid = vfork()) {
    581 	case -1:
    582 		err(1, "vfork");
    583 		/* NOTREACHED */
    584 	case 0:
    585 		if (fchdir(dotfd)) {
    586 			warn("chdir");
    587 			_exit(1);
    588 		}
    589 		execvp(plan->e_argv[0], plan->e_argv);
    590 		warn("%s", plan->e_argv[0]);
    591 		_exit(1);
    592 	}
    593 
    594 	/* Clear out the argument list. */
    595 	plan->ep_narg = 0;
    596 	plan->ep_bxp[plan->ep_narg] = NULL;
    597 	/* As well as the argument buffer. */
    598 	plan->ep_p = plan->ep_bbp;
    599 	*plan->ep_p = '\0';
    600 
    601 	pid = waitpid(pid, &status, 0);
    602 	if (WIFEXITED(status))
    603 		rval = WEXITSTATUS(status);
    604 	else
    605 		rval = -1;
    606 
    607 	/*
    608 	 * If we have a non-zero exit status, preserve it so find(1) can
    609 	 * later exit with it.
    610 	 */
    611 	if (rval)
    612 		plan->ep_rval = rval;
    613 }
    614 
    615 /*
    616  * c_exec --
    617  *	build three parallel arrays, one with pointers to the strings passed
    618  *	on the command line, one with (possibly duplicated) pointers to the
    619  *	argv array, and one with integer values that are lengths of the
    620  *	strings, but also flags meaning that the string has to be massaged.
    621  *
    622  *	If -exec ... {} +, use only the first array, but make it large
    623  *	enough to hold 5000 args (cf. src/usr.bin/xargs/xargs.c for a
    624  *	discussion), and then allocate ARG_MAX - 4K of space for args.
    625  */
    626 PLAN *
    627 c_exec(char ***argvp, int isok)
    628 {
    629 	PLAN *new;			/* node returned */
    630 	int cnt, brace, lastbrace;
    631 	char **argv, **ap, *p;
    632 
    633 	isoutput = 1;
    634 
    635 	new = palloc(N_EXEC, f_exec);
    636 	if (isok)
    637 		new->flags |= F_NEEDOK;
    638 
    639 	/*
    640 	 * Terminate if we encounter an arg exacty equal to ";", or an
    641 	 * arg exacty equal to "+" following an arg exacty equal to
    642 	 * "{}".
    643 	 */
    644 	for (ap = argv = *argvp, brace = 0;; ++ap) {
    645 		if (!*ap)
    646 			errx(1, "%s: no terminating \";\" or \"+\"",
    647 			    isok ? "-ok" : "-exec");
    648 		lastbrace = brace;
    649 		brace = 0;
    650 		if (strcmp(*ap, "{}") == 0)
    651 			brace = 1;
    652 		if (strcmp(*ap, ";") == 0)
    653 			break;
    654 		if (strcmp(*ap, "+") == 0 && lastbrace) {
    655 			new->flags |= F_PLUSSET;
    656 			break;
    657 		}
    658 	}
    659 
    660 	/*
    661 	 * POSIX says -ok ... {} + "need not be supported," and it does
    662 	 * not make much sense anyway.
    663 	 */
    664 	if (new->flags & F_NEEDOK && new->flags & F_PLUSSET)
    665 		errx(1, "-ok: terminating \"+\" not permitted.");
    666 
    667 	if (new->flags & F_PLUSSET) {
    668 		u_int c, bufsize;
    669 
    670 		cnt = ap - *argvp - 1;			/* units are words */
    671 		new->ep_maxargs = 5000;
    672 		new->e_argv = (char **)emalloc((u_int)(cnt + new->ep_maxargs)
    673 						* sizeof(char **));
    674 
    675 		/* We start stuffing arguments after the user's last one. */
    676 		new->ep_bxp = &new->e_argv[cnt];
    677 		new->ep_narg = 0;
    678 
    679 		/*
    680 		 * Count up the space of the user's arguments, and
    681 		 * subtract that from what we allocate.
    682 		 */
    683 		for (argv = *argvp, c = 0, cnt = 0;
    684 		     argv < ap;
    685 		     ++argv, ++cnt) {
    686 			c += strlen(*argv) + 1;
    687 			new->e_argv[cnt] = *argv;
    688 		}
    689 		bufsize = ARG_MAX - 4 * 1024 - c;
    690 
    691 
    692 		/*
    693 		 * Allocate, and then initialize current, base, and
    694 		 * end pointers.
    695 		 */
    696 		new->ep_p = new->ep_bbp = malloc(bufsize + 1);
    697 		new->ep_ebp = new->ep_bbp + bufsize - 1;
    698 		new->ep_rval = 0;
    699 	} else { /* !F_PLUSSET */
    700 		cnt = ap - *argvp + 1;
    701 		new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
    702 		new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
    703 		new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
    704 
    705 		for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
    706 			new->e_orig[cnt] = *argv;
    707 			for (p = *argv; *p; ++p)
    708 				if (p[0] == '{' && p[1] == '}') {
    709 					new->e_argv[cnt] =
    710 						emalloc((u_int)MAXPATHLEN);
    711 					new->e_len[cnt] = MAXPATHLEN;
    712 					break;
    713 				}
    714 			if (!*p) {
    715 				new->e_argv[cnt] = *argv;
    716 				new->e_len[cnt] = 0;
    717 			}
    718 		}
    719 		new->e_orig[cnt] = NULL;
    720 	}
    721 
    722 	new->e_argv[cnt] = NULL;
    723 	*argvp = argv + 1;
    724 	return (new);
    725 }
    726 
    727 /*
    728  * -execdir utility [arg ... ] ; functions --
    729  *
    730  *	True if the executed utility returns a zero value as exit status.
    731  *	The end of the primary expression is delimited by a semicolon.  If
    732  *	"{}" occurs anywhere, it gets replaced by the unqualified pathname.
    733  *	The current directory for the execution of utility is the same as
    734  *	the directory where the file lives.
    735  */
    736 int
    737 f_execdir(PLAN *plan, FTSENT *entry)
    738 {
    739 	int cnt;
    740 	pid_t pid;
    741 	int status;
    742 	char *file;
    743 
    744 	/* XXX - if file/dir ends in '/' this will not work -- can it? */
    745 	if ((file = strrchr(entry->fts_path, '/')))
    746 		file++;
    747 	else
    748 		file = entry->fts_path;
    749 
    750 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
    751 		if (plan->e_len[cnt])
    752 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
    753 			    file, &plan->e_len[cnt]);
    754 
    755 	/* don't mix output of command with find output */
    756 	fflush(stdout);
    757 	fflush(stderr);
    758 
    759 	switch (pid = vfork()) {
    760 	case -1:
    761 		err(1, "fork");
    762 		/* NOTREACHED */
    763 	case 0:
    764 		execvp(plan->e_argv[0], plan->e_argv);
    765 		warn("%s", plan->e_argv[0]);
    766 		_exit(1);
    767 	}
    768 	pid = waitpid(pid, &status, 0);
    769 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
    770 }
    771 
    772 /*
    773  * c_execdir --
    774  *	build three parallel arrays, one with pointers to the strings passed
    775  *	on the command line, one with (possibly duplicated) pointers to the
    776  *	argv array, and one with integer values that are lengths of the
    777  *	strings, but also flags meaning that the string has to be massaged.
    778  */
    779 PLAN *
    780 c_execdir(char ***argvp, int isok)
    781 {
    782 	PLAN *new;			/* node returned */
    783 	int cnt;
    784 	char **argv, **ap, *p;
    785 
    786 	ftsoptions &= ~FTS_NOSTAT;
    787 	isoutput = 1;
    788 
    789 	new = palloc(N_EXECDIR, f_execdir);
    790 
    791 	for (ap = argv = *argvp;; ++ap) {
    792 		if (!*ap)
    793 			errx(1,
    794 			    "-execdir: no terminating \";\"");
    795 		if (**ap == ';')
    796 			break;
    797 	}
    798 
    799 	cnt = ap - *argvp + 1;
    800 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
    801 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
    802 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
    803 
    804 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
    805 		new->e_orig[cnt] = *argv;
    806 		for (p = *argv; *p; ++p)
    807 			if (p[0] == '{' && p[1] == '}') {
    808 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
    809 				new->e_len[cnt] = MAXPATHLEN;
    810 				break;
    811 			}
    812 		if (!*p) {
    813 			new->e_argv[cnt] = *argv;
    814 			new->e_len[cnt] = 0;
    815 		}
    816 	}
    817 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
    818 
    819 	*argvp = argv + 1;
    820 	return (new);
    821 }
    822 
    823 PLAN *
    824 c_exit(char ***argvp, int isok)
    825 {
    826 	char *arg = **argvp;
    827 	PLAN *new;
    828 
    829 	/* not technically true, but otherwise '-print' is implied */
    830 	isoutput = 1;
    831 
    832 	new = palloc(N_EXIT, f_always_true);
    833 
    834 	if (arg) {
    835 		(*argvp)++;
    836 		new->exit_val = find_parsenum(new, "-exit", arg, NULL);
    837 	} else
    838 		new->exit_val = 0;
    839 
    840 	return (new);
    841 }
    842 
    843 
    844 /*
    845  * -false function
    846  */
    847 int
    848 f_false(PLAN *plan, FTSENT *entry)
    849 {
    850 
    851 	return (0);
    852 }
    853 
    854 PLAN *
    855 c_false(char ***argvp, int isok)
    856 {
    857 	return (palloc(N_FALSE, f_false));
    858 }
    859 
    860 
    861 /*
    862  * -flags [-]flags functions --
    863  */
    864 int
    865 f_flags(PLAN *plan, FTSENT *entry)
    866 {
    867 	u_int32_t flags;
    868 
    869 	flags = entry->fts_statp->st_flags;
    870 	if (plan->flags == F_ATLEAST)
    871 		return ((plan->f_data | flags) == flags);
    872 	else
    873 		return (flags == plan->f_data);
    874 	/* NOTREACHED */
    875 }
    876 
    877 PLAN *
    878 c_flags(char ***argvp, int isok)
    879 {
    880 	char *flags = **argvp;
    881 	PLAN *new;
    882 	u_long flagset;
    883 
    884 	(*argvp)++;
    885 	ftsoptions &= ~FTS_NOSTAT;
    886 
    887 	new = palloc(N_FLAGS, f_flags);
    888 
    889 	if (*flags == '-') {
    890 		new->flags = F_ATLEAST;
    891 		++flags;
    892 	}
    893 
    894 	flagset = 0;
    895 	if ((strcmp(flags, "none") != 0) &&
    896 	    (string_to_flags(&flags, &flagset, NULL) != 0))
    897 		errx(1, "-flags: %s: illegal flags string", flags);
    898 	new->f_data = flagset;
    899 	return (new);
    900 }
    901 
    902 /*
    903  * -follow functions --
    904  *
    905  *	Always true, causes symbolic links to be followed on a global
    906  *	basis.
    907  */
    908 PLAN *
    909 c_follow(char ***argvp, int isok)
    910 {
    911 	ftsoptions &= ~FTS_PHYSICAL;
    912 	ftsoptions |= FTS_LOGICAL;
    913 
    914 	return (palloc(N_FOLLOW, f_always_true));
    915 }
    916 
    917 /* -fprint functions --
    918  *
    919  *	Causes the current pathame to be written to the defined output file.
    920  */
    921 int
    922 f_fprint(PLAN *plan, FTSENT *entry)
    923 {
    924 
    925 	if (-1 == fprintf(plan->fprint_file, "%s\n", entry->fts_path))
    926 		warn("fprintf");
    927 
    928 	return(1);
    929 
    930 	/* no descriptors are closed; they will be closed by
    931 	   operating system when this find command exits.  */
    932 }
    933 
    934 PLAN *
    935 c_fprint(char ***argvp, int isok)
    936 {
    937 	PLAN *new;
    938 
    939 	isoutput = 1; /* do not assume -print */
    940 
    941 	new = palloc(N_FPRINT, f_fprint);
    942 
    943 	if (NULL == (new->fprint_file = fopen(**argvp, "w")))
    944 		err(1, "-fprint: %s: cannot create file", **argvp);
    945 
    946 	(*argvp)++;
    947 	return (new);
    948 }
    949 
    950 /*
    951  * -fstype functions --
    952  *
    953  *	True if the file is of a certain type.
    954  */
    955 int
    956 f_fstype(PLAN *plan, FTSENT *entry)
    957 {
    958 	static dev_t curdev;	/* need a guaranteed illegal dev value */
    959 	static int first = 1;
    960 	struct statvfs sb;
    961 	static short val;
    962 	static char fstype[sizeof(sb.f_fstypename)];
    963 	char *p, save[2];
    964 
    965 	memset(&save, 0, sizeof save);	/* XXX gcc */
    966 
    967 	/* Only check when we cross mount point. */
    968 	if (first || curdev != entry->fts_statp->st_dev) {
    969 		curdev = entry->fts_statp->st_dev;
    970 
    971 		/*
    972 		 * Statfs follows symlinks; find wants the link's file system,
    973 		 * not where it points.
    974 		 */
    975 		if (entry->fts_info == FTS_SL ||
    976 		    entry->fts_info == FTS_SLNONE) {
    977 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
    978 				++p;
    979 			else
    980 				p = entry->fts_accpath;
    981 			save[0] = p[0];
    982 			p[0] = '.';
    983 			save[1] = p[1];
    984 			p[1] = '\0';
    985 
    986 		} else
    987 			p = NULL;
    988 
    989 		if (statvfs(entry->fts_accpath, &sb))
    990 			err(1, "%s", entry->fts_accpath);
    991 
    992 		if (p) {
    993 			p[0] = save[0];
    994 			p[1] = save[1];
    995 		}
    996 
    997 		first = 0;
    998 
    999 		/*
   1000 		 * Further tests may need both of these values, so
   1001 		 * always copy both of them.
   1002 		 */
   1003 		val = sb.f_flag;
   1004 		strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
   1005 	}
   1006 	switch (plan->flags) {
   1007 	case F_MTFLAG:
   1008 		return (val & plan->mt_data);
   1009 	case F_MTTYPE:
   1010 		return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
   1011 	default:
   1012 		abort();
   1013 	}
   1014 }
   1015 
   1016 PLAN *
   1017 c_fstype(char ***argvp, int isok)
   1018 {
   1019 	char *arg = **argvp;
   1020 	PLAN *new;
   1021 
   1022 	(*argvp)++;
   1023 	ftsoptions &= ~FTS_NOSTAT;
   1024 
   1025 	new = palloc(N_FSTYPE, f_fstype);
   1026 
   1027 	switch (*arg) {
   1028 	case 'l':
   1029 		if (!strcmp(arg, "local")) {
   1030 			new->flags = F_MTFLAG;
   1031 			new->mt_data = MNT_LOCAL;
   1032 			return (new);
   1033 		}
   1034 		break;
   1035 	case 'r':
   1036 		if (!strcmp(arg, "rdonly")) {
   1037 			new->flags = F_MTFLAG;
   1038 			new->mt_data = MNT_RDONLY;
   1039 			return (new);
   1040 		}
   1041 		break;
   1042 	}
   1043 
   1044 	new->flags = F_MTTYPE;
   1045 	new->c_data = arg;
   1046 	return (new);
   1047 }
   1048 
   1049 /*
   1050  * -group gname functions --
   1051  *
   1052  *	True if the file belongs to the group gname.  If gname is numeric and
   1053  *	an equivalent of the getgrnam() function does not return a valid group
   1054  *	name, gname is taken as a group ID.
   1055  */
   1056 int
   1057 f_group(PLAN *plan, FTSENT *entry)
   1058 {
   1059 
   1060 	return (entry->fts_statp->st_gid == plan->g_data);
   1061 }
   1062 
   1063 PLAN *
   1064 c_group(char ***argvp, int isok)
   1065 {
   1066 	char *gname = **argvp;
   1067 	PLAN *new;
   1068 	struct group *g;
   1069 	gid_t gid;
   1070 
   1071 	(*argvp)++;
   1072 	ftsoptions &= ~FTS_NOSTAT;
   1073 
   1074 	g = getgrnam(gname);
   1075 	if (g == NULL) {
   1076 		gid = atoi(gname);
   1077 		if (gid == 0 && gname[0] != '0')
   1078 			errx(1, "-group: %s: no such group", gname);
   1079 	} else
   1080 		gid = g->gr_gid;
   1081 
   1082 	new = palloc(N_GROUP, f_group);
   1083 	new->g_data = gid;
   1084 	return (new);
   1085 }
   1086 
   1087 /*
   1088  * -inum n functions --
   1089  *
   1090  *	True if the file has inode # n.
   1091  */
   1092 int
   1093 f_inum(PLAN *plan, FTSENT *entry)
   1094 {
   1095 
   1096 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
   1097 }
   1098 
   1099 PLAN *
   1100 c_inum(char ***argvp, int isok)
   1101 {
   1102 	char *arg = **argvp;
   1103 	PLAN *new;
   1104 
   1105 	(*argvp)++;
   1106 	ftsoptions &= ~FTS_NOSTAT;
   1107 
   1108 	new = palloc(N_INUM, f_inum);
   1109 	new->i_data = find_parsenum(new, "-inum", arg, NULL);
   1110 	return (new);
   1111 }
   1112 
   1113 /*
   1114  * -links n functions --
   1115  *
   1116  *	True if the file has n links.
   1117  */
   1118 int
   1119 f_links(PLAN *plan, FTSENT *entry)
   1120 {
   1121 
   1122 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
   1123 }
   1124 
   1125 PLAN *
   1126 c_links(char ***argvp, int isok)
   1127 {
   1128 	char *arg = **argvp;
   1129 	PLAN *new;
   1130 
   1131 	(*argvp)++;
   1132 	ftsoptions &= ~FTS_NOSTAT;
   1133 
   1134 	new = palloc(N_LINKS, f_links);
   1135 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
   1136 	return (new);
   1137 }
   1138 
   1139 /*
   1140  * -ls functions --
   1141  *
   1142  *	Always true - prints the current entry to stdout in "ls" format.
   1143  */
   1144 int
   1145 f_ls(PLAN *plan, FTSENT *entry)
   1146 {
   1147 
   1148 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
   1149 	return (1);
   1150 }
   1151 
   1152 PLAN *
   1153 c_ls(char ***argvp, int isok)
   1154 {
   1155 
   1156 	ftsoptions &= ~FTS_NOSTAT;
   1157 	isoutput = 1;
   1158 
   1159 	return (palloc(N_LS, f_ls));
   1160 }
   1161 
   1162 /*
   1163  * - maxdepth n functions --
   1164  *
   1165  *	True if the current search depth is less than or equal to the
   1166  *	maximum depth specified
   1167  */
   1168 int
   1169 f_maxdepth(PLAN *plan, FTSENT *entry)
   1170 {
   1171 	extern FTS *tree;
   1172 
   1173 	if (entry->fts_level >= plan->max_data)
   1174 		fts_set(tree, entry, FTS_SKIP);
   1175 	return (entry->fts_level <= plan->max_data);
   1176 }
   1177 
   1178 PLAN *
   1179 c_maxdepth(char ***argvp, int isok)
   1180 {
   1181 	char *arg = **argvp;
   1182 	PLAN *new;
   1183 
   1184 	(*argvp)++;
   1185 	new = palloc(N_MAXDEPTH, f_maxdepth);
   1186 	new->max_data = atoi(arg);
   1187 	return (new);
   1188 }
   1189 
   1190 /*
   1191  * - mindepth n functions --
   1192  *
   1193  *	True if the current search depth is greater than or equal to the
   1194  *	minimum depth specified
   1195  */
   1196 int
   1197 f_mindepth(PLAN *plan, FTSENT *entry)
   1198 {
   1199 	return (entry->fts_level >= plan->min_data);
   1200 }
   1201 
   1202 PLAN *
   1203 c_mindepth(char ***argvp, int isok)
   1204 {
   1205 	char *arg = **argvp;
   1206 	PLAN *new;
   1207 
   1208 	(*argvp)++;
   1209 	new = palloc(N_MINDEPTH, f_mindepth);
   1210 	new->min_data = atoi(arg);
   1211 	return (new);
   1212 }
   1213 /*
   1214  * -mmin n functions --
   1215  *
   1216  *	True if the difference between the file modification time and the
   1217  *	current time is n 24 hour periods.
   1218  */
   1219 int
   1220 f_mmin(PLAN *plan, FTSENT *entry)
   1221 {
   1222 	COMPARE((now - entry->fts_statp->st_mtime + SECSPERMIN - 1) /
   1223 	    SECSPERMIN, plan->t_data);
   1224 }
   1225 
   1226 PLAN *
   1227 c_mmin(char ***argvp, int isok)
   1228 {
   1229 	char *arg = **argvp;
   1230 	PLAN *new;
   1231 
   1232 	(*argvp)++;
   1233 	ftsoptions &= ~FTS_NOSTAT;
   1234 
   1235 	new = palloc(N_MMIN, f_mmin);
   1236 	new->t_data = find_parsenum(new, "-mmin", arg, NULL);
   1237 	TIME_CORRECT(new, N_MMIN);
   1238 	return (new);
   1239 }
   1240 /*
   1241  * -mtime n functions --
   1242  *
   1243  *	True if the difference between the file modification time and the
   1244  *	current time is n 24 hour periods.
   1245  */
   1246 int
   1247 f_mtime(PLAN *plan, FTSENT *entry)
   1248 {
   1249 	COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
   1250 	    SECSPERDAY, plan->t_data);
   1251 }
   1252 
   1253 PLAN *
   1254 c_mtime(char ***argvp, int isok)
   1255 {
   1256 	char *arg = **argvp;
   1257 	PLAN *new;
   1258 
   1259 	(*argvp)++;
   1260 	ftsoptions &= ~FTS_NOSTAT;
   1261 
   1262 	new = palloc(N_MTIME, f_mtime);
   1263 	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
   1264 	TIME_CORRECT(new, N_MTIME);
   1265 	return (new);
   1266 }
   1267 
   1268 /*
   1269  * -name functions --
   1270  *
   1271  *	True if the basename of the filename being examined
   1272  *	matches pattern using Pattern Matching Notation S3.14
   1273  */
   1274 int
   1275 f_name(PLAN *plan, FTSENT *entry)
   1276 {
   1277 
   1278 	return (!fnmatch(plan->c_data, entry->fts_name, 0));
   1279 }
   1280 
   1281 PLAN *
   1282 c_name(char ***argvp, int isok)
   1283 {
   1284 	char *pattern = **argvp;
   1285 	PLAN *new;
   1286 
   1287 	(*argvp)++;
   1288 	new = palloc(N_NAME, f_name);
   1289 	new->c_data = pattern;
   1290 	return (new);
   1291 }
   1292 
   1293 /*
   1294  * -iname functions --
   1295  *
   1296  *	Similar to -name, but does case insensitive matching
   1297  *
   1298  */
   1299 int
   1300 f_iname(PLAN *plan, FTSENT *entry)
   1301 {
   1302 	return (!fnmatch(plan->c_data, entry->fts_name, FNM_CASEFOLD));
   1303 }
   1304 
   1305 PLAN *
   1306 c_iname(char ***argvp, int isok)
   1307 {
   1308 	char *pattern = **argvp;
   1309 	PLAN *new;
   1310 
   1311 	(*argvp)++;
   1312 	new = palloc(N_INAME, f_iname);
   1313 	new->c_data = pattern;
   1314 	return (new);
   1315 }
   1316 
   1317 /*
   1318  * -newer file functions --
   1319  *
   1320  *	True if the current file has been modified more recently
   1321  *	than the modification time of the file named by the pathname
   1322  *	file.
   1323  */
   1324 int
   1325 f_newer(PLAN *plan, FTSENT *entry)
   1326 {
   1327 
   1328 	return (entry->fts_statp->st_mtime > plan->t_data);
   1329 }
   1330 
   1331 PLAN *
   1332 c_newer(char ***argvp, int isok)
   1333 {
   1334 	char *filename = **argvp;
   1335 	PLAN *new;
   1336 	struct stat sb;
   1337 
   1338 	(*argvp)++;
   1339 	ftsoptions &= ~FTS_NOSTAT;
   1340 
   1341 	if (stat(filename, &sb))
   1342 		err(1, "%s", filename);
   1343 	new = palloc(N_NEWER, f_newer);
   1344 	new->t_data = sb.st_mtime;
   1345 	return (new);
   1346 }
   1347 
   1348 /*
   1349  * -nogroup functions --
   1350  *
   1351  *	True if file belongs to a user ID for which the equivalent
   1352  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
   1353  */
   1354 int
   1355 f_nogroup(PLAN *plan, FTSENT *entry)
   1356 {
   1357 
   1358 	return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
   1359 }
   1360 
   1361 PLAN *
   1362 c_nogroup(char ***argvp, int isok)
   1363 {
   1364 	ftsoptions &= ~FTS_NOSTAT;
   1365 
   1366 	return (palloc(N_NOGROUP, f_nogroup));
   1367 }
   1368 
   1369 /*
   1370  * -nouser functions --
   1371  *
   1372  *	True if file belongs to a user ID for which the equivalent
   1373  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
   1374  */
   1375 int
   1376 f_nouser(PLAN *plan, FTSENT *entry)
   1377 {
   1378 
   1379 	return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
   1380 }
   1381 
   1382 PLAN *
   1383 c_nouser(char ***argvp, int isok)
   1384 {
   1385 	ftsoptions &= ~FTS_NOSTAT;
   1386 
   1387 	return (palloc(N_NOUSER, f_nouser));
   1388 }
   1389 
   1390 /*
   1391  * -path functions --
   1392  *
   1393  *	True if the path of the filename being examined
   1394  *	matches pattern using Pattern Matching Notation S3.14
   1395  */
   1396 int
   1397 f_path(PLAN *plan, FTSENT *entry)
   1398 {
   1399 
   1400 	return (!fnmatch(plan->c_data, entry->fts_path, 0));
   1401 }
   1402 
   1403 PLAN *
   1404 c_path(char ***argvp, int isok)
   1405 {
   1406 	char *pattern = **argvp;
   1407 	PLAN *new;
   1408 
   1409 	(*argvp)++;
   1410 	new = palloc(N_NAME, f_path);
   1411 	new->c_data = pattern;
   1412 	return (new);
   1413 }
   1414 
   1415 /*
   1416  * -perm functions --
   1417  *
   1418  *	The mode argument is used to represent file mode bits.  If it starts
   1419  *	with a leading digit, it's treated as an octal mode, otherwise as a
   1420  *	symbolic mode.
   1421  */
   1422 int
   1423 f_perm(PLAN *plan, FTSENT *entry)
   1424 {
   1425 	mode_t mode;
   1426 
   1427 	mode = entry->fts_statp->st_mode &
   1428 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
   1429 	if (plan->flags == F_ATLEAST)
   1430 		return ((plan->m_data | mode) == mode);
   1431 	else
   1432 		return (mode == plan->m_data);
   1433 	/* NOTREACHED */
   1434 }
   1435 
   1436 PLAN *
   1437 c_perm(char ***argvp, int isok)
   1438 {
   1439 	char *perm = **argvp;
   1440 	PLAN *new;
   1441 	mode_t *set;
   1442 
   1443 	(*argvp)++;
   1444 	ftsoptions &= ~FTS_NOSTAT;
   1445 
   1446 	new = palloc(N_PERM, f_perm);
   1447 
   1448 	if (*perm == '-') {
   1449 		new->flags = F_ATLEAST;
   1450 		++perm;
   1451 	}
   1452 
   1453 	if ((set = setmode(perm)) == NULL)
   1454 		err(1, "-perm: Cannot set file mode `%s'", perm);
   1455 
   1456 	new->m_data = getmode(set, 0);
   1457 	free(set);
   1458 	return (new);
   1459 }
   1460 
   1461 /*
   1462  * -print functions --
   1463  *
   1464  *	Always true, causes the current pathame to be written to
   1465  *	standard output.
   1466  */
   1467 int
   1468 f_print(PLAN *plan, FTSENT *entry)
   1469 {
   1470 
   1471 	(void)printf("%s\n", entry->fts_path);
   1472 	return (1);
   1473 }
   1474 
   1475 int
   1476 f_print0(PLAN *plan, FTSENT *entry)
   1477 {
   1478 
   1479 	(void)fputs(entry->fts_path, stdout);
   1480 	(void)fputc('\0', stdout);
   1481 	return (1);
   1482 }
   1483 
   1484 int
   1485 f_printx(PLAN *plan, FTSENT *entry)
   1486 {
   1487 	char *cp;
   1488 
   1489 	for (cp = entry->fts_path; *cp; cp++) {
   1490 		if (*cp == '\'' || *cp == '\"' || *cp == ' ' ||
   1491 		    *cp == '$'  || *cp == '`'  ||
   1492 		    *cp == '\t' || *cp == '\n' || *cp == '\\')
   1493 			fputc('\\', stdout);
   1494 
   1495 		fputc(*cp, stdout);
   1496 	}
   1497 
   1498 	fputc('\n', stdout);
   1499 	return (1);
   1500 }
   1501 
   1502 PLAN *
   1503 c_print(char ***argvp, int isok)
   1504 {
   1505 
   1506 	isoutput = 1;
   1507 
   1508 	return (palloc(N_PRINT, f_print));
   1509 }
   1510 
   1511 PLAN *
   1512 c_print0(char ***argvp, int isok)
   1513 {
   1514 
   1515 	isoutput = 1;
   1516 
   1517 	return (palloc(N_PRINT0, f_print0));
   1518 }
   1519 
   1520 PLAN *
   1521 c_printx(char ***argvp, int isok)
   1522 {
   1523 
   1524 	isoutput = 1;
   1525 
   1526 	return (palloc(N_PRINTX, f_printx));
   1527 }
   1528 
   1529 /*
   1530  * -prune functions --
   1531  *
   1532  *	Prune a portion of the hierarchy.
   1533  */
   1534 int
   1535 f_prune(PLAN *plan, FTSENT *entry)
   1536 {
   1537 	if (fts_set(tree, entry, FTS_SKIP))
   1538 		err(1, "%s", entry->fts_path);
   1539 	return (1);
   1540 }
   1541 
   1542 PLAN *
   1543 c_prune(char ***argvp, int isok)
   1544 {
   1545 
   1546 	return (palloc(N_PRUNE, f_prune));
   1547 }
   1548 
   1549 /*
   1550  * -regex regexp (and related) functions --
   1551  *
   1552  *	True if the complete file path matches the regular expression regexp.
   1553  *	For -regex, regexp is a case-sensitive (basic) regular expression.
   1554  *	For -iregex, regexp is a case-insensitive (basic) regular expression.
   1555  */
   1556 int
   1557 f_regex(PLAN *plan, FTSENT *entry)
   1558 {
   1559 
   1560 	return (regexec(&plan->regexp_data, entry->fts_path, 0, NULL, 0) == 0);
   1561 }
   1562 
   1563 static PLAN *
   1564 c_regex_common(char ***argvp, int isok, enum ntype type, bool icase)
   1565 {
   1566 	char errbuf[LINE_MAX];
   1567 	regex_t reg;
   1568 	char *regexp = **argvp;
   1569 	char *lineregexp;
   1570 	PLAN *new;
   1571 	int rv;
   1572 	size_t len;
   1573 
   1574 	(*argvp)++;
   1575 
   1576 	len = strlen(regexp) + 1 + 6;
   1577 	lineregexp = malloc(len);	/* max needed */
   1578 	if (lineregexp == NULL)
   1579 		err(1, NULL);
   1580 	snprintf(lineregexp, len, "^%s(%s%s)$",
   1581 	    (regcomp_flags & REG_EXTENDED) ? "" : "\\", regexp,
   1582 	    (regcomp_flags & REG_EXTENDED) ? "" : "\\");
   1583 	rv = regcomp(&reg, lineregexp, REG_NOSUB|regcomp_flags|
   1584 	    (icase ? REG_ICASE : 0));
   1585 	free(lineregexp);
   1586 	if (rv != 0) {
   1587 		regerror(rv, &reg, errbuf, sizeof errbuf);
   1588 		errx(1, "regexp %s: %s", regexp, errbuf);
   1589 	}
   1590 
   1591 	new = palloc(type, f_regex);
   1592 	new->regexp_data = reg;
   1593 	return (new);
   1594 }
   1595 
   1596 PLAN *
   1597 c_regex(char ***argvp, int isok)
   1598 {
   1599 
   1600 	return (c_regex_common(argvp, isok, N_REGEX, false));
   1601 }
   1602 
   1603 PLAN *
   1604 c_iregex(char ***argvp, int isok)
   1605 {
   1606 
   1607 	return (c_regex_common(argvp, isok, N_IREGEX, true));
   1608 }
   1609 
   1610 /*
   1611  * -size n[c] functions --
   1612  *
   1613  *	True if the file size in bytes, divided by an implementation defined
   1614  *	value and rounded up to the next integer, is n.  If n is followed by
   1615  *	a c, the size is in bytes.
   1616  */
   1617 #define	FIND_SIZE	512
   1618 static int divsize = 1;
   1619 
   1620 int
   1621 f_size(PLAN *plan, FTSENT *entry)
   1622 {
   1623 	off_t size;
   1624 
   1625 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
   1626 	    FIND_SIZE : entry->fts_statp->st_size;
   1627 	COMPARE(size, plan->o_data);
   1628 }
   1629 
   1630 PLAN *
   1631 c_size(char ***argvp, int isok)
   1632 {
   1633 	char *arg = **argvp;
   1634 	PLAN *new;
   1635 	char endch;
   1636 
   1637 	(*argvp)++;
   1638 	ftsoptions &= ~FTS_NOSTAT;
   1639 
   1640 	new = palloc(N_SIZE, f_size);
   1641 	endch = 'c';
   1642 	new->o_data = find_parsenum(new, "-size", arg, &endch);
   1643 	if (endch == 'c')
   1644 		divsize = 0;
   1645 	return (new);
   1646 }
   1647 
   1648 /*
   1649  * -type c functions --
   1650  *
   1651  *	True if the type of the file is c, where c is b, c, d, p, f or w
   1652  *	for block special file, character special file, directory, FIFO,
   1653  *	regular file or whiteout respectively.
   1654  */
   1655 int
   1656 f_type(PLAN *plan, FTSENT *entry)
   1657 {
   1658 
   1659 	return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
   1660 }
   1661 
   1662 PLAN *
   1663 c_type(char ***argvp, int isok)
   1664 {
   1665 	char *typestring = **argvp;
   1666 	PLAN *new;
   1667 	mode_t  mask = (mode_t)0;
   1668 
   1669 	(*argvp)++;
   1670 	ftsoptions &= ~FTS_NOSTAT;
   1671 
   1672 	switch (typestring[0]) {
   1673 	case 'b':
   1674 		mask = S_IFBLK;
   1675 		break;
   1676 	case 'c':
   1677 		mask = S_IFCHR;
   1678 		break;
   1679 	case 'd':
   1680 		mask = S_IFDIR;
   1681 		break;
   1682 	case 'f':
   1683 		mask = S_IFREG;
   1684 		break;
   1685 	case 'l':
   1686 		mask = S_IFLNK;
   1687 		break;
   1688 	case 'p':
   1689 		mask = S_IFIFO;
   1690 		break;
   1691 	case 's':
   1692 		mask = S_IFSOCK;
   1693 		break;
   1694 #ifdef S_IFWHT
   1695 	case 'W':
   1696 	case 'w':
   1697 		mask = S_IFWHT;
   1698 #ifdef FTS_WHITEOUT
   1699 		ftsoptions |= FTS_WHITEOUT;
   1700 #endif
   1701 		break;
   1702 #endif /* S_IFWHT */
   1703 	default:
   1704 		errx(1, "-type: %s: unknown type", typestring);
   1705 	}
   1706 
   1707 	new = palloc(N_TYPE, f_type);
   1708 	new->m_data = mask;
   1709 	return (new);
   1710 }
   1711 
   1712 /*
   1713  * -user uname functions --
   1714  *
   1715  *	True if the file belongs to the user uname.  If uname is numeric and
   1716  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
   1717  *	return a valid user name, uname is taken as a user ID.
   1718  */
   1719 int
   1720 f_user(PLAN *plan, FTSENT *entry)
   1721 {
   1722 
   1723 	COMPARE(entry->fts_statp->st_uid, plan->u_data);
   1724 }
   1725 
   1726 PLAN *
   1727 c_user(char ***argvp, int isok)
   1728 {
   1729 	char *username = **argvp;
   1730 	PLAN *new;
   1731 	struct passwd *p;
   1732 	uid_t uid;
   1733 
   1734 	(*argvp)++;
   1735 	ftsoptions &= ~FTS_NOSTAT;
   1736 
   1737 	new = palloc(N_USER, f_user);
   1738 	p = getpwnam(username);
   1739 	if (p == NULL) {
   1740 		if (atoi(username) == 0 && username[0] != '0' &&
   1741 		    strcmp(username, "+0") && strcmp(username, "-0"))
   1742 			errx(1, "-user: %s: no such user", username);
   1743 		uid = find_parsenum(new, "-user", username, NULL);
   1744 
   1745 	} else {
   1746 		new->flags = F_EQUAL;
   1747 		uid = p->pw_uid;
   1748 	}
   1749 
   1750 	new->u_data = uid;
   1751 	return (new);
   1752 }
   1753 
   1754 /*
   1755  * -xdev functions --
   1756  *
   1757  *	Always true, causes find not to descend past directories that have a
   1758  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
   1759  */
   1760 PLAN *
   1761 c_xdev(char ***argvp, int isok)
   1762 {
   1763 	ftsoptions |= FTS_XDEV;
   1764 
   1765 	return (palloc(N_XDEV, f_always_true));
   1766 }
   1767 
   1768 /*
   1769  * ( expression ) functions --
   1770  *
   1771  *	True if expression is true.
   1772  */
   1773 int
   1774 f_expr(PLAN *plan, FTSENT *entry)
   1775 {
   1776 	PLAN *p;
   1777 	int state;
   1778 
   1779 	state = 0;
   1780 	for (p = plan->p_data[0];
   1781 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1782 	return (state);
   1783 }
   1784 
   1785 /*
   1786  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
   1787  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
   1788  * to a N_EXPR node containing the expression and the ')' node is discarded.
   1789  */
   1790 PLAN *
   1791 c_openparen(char ***argvp, int isok)
   1792 {
   1793 
   1794 	return (palloc(N_OPENPAREN, (int (*)(PLAN *, FTSENT *))-1));
   1795 }
   1796 
   1797 PLAN *
   1798 c_closeparen(char ***argvp, int isok)
   1799 {
   1800 
   1801 	return (palloc(N_CLOSEPAREN, (int (*)(PLAN *, FTSENT *))-1));
   1802 }
   1803 
   1804 /*
   1805  * ! expression functions --
   1806  *
   1807  *	Negation of a primary; the unary NOT operator.
   1808  */
   1809 int
   1810 f_not(PLAN *plan, FTSENT *entry)
   1811 {
   1812 	PLAN *p;
   1813 	int state;
   1814 
   1815 	state = 0;
   1816 	for (p = plan->p_data[0];
   1817 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1818 	return (!state);
   1819 }
   1820 
   1821 PLAN *
   1822 c_not(char ***argvp, int isok)
   1823 {
   1824 
   1825 	return (palloc(N_NOT, f_not));
   1826 }
   1827 
   1828 /*
   1829  * expression -o expression functions --
   1830  *
   1831  *	Alternation of primaries; the OR operator.  The second expression is
   1832  * not evaluated if the first expression is true.
   1833  */
   1834 int
   1835 f_or(PLAN *plan, FTSENT *entry)
   1836 {
   1837 	PLAN *p;
   1838 	int state;
   1839 
   1840 	state = 0;
   1841 	for (p = plan->p_data[0];
   1842 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1843 
   1844 	if (state)
   1845 		return (1);
   1846 
   1847 	for (p = plan->p_data[1];
   1848 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1849 	return (state);
   1850 }
   1851 
   1852 PLAN *
   1853 c_or(char ***argvp, int isok)
   1854 {
   1855 
   1856 	return (palloc(N_OR, f_or));
   1857 }
   1858 
   1859 PLAN *
   1860 c_null(char ***argvp, int isok)
   1861 {
   1862 
   1863 	return (NULL);
   1864 }
   1865 
   1866 
   1867 /*
   1868  * plan_cleanup --
   1869  *	Check and see if the specified plan has any residual state,
   1870  *	and if so, clean it up as appropriate.
   1871  *
   1872  *	At the moment, only N_EXEC has state. Two kinds: 1)
   1873  * 	lists of files to feed to subprocesses 2) State on exit
   1874  *	statusses of past subprocesses.
   1875  */
   1876 /* ARGSUSED1 */
   1877 int
   1878 plan_cleanup(PLAN *plan, void *arg)
   1879 {
   1880 	if (plan->type==N_EXEC && plan->ep_narg)
   1881 		run_f_exec(plan);
   1882 
   1883 	return plan->ep_rval;		/* Passed save exit-status up chain */
   1884 }
   1885 
   1886 static PLAN *
   1887 palloc(enum ntype t, int (*f)(PLAN *, FTSENT *))
   1888 {
   1889 	PLAN *new;
   1890 
   1891 	if ((new = malloc(sizeof(PLAN))) == NULL)
   1892 		err(1, NULL);
   1893 	memset(new, 0, sizeof(PLAN));
   1894 	new->type = t;
   1895 	new->eval = f;
   1896 	return (new);
   1897 }
   1898