xref: /dflybsd-src/usr.bin/find/function.c (revision cf6a53ca558fa4bbc637ee3949e2436254bcf4c2)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Cimarron D. Taylor of the University of California, Berkeley.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#)function.c	8.10 (Berkeley) 5/4/95
33  * $FreeBSD: head/usr.bin/find/function.c 253886 2013-08-02 14:14:23Z jilles $
34  */
35 
36 #include <sys/param.h>
37 #include <sys/ucred.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 #include <sys/mount.h>
42 
43 #include <dirent.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fnmatch.h>
47 #include <fts.h>
48 #include <grp.h>
49 #include <limits.h>
50 #include <pwd.h>
51 #include <regex.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <ctype.h>
57 
58 #include "find.h"
59 
60 static PLAN *palloc(OPTION *);
61 static long long find_parsenum(PLAN *, const char *, char *, char *);
62 static long long find_parsetime(PLAN *, const char *, char *);
63 static char *nextarg(OPTION *, char ***);
64 
65 extern char **environ;
66 
67 static PLAN *lastexecplus = NULL;
68 
69 #define	COMPARE(a, b) do {						\
70 	switch (plan->flags & F_ELG_MASK) {				\
71 	case F_EQUAL:							\
72 		return (a == b);					\
73 	case F_LESSTHAN:						\
74 		return (a < b);						\
75 	case F_GREATER:							\
76 		return (a > b);						\
77 	default:							\
78 		abort();						\
79 	}								\
80 } while(0)
81 
82 static PLAN *
83 palloc(OPTION *option)
84 {
85 	PLAN *new;
86 
87 	if ((new = malloc(sizeof(PLAN))) == NULL)
88 		err(1, NULL);
89 	new->execute = option->execute;
90 	new->flags = option->flags;
91 	new->next = NULL;
92 	return new;
93 }
94 
95 /*
96  * find_parsenum --
97  *	Parse a string of the form [+-]# and return the value.
98  */
99 static long long
100 find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
101 {
102 	long long value;
103 	char *endchar, *str;	/* Pointer to character ending conversion. */
104 
105 	/* Determine comparison from leading + or -. */
106 	str = vp;
107 	switch (*str) {
108 	case '+':
109 		++str;
110 		plan->flags |= F_GREATER;
111 		break;
112 	case '-':
113 		++str;
114 		plan->flags |= F_LESSTHAN;
115 		break;
116 	default:
117 		plan->flags |= F_EQUAL;
118 		break;
119 	}
120 
121 	/*
122 	 * Convert the string with strtoq().  Note, if strtoq() returns zero
123 	 * and endchar points to the beginning of the string we know we have
124 	 * a syntax error.
125 	 */
126 	value = strtoq(str, &endchar, 10);
127 	if (value == 0 && endchar == str)
128 		errx(1, "%s: %s: illegal numeric value", option, vp);
129 	if (endchar[0] && endch == NULL)
130 		errx(1, "%s: %s: illegal trailing character", option, vp);
131 	if (endch)
132 		*endch = endchar[0];
133 	return value;
134 }
135 
136 /*
137  * find_parsetime --
138  *	Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
139  */
140 static long long
141 find_parsetime(PLAN *plan, const char *option, char *vp)
142 {
143 	long long secs, value;
144 	char *str, *unit;	/* Pointer to character ending conversion. */
145 
146 	/* Determine comparison from leading + or -. */
147 	str = vp;
148 	switch (*str) {
149 	case '+':
150 		++str;
151 		plan->flags |= F_GREATER;
152 		break;
153 	case '-':
154 		++str;
155 		plan->flags |= F_LESSTHAN;
156 		break;
157 	default:
158 		plan->flags |= F_EQUAL;
159 		break;
160 	}
161 
162 	value = strtoq(str, &unit, 10);
163 	if (value == 0 && unit == str) {
164 		errx(1, "%s: %s: illegal time value", option, vp);
165 		/* NOTREACHED */
166 	}
167 	if (*unit == '\0')
168 		return value;
169 
170 	/* Units syntax. */
171 	secs = 0;
172 	for (;;) {
173 		switch(*unit) {
174 		case 's':	/* seconds */
175 			secs += value;
176 			break;
177 		case 'm':	/* minutes */
178 			secs += value * 60;
179 			break;
180 		case 'h':	/* hours */
181 			secs += value * 3600;
182 			break;
183 		case 'd':	/* days */
184 			secs += value * 86400;
185 			break;
186 		case 'w':	/* weeks */
187 			secs += value * 604800;
188 			break;
189 		default:
190 			errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
191 			/* NOTREACHED */
192 		}
193 		str = unit + 1;
194 		if (*str == '\0')	/* EOS */
195 			break;
196 		value = strtoq(str, &unit, 10);
197 		if (value == 0 && unit == str) {
198 			errx(1, "%s: %s: illegal time value", option, vp);
199 			/* NOTREACHED */
200 		}
201 		if (*unit == '\0') {
202 			errx(1, "%s: %s: missing trailing unit", option, vp);
203 			/* NOTREACHED */
204 		}
205 	}
206 	plan->flags |= F_EXACTTIME;
207 	return secs;
208 }
209 
210 /*
211  * nextarg --
212  *	Check that another argument still exists, return a pointer to it,
213  *	and increment the argument vector pointer.
214  */
215 static char *
216 nextarg(OPTION *option, char ***argvp)
217 {
218 	char *arg;
219 
220 	if ((arg = **argvp) == NULL)
221 		errx(1, "%s: requires additional arguments", option->name);
222 	(*argvp)++;
223 	return arg;
224 } /* nextarg() */
225 
226 /*
227  * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
228  * range, i.e. n matches from (n - 1) to n 24 hour periods.  This interacts
229  * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
230  * the user wanted.  Correct so that -1 is "less than 1".
231  */
232 #define	TIME_CORRECT(p) \
233 	if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
234 		++((p)->t_data.tv_sec);
235 
236 /*
237  * -[acm]min n functions --
238  *
239  *    True if the difference between the
240  *		file access time (-amin)
241  *		file birth time (-Bmin)  (Not supported on DragonFly)
242  *		last change of file status information (-cmin)
243  *		file modification time (-mmin)
244  *    and the current time is n min periods.
245  */
246 int
247 f_Xmin(PLAN *plan, FTSENT *entry)
248 {
249 	if (plan->flags & F_TIME_C) {
250 		COMPARE((now - entry->fts_statp->st_ctime +
251 		    60 - 1) / 60, plan->t_data.tv_sec);
252 	} else if (plan->flags & F_TIME_A) {
253 		COMPARE((now - entry->fts_statp->st_atime +
254 		    60 - 1) / 60, plan->t_data.tv_sec);
255 	} else {
256 		COMPARE((now - entry->fts_statp->st_mtime +
257 		    60 - 1) / 60, plan->t_data.tv_sec);
258 	}
259 }
260 
261 PLAN *
262 c_Xmin(OPTION *option, char ***argvp)
263 {
264 	char *nmins;
265 	PLAN *new;
266 
267 	nmins = nextarg(option, argvp);
268 	ftsoptions &= ~FTS_NOSTAT;
269 
270 	new = palloc(option);
271 	new->t_data.tv_sec = find_parsenum(new, option->name, nmins, NULL);
272 	new->t_data.tv_nsec = 0;
273 	TIME_CORRECT(new);
274 	return new;
275 }
276 
277 /*
278  * -[acm]time n functions --
279  *
280  *	True if the difference between the
281  *		file access time (-atime)
282  *		file birth time (-Btime) (Not supported on DragonFly)
283  *		last change of file status information (-ctime)
284  *		file modification time (-mtime)
285  *	and the current time is n 24 hour periods.
286  */
287 
288 int
289 f_Xtime(PLAN *plan, FTSENT *entry)
290 {
291 	time_t xtime;
292 
293 	if (plan->flags & F_TIME_A)
294 		xtime = entry->fts_statp->st_atime;
295 	else if (plan->flags & F_TIME_C)
296 		xtime = entry->fts_statp->st_ctime;
297 	else
298 		xtime = entry->fts_statp->st_mtime;
299 
300 	if (plan->flags & F_EXACTTIME)
301 		COMPARE(now - xtime, plan->t_data.tv_sec);
302 	else
303 		COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data.tv_sec);
304 }
305 
306 PLAN *
307 c_Xtime(OPTION *option, char ***argvp)
308 {
309 	char *value;
310 	PLAN *new;
311 
312 	value = nextarg(option, argvp);
313 	ftsoptions &= ~FTS_NOSTAT;
314 
315 	new = palloc(option);
316 	new->t_data.tv_sec = find_parsetime(new, option->name, value);
317 	new->t_data.tv_nsec = 0;
318 	if (!(new->flags & F_EXACTTIME))
319 		TIME_CORRECT(new);
320 	return new;
321 }
322 
323 /*
324  * -maxdepth/-mindepth n functions --
325  *
326  *        Does the same as -prune if the level of the current file is
327  *        greater/less than the specified maximum/minimum depth.
328  *
329  *        Note that -maxdepth and -mindepth are handled specially in
330  *        find_execute() so their f_* functions are set to f_always_true().
331  */
332 PLAN *
333 c_mXXdepth(OPTION *option, char ***argvp)
334 {
335 	char *dstr;
336 	PLAN *new;
337 
338 	dstr = nextarg(option, argvp);
339 	if (dstr[0] == '-')
340 		/* all other errors handled by find_parsenum() */
341 		errx(1, "%s: %s: value must be positive", option->name, dstr);
342 
343 	new = palloc(option);
344 	if (option->flags & F_MAXDEPTH)
345 		maxdepth = find_parsenum(new, option->name, dstr, NULL);
346 	else
347 		mindepth = find_parsenum(new, option->name, dstr, NULL);
348 	return new;
349 }
350 
351 /*
352  * -delete functions --
353  *
354  *	True always.  Makes its best shot and continues on regardless.
355  */
356 int
357 f_delete(PLAN *plan __unused, FTSENT *entry)
358 {
359 	/* ignore these from fts */
360 	if (strcmp(entry->fts_accpath, ".") == 0 ||
361 	    strcmp(entry->fts_accpath, "..") == 0)
362 		return 1;
363 
364 	/* sanity check */
365 	if (isdepth == 0 ||			/* depth off */
366 	    (ftsoptions & FTS_NOSTAT))		/* not stat()ing */
367 		errx(1, "-delete: insecure options got turned on");
368 
369 	if (!(ftsoptions & FTS_PHYSICAL) ||	/* physical off */
370 	    (ftsoptions & FTS_LOGICAL))		/* or finally, logical on */
371 		errx(1, "-delete: forbidden when symlinks are followed");
372 
373 	/* Potentially unsafe - do not accept relative paths whatsoever */
374 	if (entry->fts_level > FTS_ROOTLEVEL &&
375 	    strchr(entry->fts_accpath, '/') != NULL)
376 		errx(1, "-delete: %s: relative path potentially not safe",
377 			entry->fts_accpath);
378 
379 	/* Turn off user immutable bits if running as root */
380 	if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
381 	    !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
382 	    geteuid() == 0)
383 		lchflags(entry->fts_accpath,
384 		       entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
385 
386 	/* rmdir directories, unlink everything else */
387 	if (S_ISDIR(entry->fts_statp->st_mode)) {
388 		if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
389 			warn("-delete: rmdir(%s)", entry->fts_path);
390 	} else {
391 		if (unlink(entry->fts_accpath) < 0)
392 			warn("-delete: unlink(%s)", entry->fts_path);
393 	}
394 
395 	/* "succeed" */
396 	return 1;
397 }
398 
399 PLAN *
400 c_delete(OPTION *option, char ***argvp __unused)
401 {
402 
403 	ftsoptions &= ~FTS_NOSTAT;	/* no optimise */
404 	isoutput = 1;			/* possible output */
405 	isdepth = 1;			/* -depth implied */
406 
407 	/*
408 	 * Try to avoid the confusing error message about relative paths
409 	 * being potentially not safe.
410 	 */
411 	if (ftsoptions & FTS_NOCHDIR)
412 		errx(1, "%s: forbidden when the current directory cannot be opened",
413 		    "-delete");
414 
415 	return palloc(option);
416 }
417 
418 
419 /*
420  * always_true --
421  *
422  *	Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
423  */
424 int
425 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
426 {
427 	return 1;
428 }
429 
430 /*
431  * -depth functions --
432  *
433  *	With argument: True if the file is at level n.
434  *	Without argument: Always true, causes descent of the directory hierarchy
435  *	to be done so that all entries in a directory are acted on before the
436  *	directory itself.
437  */
438 int
439 f_depth(PLAN *plan, FTSENT *entry)
440 {
441 	if (plan->flags & F_DEPTH)
442 		COMPARE(entry->fts_level, plan->d_data);
443 	else
444 		return 1;
445 }
446 
447 PLAN *
448 c_depth(OPTION *option, char ***argvp)
449 {
450 	PLAN *new;
451 	char *str;
452 
453 	new = palloc(option);
454 
455 	str = **argvp;
456 	if (str && !(new->flags & F_DEPTH)) {
457 		/* skip leading + or - */
458 		if (*str == '+' || *str == '-')
459 			str++;
460 		/* skip sign */
461 		if (*str == '+' || *str == '-')
462 			str++;
463 		if (isdigit(*str))
464 			new->flags |= F_DEPTH;
465 	}
466 
467 	if (new->flags & F_DEPTH) {	/* -depth n */
468 		char *ndepth;
469 
470 		ndepth = nextarg(option, argvp);
471 		new->d_data = find_parsenum(new, option->name, ndepth, NULL);
472 	} else {			/* -d */
473 		isdepth = 1;
474 	}
475 
476 	return new;
477 }
478 
479 /*
480  * -empty functions --
481  *
482  *	True if the file or directory is empty
483  */
484 int
485 f_empty(PLAN *plan __unused, FTSENT *entry)
486 {
487 	if (S_ISREG(entry->fts_statp->st_mode) &&
488 	    entry->fts_statp->st_size == 0)
489 		return 1;
490 	if (S_ISDIR(entry->fts_statp->st_mode)) {
491 		struct dirent *dp;
492 		int empty;
493 		DIR *dir;
494 
495 		empty = 1;
496 		dir = opendir(entry->fts_accpath);
497 		if (dir == NULL)
498 			return 0;
499 		for (dp = readdir(dir); dp; dp = readdir(dir))
500 			if (dp->d_name[0] != '.' ||
501 			    (dp->d_name[1] != '\0' &&
502 			     (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
503 				empty = 0;
504 				break;
505 			}
506 		closedir(dir);
507 		return empty;
508 	}
509 	return 0;
510 }
511 
512 PLAN *
513 c_empty(OPTION *option, char ***argvp __unused)
514 {
515 	ftsoptions &= ~FTS_NOSTAT;
516 
517 	return palloc(option);
518 }
519 
520 /*
521  * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
522  *
523  *	True if the executed utility returns a zero value as exit status.
524  *	The end of the primary expression is delimited by a semicolon.  If
525  *	"{}" occurs anywhere, it gets replaced by the current pathname,
526  *	or, in the case of -execdir, the current basename (filename
527  *	without leading directory prefix). For -exec and -ok,
528  *	the current directory for the execution of utility is the same as
529  *	the current directory when the find utility was started, whereas
530  *	for -execdir, it is the directory the file resides in.
531  *
532  *	The primary -ok differs from -exec in that it requests affirmation
533  *	of the user before executing the utility.
534  */
535 int
536 f_exec(PLAN *plan, FTSENT *entry)
537 {
538 	int cnt;
539 	pid_t pid;
540 	int status;
541 	char *file;
542 
543 	if (entry == NULL && plan->flags & F_EXECPLUS) {
544 		if (plan->e_ppos == plan->e_pbnum)
545 			return (1);
546 		plan->e_argv[plan->e_ppos] = NULL;
547 		goto doexec;
548 	}
549 
550 	/* XXX - if file/dir ends in '/' this will not work -- can it? */
551 	if ((plan->flags & F_EXECDIR) && \
552 	    (file = strrchr(entry->fts_path, '/')))
553 		file++;
554 	else
555 		file = entry->fts_path;
556 
557 	if (plan->flags & F_EXECPLUS) {
558 		if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
559 			err(1, NULL);
560 		plan->e_len[plan->e_ppos] = strlen(file);
561 		plan->e_psize += plan->e_len[plan->e_ppos];
562 		if (++plan->e_ppos < plan->e_pnummax &&
563 		    plan->e_psize < plan->e_psizemax)
564 			return (1);
565 		plan->e_argv[plan->e_ppos] = NULL;
566 	} else {
567 		for (cnt = 0; plan->e_argv[cnt]; ++cnt)
568 			if (plan->e_len[cnt])
569 				brace_subst(plan->e_orig[cnt],
570 				    &plan->e_argv[cnt], file,
571 				    plan->e_len[cnt]);
572 	}
573 
574 doexec:	if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
575 		return 0;
576 
577 	/* make sure find output is interspersed correctly with subprocesses */
578 	fflush(stdout);
579 	fflush(stderr);
580 
581 	switch (pid = fork()) {
582 	case -1:
583 		err(1, "fork");
584 		/* NOTREACHED */
585 	case 0:
586 		/* change dir back from where we started */
587 		if (!(plan->flags & F_EXECDIR) &&
588 		    !(ftsoptions & FTS_NOCHDIR) && fchdir(dotfd)) {
589 			warn("chdir");
590 			_exit(1);
591 		}
592 		execvp(plan->e_argv[0], plan->e_argv);
593 		warn("%s", plan->e_argv[0]);
594 		_exit(1);
595 	}
596 	if (plan->flags & F_EXECPLUS) {
597 		while (--plan->e_ppos >= plan->e_pbnum)
598 			free(plan->e_argv[plan->e_ppos]);
599 		plan->e_ppos = plan->e_pbnum;
600 		plan->e_psize = plan->e_pbsize;
601 	}
602 	pid = waitpid(pid, &status, 0);
603 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
604 }
605 
606 /*
607  * c_exec, c_execdir, c_ok --
608  *	build three parallel arrays, one with pointers to the strings passed
609  *	on the command line, one with (possibly duplicated) pointers to the
610  *	argv array, and one with integer values that are lengths of the
611  *	strings, but also flags meaning that the string has to be massaged.
612  */
613 PLAN *
614 c_exec(OPTION *option, char ***argvp)
615 {
616 	PLAN *new;			/* node returned */
617 	long argmax;
618 	int cnt, i;
619 	char **argv, **ap, **ep, *p;
620 
621 	/* This would defeat -execdir's intended security. */
622 	if (option->flags & F_EXECDIR && ftsoptions & FTS_NOCHDIR)
623 		errx(1, "%s: forbidden when the current directory cannot be opened",
624 		    "-execdir");
625 
626 	/* XXX - was in c_execdir, but seems unnecessary!?
627 	ftsoptions &= ~FTS_NOSTAT;
628 	*/
629 	isoutput = 1;
630 
631 	/* XXX - this is a change from the previous coding */
632 	new = palloc(option);
633 
634 	for (ap = argv = *argvp;; ++ap) {
635 		if (!*ap)
636 			errx(1,
637 			    "%s: no terminating \";\" or \"+\"", option->name);
638 		if (**ap == ';')
639 			break;
640 		if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
641 			new->flags |= F_EXECPLUS;
642 			break;
643 		}
644 	}
645 
646 	if (ap == argv)
647 		errx(1, "%s: no command specified", option->name);
648 
649 	cnt = ap - *argvp + 1;
650 	if (new->flags & F_EXECPLUS) {
651 		new->e_ppos = new->e_pbnum = cnt - 2;
652 		if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
653 			warn("sysconf(_SC_ARG_MAX)");
654 			argmax = _POSIX_ARG_MAX;
655 		}
656 		argmax -= 1024;
657 		for (ep = environ; *ep != NULL; ep++)
658 			argmax -= strlen(*ep) + 1 + sizeof(*ep);
659 		argmax -= 1 + sizeof(*ep);
660 		/*
661 		 * Ensure that -execdir ... {} + does not mix files
662 		 * from different directories in one invocation.
663 		 * Files from the same directory should be handled
664 		 * in one invocation but there is no code for it.
665 		 */
666 		new->e_pnummax = new->flags & F_EXECDIR ? 1 : argmax / 16;
667 		argmax -= sizeof(char *) * new->e_pnummax;
668 		if (argmax <= 0)
669 			errx(1, "no space for arguments");
670 		new->e_psizemax = argmax;
671 		new->e_pbsize = 0;
672 		cnt += new->e_pnummax + 1;
673 		new->e_next = lastexecplus;
674 		lastexecplus = new;
675 	}
676 	if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
677 		err(1, NULL);
678 	if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
679 		err(1, NULL);
680 	if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
681 		err(1, NULL);
682 
683 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
684 		new->e_orig[cnt] = *argv;
685 		if (new->flags & F_EXECPLUS)
686 			new->e_pbsize += strlen(*argv) + 1;
687 		for (p = *argv; *p; ++p)
688 			if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
689 			    p[1] == '}') {
690 				if ((new->e_argv[cnt] =
691 				    malloc(MAXPATHLEN)) == NULL)
692 					err(1, NULL);
693 				new->e_len[cnt] = MAXPATHLEN;
694 				break;
695 			}
696 		if (!*p) {
697 			new->e_argv[cnt] = *argv;
698 			new->e_len[cnt] = 0;
699 		}
700 	}
701 	if (new->flags & F_EXECPLUS) {
702 		new->e_psize = new->e_pbsize;
703 		cnt--;
704 		for (i = 0; i < new->e_pnummax; i++) {
705 			new->e_argv[cnt] = NULL;
706 			new->e_len[cnt] = 0;
707 			cnt++;
708 		}
709 		argv = ap;
710 		goto done;
711 	}
712 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
713 
714 done:	*argvp = argv + 1;
715 	return new;
716 }
717 
718 /* Finish any pending -exec ... {} + functions. */
719 void
720 finish_execplus(void)
721 {
722 	PLAN *p;
723 
724 	p = lastexecplus;
725 	while (p != NULL) {
726 		(p->execute)(p, NULL);
727 		p = p->e_next;
728 	}
729 }
730 
731 int
732 f_flags(PLAN *plan, FTSENT *entry)
733 {
734 	u_long flags;
735 
736 	flags = entry->fts_statp->st_flags;
737 	if (plan->flags & F_ATLEAST)
738 		return (flags | plan->fl_flags) == flags &&
739 		    !(flags & plan->fl_notflags);
740 	else if (plan->flags & F_ANY)
741 		return (flags & plan->fl_flags) ||
742 		    (flags | plan->fl_notflags) != flags;
743 	else
744 		return flags == plan->fl_flags &&
745 		    !(plan->fl_flags & plan->fl_notflags);
746 }
747 
748 PLAN *
749 c_flags(OPTION *option, char ***argvp)
750 {
751 	char *flags_str;
752 	PLAN *new;
753 	u_long flags, notflags;
754 
755 	flags_str = nextarg(option, argvp);
756 	ftsoptions &= ~FTS_NOSTAT;
757 
758 	new = palloc(option);
759 
760 	if (*flags_str == '-') {
761 		new->flags |= F_ATLEAST;
762 		flags_str++;
763 	} else if (*flags_str == '+') {
764 		new->flags |= F_ANY;
765 		flags_str++;
766 	}
767 	if (strtofflags(&flags_str, &flags, &notflags) == 1)
768 		errx(1, "%s: %s: illegal flags string", option->name, flags_str);
769 
770 	new->fl_flags = flags;
771 	new->fl_notflags = notflags;
772 	return new;
773 }
774 
775 /*
776  * -follow functions --
777  *
778  *	Always true, causes symbolic links to be followed on a global
779  *	basis.
780  */
781 PLAN *
782 c_follow(OPTION *option, char ***argvp __unused)
783 {
784 	ftsoptions &= ~FTS_PHYSICAL;
785 	ftsoptions |= FTS_LOGICAL;
786 
787 	return palloc(option);
788 }
789 
790 /*
791  * -fstype functions --
792  *
793  *	True if the file is of a certain type.
794  */
795 int
796 f_fstype(PLAN *plan, FTSENT *entry)
797 {
798 	static dev_t curdev;	/* need a guaranteed illegal dev value */
799 	static int first = 1;
800 	struct statfs sb;
801 	static int val_flags;
802 	static char fstype[sizeof(sb.f_fstypename)];
803 	char *p, save[2] = {0,0};
804 
805 	if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
806 		return 0;
807 
808 	/* Only check when we cross mount point. */
809 	if (first || curdev != entry->fts_statp->st_dev) {
810 		curdev = entry->fts_statp->st_dev;
811 
812 		/*
813 		 * Statfs follows symlinks; find wants the link's filesystem,
814 		 * not where it points.
815 		 */
816 		if (entry->fts_info == FTS_SL ||
817 		    entry->fts_info == FTS_SLNONE) {
818 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
819 				++p;
820 			else
821 				p = entry->fts_accpath;
822 			save[0] = p[0];
823 			p[0] = '.';
824 			save[1] = p[1];
825 			p[1] = '\0';
826 		} else
827 			p = NULL;
828 
829 		if (statfs(entry->fts_accpath, &sb))
830 			err(1, "%s", entry->fts_accpath);
831 
832 		if (p) {
833 			p[0] = save[0];
834 			p[1] = save[1];
835 		}
836 
837 		first = 0;
838 
839 		/*
840 		 * Further tests may need both of these values, so
841 		 * always copy both of them.
842 		 */
843 		val_flags = sb.f_flags;
844 		strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
845 	}
846 	switch (plan->flags & F_MTMASK) {
847 	case F_MTFLAG:
848 		return val_flags & plan->mt_data;
849 	case F_MTTYPE:
850 		return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
851 	default:
852 		abort();
853 	}
854 }
855 
856 PLAN *
857 c_fstype(OPTION *option, char ***argvp)
858 {
859 	char *fsname;
860 	PLAN *new;
861 
862 	fsname = nextarg(option, argvp);
863 	ftsoptions &= ~FTS_NOSTAT;
864 
865 	new = palloc(option);
866 	switch (*fsname) {
867 	case 'l':
868 		if (!strcmp(fsname, "local")) {
869 			new->flags |= F_MTFLAG;
870 			new->mt_data = MNT_LOCAL;
871 			return new;
872 		}
873 		break;
874 	case 'r':
875 		if (!strcmp(fsname, "rdonly")) {
876 			new->flags |= F_MTFLAG;
877 			new->mt_data = MNT_RDONLY;
878 			return new;
879 		}
880 		break;
881 	}
882 
883 	new->flags |= F_MTTYPE;
884 	new->c_data = fsname;
885 	return new;
886 }
887 
888 /*
889  * -group gname functions --
890  *
891  *	True if the file belongs to the group gname.  If gname is numeric and
892  *	an equivalent of the getgrnam() function does not return a valid group
893  *	name, gname is taken as a group ID.
894  */
895 int
896 f_group(PLAN *plan, FTSENT *entry)
897 {
898 	COMPARE(entry->fts_statp->st_gid, plan->g_data);
899 }
900 
901 PLAN *
902 c_group(OPTION *option, char ***argvp)
903 {
904 	char *gname;
905 	PLAN *new;
906 	struct group *g;
907 	gid_t gid;
908 
909 	gname = nextarg(option, argvp);
910 	ftsoptions &= ~FTS_NOSTAT;
911 
912 	new = palloc(option);
913 	g = getgrnam(gname);
914 	if (g == NULL) {
915 		char* cp = gname;
916 		if (gname[0] == '-' || gname[0] == '+')
917 			gname++;
918 		gid = atoi(gname);
919 		if (gid == 0 && gname[0] != '0')
920 			errx(1, "%s: %s: no such group", option->name, gname);
921 		gid = find_parsenum(new, option->name, cp, NULL);
922 	} else
923 		gid = g->gr_gid;
924 
925 	new->g_data = gid;
926 	return new;
927 }
928 
929 /*
930  * -ignore_readdir_race functions --
931  *
932  *	Always true. Ignore errors which occur if a file or a directory
933  *	in a starting point gets deleted between reading the name and calling
934  *	stat on it while find is traversing the starting point.
935  */
936 
937 PLAN *
938 c_ignore_readdir_race(OPTION *option, char ***argvp __unused)
939 {
940 	if (strcmp(option->name, "-ignore_readdir_race") == 0)
941 		ignore_readdir_race = 1;
942 	else
943 		ignore_readdir_race = 0;
944 
945 	return palloc(option);
946 }
947 
948 /*
949  * -inum n functions --
950  *
951  *	True if the file has inode # n.
952  */
953 int
954 f_inum(PLAN *plan, FTSENT *entry)
955 {
956 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
957 }
958 
959 PLAN *
960 c_inum(OPTION *option, char ***argvp)
961 {
962 	char *inum_str;
963 	PLAN *new;
964 
965 	inum_str = nextarg(option, argvp);
966 	ftsoptions &= ~FTS_NOSTAT;
967 
968 	new = palloc(option);
969 	new->i_data = find_parsenum(new, option->name, inum_str, NULL);
970 	return new;
971 }
972 
973 /*
974  * -samefile FN
975  *
976  *	True if the file has the same inode (eg hard link) FN
977  */
978 
979 /* f_samefile is just f_inum */
980 PLAN *
981 c_samefile(OPTION *option, char ***argvp)
982 {
983 	char *fn;
984 	PLAN *new;
985 	struct stat sb;
986 
987 	fn = nextarg(option, argvp);
988 	ftsoptions &= ~FTS_NOSTAT;
989 
990 	new = palloc(option);
991 	if (stat(fn, &sb))
992 		err(1, "%s", fn);
993 	new->i_data = sb.st_ino;
994 	return new;
995 }
996 
997 /*
998  * -links n functions --
999  *
1000  *	True if the file has n links.
1001  */
1002 int
1003 f_links(PLAN *plan, FTSENT *entry)
1004 {
1005 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1006 }
1007 
1008 PLAN *
1009 c_links(OPTION *option, char ***argvp)
1010 {
1011 	char *nlinks;
1012 	PLAN *new;
1013 
1014 	nlinks = nextarg(option, argvp);
1015 	ftsoptions &= ~FTS_NOSTAT;
1016 
1017 	new = palloc(option);
1018 	new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1019 	return new;
1020 }
1021 
1022 /*
1023  * -ls functions --
1024  *
1025  *	Always true - prints the current entry to stdout in "ls" format.
1026  */
1027 int
1028 f_ls(PLAN *plan __unused, FTSENT *entry)
1029 {
1030 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1031 	return 1;
1032 }
1033 
1034 PLAN *
1035 c_ls(OPTION *option, char ***argvp __unused)
1036 {
1037 	ftsoptions &= ~FTS_NOSTAT;
1038 	isoutput = 1;
1039 
1040 	return palloc(option);
1041 }
1042 
1043 /*
1044  * -name functions --
1045  *
1046  *	True if the basename of the filename being examined
1047  *	matches pattern using Pattern Matching Notation S3.14
1048  */
1049 int
1050 f_name(PLAN *plan, FTSENT *entry)
1051 {
1052 	char fn[PATH_MAX];
1053 	const char *name;
1054 
1055 	if (plan->flags & F_LINK) {
1056 		name = fn;
1057 		if (readlink(entry->fts_path, fn, sizeof(fn)) == -1)
1058 			return 0;
1059 	} else
1060 		name = entry->fts_name;
1061 	return !fnmatch(plan->c_data, name,
1062 	    plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1063 }
1064 
1065 PLAN *
1066 c_name(OPTION *option, char ***argvp)
1067 {
1068 	char *pattern;
1069 	PLAN *new;
1070 
1071 	pattern = nextarg(option, argvp);
1072 	new = palloc(option);
1073 	new->c_data = pattern;
1074 	return new;
1075 }
1076 
1077 /*
1078  * -newer file functions --
1079  *
1080  *	True if the current file has been modified more recently
1081  *	then the modification time of the file named by the pathname
1082  *	file.
1083  */
1084 int
1085 f_newer(PLAN *plan, FTSENT *entry)
1086 {
1087 	struct timespec ft;
1088 
1089 	if (plan->flags & F_TIME_C)
1090 		ft = entry->fts_statp->st_ctim;
1091 	else if (plan->flags & F_TIME_A)
1092 		ft = entry->fts_statp->st_atim;
1093 	else
1094 		ft = entry->fts_statp->st_mtim;
1095 	return (ft.tv_sec > plan->t_data.tv_sec ||
1096 		(ft.tv_sec == plan->t_data.tv_sec &&
1097 		 ft.tv_nsec > plan->t_data.tv_nsec));
1098 }
1099 
1100 PLAN *
1101 c_newer(OPTION *option, char ***argvp)
1102 {
1103 	char *fn_or_tspec;
1104 	PLAN *new;
1105 	struct stat sb;
1106 
1107 	fn_or_tspec = nextarg(option, argvp);
1108 	ftsoptions &= ~FTS_NOSTAT;
1109 
1110 	new = palloc(option);
1111 	/* compare against what */
1112 	if (option->flags & F_TIME2_T) {
1113 		new->t_data.tv_sec = get_date(fn_or_tspec);
1114 		if (new->t_data.tv_sec == (time_t) -1)
1115 			errx(1, "Can't parse date/time: %s", fn_or_tspec);
1116 		/* Use the seconds only in the comparison. */
1117 		new->t_data.tv_nsec = 999999999;
1118 	} else {
1119 		if (stat(fn_or_tspec, &sb))
1120 			err(1, "%s", fn_or_tspec);
1121 		if (option->flags & F_TIME2_C)
1122 			new->t_data = sb.st_ctim;
1123 		else if (option->flags & F_TIME2_A)
1124 			new->t_data = sb.st_atim;
1125 		else
1126 			new->t_data = sb.st_mtim;
1127 	}
1128 	return new;
1129 }
1130 
1131 /*
1132  * -nogroup functions --
1133  *
1134  *	True if file belongs to a user ID for which the equivalent
1135  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1136  */
1137 int
1138 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1139 {
1140 	return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1141 }
1142 
1143 PLAN *
1144 c_nogroup(OPTION *option, char ***argvp __unused)
1145 {
1146 	ftsoptions &= ~FTS_NOSTAT;
1147 
1148 	return palloc(option);
1149 }
1150 
1151 /*
1152  * -nouser functions --
1153  *
1154  *	True if file belongs to a user ID for which the equivalent
1155  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1156  */
1157 int
1158 f_nouser(PLAN *plan __unused, FTSENT *entry)
1159 {
1160 	return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1161 }
1162 
1163 PLAN *
1164 c_nouser(OPTION *option, char ***argvp __unused)
1165 {
1166 	ftsoptions &= ~FTS_NOSTAT;
1167 
1168 	return palloc(option);
1169 }
1170 
1171 /*
1172  * -path functions --
1173  *
1174  *	True if the path of the filename being examined
1175  *	matches pattern using Pattern Matching Notation S3.14
1176  */
1177 int
1178 f_path(PLAN *plan, FTSENT *entry)
1179 {
1180 	return !fnmatch(plan->c_data, entry->fts_path,
1181 	    plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1182 }
1183 
1184 /* c_path is the same as c_name */
1185 
1186 /*
1187  * -perm functions --
1188  *
1189  *	The mode argument is used to represent file mode bits.  If it starts
1190  *	with a leading digit, it's treated as an octal mode, otherwise as a
1191  *	symbolic mode.
1192  */
1193 int
1194 f_perm(PLAN *plan, FTSENT *entry)
1195 {
1196 	mode_t mode;
1197 
1198 	mode = entry->fts_statp->st_mode &
1199 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1200 	if (plan->flags & F_ATLEAST)
1201 		return (plan->m_data | mode) == mode;
1202 	else if (plan->flags & F_ANY)
1203 		return (mode & plan->m_data);
1204 	else
1205 		return mode == plan->m_data;
1206 	/* NOTREACHED */
1207 }
1208 
1209 PLAN *
1210 c_perm(OPTION *option, char ***argvp)
1211 {
1212 	char *perm;
1213 	PLAN *new;
1214 	mode_t *set;
1215 
1216 	perm = nextarg(option, argvp);
1217 	ftsoptions &= ~FTS_NOSTAT;
1218 
1219 	new = palloc(option);
1220 
1221 	if (*perm == '-') {
1222 		new->flags |= F_ATLEAST;
1223 		++perm;
1224 	} else if (*perm == '+') {
1225 		new->flags |= F_ANY;
1226 		++perm;
1227 	}
1228 
1229 	if ((set = setmode(perm)) == NULL)
1230 		errx(1, "%s: %s: illegal mode string", option->name, perm);
1231 
1232 	new->m_data = getmode(set, 0);
1233 	free(set);
1234 	return new;
1235 }
1236 
1237 /*
1238  * -print functions --
1239  *
1240  *	Always true, causes the current pathname to be written to
1241  *	standard output.
1242  */
1243 int
1244 f_print(PLAN *plan __unused, FTSENT *entry)
1245 {
1246 	(void)puts(entry->fts_path);
1247 	return 1;
1248 }
1249 
1250 PLAN *
1251 c_print(OPTION *option, char ***argvp __unused)
1252 {
1253 	isoutput = 1;
1254 
1255 	return palloc(option);
1256 }
1257 
1258 /*
1259  * -print0 functions --
1260  *
1261  *	Always true, causes the current pathname to be written to
1262  *	standard output followed by a NUL character
1263  */
1264 int
1265 f_print0(PLAN *plan __unused, FTSENT *entry)
1266 {
1267 	fputs(entry->fts_path, stdout);
1268 	fputc('\0', stdout);
1269 	return 1;
1270 }
1271 
1272 /* c_print0 is the same as c_print */
1273 
1274 /*
1275  * -prune functions --
1276  *
1277  *	Prune a portion of the hierarchy.
1278  */
1279 int
1280 f_prune(PLAN *plan __unused, FTSENT *entry)
1281 {
1282 	if (fts_set(tree, entry, FTS_SKIP))
1283 		err(1, "%s", entry->fts_path);
1284 	return 1;
1285 }
1286 
1287 /* c_prune == c_simple */
1288 
1289 /*
1290  * -regex functions --
1291  *
1292  *	True if the whole path of the file matches pattern using
1293  *	regular expression.
1294  */
1295 int
1296 f_regex(PLAN *plan, FTSENT *entry)
1297 {
1298 	char *str;
1299 	int len;
1300 	regex_t *pre;
1301 	regmatch_t pmatch;
1302 	int errcode;
1303 	char errbuf[LINE_MAX];
1304 	int matched;
1305 
1306 	pre = plan->re_data;
1307 	str = entry->fts_path;
1308 	len = strlen(str);
1309 	matched = 0;
1310 
1311 	pmatch.rm_so = 0;
1312 	pmatch.rm_eo = len;
1313 
1314 	errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1315 
1316 	if (errcode != 0 && errcode != REG_NOMATCH) {
1317 		regerror(errcode, pre, errbuf, sizeof errbuf);
1318 		errx(1, "%s: %s",
1319 		     plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1320 	}
1321 
1322 	if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1323 		matched = 1;
1324 
1325 	return matched;
1326 }
1327 
1328 PLAN *
1329 c_regex(OPTION *option, char ***argvp)
1330 {
1331 	PLAN *new;
1332 	char *pattern;
1333 	regex_t *pre;
1334 	int errcode;
1335 	char errbuf[LINE_MAX];
1336 
1337 	if ((pre = malloc(sizeof(regex_t))) == NULL)
1338 		err(1, NULL);
1339 
1340 	pattern = nextarg(option, argvp);
1341 
1342 	if ((errcode = regcomp(pre, pattern,
1343 	    regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1344 		regerror(errcode, pre, errbuf, sizeof errbuf);
1345 		errx(1, "%s: %s: %s",
1346 		     option->flags & F_IGNCASE ? "-iregex" : "-regex",
1347 		     pattern, errbuf);
1348 	}
1349 
1350 	new = palloc(option);
1351 	new->re_data = pre;
1352 
1353 	return new;
1354 }
1355 
1356 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1357 
1358 PLAN *
1359 c_simple(OPTION *option, char ***argvp __unused)
1360 {
1361 	return palloc(option);
1362 }
1363 
1364 /*
1365  * -size n[c] functions --
1366  *
1367  *	True if the file size in bytes, divided by an implementation defined
1368  *	value and rounded up to the next integer, is n.  If n is followed by
1369  *      one of c k M G T P, the size is in bytes, kilobytes,
1370  *      megabytes, gigabytes, terabytes or petabytes respectively.
1371  */
1372 #define	FIND_SIZE	512
1373 static int divsize = 1;
1374 
1375 int
1376 f_size(PLAN *plan, FTSENT *entry)
1377 {
1378 	off_t size;
1379 
1380 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1381 	    FIND_SIZE : entry->fts_statp->st_size;
1382 	COMPARE(size, plan->o_data);
1383 }
1384 
1385 PLAN *
1386 c_size(OPTION *option, char ***argvp)
1387 {
1388 	char *size_str;
1389 	PLAN *new;
1390 	char endch;
1391 	off_t scale;
1392 
1393 	size_str = nextarg(option, argvp);
1394 	ftsoptions &= ~FTS_NOSTAT;
1395 
1396 	new = palloc(option);
1397 	endch = 'c';
1398 	new->o_data = find_parsenum(new, option->name, size_str, &endch);
1399 	if (endch != '\0') {
1400 		divsize = 0;
1401 
1402 		switch (endch) {
1403 		case 'c':                       /* characters */
1404 			scale = 0x1LL;
1405 			break;
1406 		case 'k':                       /* kilobytes 1<<10 */
1407 			scale = 0x400LL;
1408 			break;
1409 		case 'M':                       /* megabytes 1<<20 */
1410 			scale = 0x100000LL;
1411 			break;
1412 		case 'G':                       /* gigabytes 1<<30 */
1413 			scale = 0x40000000LL;
1414 			break;
1415 		case 'T':                       /* terabytes 1<<40 */
1416 			scale = 0x1000000000LL;
1417 			break;
1418 		case 'P':                       /* petabytes 1<<50 */
1419 			scale = 0x4000000000000LL;
1420 			break;
1421 		default:
1422 			errx(1, "%s: %s: illegal trailing character",
1423 				option->name, size_str);
1424 			break;
1425 		}
1426 		if (new->o_data > QUAD_MAX / scale)
1427 			errx(1, "%s: %s: value too large",
1428 				option->name, size_str);
1429 		new->o_data *= scale;
1430 	}
1431 	return new;
1432 }
1433 
1434 /*
1435  * -sparse functions --
1436  *
1437  *      Check if a file is sparse by finding if it occupies fewer blocks
1438  *      than we expect based on its size.
1439  */
1440 int
1441 f_sparse(PLAN *plan __unused, FTSENT *entry)
1442 {
1443 	off_t expected_blocks;
1444 
1445 	expected_blocks = (entry->fts_statp->st_size + 511) / 512;
1446 	return entry->fts_statp->st_blocks < expected_blocks;
1447 }
1448 
1449 PLAN *
1450 c_sparse(OPTION *option, char ***argvp __unused)
1451 {
1452 	ftsoptions &= ~FTS_NOSTAT;
1453 
1454 	return palloc(option);
1455 }
1456 
1457 /*
1458  * -type c functions --
1459  *
1460  *	True if the type of the file is c, where c is b, c, d, p, f or w
1461  *	for block special file, character special file, directory, FIFO,
1462  *	regular file or whiteout respectively.
1463  */
1464 int
1465 f_type(PLAN *plan, FTSENT *entry)
1466 {
1467 	return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1468 }
1469 
1470 PLAN *
1471 c_type(OPTION *option, char ***argvp)
1472 {
1473 	char *typestring;
1474 	PLAN *new;
1475 	mode_t  mask;
1476 
1477 	typestring = nextarg(option, argvp);
1478 	ftsoptions &= ~FTS_NOSTAT;
1479 
1480 	switch (typestring[0]) {
1481 	case 'b':
1482 		mask = S_IFBLK;
1483 		break;
1484 	case 'c':
1485 		mask = S_IFCHR;
1486 		break;
1487 	case 'd':
1488 		mask = S_IFDIR;
1489 		break;
1490 	case 'f':
1491 		mask = S_IFREG;
1492 		break;
1493 	case 'l':
1494 		mask = S_IFLNK;
1495 		break;
1496 	case 'p':
1497 		mask = S_IFIFO;
1498 		break;
1499 	case 's':
1500 		mask = S_IFSOCK;
1501 		break;
1502 #ifdef FTS_WHITEOUT
1503 	case 'w':
1504 		mask = S_IFWHT;
1505 		ftsoptions |= FTS_WHITEOUT;
1506 		break;
1507 #endif /* FTS_WHITEOUT */
1508 	default:
1509 		errx(1, "%s: %s: unknown type", option->name, typestring);
1510 	}
1511 
1512 	new = palloc(option);
1513 	new->m_data = mask;
1514 	return new;
1515 }
1516 
1517 /*
1518  * -user uname functions --
1519  *
1520  *	True if the file belongs to the user uname.  If uname is numeric and
1521  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1522  *	return a valid user name, uname is taken as a user ID.
1523  */
1524 int
1525 f_user(PLAN *plan, FTSENT *entry)
1526 {
1527 	COMPARE(entry->fts_statp->st_uid, plan->u_data);
1528 }
1529 
1530 PLAN *
1531 c_user(OPTION *option, char ***argvp)
1532 {
1533 	char *username;
1534 	PLAN *new;
1535 	struct passwd *p;
1536 	uid_t uid;
1537 
1538 	username = nextarg(option, argvp);
1539 	ftsoptions &= ~FTS_NOSTAT;
1540 
1541 	new = palloc(option);
1542 	p = getpwnam(username);
1543 	if (p == NULL) {
1544 		char* cp = username;
1545 		if( username[0] == '-' || username[0] == '+' )
1546 			username++;
1547 		uid = atoi(username);
1548 		if (uid == 0 && username[0] != '0')
1549 			errx(1, "%s: %s: no such user", option->name, username);
1550 		uid = find_parsenum(new, option->name, cp, NULL);
1551 	} else
1552 		uid = p->pw_uid;
1553 
1554 	new->u_data = uid;
1555 	return new;
1556 }
1557 
1558 /*
1559  * -xdev functions --
1560  *
1561  *	Always true, causes find not to descend past directories that have a
1562  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1563  */
1564 PLAN *
1565 c_xdev(OPTION *option, char ***argvp __unused)
1566 {
1567 	ftsoptions |= FTS_XDEV;
1568 
1569 	return palloc(option);
1570 }
1571 
1572 /*
1573  * ( expression ) functions --
1574  *
1575  *	True if expression is true.
1576  */
1577 int
1578 f_expr(PLAN *plan, FTSENT *entry)
1579 {
1580 	PLAN *p;
1581 	int state = 0;
1582 
1583 	for (p = plan->p_data[0];
1584 	    p && (state = (p->execute)(p, entry)); p = p->next);
1585 	return state;
1586 }
1587 
1588 /*
1589  * f_openparen and f_closeparen nodes are temporary place markers.  They are
1590  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1591  * to a f_expr node containing the expression and the ')' node is discarded.
1592  * The functions themselves are only used as constants.
1593  */
1594 
1595 int
1596 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1597 {
1598 	abort();
1599 }
1600 
1601 int
1602 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1603 {
1604 	abort();
1605 }
1606 
1607 /* c_openparen == c_simple */
1608 /* c_closeparen == c_simple */
1609 
1610 /*
1611  * AND operator. Since AND is implicit, no node is allocated.
1612  */
1613 PLAN *
1614 c_and(OPTION *option __unused, char ***argvp __unused)
1615 {
1616 	return NULL;
1617 }
1618 
1619 /*
1620  * ! expression functions --
1621  *
1622  *	Negation of a primary; the unary NOT operator.
1623  */
1624 int
1625 f_not(PLAN *plan, FTSENT *entry)
1626 {
1627 	PLAN *p;
1628 	int state = 0;
1629 
1630 	for (p = plan->p_data[0];
1631 	    p && (state = (p->execute)(p, entry)); p = p->next);
1632 	return !state;
1633 }
1634 
1635 /* c_not == c_simple */
1636 
1637 /*
1638  * expression -o expression functions --
1639  *
1640  *	Alternation of primaries; the OR operator.  The second expression is
1641  * not evaluated if the first expression is true.
1642  */
1643 int
1644 f_or(PLAN *plan, FTSENT *entry)
1645 {
1646 	PLAN *p;
1647 	int state = 0;
1648 
1649 	for (p = plan->p_data[0];
1650 	    p && (state = (p->execute)(p, entry)); p = p->next);
1651 
1652 	if (state)
1653 		return 1;
1654 
1655 	for (p = plan->p_data[1];
1656 	    p && (state = (p->execute)(p, entry)); p = p->next);
1657 	return state;
1658 }
1659 
1660 /* c_or == c_simple */
1661 
1662 /*
1663  * -false
1664  *
1665  *	Always false.
1666  */
1667 int
1668 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1669 {
1670 	return 0;
1671 }
1672 
1673 /* c_false == c_simple */
1674 
1675 /*
1676  * -quit
1677  *
1678  *	Exits the program
1679  */
1680 int
1681 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1682 {
1683 	exit(0);
1684 }
1685 
1686 /* c_quit == c_simple */
1687