xref: /csrg-svn/usr.bin/find/function.c (revision 58066)
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*58066Smarc static char sccsid[] = "@(#)function.c	5.24 (Berkeley) 02/19/93";
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>
2054558Sbostic 
2147192Sbostic #include <errno.h>
2240947Sbostic #include <grp.h>
2340947Sbostic #include <pwd.h>
2440947Sbostic #include <fts.h>
2554558Sbostic #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  */
5155710Sbostic static long
52*58066Smarc 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 	/*
7555710Sbostic 	 * 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);
8055710Sbostic 	if (value == 0 && endchar == str)
8149868Sbostic 		err("%s: %s", option, "illegal numeric value");
8255710Sbostic 	if (endchar[0] && (endch == NULL || endchar[0] != *endch))
8355710Sbostic 		err("%s: %s", option, "illegal trailing character");
8440947Sbostic 	if (endch)
8540947Sbostic 		*endch = endchar[0];
8640947Sbostic 	return(value);
8740947Sbostic }
8840947Sbostic 
8940947Sbostic /*
9040947Sbostic  * -atime n functions --
9140947Sbostic  *
9240947Sbostic  *	True if the difference between the file access time and the
9340947Sbostic  *	current time is n 24 hour periods.
9440947Sbostic  *
9540947Sbostic  */
9640947Sbostic f_atime(plan, entry)
9740947Sbostic 	PLAN *plan;
9840947Sbostic 	FTSENT *entry;
9940947Sbostic {
10040947Sbostic 	extern time_t now;
10140947Sbostic 
10252239Sbostic 	COMPARE((now - entry->fts_statp->st_atime +
10342255Sbostic 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
10440947Sbostic }
10540947Sbostic 
10640947Sbostic PLAN *
10740947Sbostic c_atime(arg)
10840947Sbostic 	char *arg;
10940947Sbostic {
11040947Sbostic 	PLAN *new;
11140947Sbostic 
11240947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
11340947Sbostic 
11449868Sbostic 	new = palloc(N_ATIME, f_atime);
115*58066Smarc 	new->t_data = find_parsenum(new, "-atime", arg, NULL);
11640947Sbostic 	return(new);
11740947Sbostic }
11840947Sbostic /*
11940947Sbostic  * -ctime n functions --
12040947Sbostic  *
12140947Sbostic  *	True if the difference between the last change of file
12240947Sbostic  *	status information and the current time is n 24 hour periods.
12340947Sbostic  */
12440947Sbostic f_ctime(plan, entry)
12540947Sbostic 	PLAN *plan;
12640947Sbostic 	FTSENT *entry;
12740947Sbostic {
12840947Sbostic 	extern time_t now;
12940947Sbostic 
13052239Sbostic 	COMPARE((now - entry->fts_statp->st_ctime +
13142255Sbostic 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
13240947Sbostic }
13340947Sbostic 
13440947Sbostic PLAN *
13540947Sbostic c_ctime(arg)
13640947Sbostic 	char *arg;
13740947Sbostic {
13840947Sbostic 	PLAN *new;
13940947Sbostic 
14040947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
14140947Sbostic 
14249868Sbostic 	new = palloc(N_CTIME, f_ctime);
143*58066Smarc 	new->t_data = find_parsenum(new, "-ctime", arg, NULL);
14440947Sbostic 	return(new);
14540947Sbostic }
14640947Sbostic 
14740947Sbostic /*
14840947Sbostic  * -depth functions --
14940947Sbostic  *
15040947Sbostic  *	Always true, causes descent of the directory hierarchy to be done
15140947Sbostic  *	so that all entries in a directory are acted on before the directory
15240947Sbostic  *	itself.
15340947Sbostic  */
15440947Sbostic /* ARGSUSED */
15540947Sbostic f_always_true(plan, entry)
15640947Sbostic 	PLAN *plan;
15740947Sbostic 	FTSENT *entry;
15840947Sbostic {
15942255Sbostic 	return(1);
16040947Sbostic }
16140947Sbostic 
16240947Sbostic PLAN *
16340947Sbostic c_depth()
16440947Sbostic {
16545615Sbostic 	isdepth = 1;
16640947Sbostic 
16749868Sbostic 	return(palloc(N_DEPTH, f_always_true));
16840947Sbostic }
16940947Sbostic 
17040947Sbostic /*
17140947Sbostic  * [-exec | -ok] utility [arg ... ] ; functions --
17240947Sbostic  *
17340947Sbostic  *	True if the executed utility returns a zero value as exit status.
17440947Sbostic  *	The end of the primary expression is delimited by a semicolon.  If
17540947Sbostic  *	"{}" occurs anywhere, it gets replaced by the current pathname.
17640947Sbostic  *	The current directory for the execution of utility is the same as
17740947Sbostic  *	the current directory when the find utility was started.
17840947Sbostic  *
17940947Sbostic  *	The primary -ok is different in that it requests affirmation of the
18040947Sbostic  *	user before executing the utility.
18140947Sbostic  */
18240947Sbostic f_exec(plan, entry)
18340947Sbostic 	register PLAN *plan;
18440947Sbostic 	FTSENT *entry;
18540947Sbostic {
18649869Sbostic 	extern int dotfd;
18740947Sbostic 	register int cnt;
18847192Sbostic 	pid_t pid;
18947192Sbostic 	int status;
19040947Sbostic 
19140947Sbostic 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
19240947Sbostic 		if (plan->e_len[cnt])
19345616Sbostic 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
19449869Sbostic 			    entry->fts_path, plan->e_len[cnt]);
19540947Sbostic 
19650437Sbostic 	if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
19742255Sbostic 		return(0);
19840947Sbostic 
19940947Sbostic 	switch(pid = vfork()) {
20040947Sbostic 	case -1:
20149868Sbostic 		err("fork: %s", strerror(errno));
20240947Sbostic 		/* NOTREACHED */
20340947Sbostic 	case 0:
20449869Sbostic 		if (fchdir(dotfd)) {
20549869Sbostic 			(void)fprintf(stderr,
20649869Sbostic 			    "find: chdir: %s\n", strerror(errno));
20749869Sbostic 			_exit(1);
20849869Sbostic 		}
20940947Sbostic 		execvp(plan->e_argv[0], plan->e_argv);
21049869Sbostic 		(void)fprintf(stderr,
21149869Sbostic 		    "find: %s: %s\n", plan->e_argv[0], strerror(errno));
21249869Sbostic 		_exit(1);
21340947Sbostic 	}
21447192Sbostic 	pid = waitpid(pid, &status, 0);
21547192Sbostic 	return(pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
21640947Sbostic }
21740947Sbostic 
21840947Sbostic /*
21940947Sbostic  * c_exec --
22040947Sbostic  *	build three parallel arrays, one with pointers to the strings passed
22140947Sbostic  *	on the command line, one with (possibly duplicated) pointers to the
22240947Sbostic  *	argv array, and one with integer values that are lengths of the
22340947Sbostic  *	strings, but also flags meaning that the string has to be massaged.
22440947Sbostic  */
22540947Sbostic PLAN *
22640947Sbostic c_exec(argvp, isok)
22740947Sbostic 	char ***argvp;
22840947Sbostic 	int isok;
22940947Sbostic {
23040947Sbostic 	PLAN *new;			/* node returned */
23140947Sbostic 	register int cnt;
23240947Sbostic 	register char **argv, **ap, *p;
23340947Sbostic 
23447192Sbostic 	isoutput = 1;
23540947Sbostic 
23649868Sbostic 	new = palloc(N_EXEC, f_exec);
23750437Sbostic 	if (isok)
23850437Sbostic 		new->flags = F_NEEDOK;
23940947Sbostic 
24040947Sbostic 	for (ap = argv = *argvp;; ++ap) {
24140947Sbostic 		if (!*ap)
24249868Sbostic 			err("%s: %s",
24349868Sbostic 			    isok ? "-ok" : "-exec", "no terminating \";\"");
24440947Sbostic 		if (**ap == ';')
24540947Sbostic 			break;
24640947Sbostic 	}
24740947Sbostic 
24840947Sbostic 	cnt = ap - *argvp + 1;
24940947Sbostic 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
25040947Sbostic 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
25145868Sbostic 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
25240947Sbostic 
25340947Sbostic 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
25440947Sbostic 		new->e_orig[cnt] = *argv;
25540947Sbostic 		for (p = *argv; *p; ++p)
25640947Sbostic 			if (p[0] == '{' && p[1] == '}') {
25745868Sbostic 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
25845868Sbostic 				new->e_len[cnt] = MAXPATHLEN;
25940947Sbostic 				break;
26040947Sbostic 			}
26140947Sbostic 		if (!*p) {
26240947Sbostic 			new->e_argv[cnt] = *argv;
26340947Sbostic 			new->e_len[cnt] = 0;
26440947Sbostic 		}
26540947Sbostic 	}
26640947Sbostic 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
26740947Sbostic 
26840947Sbostic 	*argvp = argv + 1;
26940947Sbostic 	return(new);
27040947Sbostic }
27140947Sbostic 
27240947Sbostic /*
27340947Sbostic  * -follow functions --
27440947Sbostic  *
27540947Sbostic  *	Always true, causes symbolic links to be followed on a global
27640947Sbostic  *	basis.
27740947Sbostic  */
27840947Sbostic PLAN *
27940947Sbostic c_follow()
28040947Sbostic {
28140947Sbostic 	ftsoptions &= ~FTS_PHYSICAL;
28240947Sbostic 	ftsoptions |= FTS_LOGICAL;
28340947Sbostic 
28449868Sbostic 	return(palloc(N_FOLLOW, f_always_true));
28540947Sbostic }
28640947Sbostic 
28740947Sbostic /*
28840947Sbostic  * -fstype functions --
28940947Sbostic  *
29040947Sbostic  *	True if the file is of a certain type.
29140947Sbostic  */
29240947Sbostic f_fstype(plan, entry)
29340947Sbostic 	PLAN *plan;
29440947Sbostic 	FTSENT *entry;
29540947Sbostic {
29642275Sbostic 	static dev_t curdev;	/* need a guaranteed illegal dev value */
29742275Sbostic 	static int first = 1;
29840947Sbostic 	struct statfs sb;
29942255Sbostic 	static short val;
30045626Sbostic 	char *p, save[2];
30140947Sbostic 
30250437Sbostic 	/* Only check when we cross mount point. */
30352239Sbostic 	if (first || curdev != entry->fts_statp->st_dev) {
30452239Sbostic 		curdev = entry->fts_statp->st_dev;
30545626Sbostic 
30645626Sbostic 		/*
30745626Sbostic 		 * Statfs follows symlinks; find wants the link's file system,
30845626Sbostic 		 * not where it points.
30945626Sbostic 		 */
31045626Sbostic 		if (entry->fts_info == FTS_SL ||
31145626Sbostic 		    entry->fts_info == FTS_SLNONE) {
31245626Sbostic 			if (p = rindex(entry->fts_accpath, '/'))
31345626Sbostic 				++p;
31445626Sbostic 			else
31545626Sbostic 				p = entry->fts_accpath;
31645626Sbostic 			save[0] = p[0];
31745626Sbostic 			p[0] = '.';
31845626Sbostic 			save[1] = p[1];
31945626Sbostic 			p[1] = '\0';
32045626Sbostic 
32145626Sbostic 		} else
32245626Sbostic 			p = NULL;
32345626Sbostic 
32449868Sbostic 		if (statfs(entry->fts_accpath, &sb))
32549868Sbostic 			err("%s: %s", entry->fts_accpath, strerror(errno));
32645626Sbostic 
32745626Sbostic 		if (p) {
32845626Sbostic 			p[0] = save[0];
32945626Sbostic 			p[1] = save[1];
33045626Sbostic 		}
33145626Sbostic 
33242275Sbostic 		first = 0;
33350437Sbostic 		switch(plan->flags) {
33450437Sbostic 		case F_MTFLAG:
33550437Sbostic 			val = sb.f_flags;
33650437Sbostic 			break;
33750437Sbostic 		case F_MTTYPE:
33850437Sbostic 			val = sb.f_type;
33950437Sbostic 			break;
34050437Sbostic 		}
34140947Sbostic 	}
34250437Sbostic 	switch(plan->flags) {
34350437Sbostic 	case F_MTFLAG:
34450437Sbostic 		return(val & plan->mt_data);
34550437Sbostic 	case F_MTTYPE:
34650437Sbostic 		return(val == plan->mt_data);
34750437Sbostic 	}
34840947Sbostic }
34940947Sbostic 
35040947Sbostic PLAN *
35140947Sbostic c_fstype(arg)
35240947Sbostic 	char *arg;
35340947Sbostic {
35440947Sbostic 	register PLAN *new;
35540947Sbostic 
35640947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
35740947Sbostic 
35849868Sbostic 	new = palloc(N_FSTYPE, f_fstype);
35940947Sbostic 	switch(*arg) {
36042255Sbostic 	case 'l':
36142255Sbostic 		if (!strcmp(arg, "local")) {
36250437Sbostic 			new->flags = F_MTFLAG;
36350437Sbostic 			new->mt_data = MNT_LOCAL;
36442255Sbostic 			return(new);
36542255Sbostic 		}
36642255Sbostic 		break;
36740947Sbostic 	case 'm':
36840947Sbostic 		if (!strcmp(arg, "mfs")) {
36950437Sbostic 			new->flags = F_MTTYPE;
37050437Sbostic 			new->mt_data = MOUNT_MFS;
37140947Sbostic 			return(new);
37240947Sbostic 		}
37340947Sbostic 		break;
37440947Sbostic 	case 'n':
37540947Sbostic 		if (!strcmp(arg, "nfs")) {
37650437Sbostic 			new->flags = F_MTTYPE;
37750437Sbostic 			new->mt_data = MOUNT_NFS;
37840947Sbostic 			return(new);
37940947Sbostic 		}
38040947Sbostic 		break;
38140947Sbostic 	case 'p':
38240947Sbostic 		if (!strcmp(arg, "pc")) {
38350437Sbostic 			new->flags = F_MTTYPE;
38450437Sbostic 			new->mt_data = MOUNT_PC;
38540947Sbostic 			return(new);
38640947Sbostic 		}
38740947Sbostic 		break;
38850437Sbostic 	case 'r':
38950437Sbostic 		if (!strcmp(arg, "rdonly")) {
39050437Sbostic 			new->flags = F_MTFLAG;
39150437Sbostic 			new->mt_data = MNT_RDONLY;
39250437Sbostic 			return(new);
39350437Sbostic 		}
39450437Sbostic 		break;
39540947Sbostic 	case 'u':
39640947Sbostic 		if (!strcmp(arg, "ufs")) {
39750437Sbostic 			new->flags = F_MTTYPE;
39850437Sbostic 			new->mt_data = MOUNT_UFS;
39940947Sbostic 			return(new);
40040947Sbostic 		}
40140947Sbostic 		break;
40240947Sbostic 	}
40349868Sbostic 	err("unknown file type %s", arg);
40440947Sbostic 	/* NOTREACHED */
40540947Sbostic }
40640947Sbostic 
40740947Sbostic /*
40840947Sbostic  * -group gname functions --
40940947Sbostic  *
41040947Sbostic  *	True if the file belongs to the group gname.  If gname is numeric and
41140947Sbostic  *	an equivalent of the getgrnam() function does not return a valid group
41240947Sbostic  *	name, gname is taken as a group ID.
41340947Sbostic  */
41440947Sbostic f_group(plan, entry)
41540947Sbostic 	PLAN *plan;
41640947Sbostic 	FTSENT *entry;
41740947Sbostic {
41852239Sbostic 	return(entry->fts_statp->st_gid == plan->g_data);
41940947Sbostic }
42040947Sbostic 
42140947Sbostic PLAN *
42240947Sbostic c_group(gname)
42340947Sbostic 	char *gname;
42440947Sbostic {
42540947Sbostic 	PLAN *new;
42640947Sbostic 	struct group *g;
42740947Sbostic 	gid_t gid;
42840947Sbostic 
42940947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
43040947Sbostic 
43140947Sbostic 	g = getgrnam(gname);
43240947Sbostic 	if (g == NULL) {
43340947Sbostic 		gid = atoi(gname);
43440947Sbostic 		if (gid == 0 && gname[0] != '0')
43549868Sbostic 			err("%s: %s", "-group", "no such group");
43640947Sbostic 	} else
43740947Sbostic 		gid = g->gr_gid;
43840947Sbostic 
43949868Sbostic 	new = palloc(N_GROUP, f_group);
44040947Sbostic 	new->g_data = gid;
44140947Sbostic 	return(new);
44240947Sbostic }
44340947Sbostic 
44440947Sbostic /*
44540947Sbostic  * -inum n functions --
44640947Sbostic  *
44740947Sbostic  *	True if the file has inode # n.
44840947Sbostic  */
44940947Sbostic f_inum(plan, entry)
45040947Sbostic 	PLAN *plan;
45140947Sbostic 	FTSENT *entry;
45240947Sbostic {
45352239Sbostic 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
45440947Sbostic }
45540947Sbostic 
45640947Sbostic PLAN *
45740947Sbostic c_inum(arg)
45840947Sbostic 	char *arg;
45940947Sbostic {
46040947Sbostic 	PLAN *new;
46140947Sbostic 
46240947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
46340947Sbostic 
46449868Sbostic 	new = palloc(N_INUM, f_inum);
465*58066Smarc 	new->i_data = find_parsenum(new, "-inum", arg, NULL);
46640947Sbostic 	return(new);
46740947Sbostic }
46840947Sbostic 
46940947Sbostic /*
47040947Sbostic  * -links n functions --
47140947Sbostic  *
47240947Sbostic  *	True if the file has n links.
47340947Sbostic  */
47440947Sbostic f_links(plan, entry)
47540947Sbostic 	PLAN *plan;
47640947Sbostic 	FTSENT *entry;
47740947Sbostic {
47852239Sbostic 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
47940947Sbostic }
48040947Sbostic 
48140947Sbostic PLAN *
48240947Sbostic c_links(arg)
48340947Sbostic 	char *arg;
48440947Sbostic {
48540947Sbostic 	PLAN *new;
48640947Sbostic 
48740947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
48840947Sbostic 
48949868Sbostic 	new = palloc(N_LINKS, f_links);
490*58066Smarc 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
49140947Sbostic 	return(new);
49240947Sbostic }
49340947Sbostic 
49440947Sbostic /*
49540947Sbostic  * -ls functions --
49640947Sbostic  *
49740947Sbostic  *	Always true - prints the current entry to stdout in "ls" format.
49840947Sbostic  */
49940947Sbostic /* ARGSUSED */
50040947Sbostic f_ls(plan, entry)
50140947Sbostic 	PLAN *plan;
50240947Sbostic 	FTSENT *entry;
50340947Sbostic {
50452239Sbostic 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
50542255Sbostic 	return(1);
50640947Sbostic }
50740947Sbostic 
50840947Sbostic PLAN *
50940947Sbostic c_ls()
51040947Sbostic {
51140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
51245615Sbostic 	isoutput = 1;
51340947Sbostic 
51449868Sbostic 	return(palloc(N_LS, f_ls));
51540947Sbostic }
51640947Sbostic 
51740947Sbostic /*
51850437Sbostic  * -mtime n functions --
51950437Sbostic  *
52050437Sbostic  *	True if the difference between the file modification time and the
52150437Sbostic  *	current time is n 24 hour periods.
52250437Sbostic  */
52350437Sbostic f_mtime(plan, entry)
52450437Sbostic 	PLAN *plan;
52550437Sbostic 	FTSENT *entry;
52650437Sbostic {
52750437Sbostic 	extern time_t now;
52850437Sbostic 
52952239Sbostic 	COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
53050437Sbostic 	    SECSPERDAY, plan->t_data);
53150437Sbostic }
53250437Sbostic 
53350437Sbostic PLAN *
53450437Sbostic c_mtime(arg)
53550437Sbostic 	char *arg;
53650437Sbostic {
53750437Sbostic 	PLAN *new;
53850437Sbostic 
53950437Sbostic 	ftsoptions &= ~FTS_NOSTAT;
54050437Sbostic 
54150437Sbostic 	new = palloc(N_MTIME, f_mtime);
542*58066Smarc 	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
54350437Sbostic 	return(new);
54450437Sbostic }
54550437Sbostic 
54650437Sbostic /*
54740947Sbostic  * -name functions --
54840947Sbostic  *
54940947Sbostic  *	True if the basename of the filename being examined
55040947Sbostic  *	matches pattern using Pattern Matching Notation S3.14
55140947Sbostic  */
55240947Sbostic f_name(plan, entry)
55340947Sbostic 	PLAN *plan;
55440947Sbostic 	FTSENT *entry;
55540947Sbostic {
55654555Sbostic 	return(!fnmatch(plan->c_data, entry->fts_name, 0));
55740947Sbostic }
55840947Sbostic 
55940947Sbostic PLAN *
56040947Sbostic c_name(pattern)
56140947Sbostic 	char *pattern;
56240947Sbostic {
56340947Sbostic 	PLAN *new;
56440947Sbostic 
56549868Sbostic 	new = palloc(N_NAME, f_name);
56640947Sbostic 	new->c_data = pattern;
56740947Sbostic 	return(new);
56840947Sbostic }
56940947Sbostic 
57040947Sbostic /*
57140947Sbostic  * -newer file functions --
57240947Sbostic  *
57340947Sbostic  *	True if the current file has been modified more recently
57440947Sbostic  *	then the modification time of the file named by the pathname
57540947Sbostic  *	file.
57640947Sbostic  */
57740947Sbostic f_newer(plan, entry)
57840947Sbostic 	PLAN *plan;
57940947Sbostic 	FTSENT *entry;
58040947Sbostic {
58152239Sbostic 	return(entry->fts_statp->st_mtime > plan->t_data);
58240947Sbostic }
58340947Sbostic 
58440947Sbostic PLAN *
58540947Sbostic c_newer(filename)
58640947Sbostic 	char *filename;
58740947Sbostic {
58840947Sbostic 	PLAN *new;
58940947Sbostic 	struct stat sb;
59040947Sbostic 
59140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
59240947Sbostic 
59349868Sbostic 	if (stat(filename, &sb))
59449868Sbostic 		err("%s: %s", filename, strerror(errno));
59549868Sbostic 	new = palloc(N_NEWER, f_newer);
59640947Sbostic 	new->t_data = sb.st_mtime;
59740947Sbostic 	return(new);
59840947Sbostic }
59940947Sbostic 
60040947Sbostic /*
60140947Sbostic  * -nogroup functions --
60240947Sbostic  *
60340947Sbostic  *	True if file belongs to a user ID for which the equivalent
60440947Sbostic  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
60540947Sbostic  */
60640947Sbostic /* ARGSUSED */
60740947Sbostic f_nogroup(plan, entry)
60840947Sbostic 	PLAN *plan;
60940947Sbostic 	FTSENT *entry;
61040947Sbostic {
61145615Sbostic 	char *group_from_gid();
61245615Sbostic 
61352239Sbostic 	return(group_from_gid(entry->fts_statp->st_gid, 1) ? 1 : 0);
61440947Sbostic }
61540947Sbostic 
61640947Sbostic PLAN *
61740947Sbostic c_nogroup()
61840947Sbostic {
61940947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
62040947Sbostic 
62149868Sbostic 	return(palloc(N_NOGROUP, f_nogroup));
62240947Sbostic }
62340947Sbostic 
62440947Sbostic /*
62540947Sbostic  * -nouser functions --
62640947Sbostic  *
62740947Sbostic  *	True if file belongs to a user ID for which the equivalent
62840947Sbostic  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
62940947Sbostic  */
63040947Sbostic /* ARGSUSED */
63140947Sbostic f_nouser(plan, entry)
63240947Sbostic 	PLAN *plan;
63340947Sbostic 	FTSENT *entry;
63440947Sbostic {
63545615Sbostic 	char *user_from_uid();
63645615Sbostic 
63752239Sbostic 	return(user_from_uid(entry->fts_statp->st_uid, 1) ? 1 : 0);
63840947Sbostic }
63940947Sbostic 
64040947Sbostic PLAN *
64140947Sbostic c_nouser()
64240947Sbostic {
64340947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
64440947Sbostic 
64549868Sbostic 	return(palloc(N_NOUSER, f_nouser));
64640947Sbostic }
64740947Sbostic 
64840947Sbostic /*
64950437Sbostic  * -path functions --
65050437Sbostic  *
65150437Sbostic  *	True if the path of the filename being examined
65250437Sbostic  *	matches pattern using Pattern Matching Notation S3.14
65350437Sbostic  */
65450437Sbostic f_path(plan, entry)
65550437Sbostic 	PLAN *plan;
65650437Sbostic 	FTSENT *entry;
65750437Sbostic {
65854555Sbostic 	return(!fnmatch(plan->c_data, entry->fts_path, 0));
65950437Sbostic }
66050437Sbostic 
66150437Sbostic PLAN *
66250437Sbostic c_path(pattern)
66350437Sbostic 	char *pattern;
66450437Sbostic {
66550437Sbostic 	PLAN *new;
66650437Sbostic 
66750437Sbostic 	new = palloc(N_NAME, f_path);
66850437Sbostic 	new->c_data = pattern;
66950437Sbostic 	return(new);
67050437Sbostic }
67150437Sbostic 
67250437Sbostic /*
67340947Sbostic  * -perm functions --
67440947Sbostic  *
67540947Sbostic  *	The mode argument is used to represent file mode bits.  If it starts
67640947Sbostic  *	with a leading digit, it's treated as an octal mode, otherwise as a
67740947Sbostic  *	symbolic mode.
67840947Sbostic  */
67940947Sbostic f_perm(plan, entry)
68040947Sbostic 	PLAN *plan;
68140947Sbostic 	FTSENT *entry;
68240947Sbostic {
68340947Sbostic 	mode_t mode;
68440947Sbostic 
68552239Sbostic 	mode = entry->fts_statp->st_mode &
68640947Sbostic 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
68750437Sbostic 	if (plan->flags == F_ATLEAST)
68840947Sbostic 		return((plan->m_data | mode) == mode);
68940947Sbostic 	else
69040947Sbostic 		return(mode == plan->m_data);
69140947Sbostic 	/* NOTREACHED */
69240947Sbostic }
69340947Sbostic 
69440947Sbostic PLAN *
69540947Sbostic c_perm(perm)
69640947Sbostic 	char *perm;
69740947Sbostic {
69840947Sbostic 	PLAN *new;
69947192Sbostic 	mode_t *set;
70040947Sbostic 
70140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
70240947Sbostic 
70349868Sbostic 	new = palloc(N_PERM, f_perm);
70440947Sbostic 
70540947Sbostic 	if (*perm == '-') {
70650437Sbostic 		new->flags = F_ATLEAST;
70740947Sbostic 		++perm;
70840947Sbostic 	}
70940947Sbostic 
71044764Strent 	if ((set = setmode(perm)) == NULL)
71149868Sbostic 		err("%s: %s", "-perm", "illegal mode string");
71240947Sbostic 
71344764Strent 	new->m_data = getmode(set, 0);
71440947Sbostic 	return(new);
71540947Sbostic }
71640947Sbostic 
71740947Sbostic /*
71840947Sbostic  * -print functions --
71940947Sbostic  *
72040947Sbostic  *	Always true, causes the current pathame to be written to
72140947Sbostic  *	standard output.
72240947Sbostic  */
72340947Sbostic /* ARGSUSED */
72440947Sbostic f_print(plan, entry)
72540947Sbostic 	PLAN *plan;
72640947Sbostic 	FTSENT *entry;
72740947Sbostic {
72842255Sbostic 	(void)printf("%s\n", entry->fts_path);
72942255Sbostic 	return(1);
73040947Sbostic }
73140947Sbostic 
73240947Sbostic PLAN *
73340947Sbostic c_print()
73440947Sbostic {
73545615Sbostic 	isoutput = 1;
73640947Sbostic 
73749868Sbostic 	return(palloc(N_PRINT, f_print));
73840947Sbostic }
73940947Sbostic 
74040947Sbostic /*
74140947Sbostic  * -prune functions --
74240947Sbostic  *
74340947Sbostic  *	Prune a portion of the hierarchy.
74440947Sbostic  */
74540947Sbostic /* ARGSUSED */
74640947Sbostic f_prune(plan, entry)
74740947Sbostic 	PLAN *plan;
74840947Sbostic 	FTSENT *entry;
74940947Sbostic {
75040947Sbostic 	extern FTS *tree;
75140947Sbostic 
75249868Sbostic 	if (fts_set(tree, entry, FTS_SKIP))
75349868Sbostic 		err("%s: %s", entry->fts_path, strerror(errno));
75442255Sbostic 	return(1);
75540947Sbostic }
75640947Sbostic 
75740947Sbostic PLAN *
75840947Sbostic c_prune()
75940947Sbostic {
76049868Sbostic 	return(palloc(N_PRUNE, f_prune));
76140947Sbostic }
76240947Sbostic 
76340947Sbostic /*
76440947Sbostic  * -size n[c] functions --
76540947Sbostic  *
76640947Sbostic  *	True if the file size in bytes, divided by an implementation defined
76740947Sbostic  *	value and rounded up to the next integer, is n.  If n is followed by
76840947Sbostic  *	a c, the size is in bytes.
76940947Sbostic  */
77040947Sbostic #define	FIND_SIZE	512
77140947Sbostic static int divsize = 1;
77240947Sbostic 
77340947Sbostic f_size(plan, entry)
77440947Sbostic 	PLAN *plan;
77540947Sbostic 	FTSENT *entry;
77640947Sbostic {
77740947Sbostic 	off_t size;
77840947Sbostic 
77952239Sbostic 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
78052239Sbostic 	    FIND_SIZE : entry->fts_statp->st_size;
78140947Sbostic 	COMPARE(size, plan->o_data);
78240947Sbostic }
78340947Sbostic 
78440947Sbostic PLAN *
78540947Sbostic c_size(arg)
78640947Sbostic 	char *arg;
78740947Sbostic {
78840947Sbostic 	PLAN *new;
78940947Sbostic 	char endch;
79040947Sbostic 
79140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
79240947Sbostic 
79349868Sbostic 	new = palloc(N_SIZE, f_size);
79455710Sbostic 	endch = 'c';
795*58066Smarc 	new->o_data = find_parsenum(new, "-size", arg, &endch);
79640947Sbostic 	if (endch == 'c')
79740947Sbostic 		divsize = 0;
79840947Sbostic 	return(new);
79940947Sbostic }
80040947Sbostic 
80140947Sbostic /*
80240947Sbostic  * -type c functions --
80340947Sbostic  *
80440947Sbostic  *	True if the type of the file is c, where c is b, c, d, p, or f for
80540947Sbostic  *	block special file, character special file, directory, FIFO, or
80640947Sbostic  *	regular file, respectively.
80740947Sbostic  */
80840947Sbostic f_type(plan, entry)
80940947Sbostic 	PLAN *plan;
81040947Sbostic 	FTSENT *entry;
81140947Sbostic {
81252239Sbostic 	return((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
81340947Sbostic }
81440947Sbostic 
81540947Sbostic PLAN *
81640947Sbostic c_type(typestring)
81740947Sbostic 	char *typestring;
81840947Sbostic {
81940947Sbostic 	PLAN *new;
82040947Sbostic 	mode_t  mask;
82140947Sbostic 
82240947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
82340947Sbostic 
82440947Sbostic 	switch (typestring[0]) {
82540947Sbostic 	case 'b':
82640947Sbostic 		mask = S_IFBLK;
82740947Sbostic 		break;
82840947Sbostic 	case 'c':
82940947Sbostic 		mask = S_IFCHR;
83040947Sbostic 		break;
83140947Sbostic 	case 'd':
83240947Sbostic 		mask = S_IFDIR;
83340947Sbostic 		break;
83440947Sbostic 	case 'f':
83540947Sbostic 		mask = S_IFREG;
83640947Sbostic 		break;
83740947Sbostic 	case 'l':
83840947Sbostic 		mask = S_IFLNK;
83940947Sbostic 		break;
84040947Sbostic 	case 'p':
84140947Sbostic 		mask = S_IFIFO;
84240947Sbostic 		break;
84340947Sbostic 	case 's':
84440947Sbostic 		mask = S_IFSOCK;
84540947Sbostic 		break;
84640947Sbostic 	default:
84749868Sbostic 		err("%s: %s", "-type", "unknown type");
84840947Sbostic 	}
84940947Sbostic 
85049868Sbostic 	new = palloc(N_TYPE, f_type);
85140947Sbostic 	new->m_data = mask;
85240947Sbostic 	return(new);
85340947Sbostic }
85440947Sbostic 
85540947Sbostic /*
85640947Sbostic  * -user uname functions --
85740947Sbostic  *
85840947Sbostic  *	True if the file belongs to the user uname.  If uname is numeric and
85940947Sbostic  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
86040947Sbostic  *	return a valid user name, uname is taken as a user ID.
86140947Sbostic  */
86240947Sbostic f_user(plan, entry)
86340947Sbostic 	PLAN *plan;
86440947Sbostic 	FTSENT *entry;
86540947Sbostic {
86652239Sbostic 	return(entry->fts_statp->st_uid == plan->u_data);
86740947Sbostic }
86840947Sbostic 
86940947Sbostic PLAN *
87040947Sbostic c_user(username)
87140947Sbostic 	char *username;
87240947Sbostic {
87340947Sbostic 	PLAN *new;
87440947Sbostic 	struct passwd *p;
87540947Sbostic 	uid_t uid;
87640947Sbostic 
87740947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
87840947Sbostic 
87940947Sbostic 	p = getpwnam(username);
88040947Sbostic 	if (p == NULL) {
88140947Sbostic 		uid = atoi(username);
88240947Sbostic 		if (uid == 0 && username[0] != '0')
88349868Sbostic 			err("%s: %s", "-user", "no such user");
88440947Sbostic 	} else
88540947Sbostic 		uid = p->pw_uid;
88640947Sbostic 
88749868Sbostic 	new = palloc(N_USER, f_user);
88840947Sbostic 	new->u_data = uid;
88940947Sbostic 	return(new);
89040947Sbostic }
89140947Sbostic 
89240947Sbostic /*
89340947Sbostic  * -xdev functions --
89440947Sbostic  *
89540947Sbostic  *	Always true, causes find not to decend past directories that have a
89640947Sbostic  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
89740947Sbostic  */
89840947Sbostic PLAN *
89940947Sbostic c_xdev()
90040947Sbostic {
90142275Sbostic 	ftsoptions |= FTS_XDEV;
90240947Sbostic 
90349868Sbostic 	return(palloc(N_XDEV, f_always_true));
90440947Sbostic }
90540947Sbostic 
90640947Sbostic /*
90740947Sbostic  * ( expression ) functions --
90840947Sbostic  *
90940947Sbostic  *	True if expression is true.
91040947Sbostic  */
91140947Sbostic f_expr(plan, entry)
91240947Sbostic 	PLAN *plan;
91340947Sbostic 	FTSENT *entry;
91440947Sbostic {
91540947Sbostic 	register PLAN *p;
91640947Sbostic 	register int state;
91740947Sbostic 
91840947Sbostic 	for (p = plan->p_data[0];
91940947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
92040947Sbostic 	return(state);
92140947Sbostic }
92240947Sbostic 
92340947Sbostic /*
92449864Sbostic  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
92540947Sbostic  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
92649864Sbostic  * to a N_EXPR node containing the expression and the ')' node is discarded.
92740947Sbostic  */
92840947Sbostic PLAN *
92940947Sbostic c_openparen()
93040947Sbostic {
93149868Sbostic 	return(palloc(N_OPENPAREN, (int (*)())-1));
93240947Sbostic }
93340947Sbostic 
93440947Sbostic PLAN *
93540947Sbostic c_closeparen()
93640947Sbostic {
93749868Sbostic 	return(palloc(N_CLOSEPAREN, (int (*)())-1));
93840947Sbostic }
93940947Sbostic 
94040947Sbostic /*
94140947Sbostic  * ! expression functions --
94240947Sbostic  *
94340947Sbostic  *	Negation of a primary; the unary NOT operator.
94440947Sbostic  */
94540947Sbostic f_not(plan, entry)
94640947Sbostic 	PLAN *plan;
94740947Sbostic 	FTSENT *entry;
94840947Sbostic {
94940947Sbostic 	register PLAN *p;
95040947Sbostic 	register int state;
95140947Sbostic 
95240947Sbostic 	for (p = plan->p_data[0];
95340947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
95440947Sbostic 	return(!state);
95540947Sbostic }
95640947Sbostic 
95740947Sbostic PLAN *
95840947Sbostic c_not()
95940947Sbostic {
96049868Sbostic 	return(palloc(N_NOT, f_not));
96140947Sbostic }
96240947Sbostic 
96340947Sbostic /*
96440947Sbostic  * expression -o expression functions --
96540947Sbostic  *
96640947Sbostic  *	Alternation of primaries; the OR operator.  The second expression is
96740947Sbostic  * not evaluated if the first expression is true.
96840947Sbostic  */
96940947Sbostic f_or(plan, entry)
97040947Sbostic 	PLAN *plan;
97140947Sbostic 	FTSENT *entry;
97240947Sbostic {
97340947Sbostic 	register PLAN *p;
97440947Sbostic 	register int state;
97540947Sbostic 
97640947Sbostic 	for (p = plan->p_data[0];
97740947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
97840947Sbostic 
97940947Sbostic 	if (state)
98042255Sbostic 		return(1);
98140947Sbostic 
98240947Sbostic 	for (p = plan->p_data[1];
98340947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
98440947Sbostic 	return(state);
98540947Sbostic }
98640947Sbostic 
98740947Sbostic PLAN *
98840947Sbostic c_or()
98940947Sbostic {
99049868Sbostic 	return(palloc(N_OR, f_or));
99149868Sbostic }
99249868Sbostic 
99349868Sbostic static PLAN *
99449868Sbostic palloc(t, f)
99549868Sbostic 	enum ntype t;
99649868Sbostic 	int (*f)();
99749868Sbostic {
99840947Sbostic 	PLAN *new;
99940947Sbostic 
100049868Sbostic 	if (new = malloc(sizeof(PLAN))) {
100149868Sbostic 		new->type = t;
100249868Sbostic 		new->eval = f;
100349868Sbostic 		new->flags = 0;
100449868Sbostic 		new->next = NULL;
100549868Sbostic 		return(new);
100649868Sbostic 	}
100749868Sbostic 	err("%s", strerror(errno));
100849868Sbostic 	/* NOTREACHED */
100940947Sbostic }
1010