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