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