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