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