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