xref: /original-bsd/usr.bin/find/function.c (revision 4e8da28c)
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.10 (Berkeley) 05/04/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 	struct vfsconf vfc;
374 
375 	ftsoptions &= ~FTS_NOSTAT;
376 
377 	new = palloc(N_FSTYPE, f_fstype);
378 
379 	/*
380 	 * Check first for a filesystem name.
381 	 */
382 	if (getvfsbyname(arg, &vfc) == 0) {
383 		new->flags = F_MTTYPE;
384 		new->mt_data = vfc.vfc_typenum;
385 		return (new);
386 	}
387 
388 	switch (*arg) {
389 	case 'l':
390 		if (!strcmp(arg, "local")) {
391 			new->flags = F_MTFLAG;
392 			new->mt_data = MNT_LOCAL;
393 			return (new);
394 		}
395 		break;
396 	case 'r':
397 		if (!strcmp(arg, "rdonly")) {
398 			new->flags = F_MTFLAG;
399 			new->mt_data = MNT_RDONLY;
400 			return (new);
401 		}
402 		break;
403 	}
404 	errx(1, "%s: unknown file type", arg);
405 	/* NOTREACHED */
406 }
407 
408 /*
409  * -group gname functions --
410  *
411  *	True if the file belongs to the group gname.  If gname is numeric and
412  *	an equivalent of the getgrnam() function does not return a valid group
413  *	name, gname is taken as a group ID.
414  */
415 int
416 f_group(plan, entry)
417 	PLAN *plan;
418 	FTSENT *entry;
419 {
420 	return (entry->fts_statp->st_gid == plan->g_data);
421 }
422 
423 PLAN *
424 c_group(gname)
425 	char *gname;
426 {
427 	PLAN *new;
428 	struct group *g;
429 	gid_t gid;
430 
431 	ftsoptions &= ~FTS_NOSTAT;
432 
433 	g = getgrnam(gname);
434 	if (g == NULL) {
435 		gid = atoi(gname);
436 		if (gid == 0 && gname[0] != '0')
437 			errx(1, "-group: %s: no such group", gname);
438 	} else
439 		gid = g->gr_gid;
440 
441 	new = palloc(N_GROUP, f_group);
442 	new->g_data = gid;
443 	return (new);
444 }
445 
446 /*
447  * -inum n functions --
448  *
449  *	True if the file has inode # n.
450  */
451 int
452 f_inum(plan, entry)
453 	PLAN *plan;
454 	FTSENT *entry;
455 {
456 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
457 }
458 
459 PLAN *
460 c_inum(arg)
461 	char *arg;
462 {
463 	PLAN *new;
464 
465 	ftsoptions &= ~FTS_NOSTAT;
466 
467 	new = palloc(N_INUM, f_inum);
468 	new->i_data = find_parsenum(new, "-inum", arg, NULL);
469 	return (new);
470 }
471 
472 /*
473  * -links n functions --
474  *
475  *	True if the file has n links.
476  */
477 int
478 f_links(plan, entry)
479 	PLAN *plan;
480 	FTSENT *entry;
481 {
482 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
483 }
484 
485 PLAN *
486 c_links(arg)
487 	char *arg;
488 {
489 	PLAN *new;
490 
491 	ftsoptions &= ~FTS_NOSTAT;
492 
493 	new = palloc(N_LINKS, f_links);
494 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
495 	return (new);
496 }
497 
498 /*
499  * -ls functions --
500  *
501  *	Always true - prints the current entry to stdout in "ls" format.
502  */
503 int
504 f_ls(plan, entry)
505 	PLAN *plan;
506 	FTSENT *entry;
507 {
508 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
509 	return (1);
510 }
511 
512 PLAN *
513 c_ls()
514 {
515 	ftsoptions &= ~FTS_NOSTAT;
516 	isoutput = 1;
517 
518 	return (palloc(N_LS, f_ls));
519 }
520 
521 /*
522  * -mtime n functions --
523  *
524  *	True if the difference between the file modification time and the
525  *	current time is n 24 hour periods.
526  */
527 int
528 f_mtime(plan, entry)
529 	PLAN *plan;
530 	FTSENT *entry;
531 {
532 	extern time_t now;
533 
534 	COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
535 	    SECSPERDAY, plan->t_data);
536 }
537 
538 PLAN *
539 c_mtime(arg)
540 	char *arg;
541 {
542 	PLAN *new;
543 
544 	ftsoptions &= ~FTS_NOSTAT;
545 
546 	new = palloc(N_MTIME, f_mtime);
547 	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
548 	TIME_CORRECT(new, N_MTIME);
549 	return (new);
550 }
551 
552 /*
553  * -name functions --
554  *
555  *	True if the basename of the filename being examined
556  *	matches pattern using Pattern Matching Notation S3.14
557  */
558 int
559 f_name(plan, entry)
560 	PLAN *plan;
561 	FTSENT *entry;
562 {
563 	return (!fnmatch(plan->c_data, entry->fts_name, 0));
564 }
565 
566 PLAN *
567 c_name(pattern)
568 	char *pattern;
569 {
570 	PLAN *new;
571 
572 	new = palloc(N_NAME, f_name);
573 	new->c_data = pattern;
574 	return (new);
575 }
576 
577 /*
578  * -newer file functions --
579  *
580  *	True if the current file has been modified more recently
581  *	then the modification time of the file named by the pathname
582  *	file.
583  */
584 int
585 f_newer(plan, entry)
586 	PLAN *plan;
587 	FTSENT *entry;
588 {
589 	return (entry->fts_statp->st_mtime > plan->t_data);
590 }
591 
592 PLAN *
593 c_newer(filename)
594 	char *filename;
595 {
596 	PLAN *new;
597 	struct stat sb;
598 
599 	ftsoptions &= ~FTS_NOSTAT;
600 
601 	if (stat(filename, &sb))
602 		err(1, "%s", filename);
603 	new = palloc(N_NEWER, f_newer);
604 	new->t_data = sb.st_mtime;
605 	return (new);
606 }
607 
608 /*
609  * -nogroup functions --
610  *
611  *	True if file belongs to a user ID for which the equivalent
612  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
613  */
614 int
615 f_nogroup(plan, entry)
616 	PLAN *plan;
617 	FTSENT *entry;
618 {
619 	char *group_from_gid();
620 
621 	return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
622 }
623 
624 PLAN *
625 c_nogroup()
626 {
627 	ftsoptions &= ~FTS_NOSTAT;
628 
629 	return (palloc(N_NOGROUP, f_nogroup));
630 }
631 
632 /*
633  * -nouser functions --
634  *
635  *	True if file belongs to a user ID for which the equivalent
636  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
637  */
638 int
639 f_nouser(plan, entry)
640 	PLAN *plan;
641 	FTSENT *entry;
642 {
643 	char *user_from_uid();
644 
645 	return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
646 }
647 
648 PLAN *
649 c_nouser()
650 {
651 	ftsoptions &= ~FTS_NOSTAT;
652 
653 	return (palloc(N_NOUSER, f_nouser));
654 }
655 
656 /*
657  * -path functions --
658  *
659  *	True if the path of the filename being examined
660  *	matches pattern using Pattern Matching Notation S3.14
661  */
662 int
663 f_path(plan, entry)
664 	PLAN *plan;
665 	FTSENT *entry;
666 {
667 	return (!fnmatch(plan->c_data, entry->fts_path, 0));
668 }
669 
670 PLAN *
671 c_path(pattern)
672 	char *pattern;
673 {
674 	PLAN *new;
675 
676 	new = palloc(N_NAME, f_path);
677 	new->c_data = pattern;
678 	return (new);
679 }
680 
681 /*
682  * -perm functions --
683  *
684  *	The mode argument is used to represent file mode bits.  If it starts
685  *	with a leading digit, it's treated as an octal mode, otherwise as a
686  *	symbolic mode.
687  */
688 int
689 f_perm(plan, entry)
690 	PLAN *plan;
691 	FTSENT *entry;
692 {
693 	mode_t mode;
694 
695 	mode = entry->fts_statp->st_mode &
696 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
697 	if (plan->flags == F_ATLEAST)
698 		return ((plan->m_data | mode) == mode);
699 	else
700 		return (mode == plan->m_data);
701 	/* NOTREACHED */
702 }
703 
704 PLAN *
705 c_perm(perm)
706 	char *perm;
707 {
708 	PLAN *new;
709 	mode_t *set;
710 
711 	ftsoptions &= ~FTS_NOSTAT;
712 
713 	new = palloc(N_PERM, f_perm);
714 
715 	if (*perm == '-') {
716 		new->flags = F_ATLEAST;
717 		++perm;
718 	}
719 
720 	if ((set = setmode(perm)) == NULL)
721 		err(1, "-perm: %s: illegal mode string", perm);
722 
723 	new->m_data = getmode(set, 0);
724 	return (new);
725 }
726 
727 /*
728  * -print functions --
729  *
730  *	Always true, causes the current pathame to be written to
731  *	standard output.
732  */
733 int
734 f_print(plan, entry)
735 	PLAN *plan;
736 	FTSENT *entry;
737 {
738 	(void)printf("%s\n", entry->fts_path);
739 	return (1);
740 }
741 
742 PLAN *
743 c_print()
744 {
745 	isoutput = 1;
746 
747 	return (palloc(N_PRINT, f_print));
748 }
749 
750 /*
751  * -prune functions --
752  *
753  *	Prune a portion of the hierarchy.
754  */
755 int
756 f_prune(plan, entry)
757 	PLAN *plan;
758 	FTSENT *entry;
759 {
760 	extern FTS *tree;
761 
762 	if (fts_set(tree, entry, FTS_SKIP))
763 		err(1, "%s", entry->fts_path);
764 	return (1);
765 }
766 
767 PLAN *
768 c_prune()
769 {
770 	return (palloc(N_PRUNE, f_prune));
771 }
772 
773 /*
774  * -size n[c] functions --
775  *
776  *	True if the file size in bytes, divided by an implementation defined
777  *	value and rounded up to the next integer, is n.  If n is followed by
778  *	a c, the size is in bytes.
779  */
780 #define	FIND_SIZE	512
781 static int divsize = 1;
782 
783 int
784 f_size(plan, entry)
785 	PLAN *plan;
786 	FTSENT *entry;
787 {
788 	off_t size;
789 
790 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
791 	    FIND_SIZE : entry->fts_statp->st_size;
792 	COMPARE(size, plan->o_data);
793 }
794 
795 PLAN *
796 c_size(arg)
797 	char *arg;
798 {
799 	PLAN *new;
800 	char endch;
801 
802 	ftsoptions &= ~FTS_NOSTAT;
803 
804 	new = palloc(N_SIZE, f_size);
805 	endch = 'c';
806 	new->o_data = find_parsenum(new, "-size", arg, &endch);
807 	if (endch == 'c')
808 		divsize = 0;
809 	return (new);
810 }
811 
812 /*
813  * -type c functions --
814  *
815  *	True if the type of the file is c, where c is b, c, d, p, f or w
816  *	for block special file, character special file, directory, FIFO,
817  *	regular file or whiteout respectively.
818  */
819 int
820 f_type(plan, entry)
821 	PLAN *plan;
822 	FTSENT *entry;
823 {
824 	return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
825 }
826 
827 PLAN *
828 c_type(typestring)
829 	char *typestring;
830 {
831 	PLAN *new;
832 	mode_t  mask;
833 
834 	ftsoptions &= ~FTS_NOSTAT;
835 
836 	switch (typestring[0]) {
837 	case 'b':
838 		mask = S_IFBLK;
839 		break;
840 	case 'c':
841 		mask = S_IFCHR;
842 		break;
843 	case 'd':
844 		mask = S_IFDIR;
845 		break;
846 	case 'f':
847 		mask = S_IFREG;
848 		break;
849 	case 'l':
850 		mask = S_IFLNK;
851 		break;
852 	case 'p':
853 		mask = S_IFIFO;
854 		break;
855 	case 's':
856 		mask = S_IFSOCK;
857 		break;
858 #ifdef FTS_WHITEOUT
859 	case 'w':
860 		mask = S_IFWHT;
861 		ftsoptions |= FTS_WHITEOUT;
862 		break;
863 #endif /* FTS_WHITEOUT */
864 	default:
865 		errx(1, "-type: %s: unknown type", typestring);
866 	}
867 
868 	new = palloc(N_TYPE, f_type);
869 	new->m_data = mask;
870 	return (new);
871 }
872 
873 /*
874  * -user uname functions --
875  *
876  *	True if the file belongs to the user uname.  If uname is numeric and
877  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
878  *	return a valid user name, uname is taken as a user ID.
879  */
880 int
881 f_user(plan, entry)
882 	PLAN *plan;
883 	FTSENT *entry;
884 {
885 	return (entry->fts_statp->st_uid == plan->u_data);
886 }
887 
888 PLAN *
889 c_user(username)
890 	char *username;
891 {
892 	PLAN *new;
893 	struct passwd *p;
894 	uid_t uid;
895 
896 	ftsoptions &= ~FTS_NOSTAT;
897 
898 	p = getpwnam(username);
899 	if (p == NULL) {
900 		uid = atoi(username);
901 		if (uid == 0 && username[0] != '0')
902 			errx(1, "-user: %s: no such user", username);
903 	} else
904 		uid = p->pw_uid;
905 
906 	new = palloc(N_USER, f_user);
907 	new->u_data = uid;
908 	return (new);
909 }
910 
911 /*
912  * -xdev functions --
913  *
914  *	Always true, causes find not to decend past directories that have a
915  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
916  */
917 PLAN *
918 c_xdev()
919 {
920 	ftsoptions |= FTS_XDEV;
921 
922 	return (palloc(N_XDEV, f_always_true));
923 }
924 
925 /*
926  * ( expression ) functions --
927  *
928  *	True if expression is true.
929  */
930 int
931 f_expr(plan, entry)
932 	PLAN *plan;
933 	FTSENT *entry;
934 {
935 	register PLAN *p;
936 	register int state;
937 
938 	for (p = plan->p_data[0];
939 	    p && (state = (p->eval)(p, entry)); p = p->next);
940 	return (state);
941 }
942 
943 /*
944  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
945  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
946  * to a N_EXPR node containing the expression and the ')' node is discarded.
947  */
948 PLAN *
949 c_openparen()
950 {
951 	return (palloc(N_OPENPAREN, (int (*)())-1));
952 }
953 
954 PLAN *
955 c_closeparen()
956 {
957 	return (palloc(N_CLOSEPAREN, (int (*)())-1));
958 }
959 
960 /*
961  * ! expression functions --
962  *
963  *	Negation of a primary; the unary NOT operator.
964  */
965 int
966 f_not(plan, entry)
967 	PLAN *plan;
968 	FTSENT *entry;
969 {
970 	register PLAN *p;
971 	register int state;
972 
973 	for (p = plan->p_data[0];
974 	    p && (state = (p->eval)(p, entry)); p = p->next);
975 	return (!state);
976 }
977 
978 PLAN *
979 c_not()
980 {
981 	return (palloc(N_NOT, f_not));
982 }
983 
984 /*
985  * expression -o expression functions --
986  *
987  *	Alternation of primaries; the OR operator.  The second expression is
988  * not evaluated if the first expression is true.
989  */
990 int
991 f_or(plan, entry)
992 	PLAN *plan;
993 	FTSENT *entry;
994 {
995 	register PLAN *p;
996 	register int state;
997 
998 	for (p = plan->p_data[0];
999 	    p && (state = (p->eval)(p, entry)); p = p->next);
1000 
1001 	if (state)
1002 		return (1);
1003 
1004 	for (p = plan->p_data[1];
1005 	    p && (state = (p->eval)(p, entry)); p = p->next);
1006 	return (state);
1007 }
1008 
1009 PLAN *
1010 c_or()
1011 {
1012 	return (palloc(N_OR, f_or));
1013 }
1014 
1015 static PLAN *
1016 palloc(t, f)
1017 	enum ntype t;
1018 	int (*f) __P((PLAN *, FTSENT *));
1019 {
1020 	PLAN *new;
1021 
1022 	if ((new = malloc(sizeof(PLAN))) == NULL)
1023 		err(1, NULL);
1024 	new->type = t;
1025 	new->eval = f;
1026 	new->flags = 0;
1027 	new->next = NULL;
1028 	return (new);
1029 }
1030