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