xref: /csrg-svn/usr.bin/find/function.c (revision 42047)
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*42047Sbostic static char sccsid[] = "@(#)function.c	5.3 (Berkeley) 05/15/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>
1940947Sbostic #include <grp.h>
2040947Sbostic #include <pwd.h>
2140947Sbostic #include <fts.h>
2240947Sbostic #include <unistd.h>
2340947Sbostic #include <tzfile.h>
2440947Sbostic #include <stdio.h>
25*42047Sbostic #include <string.h>
2640947Sbostic #include "find.h"
2740947Sbostic 
2840947Sbostic #define	FIND_EQUAL	0
2940947Sbostic #define	FIND_LESSTHAN	1
3040947Sbostic #define	FIND_GREATER	2
3140947Sbostic 
3240947Sbostic #define	FIND_FALSE	0
3340947Sbostic #define	FIND_TRUE	1
3440947Sbostic 
3540947Sbostic #define	COMPARE(a, b) { \
3640947Sbostic 	switch(plan->flags) { \
3740947Sbostic 	case FIND_EQUAL: \
3840947Sbostic 		return(a == b); \
3940947Sbostic 	case FIND_LESSTHAN: \
4040947Sbostic 		return(a < b); \
4140947Sbostic 	case FIND_GREATER: \
4240947Sbostic 		return(a > b); \
4340947Sbostic 	} \
4440947Sbostic 	return(FIND_FALSE); \
4540947Sbostic }
4640947Sbostic 
4740947Sbostic #define NEW(t, f) { \
4840947Sbostic 	new = (PLAN *)emalloc(sizeof(PLAN)); \
4940947Sbostic 	new->type = t; \
5040947Sbostic 	new->eval = f; \
5140947Sbostic 	new->flags = 0; \
5240947Sbostic 	new->next = NULL; \
5340947Sbostic }
5440947Sbostic 
5540947Sbostic /*
5640947Sbostic  * find_parsenum --
5740947Sbostic  *	Parse a string of the form [+-]# and return the value.
5840947Sbostic  */
5940947Sbostic long
6040947Sbostic find_parsenum(plan, option, str, endch)
6140947Sbostic 	PLAN *plan;
6240947Sbostic 	char *option, *str, *endch;
6340947Sbostic {
6440947Sbostic 	long value;
6540947Sbostic 	char *endchar;		/* pointer to character ending conversion */
6640947Sbostic 
6740947Sbostic 	/* determine comparison from leading + or - */
6840947Sbostic 	switch(*str) {
6940947Sbostic 	case '+':
7040947Sbostic 		++str;
7140947Sbostic 		plan->flags = FIND_GREATER;
7240947Sbostic 		break;
7340947Sbostic 	case '-':
7440947Sbostic 		++str;
7540947Sbostic 		plan->flags = FIND_LESSTHAN;
7640947Sbostic 		break;
7740947Sbostic 	default:
7840947Sbostic 		plan->flags = FIND_EQUAL;
7940947Sbostic 		break;
8040947Sbostic 	}
8140947Sbostic 
8240947Sbostic 	/*
8340947Sbostic 	 * convert the string with strtol().  Note, if strtol() returns zero
8440947Sbostic 	 * and endchar points to the beginning of the string we know we have
8540947Sbostic 	 * a syntax error.
8640947Sbostic 	 */
8740947Sbostic 	value = strtol(str, &endchar, 10);
8840947Sbostic 	if (!value && endchar == str ||
8940947Sbostic 	    endchar[0] && (!endch || endchar[0] != *endch))
9040947Sbostic 		bad_arg(option, "illegal numeric value");
9140947Sbostic 	if (endch)
9240947Sbostic 		*endch = endchar[0];
9340947Sbostic 	return(value);
9440947Sbostic }
9540947Sbostic 
9640947Sbostic /*
9740947Sbostic  * -atime n functions --
9840947Sbostic  *
9940947Sbostic  *	True if the difference between the file access time and the
10040947Sbostic  *	current time is n 24 hour periods.
10140947Sbostic  *
10240947Sbostic  */
10340947Sbostic f_atime(plan, entry)
10440947Sbostic 	PLAN *plan;
10540947Sbostic 	FTSENT *entry;
10640947Sbostic {
10740947Sbostic 	extern time_t now;
10840947Sbostic 
10940947Sbostic 	COMPARE((now - entry->statb.st_atime + SECSPERDAY - 1) / SECSPERDAY,
11040947Sbostic 	    plan->t_data);
11140947Sbostic }
11240947Sbostic 
11340947Sbostic PLAN *
11440947Sbostic c_atime(arg)
11540947Sbostic 	char *arg;
11640947Sbostic {
11740947Sbostic 	PLAN *new;
11840947Sbostic 
11940947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
12040947Sbostic 
12140947Sbostic 	NEW(T_ATIME, f_atime);
12240947Sbostic 	new->t_data = find_parsenum(new, "-atime", arg, (char *)NULL);
12340947Sbostic 	return(new);
12440947Sbostic }
12540947Sbostic /*
12640947Sbostic  * -ctime n functions --
12740947Sbostic  *
12840947Sbostic  *	True if the difference between the last change of file
12940947Sbostic  *	status information and the current time is n 24 hour periods.
13040947Sbostic  */
13140947Sbostic f_ctime(plan, entry)
13240947Sbostic 	PLAN *plan;
13340947Sbostic 	FTSENT *entry;
13440947Sbostic {
13540947Sbostic 	extern time_t now;
13640947Sbostic 
13740947Sbostic 	COMPARE((now - entry->statb.st_ctime + SECSPERDAY - 1) / SECSPERDAY,
13840947Sbostic 	    plan->t_data);
13940947Sbostic }
14040947Sbostic 
14140947Sbostic PLAN *
14240947Sbostic c_ctime(arg)
14340947Sbostic 	char *arg;
14440947Sbostic {
14540947Sbostic 	PLAN *new;
14640947Sbostic 
14740947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
14840947Sbostic 
14940947Sbostic 	NEW(T_CTIME, f_ctime);
15040947Sbostic 	new->t_data = find_parsenum(new, "-ctime", arg, (char *)NULL);
15140947Sbostic 	return(new);
15240947Sbostic }
15340947Sbostic 
15440947Sbostic /*
15540947Sbostic  * -depth functions --
15640947Sbostic  *
15740947Sbostic  *	Always true, causes descent of the directory hierarchy to be done
15840947Sbostic  *	so that all entries in a directory are acted on before the directory
15940947Sbostic  *	itself.
16040947Sbostic  */
16140947Sbostic /* ARGSUSED */
16240947Sbostic f_always_true(plan, entry)
16340947Sbostic 	PLAN *plan;
16440947Sbostic 	FTSENT *entry;
16540947Sbostic {
16640947Sbostic 	return(FIND_TRUE);
16740947Sbostic }
16840947Sbostic 
16940947Sbostic PLAN *
17040947Sbostic c_depth()
17140947Sbostic {
17240947Sbostic 	extern int depth;
17340947Sbostic 	PLAN *new;
17440947Sbostic 
17540947Sbostic 	depth = 1;
17640947Sbostic 
17740947Sbostic 	NEW(T_DEPTH, f_always_true);
17840947Sbostic 	return(new);
17940947Sbostic }
18040947Sbostic 
18140947Sbostic /*
18240947Sbostic  * [-exec | -ok] utility [arg ... ] ; functions --
18340947Sbostic  *
18440947Sbostic  *	True if the executed utility returns a zero value as exit status.
18540947Sbostic  *	The end of the primary expression is delimited by a semicolon.  If
18640947Sbostic  *	"{}" occurs anywhere, it gets replaced by the current pathname.
18740947Sbostic  *	The current directory for the execution of utility is the same as
18840947Sbostic  *	the current directory when the find utility was started.
18940947Sbostic  *
19040947Sbostic  *	The primary -ok is different in that it requests affirmation of the
19140947Sbostic  *	user before executing the utility.
19240947Sbostic  */
19340947Sbostic f_exec(plan, entry)
19440947Sbostic 	register PLAN *plan;
19540947Sbostic 	FTSENT *entry;
19640947Sbostic {
19740947Sbostic 	register int cnt;
19840947Sbostic 	char *find_subst();
19940947Sbostic 	union wait pstat;
20040947Sbostic 	pid_t pid, waitpid();
20140947Sbostic 
20240947Sbostic 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
20340947Sbostic 		if (plan->e_len[cnt])
20440947Sbostic 			find_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
20540947Sbostic 			    entry->path, plan->e_len[cnt]);
20640947Sbostic 
20740947Sbostic 	if (plan->flags && !find_queryuser(plan->e_argv))
20840947Sbostic 		return(FIND_FALSE);
20940947Sbostic 
21040947Sbostic 	switch(pid = vfork()) {
21140947Sbostic 	case -1:
21240947Sbostic 		(void)fprintf(stderr, "find: fork: %s.\n", strerror(errno));
21340947Sbostic 		exit(1);
21440947Sbostic 		/* NOTREACHED */
21540947Sbostic 	case 0:
21640947Sbostic 		execvp(plan->e_argv[0], plan->e_argv);
21740947Sbostic 		(void)fprintf(stderr,
21840947Sbostic 		    "find: %s: %s.\n", plan->e_argv[0], strerror(errno));
21940947Sbostic 		exit(1);
22040947Sbostic 		/* NOTREACHED */
22140947Sbostic 	}
22240947Sbostic 	pid = waitpid(pid, &pstat, 0);
22340947Sbostic 	return(pid == -1 || pstat.w_status ? FIND_FALSE : FIND_TRUE);
22440947Sbostic }
22540947Sbostic 
22640947Sbostic /*
22740947Sbostic  * c_exec --
22840947Sbostic  *	build three parallel arrays, one with pointers to the strings passed
22940947Sbostic  *	on the command line, one with (possibly duplicated) pointers to the
23040947Sbostic  *	argv array, and one with integer values that are lengths of the
23140947Sbostic  *	strings, but also flags meaning that the string has to be massaged.
23240947Sbostic  */
23340947Sbostic PLAN *
23440947Sbostic c_exec(argvp, isok)
23540947Sbostic 	char ***argvp;
23640947Sbostic 	int isok;
23740947Sbostic {
23840947Sbostic 	PLAN *new;			/* node returned */
23940947Sbostic 	register int cnt;
24040947Sbostic 	register char **argv, **ap, *p;
24140947Sbostic 
24240947Sbostic 	ftsoptions |= FTS_NOCHDIR;
24340947Sbostic 	output_specified = 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 {
30640947Sbostic 	extern dev_t curdev;
30740947Sbostic 	struct statfs sb;
30840947Sbostic 	static short curtype;
30940947Sbostic 
31040947Sbostic 	/* only check when we cross mount point */
31140947Sbostic 	if (curdev != entry->statb.st_dev) {
31240947Sbostic 		if (statfs(entry->name, &sb)) {
31340947Sbostic 			(void)fprintf(stderr, "find: %s: %s.\n",
31440947Sbostic 			    entry->name, strerror(errno));
31540947Sbostic 			exit(1);
31640947Sbostic 		}
31740947Sbostic 		curtype = sb.f_type;
31840947Sbostic 	}
31940947Sbostic 	return(plan->flags == curtype);
32040947Sbostic }
32140947Sbostic 
32240947Sbostic PLAN *
32340947Sbostic c_fstype(arg)
32440947Sbostic 	char *arg;
32540947Sbostic {
32640947Sbostic 	register PLAN *new;
32740947Sbostic 
32840947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
32940947Sbostic 
33040947Sbostic 	NEW(T_FSTYPE, f_fstype);
33140947Sbostic 	switch(*arg) {
33240947Sbostic 	case 'm':
33340947Sbostic 		if (!strcmp(arg, "mfs")) {
33440947Sbostic 			new->flags = MOUNT_MFS;
33540947Sbostic 			return(new);
33640947Sbostic 		}
33740947Sbostic 		break;
33840947Sbostic 	case 'n':
33940947Sbostic 		if (!strcmp(arg, "nfs")) {
34040947Sbostic 			new->flags = MOUNT_NFS;
34140947Sbostic 			return(new);
34240947Sbostic 		}
34340947Sbostic 		break;
34440947Sbostic 	case 'p':
34540947Sbostic 		if (!strcmp(arg, "pc")) {
34640947Sbostic 			new->flags = MOUNT_PC;
34740947Sbostic 			return(new);
34840947Sbostic 		}
34940947Sbostic 		break;
35040947Sbostic 	case 'u':
35140947Sbostic 		if (!strcmp(arg, "ufs")) {
35240947Sbostic 			new->flags = MOUNT_UFS;
35340947Sbostic 			return(new);
35440947Sbostic 		}
35540947Sbostic 		break;
35640947Sbostic 	}
35740947Sbostic 	(void)fprintf(stderr, "find: unknown file type %s.\n", arg);
35840947Sbostic 	exit(1);
35940947Sbostic 	/* NOTREACHED */
36040947Sbostic }
36140947Sbostic 
36240947Sbostic /*
36340947Sbostic  * -group gname functions --
36440947Sbostic  *
36540947Sbostic  *	True if the file belongs to the group gname.  If gname is numeric and
36640947Sbostic  *	an equivalent of the getgrnam() function does not return a valid group
36740947Sbostic  *	name, gname is taken as a group ID.
36840947Sbostic  */
36940947Sbostic f_group(plan, entry)
37040947Sbostic 	PLAN *plan;
37140947Sbostic 	FTSENT *entry;
37240947Sbostic {
37340947Sbostic 	return(entry->statb.st_gid == plan->g_data ? FIND_TRUE : FIND_FALSE);
37440947Sbostic }
37540947Sbostic 
37640947Sbostic PLAN *
37740947Sbostic c_group(gname)
37840947Sbostic 	char *gname;
37940947Sbostic {
38040947Sbostic 	PLAN *new;
38140947Sbostic 	struct group *g;
38240947Sbostic 	gid_t gid;
38340947Sbostic 
38440947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
38540947Sbostic 
38640947Sbostic 	g = getgrnam(gname);
38740947Sbostic 	if (g == NULL) {
38840947Sbostic 		gid = atoi(gname);
38940947Sbostic 		if (gid == 0 && gname[0] != '0')
39040947Sbostic 			bad_arg("-group", "no such group");
39140947Sbostic 	} else
39240947Sbostic 		gid = g->gr_gid;
39340947Sbostic 
39440947Sbostic 	NEW(T_GROUP, f_group);
39540947Sbostic 	new->g_data = gid;
39640947Sbostic 	return(new);
39740947Sbostic }
39840947Sbostic 
39940947Sbostic /*
40040947Sbostic  * -inum n functions --
40140947Sbostic  *
40240947Sbostic  *	True if the file has inode # n.
40340947Sbostic  */
40440947Sbostic f_inum(plan, entry)
40540947Sbostic 	PLAN *plan;
40640947Sbostic 	FTSENT *entry;
40740947Sbostic {
40840947Sbostic 	COMPARE(entry->statb.st_ino, plan->i_data);
40940947Sbostic }
41040947Sbostic 
41140947Sbostic PLAN *
41240947Sbostic c_inum(arg)
41340947Sbostic 	char *arg;
41440947Sbostic {
41540947Sbostic 	PLAN *new;
41640947Sbostic 
41740947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
41840947Sbostic 
41940947Sbostic 	NEW(T_INUM, f_inum);
42040947Sbostic 	new->i_data = find_parsenum(new, "-inum", arg, (char *)NULL);
42140947Sbostic 	return(new);
42240947Sbostic }
42340947Sbostic 
42440947Sbostic /*
42540947Sbostic  * -links n functions --
42640947Sbostic  *
42740947Sbostic  *	True if the file has n links.
42840947Sbostic  */
42940947Sbostic f_links(plan, entry)
43040947Sbostic 	PLAN *plan;
43140947Sbostic 	FTSENT *entry;
43240947Sbostic {
43340947Sbostic 	COMPARE(entry->statb.st_nlink, plan->l_data);
43440947Sbostic }
43540947Sbostic 
43640947Sbostic PLAN *
43740947Sbostic c_links(arg)
43840947Sbostic 	char *arg;
43940947Sbostic {
44040947Sbostic 	PLAN *new;
44140947Sbostic 
44240947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
44340947Sbostic 
44440947Sbostic 	NEW(T_LINKS, f_links);
44540947Sbostic 	new->l_data = find_parsenum(new, "-links", arg, (char *)NULL);
44640947Sbostic 	return(new);
44740947Sbostic }
44840947Sbostic 
44940947Sbostic /*
45040947Sbostic  * -ls functions --
45140947Sbostic  *
45240947Sbostic  *	Always true - prints the current entry to stdout in "ls" format.
45340947Sbostic  */
45440947Sbostic /* ARGSUSED */
45540947Sbostic f_ls(plan, entry)
45640947Sbostic 	PLAN *plan;
45740947Sbostic 	FTSENT *entry;
45840947Sbostic {
45940947Sbostic 	printlong(entry->path, entry->accpath, &entry->statb);
46040947Sbostic 	return(FIND_TRUE);
46140947Sbostic }
46240947Sbostic 
46340947Sbostic PLAN *
46440947Sbostic c_ls()
46540947Sbostic {
46640947Sbostic 	PLAN *new;
46740947Sbostic 
46840947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
46940947Sbostic 	output_specified = 1;
47040947Sbostic 
47140947Sbostic 	NEW(T_LS, f_ls);
47240947Sbostic 	return(new);
47340947Sbostic }
47440947Sbostic 
47540947Sbostic /*
47640947Sbostic  * -name functions --
47740947Sbostic  *
47840947Sbostic  *	True if the basename of the filename being examined
47940947Sbostic  *	matches pattern using Pattern Matching Notation S3.14
48040947Sbostic  */
48140947Sbostic f_name(plan, entry)
48240947Sbostic 	PLAN *plan;
48340947Sbostic 	FTSENT *entry;
48440947Sbostic {
48540947Sbostic 	return(fnmatch(plan->c_data, entry->name, FNM_QUOTE) ?
48640947Sbostic 	    FIND_TRUE : FIND_FALSE);
48740947Sbostic }
48840947Sbostic 
48940947Sbostic PLAN *
49040947Sbostic c_name(pattern)
49140947Sbostic 	char *pattern;
49240947Sbostic {
49340947Sbostic 	PLAN *new;
49440947Sbostic 
49540947Sbostic 	NEW(T_NAME, f_name);
49640947Sbostic 	new->c_data = pattern;
49740947Sbostic 	return(new);
49840947Sbostic }
49940947Sbostic 
50040947Sbostic /*
50140947Sbostic  * -newer file functions --
50240947Sbostic  *
50340947Sbostic  *	True if the current file has been modified more recently
50440947Sbostic  *	then the modification time of the file named by the pathname
50540947Sbostic  *	file.
50640947Sbostic  */
50740947Sbostic f_newer(plan, entry)
50840947Sbostic 	PLAN *plan;
50940947Sbostic 	FTSENT *entry;
51040947Sbostic {
51140947Sbostic 	return(entry->statb.st_mtime > plan->t_data ? FIND_TRUE : FIND_FALSE);
51240947Sbostic }
51340947Sbostic 
51440947Sbostic PLAN *
51540947Sbostic c_newer(filename)
51640947Sbostic 	char *filename;
51740947Sbostic {
51840947Sbostic 	PLAN *new;
51940947Sbostic 	struct stat sb;
52040947Sbostic 
52140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
52240947Sbostic 
52340947Sbostic 	if (stat(filename, &sb)) {
52440947Sbostic 		(void)fprintf(stderr, "find: %s: %s.\n",
52540947Sbostic 		    filename, strerror(errno));
52640947Sbostic 		exit(1);
52740947Sbostic 	}
52840947Sbostic 	NEW(T_NEWER, f_newer);
52940947Sbostic 	new->t_data = sb.st_mtime;
53040947Sbostic 	return(new);
53140947Sbostic }
53240947Sbostic 
53340947Sbostic /*
53440947Sbostic  * -nogroup functions --
53540947Sbostic  *
53640947Sbostic  *	True if file belongs to a user ID for which the equivalent
53740947Sbostic  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
53840947Sbostic  */
53940947Sbostic /* ARGSUSED */
54040947Sbostic f_nogroup(plan, entry)
54140947Sbostic 	PLAN *plan;
54240947Sbostic 	FTSENT *entry;
54340947Sbostic {
54440947Sbostic 	return(group_from_gid(entry->statb.st_gid, 1) ? FIND_FALSE : FIND_TRUE);
54540947Sbostic }
54640947Sbostic 
54740947Sbostic PLAN *
54840947Sbostic c_nogroup()
54940947Sbostic {
55040947Sbostic 	PLAN *new;
55140947Sbostic 
55240947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
55340947Sbostic 
55440947Sbostic 	NEW(T_NOGROUP, f_nogroup);
55540947Sbostic 	return(new);
55640947Sbostic }
55740947Sbostic 
55840947Sbostic /*
55940947Sbostic  * -nouser functions --
56040947Sbostic  *
56140947Sbostic  *	True if file belongs to a user ID for which the equivalent
56240947Sbostic  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
56340947Sbostic  */
56440947Sbostic /* ARGSUSED */
56540947Sbostic f_nouser(plan, entry)
56640947Sbostic 	PLAN *plan;
56740947Sbostic 	FTSENT *entry;
56840947Sbostic {
56940947Sbostic 	return(user_from_uid(entry->statb.st_uid, 1) ? FIND_FALSE : FIND_TRUE);
57040947Sbostic }
57140947Sbostic 
57240947Sbostic PLAN *
57340947Sbostic c_nouser()
57440947Sbostic {
57540947Sbostic 	PLAN *new;
57640947Sbostic 
57740947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
57840947Sbostic 
57940947Sbostic 	NEW(T_NOUSER, f_nouser);
58040947Sbostic 	return(new);
58140947Sbostic }
58240947Sbostic 
58340947Sbostic /*
58440947Sbostic  * -perm functions --
58540947Sbostic  *
58640947Sbostic  *	The mode argument is used to represent file mode bits.  If it starts
58740947Sbostic  *	with a leading digit, it's treated as an octal mode, otherwise as a
58840947Sbostic  *	symbolic mode.
58940947Sbostic  */
59040947Sbostic f_perm(plan, entry)
59140947Sbostic 	PLAN *plan;
59240947Sbostic 	FTSENT *entry;
59340947Sbostic {
59440947Sbostic 	mode_t mode;
59540947Sbostic 
59640947Sbostic 	mode = entry->statb.st_mode &
59740947Sbostic 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
59840947Sbostic 	if (plan->flags)
59940947Sbostic 		return((plan->m_data | mode) == mode);
60040947Sbostic 	else
60140947Sbostic 		return(mode == plan->m_data);
60240947Sbostic 	/* NOTREACHED */
60340947Sbostic }
60440947Sbostic 
60540947Sbostic PLAN *
60640947Sbostic c_perm(perm)
60740947Sbostic 	char *perm;
60840947Sbostic {
60940947Sbostic 	PLAN *new;
61040947Sbostic 
61140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
61240947Sbostic 
61340947Sbostic 	NEW(T_PERM, f_perm);
61440947Sbostic 
61540947Sbostic 	if (*perm == '-') {
61640947Sbostic 		new->flags = 1;
61740947Sbostic 		++perm;
61840947Sbostic 	}
61940947Sbostic 
62040947Sbostic 	if (setmode(perm))
62140947Sbostic 		bad_arg("-perm", "illegal mode string");
62240947Sbostic 
62340947Sbostic 	new->m_data = getmode(0);
62440947Sbostic 	return(new);
62540947Sbostic }
62640947Sbostic 
62740947Sbostic /*
62840947Sbostic  * -print functions --
62940947Sbostic  *
63040947Sbostic  *	Always true, causes the current pathame to be written to
63140947Sbostic  *	standard output.
63240947Sbostic  */
63340947Sbostic /* ARGSUSED */
63440947Sbostic f_print(plan, entry)
63540947Sbostic 	PLAN *plan;
63640947Sbostic 	FTSENT *entry;
63740947Sbostic {
63840947Sbostic 	(void)printf("%s\n", entry->path);
63940947Sbostic 	return(FIND_TRUE);
64040947Sbostic }
64140947Sbostic 
64240947Sbostic PLAN *
64340947Sbostic c_print()
64440947Sbostic {
64540947Sbostic 	PLAN *new;
64640947Sbostic 
64740947Sbostic 	output_specified = 1;
64840947Sbostic 
64940947Sbostic 	NEW(T_PRINT, f_print);
65040947Sbostic 	return(new);
65140947Sbostic }
65240947Sbostic 
65340947Sbostic /*
65440947Sbostic  * -prune functions --
65540947Sbostic  *
65640947Sbostic  *	Prune a portion of the hierarchy.
65740947Sbostic  */
65840947Sbostic /* ARGSUSED */
65940947Sbostic f_prune(plan, entry)
66040947Sbostic 	PLAN *plan;
66140947Sbostic 	FTSENT *entry;
66240947Sbostic {
66340947Sbostic 	extern FTS *tree;
66440947Sbostic 
66540947Sbostic 	if (ftsset(tree, entry, FTS_SKIP)) {
66640947Sbostic 		(void)fprintf(stderr,
66740947Sbostic 		    "find: %s: %s.\n", entry->path, strerror(errno));
66840947Sbostic 		exit(1);
66940947Sbostic 	}
67040947Sbostic 	return(FIND_TRUE);
67140947Sbostic }
67240947Sbostic 
67340947Sbostic PLAN *
67440947Sbostic c_prune()
67540947Sbostic {
67640947Sbostic 	PLAN *new;
67740947Sbostic 
67840947Sbostic 	NEW(T_PRUNE, f_prune);
67940947Sbostic 	return(new);
68040947Sbostic }
68140947Sbostic 
68240947Sbostic /*
68340947Sbostic  * -size n[c] functions --
68440947Sbostic  *
68540947Sbostic  *	True if the file size in bytes, divided by an implementation defined
68640947Sbostic  *	value and rounded up to the next integer, is n.  If n is followed by
68740947Sbostic  *	a c, the size is in bytes.
68840947Sbostic  */
68940947Sbostic #define	FIND_SIZE	512
69040947Sbostic static int divsize = 1;
69140947Sbostic 
69240947Sbostic f_size(plan, entry)
69340947Sbostic 	PLAN *plan;
69440947Sbostic 	FTSENT *entry;
69540947Sbostic {
69640947Sbostic 	off_t size;
69740947Sbostic 
69840947Sbostic 	size = divsize ?
69940947Sbostic 	    (entry->statb.st_size + FIND_SIZE - 1) / FIND_SIZE :
70040947Sbostic 	    entry->statb.st_size;
70140947Sbostic 	COMPARE(size, plan->o_data);
70240947Sbostic }
70340947Sbostic 
70440947Sbostic PLAN *
70540947Sbostic c_size(arg)
70640947Sbostic 	char *arg;
70740947Sbostic {
70840947Sbostic 	PLAN *new;
70940947Sbostic 	char endch;
71040947Sbostic 
71140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
71240947Sbostic 
71340947Sbostic 	NEW(T_SIZE, f_size);
71440947Sbostic 	new->o_data = find_parsenum(new, "-size", arg, &endch);
71540947Sbostic 	if (endch == 'c')
71640947Sbostic 		divsize = 0;
71740947Sbostic 	return(new);
71840947Sbostic }
71940947Sbostic 
72040947Sbostic /*
72140947Sbostic  * -type c functions --
72240947Sbostic  *
72340947Sbostic  *	True if the type of the file is c, where c is b, c, d, p, or f for
72440947Sbostic  *	block special file, character special file, directory, FIFO, or
72540947Sbostic  *	regular file, respectively.
72640947Sbostic  */
72740947Sbostic f_type(plan, entry)
72840947Sbostic 	PLAN *plan;
72940947Sbostic 	FTSENT *entry;
73040947Sbostic {
73140947Sbostic 	return(entry->statb.st_mode & plan->m_data ? FIND_TRUE : FIND_FALSE);
73240947Sbostic }
73340947Sbostic 
73440947Sbostic PLAN *
73540947Sbostic c_type(typestring)
73640947Sbostic 	char *typestring;
73740947Sbostic {
73840947Sbostic 	PLAN *new;
73940947Sbostic 	mode_t  mask;
74040947Sbostic 
74140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
74240947Sbostic 
74340947Sbostic 	switch (typestring[0]) {
74440947Sbostic 	case 'b':
74540947Sbostic 		mask = S_IFBLK;
74640947Sbostic 		break;
74740947Sbostic 	case 'c':
74840947Sbostic 		mask = S_IFCHR;
74940947Sbostic 		break;
75040947Sbostic 	case 'd':
75140947Sbostic 		mask = S_IFDIR;
75240947Sbostic 		break;
75340947Sbostic 	case 'f':
75440947Sbostic 		mask = S_IFREG;
75540947Sbostic 		break;
75640947Sbostic 	case 'l':
75740947Sbostic 		mask = S_IFLNK;
75840947Sbostic 		break;
75940947Sbostic 	case 'p':
76040947Sbostic 		mask = S_IFIFO;
76140947Sbostic 		break;
76240947Sbostic 	case 's':
76340947Sbostic 		mask = S_IFSOCK;
76440947Sbostic 		break;
76540947Sbostic 	default:
76640947Sbostic 		bad_arg("-type", "unknown type");
76740947Sbostic 	}
76840947Sbostic 
76940947Sbostic 	NEW(T_TYPE, f_type);
77040947Sbostic 	new->m_data = mask;
77140947Sbostic 	return(new);
77240947Sbostic }
77340947Sbostic 
77440947Sbostic /*
77540947Sbostic  * -user uname functions --
77640947Sbostic  *
77740947Sbostic  *	True if the file belongs to the user uname.  If uname is numeric and
77840947Sbostic  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
77940947Sbostic  *	return a valid user name, uname is taken as a user ID.
78040947Sbostic  */
78140947Sbostic f_user(plan, entry)
78240947Sbostic 	PLAN *plan;
78340947Sbostic 	FTSENT *entry;
78440947Sbostic {
78540947Sbostic 	return(entry->statb.st_uid == plan->u_data ? FIND_TRUE : FIND_FALSE);
78640947Sbostic }
78740947Sbostic 
78840947Sbostic PLAN *
78940947Sbostic c_user(username)
79040947Sbostic 	char *username;
79140947Sbostic {
79240947Sbostic 	PLAN *new;
79340947Sbostic 	struct passwd *p;
79440947Sbostic 	uid_t uid;
79540947Sbostic 
79640947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
79740947Sbostic 
79840947Sbostic 	p = getpwnam(username);
79940947Sbostic 	if (p == NULL) {
80040947Sbostic 		uid = atoi(username);
80140947Sbostic 		if (uid == 0 && username[0] != '0')
80240947Sbostic 			bad_arg("-user", "no such user");
80340947Sbostic 	} else
80440947Sbostic 		uid = p->pw_uid;
80540947Sbostic 
80640947Sbostic 	NEW(T_USER, f_user);
80740947Sbostic 	new->u_data = uid;
80840947Sbostic 	return(new);
80940947Sbostic }
81040947Sbostic 
81140947Sbostic /*
81240947Sbostic  * -xdev functions --
81340947Sbostic  *
81440947Sbostic  *	Always true, causes find not to decend past directories that have a
81540947Sbostic  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
81640947Sbostic  *
81740947Sbostic  *	Note: this checking is done in find_execute().
81840947Sbostic  */
81940947Sbostic PLAN *
82040947Sbostic c_xdev()
82140947Sbostic {
82240947Sbostic 	extern int xdev;
82340947Sbostic 	PLAN *new;
82440947Sbostic 
82540947Sbostic 	xdev = 1;
82640947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
82740947Sbostic 
82840947Sbostic 	NEW(T_XDEV, f_always_true);
82940947Sbostic 	return(new);
83040947Sbostic }
83140947Sbostic 
83240947Sbostic /*
83340947Sbostic  * ( expression ) functions --
83440947Sbostic  *
83540947Sbostic  *	True if expression is true.
83640947Sbostic  */
83740947Sbostic f_expr(plan, entry)
83840947Sbostic 	PLAN *plan;
83940947Sbostic 	FTSENT *entry;
84040947Sbostic {
84140947Sbostic 	register PLAN *p;
84240947Sbostic 	register int state;
84340947Sbostic 
84440947Sbostic 	for (p = plan->p_data[0];
84540947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
84640947Sbostic 	return(state);
84740947Sbostic }
84840947Sbostic 
84940947Sbostic /*
85040947Sbostic  * T_OPENPAREN and T_CLOSEPAREN nodes are temporary place markers.  They are
85140947Sbostic  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
85240947Sbostic  * to a T_EXPR node containing the expression and the ')' node is discarded.
85340947Sbostic  */
85440947Sbostic PLAN *
85540947Sbostic c_openparen()
85640947Sbostic {
85740947Sbostic 	PLAN *new;
85840947Sbostic 
85940947Sbostic 	NEW(T_OPENPAREN, (int (*)())-1);
86040947Sbostic 	return(new);
86140947Sbostic }
86240947Sbostic 
86340947Sbostic PLAN *
86440947Sbostic c_closeparen()
86540947Sbostic {
86640947Sbostic 	PLAN *new;
86740947Sbostic 
86840947Sbostic 	NEW(T_CLOSEPAREN, (int (*)())-1);
86940947Sbostic 	return(new);
87040947Sbostic }
87140947Sbostic 
87240947Sbostic /*
87340947Sbostic  * -mtime n functions --
87440947Sbostic  *
87540947Sbostic  *	True if the difference between the file modification time and the
87640947Sbostic  *	current time is n 24 hour periods.
87740947Sbostic  */
87840947Sbostic f_mtime(plan, entry)
87940947Sbostic 	PLAN *plan;
88040947Sbostic 	FTSENT *entry;
88140947Sbostic {
88240947Sbostic 	extern time_t now;
88340947Sbostic 
88440947Sbostic 	COMPARE((now - entry->statb.st_mtime + SECSPERDAY - 1) / SECSPERDAY,
88540947Sbostic 	    plan->t_data);
88640947Sbostic }
88740947Sbostic 
88840947Sbostic PLAN *
88940947Sbostic c_mtime(arg)
89040947Sbostic 	char *arg;
89140947Sbostic {
89240947Sbostic 	PLAN *new;
89340947Sbostic 
89440947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
89540947Sbostic 
89640947Sbostic 	NEW(T_MTIME, f_mtime);
89740947Sbostic 	new->t_data = find_parsenum(new, "-mtime", arg, (char *)NULL);
89840947Sbostic 	return(new);
89940947Sbostic }
90040947Sbostic 
90140947Sbostic /*
90240947Sbostic  * ! expression functions --
90340947Sbostic  *
90440947Sbostic  *	Negation of a primary; the unary NOT operator.
90540947Sbostic  */
90640947Sbostic f_not(plan, entry)
90740947Sbostic 	PLAN *plan;
90840947Sbostic 	FTSENT *entry;
90940947Sbostic {
91040947Sbostic 	register PLAN *p;
91140947Sbostic 	register int state;
91240947Sbostic 
91340947Sbostic 	for (p = plan->p_data[0];
91440947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
91540947Sbostic 	return(!state);
91640947Sbostic }
91740947Sbostic 
91840947Sbostic PLAN *
91940947Sbostic c_not()
92040947Sbostic {
92140947Sbostic 	PLAN *new;
92240947Sbostic 
92340947Sbostic 	NEW(T_NOT, f_not);
92440947Sbostic 	return(new);
92540947Sbostic }
92640947Sbostic 
92740947Sbostic /*
92840947Sbostic  * expression -o expression functions --
92940947Sbostic  *
93040947Sbostic  *	Alternation of primaries; the OR operator.  The second expression is
93140947Sbostic  * not evaluated if the first expression is true.
93240947Sbostic  */
93340947Sbostic f_or(plan, entry)
93440947Sbostic 	PLAN *plan;
93540947Sbostic 	FTSENT *entry;
93640947Sbostic {
93740947Sbostic 	register PLAN *p;
93840947Sbostic 	register int state;
93940947Sbostic 
94040947Sbostic 	for (p = plan->p_data[0];
94140947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
94240947Sbostic 
94340947Sbostic 	if (state)
94440947Sbostic 		return(FIND_TRUE);
94540947Sbostic 
94640947Sbostic 	for (p = plan->p_data[1];
94740947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
94840947Sbostic 	return(state);
94940947Sbostic }
95040947Sbostic 
95140947Sbostic PLAN *
95240947Sbostic c_or()
95340947Sbostic {
95440947Sbostic 	PLAN *new;
95540947Sbostic 
95640947Sbostic 	NEW(T_OR, f_or);
95740947Sbostic 	return(new);
95840947Sbostic }
959