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