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