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