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