xref: /csrg-svn/usr.bin/find/function.c (revision 45626)
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*45626Sbostic static char sccsid[] = "@(#)function.c	5.12 (Berkeley) 11/18/90";
1340947Sbostic #endif /* not lint */
1440947Sbostic 
1540947Sbostic #include <sys/types.h>
1640947Sbostic #include <sys/stat.h>
1740947Sbostic #include <sys/wait.h>
1840947Sbostic #include <sys/mount.h>
1945615Sbostic #include <sys/errno.h>
2040947Sbostic #include <grp.h>
2140947Sbostic #include <pwd.h>
2240947Sbostic #include <fts.h>
2340947Sbostic #include <unistd.h>
2440947Sbostic #include <tzfile.h>
2540947Sbostic #include <stdio.h>
2642047Sbostic #include <string.h>
2740947Sbostic #include "find.h"
2840947Sbostic 
2940947Sbostic #define	FIND_EQUAL	0
3040947Sbostic #define	FIND_LESSTHAN	1
3140947Sbostic #define	FIND_GREATER	2
3240947Sbostic 
3340947Sbostic #define	COMPARE(a, b) { \
3440947Sbostic 	switch(plan->flags) { \
3540947Sbostic 	case FIND_EQUAL: \
3640947Sbostic 		return(a == b); \
3740947Sbostic 	case FIND_LESSTHAN: \
3840947Sbostic 		return(a < b); \
3940947Sbostic 	case FIND_GREATER: \
4040947Sbostic 		return(a > b); \
4140947Sbostic 	} \
4242255Sbostic 	return(0); \
4340947Sbostic }
4440947Sbostic 
4540947Sbostic #define NEW(t, f) { \
4640947Sbostic 	new = (PLAN *)emalloc(sizeof(PLAN)); \
4740947Sbostic 	new->type = t; \
4840947Sbostic 	new->eval = f; \
4940947Sbostic 	new->flags = 0; \
5040947Sbostic 	new->next = NULL; \
5140947Sbostic }
5240947Sbostic 
5340947Sbostic /*
5440947Sbostic  * find_parsenum --
5540947Sbostic  *	Parse a string of the form [+-]# and return the value.
5640947Sbostic  */
5740947Sbostic long
5840947Sbostic find_parsenum(plan, option, str, endch)
5940947Sbostic 	PLAN *plan;
6040947Sbostic 	char *option, *str, *endch;
6140947Sbostic {
6240947Sbostic 	long value;
6340947Sbostic 	char *endchar;		/* pointer to character ending conversion */
6445615Sbostic 	void bad_arg();
6540947Sbostic 
6640947Sbostic 	/* determine comparison from leading + or - */
6740947Sbostic 	switch(*str) {
6840947Sbostic 	case '+':
6940947Sbostic 		++str;
7040947Sbostic 		plan->flags = FIND_GREATER;
7140947Sbostic 		break;
7240947Sbostic 	case '-':
7340947Sbostic 		++str;
7440947Sbostic 		plan->flags = FIND_LESSTHAN;
7540947Sbostic 		break;
7640947Sbostic 	default:
7740947Sbostic 		plan->flags = FIND_EQUAL;
7840947Sbostic 		break;
7940947Sbostic 	}
8040947Sbostic 
8140947Sbostic 	/*
8240947Sbostic 	 * convert the string with strtol().  Note, if strtol() returns zero
8340947Sbostic 	 * and endchar points to the beginning of the string we know we have
8440947Sbostic 	 * a syntax error.
8540947Sbostic 	 */
8640947Sbostic 	value = strtol(str, &endchar, 10);
8740947Sbostic 	if (!value && endchar == str ||
8840947Sbostic 	    endchar[0] && (!endch || endchar[0] != *endch))
8940947Sbostic 		bad_arg(option, "illegal numeric value");
9040947Sbostic 	if (endch)
9140947Sbostic 		*endch = endchar[0];
9240947Sbostic 	return(value);
9340947Sbostic }
9440947Sbostic 
9540947Sbostic /*
9640947Sbostic  * -atime n functions --
9740947Sbostic  *
9840947Sbostic  *	True if the difference between the file access time and the
9940947Sbostic  *	current time is n 24 hour periods.
10040947Sbostic  *
10140947Sbostic  */
10240947Sbostic f_atime(plan, entry)
10340947Sbostic 	PLAN *plan;
10440947Sbostic 	FTSENT *entry;
10540947Sbostic {
10640947Sbostic 	extern time_t now;
10740947Sbostic 
10842255Sbostic 	COMPARE((now - entry->fts_statb.st_atime +
10942255Sbostic 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
11040947Sbostic }
11140947Sbostic 
11240947Sbostic PLAN *
11340947Sbostic c_atime(arg)
11440947Sbostic 	char *arg;
11540947Sbostic {
11640947Sbostic 	PLAN *new;
11740947Sbostic 
11840947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
11940947Sbostic 
12040947Sbostic 	NEW(T_ATIME, f_atime);
12140947Sbostic 	new->t_data = find_parsenum(new, "-atime", arg, (char *)NULL);
12240947Sbostic 	return(new);
12340947Sbostic }
12440947Sbostic /*
12540947Sbostic  * -ctime n functions --
12640947Sbostic  *
12740947Sbostic  *	True if the difference between the last change of file
12840947Sbostic  *	status information and the current time is n 24 hour periods.
12940947Sbostic  */
13040947Sbostic f_ctime(plan, entry)
13140947Sbostic 	PLAN *plan;
13240947Sbostic 	FTSENT *entry;
13340947Sbostic {
13440947Sbostic 	extern time_t now;
13540947Sbostic 
13642255Sbostic 	COMPARE((now - entry->fts_statb.st_ctime +
13742255Sbostic 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
13840947Sbostic }
13940947Sbostic 
14040947Sbostic PLAN *
14140947Sbostic c_ctime(arg)
14240947Sbostic 	char *arg;
14340947Sbostic {
14440947Sbostic 	PLAN *new;
14540947Sbostic 
14640947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
14740947Sbostic 
14840947Sbostic 	NEW(T_CTIME, f_ctime);
14940947Sbostic 	new->t_data = find_parsenum(new, "-ctime", arg, (char *)NULL);
15040947Sbostic 	return(new);
15140947Sbostic }
15240947Sbostic 
15340947Sbostic /*
15440947Sbostic  * -depth functions --
15540947Sbostic  *
15640947Sbostic  *	Always true, causes descent of the directory hierarchy to be done
15740947Sbostic  *	so that all entries in a directory are acted on before the directory
15840947Sbostic  *	itself.
15940947Sbostic  */
16040947Sbostic /* ARGSUSED */
16140947Sbostic f_always_true(plan, entry)
16240947Sbostic 	PLAN *plan;
16340947Sbostic 	FTSENT *entry;
16440947Sbostic {
16542255Sbostic 	return(1);
16640947Sbostic }
16740947Sbostic 
16840947Sbostic PLAN *
16940947Sbostic c_depth()
17040947Sbostic {
17140947Sbostic 	PLAN *new;
17240947Sbostic 
17345615Sbostic 	isdepth = 1;
17440947Sbostic 
17540947Sbostic 	NEW(T_DEPTH, f_always_true);
17640947Sbostic 	return(new);
17740947Sbostic }
17840947Sbostic 
17940947Sbostic /*
18040947Sbostic  * [-exec | -ok] utility [arg ... ] ; functions --
18140947Sbostic  *
18240947Sbostic  *	True if the executed utility returns a zero value as exit status.
18340947Sbostic  *	The end of the primary expression is delimited by a semicolon.  If
18440947Sbostic  *	"{}" occurs anywhere, it gets replaced by the current pathname.
18540947Sbostic  *	The current directory for the execution of utility is the same as
18640947Sbostic  *	the current directory when the find utility was started.
18740947Sbostic  *
18840947Sbostic  *	The primary -ok is different in that it requests affirmation of the
18940947Sbostic  *	user before executing the utility.
19040947Sbostic  */
19140947Sbostic f_exec(plan, entry)
19240947Sbostic 	register PLAN *plan;
19340947Sbostic 	FTSENT *entry;
19440947Sbostic {
19540947Sbostic 	register int cnt;
19640947Sbostic 	union wait pstat;
19740947Sbostic 	pid_t pid, waitpid();
19845616Sbostic 	void brace_subst();
19940947Sbostic 
20040947Sbostic 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
20140947Sbostic 		if (plan->e_len[cnt])
20245616Sbostic 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
20345404Sbostic 			    entry->fts_accpath, plan->e_len[cnt]);
20440947Sbostic 
20545616Sbostic 	if (plan->flags && !queryuser(plan->e_argv))
20642255Sbostic 		return(0);
20740947Sbostic 
20840947Sbostic 	switch(pid = vfork()) {
20940947Sbostic 	case -1:
21040947Sbostic 		(void)fprintf(stderr, "find: fork: %s.\n", strerror(errno));
21140947Sbostic 		exit(1);
21240947Sbostic 		/* NOTREACHED */
21340947Sbostic 	case 0:
21440947Sbostic 		execvp(plan->e_argv[0], plan->e_argv);
21540947Sbostic 		(void)fprintf(stderr,
21640947Sbostic 		    "find: %s: %s.\n", plan->e_argv[0], strerror(errno));
21740947Sbostic 		exit(1);
21840947Sbostic 		/* NOTREACHED */
21940947Sbostic 	}
22040947Sbostic 	pid = waitpid(pid, &pstat, 0);
22142255Sbostic 	return(pid != -1 && !pstat.w_status);
22240947Sbostic }
22340947Sbostic 
22440947Sbostic /*
22540947Sbostic  * c_exec --
22640947Sbostic  *	build three parallel arrays, one with pointers to the strings passed
22740947Sbostic  *	on the command line, one with (possibly duplicated) pointers to the
22840947Sbostic  *	argv array, and one with integer values that are lengths of the
22940947Sbostic  *	strings, but also flags meaning that the string has to be massaged.
23040947Sbostic  */
23140947Sbostic PLAN *
23240947Sbostic c_exec(argvp, isok)
23340947Sbostic 	char ***argvp;
23440947Sbostic 	int isok;
23540947Sbostic {
23640947Sbostic 	PLAN *new;			/* node returned */
23740947Sbostic 	register int cnt;
23840947Sbostic 	register char **argv, **ap, *p;
23945615Sbostic 	void bad_arg();
24040947Sbostic 
24145615Sbostic 	if (!isrelative)
24245404Sbostic 		ftsoptions |= FTS_NOCHDIR;
24345615Sbostic 	isoutput = isstopdnx = 1;
24440947Sbostic 
24540947Sbostic 	NEW(T_EXEC, f_exec);
24640947Sbostic 	new->flags = isok;
24740947Sbostic 
24840947Sbostic 	for (ap = argv = *argvp;; ++ap) {
24940947Sbostic 		if (!*ap)
25040947Sbostic 			bad_arg(isok ? "-ok" : "-exec", "no terminating \";\"");
25140947Sbostic 		if (**ap == ';')
25240947Sbostic 			break;
25340947Sbostic 	}
25440947Sbostic 
25540947Sbostic 	cnt = ap - *argvp + 1;
25640947Sbostic 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
25740947Sbostic 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
25840947Sbostic 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(u_char));
25940947Sbostic 
26040947Sbostic 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
26140947Sbostic 		new->e_orig[cnt] = *argv;
26240947Sbostic 		for (p = *argv; *p; ++p)
26340947Sbostic 			if (p[0] == '{' && p[1] == '}') {
26440947Sbostic 				new->e_argv[cnt] = emalloc((u_int)1024);
26540947Sbostic 				new->e_len[cnt] = 1024;
26640947Sbostic 				break;
26740947Sbostic 			}
26840947Sbostic 		if (!*p) {
26940947Sbostic 			new->e_argv[cnt] = *argv;
27040947Sbostic 			new->e_len[cnt] = 0;
27140947Sbostic 		}
27240947Sbostic 	}
27340947Sbostic 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
27440947Sbostic 
27540947Sbostic 	*argvp = argv + 1;
27640947Sbostic 	return(new);
27740947Sbostic }
27840947Sbostic 
27940947Sbostic /*
28040947Sbostic  * -follow functions --
28140947Sbostic  *
28240947Sbostic  *	Always true, causes symbolic links to be followed on a global
28340947Sbostic  *	basis.
28440947Sbostic  */
28540947Sbostic PLAN *
28640947Sbostic c_follow()
28740947Sbostic {
28840947Sbostic 	PLAN *new;
28940947Sbostic 
29040947Sbostic 	ftsoptions &= ~FTS_PHYSICAL;
29140947Sbostic 	ftsoptions |= FTS_LOGICAL;
29240947Sbostic 
29340947Sbostic 	NEW(T_FOLLOW, f_always_true);
29440947Sbostic 	return(new);
29540947Sbostic }
29640947Sbostic 
29740947Sbostic /*
29840947Sbostic  * -fstype functions --
29940947Sbostic  *
30040947Sbostic  *	True if the file is of a certain type.
30140947Sbostic  */
30240947Sbostic f_fstype(plan, entry)
30340947Sbostic 	PLAN *plan;
30440947Sbostic 	FTSENT *entry;
30540947Sbostic {
30642275Sbostic 	static dev_t curdev;	/* need a guaranteed illegal dev value */
30742275Sbostic 	static int first = 1;
30840947Sbostic 	struct statfs sb;
30942255Sbostic 	static short val;
310*45626Sbostic 	char *p, save[2];
31140947Sbostic 
31240947Sbostic 	/* only check when we cross mount point */
31342275Sbostic 	if (first || curdev != entry->fts_statb.st_dev) {
31443856Sbostic 		curdev = entry->fts_statb.st_dev;
315*45626Sbostic 
316*45626Sbostic 		/*
317*45626Sbostic 		 * Statfs follows symlinks; find wants the link's file system,
318*45626Sbostic 		 * not where it points.
319*45626Sbostic 		 */
320*45626Sbostic 		if (entry->fts_info == FTS_SL ||
321*45626Sbostic 		    entry->fts_info == FTS_SLNONE) {
322*45626Sbostic 			if (p = rindex(entry->fts_accpath, '/'))
323*45626Sbostic 				++p;
324*45626Sbostic 			else
325*45626Sbostic 				p = entry->fts_accpath;
326*45626Sbostic 			save[0] = p[0];
327*45626Sbostic 			p[0] = '.';
328*45626Sbostic 			save[1] = p[1];
329*45626Sbostic 			p[1] = '\0';
330*45626Sbostic 
331*45626Sbostic 		} else
332*45626Sbostic 			p = NULL;
333*45626Sbostic 
33442255Sbostic 		if (statfs(entry->fts_accpath, &sb)) {
33540947Sbostic 			(void)fprintf(stderr, "find: %s: %s.\n",
33642255Sbostic 			    entry->fts_accpath, strerror(errno));
33740947Sbostic 			exit(1);
33840947Sbostic 		}
339*45626Sbostic 
340*45626Sbostic 		if (p) {
341*45626Sbostic 			p[0] = save[0];
342*45626Sbostic 			p[1] = save[1];
343*45626Sbostic 		}
344*45626Sbostic 
34542275Sbostic 		first = 0;
34642255Sbostic 		val = plan->flags == MOUNT_NONE ? sb.f_flags : sb.f_type;
34740947Sbostic 	}
34842255Sbostic 	return(plan->flags == MOUNT_NONE ?
34942255Sbostic 	    val & MNT_LOCAL : val == plan->flags);
35040947Sbostic }
35140947Sbostic 
35240947Sbostic PLAN *
35340947Sbostic c_fstype(arg)
35440947Sbostic 	char *arg;
35540947Sbostic {
35640947Sbostic 	register PLAN *new;
35740947Sbostic 
35840947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
35940947Sbostic 
36040947Sbostic 	NEW(T_FSTYPE, f_fstype);
36140947Sbostic 	switch(*arg) {
36242255Sbostic 	case 'l':
36342255Sbostic 		if (!strcmp(arg, "local")) {
36442255Sbostic 			new->flags = MOUNT_NONE;
36542255Sbostic 			return(new);
36642255Sbostic 		}
36742255Sbostic 		break;
36840947Sbostic 	case 'm':
36940947Sbostic 		if (!strcmp(arg, "mfs")) {
37040947Sbostic 			new->flags = MOUNT_MFS;
37140947Sbostic 			return(new);
37240947Sbostic 		}
37340947Sbostic 		break;
37440947Sbostic 	case 'n':
37540947Sbostic 		if (!strcmp(arg, "nfs")) {
37640947Sbostic 			new->flags = MOUNT_NFS;
37740947Sbostic 			return(new);
37840947Sbostic 		}
37940947Sbostic 		break;
38040947Sbostic 	case 'p':
38140947Sbostic 		if (!strcmp(arg, "pc")) {
38240947Sbostic 			new->flags = MOUNT_PC;
38340947Sbostic 			return(new);
38440947Sbostic 		}
38540947Sbostic 		break;
38640947Sbostic 	case 'u':
38740947Sbostic 		if (!strcmp(arg, "ufs")) {
38840947Sbostic 			new->flags = MOUNT_UFS;
38940947Sbostic 			return(new);
39040947Sbostic 		}
39140947Sbostic 		break;
39240947Sbostic 	}
39340947Sbostic 	(void)fprintf(stderr, "find: unknown file type %s.\n", arg);
39440947Sbostic 	exit(1);
39540947Sbostic 	/* NOTREACHED */
39640947Sbostic }
39740947Sbostic 
39840947Sbostic /*
39940947Sbostic  * -group gname functions --
40040947Sbostic  *
40140947Sbostic  *	True if the file belongs to the group gname.  If gname is numeric and
40240947Sbostic  *	an equivalent of the getgrnam() function does not return a valid group
40340947Sbostic  *	name, gname is taken as a group ID.
40440947Sbostic  */
40540947Sbostic f_group(plan, entry)
40640947Sbostic 	PLAN *plan;
40740947Sbostic 	FTSENT *entry;
40840947Sbostic {
40942255Sbostic 	return(entry->fts_statb.st_gid == plan->g_data);
41040947Sbostic }
41140947Sbostic 
41240947Sbostic PLAN *
41340947Sbostic c_group(gname)
41440947Sbostic 	char *gname;
41540947Sbostic {
41640947Sbostic 	PLAN *new;
41740947Sbostic 	struct group *g;
41840947Sbostic 	gid_t gid;
41945615Sbostic 	void bad_arg();
42040947Sbostic 
42140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
42240947Sbostic 
42340947Sbostic 	g = getgrnam(gname);
42440947Sbostic 	if (g == NULL) {
42540947Sbostic 		gid = atoi(gname);
42640947Sbostic 		if (gid == 0 && gname[0] != '0')
42740947Sbostic 			bad_arg("-group", "no such group");
42840947Sbostic 	} else
42940947Sbostic 		gid = g->gr_gid;
43040947Sbostic 
43140947Sbostic 	NEW(T_GROUP, f_group);
43240947Sbostic 	new->g_data = gid;
43340947Sbostic 	return(new);
43440947Sbostic }
43540947Sbostic 
43640947Sbostic /*
43740947Sbostic  * -inum n functions --
43840947Sbostic  *
43940947Sbostic  *	True if the file has inode # n.
44040947Sbostic  */
44140947Sbostic f_inum(plan, entry)
44240947Sbostic 	PLAN *plan;
44340947Sbostic 	FTSENT *entry;
44440947Sbostic {
44542255Sbostic 	COMPARE(entry->fts_statb.st_ino, plan->i_data);
44640947Sbostic }
44740947Sbostic 
44840947Sbostic PLAN *
44940947Sbostic c_inum(arg)
45040947Sbostic 	char *arg;
45140947Sbostic {
45240947Sbostic 	PLAN *new;
45340947Sbostic 
45440947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
45540947Sbostic 
45640947Sbostic 	NEW(T_INUM, f_inum);
45740947Sbostic 	new->i_data = find_parsenum(new, "-inum", arg, (char *)NULL);
45840947Sbostic 	return(new);
45940947Sbostic }
46040947Sbostic 
46140947Sbostic /*
46240947Sbostic  * -links n functions --
46340947Sbostic  *
46440947Sbostic  *	True if the file has n links.
46540947Sbostic  */
46640947Sbostic f_links(plan, entry)
46740947Sbostic 	PLAN *plan;
46840947Sbostic 	FTSENT *entry;
46940947Sbostic {
47042255Sbostic 	COMPARE(entry->fts_statb.st_nlink, plan->l_data);
47140947Sbostic }
47240947Sbostic 
47340947Sbostic PLAN *
47440947Sbostic c_links(arg)
47540947Sbostic 	char *arg;
47640947Sbostic {
47740947Sbostic 	PLAN *new;
47840947Sbostic 
47940947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
48040947Sbostic 
48140947Sbostic 	NEW(T_LINKS, f_links);
48245616Sbostic 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, (char *)NULL);
48340947Sbostic 	return(new);
48440947Sbostic }
48540947Sbostic 
48640947Sbostic /*
48740947Sbostic  * -ls functions --
48840947Sbostic  *
48940947Sbostic  *	Always true - prints the current entry to stdout in "ls" format.
49040947Sbostic  */
49140947Sbostic /* ARGSUSED */
49240947Sbostic f_ls(plan, entry)
49340947Sbostic 	PLAN *plan;
49440947Sbostic 	FTSENT *entry;
49540947Sbostic {
49645615Sbostic 	void printlong();
49745615Sbostic 
49842255Sbostic 	printlong(entry->fts_path, entry->fts_accpath, &entry->fts_statb);
49942255Sbostic 	return(1);
50040947Sbostic }
50140947Sbostic 
50240947Sbostic PLAN *
50340947Sbostic c_ls()
50440947Sbostic {
50540947Sbostic 	PLAN *new;
50640947Sbostic 
50740947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
50845615Sbostic 	isoutput = 1;
50940947Sbostic 
51040947Sbostic 	NEW(T_LS, f_ls);
51140947Sbostic 	return(new);
51240947Sbostic }
51340947Sbostic 
51440947Sbostic /*
51540947Sbostic  * -name functions --
51640947Sbostic  *
51740947Sbostic  *	True if the basename of the filename being examined
51840947Sbostic  *	matches pattern using Pattern Matching Notation S3.14
51940947Sbostic  */
52040947Sbostic f_name(plan, entry)
52140947Sbostic 	PLAN *plan;
52240947Sbostic 	FTSENT *entry;
52340947Sbostic {
52442255Sbostic 	return(fnmatch(plan->c_data, entry->fts_name, FNM_QUOTE));
52540947Sbostic }
52640947Sbostic 
52740947Sbostic PLAN *
52840947Sbostic c_name(pattern)
52940947Sbostic 	char *pattern;
53040947Sbostic {
53140947Sbostic 	PLAN *new;
53240947Sbostic 
53340947Sbostic 	NEW(T_NAME, f_name);
53440947Sbostic 	new->c_data = pattern;
53540947Sbostic 	return(new);
53640947Sbostic }
53740947Sbostic 
53840947Sbostic /*
53940947Sbostic  * -newer file functions --
54040947Sbostic  *
54140947Sbostic  *	True if the current file has been modified more recently
54240947Sbostic  *	then the modification time of the file named by the pathname
54340947Sbostic  *	file.
54440947Sbostic  */
54540947Sbostic f_newer(plan, entry)
54640947Sbostic 	PLAN *plan;
54740947Sbostic 	FTSENT *entry;
54840947Sbostic {
54942255Sbostic 	return(entry->fts_statb.st_mtime > plan->t_data);
55040947Sbostic }
55140947Sbostic 
55240947Sbostic PLAN *
55340947Sbostic c_newer(filename)
55440947Sbostic 	char *filename;
55540947Sbostic {
55640947Sbostic 	PLAN *new;
55740947Sbostic 	struct stat sb;
55840947Sbostic 
55940947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
56040947Sbostic 
56140947Sbostic 	if (stat(filename, &sb)) {
56240947Sbostic 		(void)fprintf(stderr, "find: %s: %s.\n",
56340947Sbostic 		    filename, strerror(errno));
56440947Sbostic 		exit(1);
56540947Sbostic 	}
56640947Sbostic 	NEW(T_NEWER, f_newer);
56740947Sbostic 	new->t_data = sb.st_mtime;
56840947Sbostic 	return(new);
56940947Sbostic }
57040947Sbostic 
57140947Sbostic /*
57240947Sbostic  * -nogroup functions --
57340947Sbostic  *
57440947Sbostic  *	True if file belongs to a user ID for which the equivalent
57540947Sbostic  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
57640947Sbostic  */
57740947Sbostic /* ARGSUSED */
57840947Sbostic f_nogroup(plan, entry)
57940947Sbostic 	PLAN *plan;
58040947Sbostic 	FTSENT *entry;
58140947Sbostic {
58245615Sbostic 	char *group_from_gid();
58345615Sbostic 
58445615Sbostic 	return(group_from_gid(entry->fts_statb.st_gid, 1) ? 1 : 0);
58540947Sbostic }
58640947Sbostic 
58740947Sbostic PLAN *
58840947Sbostic c_nogroup()
58940947Sbostic {
59040947Sbostic 	PLAN *new;
59140947Sbostic 
59240947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
59340947Sbostic 
59440947Sbostic 	NEW(T_NOGROUP, f_nogroup);
59540947Sbostic 	return(new);
59640947Sbostic }
59740947Sbostic 
59840947Sbostic /*
59940947Sbostic  * -nouser functions --
60040947Sbostic  *
60140947Sbostic  *	True if file belongs to a user ID for which the equivalent
60240947Sbostic  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
60340947Sbostic  */
60440947Sbostic /* ARGSUSED */
60540947Sbostic f_nouser(plan, entry)
60640947Sbostic 	PLAN *plan;
60740947Sbostic 	FTSENT *entry;
60840947Sbostic {
60945615Sbostic 	char *user_from_uid();
61045615Sbostic 
61145615Sbostic 	return(user_from_uid(entry->fts_statb.st_uid, 1) ? 1 : 0);
61240947Sbostic }
61340947Sbostic 
61440947Sbostic PLAN *
61540947Sbostic c_nouser()
61640947Sbostic {
61740947Sbostic 	PLAN *new;
61840947Sbostic 
61940947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
62040947Sbostic 
62140947Sbostic 	NEW(T_NOUSER, f_nouser);
62240947Sbostic 	return(new);
62340947Sbostic }
62440947Sbostic 
62540947Sbostic /*
62640947Sbostic  * -perm functions --
62740947Sbostic  *
62840947Sbostic  *	The mode argument is used to represent file mode bits.  If it starts
62940947Sbostic  *	with a leading digit, it's treated as an octal mode, otherwise as a
63040947Sbostic  *	symbolic mode.
63140947Sbostic  */
63240947Sbostic f_perm(plan, entry)
63340947Sbostic 	PLAN *plan;
63440947Sbostic 	FTSENT *entry;
63540947Sbostic {
63640947Sbostic 	mode_t mode;
63740947Sbostic 
63842255Sbostic 	mode = entry->fts_statb.st_mode &
63940947Sbostic 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
64040947Sbostic 	if (plan->flags)
64140947Sbostic 		return((plan->m_data | mode) == mode);
64240947Sbostic 	else
64340947Sbostic 		return(mode == plan->m_data);
64440947Sbostic 	/* NOTREACHED */
64540947Sbostic }
64640947Sbostic 
64740947Sbostic PLAN *
64840947Sbostic c_perm(perm)
64940947Sbostic 	char *perm;
65040947Sbostic {
65140947Sbostic 	PLAN *new;
65244766Strent 	mode_t *set, *setmode();
65345615Sbostic 	void bad_arg();
65440947Sbostic 
65540947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
65640947Sbostic 
65740947Sbostic 	NEW(T_PERM, f_perm);
65840947Sbostic 
65940947Sbostic 	if (*perm == '-') {
66040947Sbostic 		new->flags = 1;
66140947Sbostic 		++perm;
66240947Sbostic 	}
66340947Sbostic 
66444764Strent 	if ((set = setmode(perm)) == NULL)
66540947Sbostic 		bad_arg("-perm", "illegal mode string");
66640947Sbostic 
66744764Strent 	new->m_data = getmode(set, 0);
66840947Sbostic 	return(new);
66940947Sbostic }
67040947Sbostic 
67140947Sbostic /*
67240947Sbostic  * -print functions --
67340947Sbostic  *
67440947Sbostic  *	Always true, causes the current pathame to be written to
67540947Sbostic  *	standard output.
67640947Sbostic  */
67740947Sbostic /* ARGSUSED */
67840947Sbostic f_print(plan, entry)
67940947Sbostic 	PLAN *plan;
68040947Sbostic 	FTSENT *entry;
68140947Sbostic {
68242255Sbostic 	(void)printf("%s\n", entry->fts_path);
68342255Sbostic 	return(1);
68440947Sbostic }
68540947Sbostic 
68640947Sbostic PLAN *
68740947Sbostic c_print()
68840947Sbostic {
68940947Sbostic 	PLAN *new;
69040947Sbostic 
69145615Sbostic 	isoutput = 1;
69240947Sbostic 
69340947Sbostic 	NEW(T_PRINT, f_print);
69440947Sbostic 	return(new);
69540947Sbostic }
69640947Sbostic 
69740947Sbostic /*
69840947Sbostic  * -prune functions --
69940947Sbostic  *
70040947Sbostic  *	Prune a portion of the hierarchy.
70140947Sbostic  */
70240947Sbostic /* ARGSUSED */
70340947Sbostic f_prune(plan, entry)
70440947Sbostic 	PLAN *plan;
70540947Sbostic 	FTSENT *entry;
70640947Sbostic {
70740947Sbostic 	extern FTS *tree;
70840947Sbostic 
70945615Sbostic 	if (fts_set(tree, entry, FTS_SKIP)) {
71040947Sbostic 		(void)fprintf(stderr,
71142255Sbostic 		    "find: %s: %s.\n", entry->fts_path, strerror(errno));
71240947Sbostic 		exit(1);
71340947Sbostic 	}
71442255Sbostic 	return(1);
71540947Sbostic }
71640947Sbostic 
71740947Sbostic PLAN *
71840947Sbostic c_prune()
71940947Sbostic {
72040947Sbostic 	PLAN *new;
72140947Sbostic 
72240947Sbostic 	NEW(T_PRUNE, f_prune);
72340947Sbostic 	return(new);
72440947Sbostic }
72540947Sbostic 
72640947Sbostic /*
72740947Sbostic  * -size n[c] functions --
72840947Sbostic  *
72940947Sbostic  *	True if the file size in bytes, divided by an implementation defined
73040947Sbostic  *	value and rounded up to the next integer, is n.  If n is followed by
73140947Sbostic  *	a c, the size is in bytes.
73240947Sbostic  */
73340947Sbostic #define	FIND_SIZE	512
73440947Sbostic static int divsize = 1;
73540947Sbostic 
73640947Sbostic f_size(plan, entry)
73740947Sbostic 	PLAN *plan;
73840947Sbostic 	FTSENT *entry;
73940947Sbostic {
74040947Sbostic 	off_t size;
74140947Sbostic 
74242255Sbostic 	size = divsize ? (entry->fts_statb.st_size + FIND_SIZE - 1) /
74342255Sbostic 	    FIND_SIZE : entry->fts_statb.st_size;
74440947Sbostic 	COMPARE(size, plan->o_data);
74540947Sbostic }
74640947Sbostic 
74740947Sbostic PLAN *
74840947Sbostic c_size(arg)
74940947Sbostic 	char *arg;
75040947Sbostic {
75140947Sbostic 	PLAN *new;
75240947Sbostic 	char endch;
75340947Sbostic 
75440947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
75540947Sbostic 
75640947Sbostic 	NEW(T_SIZE, f_size);
75740947Sbostic 	new->o_data = find_parsenum(new, "-size", arg, &endch);
75840947Sbostic 	if (endch == 'c')
75940947Sbostic 		divsize = 0;
76040947Sbostic 	return(new);
76140947Sbostic }
76240947Sbostic 
76340947Sbostic /*
76440947Sbostic  * -type c functions --
76540947Sbostic  *
76640947Sbostic  *	True if the type of the file is c, where c is b, c, d, p, or f for
76740947Sbostic  *	block special file, character special file, directory, FIFO, or
76840947Sbostic  *	regular file, respectively.
76940947Sbostic  */
77040947Sbostic f_type(plan, entry)
77140947Sbostic 	PLAN *plan;
77240947Sbostic 	FTSENT *entry;
77340947Sbostic {
77444764Strent 	return((entry->fts_statb.st_mode & S_IFMT) == plan->m_data);
77540947Sbostic }
77640947Sbostic 
77740947Sbostic PLAN *
77840947Sbostic c_type(typestring)
77940947Sbostic 	char *typestring;
78040947Sbostic {
78140947Sbostic 	PLAN *new;
78240947Sbostic 	mode_t  mask;
78345615Sbostic 	void bad_arg();
78440947Sbostic 
78540947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
78640947Sbostic 
78740947Sbostic 	switch (typestring[0]) {
78840947Sbostic 	case 'b':
78940947Sbostic 		mask = S_IFBLK;
79040947Sbostic 		break;
79140947Sbostic 	case 'c':
79240947Sbostic 		mask = S_IFCHR;
79340947Sbostic 		break;
79440947Sbostic 	case 'd':
79540947Sbostic 		mask = S_IFDIR;
79640947Sbostic 		break;
79740947Sbostic 	case 'f':
79840947Sbostic 		mask = S_IFREG;
79940947Sbostic 		break;
80040947Sbostic 	case 'l':
80140947Sbostic 		mask = S_IFLNK;
80240947Sbostic 		break;
80340947Sbostic 	case 'p':
80440947Sbostic 		mask = S_IFIFO;
80540947Sbostic 		break;
80640947Sbostic 	case 's':
80740947Sbostic 		mask = S_IFSOCK;
80840947Sbostic 		break;
80940947Sbostic 	default:
81040947Sbostic 		bad_arg("-type", "unknown type");
81140947Sbostic 	}
81240947Sbostic 
81340947Sbostic 	NEW(T_TYPE, f_type);
81440947Sbostic 	new->m_data = mask;
81540947Sbostic 	return(new);
81640947Sbostic }
81740947Sbostic 
81840947Sbostic /*
81940947Sbostic  * -user uname functions --
82040947Sbostic  *
82140947Sbostic  *	True if the file belongs to the user uname.  If uname is numeric and
82240947Sbostic  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
82340947Sbostic  *	return a valid user name, uname is taken as a user ID.
82440947Sbostic  */
82540947Sbostic f_user(plan, entry)
82640947Sbostic 	PLAN *plan;
82740947Sbostic 	FTSENT *entry;
82840947Sbostic {
82942255Sbostic 	return(entry->fts_statb.st_uid == plan->u_data);
83040947Sbostic }
83140947Sbostic 
83240947Sbostic PLAN *
83340947Sbostic c_user(username)
83440947Sbostic 	char *username;
83540947Sbostic {
83640947Sbostic 	PLAN *new;
83740947Sbostic 	struct passwd *p;
83840947Sbostic 	uid_t uid;
83945615Sbostic 	void bad_arg();
84040947Sbostic 
84140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
84240947Sbostic 
84340947Sbostic 	p = getpwnam(username);
84440947Sbostic 	if (p == NULL) {
84540947Sbostic 		uid = atoi(username);
84640947Sbostic 		if (uid == 0 && username[0] != '0')
84740947Sbostic 			bad_arg("-user", "no such user");
84840947Sbostic 	} else
84940947Sbostic 		uid = p->pw_uid;
85040947Sbostic 
85140947Sbostic 	NEW(T_USER, f_user);
85240947Sbostic 	new->u_data = uid;
85340947Sbostic 	return(new);
85440947Sbostic }
85540947Sbostic 
85640947Sbostic /*
85740947Sbostic  * -xdev functions --
85840947Sbostic  *
85940947Sbostic  *	Always true, causes find not to decend past directories that have a
86040947Sbostic  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
86140947Sbostic  */
86240947Sbostic PLAN *
86340947Sbostic c_xdev()
86440947Sbostic {
86540947Sbostic 	PLAN *new;
86640947Sbostic 
86742275Sbostic 	ftsoptions |= FTS_XDEV;
86840947Sbostic 
86940947Sbostic 	NEW(T_XDEV, f_always_true);
87040947Sbostic 	return(new);
87140947Sbostic }
87240947Sbostic 
87340947Sbostic /*
87440947Sbostic  * ( expression ) functions --
87540947Sbostic  *
87640947Sbostic  *	True if expression is true.
87740947Sbostic  */
87840947Sbostic f_expr(plan, entry)
87940947Sbostic 	PLAN *plan;
88040947Sbostic 	FTSENT *entry;
88140947Sbostic {
88240947Sbostic 	register PLAN *p;
88340947Sbostic 	register int state;
88440947Sbostic 
88540947Sbostic 	for (p = plan->p_data[0];
88640947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
88740947Sbostic 	return(state);
88840947Sbostic }
88940947Sbostic 
89040947Sbostic /*
89140947Sbostic  * T_OPENPAREN and T_CLOSEPAREN nodes are temporary place markers.  They are
89240947Sbostic  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
89340947Sbostic  * to a T_EXPR node containing the expression and the ')' node is discarded.
89440947Sbostic  */
89540947Sbostic PLAN *
89640947Sbostic c_openparen()
89740947Sbostic {
89840947Sbostic 	PLAN *new;
89940947Sbostic 
90040947Sbostic 	NEW(T_OPENPAREN, (int (*)())-1);
90140947Sbostic 	return(new);
90240947Sbostic }
90340947Sbostic 
90440947Sbostic PLAN *
90540947Sbostic c_closeparen()
90640947Sbostic {
90740947Sbostic 	PLAN *new;
90840947Sbostic 
90940947Sbostic 	NEW(T_CLOSEPAREN, (int (*)())-1);
91040947Sbostic 	return(new);
91140947Sbostic }
91240947Sbostic 
91340947Sbostic /*
91440947Sbostic  * -mtime n functions --
91540947Sbostic  *
91640947Sbostic  *	True if the difference between the file modification time and the
91740947Sbostic  *	current time is n 24 hour periods.
91840947Sbostic  */
91940947Sbostic f_mtime(plan, entry)
92040947Sbostic 	PLAN *plan;
92140947Sbostic 	FTSENT *entry;
92240947Sbostic {
92340947Sbostic 	extern time_t now;
92440947Sbostic 
92542255Sbostic 	COMPARE((now - entry->fts_statb.st_mtime + SECSPERDAY - 1) /
92642255Sbostic 	    SECSPERDAY, plan->t_data);
92740947Sbostic }
92840947Sbostic 
92940947Sbostic PLAN *
93040947Sbostic c_mtime(arg)
93140947Sbostic 	char *arg;
93240947Sbostic {
93340947Sbostic 	PLAN *new;
93440947Sbostic 
93540947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
93640947Sbostic 
93740947Sbostic 	NEW(T_MTIME, f_mtime);
93840947Sbostic 	new->t_data = find_parsenum(new, "-mtime", arg, (char *)NULL);
93940947Sbostic 	return(new);
94040947Sbostic }
94140947Sbostic 
94240947Sbostic /*
94340947Sbostic  * ! expression functions --
94440947Sbostic  *
94540947Sbostic  *	Negation of a primary; the unary NOT operator.
94640947Sbostic  */
94740947Sbostic f_not(plan, entry)
94840947Sbostic 	PLAN *plan;
94940947Sbostic 	FTSENT *entry;
95040947Sbostic {
95140947Sbostic 	register PLAN *p;
95240947Sbostic 	register int state;
95340947Sbostic 
95440947Sbostic 	for (p = plan->p_data[0];
95540947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
95640947Sbostic 	return(!state);
95740947Sbostic }
95840947Sbostic 
95940947Sbostic PLAN *
96040947Sbostic c_not()
96140947Sbostic {
96240947Sbostic 	PLAN *new;
96340947Sbostic 
96440947Sbostic 	NEW(T_NOT, f_not);
96540947Sbostic 	return(new);
96640947Sbostic }
96740947Sbostic 
96840947Sbostic /*
96940947Sbostic  * expression -o expression functions --
97040947Sbostic  *
97140947Sbostic  *	Alternation of primaries; the OR operator.  The second expression is
97240947Sbostic  * not evaluated if the first expression is true.
97340947Sbostic  */
97440947Sbostic f_or(plan, entry)
97540947Sbostic 	PLAN *plan;
97640947Sbostic 	FTSENT *entry;
97740947Sbostic {
97840947Sbostic 	register PLAN *p;
97940947Sbostic 	register int state;
98040947Sbostic 
98140947Sbostic 	for (p = plan->p_data[0];
98240947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
98340947Sbostic 
98440947Sbostic 	if (state)
98542255Sbostic 		return(1);
98640947Sbostic 
98740947Sbostic 	for (p = plan->p_data[1];
98840947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
98940947Sbostic 	return(state);
99040947Sbostic }
99140947Sbostic 
99240947Sbostic PLAN *
99340947Sbostic c_or()
99440947Sbostic {
99540947Sbostic 	PLAN *new;
99640947Sbostic 
99740947Sbostic 	NEW(T_OR, f_or);
99840947Sbostic 	return(new);
99940947Sbostic }
1000