xref: /original-bsd/usr.bin/find/function.c (revision 27393bdf)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Cimarron D. Taylor of the University of California, Berkeley.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)function.c	8.9 (Berkeley) 04/28/95";
13 #endif /* not lint */
14 
15 #include <sys/param.h>
16 #include <sys/ucred.h>
17 #include <sys/stat.h>
18 #include <sys/wait.h>
19 #include <sys/mount.h>
20 
21 #include <err.h>
22 #include <errno.h>
23 #include <fnmatch.h>
24 #include <fts.h>
25 #include <grp.h>
26 #include <pwd.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <tzfile.h>
31 #include <unistd.h>
32 
33 #include "find.h"
34 
35 #define	COMPARE(a, b) {							\
36 	switch (plan->flags) {						\
37 	case F_EQUAL:							\
38 		return (a == b);					\
39 	case F_LESSTHAN:						\
40 		return (a < b);						\
41 	case F_GREATER:							\
42 		return (a > b);						\
43 	default:							\
44 		abort();						\
45 	}								\
46 }
47 
48 static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
49 
50 /*
51  * find_parsenum --
52  *	Parse a string of the form [+-]# and return the value.
53  */
54 static long
55 find_parsenum(plan, option, vp, endch)
56 	PLAN *plan;
57 	char *option, *vp, *endch;
58 {
59 	long value;
60 	char *endchar, *str;	/* Pointer to character ending conversion. */
61 
62 	/* Determine comparison from leading + or -. */
63 	str = vp;
64 	switch (*str) {
65 	case '+':
66 		++str;
67 		plan->flags = F_GREATER;
68 		break;
69 	case '-':
70 		++str;
71 		plan->flags = F_LESSTHAN;
72 		break;
73 	default:
74 		plan->flags = F_EQUAL;
75 		break;
76 	}
77 
78 	/*
79 	 * Convert the string with strtol().  Note, if strtol() returns zero
80 	 * and endchar points to the beginning of the string we know we have
81 	 * a syntax error.
82 	 */
83 	value = strtol(str, &endchar, 10);
84 	if (value == 0 && endchar == str)
85 		errx(1, "%s: %s: illegal numeric value", option, vp);
86 	if (endchar[0] && (endch == NULL || endchar[0] != *endch))
87 		errx(1, "%s: %s: illegal trailing character", option, vp);
88 	if (endch)
89 		*endch = endchar[0];
90 	return (value);
91 }
92 
93 /*
94  * The value of n for the inode times (atime, ctime, and mtime) is a range,
95  * i.e. n matches from (n - 1) to n 24 hour periods.  This interacts with
96  * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
97  * user wanted.  Correct so that -1 is "less than 1".
98  */
99 #define	TIME_CORRECT(p, ttype)						\
100 	if ((p)->type == ttype && (p)->flags == F_LESSTHAN)		\
101 		++((p)->t_data);
102 
103 /*
104  * -atime n functions --
105  *
106  *	True if the difference between the file access time and the
107  *	current time is n 24 hour periods.
108  */
109 int
110 f_atime(plan, entry)
111 	PLAN *plan;
112 	FTSENT *entry;
113 {
114 	extern time_t now;
115 
116 	COMPARE((now - entry->fts_statp->st_atime +
117 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
118 }
119 
120 PLAN *
121 c_atime(arg)
122 	char *arg;
123 {
124 	PLAN *new;
125 
126 	ftsoptions &= ~FTS_NOSTAT;
127 
128 	new = palloc(N_ATIME, f_atime);
129 	new->t_data = find_parsenum(new, "-atime", arg, NULL);
130 	TIME_CORRECT(new, N_ATIME);
131 	return (new);
132 }
133 /*
134  * -ctime n functions --
135  *
136  *	True if the difference between the last change of file
137  *	status information and the current time is n 24 hour periods.
138  */
139 int
140 f_ctime(plan, entry)
141 	PLAN *plan;
142 	FTSENT *entry;
143 {
144 	extern time_t now;
145 
146 	COMPARE((now - entry->fts_statp->st_ctime +
147 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
148 }
149 
150 PLAN *
151 c_ctime(arg)
152 	char *arg;
153 {
154 	PLAN *new;
155 
156 	ftsoptions &= ~FTS_NOSTAT;
157 
158 	new = palloc(N_CTIME, f_ctime);
159 	new->t_data = find_parsenum(new, "-ctime", arg, NULL);
160 	TIME_CORRECT(new, N_CTIME);
161 	return (new);
162 }
163 
164 /*
165  * -depth functions --
166  *
167  *	Always true, causes descent of the directory hierarchy to be done
168  *	so that all entries in a directory are acted on before the directory
169  *	itself.
170  */
171 int
172 f_always_true(plan, entry)
173 	PLAN *plan;
174 	FTSENT *entry;
175 {
176 	return (1);
177 }
178 
179 PLAN *
180 c_depth()
181 {
182 	isdepth = 1;
183 
184 	return (palloc(N_DEPTH, f_always_true));
185 }
186 
187 /*
188  * [-exec | -ok] utility [arg ... ] ; functions --
189  *
190  *	True if the executed utility returns a zero value as exit status.
191  *	The end of the primary expression is delimited by a semicolon.  If
192  *	"{}" occurs anywhere, it gets replaced by the current pathname.
193  *	The current directory for the execution of utility is the same as
194  *	the current directory when the find utility was started.
195  *
196  *	The primary -ok is different in that it requests affirmation of the
197  *	user before executing the utility.
198  */
199 int
200 f_exec(plan, entry)
201 	register PLAN *plan;
202 	FTSENT *entry;
203 {
204 	extern int dotfd;
205 	register int cnt;
206 	pid_t pid;
207 	int status;
208 
209 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
210 		if (plan->e_len[cnt])
211 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
212 			    entry->fts_path, plan->e_len[cnt]);
213 
214 	if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
215 		return (0);
216 
217 	switch (pid = vfork()) {
218 	case -1:
219 		err(1, "fork");
220 		/* NOTREACHED */
221 	case 0:
222 		if (fchdir(dotfd)) {
223 			warn("chdir");
224 			_exit(1);
225 		}
226 		execvp(plan->e_argv[0], plan->e_argv);
227 		warn("%s", plan->e_argv[0]);
228 		_exit(1);
229 	}
230 	pid = waitpid(pid, &status, 0);
231 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
232 }
233 
234 /*
235  * c_exec --
236  *	build three parallel arrays, one with pointers to the strings passed
237  *	on the command line, one with (possibly duplicated) pointers to the
238  *	argv array, and one with integer values that are lengths of the
239  *	strings, but also flags meaning that the string has to be massaged.
240  */
241 PLAN *
242 c_exec(argvp, isok)
243 	char ***argvp;
244 	int isok;
245 {
246 	PLAN *new;			/* node returned */
247 	register int cnt;
248 	register char **argv, **ap, *p;
249 
250 	isoutput = 1;
251 
252 	new = palloc(N_EXEC, f_exec);
253 	if (isok)
254 		new->flags = F_NEEDOK;
255 
256 	for (ap = argv = *argvp;; ++ap) {
257 		if (!*ap)
258 			errx(1,
259 			    "%s: no terminating \";\"", isok ? "-ok" : "-exec");
260 		if (**ap == ';')
261 			break;
262 	}
263 
264 	cnt = ap - *argvp + 1;
265 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
266 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
267 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
268 
269 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
270 		new->e_orig[cnt] = *argv;
271 		for (p = *argv; *p; ++p)
272 			if (p[0] == '{' && p[1] == '}') {
273 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
274 				new->e_len[cnt] = MAXPATHLEN;
275 				break;
276 			}
277 		if (!*p) {
278 			new->e_argv[cnt] = *argv;
279 			new->e_len[cnt] = 0;
280 		}
281 	}
282 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
283 
284 	*argvp = argv + 1;
285 	return (new);
286 }
287 
288 /*
289  * -follow functions --
290  *
291  *	Always true, causes symbolic links to be followed on a global
292  *	basis.
293  */
294 PLAN *
295 c_follow()
296 {
297 	ftsoptions &= ~FTS_PHYSICAL;
298 	ftsoptions |= FTS_LOGICAL;
299 
300 	return (palloc(N_FOLLOW, f_always_true));
301 }
302 
303 /*
304  * -fstype functions --
305  *
306  *	True if the file is of a certain type.
307  */
308 int
309 f_fstype(plan, entry)
310 	PLAN *plan;
311 	FTSENT *entry;
312 {
313 	static dev_t curdev;	/* need a guaranteed illegal dev value */
314 	static int first = 1;
315 	struct statfs sb;
316 	static short val;
317 	char *p, save[2];
318 
319 	/* Only check when we cross mount point. */
320 	if (first || curdev != entry->fts_statp->st_dev) {
321 		curdev = entry->fts_statp->st_dev;
322 
323 		/*
324 		 * Statfs follows symlinks; find wants the link's file system,
325 		 * not where it points.
326 		 */
327 		if (entry->fts_info == FTS_SL ||
328 		    entry->fts_info == FTS_SLNONE) {
329 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
330 				++p;
331 			else
332 				p = entry->fts_accpath;
333 			save[0] = p[0];
334 			p[0] = '.';
335 			save[1] = p[1];
336 			p[1] = '\0';
337 
338 		} else
339 			p = NULL;
340 
341 		if (statfs(entry->fts_accpath, &sb))
342 			err(1, "%s", entry->fts_accpath);
343 
344 		if (p) {
345 			p[0] = save[0];
346 			p[1] = save[1];
347 		}
348 
349 		first = 0;
350 
351 		/*
352 		 * Further tests may need both of these values, so
353 		 * always copy both of them.
354 		 */
355 		val = sb.f_flags;
356 		val = sb.f_type;
357 	}
358 	switch (plan->flags) {
359 	case F_MTFLAG:
360 		return (val & plan->mt_data);
361 	case F_MTTYPE:
362 		return (val == plan->mt_data);
363 	default:
364 		abort();
365 	}
366 }
367 
368 PLAN *
369 c_fstype(arg)
370 	char *arg;
371 {
372 	register PLAN *new;
373 
374 	ftsoptions &= ~FTS_NOSTAT;
375 
376 	new = palloc(N_FSTYPE, f_fstype);
377 	switch (*arg) {
378 	case 'l':
379 		if (!strcmp(arg, "local")) {
380 			new->flags = F_MTFLAG;
381 			new->mt_data = MNT_LOCAL;
382 			return (new);
383 		}
384 		break;
385 	case 'm':
386 		if (!strcmp(arg, "mfs")) {
387 			new->flags = F_MTTYPE;
388 			new->mt_data = MOUNT_MFS;
389 			return (new);
390 		}
391 		break;
392 	case 'n':
393 		if (!strcmp(arg, "nfs")) {
394 			new->flags = F_MTTYPE;
395 			new->mt_data = MOUNT_NFS;
396 			return (new);
397 		}
398 		break;
399 	case 'p':
400 		if (!strcmp(arg, "msdos")) {
401 			new->flags = F_MTTYPE;
402 			new->mt_data = MOUNT_MSDOS;
403 			return (new);
404 		}
405 		break;
406 	case 'r':
407 		if (!strcmp(arg, "rdonly")) {
408 			new->flags = F_MTFLAG;
409 			new->mt_data = MNT_RDONLY;
410 			return (new);
411 		}
412 		break;
413 	case 'u':
414 		if (!strcmp(arg, "ufs")) {
415 			new->flags = F_MTTYPE;
416 			new->mt_data = MOUNT_UFS;
417 			return (new);
418 		}
419 		break;
420 	}
421 	errx(1, "%s: unknown file type", arg);
422 	/* NOTREACHED */
423 }
424 
425 /*
426  * -group gname functions --
427  *
428  *	True if the file belongs to the group gname.  If gname is numeric and
429  *	an equivalent of the getgrnam() function does not return a valid group
430  *	name, gname is taken as a group ID.
431  */
432 int
433 f_group(plan, entry)
434 	PLAN *plan;
435 	FTSENT *entry;
436 {
437 	return (entry->fts_statp->st_gid == plan->g_data);
438 }
439 
440 PLAN *
441 c_group(gname)
442 	char *gname;
443 {
444 	PLAN *new;
445 	struct group *g;
446 	gid_t gid;
447 
448 	ftsoptions &= ~FTS_NOSTAT;
449 
450 	g = getgrnam(gname);
451 	if (g == NULL) {
452 		gid = atoi(gname);
453 		if (gid == 0 && gname[0] != '0')
454 			errx(1, "-group: %s: no such group", gname);
455 	} else
456 		gid = g->gr_gid;
457 
458 	new = palloc(N_GROUP, f_group);
459 	new->g_data = gid;
460 	return (new);
461 }
462 
463 /*
464  * -inum n functions --
465  *
466  *	True if the file has inode # n.
467  */
468 int
469 f_inum(plan, entry)
470 	PLAN *plan;
471 	FTSENT *entry;
472 {
473 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
474 }
475 
476 PLAN *
477 c_inum(arg)
478 	char *arg;
479 {
480 	PLAN *new;
481 
482 	ftsoptions &= ~FTS_NOSTAT;
483 
484 	new = palloc(N_INUM, f_inum);
485 	new->i_data = find_parsenum(new, "-inum", arg, NULL);
486 	return (new);
487 }
488 
489 /*
490  * -links n functions --
491  *
492  *	True if the file has n links.
493  */
494 int
495 f_links(plan, entry)
496 	PLAN *plan;
497 	FTSENT *entry;
498 {
499 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
500 }
501 
502 PLAN *
503 c_links(arg)
504 	char *arg;
505 {
506 	PLAN *new;
507 
508 	ftsoptions &= ~FTS_NOSTAT;
509 
510 	new = palloc(N_LINKS, f_links);
511 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
512 	return (new);
513 }
514 
515 /*
516  * -ls functions --
517  *
518  *	Always true - prints the current entry to stdout in "ls" format.
519  */
520 int
521 f_ls(plan, entry)
522 	PLAN *plan;
523 	FTSENT *entry;
524 {
525 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
526 	return (1);
527 }
528 
529 PLAN *
530 c_ls()
531 {
532 	ftsoptions &= ~FTS_NOSTAT;
533 	isoutput = 1;
534 
535 	return (palloc(N_LS, f_ls));
536 }
537 
538 /*
539  * -mtime n functions --
540  *
541  *	True if the difference between the file modification time and the
542  *	current time is n 24 hour periods.
543  */
544 int
545 f_mtime(plan, entry)
546 	PLAN *plan;
547 	FTSENT *entry;
548 {
549 	extern time_t now;
550 
551 	COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
552 	    SECSPERDAY, plan->t_data);
553 }
554 
555 PLAN *
556 c_mtime(arg)
557 	char *arg;
558 {
559 	PLAN *new;
560 
561 	ftsoptions &= ~FTS_NOSTAT;
562 
563 	new = palloc(N_MTIME, f_mtime);
564 	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
565 	TIME_CORRECT(new, N_MTIME);
566 	return (new);
567 }
568 
569 /*
570  * -name functions --
571  *
572  *	True if the basename of the filename being examined
573  *	matches pattern using Pattern Matching Notation S3.14
574  */
575 int
576 f_name(plan, entry)
577 	PLAN *plan;
578 	FTSENT *entry;
579 {
580 	return (!fnmatch(plan->c_data, entry->fts_name, 0));
581 }
582 
583 PLAN *
584 c_name(pattern)
585 	char *pattern;
586 {
587 	PLAN *new;
588 
589 	new = palloc(N_NAME, f_name);
590 	new->c_data = pattern;
591 	return (new);
592 }
593 
594 /*
595  * -newer file functions --
596  *
597  *	True if the current file has been modified more recently
598  *	then the modification time of the file named by the pathname
599  *	file.
600  */
601 int
602 f_newer(plan, entry)
603 	PLAN *plan;
604 	FTSENT *entry;
605 {
606 	return (entry->fts_statp->st_mtime > plan->t_data);
607 }
608 
609 PLAN *
610 c_newer(filename)
611 	char *filename;
612 {
613 	PLAN *new;
614 	struct stat sb;
615 
616 	ftsoptions &= ~FTS_NOSTAT;
617 
618 	if (stat(filename, &sb))
619 		err(1, "%s", filename);
620 	new = palloc(N_NEWER, f_newer);
621 	new->t_data = sb.st_mtime;
622 	return (new);
623 }
624 
625 /*
626  * -nogroup functions --
627  *
628  *	True if file belongs to a user ID for which the equivalent
629  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
630  */
631 int
632 f_nogroup(plan, entry)
633 	PLAN *plan;
634 	FTSENT *entry;
635 {
636 	char *group_from_gid();
637 
638 	return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
639 }
640 
641 PLAN *
642 c_nogroup()
643 {
644 	ftsoptions &= ~FTS_NOSTAT;
645 
646 	return (palloc(N_NOGROUP, f_nogroup));
647 }
648 
649 /*
650  * -nouser functions --
651  *
652  *	True if file belongs to a user ID for which the equivalent
653  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
654  */
655 int
656 f_nouser(plan, entry)
657 	PLAN *plan;
658 	FTSENT *entry;
659 {
660 	char *user_from_uid();
661 
662 	return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
663 }
664 
665 PLAN *
666 c_nouser()
667 {
668 	ftsoptions &= ~FTS_NOSTAT;
669 
670 	return (palloc(N_NOUSER, f_nouser));
671 }
672 
673 /*
674  * -path functions --
675  *
676  *	True if the path of the filename being examined
677  *	matches pattern using Pattern Matching Notation S3.14
678  */
679 int
680 f_path(plan, entry)
681 	PLAN *plan;
682 	FTSENT *entry;
683 {
684 	return (!fnmatch(plan->c_data, entry->fts_path, 0));
685 }
686 
687 PLAN *
688 c_path(pattern)
689 	char *pattern;
690 {
691 	PLAN *new;
692 
693 	new = palloc(N_NAME, f_path);
694 	new->c_data = pattern;
695 	return (new);
696 }
697 
698 /*
699  * -perm functions --
700  *
701  *	The mode argument is used to represent file mode bits.  If it starts
702  *	with a leading digit, it's treated as an octal mode, otherwise as a
703  *	symbolic mode.
704  */
705 int
706 f_perm(plan, entry)
707 	PLAN *plan;
708 	FTSENT *entry;
709 {
710 	mode_t mode;
711 
712 	mode = entry->fts_statp->st_mode &
713 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
714 	if (plan->flags == F_ATLEAST)
715 		return ((plan->m_data | mode) == mode);
716 	else
717 		return (mode == plan->m_data);
718 	/* NOTREACHED */
719 }
720 
721 PLAN *
722 c_perm(perm)
723 	char *perm;
724 {
725 	PLAN *new;
726 	mode_t *set;
727 
728 	ftsoptions &= ~FTS_NOSTAT;
729 
730 	new = palloc(N_PERM, f_perm);
731 
732 	if (*perm == '-') {
733 		new->flags = F_ATLEAST;
734 		++perm;
735 	}
736 
737 	if ((set = setmode(perm)) == NULL)
738 		err(1, "-perm: %s: illegal mode string", perm);
739 
740 	new->m_data = getmode(set, 0);
741 	return (new);
742 }
743 
744 /*
745  * -print functions --
746  *
747  *	Always true, causes the current pathame to be written to
748  *	standard output.
749  */
750 int
751 f_print(plan, entry)
752 	PLAN *plan;
753 	FTSENT *entry;
754 {
755 	(void)printf("%s\n", entry->fts_path);
756 	return (1);
757 }
758 
759 PLAN *
760 c_print()
761 {
762 	isoutput = 1;
763 
764 	return (palloc(N_PRINT, f_print));
765 }
766 
767 /*
768  * -prune functions --
769  *
770  *	Prune a portion of the hierarchy.
771  */
772 int
773 f_prune(plan, entry)
774 	PLAN *plan;
775 	FTSENT *entry;
776 {
777 	extern FTS *tree;
778 
779 	if (fts_set(tree, entry, FTS_SKIP))
780 		err(1, "%s", entry->fts_path);
781 	return (1);
782 }
783 
784 PLAN *
785 c_prune()
786 {
787 	return (palloc(N_PRUNE, f_prune));
788 }
789 
790 /*
791  * -size n[c] functions --
792  *
793  *	True if the file size in bytes, divided by an implementation defined
794  *	value and rounded up to the next integer, is n.  If n is followed by
795  *	a c, the size is in bytes.
796  */
797 #define	FIND_SIZE	512
798 static int divsize = 1;
799 
800 int
801 f_size(plan, entry)
802 	PLAN *plan;
803 	FTSENT *entry;
804 {
805 	off_t size;
806 
807 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
808 	    FIND_SIZE : entry->fts_statp->st_size;
809 	COMPARE(size, plan->o_data);
810 }
811 
812 PLAN *
813 c_size(arg)
814 	char *arg;
815 {
816 	PLAN *new;
817 	char endch;
818 
819 	ftsoptions &= ~FTS_NOSTAT;
820 
821 	new = palloc(N_SIZE, f_size);
822 	endch = 'c';
823 	new->o_data = find_parsenum(new, "-size", arg, &endch);
824 	if (endch == 'c')
825 		divsize = 0;
826 	return (new);
827 }
828 
829 /*
830  * -type c functions --
831  *
832  *	True if the type of the file is c, where c is b, c, d, p, f or w
833  *	for block special file, character special file, directory, FIFO,
834  *	regular file or whiteout respectively.
835  */
836 int
837 f_type(plan, entry)
838 	PLAN *plan;
839 	FTSENT *entry;
840 {
841 	return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
842 }
843 
844 PLAN *
845 c_type(typestring)
846 	char *typestring;
847 {
848 	PLAN *new;
849 	mode_t  mask;
850 
851 	ftsoptions &= ~FTS_NOSTAT;
852 
853 	switch (typestring[0]) {
854 	case 'b':
855 		mask = S_IFBLK;
856 		break;
857 	case 'c':
858 		mask = S_IFCHR;
859 		break;
860 	case 'd':
861 		mask = S_IFDIR;
862 		break;
863 	case 'f':
864 		mask = S_IFREG;
865 		break;
866 	case 'l':
867 		mask = S_IFLNK;
868 		break;
869 	case 'p':
870 		mask = S_IFIFO;
871 		break;
872 	case 's':
873 		mask = S_IFSOCK;
874 		break;
875 #ifdef FTS_WHITEOUT
876 	case 'w':
877 		mask = S_IFWHT;
878 		ftsoptions |= FTS_WHITEOUT;
879 		break;
880 #endif /* FTS_WHITEOUT */
881 	default:
882 		errx(1, "-type: %s: unknown type", typestring);
883 	}
884 
885 	new = palloc(N_TYPE, f_type);
886 	new->m_data = mask;
887 	return (new);
888 }
889 
890 /*
891  * -user uname functions --
892  *
893  *	True if the file belongs to the user uname.  If uname is numeric and
894  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
895  *	return a valid user name, uname is taken as a user ID.
896  */
897 int
898 f_user(plan, entry)
899 	PLAN *plan;
900 	FTSENT *entry;
901 {
902 	return (entry->fts_statp->st_uid == plan->u_data);
903 }
904 
905 PLAN *
906 c_user(username)
907 	char *username;
908 {
909 	PLAN *new;
910 	struct passwd *p;
911 	uid_t uid;
912 
913 	ftsoptions &= ~FTS_NOSTAT;
914 
915 	p = getpwnam(username);
916 	if (p == NULL) {
917 		uid = atoi(username);
918 		if (uid == 0 && username[0] != '0')
919 			errx(1, "-user: %s: no such user", username);
920 	} else
921 		uid = p->pw_uid;
922 
923 	new = palloc(N_USER, f_user);
924 	new->u_data = uid;
925 	return (new);
926 }
927 
928 /*
929  * -xdev functions --
930  *
931  *	Always true, causes find not to decend past directories that have a
932  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
933  */
934 PLAN *
935 c_xdev()
936 {
937 	ftsoptions |= FTS_XDEV;
938 
939 	return (palloc(N_XDEV, f_always_true));
940 }
941 
942 /*
943  * ( expression ) functions --
944  *
945  *	True if expression is true.
946  */
947 int
948 f_expr(plan, entry)
949 	PLAN *plan;
950 	FTSENT *entry;
951 {
952 	register PLAN *p;
953 	register int state;
954 
955 	for (p = plan->p_data[0];
956 	    p && (state = (p->eval)(p, entry)); p = p->next);
957 	return (state);
958 }
959 
960 /*
961  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
962  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
963  * to a N_EXPR node containing the expression and the ')' node is discarded.
964  */
965 PLAN *
966 c_openparen()
967 {
968 	return (palloc(N_OPENPAREN, (int (*)())-1));
969 }
970 
971 PLAN *
972 c_closeparen()
973 {
974 	return (palloc(N_CLOSEPAREN, (int (*)())-1));
975 }
976 
977 /*
978  * ! expression functions --
979  *
980  *	Negation of a primary; the unary NOT operator.
981  */
982 int
983 f_not(plan, entry)
984 	PLAN *plan;
985 	FTSENT *entry;
986 {
987 	register PLAN *p;
988 	register int state;
989 
990 	for (p = plan->p_data[0];
991 	    p && (state = (p->eval)(p, entry)); p = p->next);
992 	return (!state);
993 }
994 
995 PLAN *
996 c_not()
997 {
998 	return (palloc(N_NOT, f_not));
999 }
1000 
1001 /*
1002  * expression -o expression functions --
1003  *
1004  *	Alternation of primaries; the OR operator.  The second expression is
1005  * not evaluated if the first expression is true.
1006  */
1007 int
1008 f_or(plan, entry)
1009 	PLAN *plan;
1010 	FTSENT *entry;
1011 {
1012 	register PLAN *p;
1013 	register int state;
1014 
1015 	for (p = plan->p_data[0];
1016 	    p && (state = (p->eval)(p, entry)); p = p->next);
1017 
1018 	if (state)
1019 		return (1);
1020 
1021 	for (p = plan->p_data[1];
1022 	    p && (state = (p->eval)(p, entry)); p = p->next);
1023 	return (state);
1024 }
1025 
1026 PLAN *
1027 c_or()
1028 {
1029 	return (palloc(N_OR, f_or));
1030 }
1031 
1032 static PLAN *
1033 palloc(t, f)
1034 	enum ntype t;
1035 	int (*f) __P((PLAN *, FTSENT *));
1036 {
1037 	PLAN *new;
1038 
1039 	if ((new = malloc(sizeof(PLAN))) == NULL)
1040 		err(1, NULL);
1041 	new->type = t;
1042 	new->eval = f;
1043 	new->flags = 0;
1044 	new->next = NULL;
1045 	return (new);
1046 }
1047