xref: /csrg-svn/usr.bin/find/function.c (revision 54558)
140947Sbostic /*-
240947Sbostic  * Copyright (c) 1990 The Regents of the University of California.
340947Sbostic  * All rights reserved.
440947Sbostic  *
540947Sbostic  * This code is derived from software contributed to Berkeley by
640947Sbostic  * Cimarron D. Taylor of the University of California, Berkeley.
740947Sbostic  *
840947Sbostic  * %sccs.include.redist.c%
940947Sbostic  */
1040947Sbostic 
1140947Sbostic #ifndef lint
12*54558Sbostic static char sccsid[] = "@(#)function.c	5.22 (Berkeley) 06/29/92";
1340947Sbostic #endif /* not lint */
1440947Sbostic 
1545868Sbostic #include <sys/param.h>
1651833Sbostic #include <sys/ucred.h>
1740947Sbostic #include <sys/stat.h>
1840947Sbostic #include <sys/wait.h>
1940947Sbostic #include <sys/mount.h>
20*54558Sbostic 
2147192Sbostic #include <errno.h>
2240947Sbostic #include <grp.h>
2340947Sbostic #include <pwd.h>
2440947Sbostic #include <fts.h>
25*54558Sbostic #include <fnmatch.h>
2640947Sbostic #include <unistd.h>
2740947Sbostic #include <tzfile.h>
2840947Sbostic #include <stdio.h>
2947192Sbostic #include <stdlib.h>
3042047Sbostic #include <string.h>
3140947Sbostic #include "find.h"
3240947Sbostic 
3340947Sbostic #define	COMPARE(a, b) { \
3440947Sbostic 	switch(plan->flags) { \
3550437Sbostic 	case F_EQUAL: \
3640947Sbostic 		return(a == b); \
3750437Sbostic 	case F_LESSTHAN: \
3840947Sbostic 		return(a < b); \
3950437Sbostic 	case F_GREATER: \
4040947Sbostic 		return(a > b); \
4140947Sbostic 	} \
4242255Sbostic 	return(0); \
4340947Sbostic }
4440947Sbostic 
4549868Sbostic static PLAN *palloc __P((enum ntype, int (*)()));
4640947Sbostic 
4740947Sbostic /*
4840947Sbostic  * find_parsenum --
4940947Sbostic  *	Parse a string of the form [+-]# and return the value.
5040947Sbostic  */
5140947Sbostic long
5240947Sbostic find_parsenum(plan, option, str, endch)
5340947Sbostic 	PLAN *plan;
5440947Sbostic 	char *option, *str, *endch;
5540947Sbostic {
5640947Sbostic 	long value;
5740947Sbostic 	char *endchar;		/* pointer to character ending conversion */
5840947Sbostic 
5940947Sbostic 	/* determine comparison from leading + or - */
6040947Sbostic 	switch(*str) {
6140947Sbostic 	case '+':
6240947Sbostic 		++str;
6350437Sbostic 		plan->flags = F_GREATER;
6440947Sbostic 		break;
6540947Sbostic 	case '-':
6640947Sbostic 		++str;
6750437Sbostic 		plan->flags = F_LESSTHAN;
6840947Sbostic 		break;
6940947Sbostic 	default:
7050437Sbostic 		plan->flags = F_EQUAL;
7140947Sbostic 		break;
7240947Sbostic 	}
7340947Sbostic 
7440947Sbostic 	/*
7540947Sbostic 	 * convert the string with strtol().  Note, if strtol() returns zero
7640947Sbostic 	 * and endchar points to the beginning of the string we know we have
7740947Sbostic 	 * a syntax error.
7840947Sbostic 	 */
7940947Sbostic 	value = strtol(str, &endchar, 10);
8040947Sbostic 	if (!value && endchar == str ||
8140947Sbostic 	    endchar[0] && (!endch || endchar[0] != *endch))
8249868Sbostic 		err("%s: %s", option, "illegal numeric value");
8340947Sbostic 	if (endch)
8440947Sbostic 		*endch = endchar[0];
8540947Sbostic 	return(value);
8640947Sbostic }
8740947Sbostic 
8840947Sbostic /*
8940947Sbostic  * -atime n functions --
9040947Sbostic  *
9140947Sbostic  *	True if the difference between the file access time and the
9240947Sbostic  *	current time is n 24 hour periods.
9340947Sbostic  *
9440947Sbostic  */
9540947Sbostic f_atime(plan, entry)
9640947Sbostic 	PLAN *plan;
9740947Sbostic 	FTSENT *entry;
9840947Sbostic {
9940947Sbostic 	extern time_t now;
10040947Sbostic 
10152239Sbostic 	COMPARE((now - entry->fts_statp->st_atime +
10242255Sbostic 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
10340947Sbostic }
10440947Sbostic 
10540947Sbostic PLAN *
10640947Sbostic c_atime(arg)
10740947Sbostic 	char *arg;
10840947Sbostic {
10940947Sbostic 	PLAN *new;
11040947Sbostic 
11140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
11240947Sbostic 
11349868Sbostic 	new = palloc(N_ATIME, f_atime);
11449868Sbostic 	new->t_data = find_parsenum(new, "-atime", arg, NULL);
11540947Sbostic 	return(new);
11640947Sbostic }
11740947Sbostic /*
11840947Sbostic  * -ctime n functions --
11940947Sbostic  *
12040947Sbostic  *	True if the difference between the last change of file
12140947Sbostic  *	status information and the current time is n 24 hour periods.
12240947Sbostic  */
12340947Sbostic f_ctime(plan, entry)
12440947Sbostic 	PLAN *plan;
12540947Sbostic 	FTSENT *entry;
12640947Sbostic {
12740947Sbostic 	extern time_t now;
12840947Sbostic 
12952239Sbostic 	COMPARE((now - entry->fts_statp->st_ctime +
13042255Sbostic 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
13140947Sbostic }
13240947Sbostic 
13340947Sbostic PLAN *
13440947Sbostic c_ctime(arg)
13540947Sbostic 	char *arg;
13640947Sbostic {
13740947Sbostic 	PLAN *new;
13840947Sbostic 
13940947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
14040947Sbostic 
14149868Sbostic 	new = palloc(N_CTIME, f_ctime);
14240947Sbostic 	new->t_data = find_parsenum(new, "-ctime", arg, (char *)NULL);
14340947Sbostic 	return(new);
14440947Sbostic }
14540947Sbostic 
14640947Sbostic /*
14740947Sbostic  * -depth functions --
14840947Sbostic  *
14940947Sbostic  *	Always true, causes descent of the directory hierarchy to be done
15040947Sbostic  *	so that all entries in a directory are acted on before the directory
15140947Sbostic  *	itself.
15240947Sbostic  */
15340947Sbostic /* ARGSUSED */
15440947Sbostic f_always_true(plan, entry)
15540947Sbostic 	PLAN *plan;
15640947Sbostic 	FTSENT *entry;
15740947Sbostic {
15842255Sbostic 	return(1);
15940947Sbostic }
16040947Sbostic 
16140947Sbostic PLAN *
16240947Sbostic c_depth()
16340947Sbostic {
16445615Sbostic 	isdepth = 1;
16540947Sbostic 
16649868Sbostic 	return(palloc(N_DEPTH, f_always_true));
16740947Sbostic }
16840947Sbostic 
16940947Sbostic /*
17040947Sbostic  * [-exec | -ok] utility [arg ... ] ; functions --
17140947Sbostic  *
17240947Sbostic  *	True if the executed utility returns a zero value as exit status.
17340947Sbostic  *	The end of the primary expression is delimited by a semicolon.  If
17440947Sbostic  *	"{}" occurs anywhere, it gets replaced by the current pathname.
17540947Sbostic  *	The current directory for the execution of utility is the same as
17640947Sbostic  *	the current directory when the find utility was started.
17740947Sbostic  *
17840947Sbostic  *	The primary -ok is different in that it requests affirmation of the
17940947Sbostic  *	user before executing the utility.
18040947Sbostic  */
18140947Sbostic f_exec(plan, entry)
18240947Sbostic 	register PLAN *plan;
18340947Sbostic 	FTSENT *entry;
18440947Sbostic {
18549869Sbostic 	extern int dotfd;
18640947Sbostic 	register int cnt;
18747192Sbostic 	pid_t pid;
18847192Sbostic 	int status;
18940947Sbostic 
19040947Sbostic 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
19140947Sbostic 		if (plan->e_len[cnt])
19245616Sbostic 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
19349869Sbostic 			    entry->fts_path, plan->e_len[cnt]);
19440947Sbostic 
19550437Sbostic 	if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
19642255Sbostic 		return(0);
19740947Sbostic 
19840947Sbostic 	switch(pid = vfork()) {
19940947Sbostic 	case -1:
20049868Sbostic 		err("fork: %s", strerror(errno));
20140947Sbostic 		/* NOTREACHED */
20240947Sbostic 	case 0:
20349869Sbostic 		if (fchdir(dotfd)) {
20449869Sbostic 			(void)fprintf(stderr,
20549869Sbostic 			    "find: chdir: %s\n", strerror(errno));
20649869Sbostic 			_exit(1);
20749869Sbostic 		}
20840947Sbostic 		execvp(plan->e_argv[0], plan->e_argv);
20949869Sbostic 		(void)fprintf(stderr,
21049869Sbostic 		    "find: %s: %s\n", plan->e_argv[0], strerror(errno));
21149869Sbostic 		_exit(1);
21240947Sbostic 	}
21347192Sbostic 	pid = waitpid(pid, &status, 0);
21447192Sbostic 	return(pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
21540947Sbostic }
21640947Sbostic 
21740947Sbostic /*
21840947Sbostic  * c_exec --
21940947Sbostic  *	build three parallel arrays, one with pointers to the strings passed
22040947Sbostic  *	on the command line, one with (possibly duplicated) pointers to the
22140947Sbostic  *	argv array, and one with integer values that are lengths of the
22240947Sbostic  *	strings, but also flags meaning that the string has to be massaged.
22340947Sbostic  */
22440947Sbostic PLAN *
22540947Sbostic c_exec(argvp, isok)
22640947Sbostic 	char ***argvp;
22740947Sbostic 	int isok;
22840947Sbostic {
22940947Sbostic 	PLAN *new;			/* node returned */
23040947Sbostic 	register int cnt;
23140947Sbostic 	register char **argv, **ap, *p;
23240947Sbostic 
23347192Sbostic 	isoutput = 1;
23440947Sbostic 
23549868Sbostic 	new = palloc(N_EXEC, f_exec);
23650437Sbostic 	if (isok)
23750437Sbostic 		new->flags = F_NEEDOK;
23840947Sbostic 
23940947Sbostic 	for (ap = argv = *argvp;; ++ap) {
24040947Sbostic 		if (!*ap)
24149868Sbostic 			err("%s: %s",
24249868Sbostic 			    isok ? "-ok" : "-exec", "no terminating \";\"");
24340947Sbostic 		if (**ap == ';')
24440947Sbostic 			break;
24540947Sbostic 	}
24640947Sbostic 
24740947Sbostic 	cnt = ap - *argvp + 1;
24840947Sbostic 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
24940947Sbostic 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
25045868Sbostic 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
25140947Sbostic 
25240947Sbostic 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
25340947Sbostic 		new->e_orig[cnt] = *argv;
25440947Sbostic 		for (p = *argv; *p; ++p)
25540947Sbostic 			if (p[0] == '{' && p[1] == '}') {
25645868Sbostic 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
25745868Sbostic 				new->e_len[cnt] = MAXPATHLEN;
25840947Sbostic 				break;
25940947Sbostic 			}
26040947Sbostic 		if (!*p) {
26140947Sbostic 			new->e_argv[cnt] = *argv;
26240947Sbostic 			new->e_len[cnt] = 0;
26340947Sbostic 		}
26440947Sbostic 	}
26540947Sbostic 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
26640947Sbostic 
26740947Sbostic 	*argvp = argv + 1;
26840947Sbostic 	return(new);
26940947Sbostic }
27040947Sbostic 
27140947Sbostic /*
27240947Sbostic  * -follow functions --
27340947Sbostic  *
27440947Sbostic  *	Always true, causes symbolic links to be followed on a global
27540947Sbostic  *	basis.
27640947Sbostic  */
27740947Sbostic PLAN *
27840947Sbostic c_follow()
27940947Sbostic {
28040947Sbostic 	ftsoptions &= ~FTS_PHYSICAL;
28140947Sbostic 	ftsoptions |= FTS_LOGICAL;
28240947Sbostic 
28349868Sbostic 	return(palloc(N_FOLLOW, f_always_true));
28440947Sbostic }
28540947Sbostic 
28640947Sbostic /*
28740947Sbostic  * -fstype functions --
28840947Sbostic  *
28940947Sbostic  *	True if the file is of a certain type.
29040947Sbostic  */
29140947Sbostic f_fstype(plan, entry)
29240947Sbostic 	PLAN *plan;
29340947Sbostic 	FTSENT *entry;
29440947Sbostic {
29542275Sbostic 	static dev_t curdev;	/* need a guaranteed illegal dev value */
29642275Sbostic 	static int first = 1;
29740947Sbostic 	struct statfs sb;
29842255Sbostic 	static short val;
29945626Sbostic 	char *p, save[2];
30040947Sbostic 
30150437Sbostic 	/* Only check when we cross mount point. */
30252239Sbostic 	if (first || curdev != entry->fts_statp->st_dev) {
30352239Sbostic 		curdev = entry->fts_statp->st_dev;
30445626Sbostic 
30545626Sbostic 		/*
30645626Sbostic 		 * Statfs follows symlinks; find wants the link's file system,
30745626Sbostic 		 * not where it points.
30845626Sbostic 		 */
30945626Sbostic 		if (entry->fts_info == FTS_SL ||
31045626Sbostic 		    entry->fts_info == FTS_SLNONE) {
31145626Sbostic 			if (p = rindex(entry->fts_accpath, '/'))
31245626Sbostic 				++p;
31345626Sbostic 			else
31445626Sbostic 				p = entry->fts_accpath;
31545626Sbostic 			save[0] = p[0];
31645626Sbostic 			p[0] = '.';
31745626Sbostic 			save[1] = p[1];
31845626Sbostic 			p[1] = '\0';
31945626Sbostic 
32045626Sbostic 		} else
32145626Sbostic 			p = NULL;
32245626Sbostic 
32349868Sbostic 		if (statfs(entry->fts_accpath, &sb))
32449868Sbostic 			err("%s: %s", entry->fts_accpath, strerror(errno));
32545626Sbostic 
32645626Sbostic 		if (p) {
32745626Sbostic 			p[0] = save[0];
32845626Sbostic 			p[1] = save[1];
32945626Sbostic 		}
33045626Sbostic 
33142275Sbostic 		first = 0;
33250437Sbostic 		switch(plan->flags) {
33350437Sbostic 		case F_MTFLAG:
33450437Sbostic 			val = sb.f_flags;
33550437Sbostic 			break;
33650437Sbostic 		case F_MTTYPE:
33750437Sbostic 			val = sb.f_type;
33850437Sbostic 			break;
33950437Sbostic 		}
34040947Sbostic 	}
34150437Sbostic 	switch(plan->flags) {
34250437Sbostic 	case F_MTFLAG:
34350437Sbostic 		return(val & plan->mt_data);
34450437Sbostic 	case F_MTTYPE:
34550437Sbostic 		return(val == plan->mt_data);
34650437Sbostic 	}
34740947Sbostic }
34840947Sbostic 
34940947Sbostic PLAN *
35040947Sbostic c_fstype(arg)
35140947Sbostic 	char *arg;
35240947Sbostic {
35340947Sbostic 	register PLAN *new;
35440947Sbostic 
35540947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
35640947Sbostic 
35749868Sbostic 	new = palloc(N_FSTYPE, f_fstype);
35840947Sbostic 	switch(*arg) {
35942255Sbostic 	case 'l':
36042255Sbostic 		if (!strcmp(arg, "local")) {
36150437Sbostic 			new->flags = F_MTFLAG;
36250437Sbostic 			new->mt_data = MNT_LOCAL;
36342255Sbostic 			return(new);
36442255Sbostic 		}
36542255Sbostic 		break;
36640947Sbostic 	case 'm':
36740947Sbostic 		if (!strcmp(arg, "mfs")) {
36850437Sbostic 			new->flags = F_MTTYPE;
36950437Sbostic 			new->mt_data = MOUNT_MFS;
37040947Sbostic 			return(new);
37140947Sbostic 		}
37240947Sbostic 		break;
37340947Sbostic 	case 'n':
37440947Sbostic 		if (!strcmp(arg, "nfs")) {
37550437Sbostic 			new->flags = F_MTTYPE;
37650437Sbostic 			new->mt_data = MOUNT_NFS;
37740947Sbostic 			return(new);
37840947Sbostic 		}
37940947Sbostic 		break;
38040947Sbostic 	case 'p':
38140947Sbostic 		if (!strcmp(arg, "pc")) {
38250437Sbostic 			new->flags = F_MTTYPE;
38350437Sbostic 			new->mt_data = MOUNT_PC;
38440947Sbostic 			return(new);
38540947Sbostic 		}
38640947Sbostic 		break;
38750437Sbostic 	case 'r':
38850437Sbostic 		if (!strcmp(arg, "rdonly")) {
38950437Sbostic 			new->flags = F_MTFLAG;
39050437Sbostic 			new->mt_data = MNT_RDONLY;
39150437Sbostic 			return(new);
39250437Sbostic 		}
39350437Sbostic 		break;
39440947Sbostic 	case 'u':
39540947Sbostic 		if (!strcmp(arg, "ufs")) {
39650437Sbostic 			new->flags = F_MTTYPE;
39750437Sbostic 			new->mt_data = MOUNT_UFS;
39840947Sbostic 			return(new);
39940947Sbostic 		}
40040947Sbostic 		break;
40140947Sbostic 	}
40249868Sbostic 	err("unknown file type %s", arg);
40340947Sbostic 	/* NOTREACHED */
40440947Sbostic }
40540947Sbostic 
40640947Sbostic /*
40740947Sbostic  * -group gname functions --
40840947Sbostic  *
40940947Sbostic  *	True if the file belongs to the group gname.  If gname is numeric and
41040947Sbostic  *	an equivalent of the getgrnam() function does not return a valid group
41140947Sbostic  *	name, gname is taken as a group ID.
41240947Sbostic  */
41340947Sbostic f_group(plan, entry)
41440947Sbostic 	PLAN *plan;
41540947Sbostic 	FTSENT *entry;
41640947Sbostic {
41752239Sbostic 	return(entry->fts_statp->st_gid == plan->g_data);
41840947Sbostic }
41940947Sbostic 
42040947Sbostic PLAN *
42140947Sbostic c_group(gname)
42240947Sbostic 	char *gname;
42340947Sbostic {
42440947Sbostic 	PLAN *new;
42540947Sbostic 	struct group *g;
42640947Sbostic 	gid_t gid;
42740947Sbostic 
42840947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
42940947Sbostic 
43040947Sbostic 	g = getgrnam(gname);
43140947Sbostic 	if (g == NULL) {
43240947Sbostic 		gid = atoi(gname);
43340947Sbostic 		if (gid == 0 && gname[0] != '0')
43449868Sbostic 			err("%s: %s", "-group", "no such group");
43540947Sbostic 	} else
43640947Sbostic 		gid = g->gr_gid;
43740947Sbostic 
43849868Sbostic 	new = palloc(N_GROUP, f_group);
43940947Sbostic 	new->g_data = gid;
44040947Sbostic 	return(new);
44140947Sbostic }
44240947Sbostic 
44340947Sbostic /*
44440947Sbostic  * -inum n functions --
44540947Sbostic  *
44640947Sbostic  *	True if the file has inode # n.
44740947Sbostic  */
44840947Sbostic f_inum(plan, entry)
44940947Sbostic 	PLAN *plan;
45040947Sbostic 	FTSENT *entry;
45140947Sbostic {
45252239Sbostic 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
45340947Sbostic }
45440947Sbostic 
45540947Sbostic PLAN *
45640947Sbostic c_inum(arg)
45740947Sbostic 	char *arg;
45840947Sbostic {
45940947Sbostic 	PLAN *new;
46040947Sbostic 
46140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
46240947Sbostic 
46349868Sbostic 	new = palloc(N_INUM, f_inum);
46440947Sbostic 	new->i_data = find_parsenum(new, "-inum", arg, (char *)NULL);
46540947Sbostic 	return(new);
46640947Sbostic }
46740947Sbostic 
46840947Sbostic /*
46940947Sbostic  * -links n functions --
47040947Sbostic  *
47140947Sbostic  *	True if the file has n links.
47240947Sbostic  */
47340947Sbostic f_links(plan, entry)
47440947Sbostic 	PLAN *plan;
47540947Sbostic 	FTSENT *entry;
47640947Sbostic {
47752239Sbostic 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
47840947Sbostic }
47940947Sbostic 
48040947Sbostic PLAN *
48140947Sbostic c_links(arg)
48240947Sbostic 	char *arg;
48340947Sbostic {
48440947Sbostic 	PLAN *new;
48540947Sbostic 
48640947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
48740947Sbostic 
48849868Sbostic 	new = palloc(N_LINKS, f_links);
48945616Sbostic 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, (char *)NULL);
49040947Sbostic 	return(new);
49140947Sbostic }
49240947Sbostic 
49340947Sbostic /*
49440947Sbostic  * -ls functions --
49540947Sbostic  *
49640947Sbostic  *	Always true - prints the current entry to stdout in "ls" format.
49740947Sbostic  */
49840947Sbostic /* ARGSUSED */
49940947Sbostic f_ls(plan, entry)
50040947Sbostic 	PLAN *plan;
50140947Sbostic 	FTSENT *entry;
50240947Sbostic {
50352239Sbostic 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
50442255Sbostic 	return(1);
50540947Sbostic }
50640947Sbostic 
50740947Sbostic PLAN *
50840947Sbostic c_ls()
50940947Sbostic {
51040947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
51145615Sbostic 	isoutput = 1;
51240947Sbostic 
51349868Sbostic 	return(palloc(N_LS, f_ls));
51440947Sbostic }
51540947Sbostic 
51640947Sbostic /*
51750437Sbostic  * -mtime n functions --
51850437Sbostic  *
51950437Sbostic  *	True if the difference between the file modification time and the
52050437Sbostic  *	current time is n 24 hour periods.
52150437Sbostic  */
52250437Sbostic f_mtime(plan, entry)
52350437Sbostic 	PLAN *plan;
52450437Sbostic 	FTSENT *entry;
52550437Sbostic {
52650437Sbostic 	extern time_t now;
52750437Sbostic 
52852239Sbostic 	COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
52950437Sbostic 	    SECSPERDAY, plan->t_data);
53050437Sbostic }
53150437Sbostic 
53250437Sbostic PLAN *
53350437Sbostic c_mtime(arg)
53450437Sbostic 	char *arg;
53550437Sbostic {
53650437Sbostic 	PLAN *new;
53750437Sbostic 
53850437Sbostic 	ftsoptions &= ~FTS_NOSTAT;
53950437Sbostic 
54050437Sbostic 	new = palloc(N_MTIME, f_mtime);
54150437Sbostic 	new->t_data = find_parsenum(new, "-mtime", arg, (char *)NULL);
54250437Sbostic 	return(new);
54350437Sbostic }
54450437Sbostic 
54550437Sbostic /*
54640947Sbostic  * -name functions --
54740947Sbostic  *
54840947Sbostic  *	True if the basename of the filename being examined
54940947Sbostic  *	matches pattern using Pattern Matching Notation S3.14
55040947Sbostic  */
55140947Sbostic f_name(plan, entry)
55240947Sbostic 	PLAN *plan;
55340947Sbostic 	FTSENT *entry;
55440947Sbostic {
55554555Sbostic 	return(!fnmatch(plan->c_data, entry->fts_name, 0));
55640947Sbostic }
55740947Sbostic 
55840947Sbostic PLAN *
55940947Sbostic c_name(pattern)
56040947Sbostic 	char *pattern;
56140947Sbostic {
56240947Sbostic 	PLAN *new;
56340947Sbostic 
56449868Sbostic 	new = palloc(N_NAME, f_name);
56540947Sbostic 	new->c_data = pattern;
56640947Sbostic 	return(new);
56740947Sbostic }
56840947Sbostic 
56940947Sbostic /*
57040947Sbostic  * -newer file functions --
57140947Sbostic  *
57240947Sbostic  *	True if the current file has been modified more recently
57340947Sbostic  *	then the modification time of the file named by the pathname
57440947Sbostic  *	file.
57540947Sbostic  */
57640947Sbostic f_newer(plan, entry)
57740947Sbostic 	PLAN *plan;
57840947Sbostic 	FTSENT *entry;
57940947Sbostic {
58052239Sbostic 	return(entry->fts_statp->st_mtime > plan->t_data);
58140947Sbostic }
58240947Sbostic 
58340947Sbostic PLAN *
58440947Sbostic c_newer(filename)
58540947Sbostic 	char *filename;
58640947Sbostic {
58740947Sbostic 	PLAN *new;
58840947Sbostic 	struct stat sb;
58940947Sbostic 
59040947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
59140947Sbostic 
59249868Sbostic 	if (stat(filename, &sb))
59349868Sbostic 		err("%s: %s", filename, strerror(errno));
59449868Sbostic 	new = palloc(N_NEWER, f_newer);
59540947Sbostic 	new->t_data = sb.st_mtime;
59640947Sbostic 	return(new);
59740947Sbostic }
59840947Sbostic 
59940947Sbostic /*
60040947Sbostic  * -nogroup functions --
60140947Sbostic  *
60240947Sbostic  *	True if file belongs to a user ID for which the equivalent
60340947Sbostic  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
60440947Sbostic  */
60540947Sbostic /* ARGSUSED */
60640947Sbostic f_nogroup(plan, entry)
60740947Sbostic 	PLAN *plan;
60840947Sbostic 	FTSENT *entry;
60940947Sbostic {
61045615Sbostic 	char *group_from_gid();
61145615Sbostic 
61252239Sbostic 	return(group_from_gid(entry->fts_statp->st_gid, 1) ? 1 : 0);
61340947Sbostic }
61440947Sbostic 
61540947Sbostic PLAN *
61640947Sbostic c_nogroup()
61740947Sbostic {
61840947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
61940947Sbostic 
62049868Sbostic 	return(palloc(N_NOGROUP, f_nogroup));
62140947Sbostic }
62240947Sbostic 
62340947Sbostic /*
62440947Sbostic  * -nouser functions --
62540947Sbostic  *
62640947Sbostic  *	True if file belongs to a user ID for which the equivalent
62740947Sbostic  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
62840947Sbostic  */
62940947Sbostic /* ARGSUSED */
63040947Sbostic f_nouser(plan, entry)
63140947Sbostic 	PLAN *plan;
63240947Sbostic 	FTSENT *entry;
63340947Sbostic {
63445615Sbostic 	char *user_from_uid();
63545615Sbostic 
63652239Sbostic 	return(user_from_uid(entry->fts_statp->st_uid, 1) ? 1 : 0);
63740947Sbostic }
63840947Sbostic 
63940947Sbostic PLAN *
64040947Sbostic c_nouser()
64140947Sbostic {
64240947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
64340947Sbostic 
64449868Sbostic 	return(palloc(N_NOUSER, f_nouser));
64540947Sbostic }
64640947Sbostic 
64740947Sbostic /*
64850437Sbostic  * -path functions --
64950437Sbostic  *
65050437Sbostic  *	True if the path of the filename being examined
65150437Sbostic  *	matches pattern using Pattern Matching Notation S3.14
65250437Sbostic  */
65350437Sbostic f_path(plan, entry)
65450437Sbostic 	PLAN *plan;
65550437Sbostic 	FTSENT *entry;
65650437Sbostic {
65754555Sbostic 	return(!fnmatch(plan->c_data, entry->fts_path, 0));
65850437Sbostic }
65950437Sbostic 
66050437Sbostic PLAN *
66150437Sbostic c_path(pattern)
66250437Sbostic 	char *pattern;
66350437Sbostic {
66450437Sbostic 	PLAN *new;
66550437Sbostic 
66650437Sbostic 	new = palloc(N_NAME, f_path);
66750437Sbostic 	new->c_data = pattern;
66850437Sbostic 	return(new);
66950437Sbostic }
67050437Sbostic 
67150437Sbostic /*
67240947Sbostic  * -perm functions --
67340947Sbostic  *
67440947Sbostic  *	The mode argument is used to represent file mode bits.  If it starts
67540947Sbostic  *	with a leading digit, it's treated as an octal mode, otherwise as a
67640947Sbostic  *	symbolic mode.
67740947Sbostic  */
67840947Sbostic f_perm(plan, entry)
67940947Sbostic 	PLAN *plan;
68040947Sbostic 	FTSENT *entry;
68140947Sbostic {
68240947Sbostic 	mode_t mode;
68340947Sbostic 
68452239Sbostic 	mode = entry->fts_statp->st_mode &
68540947Sbostic 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
68650437Sbostic 	if (plan->flags == F_ATLEAST)
68740947Sbostic 		return((plan->m_data | mode) == mode);
68840947Sbostic 	else
68940947Sbostic 		return(mode == plan->m_data);
69040947Sbostic 	/* NOTREACHED */
69140947Sbostic }
69240947Sbostic 
69340947Sbostic PLAN *
69440947Sbostic c_perm(perm)
69540947Sbostic 	char *perm;
69640947Sbostic {
69740947Sbostic 	PLAN *new;
69847192Sbostic 	mode_t *set;
69940947Sbostic 
70040947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
70140947Sbostic 
70249868Sbostic 	new = palloc(N_PERM, f_perm);
70340947Sbostic 
70440947Sbostic 	if (*perm == '-') {
70550437Sbostic 		new->flags = F_ATLEAST;
70640947Sbostic 		++perm;
70740947Sbostic 	}
70840947Sbostic 
70944764Strent 	if ((set = setmode(perm)) == NULL)
71049868Sbostic 		err("%s: %s", "-perm", "illegal mode string");
71140947Sbostic 
71244764Strent 	new->m_data = getmode(set, 0);
71340947Sbostic 	return(new);
71440947Sbostic }
71540947Sbostic 
71640947Sbostic /*
71740947Sbostic  * -print functions --
71840947Sbostic  *
71940947Sbostic  *	Always true, causes the current pathame to be written to
72040947Sbostic  *	standard output.
72140947Sbostic  */
72240947Sbostic /* ARGSUSED */
72340947Sbostic f_print(plan, entry)
72440947Sbostic 	PLAN *plan;
72540947Sbostic 	FTSENT *entry;
72640947Sbostic {
72742255Sbostic 	(void)printf("%s\n", entry->fts_path);
72842255Sbostic 	return(1);
72940947Sbostic }
73040947Sbostic 
73140947Sbostic PLAN *
73240947Sbostic c_print()
73340947Sbostic {
73445615Sbostic 	isoutput = 1;
73540947Sbostic 
73649868Sbostic 	return(palloc(N_PRINT, f_print));
73740947Sbostic }
73840947Sbostic 
73940947Sbostic /*
74040947Sbostic  * -prune functions --
74140947Sbostic  *
74240947Sbostic  *	Prune a portion of the hierarchy.
74340947Sbostic  */
74440947Sbostic /* ARGSUSED */
74540947Sbostic f_prune(plan, entry)
74640947Sbostic 	PLAN *plan;
74740947Sbostic 	FTSENT *entry;
74840947Sbostic {
74940947Sbostic 	extern FTS *tree;
75040947Sbostic 
75149868Sbostic 	if (fts_set(tree, entry, FTS_SKIP))
75249868Sbostic 		err("%s: %s", entry->fts_path, strerror(errno));
75342255Sbostic 	return(1);
75440947Sbostic }
75540947Sbostic 
75640947Sbostic PLAN *
75740947Sbostic c_prune()
75840947Sbostic {
75949868Sbostic 	return(palloc(N_PRUNE, f_prune));
76040947Sbostic }
76140947Sbostic 
76240947Sbostic /*
76340947Sbostic  * -size n[c] functions --
76440947Sbostic  *
76540947Sbostic  *	True if the file size in bytes, divided by an implementation defined
76640947Sbostic  *	value and rounded up to the next integer, is n.  If n is followed by
76740947Sbostic  *	a c, the size is in bytes.
76840947Sbostic  */
76940947Sbostic #define	FIND_SIZE	512
77040947Sbostic static int divsize = 1;
77140947Sbostic 
77240947Sbostic f_size(plan, entry)
77340947Sbostic 	PLAN *plan;
77440947Sbostic 	FTSENT *entry;
77540947Sbostic {
77640947Sbostic 	off_t size;
77740947Sbostic 
77852239Sbostic 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
77952239Sbostic 	    FIND_SIZE : entry->fts_statp->st_size;
78040947Sbostic 	COMPARE(size, plan->o_data);
78140947Sbostic }
78240947Sbostic 
78340947Sbostic PLAN *
78440947Sbostic c_size(arg)
78540947Sbostic 	char *arg;
78640947Sbostic {
78740947Sbostic 	PLAN *new;
78840947Sbostic 	char endch;
78940947Sbostic 
79040947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
79140947Sbostic 
79249868Sbostic 	new = palloc(N_SIZE, f_size);
79340947Sbostic 	new->o_data = find_parsenum(new, "-size", arg, &endch);
79440947Sbostic 	if (endch == 'c')
79540947Sbostic 		divsize = 0;
79640947Sbostic 	return(new);
79740947Sbostic }
79840947Sbostic 
79940947Sbostic /*
80040947Sbostic  * -type c functions --
80140947Sbostic  *
80240947Sbostic  *	True if the type of the file is c, where c is b, c, d, p, or f for
80340947Sbostic  *	block special file, character special file, directory, FIFO, or
80440947Sbostic  *	regular file, respectively.
80540947Sbostic  */
80640947Sbostic f_type(plan, entry)
80740947Sbostic 	PLAN *plan;
80840947Sbostic 	FTSENT *entry;
80940947Sbostic {
81052239Sbostic 	return((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
81140947Sbostic }
81240947Sbostic 
81340947Sbostic PLAN *
81440947Sbostic c_type(typestring)
81540947Sbostic 	char *typestring;
81640947Sbostic {
81740947Sbostic 	PLAN *new;
81840947Sbostic 	mode_t  mask;
81940947Sbostic 
82040947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
82140947Sbostic 
82240947Sbostic 	switch (typestring[0]) {
82340947Sbostic 	case 'b':
82440947Sbostic 		mask = S_IFBLK;
82540947Sbostic 		break;
82640947Sbostic 	case 'c':
82740947Sbostic 		mask = S_IFCHR;
82840947Sbostic 		break;
82940947Sbostic 	case 'd':
83040947Sbostic 		mask = S_IFDIR;
83140947Sbostic 		break;
83240947Sbostic 	case 'f':
83340947Sbostic 		mask = S_IFREG;
83440947Sbostic 		break;
83540947Sbostic 	case 'l':
83640947Sbostic 		mask = S_IFLNK;
83740947Sbostic 		break;
83840947Sbostic 	case 'p':
83940947Sbostic 		mask = S_IFIFO;
84040947Sbostic 		break;
84140947Sbostic 	case 's':
84240947Sbostic 		mask = S_IFSOCK;
84340947Sbostic 		break;
84440947Sbostic 	default:
84549868Sbostic 		err("%s: %s", "-type", "unknown type");
84640947Sbostic 	}
84740947Sbostic 
84849868Sbostic 	new = palloc(N_TYPE, f_type);
84940947Sbostic 	new->m_data = mask;
85040947Sbostic 	return(new);
85140947Sbostic }
85240947Sbostic 
85340947Sbostic /*
85440947Sbostic  * -user uname functions --
85540947Sbostic  *
85640947Sbostic  *	True if the file belongs to the user uname.  If uname is numeric and
85740947Sbostic  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
85840947Sbostic  *	return a valid user name, uname is taken as a user ID.
85940947Sbostic  */
86040947Sbostic f_user(plan, entry)
86140947Sbostic 	PLAN *plan;
86240947Sbostic 	FTSENT *entry;
86340947Sbostic {
86452239Sbostic 	return(entry->fts_statp->st_uid == plan->u_data);
86540947Sbostic }
86640947Sbostic 
86740947Sbostic PLAN *
86840947Sbostic c_user(username)
86940947Sbostic 	char *username;
87040947Sbostic {
87140947Sbostic 	PLAN *new;
87240947Sbostic 	struct passwd *p;
87340947Sbostic 	uid_t uid;
87440947Sbostic 
87540947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
87640947Sbostic 
87740947Sbostic 	p = getpwnam(username);
87840947Sbostic 	if (p == NULL) {
87940947Sbostic 		uid = atoi(username);
88040947Sbostic 		if (uid == 0 && username[0] != '0')
88149868Sbostic 			err("%s: %s", "-user", "no such user");
88240947Sbostic 	} else
88340947Sbostic 		uid = p->pw_uid;
88440947Sbostic 
88549868Sbostic 	new = palloc(N_USER, f_user);
88640947Sbostic 	new->u_data = uid;
88740947Sbostic 	return(new);
88840947Sbostic }
88940947Sbostic 
89040947Sbostic /*
89140947Sbostic  * -xdev functions --
89240947Sbostic  *
89340947Sbostic  *	Always true, causes find not to decend past directories that have a
89440947Sbostic  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
89540947Sbostic  */
89640947Sbostic PLAN *
89740947Sbostic c_xdev()
89840947Sbostic {
89942275Sbostic 	ftsoptions |= FTS_XDEV;
90040947Sbostic 
90149868Sbostic 	return(palloc(N_XDEV, f_always_true));
90240947Sbostic }
90340947Sbostic 
90440947Sbostic /*
90540947Sbostic  * ( expression ) functions --
90640947Sbostic  *
90740947Sbostic  *	True if expression is true.
90840947Sbostic  */
90940947Sbostic f_expr(plan, entry)
91040947Sbostic 	PLAN *plan;
91140947Sbostic 	FTSENT *entry;
91240947Sbostic {
91340947Sbostic 	register PLAN *p;
91440947Sbostic 	register int state;
91540947Sbostic 
91640947Sbostic 	for (p = plan->p_data[0];
91740947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
91840947Sbostic 	return(state);
91940947Sbostic }
92040947Sbostic 
92140947Sbostic /*
92249864Sbostic  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
92340947Sbostic  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
92449864Sbostic  * to a N_EXPR node containing the expression and the ')' node is discarded.
92540947Sbostic  */
92640947Sbostic PLAN *
92740947Sbostic c_openparen()
92840947Sbostic {
92949868Sbostic 	return(palloc(N_OPENPAREN, (int (*)())-1));
93040947Sbostic }
93140947Sbostic 
93240947Sbostic PLAN *
93340947Sbostic c_closeparen()
93440947Sbostic {
93549868Sbostic 	return(palloc(N_CLOSEPAREN, (int (*)())-1));
93640947Sbostic }
93740947Sbostic 
93840947Sbostic /*
93940947Sbostic  * ! expression functions --
94040947Sbostic  *
94140947Sbostic  *	Negation of a primary; the unary NOT operator.
94240947Sbostic  */
94340947Sbostic f_not(plan, entry)
94440947Sbostic 	PLAN *plan;
94540947Sbostic 	FTSENT *entry;
94640947Sbostic {
94740947Sbostic 	register PLAN *p;
94840947Sbostic 	register int state;
94940947Sbostic 
95040947Sbostic 	for (p = plan->p_data[0];
95140947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
95240947Sbostic 	return(!state);
95340947Sbostic }
95440947Sbostic 
95540947Sbostic PLAN *
95640947Sbostic c_not()
95740947Sbostic {
95849868Sbostic 	return(palloc(N_NOT, f_not));
95940947Sbostic }
96040947Sbostic 
96140947Sbostic /*
96240947Sbostic  * expression -o expression functions --
96340947Sbostic  *
96440947Sbostic  *	Alternation of primaries; the OR operator.  The second expression is
96540947Sbostic  * not evaluated if the first expression is true.
96640947Sbostic  */
96740947Sbostic f_or(plan, entry)
96840947Sbostic 	PLAN *plan;
96940947Sbostic 	FTSENT *entry;
97040947Sbostic {
97140947Sbostic 	register PLAN *p;
97240947Sbostic 	register int state;
97340947Sbostic 
97440947Sbostic 	for (p = plan->p_data[0];
97540947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
97640947Sbostic 
97740947Sbostic 	if (state)
97842255Sbostic 		return(1);
97940947Sbostic 
98040947Sbostic 	for (p = plan->p_data[1];
98140947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
98240947Sbostic 	return(state);
98340947Sbostic }
98440947Sbostic 
98540947Sbostic PLAN *
98640947Sbostic c_or()
98740947Sbostic {
98849868Sbostic 	return(palloc(N_OR, f_or));
98949868Sbostic }
99049868Sbostic 
99149868Sbostic static PLAN *
99249868Sbostic palloc(t, f)
99349868Sbostic 	enum ntype t;
99449868Sbostic 	int (*f)();
99549868Sbostic {
99640947Sbostic 	PLAN *new;
99740947Sbostic 
99849868Sbostic 	if (new = malloc(sizeof(PLAN))) {
99949868Sbostic 		new->type = t;
100049868Sbostic 		new->eval = f;
100149868Sbostic 		new->flags = 0;
100249868Sbostic 		new->next = NULL;
100349868Sbostic 		return(new);
100449868Sbostic 	}
100549868Sbostic 	err("%s", strerror(errno));
100649868Sbostic 	/* NOTREACHED */
100740947Sbostic }
1008