Home | History | Annotate | Line # | Download | only in find
function.c revision 1.64
      1 /*	$NetBSD: function.c,v 1.64 2007/07/19 07:49:30 daniel 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.64 2007/07/19 07:49:30 daniel 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 			err(1, "%s", entry->fts_accpath);
    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 		if (strcmp(*ap, "{}") == 0)
    650 			brace = 1;
    651 		if (strcmp(*ap, ";") == 0)
    652 			break;
    653 		if (strcmp(*ap, "+") == 0 && lastbrace) {
    654 			new->flags |= F_PLUSSET;
    655 			break;
    656 		}
    657 	}
    658 
    659 	/*
    660 	 * POSIX says -ok ... {} + "need not be supported," and it does
    661 	 * not make much sense anyway.
    662 	 */
    663 	if (new->flags & F_NEEDOK && new->flags & F_PLUSSET)
    664 		errx(1, "-ok: terminating \"+\" not permitted.");
    665 
    666 	if (new->flags & F_PLUSSET) {
    667 		u_int c, bufsize;
    668 
    669 		cnt = ap - *argvp - 1;			/* units are words */
    670 		new->ep_maxargs = 5000;
    671 		new->e_argv = (char **)emalloc((u_int)(cnt + new->ep_maxargs)
    672 						* sizeof(char **));
    673 
    674 		/* We start stuffing arguments after the user's last one. */
    675 		new->ep_bxp = &new->e_argv[cnt];
    676 		new->ep_narg = 0;
    677 
    678 		/*
    679 		 * Count up the space of the user's arguments, and
    680 		 * subtract that from what we allocate.
    681 		 */
    682 		for (argv = *argvp, c = 0, cnt = 0;
    683 		     argv < ap;
    684 		     ++argv, ++cnt) {
    685 			c += strlen(*argv) + 1;
    686 			new->e_argv[cnt] = *argv;
    687 		}
    688 		bufsize = ARG_MAX - 4 * 1024 - c;
    689 
    690 
    691 		/*
    692 		 * Allocate, and then initialize current, base, and
    693 		 * end pointers.
    694 		 */
    695 		new->ep_p = new->ep_bbp = malloc(bufsize + 1);
    696 		new->ep_ebp = new->ep_bbp + bufsize - 1;
    697 		new->ep_rval = 0;
    698 	} else { /* !F_PLUSSET */
    699 		cnt = ap - *argvp + 1;
    700 		new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
    701 		new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
    702 		new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
    703 
    704 		for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
    705 			new->e_orig[cnt] = *argv;
    706 			for (p = *argv; *p; ++p)
    707 				if (p[0] == '{' && p[1] == '}') {
    708 					new->e_argv[cnt] =
    709 						emalloc((u_int)MAXPATHLEN);
    710 					new->e_len[cnt] = MAXPATHLEN;
    711 					break;
    712 				}
    713 			if (!*p) {
    714 				new->e_argv[cnt] = *argv;
    715 				new->e_len[cnt] = 0;
    716 			}
    717 		}
    718 		new->e_orig[cnt] = NULL;
    719 	}
    720 
    721 	new->e_argv[cnt] = NULL;
    722 	*argvp = argv + 1;
    723 	return (new);
    724 }
    725 
    726 /*
    727  * -execdir utility [arg ... ] ; functions --
    728  *
    729  *	True if the executed utility returns a zero value as exit status.
    730  *	The end of the primary expression is delimited by a semicolon.  If
    731  *	"{}" occurs anywhere, it gets replaced by the unqualified pathname.
    732  *	The current directory for the execution of utility is the same as
    733  *	the directory where the file lives.
    734  */
    735 int
    736 f_execdir(PLAN *plan, FTSENT *entry)
    737 {
    738 	int cnt;
    739 	pid_t pid;
    740 	int status;
    741 	char *file;
    742 
    743 	/* XXX - if file/dir ends in '/' this will not work -- can it? */
    744 	if ((file = strrchr(entry->fts_path, '/')))
    745 		file++;
    746 	else
    747 		file = entry->fts_path;
    748 
    749 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
    750 		if (plan->e_len[cnt])
    751 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
    752 			    file, &plan->e_len[cnt]);
    753 
    754 	/* don't mix output of command with find output */
    755 	fflush(stdout);
    756 	fflush(stderr);
    757 
    758 	switch (pid = vfork()) {
    759 	case -1:
    760 		err(1, "fork");
    761 		/* NOTREACHED */
    762 	case 0:
    763 		execvp(plan->e_argv[0], plan->e_argv);
    764 		warn("%s", plan->e_argv[0]);
    765 		_exit(1);
    766 	}
    767 	pid = waitpid(pid, &status, 0);
    768 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
    769 }
    770 
    771 /*
    772  * c_execdir --
    773  *	build three parallel arrays, one with pointers to the strings passed
    774  *	on the command line, one with (possibly duplicated) pointers to the
    775  *	argv array, and one with integer values that are lengths of the
    776  *	strings, but also flags meaning that the string has to be massaged.
    777  */
    778 PLAN *
    779 c_execdir(char ***argvp, int isok)
    780 {
    781 	PLAN *new;			/* node returned */
    782 	int cnt;
    783 	char **argv, **ap, *p;
    784 
    785 	ftsoptions &= ~FTS_NOSTAT;
    786 	isoutput = 1;
    787 
    788 	new = palloc(N_EXECDIR, f_execdir);
    789 
    790 	for (ap = argv = *argvp;; ++ap) {
    791 		if (!*ap)
    792 			errx(1,
    793 			    "-execdir: no terminating \";\"");
    794 		if (**ap == ';')
    795 			break;
    796 	}
    797 
    798 	cnt = ap - *argvp + 1;
    799 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
    800 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
    801 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
    802 
    803 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
    804 		new->e_orig[cnt] = *argv;
    805 		for (p = *argv; *p; ++p)
    806 			if (p[0] == '{' && p[1] == '}') {
    807 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
    808 				new->e_len[cnt] = MAXPATHLEN;
    809 				break;
    810 			}
    811 		if (!*p) {
    812 			new->e_argv[cnt] = *argv;
    813 			new->e_len[cnt] = 0;
    814 		}
    815 	}
    816 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
    817 
    818 	*argvp = argv + 1;
    819 	return (new);
    820 }
    821 
    822 PLAN *
    823 c_exit(char ***argvp, int isok)
    824 {
    825 	char *arg = **argvp;
    826 	PLAN *new;
    827 
    828 	/* not technically true, but otherwise '-print' is implied */
    829 	isoutput = 1;
    830 
    831 	new = palloc(N_EXIT, f_always_true);
    832 
    833 	if (arg) {
    834 		(*argvp)++;
    835 		new->exit_val = find_parsenum(new, "-exit", arg, NULL);
    836 	} else
    837 		new->exit_val = 0;
    838 
    839 	return (new);
    840 }
    841 
    842 
    843 /*
    844  * -false function
    845  */
    846 int
    847 f_false(PLAN *plan, FTSENT *entry)
    848 {
    849 
    850 	return (0);
    851 }
    852 
    853 PLAN *
    854 c_false(char ***argvp, int isok)
    855 {
    856 	return (palloc(N_FALSE, f_false));
    857 }
    858 
    859 
    860 /*
    861  * -flags [-]flags functions --
    862  */
    863 int
    864 f_flags(PLAN *plan, FTSENT *entry)
    865 {
    866 	u_int32_t flags;
    867 
    868 	flags = entry->fts_statp->st_flags;
    869 	if (plan->flags == F_ATLEAST)
    870 		return ((plan->f_data | flags) == flags);
    871 	else
    872 		return (flags == plan->f_data);
    873 	/* NOTREACHED */
    874 }
    875 
    876 PLAN *
    877 c_flags(char ***argvp, int isok)
    878 {
    879 	char *flags = **argvp;
    880 	PLAN *new;
    881 	u_long flagset;
    882 
    883 	(*argvp)++;
    884 	ftsoptions &= ~FTS_NOSTAT;
    885 
    886 	new = palloc(N_FLAGS, f_flags);
    887 
    888 	if (*flags == '-') {
    889 		new->flags = F_ATLEAST;
    890 		++flags;
    891 	}
    892 
    893 	flagset = 0;
    894 	if ((strcmp(flags, "none") != 0) &&
    895 	    (string_to_flags(&flags, &flagset, NULL) != 0))
    896 		errx(1, "-flags: %s: illegal flags string", flags);
    897 	new->f_data = flagset;
    898 	return (new);
    899 }
    900 
    901 /*
    902  * -follow functions --
    903  *
    904  *	Always true, causes symbolic links to be followed on a global
    905  *	basis.
    906  */
    907 PLAN *
    908 c_follow(char ***argvp, int isok)
    909 {
    910 	ftsoptions &= ~FTS_PHYSICAL;
    911 	ftsoptions |= FTS_LOGICAL;
    912 
    913 	return (palloc(N_FOLLOW, f_always_true));
    914 }
    915 
    916 /* -fprint functions --
    917  *
    918  *	Causes the current pathame to be written to the defined output file.
    919  */
    920 int
    921 f_fprint(PLAN *plan, FTSENT *entry)
    922 {
    923 
    924 	if (-1 == fprintf(plan->fprint_file, "%s\n", entry->fts_path))
    925 		warn("fprintf");
    926 
    927 	return(1);
    928 
    929 	/* no descriptors are closed; they will be closed by
    930 	   operating system when this find command exits.  */
    931 }
    932 
    933 PLAN *
    934 c_fprint(char ***argvp, int isok)
    935 {
    936 	PLAN *new;
    937 
    938 	isoutput = 1; /* do not assume -print */
    939 
    940 	new = palloc(N_FPRINT, f_fprint);
    941 
    942 	if (NULL == (new->fprint_file = fopen(**argvp, "w")))
    943 		err(1, "-fprint: %s: cannot create file", **argvp);
    944 
    945 	(*argvp)++;
    946 	return (new);
    947 }
    948 
    949 /*
    950  * -fstype functions --
    951  *
    952  *	True if the file is of a certain type.
    953  */
    954 int
    955 f_fstype(PLAN *plan, FTSENT *entry)
    956 {
    957 	static dev_t curdev;	/* need a guaranteed illegal dev value */
    958 	static int first = 1;
    959 	struct statvfs sb;
    960 	static short val;
    961 	static char fstype[sizeof(sb.f_fstypename)];
    962 	char *p, save[2];
    963 
    964 	memset(&save, 0, sizeof save);	/* XXX gcc */
    965 
    966 	/* Only check when we cross mount point. */
    967 	if (first || curdev != entry->fts_statp->st_dev) {
    968 		curdev = entry->fts_statp->st_dev;
    969 
    970 		/*
    971 		 * Statfs follows symlinks; find wants the link's file system,
    972 		 * not where it points.
    973 		 */
    974 		if (entry->fts_info == FTS_SL ||
    975 		    entry->fts_info == FTS_SLNONE) {
    976 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
    977 				++p;
    978 			else
    979 				p = entry->fts_accpath;
    980 			save[0] = p[0];
    981 			p[0] = '.';
    982 			save[1] = p[1];
    983 			p[1] = '\0';
    984 
    985 		} else
    986 			p = NULL;
    987 
    988 		if (statvfs(entry->fts_accpath, &sb))
    989 			err(1, "%s", entry->fts_accpath);
    990 
    991 		if (p) {
    992 			p[0] = save[0];
    993 			p[1] = save[1];
    994 		}
    995 
    996 		first = 0;
    997 
    998 		/*
    999 		 * Further tests may need both of these values, so
   1000 		 * always copy both of them.
   1001 		 */
   1002 		val = sb.f_flag;
   1003 		strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
   1004 	}
   1005 	switch (plan->flags) {
   1006 	case F_MTFLAG:
   1007 		return (val & plan->mt_data);
   1008 	case F_MTTYPE:
   1009 		return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
   1010 	default:
   1011 		abort();
   1012 	}
   1013 }
   1014 
   1015 PLAN *
   1016 c_fstype(char ***argvp, int isok)
   1017 {
   1018 	char *arg = **argvp;
   1019 	PLAN *new;
   1020 
   1021 	(*argvp)++;
   1022 	ftsoptions &= ~FTS_NOSTAT;
   1023 
   1024 	new = palloc(N_FSTYPE, f_fstype);
   1025 
   1026 	switch (*arg) {
   1027 	case 'l':
   1028 		if (!strcmp(arg, "local")) {
   1029 			new->flags = F_MTFLAG;
   1030 			new->mt_data = MNT_LOCAL;
   1031 			return (new);
   1032 		}
   1033 		break;
   1034 	case 'r':
   1035 		if (!strcmp(arg, "rdonly")) {
   1036 			new->flags = F_MTFLAG;
   1037 			new->mt_data = MNT_RDONLY;
   1038 			return (new);
   1039 		}
   1040 		break;
   1041 	}
   1042 
   1043 	new->flags = F_MTTYPE;
   1044 	new->c_data = arg;
   1045 	return (new);
   1046 }
   1047 
   1048 /*
   1049  * -group gname functions --
   1050  *
   1051  *	True if the file belongs to the group gname.  If gname is numeric and
   1052  *	an equivalent of the getgrnam() function does not return a valid group
   1053  *	name, gname is taken as a group ID.
   1054  */
   1055 int
   1056 f_group(PLAN *plan, FTSENT *entry)
   1057 {
   1058 
   1059 	return (entry->fts_statp->st_gid == plan->g_data);
   1060 }
   1061 
   1062 PLAN *
   1063 c_group(char ***argvp, int isok)
   1064 {
   1065 	char *gname = **argvp;
   1066 	PLAN *new;
   1067 	struct group *g;
   1068 	gid_t gid;
   1069 
   1070 	(*argvp)++;
   1071 	ftsoptions &= ~FTS_NOSTAT;
   1072 
   1073 	g = getgrnam(gname);
   1074 	if (g == NULL) {
   1075 		gid = atoi(gname);
   1076 		if (gid == 0 && gname[0] != '0')
   1077 			errx(1, "-group: %s: no such group", gname);
   1078 	} else
   1079 		gid = g->gr_gid;
   1080 
   1081 	new = palloc(N_GROUP, f_group);
   1082 	new->g_data = gid;
   1083 	return (new);
   1084 }
   1085 
   1086 /*
   1087  * -inum n functions --
   1088  *
   1089  *	True if the file has inode # n.
   1090  */
   1091 int
   1092 f_inum(PLAN *plan, FTSENT *entry)
   1093 {
   1094 
   1095 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
   1096 }
   1097 
   1098 PLAN *
   1099 c_inum(char ***argvp, int isok)
   1100 {
   1101 	char *arg = **argvp;
   1102 	PLAN *new;
   1103 
   1104 	(*argvp)++;
   1105 	ftsoptions &= ~FTS_NOSTAT;
   1106 
   1107 	new = palloc(N_INUM, f_inum);
   1108 	new->i_data = find_parsenum(new, "-inum", arg, NULL);
   1109 	return (new);
   1110 }
   1111 
   1112 /*
   1113  * -links n functions --
   1114  *
   1115  *	True if the file has n links.
   1116  */
   1117 int
   1118 f_links(PLAN *plan, FTSENT *entry)
   1119 {
   1120 
   1121 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
   1122 }
   1123 
   1124 PLAN *
   1125 c_links(char ***argvp, int isok)
   1126 {
   1127 	char *arg = **argvp;
   1128 	PLAN *new;
   1129 
   1130 	(*argvp)++;
   1131 	ftsoptions &= ~FTS_NOSTAT;
   1132 
   1133 	new = palloc(N_LINKS, f_links);
   1134 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
   1135 	return (new);
   1136 }
   1137 
   1138 /*
   1139  * -ls functions --
   1140  *
   1141  *	Always true - prints the current entry to stdout in "ls" format.
   1142  */
   1143 int
   1144 f_ls(PLAN *plan, FTSENT *entry)
   1145 {
   1146 
   1147 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
   1148 	return (1);
   1149 }
   1150 
   1151 PLAN *
   1152 c_ls(char ***argvp, int isok)
   1153 {
   1154 
   1155 	ftsoptions &= ~FTS_NOSTAT;
   1156 	isoutput = 1;
   1157 
   1158 	return (palloc(N_LS, f_ls));
   1159 }
   1160 
   1161 /*
   1162  * - maxdepth n functions --
   1163  *
   1164  *	True if the current search depth is less than or equal to the
   1165  *	maximum depth specified
   1166  */
   1167 int
   1168 f_maxdepth(PLAN *plan, FTSENT *entry)
   1169 {
   1170 	extern FTS *tree;
   1171 
   1172 	if (entry->fts_level >= plan->max_data)
   1173 		fts_set(tree, entry, FTS_SKIP);
   1174 	return (entry->fts_level <= plan->max_data);
   1175 }
   1176 
   1177 PLAN *
   1178 c_maxdepth(char ***argvp, int isok)
   1179 {
   1180 	char *arg = **argvp;
   1181 	PLAN *new;
   1182 
   1183 	(*argvp)++;
   1184 	new = palloc(N_MAXDEPTH, f_maxdepth);
   1185 	new->max_data = atoi(arg);
   1186 	return (new);
   1187 }
   1188 
   1189 /*
   1190  * - mindepth n functions --
   1191  *
   1192  *	True if the current search depth is greater than or equal to the
   1193  *	minimum depth specified
   1194  */
   1195 int
   1196 f_mindepth(PLAN *plan, FTSENT *entry)
   1197 {
   1198 	return (entry->fts_level >= plan->min_data);
   1199 }
   1200 
   1201 PLAN *
   1202 c_mindepth(char ***argvp, int isok)
   1203 {
   1204 	char *arg = **argvp;
   1205 	PLAN *new;
   1206 
   1207 	(*argvp)++;
   1208 	new = palloc(N_MINDEPTH, f_mindepth);
   1209 	new->min_data = atoi(arg);
   1210 	return (new);
   1211 }
   1212 /*
   1213  * -mmin n functions --
   1214  *
   1215  *	True if the difference between the file modification time and the
   1216  *	current time is n 24 hour periods.
   1217  */
   1218 int
   1219 f_mmin(PLAN *plan, FTSENT *entry)
   1220 {
   1221 	COMPARE((now - entry->fts_statp->st_mtime + SECSPERMIN - 1) /
   1222 	    SECSPERMIN, plan->t_data);
   1223 }
   1224 
   1225 PLAN *
   1226 c_mmin(char ***argvp, int isok)
   1227 {
   1228 	char *arg = **argvp;
   1229 	PLAN *new;
   1230 
   1231 	(*argvp)++;
   1232 	ftsoptions &= ~FTS_NOSTAT;
   1233 
   1234 	new = palloc(N_MMIN, f_mmin);
   1235 	new->t_data = find_parsenum(new, "-mmin", arg, NULL);
   1236 	TIME_CORRECT(new, N_MMIN);
   1237 	return (new);
   1238 }
   1239 /*
   1240  * -mtime n functions --
   1241  *
   1242  *	True if the difference between the file modification time and the
   1243  *	current time is n 24 hour periods.
   1244  */
   1245 int
   1246 f_mtime(PLAN *plan, FTSENT *entry)
   1247 {
   1248 	COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
   1249 	    SECSPERDAY, plan->t_data);
   1250 }
   1251 
   1252 PLAN *
   1253 c_mtime(char ***argvp, int isok)
   1254 {
   1255 	char *arg = **argvp;
   1256 	PLAN *new;
   1257 
   1258 	(*argvp)++;
   1259 	ftsoptions &= ~FTS_NOSTAT;
   1260 
   1261 	new = palloc(N_MTIME, f_mtime);
   1262 	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
   1263 	TIME_CORRECT(new, N_MTIME);
   1264 	return (new);
   1265 }
   1266 
   1267 /*
   1268  * -name functions --
   1269  *
   1270  *	True if the basename of the filename being examined
   1271  *	matches pattern using Pattern Matching Notation S3.14
   1272  */
   1273 int
   1274 f_name(PLAN *plan, FTSENT *entry)
   1275 {
   1276 
   1277 	return (!fnmatch(plan->c_data, entry->fts_name, 0));
   1278 }
   1279 
   1280 PLAN *
   1281 c_name(char ***argvp, int isok)
   1282 {
   1283 	char *pattern = **argvp;
   1284 	PLAN *new;
   1285 
   1286 	(*argvp)++;
   1287 	new = palloc(N_NAME, f_name);
   1288 	new->c_data = pattern;
   1289 	return (new);
   1290 }
   1291 
   1292 /*
   1293  * -iname functions --
   1294  *
   1295  *	Similar to -name, but does case insensitive matching
   1296  *
   1297  */
   1298 int
   1299 f_iname(PLAN *plan, FTSENT *entry)
   1300 {
   1301 	return (!fnmatch(plan->c_data, entry->fts_name, FNM_CASEFOLD));
   1302 }
   1303 
   1304 PLAN *
   1305 c_iname(char ***argvp, int isok)
   1306 {
   1307 	char *pattern = **argvp;
   1308 	PLAN *new;
   1309 
   1310 	(*argvp)++;
   1311 	new = palloc(N_INAME, f_iname);
   1312 	new->c_data = pattern;
   1313 	return (new);
   1314 }
   1315 
   1316 /*
   1317  * -newer file functions --
   1318  *
   1319  *	True if the current file has been modified more recently
   1320  *	than the modification time of the file named by the pathname
   1321  *	file.
   1322  */
   1323 int
   1324 f_newer(PLAN *plan, FTSENT *entry)
   1325 {
   1326 
   1327 	return (entry->fts_statp->st_mtime > plan->t_data);
   1328 }
   1329 
   1330 PLAN *
   1331 c_newer(char ***argvp, int isok)
   1332 {
   1333 	char *filename = **argvp;
   1334 	PLAN *new;
   1335 	struct stat sb;
   1336 
   1337 	(*argvp)++;
   1338 	ftsoptions &= ~FTS_NOSTAT;
   1339 
   1340 	if (stat(filename, &sb))
   1341 		err(1, "%s", filename);
   1342 	new = palloc(N_NEWER, f_newer);
   1343 	new->t_data = sb.st_mtime;
   1344 	return (new);
   1345 }
   1346 
   1347 /*
   1348  * -nogroup functions --
   1349  *
   1350  *	True if file belongs to a user ID for which the equivalent
   1351  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
   1352  */
   1353 int
   1354 f_nogroup(PLAN *plan, FTSENT *entry)
   1355 {
   1356 
   1357 	return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
   1358 }
   1359 
   1360 PLAN *
   1361 c_nogroup(char ***argvp, int isok)
   1362 {
   1363 	ftsoptions &= ~FTS_NOSTAT;
   1364 
   1365 	return (palloc(N_NOGROUP, f_nogroup));
   1366 }
   1367 
   1368 /*
   1369  * -nouser functions --
   1370  *
   1371  *	True if file belongs to a user ID for which the equivalent
   1372  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
   1373  */
   1374 int
   1375 f_nouser(PLAN *plan, FTSENT *entry)
   1376 {
   1377 
   1378 	return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
   1379 }
   1380 
   1381 PLAN *
   1382 c_nouser(char ***argvp, int isok)
   1383 {
   1384 	ftsoptions &= ~FTS_NOSTAT;
   1385 
   1386 	return (palloc(N_NOUSER, f_nouser));
   1387 }
   1388 
   1389 /*
   1390  * -path functions --
   1391  *
   1392  *	True if the path of the filename being examined
   1393  *	matches pattern using Pattern Matching Notation S3.14
   1394  */
   1395 int
   1396 f_path(PLAN *plan, FTSENT *entry)
   1397 {
   1398 
   1399 	return (!fnmatch(plan->c_data, entry->fts_path, 0));
   1400 }
   1401 
   1402 PLAN *
   1403 c_path(char ***argvp, int isok)
   1404 {
   1405 	char *pattern = **argvp;
   1406 	PLAN *new;
   1407 
   1408 	(*argvp)++;
   1409 	new = palloc(N_NAME, f_path);
   1410 	new->c_data = pattern;
   1411 	return (new);
   1412 }
   1413 
   1414 /*
   1415  * -perm functions --
   1416  *
   1417  *	The mode argument is used to represent file mode bits.  If it starts
   1418  *	with a leading digit, it's treated as an octal mode, otherwise as a
   1419  *	symbolic mode.
   1420  */
   1421 int
   1422 f_perm(PLAN *plan, FTSENT *entry)
   1423 {
   1424 	mode_t mode;
   1425 
   1426 	mode = entry->fts_statp->st_mode &
   1427 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
   1428 	if (plan->flags == F_ATLEAST)
   1429 		return ((plan->m_data | mode) == mode);
   1430 	else
   1431 		return (mode == plan->m_data);
   1432 	/* NOTREACHED */
   1433 }
   1434 
   1435 PLAN *
   1436 c_perm(char ***argvp, int isok)
   1437 {
   1438 	char *perm = **argvp;
   1439 	PLAN *new;
   1440 	mode_t *set;
   1441 
   1442 	(*argvp)++;
   1443 	ftsoptions &= ~FTS_NOSTAT;
   1444 
   1445 	new = palloc(N_PERM, f_perm);
   1446 
   1447 	if (*perm == '-') {
   1448 		new->flags = F_ATLEAST;
   1449 		++perm;
   1450 	}
   1451 
   1452 	if ((set = setmode(perm)) == NULL)
   1453 		err(1, "-perm: Cannot set file mode `%s'", perm);
   1454 
   1455 	new->m_data = getmode(set, 0);
   1456 	free(set);
   1457 	return (new);
   1458 }
   1459 
   1460 /*
   1461  * -print functions --
   1462  *
   1463  *	Always true, causes the current pathame to be written to
   1464  *	standard output.
   1465  */
   1466 int
   1467 f_print(PLAN *plan, FTSENT *entry)
   1468 {
   1469 
   1470 	(void)printf("%s\n", entry->fts_path);
   1471 	return (1);
   1472 }
   1473 
   1474 int
   1475 f_print0(PLAN *plan, FTSENT *entry)
   1476 {
   1477 
   1478 	(void)fputs(entry->fts_path, stdout);
   1479 	(void)fputc('\0', stdout);
   1480 	return (1);
   1481 }
   1482 
   1483 int
   1484 f_printx(PLAN *plan, FTSENT *entry)
   1485 {
   1486 	char *cp;
   1487 
   1488 	for (cp = entry->fts_path; *cp; cp++) {
   1489 		if (*cp == '\'' || *cp == '\"' || *cp == ' ' ||
   1490 		    *cp == '$'  || *cp == '`'  ||
   1491 		    *cp == '\t' || *cp == '\n' || *cp == '\\')
   1492 			fputc('\\', stdout);
   1493 
   1494 		fputc(*cp, stdout);
   1495 	}
   1496 
   1497 	fputc('\n', stdout);
   1498 	return (1);
   1499 }
   1500 
   1501 PLAN *
   1502 c_print(char ***argvp, int isok)
   1503 {
   1504 
   1505 	isoutput = 1;
   1506 
   1507 	return (palloc(N_PRINT, f_print));
   1508 }
   1509 
   1510 PLAN *
   1511 c_print0(char ***argvp, int isok)
   1512 {
   1513 
   1514 	isoutput = 1;
   1515 
   1516 	return (palloc(N_PRINT0, f_print0));
   1517 }
   1518 
   1519 PLAN *
   1520 c_printx(char ***argvp, int isok)
   1521 {
   1522 
   1523 	isoutput = 1;
   1524 
   1525 	return (palloc(N_PRINTX, f_printx));
   1526 }
   1527 
   1528 /*
   1529  * -prune functions --
   1530  *
   1531  *	Prune a portion of the hierarchy.
   1532  */
   1533 int
   1534 f_prune(PLAN *plan, FTSENT *entry)
   1535 {
   1536 	if (fts_set(tree, entry, FTS_SKIP))
   1537 		err(1, "%s", entry->fts_path);
   1538 	return (1);
   1539 }
   1540 
   1541 PLAN *
   1542 c_prune(char ***argvp, int isok)
   1543 {
   1544 
   1545 	return (palloc(N_PRUNE, f_prune));
   1546 }
   1547 
   1548 /*
   1549  * -regex regexp (and related) functions --
   1550  *
   1551  *	True if the complete file path matches the regular expression regexp.
   1552  *	For -regex, regexp is a case-sensitive (basic) regular expression.
   1553  *	For -iregex, regexp is a case-insensitive (basic) regular expression.
   1554  */
   1555 int
   1556 f_regex(PLAN *plan, FTSENT *entry)
   1557 {
   1558 
   1559 	return (regexec(&plan->regexp_data, entry->fts_path, 0, NULL, 0) == 0);
   1560 }
   1561 
   1562 static PLAN *
   1563 c_regex_common(char ***argvp, int isok, enum ntype type, bool icase)
   1564 {
   1565 	char errbuf[LINE_MAX];
   1566 	regex_t reg;
   1567 	char *regexp = **argvp;
   1568 	char *lineregexp;
   1569 	PLAN *new;
   1570 	int rv;
   1571 	size_t len;
   1572 
   1573 	(*argvp)++;
   1574 
   1575 	len = strlen(regexp) + 1 + 6;
   1576 	lineregexp = malloc(len);	/* max needed */
   1577 	if (lineregexp == NULL)
   1578 		err(1, NULL);
   1579 	snprintf(lineregexp, len, "^%s(%s%s)$",
   1580 	    (regcomp_flags & REG_EXTENDED) ? "" : "\\", regexp,
   1581 	    (regcomp_flags & REG_EXTENDED) ? "" : "\\");
   1582 	rv = regcomp(&reg, lineregexp, REG_NOSUB|regcomp_flags|
   1583 	    (icase ? REG_ICASE : 0));
   1584 	free(lineregexp);
   1585 	if (rv != 0) {
   1586 		regerror(rv, &reg, errbuf, sizeof errbuf);
   1587 		errx(1, "regexp %s: %s", regexp, errbuf);
   1588 	}
   1589 
   1590 	new = palloc(type, f_regex);
   1591 	new->regexp_data = reg;
   1592 	return (new);
   1593 }
   1594 
   1595 PLAN *
   1596 c_regex(char ***argvp, int isok)
   1597 {
   1598 
   1599 	return (c_regex_common(argvp, isok, N_REGEX, false));
   1600 }
   1601 
   1602 PLAN *
   1603 c_iregex(char ***argvp, int isok)
   1604 {
   1605 
   1606 	return (c_regex_common(argvp, isok, N_IREGEX, true));
   1607 }
   1608 
   1609 /*
   1610  * -size n[c] functions --
   1611  *
   1612  *	True if the file size in bytes, divided by an implementation defined
   1613  *	value and rounded up to the next integer, is n.  If n is followed by
   1614  *	a c, the size is in bytes.
   1615  */
   1616 #define	FIND_SIZE	512
   1617 static int divsize = 1;
   1618 
   1619 int
   1620 f_size(PLAN *plan, FTSENT *entry)
   1621 {
   1622 	off_t size;
   1623 
   1624 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
   1625 	    FIND_SIZE : entry->fts_statp->st_size;
   1626 	COMPARE(size, plan->o_data);
   1627 }
   1628 
   1629 PLAN *
   1630 c_size(char ***argvp, int isok)
   1631 {
   1632 	char *arg = **argvp;
   1633 	PLAN *new;
   1634 	char endch;
   1635 
   1636 	(*argvp)++;
   1637 	ftsoptions &= ~FTS_NOSTAT;
   1638 
   1639 	new = palloc(N_SIZE, f_size);
   1640 	endch = 'c';
   1641 	new->o_data = find_parsenum(new, "-size", arg, &endch);
   1642 	if (endch == 'c')
   1643 		divsize = 0;
   1644 	return (new);
   1645 }
   1646 
   1647 /*
   1648  * -type c functions --
   1649  *
   1650  *	True if the type of the file is c, where c is b, c, d, p, f or w
   1651  *	for block special file, character special file, directory, FIFO,
   1652  *	regular file or whiteout respectively.
   1653  */
   1654 int
   1655 f_type(PLAN *plan, FTSENT *entry)
   1656 {
   1657 
   1658 	return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
   1659 }
   1660 
   1661 PLAN *
   1662 c_type(char ***argvp, int isok)
   1663 {
   1664 	char *typestring = **argvp;
   1665 	PLAN *new;
   1666 	mode_t  mask = (mode_t)0;
   1667 
   1668 	(*argvp)++;
   1669 	ftsoptions &= ~FTS_NOSTAT;
   1670 
   1671 	switch (typestring[0]) {
   1672 	case 'b':
   1673 		mask = S_IFBLK;
   1674 		break;
   1675 	case 'c':
   1676 		mask = S_IFCHR;
   1677 		break;
   1678 	case 'd':
   1679 		mask = S_IFDIR;
   1680 		break;
   1681 	case 'f':
   1682 		mask = S_IFREG;
   1683 		break;
   1684 	case 'l':
   1685 		mask = S_IFLNK;
   1686 		break;
   1687 	case 'p':
   1688 		mask = S_IFIFO;
   1689 		break;
   1690 	case 's':
   1691 		mask = S_IFSOCK;
   1692 		break;
   1693 #ifdef S_IFWHT
   1694 	case 'W':
   1695 	case 'w':
   1696 		mask = S_IFWHT;
   1697 #ifdef FTS_WHITEOUT
   1698 		ftsoptions |= FTS_WHITEOUT;
   1699 #endif
   1700 		break;
   1701 #endif /* S_IFWHT */
   1702 	default:
   1703 		errx(1, "-type: %s: unknown type", typestring);
   1704 	}
   1705 
   1706 	new = palloc(N_TYPE, f_type);
   1707 	new->m_data = mask;
   1708 	return (new);
   1709 }
   1710 
   1711 /*
   1712  * -user uname functions --
   1713  *
   1714  *	True if the file belongs to the user uname.  If uname is numeric and
   1715  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
   1716  *	return a valid user name, uname is taken as a user ID.
   1717  */
   1718 int
   1719 f_user(PLAN *plan, FTSENT *entry)
   1720 {
   1721 
   1722 	COMPARE(entry->fts_statp->st_uid, plan->u_data);
   1723 }
   1724 
   1725 PLAN *
   1726 c_user(char ***argvp, int isok)
   1727 {
   1728 	char *username = **argvp;
   1729 	PLAN *new;
   1730 	struct passwd *p;
   1731 	uid_t uid;
   1732 
   1733 	(*argvp)++;
   1734 	ftsoptions &= ~FTS_NOSTAT;
   1735 
   1736 	new = palloc(N_USER, f_user);
   1737 	p = getpwnam(username);
   1738 	if (p == NULL) {
   1739 		if (atoi(username) == 0 && username[0] != '0' &&
   1740 		    strcmp(username, "+0") && strcmp(username, "-0"))
   1741 			errx(1, "-user: %s: no such user", username);
   1742 		uid = find_parsenum(new, "-user", username, NULL);
   1743 
   1744 	} else {
   1745 		new->flags = F_EQUAL;
   1746 		uid = p->pw_uid;
   1747 	}
   1748 
   1749 	new->u_data = uid;
   1750 	return (new);
   1751 }
   1752 
   1753 /*
   1754  * -xdev functions --
   1755  *
   1756  *	Always true, causes find not to descend past directories that have a
   1757  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
   1758  */
   1759 PLAN *
   1760 c_xdev(char ***argvp, int isok)
   1761 {
   1762 	ftsoptions |= FTS_XDEV;
   1763 
   1764 	return (palloc(N_XDEV, f_always_true));
   1765 }
   1766 
   1767 /*
   1768  * ( expression ) functions --
   1769  *
   1770  *	True if expression is true.
   1771  */
   1772 int
   1773 f_expr(PLAN *plan, FTSENT *entry)
   1774 {
   1775 	PLAN *p;
   1776 	int state;
   1777 
   1778 	state = 0;
   1779 	for (p = plan->p_data[0];
   1780 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1781 	return (state);
   1782 }
   1783 
   1784 /*
   1785  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
   1786  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
   1787  * to a N_EXPR node containing the expression and the ')' node is discarded.
   1788  */
   1789 PLAN *
   1790 c_openparen(char ***argvp, int isok)
   1791 {
   1792 
   1793 	return (palloc(N_OPENPAREN, (int (*)(PLAN *, FTSENT *))-1));
   1794 }
   1795 
   1796 PLAN *
   1797 c_closeparen(char ***argvp, int isok)
   1798 {
   1799 
   1800 	return (palloc(N_CLOSEPAREN, (int (*)(PLAN *, FTSENT *))-1));
   1801 }
   1802 
   1803 /*
   1804  * ! expression functions --
   1805  *
   1806  *	Negation of a primary; the unary NOT operator.
   1807  */
   1808 int
   1809 f_not(PLAN *plan, FTSENT *entry)
   1810 {
   1811 	PLAN *p;
   1812 	int state;
   1813 
   1814 	state = 0;
   1815 	for (p = plan->p_data[0];
   1816 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1817 	return (!state);
   1818 }
   1819 
   1820 PLAN *
   1821 c_not(char ***argvp, int isok)
   1822 {
   1823 
   1824 	return (palloc(N_NOT, f_not));
   1825 }
   1826 
   1827 /*
   1828  * expression -o expression functions --
   1829  *
   1830  *	Alternation of primaries; the OR operator.  The second expression is
   1831  * not evaluated if the first expression is true.
   1832  */
   1833 int
   1834 f_or(PLAN *plan, FTSENT *entry)
   1835 {
   1836 	PLAN *p;
   1837 	int state;
   1838 
   1839 	state = 0;
   1840 	for (p = plan->p_data[0];
   1841 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1842 
   1843 	if (state)
   1844 		return (1);
   1845 
   1846 	for (p = plan->p_data[1];
   1847 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1848 	return (state);
   1849 }
   1850 
   1851 PLAN *
   1852 c_or(char ***argvp, int isok)
   1853 {
   1854 
   1855 	return (palloc(N_OR, f_or));
   1856 }
   1857 
   1858 PLAN *
   1859 c_null(char ***argvp, int isok)
   1860 {
   1861 
   1862 	return (NULL);
   1863 }
   1864 
   1865 
   1866 /*
   1867  * plan_cleanup --
   1868  *	Check and see if the specified plan has any residual state,
   1869  *	and if so, clean it up as appropriate.
   1870  *
   1871  *	At the moment, only N_EXEC has state. Two kinds: 1)
   1872  * 	lists of files to feed to subprocesses 2) State on exit
   1873  *	statusses of past subprocesses.
   1874  */
   1875 /* ARGSUSED1 */
   1876 int
   1877 plan_cleanup(PLAN *plan, void *arg)
   1878 {
   1879 	if (plan->type==N_EXEC && plan->ep_narg)
   1880 		run_f_exec(plan);
   1881 
   1882 	return plan->ep_rval;		/* Passed save exit-status up chain */
   1883 }
   1884 
   1885 static PLAN *
   1886 palloc(enum ntype t, int (*f)(PLAN *, FTSENT *))
   1887 {
   1888 	PLAN *new;
   1889 
   1890 	if ((new = malloc(sizeof(PLAN))) == NULL)
   1891 		err(1, NULL);
   1892 	memset(new, 0, sizeof(PLAN));
   1893 	new->type = t;
   1894 	new->eval = f;
   1895 	return (new);
   1896 }
   1897