xref: /csrg-svn/usr.bin/find/function.c (revision 45616)
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*45616Sbostic static char sccsid[] = "@(#)function.c	5.11 (Berkeley) 11/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>
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();
198*45616Sbostic 	void brace_subst();
19940947Sbostic 
20040947Sbostic 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
20140947Sbostic 		if (plan->e_len[cnt])
202*45616Sbostic 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
20345404Sbostic 			    entry->fts_accpath, plan->e_len[cnt]);
20440947Sbostic 
205*45616Sbostic 	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;
31040947Sbostic 
31140947Sbostic 	/* only check when we cross mount point */
31242275Sbostic 	if (first || curdev != entry->fts_statb.st_dev) {
31343856Sbostic 		curdev = entry->fts_statb.st_dev;
31442255Sbostic 		if (statfs(entry->fts_accpath, &sb)) {
31540947Sbostic 			(void)fprintf(stderr, "find: %s: %s.\n",
31642255Sbostic 			    entry->fts_accpath, strerror(errno));
31740947Sbostic 			exit(1);
31840947Sbostic 		}
31942275Sbostic 		first = 0;
32042255Sbostic 		val = plan->flags == MOUNT_NONE ? sb.f_flags : sb.f_type;
32140947Sbostic 	}
32242255Sbostic 	return(plan->flags == MOUNT_NONE ?
32342255Sbostic 	    val & MNT_LOCAL : val == plan->flags);
32440947Sbostic }
32540947Sbostic 
32640947Sbostic PLAN *
32740947Sbostic c_fstype(arg)
32840947Sbostic 	char *arg;
32940947Sbostic {
33040947Sbostic 	register PLAN *new;
33140947Sbostic 
33240947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
33340947Sbostic 
33440947Sbostic 	NEW(T_FSTYPE, f_fstype);
33540947Sbostic 	switch(*arg) {
33642255Sbostic 	case 'l':
33742255Sbostic 		if (!strcmp(arg, "local")) {
33842255Sbostic 			new->flags = MOUNT_NONE;
33942255Sbostic 			return(new);
34042255Sbostic 		}
34142255Sbostic 		break;
34240947Sbostic 	case 'm':
34340947Sbostic 		if (!strcmp(arg, "mfs")) {
34440947Sbostic 			new->flags = MOUNT_MFS;
34540947Sbostic 			return(new);
34640947Sbostic 		}
34740947Sbostic 		break;
34840947Sbostic 	case 'n':
34940947Sbostic 		if (!strcmp(arg, "nfs")) {
35040947Sbostic 			new->flags = MOUNT_NFS;
35140947Sbostic 			return(new);
35240947Sbostic 		}
35340947Sbostic 		break;
35440947Sbostic 	case 'p':
35540947Sbostic 		if (!strcmp(arg, "pc")) {
35640947Sbostic 			new->flags = MOUNT_PC;
35740947Sbostic 			return(new);
35840947Sbostic 		}
35940947Sbostic 		break;
36040947Sbostic 	case 'u':
36140947Sbostic 		if (!strcmp(arg, "ufs")) {
36240947Sbostic 			new->flags = MOUNT_UFS;
36340947Sbostic 			return(new);
36440947Sbostic 		}
36540947Sbostic 		break;
36640947Sbostic 	}
36740947Sbostic 	(void)fprintf(stderr, "find: unknown file type %s.\n", arg);
36840947Sbostic 	exit(1);
36940947Sbostic 	/* NOTREACHED */
37040947Sbostic }
37140947Sbostic 
37240947Sbostic /*
37340947Sbostic  * -group gname functions --
37440947Sbostic  *
37540947Sbostic  *	True if the file belongs to the group gname.  If gname is numeric and
37640947Sbostic  *	an equivalent of the getgrnam() function does not return a valid group
37740947Sbostic  *	name, gname is taken as a group ID.
37840947Sbostic  */
37940947Sbostic f_group(plan, entry)
38040947Sbostic 	PLAN *plan;
38140947Sbostic 	FTSENT *entry;
38240947Sbostic {
38342255Sbostic 	return(entry->fts_statb.st_gid == plan->g_data);
38440947Sbostic }
38540947Sbostic 
38640947Sbostic PLAN *
38740947Sbostic c_group(gname)
38840947Sbostic 	char *gname;
38940947Sbostic {
39040947Sbostic 	PLAN *new;
39140947Sbostic 	struct group *g;
39240947Sbostic 	gid_t gid;
39345615Sbostic 	void bad_arg();
39440947Sbostic 
39540947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
39640947Sbostic 
39740947Sbostic 	g = getgrnam(gname);
39840947Sbostic 	if (g == NULL) {
39940947Sbostic 		gid = atoi(gname);
40040947Sbostic 		if (gid == 0 && gname[0] != '0')
40140947Sbostic 			bad_arg("-group", "no such group");
40240947Sbostic 	} else
40340947Sbostic 		gid = g->gr_gid;
40440947Sbostic 
40540947Sbostic 	NEW(T_GROUP, f_group);
40640947Sbostic 	new->g_data = gid;
40740947Sbostic 	return(new);
40840947Sbostic }
40940947Sbostic 
41040947Sbostic /*
41140947Sbostic  * -inum n functions --
41240947Sbostic  *
41340947Sbostic  *	True if the file has inode # n.
41440947Sbostic  */
41540947Sbostic f_inum(plan, entry)
41640947Sbostic 	PLAN *plan;
41740947Sbostic 	FTSENT *entry;
41840947Sbostic {
41942255Sbostic 	COMPARE(entry->fts_statb.st_ino, plan->i_data);
42040947Sbostic }
42140947Sbostic 
42240947Sbostic PLAN *
42340947Sbostic c_inum(arg)
42440947Sbostic 	char *arg;
42540947Sbostic {
42640947Sbostic 	PLAN *new;
42740947Sbostic 
42840947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
42940947Sbostic 
43040947Sbostic 	NEW(T_INUM, f_inum);
43140947Sbostic 	new->i_data = find_parsenum(new, "-inum", arg, (char *)NULL);
43240947Sbostic 	return(new);
43340947Sbostic }
43440947Sbostic 
43540947Sbostic /*
43640947Sbostic  * -links n functions --
43740947Sbostic  *
43840947Sbostic  *	True if the file has n links.
43940947Sbostic  */
44040947Sbostic f_links(plan, entry)
44140947Sbostic 	PLAN *plan;
44240947Sbostic 	FTSENT *entry;
44340947Sbostic {
44442255Sbostic 	COMPARE(entry->fts_statb.st_nlink, plan->l_data);
44540947Sbostic }
44640947Sbostic 
44740947Sbostic PLAN *
44840947Sbostic c_links(arg)
44940947Sbostic 	char *arg;
45040947Sbostic {
45140947Sbostic 	PLAN *new;
45240947Sbostic 
45340947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
45440947Sbostic 
45540947Sbostic 	NEW(T_LINKS, f_links);
456*45616Sbostic 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, (char *)NULL);
45740947Sbostic 	return(new);
45840947Sbostic }
45940947Sbostic 
46040947Sbostic /*
46140947Sbostic  * -ls functions --
46240947Sbostic  *
46340947Sbostic  *	Always true - prints the current entry to stdout in "ls" format.
46440947Sbostic  */
46540947Sbostic /* ARGSUSED */
46640947Sbostic f_ls(plan, entry)
46740947Sbostic 	PLAN *plan;
46840947Sbostic 	FTSENT *entry;
46940947Sbostic {
47045615Sbostic 	void printlong();
47145615Sbostic 
47242255Sbostic 	printlong(entry->fts_path, entry->fts_accpath, &entry->fts_statb);
47342255Sbostic 	return(1);
47440947Sbostic }
47540947Sbostic 
47640947Sbostic PLAN *
47740947Sbostic c_ls()
47840947Sbostic {
47940947Sbostic 	PLAN *new;
48040947Sbostic 
48140947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
48245615Sbostic 	isoutput = 1;
48340947Sbostic 
48440947Sbostic 	NEW(T_LS, f_ls);
48540947Sbostic 	return(new);
48640947Sbostic }
48740947Sbostic 
48840947Sbostic /*
48940947Sbostic  * -name functions --
49040947Sbostic  *
49140947Sbostic  *	True if the basename of the filename being examined
49240947Sbostic  *	matches pattern using Pattern Matching Notation S3.14
49340947Sbostic  */
49440947Sbostic f_name(plan, entry)
49540947Sbostic 	PLAN *plan;
49640947Sbostic 	FTSENT *entry;
49740947Sbostic {
49842255Sbostic 	return(fnmatch(plan->c_data, entry->fts_name, FNM_QUOTE));
49940947Sbostic }
50040947Sbostic 
50140947Sbostic PLAN *
50240947Sbostic c_name(pattern)
50340947Sbostic 	char *pattern;
50440947Sbostic {
50540947Sbostic 	PLAN *new;
50640947Sbostic 
50740947Sbostic 	NEW(T_NAME, f_name);
50840947Sbostic 	new->c_data = pattern;
50940947Sbostic 	return(new);
51040947Sbostic }
51140947Sbostic 
51240947Sbostic /*
51340947Sbostic  * -newer file functions --
51440947Sbostic  *
51540947Sbostic  *	True if the current file has been modified more recently
51640947Sbostic  *	then the modification time of the file named by the pathname
51740947Sbostic  *	file.
51840947Sbostic  */
51940947Sbostic f_newer(plan, entry)
52040947Sbostic 	PLAN *plan;
52140947Sbostic 	FTSENT *entry;
52240947Sbostic {
52342255Sbostic 	return(entry->fts_statb.st_mtime > plan->t_data);
52440947Sbostic }
52540947Sbostic 
52640947Sbostic PLAN *
52740947Sbostic c_newer(filename)
52840947Sbostic 	char *filename;
52940947Sbostic {
53040947Sbostic 	PLAN *new;
53140947Sbostic 	struct stat sb;
53240947Sbostic 
53340947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
53440947Sbostic 
53540947Sbostic 	if (stat(filename, &sb)) {
53640947Sbostic 		(void)fprintf(stderr, "find: %s: %s.\n",
53740947Sbostic 		    filename, strerror(errno));
53840947Sbostic 		exit(1);
53940947Sbostic 	}
54040947Sbostic 	NEW(T_NEWER, f_newer);
54140947Sbostic 	new->t_data = sb.st_mtime;
54240947Sbostic 	return(new);
54340947Sbostic }
54440947Sbostic 
54540947Sbostic /*
54640947Sbostic  * -nogroup functions --
54740947Sbostic  *
54840947Sbostic  *	True if file belongs to a user ID for which the equivalent
54940947Sbostic  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
55040947Sbostic  */
55140947Sbostic /* ARGSUSED */
55240947Sbostic f_nogroup(plan, entry)
55340947Sbostic 	PLAN *plan;
55440947Sbostic 	FTSENT *entry;
55540947Sbostic {
55645615Sbostic 	char *group_from_gid();
55745615Sbostic 
55845615Sbostic 	return(group_from_gid(entry->fts_statb.st_gid, 1) ? 1 : 0);
55940947Sbostic }
56040947Sbostic 
56140947Sbostic PLAN *
56240947Sbostic c_nogroup()
56340947Sbostic {
56440947Sbostic 	PLAN *new;
56540947Sbostic 
56640947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
56740947Sbostic 
56840947Sbostic 	NEW(T_NOGROUP, f_nogroup);
56940947Sbostic 	return(new);
57040947Sbostic }
57140947Sbostic 
57240947Sbostic /*
57340947Sbostic  * -nouser functions --
57440947Sbostic  *
57540947Sbostic  *	True if file belongs to a user ID for which the equivalent
57640947Sbostic  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
57740947Sbostic  */
57840947Sbostic /* ARGSUSED */
57940947Sbostic f_nouser(plan, entry)
58040947Sbostic 	PLAN *plan;
58140947Sbostic 	FTSENT *entry;
58240947Sbostic {
58345615Sbostic 	char *user_from_uid();
58445615Sbostic 
58545615Sbostic 	return(user_from_uid(entry->fts_statb.st_uid, 1) ? 1 : 0);
58640947Sbostic }
58740947Sbostic 
58840947Sbostic PLAN *
58940947Sbostic c_nouser()
59040947Sbostic {
59140947Sbostic 	PLAN *new;
59240947Sbostic 
59340947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
59440947Sbostic 
59540947Sbostic 	NEW(T_NOUSER, f_nouser);
59640947Sbostic 	return(new);
59740947Sbostic }
59840947Sbostic 
59940947Sbostic /*
60040947Sbostic  * -perm functions --
60140947Sbostic  *
60240947Sbostic  *	The mode argument is used to represent file mode bits.  If it starts
60340947Sbostic  *	with a leading digit, it's treated as an octal mode, otherwise as a
60440947Sbostic  *	symbolic mode.
60540947Sbostic  */
60640947Sbostic f_perm(plan, entry)
60740947Sbostic 	PLAN *plan;
60840947Sbostic 	FTSENT *entry;
60940947Sbostic {
61040947Sbostic 	mode_t mode;
61140947Sbostic 
61242255Sbostic 	mode = entry->fts_statb.st_mode &
61340947Sbostic 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
61440947Sbostic 	if (plan->flags)
61540947Sbostic 		return((plan->m_data | mode) == mode);
61640947Sbostic 	else
61740947Sbostic 		return(mode == plan->m_data);
61840947Sbostic 	/* NOTREACHED */
61940947Sbostic }
62040947Sbostic 
62140947Sbostic PLAN *
62240947Sbostic c_perm(perm)
62340947Sbostic 	char *perm;
62440947Sbostic {
62540947Sbostic 	PLAN *new;
62644766Strent 	mode_t *set, *setmode();
62745615Sbostic 	void bad_arg();
62840947Sbostic 
62940947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
63040947Sbostic 
63140947Sbostic 	NEW(T_PERM, f_perm);
63240947Sbostic 
63340947Sbostic 	if (*perm == '-') {
63440947Sbostic 		new->flags = 1;
63540947Sbostic 		++perm;
63640947Sbostic 	}
63740947Sbostic 
63844764Strent 	if ((set = setmode(perm)) == NULL)
63940947Sbostic 		bad_arg("-perm", "illegal mode string");
64040947Sbostic 
64144764Strent 	new->m_data = getmode(set, 0);
64240947Sbostic 	return(new);
64340947Sbostic }
64440947Sbostic 
64540947Sbostic /*
64640947Sbostic  * -print functions --
64740947Sbostic  *
64840947Sbostic  *	Always true, causes the current pathame to be written to
64940947Sbostic  *	standard output.
65040947Sbostic  */
65140947Sbostic /* ARGSUSED */
65240947Sbostic f_print(plan, entry)
65340947Sbostic 	PLAN *plan;
65440947Sbostic 	FTSENT *entry;
65540947Sbostic {
65642255Sbostic 	(void)printf("%s\n", entry->fts_path);
65742255Sbostic 	return(1);
65840947Sbostic }
65940947Sbostic 
66040947Sbostic PLAN *
66140947Sbostic c_print()
66240947Sbostic {
66340947Sbostic 	PLAN *new;
66440947Sbostic 
66545615Sbostic 	isoutput = 1;
66640947Sbostic 
66740947Sbostic 	NEW(T_PRINT, f_print);
66840947Sbostic 	return(new);
66940947Sbostic }
67040947Sbostic 
67140947Sbostic /*
67240947Sbostic  * -prune functions --
67340947Sbostic  *
67440947Sbostic  *	Prune a portion of the hierarchy.
67540947Sbostic  */
67640947Sbostic /* ARGSUSED */
67740947Sbostic f_prune(plan, entry)
67840947Sbostic 	PLAN *plan;
67940947Sbostic 	FTSENT *entry;
68040947Sbostic {
68140947Sbostic 	extern FTS *tree;
68240947Sbostic 
68345615Sbostic 	if (fts_set(tree, entry, FTS_SKIP)) {
68440947Sbostic 		(void)fprintf(stderr,
68542255Sbostic 		    "find: %s: %s.\n", entry->fts_path, strerror(errno));
68640947Sbostic 		exit(1);
68740947Sbostic 	}
68842255Sbostic 	return(1);
68940947Sbostic }
69040947Sbostic 
69140947Sbostic PLAN *
69240947Sbostic c_prune()
69340947Sbostic {
69440947Sbostic 	PLAN *new;
69540947Sbostic 
69640947Sbostic 	NEW(T_PRUNE, f_prune);
69740947Sbostic 	return(new);
69840947Sbostic }
69940947Sbostic 
70040947Sbostic /*
70140947Sbostic  * -size n[c] functions --
70240947Sbostic  *
70340947Sbostic  *	True if the file size in bytes, divided by an implementation defined
70440947Sbostic  *	value and rounded up to the next integer, is n.  If n is followed by
70540947Sbostic  *	a c, the size is in bytes.
70640947Sbostic  */
70740947Sbostic #define	FIND_SIZE	512
70840947Sbostic static int divsize = 1;
70940947Sbostic 
71040947Sbostic f_size(plan, entry)
71140947Sbostic 	PLAN *plan;
71240947Sbostic 	FTSENT *entry;
71340947Sbostic {
71440947Sbostic 	off_t size;
71540947Sbostic 
71642255Sbostic 	size = divsize ? (entry->fts_statb.st_size + FIND_SIZE - 1) /
71742255Sbostic 	    FIND_SIZE : entry->fts_statb.st_size;
71840947Sbostic 	COMPARE(size, plan->o_data);
71940947Sbostic }
72040947Sbostic 
72140947Sbostic PLAN *
72240947Sbostic c_size(arg)
72340947Sbostic 	char *arg;
72440947Sbostic {
72540947Sbostic 	PLAN *new;
72640947Sbostic 	char endch;
72740947Sbostic 
72840947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
72940947Sbostic 
73040947Sbostic 	NEW(T_SIZE, f_size);
73140947Sbostic 	new->o_data = find_parsenum(new, "-size", arg, &endch);
73240947Sbostic 	if (endch == 'c')
73340947Sbostic 		divsize = 0;
73440947Sbostic 	return(new);
73540947Sbostic }
73640947Sbostic 
73740947Sbostic /*
73840947Sbostic  * -type c functions --
73940947Sbostic  *
74040947Sbostic  *	True if the type of the file is c, where c is b, c, d, p, or f for
74140947Sbostic  *	block special file, character special file, directory, FIFO, or
74240947Sbostic  *	regular file, respectively.
74340947Sbostic  */
74440947Sbostic f_type(plan, entry)
74540947Sbostic 	PLAN *plan;
74640947Sbostic 	FTSENT *entry;
74740947Sbostic {
74844764Strent 	return((entry->fts_statb.st_mode & S_IFMT) == plan->m_data);
74940947Sbostic }
75040947Sbostic 
75140947Sbostic PLAN *
75240947Sbostic c_type(typestring)
75340947Sbostic 	char *typestring;
75440947Sbostic {
75540947Sbostic 	PLAN *new;
75640947Sbostic 	mode_t  mask;
75745615Sbostic 	void bad_arg();
75840947Sbostic 
75940947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
76040947Sbostic 
76140947Sbostic 	switch (typestring[0]) {
76240947Sbostic 	case 'b':
76340947Sbostic 		mask = S_IFBLK;
76440947Sbostic 		break;
76540947Sbostic 	case 'c':
76640947Sbostic 		mask = S_IFCHR;
76740947Sbostic 		break;
76840947Sbostic 	case 'd':
76940947Sbostic 		mask = S_IFDIR;
77040947Sbostic 		break;
77140947Sbostic 	case 'f':
77240947Sbostic 		mask = S_IFREG;
77340947Sbostic 		break;
77440947Sbostic 	case 'l':
77540947Sbostic 		mask = S_IFLNK;
77640947Sbostic 		break;
77740947Sbostic 	case 'p':
77840947Sbostic 		mask = S_IFIFO;
77940947Sbostic 		break;
78040947Sbostic 	case 's':
78140947Sbostic 		mask = S_IFSOCK;
78240947Sbostic 		break;
78340947Sbostic 	default:
78440947Sbostic 		bad_arg("-type", "unknown type");
78540947Sbostic 	}
78640947Sbostic 
78740947Sbostic 	NEW(T_TYPE, f_type);
78840947Sbostic 	new->m_data = mask;
78940947Sbostic 	return(new);
79040947Sbostic }
79140947Sbostic 
79240947Sbostic /*
79340947Sbostic  * -user uname functions --
79440947Sbostic  *
79540947Sbostic  *	True if the file belongs to the user uname.  If uname is numeric and
79640947Sbostic  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
79740947Sbostic  *	return a valid user name, uname is taken as a user ID.
79840947Sbostic  */
79940947Sbostic f_user(plan, entry)
80040947Sbostic 	PLAN *plan;
80140947Sbostic 	FTSENT *entry;
80240947Sbostic {
80342255Sbostic 	return(entry->fts_statb.st_uid == plan->u_data);
80440947Sbostic }
80540947Sbostic 
80640947Sbostic PLAN *
80740947Sbostic c_user(username)
80840947Sbostic 	char *username;
80940947Sbostic {
81040947Sbostic 	PLAN *new;
81140947Sbostic 	struct passwd *p;
81240947Sbostic 	uid_t uid;
81345615Sbostic 	void bad_arg();
81440947Sbostic 
81540947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
81640947Sbostic 
81740947Sbostic 	p = getpwnam(username);
81840947Sbostic 	if (p == NULL) {
81940947Sbostic 		uid = atoi(username);
82040947Sbostic 		if (uid == 0 && username[0] != '0')
82140947Sbostic 			bad_arg("-user", "no such user");
82240947Sbostic 	} else
82340947Sbostic 		uid = p->pw_uid;
82440947Sbostic 
82540947Sbostic 	NEW(T_USER, f_user);
82640947Sbostic 	new->u_data = uid;
82740947Sbostic 	return(new);
82840947Sbostic }
82940947Sbostic 
83040947Sbostic /*
83140947Sbostic  * -xdev functions --
83240947Sbostic  *
83340947Sbostic  *	Always true, causes find not to decend past directories that have a
83440947Sbostic  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
83540947Sbostic  */
83640947Sbostic PLAN *
83740947Sbostic c_xdev()
83840947Sbostic {
83940947Sbostic 	PLAN *new;
84040947Sbostic 
84142275Sbostic 	ftsoptions |= FTS_XDEV;
84240947Sbostic 
84340947Sbostic 	NEW(T_XDEV, f_always_true);
84440947Sbostic 	return(new);
84540947Sbostic }
84640947Sbostic 
84740947Sbostic /*
84840947Sbostic  * ( expression ) functions --
84940947Sbostic  *
85040947Sbostic  *	True if expression is true.
85140947Sbostic  */
85240947Sbostic f_expr(plan, entry)
85340947Sbostic 	PLAN *plan;
85440947Sbostic 	FTSENT *entry;
85540947Sbostic {
85640947Sbostic 	register PLAN *p;
85740947Sbostic 	register int state;
85840947Sbostic 
85940947Sbostic 	for (p = plan->p_data[0];
86040947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
86140947Sbostic 	return(state);
86240947Sbostic }
86340947Sbostic 
86440947Sbostic /*
86540947Sbostic  * T_OPENPAREN and T_CLOSEPAREN nodes are temporary place markers.  They are
86640947Sbostic  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
86740947Sbostic  * to a T_EXPR node containing the expression and the ')' node is discarded.
86840947Sbostic  */
86940947Sbostic PLAN *
87040947Sbostic c_openparen()
87140947Sbostic {
87240947Sbostic 	PLAN *new;
87340947Sbostic 
87440947Sbostic 	NEW(T_OPENPAREN, (int (*)())-1);
87540947Sbostic 	return(new);
87640947Sbostic }
87740947Sbostic 
87840947Sbostic PLAN *
87940947Sbostic c_closeparen()
88040947Sbostic {
88140947Sbostic 	PLAN *new;
88240947Sbostic 
88340947Sbostic 	NEW(T_CLOSEPAREN, (int (*)())-1);
88440947Sbostic 	return(new);
88540947Sbostic }
88640947Sbostic 
88740947Sbostic /*
88840947Sbostic  * -mtime n functions --
88940947Sbostic  *
89040947Sbostic  *	True if the difference between the file modification time and the
89140947Sbostic  *	current time is n 24 hour periods.
89240947Sbostic  */
89340947Sbostic f_mtime(plan, entry)
89440947Sbostic 	PLAN *plan;
89540947Sbostic 	FTSENT *entry;
89640947Sbostic {
89740947Sbostic 	extern time_t now;
89840947Sbostic 
89942255Sbostic 	COMPARE((now - entry->fts_statb.st_mtime + SECSPERDAY - 1) /
90042255Sbostic 	    SECSPERDAY, plan->t_data);
90140947Sbostic }
90240947Sbostic 
90340947Sbostic PLAN *
90440947Sbostic c_mtime(arg)
90540947Sbostic 	char *arg;
90640947Sbostic {
90740947Sbostic 	PLAN *new;
90840947Sbostic 
90940947Sbostic 	ftsoptions &= ~FTS_NOSTAT;
91040947Sbostic 
91140947Sbostic 	NEW(T_MTIME, f_mtime);
91240947Sbostic 	new->t_data = find_parsenum(new, "-mtime", arg, (char *)NULL);
91340947Sbostic 	return(new);
91440947Sbostic }
91540947Sbostic 
91640947Sbostic /*
91740947Sbostic  * ! expression functions --
91840947Sbostic  *
91940947Sbostic  *	Negation of a primary; the unary NOT operator.
92040947Sbostic  */
92140947Sbostic f_not(plan, entry)
92240947Sbostic 	PLAN *plan;
92340947Sbostic 	FTSENT *entry;
92440947Sbostic {
92540947Sbostic 	register PLAN *p;
92640947Sbostic 	register int state;
92740947Sbostic 
92840947Sbostic 	for (p = plan->p_data[0];
92940947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
93040947Sbostic 	return(!state);
93140947Sbostic }
93240947Sbostic 
93340947Sbostic PLAN *
93440947Sbostic c_not()
93540947Sbostic {
93640947Sbostic 	PLAN *new;
93740947Sbostic 
93840947Sbostic 	NEW(T_NOT, f_not);
93940947Sbostic 	return(new);
94040947Sbostic }
94140947Sbostic 
94240947Sbostic /*
94340947Sbostic  * expression -o expression functions --
94440947Sbostic  *
94540947Sbostic  *	Alternation of primaries; the OR operator.  The second expression is
94640947Sbostic  * not evaluated if the first expression is true.
94740947Sbostic  */
94840947Sbostic f_or(plan, entry)
94940947Sbostic 	PLAN *plan;
95040947Sbostic 	FTSENT *entry;
95140947Sbostic {
95240947Sbostic 	register PLAN *p;
95340947Sbostic 	register int state;
95440947Sbostic 
95540947Sbostic 	for (p = plan->p_data[0];
95640947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
95740947Sbostic 
95840947Sbostic 	if (state)
95942255Sbostic 		return(1);
96040947Sbostic 
96140947Sbostic 	for (p = plan->p_data[1];
96240947Sbostic 	    p && (state = (p->eval)(p, entry)); p = p->next);
96340947Sbostic 	return(state);
96440947Sbostic }
96540947Sbostic 
96640947Sbostic PLAN *
96740947Sbostic c_or()
96840947Sbostic {
96940947Sbostic 	PLAN *new;
97040947Sbostic 
97140947Sbostic 	NEW(T_OR, f_or);
97240947Sbostic 	return(new);
97340947Sbostic }
974