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