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