Home | History | Annotate | Line # | Download | only in find
function.c revision 1.1
      1 /*-
      2  * Copyright (c) 1990 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Cimarron D. Taylor of the University of California, Berkeley.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef lint
     38 static char sccsid[] = "@(#)function.c	5.17 (Berkeley) 5/24/91";
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 #include <sys/stat.h>
     43 #include <sys/wait.h>
     44 #include <sys/mount.h>
     45 #include <errno.h>
     46 #include <grp.h>
     47 #include <pwd.h>
     48 #include <fts.h>
     49 #include <unistd.h>
     50 #include <tzfile.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include "find.h"
     55 
     56 #define	FIND_EQUAL	0
     57 #define	FIND_LESSTHAN	1
     58 #define	FIND_GREATER	2
     59 
     60 #define	COMPARE(a, b) { \
     61 	switch(plan->flags) { \
     62 	case FIND_EQUAL: \
     63 		return(a == b); \
     64 	case FIND_LESSTHAN: \
     65 		return(a < b); \
     66 	case FIND_GREATER: \
     67 		return(a > b); \
     68 	} \
     69 	return(0); \
     70 }
     71 
     72 static PLAN *palloc __P((enum ntype, int (*)()));
     73 
     74 /*
     75  * find_parsenum --
     76  *	Parse a string of the form [+-]# and return the value.
     77  */
     78 long
     79 find_parsenum(plan, option, str, endch)
     80 	PLAN *plan;
     81 	char *option, *str, *endch;
     82 {
     83 	long value;
     84 	char *endchar;		/* pointer to character ending conversion */
     85 
     86 	/* determine comparison from leading + or - */
     87 	switch(*str) {
     88 	case '+':
     89 		++str;
     90 		plan->flags = FIND_GREATER;
     91 		break;
     92 	case '-':
     93 		++str;
     94 		plan->flags = FIND_LESSTHAN;
     95 		break;
     96 	default:
     97 		plan->flags = FIND_EQUAL;
     98 		break;
     99 	}
    100 
    101 	/*
    102 	 * convert the string with strtol().  Note, if strtol() returns zero
    103 	 * and endchar points to the beginning of the string we know we have
    104 	 * a syntax error.
    105 	 */
    106 	value = strtol(str, &endchar, 10);
    107 	if (!value && endchar == str ||
    108 	    endchar[0] && (!endch || endchar[0] != *endch))
    109 		err("%s: %s", option, "illegal numeric value");
    110 	if (endch)
    111 		*endch = endchar[0];
    112 	return(value);
    113 }
    114 
    115 /*
    116  * -atime n functions --
    117  *
    118  *	True if the difference between the file access time and the
    119  *	current time is n 24 hour periods.
    120  *
    121  */
    122 f_atime(plan, entry)
    123 	PLAN *plan;
    124 	FTSENT *entry;
    125 {
    126 	extern time_t now;
    127 
    128 	COMPARE((now - entry->fts_statb.st_atime +
    129 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
    130 }
    131 
    132 PLAN *
    133 c_atime(arg)
    134 	char *arg;
    135 {
    136 	PLAN *new;
    137 
    138 	ftsoptions &= ~FTS_NOSTAT;
    139 
    140 	new = palloc(N_ATIME, f_atime);
    141 	new->t_data = find_parsenum(new, "-atime", arg, NULL);
    142 	return(new);
    143 }
    144 /*
    145  * -ctime n functions --
    146  *
    147  *	True if the difference between the last change of file
    148  *	status information and the current time is n 24 hour periods.
    149  */
    150 f_ctime(plan, entry)
    151 	PLAN *plan;
    152 	FTSENT *entry;
    153 {
    154 	extern time_t now;
    155 
    156 	COMPARE((now - entry->fts_statb.st_ctime +
    157 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
    158 }
    159 
    160 PLAN *
    161 c_ctime(arg)
    162 	char *arg;
    163 {
    164 	PLAN *new;
    165 
    166 	ftsoptions &= ~FTS_NOSTAT;
    167 
    168 	new = palloc(N_CTIME, f_ctime);
    169 	new->t_data = find_parsenum(new, "-ctime", arg, (char *)NULL);
    170 	return(new);
    171 }
    172 
    173 /*
    174  * -depth functions --
    175  *
    176  *	Always true, causes descent of the directory hierarchy to be done
    177  *	so that all entries in a directory are acted on before the directory
    178  *	itself.
    179  */
    180 /* ARGSUSED */
    181 f_always_true(plan, entry)
    182 	PLAN *plan;
    183 	FTSENT *entry;
    184 {
    185 	return(1);
    186 }
    187 
    188 PLAN *
    189 c_depth()
    190 {
    191 	isdepth = 1;
    192 
    193 	return(palloc(N_DEPTH, f_always_true));
    194 }
    195 
    196 /*
    197  * [-exec | -ok] utility [arg ... ] ; functions --
    198  *
    199  *	True if the executed utility returns a zero value as exit status.
    200  *	The end of the primary expression is delimited by a semicolon.  If
    201  *	"{}" occurs anywhere, it gets replaced by the current pathname.
    202  *	The current directory for the execution of utility is the same as
    203  *	the current directory when the find utility was started.
    204  *
    205  *	The primary -ok is different in that it requests affirmation of the
    206  *	user before executing the utility.
    207  */
    208 f_exec(plan, entry)
    209 	register PLAN *plan;
    210 	FTSENT *entry;
    211 {
    212 	extern int dotfd;
    213 	register int cnt;
    214 	pid_t pid;
    215 	int status;
    216 
    217 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
    218 		if (plan->e_len[cnt])
    219 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
    220 			    entry->fts_path, plan->e_len[cnt]);
    221 
    222 	if (plan->flags && !queryuser(plan->e_argv))
    223 		return(0);
    224 
    225 	switch(pid = vfork()) {
    226 	case -1:
    227 		err("fork: %s", strerror(errno));
    228 		/* NOTREACHED */
    229 	case 0:
    230 		if (fchdir(dotfd)) {
    231 			(void)fprintf(stderr,
    232 			    "find: chdir: %s\n", strerror(errno));
    233 			_exit(1);
    234 		}
    235 		execvp(plan->e_argv[0], plan->e_argv);
    236 		(void)fprintf(stderr,
    237 		    "find: %s: %s\n", plan->e_argv[0], strerror(errno));
    238 		_exit(1);
    239 	}
    240 	pid = waitpid(pid, &status, 0);
    241 	return(pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
    242 }
    243 
    244 /*
    245  * c_exec --
    246  *	build three parallel arrays, one with pointers to the strings passed
    247  *	on the command line, one with (possibly duplicated) pointers to the
    248  *	argv array, and one with integer values that are lengths of the
    249  *	strings, but also flags meaning that the string has to be massaged.
    250  */
    251 PLAN *
    252 c_exec(argvp, isok)
    253 	char ***argvp;
    254 	int isok;
    255 {
    256 	PLAN *new;			/* node returned */
    257 	register int cnt;
    258 	register char **argv, **ap, *p;
    259 
    260 	isoutput = 1;
    261 
    262 	new = palloc(N_EXEC, f_exec);
    263 	new->flags = isok;
    264 
    265 	for (ap = argv = *argvp;; ++ap) {
    266 		if (!*ap)
    267 			err("%s: %s",
    268 			    isok ? "-ok" : "-exec", "no terminating \";\"");
    269 		if (**ap == ';')
    270 			break;
    271 	}
    272 
    273 	cnt = ap - *argvp + 1;
    274 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
    275 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
    276 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
    277 
    278 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
    279 		new->e_orig[cnt] = *argv;
    280 		for (p = *argv; *p; ++p)
    281 			if (p[0] == '{' && p[1] == '}') {
    282 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
    283 				new->e_len[cnt] = MAXPATHLEN;
    284 				break;
    285 			}
    286 		if (!*p) {
    287 			new->e_argv[cnt] = *argv;
    288 			new->e_len[cnt] = 0;
    289 		}
    290 	}
    291 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
    292 
    293 	*argvp = argv + 1;
    294 	return(new);
    295 }
    296 
    297 /*
    298  * -follow functions --
    299  *
    300  *	Always true, causes symbolic links to be followed on a global
    301  *	basis.
    302  */
    303 PLAN *
    304 c_follow()
    305 {
    306 	ftsoptions &= ~FTS_PHYSICAL;
    307 	ftsoptions |= FTS_LOGICAL;
    308 
    309 	return(palloc(N_FOLLOW, f_always_true));
    310 }
    311 
    312 /*
    313  * -fstype functions --
    314  *
    315  *	True if the file is of a certain type.
    316  */
    317 f_fstype(plan, entry)
    318 	PLAN *plan;
    319 	FTSENT *entry;
    320 {
    321 	static dev_t curdev;	/* need a guaranteed illegal dev value */
    322 	static int first = 1;
    323 	struct statfs sb;
    324 	static short val;
    325 	char *p, save[2];
    326 
    327 	/* only check when we cross mount point */
    328 	if (first || curdev != entry->fts_statb.st_dev) {
    329 		curdev = entry->fts_statb.st_dev;
    330 
    331 		/*
    332 		 * Statfs follows symlinks; find wants the link's file system,
    333 		 * not where it points.
    334 		 */
    335 		if (entry->fts_info == FTS_SL ||
    336 		    entry->fts_info == FTS_SLNONE) {
    337 			if (p = rindex(entry->fts_accpath, '/'))
    338 				++p;
    339 			else
    340 				p = entry->fts_accpath;
    341 			save[0] = p[0];
    342 			p[0] = '.';
    343 			save[1] = p[1];
    344 			p[1] = '\0';
    345 
    346 		} else
    347 			p = NULL;
    348 
    349 		if (statfs(entry->fts_accpath, &sb))
    350 			err("%s: %s", entry->fts_accpath, strerror(errno));
    351 
    352 		if (p) {
    353 			p[0] = save[0];
    354 			p[1] = save[1];
    355 		}
    356 
    357 		first = 0;
    358 		val = plan->flags == MOUNT_NONE ? sb.f_flags : sb.f_type;
    359 	}
    360 	return(plan->flags == MOUNT_NONE ?
    361 	    val & MNT_LOCAL : val == plan->flags);
    362 }
    363 
    364 PLAN *
    365 c_fstype(arg)
    366 	char *arg;
    367 {
    368 	register PLAN *new;
    369 
    370 	ftsoptions &= ~FTS_NOSTAT;
    371 
    372 	new = palloc(N_FSTYPE, f_fstype);
    373 	switch(*arg) {
    374 	case 'i':
    375 		if (!strcmp(arg, "isofs")) {
    376 			new->flags = MOUNT_ISOFS;
    377 			return(new);
    378 		}
    379 		break;
    380 	case 'l':
    381 		if (!strcmp(arg, "local")) {
    382 			new->flags = MOUNT_NONE;
    383 			return(new);
    384 		}
    385 		break;
    386 	case 'm':
    387 		if (!strcmp(arg, "mfs")) {
    388 			new->flags = MOUNT_MFS;
    389 			return(new);
    390 		}
    391 		if (!strcmp(arg, "msdos")) {
    392 			new->flags = MOUNT_MSDOS;
    393 			return(new);
    394 		}
    395 		break;
    396 	case 'n':
    397 		if (!strcmp(arg, "nfs")) {
    398 			new->flags = MOUNT_NFS;
    399 			return(new);
    400 		}
    401 		break;
    402 	case 'u':
    403 		if (!strcmp(arg, "ufs")) {
    404 			new->flags = MOUNT_UFS;
    405 			return(new);
    406 		}
    407 		break;
    408 	}
    409 	err("unknown file type %s", arg);
    410 	/* NOTREACHED */
    411 }
    412 
    413 /*
    414  * -group gname functions --
    415  *
    416  *	True if the file belongs to the group gname.  If gname is numeric and
    417  *	an equivalent of the getgrnam() function does not return a valid group
    418  *	name, gname is taken as a group ID.
    419  */
    420 f_group(plan, entry)
    421 	PLAN *plan;
    422 	FTSENT *entry;
    423 {
    424 	return(entry->fts_statb.st_gid == plan->g_data);
    425 }
    426 
    427 PLAN *
    428 c_group(gname)
    429 	char *gname;
    430 {
    431 	PLAN *new;
    432 	struct group *g;
    433 	gid_t gid;
    434 
    435 	ftsoptions &= ~FTS_NOSTAT;
    436 
    437 	g = getgrnam(gname);
    438 	if (g == NULL) {
    439 		gid = atoi(gname);
    440 		if (gid == 0 && gname[0] != '0')
    441 			err("%s: %s", "-group", "no such group");
    442 	} else
    443 		gid = g->gr_gid;
    444 
    445 	new = palloc(N_GROUP, f_group);
    446 	new->g_data = gid;
    447 	return(new);
    448 }
    449 
    450 /*
    451  * -inum n functions --
    452  *
    453  *	True if the file has inode # n.
    454  */
    455 f_inum(plan, entry)
    456 	PLAN *plan;
    457 	FTSENT *entry;
    458 {
    459 	COMPARE(entry->fts_statb.st_ino, plan->i_data);
    460 }
    461 
    462 PLAN *
    463 c_inum(arg)
    464 	char *arg;
    465 {
    466 	PLAN *new;
    467 
    468 	ftsoptions &= ~FTS_NOSTAT;
    469 
    470 	new = palloc(N_INUM, f_inum);
    471 	new->i_data = find_parsenum(new, "-inum", arg, (char *)NULL);
    472 	return(new);
    473 }
    474 
    475 /*
    476  * -links n functions --
    477  *
    478  *	True if the file has n links.
    479  */
    480 f_links(plan, entry)
    481 	PLAN *plan;
    482 	FTSENT *entry;
    483 {
    484 	COMPARE(entry->fts_statb.st_nlink, plan->l_data);
    485 }
    486 
    487 PLAN *
    488 c_links(arg)
    489 	char *arg;
    490 {
    491 	PLAN *new;
    492 
    493 	ftsoptions &= ~FTS_NOSTAT;
    494 
    495 	new = palloc(N_LINKS, f_links);
    496 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, (char *)NULL);
    497 	return(new);
    498 }
    499 
    500 /*
    501  * -ls functions --
    502  *
    503  *	Always true - prints the current entry to stdout in "ls" format.
    504  */
    505 /* ARGSUSED */
    506 f_ls(plan, entry)
    507 	PLAN *plan;
    508 	FTSENT *entry;
    509 {
    510 	printlong(entry->fts_path, entry->fts_accpath, &entry->fts_statb);
    511 	return(1);
    512 }
    513 
    514 PLAN *
    515 c_ls()
    516 {
    517 	ftsoptions &= ~FTS_NOSTAT;
    518 	isoutput = 1;
    519 
    520 	return(palloc(N_LS, f_ls));
    521 }
    522 
    523 /*
    524  * -name functions --
    525  *
    526  *	True if the basename of the filename being examined
    527  *	matches pattern using Pattern Matching Notation S3.14
    528  */
    529 f_name(plan, entry)
    530 	PLAN *plan;
    531 	FTSENT *entry;
    532 {
    533 	return(fnmatch(plan->c_data, entry->fts_name, FNM_QUOTE));
    534 }
    535 
    536 PLAN *
    537 c_name(pattern)
    538 	char *pattern;
    539 {
    540 	PLAN *new;
    541 
    542 	new = palloc(N_NAME, f_name);
    543 	new->c_data = pattern;
    544 	return(new);
    545 }
    546 
    547 /*
    548  * -newer file functions --
    549  *
    550  *	True if the current file has been modified more recently
    551  *	then the modification time of the file named by the pathname
    552  *	file.
    553  */
    554 f_newer(plan, entry)
    555 	PLAN *plan;
    556 	FTSENT *entry;
    557 {
    558 	return(entry->fts_statb.st_mtime > plan->t_data);
    559 }
    560 
    561 PLAN *
    562 c_newer(filename)
    563 	char *filename;
    564 {
    565 	PLAN *new;
    566 	struct stat sb;
    567 
    568 	ftsoptions &= ~FTS_NOSTAT;
    569 
    570 	if (stat(filename, &sb))
    571 		err("%s: %s", filename, strerror(errno));
    572 	new = palloc(N_NEWER, f_newer);
    573 	new->t_data = sb.st_mtime;
    574 	return(new);
    575 }
    576 
    577 /*
    578  * -nogroup functions --
    579  *
    580  *	True if file belongs to a user ID for which the equivalent
    581  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
    582  */
    583 /* ARGSUSED */
    584 f_nogroup(plan, entry)
    585 	PLAN *plan;
    586 	FTSENT *entry;
    587 {
    588 	char *group_from_gid();
    589 
    590 	return(group_from_gid(entry->fts_statb.st_gid, 1) ? 1 : 0);
    591 }
    592 
    593 PLAN *
    594 c_nogroup()
    595 {
    596 	ftsoptions &= ~FTS_NOSTAT;
    597 
    598 	return(palloc(N_NOGROUP, f_nogroup));
    599 }
    600 
    601 /*
    602  * -nouser functions --
    603  *
    604  *	True if file belongs to a user ID for which the equivalent
    605  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
    606  */
    607 /* ARGSUSED */
    608 f_nouser(plan, entry)
    609 	PLAN *plan;
    610 	FTSENT *entry;
    611 {
    612 	char *user_from_uid();
    613 
    614 	return(user_from_uid(entry->fts_statb.st_uid, 1) ? 1 : 0);
    615 }
    616 
    617 PLAN *
    618 c_nouser()
    619 {
    620 	ftsoptions &= ~FTS_NOSTAT;
    621 
    622 	return(palloc(N_NOUSER, f_nouser));
    623 }
    624 
    625 /*
    626  * -perm functions --
    627  *
    628  *	The mode argument is used to represent file mode bits.  If it starts
    629  *	with a leading digit, it's treated as an octal mode, otherwise as a
    630  *	symbolic mode.
    631  */
    632 f_perm(plan, entry)
    633 	PLAN *plan;
    634 	FTSENT *entry;
    635 {
    636 	mode_t mode;
    637 
    638 	mode = entry->fts_statb.st_mode &
    639 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
    640 	if (plan->flags)
    641 		return((plan->m_data | mode) == mode);
    642 	else
    643 		return(mode == plan->m_data);
    644 	/* NOTREACHED */
    645 }
    646 
    647 PLAN *
    648 c_perm(perm)
    649 	char *perm;
    650 {
    651 	PLAN *new;
    652 	mode_t *set;
    653 
    654 	ftsoptions &= ~FTS_NOSTAT;
    655 
    656 	new = palloc(N_PERM, f_perm);
    657 
    658 	if (*perm == '-') {
    659 		new->flags = 1;
    660 		++perm;
    661 	}
    662 
    663 	if ((set = setmode(perm)) == NULL)
    664 		err("%s: %s", "-perm", "illegal mode string");
    665 
    666 	new->m_data = getmode(set, 0);
    667 	return(new);
    668 }
    669 
    670 /*
    671  * -print functions --
    672  *
    673  *	Always true, causes the current pathame to be written to
    674  *	standard output.
    675  */
    676 /* ARGSUSED */
    677 f_print(plan, entry)
    678 	PLAN *plan;
    679 	FTSENT *entry;
    680 {
    681 	(void)printf("%s\n", entry->fts_path);
    682 	return(1);
    683 }
    684 
    685 PLAN *
    686 c_print()
    687 {
    688 	isoutput = 1;
    689 
    690 	return(palloc(N_PRINT, f_print));
    691 }
    692 
    693 /*
    694  * -prune functions --
    695  *
    696  *	Prune a portion of the hierarchy.
    697  */
    698 /* ARGSUSED */
    699 f_prune(plan, entry)
    700 	PLAN *plan;
    701 	FTSENT *entry;
    702 {
    703 	extern FTS *tree;
    704 
    705 	if (fts_set(tree, entry, FTS_SKIP))
    706 		err("%s: %s", entry->fts_path, strerror(errno));
    707 	return(1);
    708 }
    709 
    710 PLAN *
    711 c_prune()
    712 {
    713 	return(palloc(N_PRUNE, f_prune));
    714 }
    715 
    716 /*
    717  * -size n[c] functions --
    718  *
    719  *	True if the file size in bytes, divided by an implementation defined
    720  *	value and rounded up to the next integer, is n.  If n is followed by
    721  *	a c, the size is in bytes.
    722  */
    723 #define	FIND_SIZE	512
    724 static int divsize = 1;
    725 
    726 f_size(plan, entry)
    727 	PLAN *plan;
    728 	FTSENT *entry;
    729 {
    730 	off_t size;
    731 
    732 	size = divsize ? (entry->fts_statb.st_size + FIND_SIZE - 1) /
    733 	    FIND_SIZE : entry->fts_statb.st_size;
    734 	COMPARE(size, plan->o_data);
    735 }
    736 
    737 PLAN *
    738 c_size(arg)
    739 	char *arg;
    740 {
    741 	PLAN *new;
    742 	char endch;
    743 
    744 	ftsoptions &= ~FTS_NOSTAT;
    745 
    746 	new = palloc(N_SIZE, f_size);
    747 	new->o_data = find_parsenum(new, "-size", arg, &endch);
    748 	if (endch == 'c')
    749 		divsize = 0;
    750 	return(new);
    751 }
    752 
    753 /*
    754  * -type c functions --
    755  *
    756  *	True if the type of the file is c, where c is b, c, d, p, or f for
    757  *	block special file, character special file, directory, FIFO, or
    758  *	regular file, respectively.
    759  */
    760 f_type(plan, entry)
    761 	PLAN *plan;
    762 	FTSENT *entry;
    763 {
    764 	return((entry->fts_statb.st_mode & S_IFMT) == plan->m_data);
    765 }
    766 
    767 PLAN *
    768 c_type(typestring)
    769 	char *typestring;
    770 {
    771 	PLAN *new;
    772 	mode_t  mask;
    773 
    774 	ftsoptions &= ~FTS_NOSTAT;
    775 
    776 	switch (typestring[0]) {
    777 	case 'b':
    778 		mask = S_IFBLK;
    779 		break;
    780 	case 'c':
    781 		mask = S_IFCHR;
    782 		break;
    783 	case 'd':
    784 		mask = S_IFDIR;
    785 		break;
    786 	case 'f':
    787 		mask = S_IFREG;
    788 		break;
    789 	case 'l':
    790 		mask = S_IFLNK;
    791 		break;
    792 	case 'p':
    793 		mask = S_IFIFO;
    794 		break;
    795 	case 's':
    796 		mask = S_IFSOCK;
    797 		break;
    798 	default:
    799 		err("%s: %s", "-type", "unknown type");
    800 	}
    801 
    802 	new = palloc(N_TYPE, f_type);
    803 	new->m_data = mask;
    804 	return(new);
    805 }
    806 
    807 /*
    808  * -user uname functions --
    809  *
    810  *	True if the file belongs to the user uname.  If uname is numeric and
    811  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
    812  *	return a valid user name, uname is taken as a user ID.
    813  */
    814 f_user(plan, entry)
    815 	PLAN *plan;
    816 	FTSENT *entry;
    817 {
    818 	return(entry->fts_statb.st_uid == plan->u_data);
    819 }
    820 
    821 PLAN *
    822 c_user(username)
    823 	char *username;
    824 {
    825 	PLAN *new;
    826 	struct passwd *p;
    827 	uid_t uid;
    828 
    829 	ftsoptions &= ~FTS_NOSTAT;
    830 
    831 	p = getpwnam(username);
    832 	if (p == NULL) {
    833 		uid = atoi(username);
    834 		if (uid == 0 && username[0] != '0')
    835 			err("%s: %s", "-user", "no such user");
    836 	} else
    837 		uid = p->pw_uid;
    838 
    839 	new = palloc(N_USER, f_user);
    840 	new->u_data = uid;
    841 	return(new);
    842 }
    843 
    844 /*
    845  * -xdev functions --
    846  *
    847  *	Always true, causes find not to decend past directories that have a
    848  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
    849  */
    850 PLAN *
    851 c_xdev()
    852 {
    853 	ftsoptions |= FTS_XDEV;
    854 
    855 	return(palloc(N_XDEV, f_always_true));
    856 }
    857 
    858 /*
    859  * ( expression ) functions --
    860  *
    861  *	True if expression is true.
    862  */
    863 f_expr(plan, entry)
    864 	PLAN *plan;
    865 	FTSENT *entry;
    866 {
    867 	register PLAN *p;
    868 	register int state;
    869 
    870 	for (p = plan->p_data[0];
    871 	    p && (state = (p->eval)(p, entry)); p = p->next);
    872 	return(state);
    873 }
    874 
    875 /*
    876  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
    877  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
    878  * to a N_EXPR node containing the expression and the ')' node is discarded.
    879  */
    880 PLAN *
    881 c_openparen()
    882 {
    883 	return(palloc(N_OPENPAREN, (int (*)())-1));
    884 }
    885 
    886 PLAN *
    887 c_closeparen()
    888 {
    889 	return(palloc(N_CLOSEPAREN, (int (*)())-1));
    890 }
    891 
    892 /*
    893  * -mtime n functions --
    894  *
    895  *	True if the difference between the file modification time and the
    896  *	current time is n 24 hour periods.
    897  */
    898 f_mtime(plan, entry)
    899 	PLAN *plan;
    900 	FTSENT *entry;
    901 {
    902 	extern time_t now;
    903 
    904 	COMPARE((now - entry->fts_statb.st_mtime + SECSPERDAY - 1) /
    905 	    SECSPERDAY, plan->t_data);
    906 }
    907 
    908 PLAN *
    909 c_mtime(arg)
    910 	char *arg;
    911 {
    912 	PLAN *new;
    913 
    914 	ftsoptions &= ~FTS_NOSTAT;
    915 
    916 	new = palloc(N_MTIME, f_mtime);
    917 	new->t_data = find_parsenum(new, "-mtime", arg, (char *)NULL);
    918 	return(new);
    919 }
    920 
    921 /*
    922  * ! expression functions --
    923  *
    924  *	Negation of a primary; the unary NOT operator.
    925  */
    926 f_not(plan, entry)
    927 	PLAN *plan;
    928 	FTSENT *entry;
    929 {
    930 	register PLAN *p;
    931 	register int state;
    932 
    933 	for (p = plan->p_data[0];
    934 	    p && (state = (p->eval)(p, entry)); p = p->next);
    935 	return(!state);
    936 }
    937 
    938 PLAN *
    939 c_not()
    940 {
    941 	return(palloc(N_NOT, f_not));
    942 }
    943 
    944 /*
    945  * expression -o expression functions --
    946  *
    947  *	Alternation of primaries; the OR operator.  The second expression is
    948  * not evaluated if the first expression is true.
    949  */
    950 f_or(plan, entry)
    951 	PLAN *plan;
    952 	FTSENT *entry;
    953 {
    954 	register PLAN *p;
    955 	register int state;
    956 
    957 	for (p = plan->p_data[0];
    958 	    p && (state = (p->eval)(p, entry)); p = p->next);
    959 
    960 	if (state)
    961 		return(1);
    962 
    963 	for (p = plan->p_data[1];
    964 	    p && (state = (p->eval)(p, entry)); p = p->next);
    965 	return(state);
    966 }
    967 
    968 PLAN *
    969 c_or()
    970 {
    971 	return(palloc(N_OR, f_or));
    972 }
    973 
    974 static PLAN *
    975 palloc(t, f)
    976 	enum ntype t;
    977 	int (*f)();
    978 {
    979 	PLAN *new;
    980 
    981 	if (new = malloc(sizeof(PLAN))) {
    982 		new->type = t;
    983 		new->eval = f;
    984 		new->flags = 0;
    985 		new->next = NULL;
    986 		return(new);
    987 	}
    988 	err("%s", strerror(errno));
    989 	/* NOTREACHED */
    990 }
    991