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