xref: /freebsd/usr.bin/find/function.c (revision 230f8c40e55e3462e90151e30f61bd0fdd4dcda3)
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.6 (Berkeley) 4/1/94";
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  * -follow functions --
318  *
319  *	Always true, causes symbolic links to be followed on a global
320  *	basis.
321  */
322 PLAN *
323 c_follow()
324 {
325 	ftsoptions &= ~FTS_PHYSICAL;
326 	ftsoptions |= FTS_LOGICAL;
327 
328 	return (palloc(N_FOLLOW, f_always_true));
329 }
330 
331 /*
332  * -fstype functions --
333  *
334  *	True if the file is of a certain type.
335  */
336 int
337 f_fstype(plan, entry)
338 	PLAN *plan;
339 	FTSENT *entry;
340 {
341 	static dev_t curdev;	/* need a guaranteed illegal dev value */
342 	static int first = 1;
343 	struct statfs sb;
344 	static short val;
345 	char *p, save[2];
346 
347 	/* Only check when we cross mount point. */
348 	if (first || curdev != entry->fts_statp->st_dev) {
349 		curdev = entry->fts_statp->st_dev;
350 
351 		/*
352 		 * Statfs follows symlinks; find wants the link's file system,
353 		 * not where it points.
354 		 */
355 		if (entry->fts_info == FTS_SL ||
356 		    entry->fts_info == FTS_SLNONE) {
357 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
358 				++p;
359 			else
360 				p = entry->fts_accpath;
361 			save[0] = p[0];
362 			p[0] = '.';
363 			save[1] = p[1];
364 			p[1] = '\0';
365 
366 		} else
367 			p = NULL;
368 
369 		if (statfs(entry->fts_accpath, &sb))
370 			err(1, "%s", entry->fts_accpath);
371 
372 		if (p) {
373 			p[0] = save[0];
374 			p[1] = save[1];
375 		}
376 
377 		first = 0;
378 		switch (plan->flags) {
379 		case F_MTFLAG:
380 			val = sb.f_flags;
381 			break;
382 		case F_MTTYPE:
383 			val = sb.f_type;
384 			break;
385 		default:
386 			abort();
387 		}
388 	}
389 	switch(plan->flags) {
390 	case F_MTFLAG:
391 		return (val & plan->mt_data);
392 	case F_MTTYPE:
393 		return (val == plan->mt_data);
394 	default:
395 		abort();
396 	}
397 }
398 
399 PLAN *
400 c_fstype(arg)
401 	char *arg;
402 {
403 	register PLAN *new;
404 
405 	ftsoptions &= ~FTS_NOSTAT;
406 
407 	new = palloc(N_FSTYPE, f_fstype);
408 	switch (*arg) {
409 	case 'l':
410 		if (!strcmp(arg, "local")) {
411 			new->flags = F_MTFLAG;
412 			new->mt_data = MNT_LOCAL;
413 			return (new);
414 		}
415 		break;
416 	case 'm':
417 		if (!strcmp(arg, "mfs")) {
418 			new->flags = F_MTTYPE;
419 			new->mt_data = MOUNT_MFS;
420 			return (new);
421 		} else if (!strcmp(arg, "msdos")) {
422 			new->flags = F_MTTYPE;
423 			new->mt_data = MOUNT_MSDOS;
424 			return (new);
425 		}
426 		break;
427 	case 'n':
428 		if (!strcmp(arg, "nfs")) {
429 			new->flags = F_MTTYPE;
430 			new->mt_data = MOUNT_NFS;
431 			return (new);
432 		}
433 		break;
434 	case 'r':
435 		if (!strcmp(arg, "rdonly")) {
436 			new->flags = F_MTFLAG;
437 			new->mt_data = MNT_RDONLY;
438 			return (new);
439 		}
440 		break;
441 	case 'u':
442 		if (!strcmp(arg, "ufs")) {
443 			new->flags = F_MTTYPE;
444 			new->mt_data = MOUNT_UFS;
445 			return (new);
446 		}
447 		break;
448 	}
449 	errx(1, "%s: unknown file type", arg);
450 	/* NOTREACHED */
451 }
452 
453 /*
454  * -group gname functions --
455  *
456  *	True if the file belongs to the group gname.  If gname is numeric and
457  *	an equivalent of the getgrnam() function does not return a valid group
458  *	name, gname is taken as a group ID.
459  */
460 int
461 f_group(plan, entry)
462 	PLAN *plan;
463 	FTSENT *entry;
464 {
465 	return (entry->fts_statp->st_gid == plan->g_data);
466 }
467 
468 PLAN *
469 c_group(gname)
470 	char *gname;
471 {
472 	PLAN *new;
473 	struct group *g;
474 	gid_t gid;
475 
476 	ftsoptions &= ~FTS_NOSTAT;
477 
478 	g = getgrnam(gname);
479 	if (g == NULL) {
480 		gid = atoi(gname);
481 		if (gid == 0 && gname[0] != '0')
482 			errx(1, "-group: %s: no such group", gname);
483 	} else
484 		gid = g->gr_gid;
485 
486 	new = palloc(N_GROUP, f_group);
487 	new->g_data = gid;
488 	return (new);
489 }
490 
491 /*
492  * -inum n functions --
493  *
494  *	True if the file has inode # n.
495  */
496 int
497 f_inum(plan, entry)
498 	PLAN *plan;
499 	FTSENT *entry;
500 {
501 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
502 }
503 
504 PLAN *
505 c_inum(arg)
506 	char *arg;
507 {
508 	PLAN *new;
509 
510 	ftsoptions &= ~FTS_NOSTAT;
511 
512 	new = palloc(N_INUM, f_inum);
513 	new->i_data = find_parsenum(new, "-inum", arg, NULL);
514 	return (new);
515 }
516 
517 /*
518  * -links n functions --
519  *
520  *	True if the file has n links.
521  */
522 int
523 f_links(plan, entry)
524 	PLAN *plan;
525 	FTSENT *entry;
526 {
527 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
528 }
529 
530 PLAN *
531 c_links(arg)
532 	char *arg;
533 {
534 	PLAN *new;
535 
536 	ftsoptions &= ~FTS_NOSTAT;
537 
538 	new = palloc(N_LINKS, f_links);
539 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
540 	return (new);
541 }
542 
543 /*
544  * -ls functions --
545  *
546  *	Always true - prints the current entry to stdout in "ls" format.
547  */
548 int
549 f_ls(plan, entry)
550 	PLAN *plan;
551 	FTSENT *entry;
552 {
553 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
554 	return (1);
555 }
556 
557 PLAN *
558 c_ls()
559 {
560 	ftsoptions &= ~FTS_NOSTAT;
561 	isoutput = 1;
562 
563 	return (palloc(N_LS, f_ls));
564 }
565 
566 /*
567  * -mtime n functions --
568  *
569  *	True if the difference between the file modification time and the
570  *	current time is n 24 hour periods.
571  */
572 int
573 f_mtime(plan, entry)
574 	PLAN *plan;
575 	FTSENT *entry;
576 {
577 	extern time_t now;
578 
579 	COMPARE((now - entry->fts_statp->st_mtime + 86400 - 1) /
580 	    86400, plan->t_data);
581 }
582 
583 PLAN *
584 c_mtime(arg)
585 	char *arg;
586 {
587 	PLAN *new;
588 
589 	ftsoptions &= ~FTS_NOSTAT;
590 
591 	new = palloc(N_MTIME, f_mtime);
592 	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
593 	TIME_CORRECT(new, N_MTIME);
594 	return (new);
595 }
596 
597 /*
598  * -name functions --
599  *
600  *	True if the basename of the filename being examined
601  *	matches pattern using Pattern Matching Notation S3.14
602  */
603 int
604 f_name(plan, entry)
605 	PLAN *plan;
606 	FTSENT *entry;
607 {
608 	return (!fnmatch(plan->c_data, entry->fts_name, 0));
609 }
610 
611 PLAN *
612 c_name(pattern)
613 	char *pattern;
614 {
615 	PLAN *new;
616 
617 	new = palloc(N_NAME, f_name);
618 	new->c_data = pattern;
619 	return (new);
620 }
621 
622 /*
623  * -newer file functions --
624  *
625  *	True if the current file has been modified more recently
626  *	then the modification time of the file named by the pathname
627  *	file.
628  */
629 int
630 f_newer(plan, entry)
631 	PLAN *plan;
632 	FTSENT *entry;
633 {
634 	return (entry->fts_statp->st_mtime > plan->t_data);
635 }
636 
637 PLAN *
638 c_newer(filename)
639 	char *filename;
640 {
641 	PLAN *new;
642 	struct stat sb;
643 
644 	ftsoptions &= ~FTS_NOSTAT;
645 
646 	if (stat(filename, &sb))
647 		err(1, "%s", filename);
648 	new = palloc(N_NEWER, f_newer);
649 	new->t_data = sb.st_mtime;
650 	return (new);
651 }
652 
653 /*
654  * -nogroup functions --
655  *
656  *	True if file belongs to a user ID for which the equivalent
657  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
658  */
659 int
660 f_nogroup(plan, entry)
661 	PLAN *plan;
662 	FTSENT *entry;
663 {
664 	char *group_from_gid();
665 
666 	return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
667 }
668 
669 PLAN *
670 c_nogroup()
671 {
672 	ftsoptions &= ~FTS_NOSTAT;
673 
674 	return (palloc(N_NOGROUP, f_nogroup));
675 }
676 
677 /*
678  * -nouser functions --
679  *
680  *	True if file belongs to a user ID for which the equivalent
681  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
682  */
683 int
684 f_nouser(plan, entry)
685 	PLAN *plan;
686 	FTSENT *entry;
687 {
688 	char *user_from_uid();
689 
690 	return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
691 }
692 
693 PLAN *
694 c_nouser()
695 {
696 	ftsoptions &= ~FTS_NOSTAT;
697 
698 	return (palloc(N_NOUSER, f_nouser));
699 }
700 
701 /*
702  * -path functions --
703  *
704  *	True if the path of the filename being examined
705  *	matches pattern using Pattern Matching Notation S3.14
706  */
707 int
708 f_path(plan, entry)
709 	PLAN *plan;
710 	FTSENT *entry;
711 {
712 	return (!fnmatch(plan->c_data, entry->fts_path, 0));
713 }
714 
715 PLAN *
716 c_path(pattern)
717 	char *pattern;
718 {
719 	PLAN *new;
720 
721 	new = palloc(N_NAME, f_path);
722 	new->c_data = pattern;
723 	return (new);
724 }
725 
726 /*
727  * -perm functions --
728  *
729  *	The mode argument is used to represent file mode bits.  If it starts
730  *	with a leading digit, it's treated as an octal mode, otherwise as a
731  *	symbolic mode.
732  */
733 int
734 f_perm(plan, entry)
735 	PLAN *plan;
736 	FTSENT *entry;
737 {
738 	mode_t mode;
739 
740 	mode = entry->fts_statp->st_mode &
741 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
742 	if (plan->flags == F_ATLEAST)
743 		return ((plan->m_data | mode) == mode);
744 	else
745 		return (mode == plan->m_data);
746 	/* NOTREACHED */
747 }
748 
749 PLAN *
750 c_perm(perm)
751 	char *perm;
752 {
753 	PLAN *new;
754 	mode_t *set;
755 
756 	ftsoptions &= ~FTS_NOSTAT;
757 
758 	new = palloc(N_PERM, f_perm);
759 
760 	if (*perm == '-') {
761 		new->flags = F_ATLEAST;
762 		++perm;
763 	}
764 
765 	if ((set = setmode(perm)) == NULL)
766 		err(1, "-perm: %s: illegal mode string", perm);
767 
768 	new->m_data = getmode(set, 0);
769 	return (new);
770 }
771 
772 /*
773  * -print functions --
774  *
775  *	Always true, causes the current pathame to be written to
776  *	standard output.
777  */
778 int
779 f_print(plan, entry)
780 	PLAN *plan;
781 	FTSENT *entry;
782 {
783 	(void)puts(entry->fts_path);
784 	return (1);
785 }
786 
787 PLAN *
788 c_print()
789 {
790 	isoutput = 1;
791 
792 	return (palloc(N_PRINT, f_print));
793 }
794 
795 /*
796  * -print0 functions --
797  *
798  *	Always true, causes the current pathame to be written to
799  *	standard output followed by a NUL character
800  */
801 int
802 f_print0(plan, entry)
803 	PLAN *plan;
804 	FTSENT *entry;
805 {
806 	fputs(entry->fts_path, stdout);
807 	fputc('\0', stdout);
808 	return (1);
809 }
810 
811 PLAN *
812 c_print0()
813 {
814 	isoutput = 1;
815 
816 	return (palloc(N_PRINT0, f_print0));
817 }
818 
819 /*
820  * -prune functions --
821  *
822  *	Prune a portion of the hierarchy.
823  */
824 int
825 f_prune(plan, entry)
826 	PLAN *plan;
827 	FTSENT *entry;
828 {
829 	extern FTS *tree;
830 
831 	if (fts_set(tree, entry, FTS_SKIP))
832 		err(1, "%s", entry->fts_path);
833 	return (1);
834 }
835 
836 PLAN *
837 c_prune()
838 {
839 	return (palloc(N_PRUNE, f_prune));
840 }
841 
842 /*
843  * -size n[c] functions --
844  *
845  *	True if the file size in bytes, divided by an implementation defined
846  *	value and rounded up to the next integer, is n.  If n is followed by
847  *	a c, the size is in bytes.
848  */
849 #define	FIND_SIZE	512
850 static int divsize = 1;
851 
852 int
853 f_size(plan, entry)
854 	PLAN *plan;
855 	FTSENT *entry;
856 {
857 	off_t size;
858 
859 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
860 	    FIND_SIZE : entry->fts_statp->st_size;
861 	COMPARE(size, plan->o_data);
862 }
863 
864 PLAN *
865 c_size(arg)
866 	char *arg;
867 {
868 	PLAN *new;
869 	char endch;
870 
871 	ftsoptions &= ~FTS_NOSTAT;
872 
873 	new = palloc(N_SIZE, f_size);
874 	endch = 'c';
875 	new->o_data = find_parsenum(new, "-size", arg, &endch);
876 	if (endch == 'c')
877 		divsize = 0;
878 	return (new);
879 }
880 
881 /*
882  * -type c functions --
883  *
884  *	True if the type of the file is c, where c is b, c, d, p, or f for
885  *	block special file, character special file, directory, FIFO, or
886  *	regular file, respectively.
887  */
888 int
889 f_type(plan, entry)
890 	PLAN *plan;
891 	FTSENT *entry;
892 {
893 	return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
894 }
895 
896 PLAN *
897 c_type(typestring)
898 	char *typestring;
899 {
900 	PLAN *new;
901 	mode_t  mask;
902 
903 	ftsoptions &= ~FTS_NOSTAT;
904 
905 	switch (typestring[0]) {
906 	case 'b':
907 		mask = S_IFBLK;
908 		break;
909 	case 'c':
910 		mask = S_IFCHR;
911 		break;
912 	case 'd':
913 		mask = S_IFDIR;
914 		break;
915 	case 'f':
916 		mask = S_IFREG;
917 		break;
918 	case 'l':
919 		mask = S_IFLNK;
920 		break;
921 	case 'p':
922 		mask = S_IFIFO;
923 		break;
924 	case 's':
925 		mask = S_IFSOCK;
926 		break;
927 	default:
928 		errx(1, "-type: %s: unknown type", typestring);
929 	}
930 
931 	new = palloc(N_TYPE, f_type);
932 	new->m_data = mask;
933 	return (new);
934 }
935 
936 /*
937  * -delete functions --
938  *
939  *	True always.  Makes it's best shot and continues on regardless.
940  */
941 int
942 f_delete(plan, entry)
943 	PLAN *plan;
944 	FTSENT *entry;
945 {
946 	/* ignore these from fts */
947 	if (strcmp(entry->fts_accpath, ".") == 0 ||
948 	    strcmp(entry->fts_accpath, "..") == 0)
949 		return (1);
950 
951 	/* sanity check */
952 	if (isdepth == 0 ||			/* depth off */
953 	    (ftsoptions & FTS_NOSTAT) ||	/* not stat()ing */
954 	    !(ftsoptions & FTS_PHYSICAL) ||	/* physical off */
955 	    (ftsoptions & FTS_LOGICAL))		/* or finally, logical on */
956 		errx(1, "-delete: insecure options got turned on");
957 
958 	/* Potentially unsafe - do not accept relative paths whatsoever */
959 	if (strchr(entry->fts_accpath, '/') != NULL)
960 		errx(1, "-delete: %s: relative path potentially not safe",
961 			entry->fts_accpath);
962 
963 	/* Turn off user immutable bits if running as root */
964 	if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
965 	    !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
966 	    geteuid() == 0)
967 		chflags(entry->fts_accpath,
968 		       entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
969 
970 	/* rmdir directories, unlink everything else */
971 	if (S_ISDIR(entry->fts_statp->st_mode)) {
972 		if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
973 			warn("-delete: rmdir(%s)", entry->fts_path);
974 	} else {
975 		if (unlink(entry->fts_accpath) < 0)
976 			warn("-delete: unlink(%s)", entry->fts_path);
977 	}
978 
979 	/* "succeed" */
980 	return (1);
981 }
982 
983 PLAN *
984 c_delete()
985 {
986 
987 	ftsoptions &= ~FTS_NOSTAT;	/* no optimise */
988 	ftsoptions |= FTS_PHYSICAL;	/* disable -follow */
989 	ftsoptions &= ~FTS_LOGICAL;	/* disable -follow */
990 	isoutput = 1;			/* possible output */
991 	isdepth = 1;			/* -depth implied */
992 
993 	return (palloc(N_DELETE, f_delete));
994 }
995 
996 /*
997  * -user uname functions --
998  *
999  *	True if the file belongs to the user uname.  If uname is numeric and
1000  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1001  *	return a valid user name, uname is taken as a user ID.
1002  */
1003 int
1004 f_user(plan, entry)
1005 	PLAN *plan;
1006 	FTSENT *entry;
1007 {
1008 	return (entry->fts_statp->st_uid == plan->u_data);
1009 }
1010 
1011 PLAN *
1012 c_user(username)
1013 	char *username;
1014 {
1015 	PLAN *new;
1016 	struct passwd *p;
1017 	uid_t uid;
1018 
1019 	ftsoptions &= ~FTS_NOSTAT;
1020 
1021 	p = getpwnam(username);
1022 	if (p == NULL) {
1023 		uid = atoi(username);
1024 		if (uid == 0 && username[0] != '0')
1025 			errx(1, "-user: %s: no such user", username);
1026 	} else
1027 		uid = p->pw_uid;
1028 
1029 	new = palloc(N_USER, f_user);
1030 	new->u_data = uid;
1031 	return (new);
1032 }
1033 
1034 /*
1035  * -xdev functions --
1036  *
1037  *	Always true, causes find not to decend past directories that have a
1038  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1039  */
1040 PLAN *
1041 c_xdev()
1042 {
1043 	ftsoptions |= FTS_XDEV;
1044 
1045 	return (palloc(N_XDEV, f_always_true));
1046 }
1047 
1048 /*
1049  * ( expression ) functions --
1050  *
1051  *	True if expression is true.
1052  */
1053 int
1054 f_expr(plan, entry)
1055 	PLAN *plan;
1056 	FTSENT *entry;
1057 {
1058 	register PLAN *p;
1059 	register int state;
1060 
1061 	for (p = plan->p_data[0];
1062 	    p && (state = (p->eval)(p, entry)); p = p->next);
1063 	return (state);
1064 }
1065 
1066 /*
1067  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
1068  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1069  * to a N_EXPR node containing the expression and the ')' node is discarded.
1070  */
1071 PLAN *
1072 c_openparen()
1073 {
1074 	return (palloc(N_OPENPAREN, (int (*)())-1));
1075 }
1076 
1077 PLAN *
1078 c_closeparen()
1079 {
1080 	return (palloc(N_CLOSEPAREN, (int (*)())-1));
1081 }
1082 
1083 /*
1084  * ! expression functions --
1085  *
1086  *	Negation of a primary; the unary NOT operator.
1087  */
1088 int
1089 f_not(plan, entry)
1090 	PLAN *plan;
1091 	FTSENT *entry;
1092 {
1093 	register PLAN *p;
1094 	register int state;
1095 
1096 	for (p = plan->p_data[0];
1097 	    p && (state = (p->eval)(p, entry)); p = p->next);
1098 	return (!state);
1099 }
1100 
1101 PLAN *
1102 c_not()
1103 {
1104 	return (palloc(N_NOT, f_not));
1105 }
1106 
1107 /*
1108  * expression -o expression functions --
1109  *
1110  *	Alternation of primaries; the OR operator.  The second expression is
1111  * not evaluated if the first expression is true.
1112  */
1113 int
1114 f_or(plan, entry)
1115 	PLAN *plan;
1116 	FTSENT *entry;
1117 {
1118 	register PLAN *p;
1119 	register int state;
1120 
1121 	for (p = plan->p_data[0];
1122 	    p && (state = (p->eval)(p, entry)); p = p->next);
1123 
1124 	if (state)
1125 		return (1);
1126 
1127 	for (p = plan->p_data[1];
1128 	    p && (state = (p->eval)(p, entry)); p = p->next);
1129 	return (state);
1130 }
1131 
1132 PLAN *
1133 c_or()
1134 {
1135 	return (palloc(N_OR, f_or));
1136 }
1137 
1138 static PLAN *
1139 palloc(t, f)
1140 	enum ntype t;
1141 	int (*f) __P((PLAN *, FTSENT *));
1142 {
1143 	PLAN *new;
1144 
1145 	if ((new = malloc(sizeof(PLAN))) == NULL)
1146 		err(1, NULL);
1147 	new->type = t;
1148 	new->eval = f;
1149 	new->flags = 0;
1150 	new->next = NULL;
1151 	return (new);
1152 }
1153