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