Home | History | Annotate | Line # | Download | only in find
function.c revision 1.42
      1 /*	$NetBSD: function.c,v 1.42 2003/01/26 07:07:31 matt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Cimarron D. Taylor of the University of California, Berkeley.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "from: @(#)function.c	8.10 (Berkeley) 5/4/95";
     43 #else
     44 __RCSID("$NetBSD: function.c,v 1.42 2003/01/26 07:07:31 matt Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 #include <sys/param.h>
     49 #include <sys/stat.h>
     50 #include <sys/wait.h>
     51 #include <sys/mount.h>
     52 
     53 #include <dirent.h>
     54 #include <err.h>
     55 #include <errno.h>
     56 #include <fnmatch.h>
     57 #include <fts.h>
     58 #include <grp.h>
     59 #include <inttypes.h>
     60 #include <pwd.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <tzfile.h>
     65 #include <unistd.h>
     66 
     67 #include "find.h"
     68 #include "stat_flags.h"
     69 
     70 #define	COMPARE(a, b) {							\
     71 	switch (plan->flags) {						\
     72 	case F_EQUAL:							\
     73 		return (a == b);					\
     74 	case F_LESSTHAN:						\
     75 		return (a < b);						\
     76 	case F_GREATER:							\
     77 		return (a > b);						\
     78 	default:							\
     79 		abort();						\
     80 	}								\
     81 }
     82 
     83 static	int64_t	find_parsenum __P((PLAN *, char *, char *, char *));
     84 	int	f_always_true __P((PLAN *, FTSENT *));
     85 	int	f_amin __P((PLAN *, FTSENT *));
     86 	int	f_anewer __P((PLAN *, FTSENT *));
     87 	int	f_atime __P((PLAN *, FTSENT *));
     88 	int	f_cmin __P((PLAN *, FTSENT *));
     89 	int	f_cnewer __P((PLAN *, FTSENT *));
     90 	int	f_ctime __P((PLAN *, FTSENT *));
     91 	int	f_empty __P((PLAN *, FTSENT *));
     92 	int	f_exec __P((PLAN *, FTSENT *));
     93 	int	f_execdir __P((PLAN *, FTSENT *));
     94 	int	f_flags __P((PLAN *, FTSENT *));
     95 	int	f_fstype __P((PLAN *, FTSENT *));
     96 	int	f_group __P((PLAN *, FTSENT *));
     97 	int	f_inum __P((PLAN *, FTSENT *));
     98 	int	f_links __P((PLAN *, FTSENT *));
     99 	int	f_ls __P((PLAN *, FTSENT *));
    100 	int	f_mindepth __P((PLAN *, FTSENT *));
    101 	int	f_maxdepth __P((PLAN *, FTSENT *));
    102 	int	f_mmin __P((PLAN *, FTSENT *));
    103 	int	f_mtime __P((PLAN *, FTSENT *));
    104 	int	f_name __P((PLAN *, FTSENT *));
    105 	int	f_newer __P((PLAN *, FTSENT *));
    106 	int	f_nogroup __P((PLAN *, FTSENT *));
    107 	int	f_nouser __P((PLAN *, FTSENT *));
    108 	int	f_path __P((PLAN *, FTSENT *));
    109 	int	f_perm __P((PLAN *, FTSENT *));
    110 	int	f_print __P((PLAN *, FTSENT *));
    111 	int	f_print0 __P((PLAN *, FTSENT *));
    112 	int	f_printx __P((PLAN *, FTSENT *));
    113 	int	f_prune __P((PLAN *, FTSENT *));
    114 	int	f_regex __P((PLAN *, FTSENT *));
    115 	int	f_size __P((PLAN *, FTSENT *));
    116 	int	f_type __P((PLAN *, FTSENT *));
    117 	int	f_user __P((PLAN *, FTSENT *));
    118 	int	f_not __P((PLAN *, FTSENT *));
    119 	int	f_or __P((PLAN *, FTSENT *));
    120 static	PLAN   *c_regex_common __P((char ***, int, enum ntype, int));
    121 static	PLAN   *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
    122 
    123 extern int dotfd;
    124 extern FTS *tree;
    125 extern time_t now;
    126 
    127 /*
    128  * find_parsenum --
    129  *	Parse a string of the form [+-]# and return the value.
    130  */
    131 static int64_t
    132 find_parsenum(plan, option, vp, endch)
    133 	PLAN *plan;
    134 	char *option, *vp, *endch;
    135 {
    136 	int64_t value;
    137 	char *endchar, *str;	/* Pointer to character ending conversion. */
    138 
    139 	/* Determine comparison from leading + or -. */
    140 	str = vp;
    141 	switch (*str) {
    142 	case '+':
    143 		++str;
    144 		plan->flags = F_GREATER;
    145 		break;
    146 	case '-':
    147 		++str;
    148 		plan->flags = F_LESSTHAN;
    149 		break;
    150 	default:
    151 		plan->flags = F_EQUAL;
    152 		break;
    153 	}
    154 
    155 	/*
    156 	 * Convert the string with strtol().  Note, if strtol() returns zero
    157 	 * and endchar points to the beginning of the string we know we have
    158 	 * a syntax error.
    159 	 */
    160 	value = strtoq(str, &endchar, 10);
    161 	if (value == 0 && endchar == str)
    162 		errx(1, "%s: %s: illegal numeric value", option, vp);
    163 	if (endchar[0] && (endch == NULL || endchar[0] != *endch))
    164 		errx(1, "%s: %s: illegal trailing character", option, vp);
    165 	if (endch)
    166 		*endch = endchar[0];
    167 	return (value);
    168 }
    169 
    170 /*
    171  * The value of n for the inode times (atime, ctime, and mtime) is a range,
    172  * i.e. n matches from (n - 1) to n 24 hour periods.  This interacts with
    173  * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
    174  * user wanted.  Correct so that -1 is "less than 1".
    175  */
    176 #define	TIME_CORRECT(p, ttype)						\
    177 	if ((p)->type == ttype && (p)->flags == F_LESSTHAN)		\
    178 		++((p)->t_data);
    179 
    180 /*
    181  * -amin n functions --
    182  *
    183  *	True if the difference between the file access time and the
    184  *	current time is n 1 minute periods.
    185  */
    186 int
    187 f_amin(plan, entry)
    188 	PLAN *plan;
    189 	FTSENT *entry;
    190 {
    191 	COMPARE((now - entry->fts_statp->st_atime +
    192 	    SECSPERMIN - 1) / SECSPERMIN, plan->t_data);
    193 }
    194 
    195 PLAN *
    196 c_amin(argvp, isok)
    197 	char ***argvp;
    198 	int isok;
    199 {
    200 	char *arg = **argvp;
    201 	PLAN *new;
    202 
    203 	(*argvp)++;
    204 	ftsoptions &= ~FTS_NOSTAT;
    205 
    206 	new = palloc(N_AMIN, f_amin);
    207 	new->t_data = find_parsenum(new, "-amin", arg, NULL);
    208 	TIME_CORRECT(new, N_AMIN);
    209 	return (new);
    210 }
    211 
    212 /*
    213  * -anewer file functions --
    214  *
    215  *	True if the current file has been accessed more recently
    216  *	than the access time of the file named by the pathname
    217  *	file.
    218  */
    219 int
    220 f_anewer(plan, entry)
    221 	PLAN *plan;
    222 	FTSENT *entry;
    223 {
    224 
    225 	return (entry->fts_statp->st_atime > plan->t_data);
    226 }
    227 
    228 PLAN *
    229 c_anewer(argvp, isok)
    230 	char ***argvp;
    231 	int isok;
    232 {
    233 	char *filename = **argvp;
    234 	PLAN *new;
    235 	struct stat sb;
    236 
    237 	(*argvp)++;
    238 	ftsoptions &= ~FTS_NOSTAT;
    239 
    240 	if (stat(filename, &sb))
    241 		err(1, "%s", filename);
    242 	new = palloc(N_ANEWER, f_anewer);
    243 	new->t_data = sb.st_atime;
    244 	return (new);
    245 }
    246 
    247 /*
    248  * -atime n functions --
    249  *
    250  *	True if the difference between the file access time and the
    251  *	current time is n 24 hour periods.
    252  */
    253 int
    254 f_atime(plan, entry)
    255 	PLAN *plan;
    256 	FTSENT *entry;
    257 {
    258 	COMPARE((now - entry->fts_statp->st_atime +
    259 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
    260 }
    261 
    262 PLAN *
    263 c_atime(argvp, isok)
    264 	char ***argvp;
    265 	int isok;
    266 {
    267 	char *arg = **argvp;
    268 	PLAN *new;
    269 
    270 	(*argvp)++;
    271 	ftsoptions &= ~FTS_NOSTAT;
    272 
    273 	new = palloc(N_ATIME, f_atime);
    274 	new->t_data = find_parsenum(new, "-atime", arg, NULL);
    275 	TIME_CORRECT(new, N_ATIME);
    276 	return (new);
    277 }
    278 /*
    279  * -cmin n functions --
    280  *
    281  *	True if the difference between the last change of file
    282  *	status information and the current time is n 24 hour periods.
    283  */
    284 int
    285 f_cmin(plan, entry)
    286 	PLAN *plan;
    287 	FTSENT *entry;
    288 {
    289 	COMPARE((now - entry->fts_statp->st_ctime +
    290 	    SECSPERMIN - 1) / SECSPERMIN, plan->t_data);
    291 }
    292 
    293 PLAN *
    294 c_cmin(argvp, isok)
    295 	char ***argvp;
    296 	int isok;
    297 {
    298 	char *arg = **argvp;
    299 	PLAN *new;
    300 
    301 	(*argvp)++;
    302 	ftsoptions &= ~FTS_NOSTAT;
    303 
    304 	new = palloc(N_CMIN, f_cmin);
    305 	new->t_data = find_parsenum(new, "-cmin", arg, NULL);
    306 	TIME_CORRECT(new, N_CMIN);
    307 	return (new);
    308 }
    309 
    310 /*
    311  * -cnewer file functions --
    312  *
    313  *	True if the current file has been changed more recently
    314  *	than the changed time of the file named by the pathname
    315  *	file.
    316  */
    317 int
    318 f_cnewer(plan, entry)
    319 	PLAN *plan;
    320 	FTSENT *entry;
    321 {
    322 
    323 	return (entry->fts_statp->st_ctime > plan->t_data);
    324 }
    325 
    326 PLAN *
    327 c_cnewer(argvp, isok)
    328 	char ***argvp;
    329 	int isok;
    330 {
    331 	char *filename = **argvp;
    332 	PLAN *new;
    333 	struct stat sb;
    334 
    335 	(*argvp)++;
    336 	ftsoptions &= ~FTS_NOSTAT;
    337 
    338 	if (stat(filename, &sb))
    339 		err(1, "%s", filename);
    340 	new = palloc(N_CNEWER, f_cnewer);
    341 	new->t_data = sb.st_ctime;
    342 	return (new);
    343 }
    344 
    345 /*
    346  * -ctime n functions --
    347  *
    348  *	True if the difference between the last change of file
    349  *	status information and the current time is n 24 hour periods.
    350  */
    351 int
    352 f_ctime(plan, entry)
    353 	PLAN *plan;
    354 	FTSENT *entry;
    355 {
    356 	COMPARE((now - entry->fts_statp->st_ctime +
    357 	    SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
    358 }
    359 
    360 PLAN *
    361 c_ctime(argvp, isok)
    362 	char ***argvp;
    363 	int isok;
    364 {
    365 	char *arg = **argvp;
    366 	PLAN *new;
    367 
    368 	(*argvp)++;
    369 	ftsoptions &= ~FTS_NOSTAT;
    370 
    371 	new = palloc(N_CTIME, f_ctime);
    372 	new->t_data = find_parsenum(new, "-ctime", arg, NULL);
    373 	TIME_CORRECT(new, N_CTIME);
    374 	return (new);
    375 }
    376 
    377 /*
    378  * -depth functions --
    379  *
    380  *	Always true, causes descent of the directory hierarchy to be done
    381  *	so that all entries in a directory are acted on before the directory
    382  *	itself.
    383  */
    384 int
    385 f_always_true(plan, entry)
    386 	PLAN *plan;
    387 	FTSENT *entry;
    388 {
    389 
    390 	return (1);
    391 }
    392 
    393 PLAN *
    394 c_depth(argvp, isok)
    395 	char ***argvp;
    396 	int isok;
    397 {
    398 	isdepth = 1;
    399 
    400 	return (palloc(N_DEPTH, f_always_true));
    401 }
    402 
    403 /*
    404  * -empty functions --
    405  *
    406  *	True if the file or directory is empty
    407  */
    408 int
    409 f_empty(plan, entry)
    410 	PLAN *plan;
    411 	FTSENT *entry;
    412 {
    413 	if (S_ISREG(entry->fts_statp->st_mode) &&
    414 	    entry->fts_statp->st_size == 0)
    415 		return (1);
    416 	if (S_ISDIR(entry->fts_statp->st_mode)) {
    417 		struct dirent *dp;
    418 		int empty;
    419 		DIR *dir;
    420 
    421 		empty = 1;
    422 		dir = opendir(entry->fts_accpath);
    423 		if (dir == NULL)
    424 			err(1, "%s", entry->fts_accpath);
    425 		for (dp = readdir(dir); dp; dp = readdir(dir))
    426 			if (dp->d_name[0] != '.' ||
    427 			    (dp->d_name[1] != '\0' &&
    428 				(dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
    429 				empty = 0;
    430 				break;
    431 			}
    432 		closedir(dir);
    433 		return (empty);
    434 	}
    435 	return (0);
    436 }
    437 
    438 PLAN *
    439 c_empty(argvp, isok)
    440 	char ***argvp;
    441 	int isok;
    442 {
    443 	ftsoptions &= ~FTS_NOSTAT;
    444 
    445 	return (palloc(N_EMPTY, f_empty));
    446 }
    447 
    448 /*
    449  * [-exec | -ok] utility [arg ... ] ; functions --
    450  *
    451  *	True if the executed utility returns a zero value as exit status.
    452  *	The end of the primary expression is delimited by a semicolon.  If
    453  *	"{}" occurs anywhere, it gets replaced by the current pathname.
    454  *	The current directory for the execution of utility is the same as
    455  *	the current directory when the find utility was started.
    456  *
    457  *	The primary -ok is different in that it requests affirmation of the
    458  *	user before executing the utility.
    459  */
    460 int
    461 f_exec(plan, entry)
    462 	PLAN *plan;
    463 	FTSENT *entry;
    464 {
    465 	int cnt;
    466 	pid_t pid;
    467 	int status;
    468 
    469 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
    470 		if (plan->e_len[cnt])
    471 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
    472 			    entry->fts_path, &plan->e_len[cnt]);
    473 
    474 	if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
    475 		return (0);
    476 
    477 	/* don't mix output of command with find output */
    478 	fflush(stdout);
    479 	fflush(stderr);
    480 
    481 	switch (pid = vfork()) {
    482 	case -1:
    483 		err(1, "vfork");
    484 		/* NOTREACHED */
    485 	case 0:
    486 		if (fchdir(dotfd)) {
    487 			warn("chdir");
    488 			_exit(1);
    489 		}
    490 		execvp(plan->e_argv[0], plan->e_argv);
    491 		warn("%s", plan->e_argv[0]);
    492 		_exit(1);
    493 	}
    494 	pid = waitpid(pid, &status, 0);
    495 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
    496 }
    497 
    498 /*
    499  * c_exec --
    500  *	build three parallel arrays, one with pointers to the strings passed
    501  *	on the command line, one with (possibly duplicated) pointers to the
    502  *	argv array, and one with integer values that are lengths of the
    503  *	strings, but also flags meaning that the string has to be massaged.
    504  */
    505 PLAN *
    506 c_exec(argvp, isok)
    507 	char ***argvp;
    508 	int isok;
    509 {
    510 	PLAN *new;			/* node returned */
    511 	int cnt;
    512 	char **argv, **ap, *p;
    513 
    514 	isoutput = 1;
    515 
    516 	new = palloc(N_EXEC, f_exec);
    517 	if (isok)
    518 		new->flags = F_NEEDOK;
    519 
    520 	for (ap = argv = *argvp;; ++ap) {
    521 		if (!*ap)
    522 			errx(1,
    523 			    "%s: no terminating \";\"", isok ? "-ok" : "-exec");
    524 		if (**ap == ';')
    525 			break;
    526 	}
    527 
    528 	cnt = ap - *argvp + 1;
    529 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
    530 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
    531 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
    532 
    533 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
    534 		new->e_orig[cnt] = *argv;
    535 		for (p = *argv; *p; ++p)
    536 			if (p[0] == '{' && p[1] == '}') {
    537 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
    538 				new->e_len[cnt] = MAXPATHLEN;
    539 				break;
    540 			}
    541 		if (!*p) {
    542 			new->e_argv[cnt] = *argv;
    543 			new->e_len[cnt] = 0;
    544 		}
    545 	}
    546 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
    547 
    548 	*argvp = argv + 1;
    549 	return (new);
    550 }
    551 
    552 /*
    553  * -execdir utility [arg ... ] ; functions --
    554  *
    555  *	True if the executed utility returns a zero value as exit status.
    556  *	The end of the primary expression is delimited by a semicolon.  If
    557  *	"{}" occurs anywhere, it gets replaced by the unqualified pathname.
    558  *	The current directory for the execution of utility is the same as
    559  *	the directory where the file lives.
    560  */
    561 int
    562 f_execdir(plan, entry)
    563 	PLAN *plan;
    564 	FTSENT *entry;
    565 {
    566 	int cnt;
    567 	pid_t pid;
    568 	int status;
    569 	char *file;
    570 
    571 	/* XXX - if file/dir ends in '/' this will not work -- can it? */
    572 	if ((file = strrchr(entry->fts_path, '/')))
    573 		file++;
    574 	else
    575 		file = entry->fts_path;
    576 
    577 	for (cnt = 0; plan->e_argv[cnt]; ++cnt)
    578 		if (plan->e_len[cnt])
    579 			brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
    580 			    file, &plan->e_len[cnt]);
    581 
    582 	/* don't mix output of command with find output */
    583 	fflush(stdout);
    584 	fflush(stderr);
    585 
    586 	switch (pid = vfork()) {
    587 	case -1:
    588 		err(1, "fork");
    589 		/* NOTREACHED */
    590 	case 0:
    591 		execvp(plan->e_argv[0], plan->e_argv);
    592 		warn("%s", plan->e_argv[0]);
    593 		_exit(1);
    594 	}
    595 	pid = waitpid(pid, &status, 0);
    596 	return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
    597 }
    598 
    599 /*
    600  * c_execdir --
    601  *	build three parallel arrays, one with pointers to the strings passed
    602  *	on the command line, one with (possibly duplicated) pointers to the
    603  *	argv array, and one with integer values that are lengths of the
    604  *	strings, but also flags meaning that the string has to be massaged.
    605  */
    606 PLAN *
    607 c_execdir(argvp, isok)
    608 	char ***argvp;
    609 	int isok;
    610 {
    611 	PLAN *new;			/* node returned */
    612 	int cnt;
    613 	char **argv, **ap, *p;
    614 
    615 	ftsoptions &= ~FTS_NOSTAT;
    616 	isoutput = 1;
    617 
    618 	new = palloc(N_EXECDIR, f_execdir);
    619 
    620 	for (ap = argv = *argvp;; ++ap) {
    621 		if (!*ap)
    622 			errx(1,
    623 			    "-execdir: no terminating \";\"");
    624 		if (**ap == ';')
    625 			break;
    626 	}
    627 
    628 	cnt = ap - *argvp + 1;
    629 	new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
    630 	new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
    631 	new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
    632 
    633 	for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
    634 		new->e_orig[cnt] = *argv;
    635 		for (p = *argv; *p; ++p)
    636 			if (p[0] == '{' && p[1] == '}') {
    637 				new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
    638 				new->e_len[cnt] = MAXPATHLEN;
    639 				break;
    640 			}
    641 		if (!*p) {
    642 			new->e_argv[cnt] = *argv;
    643 			new->e_len[cnt] = 0;
    644 		}
    645 	}
    646 	new->e_argv[cnt] = new->e_orig[cnt] = NULL;
    647 
    648 	*argvp = argv + 1;
    649 	return (new);
    650 }
    651 
    652 /*
    653  * -flags [-]flags functions --
    654  */
    655 int
    656 f_flags(plan, entry)
    657 	PLAN *plan;
    658 	FTSENT *entry;
    659 {
    660 	u_int32_t flags;
    661 
    662 	flags = entry->fts_statp->st_flags;
    663 	if (plan->flags == F_ATLEAST)
    664 		return ((plan->f_data | flags) == flags);
    665 	else
    666 		return (flags == plan->f_data);
    667 	/* NOTREACHED */
    668 }
    669 
    670 PLAN *
    671 c_flags(argvp, isok)
    672 	char ***argvp;
    673 	int isok;
    674 {
    675 	char *flags = **argvp;
    676 	PLAN *new;
    677 	u_long flagset;
    678 
    679 	(*argvp)++;
    680 	ftsoptions &= ~FTS_NOSTAT;
    681 
    682 	new = palloc(N_FLAGS, f_flags);
    683 
    684 	if (*flags == '-') {
    685 		new->flags = F_ATLEAST;
    686 		++flags;
    687 	}
    688 
    689 	flagset = 0;
    690 	if ((strcmp(flags, "none") != 0) &&
    691 	    (string_to_flags(&flags, &flagset, NULL) != 0))
    692 		errx(1, "-flags: %s: illegal flags string", flags);
    693 	new->f_data = flagset;
    694 	return (new);
    695 }
    696 
    697 /*
    698  * -follow functions --
    699  *
    700  *	Always true, causes symbolic links to be followed on a global
    701  *	basis.
    702  */
    703 PLAN *
    704 c_follow(argvp, isok)
    705 	char ***argvp;
    706 	int isok;
    707 {
    708 	ftsoptions &= ~FTS_PHYSICAL;
    709 	ftsoptions |= FTS_LOGICAL;
    710 
    711 	return (palloc(N_FOLLOW, f_always_true));
    712 }
    713 
    714 /*
    715  * -fstype functions --
    716  *
    717  *	True if the file is of a certain type.
    718  */
    719 int
    720 f_fstype(plan, entry)
    721 	PLAN *plan;
    722 	FTSENT *entry;
    723 {
    724 	static dev_t curdev;	/* need a guaranteed illegal dev value */
    725 	static int first = 1;
    726 	struct statfs sb;
    727 	static short val;
    728 	static char fstype[MFSNAMELEN];
    729 	char *p, save[2];
    730 
    731 	/* Only check when we cross mount point. */
    732 	if (first || curdev != entry->fts_statp->st_dev) {
    733 		curdev = entry->fts_statp->st_dev;
    734 
    735 		/*
    736 		 * Statfs follows symlinks; find wants the link's file system,
    737 		 * not where it points.
    738 		 */
    739 		if (entry->fts_info == FTS_SL ||
    740 		    entry->fts_info == FTS_SLNONE) {
    741 			if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
    742 				++p;
    743 			else
    744 				p = entry->fts_accpath;
    745 			save[0] = p[0];
    746 			p[0] = '.';
    747 			save[1] = p[1];
    748 			p[1] = '\0';
    749 
    750 		} else
    751 			p = NULL;
    752 
    753 		if (statfs(entry->fts_accpath, &sb))
    754 			err(1, "%s", entry->fts_accpath);
    755 
    756 		if (p) {
    757 			p[0] = save[0];
    758 			p[1] = save[1];
    759 		}
    760 
    761 		first = 0;
    762 
    763 		/*
    764 		 * Further tests may need both of these values, so
    765 		 * always copy both of them.
    766 		 */
    767 		val = sb.f_flags;
    768 		strncpy(fstype, sb.f_fstypename, MFSNAMELEN);
    769 	}
    770 	switch (plan->flags) {
    771 	case F_MTFLAG:
    772 		return (val & plan->mt_data);
    773 	case F_MTTYPE:
    774 		return (strncmp(fstype, plan->c_data, MFSNAMELEN) == 0);
    775 	default:
    776 		abort();
    777 	}
    778 }
    779 
    780 PLAN *
    781 c_fstype(argvp, isok)
    782 	char ***argvp;
    783 	int isok;
    784 {
    785 	char *arg = **argvp;
    786 	PLAN *new;
    787 
    788 	(*argvp)++;
    789 	ftsoptions &= ~FTS_NOSTAT;
    790 
    791 	new = palloc(N_FSTYPE, f_fstype);
    792 
    793 	switch (*arg) {
    794 	case 'l':
    795 		if (!strcmp(arg, "local")) {
    796 			new->flags = F_MTFLAG;
    797 			new->mt_data = MNT_LOCAL;
    798 			return (new);
    799 		}
    800 		break;
    801 	case 'r':
    802 		if (!strcmp(arg, "rdonly")) {
    803 			new->flags = F_MTFLAG;
    804 			new->mt_data = MNT_RDONLY;
    805 			return (new);
    806 		}
    807 		break;
    808 	}
    809 
    810 	new->flags = F_MTTYPE;
    811 	new->c_data = arg;
    812 	return (new);
    813 }
    814 
    815 /*
    816  * -group gname functions --
    817  *
    818  *	True if the file belongs to the group gname.  If gname is numeric and
    819  *	an equivalent of the getgrnam() function does not return a valid group
    820  *	name, gname is taken as a group ID.
    821  */
    822 int
    823 f_group(plan, entry)
    824 	PLAN *plan;
    825 	FTSENT *entry;
    826 {
    827 
    828 	return (entry->fts_statp->st_gid == plan->g_data);
    829 }
    830 
    831 PLAN *
    832 c_group(argvp, isok)
    833 	char ***argvp;
    834 	int isok;
    835 {
    836 	char *gname = **argvp;
    837 	PLAN *new;
    838 	struct group *g;
    839 	gid_t gid;
    840 
    841 	(*argvp)++;
    842 	ftsoptions &= ~FTS_NOSTAT;
    843 
    844 	g = getgrnam(gname);
    845 	if (g == NULL) {
    846 		gid = atoi(gname);
    847 		if (gid == 0 && gname[0] != '0')
    848 			errx(1, "-group: %s: no such group", gname);
    849 	} else
    850 		gid = g->gr_gid;
    851 
    852 	new = palloc(N_GROUP, f_group);
    853 	new->g_data = gid;
    854 	return (new);
    855 }
    856 
    857 /*
    858  * -inum n functions --
    859  *
    860  *	True if the file has inode # n.
    861  */
    862 int
    863 f_inum(plan, entry)
    864 	PLAN *plan;
    865 	FTSENT *entry;
    866 {
    867 
    868 	COMPARE(entry->fts_statp->st_ino, plan->i_data);
    869 }
    870 
    871 PLAN *
    872 c_inum(argvp, isok)
    873 	char ***argvp;
    874 	int isok;
    875 {
    876 	char *arg = **argvp;
    877 	PLAN *new;
    878 
    879 	(*argvp)++;
    880 	ftsoptions &= ~FTS_NOSTAT;
    881 
    882 	new = palloc(N_INUM, f_inum);
    883 	new->i_data = find_parsenum(new, "-inum", arg, NULL);
    884 	return (new);
    885 }
    886 
    887 /*
    888  * -links n functions --
    889  *
    890  *	True if the file has n links.
    891  */
    892 int
    893 f_links(plan, entry)
    894 	PLAN *plan;
    895 	FTSENT *entry;
    896 {
    897 
    898 	COMPARE(entry->fts_statp->st_nlink, plan->l_data);
    899 }
    900 
    901 PLAN *
    902 c_links(argvp, isok)
    903 	char ***argvp;
    904 	int isok;
    905 {
    906 	char *arg = **argvp;
    907 	PLAN *new;
    908 
    909 	(*argvp)++;
    910 	ftsoptions &= ~FTS_NOSTAT;
    911 
    912 	new = palloc(N_LINKS, f_links);
    913 	new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL);
    914 	return (new);
    915 }
    916 
    917 /*
    918  * -ls functions --
    919  *
    920  *	Always true - prints the current entry to stdout in "ls" format.
    921  */
    922 int
    923 f_ls(plan, entry)
    924 	PLAN *plan;
    925 	FTSENT *entry;
    926 {
    927 
    928 	printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
    929 	return (1);
    930 }
    931 
    932 PLAN *
    933 c_ls(argvp, isok)
    934 	char ***argvp;
    935 	int isok;
    936 {
    937 
    938 	ftsoptions &= ~FTS_NOSTAT;
    939 	isoutput = 1;
    940 
    941 	return (palloc(N_LS, f_ls));
    942 }
    943 
    944 /*
    945  * - maxdepth n functions --
    946  *
    947  *	True if the current search depth is less than or equal to the
    948  *	maximum depth specified
    949  */
    950 int
    951 f_maxdepth(plan, entry)
    952 	PLAN *plan;
    953 	FTSENT *entry;
    954 {
    955 	extern FTS *tree;
    956 
    957 	if (entry->fts_level >= plan->max_data)
    958 		fts_set(tree, entry, FTS_SKIP);
    959 	return (entry->fts_level <= plan->max_data);
    960 }
    961 
    962 PLAN *
    963 c_maxdepth(argvp, isok)
    964 	char ***argvp;
    965 	int isok;
    966 {
    967 	char *arg = **argvp;
    968 	PLAN *new;
    969 
    970 	(*argvp)++;
    971 	new = palloc(N_MAXDEPTH, f_maxdepth);
    972 	new->max_data = atoi(arg);
    973 	return (new);
    974 }
    975 
    976 /*
    977  * - mindepth n functions --
    978  *
    979  *	True if the current search depth is greater than or equal to the
    980  *	minimum depth specified
    981  */
    982 int
    983 f_mindepth(plan, entry)
    984 	PLAN *plan;
    985 	FTSENT *entry;
    986 {
    987 	return (entry->fts_level >= plan->min_data);
    988 }
    989 
    990 PLAN *
    991 c_mindepth(argvp, isok)
    992 	char ***argvp;
    993 	int isok;
    994 {
    995 	char *arg = **argvp;
    996 	PLAN *new;
    997 
    998 	(*argvp)++;
    999 	new = palloc(N_MINDEPTH, f_mindepth);
   1000 	new->min_data = atoi(arg);
   1001 	return (new);
   1002 }
   1003 /*
   1004  * -mmin n functions --
   1005  *
   1006  *	True if the difference between the file modification time and the
   1007  *	current time is n 24 hour periods.
   1008  */
   1009 int
   1010 f_mmin(plan, entry)
   1011 	PLAN *plan;
   1012 	FTSENT *entry;
   1013 {
   1014 	COMPARE((now - entry->fts_statp->st_mtime + SECSPERMIN - 1) /
   1015 	    SECSPERMIN, plan->t_data);
   1016 }
   1017 
   1018 PLAN *
   1019 c_mmin(argvp, isok)
   1020 	char ***argvp;
   1021 	int isok;
   1022 {
   1023 	char *arg = **argvp;
   1024 	PLAN *new;
   1025 
   1026 	(*argvp)++;
   1027 	ftsoptions &= ~FTS_NOSTAT;
   1028 
   1029 	new = palloc(N_MMIN, f_mmin);
   1030 	new->t_data = find_parsenum(new, "-mmin", arg, NULL);
   1031 	TIME_CORRECT(new, N_MMIN);
   1032 	return (new);
   1033 }
   1034 /*
   1035  * -mtime n functions --
   1036  *
   1037  *	True if the difference between the file modification time and the
   1038  *	current time is n 24 hour periods.
   1039  */
   1040 int
   1041 f_mtime(plan, entry)
   1042 	PLAN *plan;
   1043 	FTSENT *entry;
   1044 {
   1045 	COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) /
   1046 	    SECSPERDAY, plan->t_data);
   1047 }
   1048 
   1049 PLAN *
   1050 c_mtime(argvp, isok)
   1051 	char ***argvp;
   1052 	int isok;
   1053 {
   1054 	char *arg = **argvp;
   1055 	PLAN *new;
   1056 
   1057 	(*argvp)++;
   1058 	ftsoptions &= ~FTS_NOSTAT;
   1059 
   1060 	new = palloc(N_MTIME, f_mtime);
   1061 	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
   1062 	TIME_CORRECT(new, N_MTIME);
   1063 	return (new);
   1064 }
   1065 
   1066 /*
   1067  * -name functions --
   1068  *
   1069  *	True if the basename of the filename being examined
   1070  *	matches pattern using Pattern Matching Notation S3.14
   1071  */
   1072 int
   1073 f_name(plan, entry)
   1074 	PLAN *plan;
   1075 	FTSENT *entry;
   1076 {
   1077 
   1078 	return (!fnmatch(plan->c_data, entry->fts_name, 0));
   1079 }
   1080 
   1081 PLAN *
   1082 c_name(argvp, isok)
   1083 	char ***argvp;
   1084 	int isok;
   1085 {
   1086 	char *pattern = **argvp;
   1087 	PLAN *new;
   1088 
   1089 	(*argvp)++;
   1090 	new = palloc(N_NAME, f_name);
   1091 	new->c_data = pattern;
   1092 	return (new);
   1093 }
   1094 
   1095 /*
   1096  * -newer file functions --
   1097  *
   1098  *	True if the current file has been modified more recently
   1099  *	than the modification time of the file named by the pathname
   1100  *	file.
   1101  */
   1102 int
   1103 f_newer(plan, entry)
   1104 	PLAN *plan;
   1105 	FTSENT *entry;
   1106 {
   1107 
   1108 	return (entry->fts_statp->st_mtime > plan->t_data);
   1109 }
   1110 
   1111 PLAN *
   1112 c_newer(argvp, isok)
   1113 	char ***argvp;
   1114 	int isok;
   1115 {
   1116 	char *filename = **argvp;
   1117 	PLAN *new;
   1118 	struct stat sb;
   1119 
   1120 	(*argvp)++;
   1121 	ftsoptions &= ~FTS_NOSTAT;
   1122 
   1123 	if (stat(filename, &sb))
   1124 		err(1, "%s", filename);
   1125 	new = palloc(N_NEWER, f_newer);
   1126 	new->t_data = sb.st_mtime;
   1127 	return (new);
   1128 }
   1129 
   1130 /*
   1131  * -nogroup functions --
   1132  *
   1133  *	True if file belongs to a user ID for which the equivalent
   1134  *	of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
   1135  */
   1136 int
   1137 f_nogroup(plan, entry)
   1138 	PLAN *plan;
   1139 	FTSENT *entry;
   1140 {
   1141 
   1142 	return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);
   1143 }
   1144 
   1145 PLAN *
   1146 c_nogroup(argvp, isok)
   1147 	char ***argvp;
   1148 	int isok;
   1149 {
   1150 	ftsoptions &= ~FTS_NOSTAT;
   1151 
   1152 	return (palloc(N_NOGROUP, f_nogroup));
   1153 }
   1154 
   1155 /*
   1156  * -nouser functions --
   1157  *
   1158  *	True if file belongs to a user ID for which the equivalent
   1159  *	of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
   1160  */
   1161 int
   1162 f_nouser(plan, entry)
   1163 	PLAN *plan;
   1164 	FTSENT *entry;
   1165 {
   1166 
   1167 	return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);
   1168 }
   1169 
   1170 PLAN *
   1171 c_nouser(argvp, isok)
   1172 	char ***argvp;
   1173 	int isok;
   1174 {
   1175 	ftsoptions &= ~FTS_NOSTAT;
   1176 
   1177 	return (palloc(N_NOUSER, f_nouser));
   1178 }
   1179 
   1180 /*
   1181  * -path functions --
   1182  *
   1183  *	True if the path of the filename being examined
   1184  *	matches pattern using Pattern Matching Notation S3.14
   1185  */
   1186 int
   1187 f_path(plan, entry)
   1188 	PLAN *plan;
   1189 	FTSENT *entry;
   1190 {
   1191 
   1192 	return (!fnmatch(plan->c_data, entry->fts_path, 0));
   1193 }
   1194 
   1195 PLAN *
   1196 c_path(argvp, isok)
   1197 	char ***argvp;
   1198 	int isok;
   1199 {
   1200 	char *pattern = **argvp;
   1201 	PLAN *new;
   1202 
   1203 	(*argvp)++;
   1204 	new = palloc(N_NAME, f_path);
   1205 	new->c_data = pattern;
   1206 	return (new);
   1207 }
   1208 
   1209 /*
   1210  * -perm functions --
   1211  *
   1212  *	The mode argument is used to represent file mode bits.  If it starts
   1213  *	with a leading digit, it's treated as an octal mode, otherwise as a
   1214  *	symbolic mode.
   1215  */
   1216 int
   1217 f_perm(plan, entry)
   1218 	PLAN *plan;
   1219 	FTSENT *entry;
   1220 {
   1221 	mode_t mode;
   1222 
   1223 	mode = entry->fts_statp->st_mode &
   1224 	    (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
   1225 	if (plan->flags == F_ATLEAST)
   1226 		return ((plan->m_data | mode) == mode);
   1227 	else
   1228 		return (mode == plan->m_data);
   1229 	/* NOTREACHED */
   1230 }
   1231 
   1232 PLAN *
   1233 c_perm(argvp, isok)
   1234 	char ***argvp;
   1235 	int isok;
   1236 {
   1237 	char *perm = **argvp;
   1238 	PLAN *new;
   1239 	mode_t *set;
   1240 
   1241 	(*argvp)++;
   1242 	ftsoptions &= ~FTS_NOSTAT;
   1243 
   1244 	new = palloc(N_PERM, f_perm);
   1245 
   1246 	if (*perm == '-') {
   1247 		new->flags = F_ATLEAST;
   1248 		++perm;
   1249 	}
   1250 
   1251 	if ((set = setmode(perm)) == NULL)
   1252 		err(1, "-perm: %s: illegal mode string", perm);
   1253 
   1254 	new->m_data = getmode(set, 0);
   1255 	free(set);
   1256 	return (new);
   1257 }
   1258 
   1259 /*
   1260  * -print functions --
   1261  *
   1262  *	Always true, causes the current pathame to be written to
   1263  *	standard output.
   1264  */
   1265 int
   1266 f_print(plan, entry)
   1267 	PLAN *plan;
   1268 	FTSENT *entry;
   1269 {
   1270 
   1271 	(void)printf("%s\n", entry->fts_path);
   1272 	return (1);
   1273 }
   1274 
   1275 int
   1276 f_print0(plan, entry)
   1277 	PLAN *plan;
   1278 	FTSENT *entry;
   1279 {
   1280 
   1281 	(void)fputs(entry->fts_path, stdout);
   1282 	(void)fputc('\0', stdout);
   1283 	return (1);
   1284 }
   1285 
   1286 int
   1287 f_printx(plan, entry)
   1288 	PLAN *plan;
   1289 	FTSENT *entry;
   1290 {
   1291 	char *cp;
   1292 
   1293 	for (cp = entry->fts_path; *cp; cp++) {
   1294 		if (*cp == '\'' || *cp == '\"' || *cp == ' ' ||
   1295 		    *cp == '$'  || *cp == '`'  ||
   1296 		    *cp == '\t' || *cp == '\n' || *cp == '\\')
   1297 			fputc('\\', stdout);
   1298 
   1299 		fputc(*cp, stdout);
   1300 	}
   1301 
   1302 	fputc('\n', stdout);
   1303 	return (1);
   1304 }
   1305 
   1306 PLAN *
   1307 c_print(argvp, isok)
   1308 	char ***argvp;
   1309 	int isok;
   1310 {
   1311 
   1312 	isoutput = 1;
   1313 
   1314 	return (palloc(N_PRINT, f_print));
   1315 }
   1316 
   1317 PLAN *
   1318 c_print0(argvp, isok)
   1319 	char ***argvp;
   1320 	int isok;
   1321 {
   1322 
   1323 	isoutput = 1;
   1324 
   1325 	return (palloc(N_PRINT0, f_print0));
   1326 }
   1327 
   1328 PLAN *
   1329 c_printx(argvp, isok)
   1330 	char ***argvp;
   1331 	int isok;
   1332 {
   1333 
   1334 	isoutput = 1;
   1335 
   1336 	return (palloc(N_PRINTX, f_printx));
   1337 }
   1338 
   1339 /*
   1340  * -prune functions --
   1341  *
   1342  *	Prune a portion of the hierarchy.
   1343  */
   1344 int
   1345 f_prune(plan, entry)
   1346 	PLAN *plan;
   1347 	FTSENT *entry;
   1348 {
   1349 	if (fts_set(tree, entry, FTS_SKIP))
   1350 		err(1, "%s", entry->fts_path);
   1351 	return (1);
   1352 }
   1353 
   1354 PLAN *
   1355 c_prune(argvp, isok)
   1356 	char ***argvp;
   1357 	int isok;
   1358 {
   1359 
   1360 	return (palloc(N_PRUNE, f_prune));
   1361 }
   1362 
   1363 /*
   1364  * -regex regexp (and related) functions --
   1365  *
   1366  *	True if the complete file path matches the regular expression regexp.
   1367  *	For -regex, regexp is a case-sensitive (basic) regular expression.
   1368  *	For -iregex, regexp is a case-insensitive (basic) regular expression.
   1369  */
   1370 int
   1371 f_regex(plan, entry)
   1372 	PLAN *plan;
   1373 	FTSENT *entry;
   1374 {
   1375 
   1376 	return (regexec(&plan->regexp_data, entry->fts_path, 0, NULL, 0) == 0);
   1377 }
   1378 
   1379 static PLAN *
   1380 c_regex_common(argvp, isok, type, regcomp_flags)
   1381 	char ***argvp;
   1382 	int isok, regcomp_flags;
   1383 	enum ntype type;
   1384 {
   1385 	char errbuf[LINE_MAX];
   1386 	regex_t reg;
   1387 	char *regexp = **argvp;
   1388 	char *lineregexp;
   1389 	PLAN *new;
   1390 	int rv;
   1391 
   1392 	(*argvp)++;
   1393 
   1394 	lineregexp = alloca(strlen(regexp) + 1 + 6);	/* max needed */
   1395 	sprintf(lineregexp, "^%s(%s%s)$",
   1396 	    (regcomp_flags & REG_EXTENDED) ? "" : "\\", regexp,
   1397 	    (regcomp_flags & REG_EXTENDED) ? "" : "\\");
   1398 	rv = regcomp(&reg, lineregexp, REG_NOSUB|regcomp_flags);
   1399 	if (rv != 0) {
   1400 		regerror(rv, &reg, errbuf, sizeof errbuf);
   1401 		errx(1, "regexp %s: %s", regexp, errbuf);
   1402 	}
   1403 
   1404 	new = palloc(type, f_regex);
   1405 	new->regexp_data = reg;
   1406 	return (new);
   1407 }
   1408 
   1409 PLAN *
   1410 c_regex(argvp, isok)
   1411 	char ***argvp;
   1412 	int isok;
   1413 {
   1414 
   1415 	return (c_regex_common(argvp, isok, N_REGEX, REG_BASIC));
   1416 }
   1417 
   1418 PLAN *
   1419 c_iregex(argvp, isok)
   1420 	char ***argvp;
   1421 	int isok;
   1422 {
   1423 
   1424 	return (c_regex_common(argvp, isok, N_IREGEX, REG_BASIC|REG_ICASE));
   1425 }
   1426 
   1427 /*
   1428  * -size n[c] functions --
   1429  *
   1430  *	True if the file size in bytes, divided by an implementation defined
   1431  *	value and rounded up to the next integer, is n.  If n is followed by
   1432  *	a c, the size is in bytes.
   1433  */
   1434 #define	FIND_SIZE	512
   1435 static int divsize = 1;
   1436 
   1437 int
   1438 f_size(plan, entry)
   1439 	PLAN *plan;
   1440 	FTSENT *entry;
   1441 {
   1442 	off_t size;
   1443 
   1444 	size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
   1445 	    FIND_SIZE : entry->fts_statp->st_size;
   1446 	COMPARE(size, plan->o_data);
   1447 }
   1448 
   1449 PLAN *
   1450 c_size(argvp, isok)
   1451 	char ***argvp;
   1452 	int isok;
   1453 {
   1454 	char *arg = **argvp;
   1455 	PLAN *new;
   1456 	char endch;
   1457 
   1458 	(*argvp)++;
   1459 	ftsoptions &= ~FTS_NOSTAT;
   1460 
   1461 	new = palloc(N_SIZE, f_size);
   1462 	endch = 'c';
   1463 	new->o_data = find_parsenum(new, "-size", arg, &endch);
   1464 	if (endch == 'c')
   1465 		divsize = 0;
   1466 	return (new);
   1467 }
   1468 
   1469 /*
   1470  * -type c functions --
   1471  *
   1472  *	True if the type of the file is c, where c is b, c, d, p, f or w
   1473  *	for block special file, character special file, directory, FIFO,
   1474  *	regular file or whiteout respectively.
   1475  */
   1476 int
   1477 f_type(plan, entry)
   1478 	PLAN *plan;
   1479 	FTSENT *entry;
   1480 {
   1481 
   1482 	return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);
   1483 }
   1484 
   1485 PLAN *
   1486 c_type(argvp, isok)
   1487 	char ***argvp;
   1488 	int isok;
   1489 {
   1490 	char *typestring = **argvp;
   1491 	PLAN *new;
   1492 	mode_t  mask = (mode_t)0;
   1493 
   1494 	(*argvp)++;
   1495 	ftsoptions &= ~FTS_NOSTAT;
   1496 
   1497 	switch (typestring[0]) {
   1498 #ifdef S_IFWHT
   1499       case 'W':
   1500 #ifdef FTS_WHITEOUT
   1501 	      ftsoptions |= FTS_WHITEOUT;
   1502 #endif
   1503               mask = S_IFWHT;
   1504               break;
   1505 #endif
   1506 	case 'b':
   1507 		mask = S_IFBLK;
   1508 		break;
   1509 	case 'c':
   1510 		mask = S_IFCHR;
   1511 		break;
   1512 	case 'd':
   1513 		mask = S_IFDIR;
   1514 		break;
   1515 	case 'f':
   1516 		mask = S_IFREG;
   1517 		break;
   1518 	case 'l':
   1519 		mask = S_IFLNK;
   1520 		break;
   1521 	case 'p':
   1522 		mask = S_IFIFO;
   1523 		break;
   1524 	case 's':
   1525 		mask = S_IFSOCK;
   1526 		break;
   1527 #ifdef FTS_WHITEOUT
   1528 	case 'w':
   1529 		mask = S_IFWHT;
   1530 		ftsoptions |= FTS_WHITEOUT;
   1531 		break;
   1532 #endif /* FTS_WHITEOUT */
   1533 	default:
   1534 		errx(1, "-type: %s: unknown type", typestring);
   1535 	}
   1536 
   1537 	new = palloc(N_TYPE, f_type);
   1538 	new->m_data = mask;
   1539 	return (new);
   1540 }
   1541 
   1542 /*
   1543  * -user uname functions --
   1544  *
   1545  *	True if the file belongs to the user uname.  If uname is numeric and
   1546  *	an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
   1547  *	return a valid user name, uname is taken as a user ID.
   1548  */
   1549 int
   1550 f_user(plan, entry)
   1551 	PLAN *plan;
   1552 	FTSENT *entry;
   1553 {
   1554 
   1555 	return (entry->fts_statp->st_uid == plan->u_data);
   1556 }
   1557 
   1558 PLAN *
   1559 c_user(argvp, isok)
   1560 	char ***argvp;
   1561 	int isok;
   1562 {
   1563 	char *username = **argvp;
   1564 	PLAN *new;
   1565 	struct passwd *p;
   1566 	uid_t uid;
   1567 
   1568 	(*argvp)++;
   1569 	ftsoptions &= ~FTS_NOSTAT;
   1570 
   1571 	p = getpwnam(username);
   1572 	if (p == NULL) {
   1573 		uid = atoi(username);
   1574 		if (uid == 0 && username[0] != '0')
   1575 			errx(1, "-user: %s: no such user", username);
   1576 	} else
   1577 		uid = p->pw_uid;
   1578 
   1579 	new = palloc(N_USER, f_user);
   1580 	new->u_data = uid;
   1581 	return (new);
   1582 }
   1583 
   1584 /*
   1585  * -xdev functions --
   1586  *
   1587  *	Always true, causes find not to decend past directories that have a
   1588  *	different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
   1589  */
   1590 PLAN *
   1591 c_xdev(argvp, isok)
   1592 	char ***argvp;
   1593 	int isok;
   1594 {
   1595 	ftsoptions |= FTS_XDEV;
   1596 
   1597 	return (palloc(N_XDEV, f_always_true));
   1598 }
   1599 
   1600 /*
   1601  * ( expression ) functions --
   1602  *
   1603  *	True if expression is true.
   1604  */
   1605 int
   1606 f_expr(plan, entry)
   1607 	PLAN *plan;
   1608 	FTSENT *entry;
   1609 {
   1610 	PLAN *p;
   1611 	int state;
   1612 
   1613 	state = 0;
   1614 	for (p = plan->p_data[0];
   1615 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1616 	return (state);
   1617 }
   1618 
   1619 /*
   1620  * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
   1621  * eliminated during phase 2 of find_formplan() --- the '(' node is converted
   1622  * to a N_EXPR node containing the expression and the ')' node is discarded.
   1623  */
   1624 PLAN *
   1625 c_openparen(argvp, isok)
   1626 	char ***argvp;
   1627 	int isok;
   1628 {
   1629 
   1630 	return (palloc(N_OPENPAREN, (int (*) __P((PLAN *, FTSENT *)))-1));
   1631 }
   1632 
   1633 PLAN *
   1634 c_closeparen(argvp, isok)
   1635 	char ***argvp;
   1636 	int isok;
   1637 {
   1638 
   1639 	return (palloc(N_CLOSEPAREN, (int (*) __P((PLAN *, FTSENT *)))-1));
   1640 }
   1641 
   1642 /*
   1643  * ! expression functions --
   1644  *
   1645  *	Negation of a primary; the unary NOT operator.
   1646  */
   1647 int
   1648 f_not(plan, entry)
   1649 	PLAN *plan;
   1650 	FTSENT *entry;
   1651 {
   1652 	PLAN *p;
   1653 	int state;
   1654 
   1655 	state = 0;
   1656 	for (p = plan->p_data[0];
   1657 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1658 	return (!state);
   1659 }
   1660 
   1661 PLAN *
   1662 c_not(argvp, isok)
   1663 	char ***argvp;
   1664 	int isok;
   1665 {
   1666 
   1667 	return (palloc(N_NOT, f_not));
   1668 }
   1669 
   1670 /*
   1671  * expression -o expression functions --
   1672  *
   1673  *	Alternation of primaries; the OR operator.  The second expression is
   1674  * not evaluated if the first expression is true.
   1675  */
   1676 int
   1677 f_or(plan, entry)
   1678 	PLAN *plan;
   1679 	FTSENT *entry;
   1680 {
   1681 	PLAN *p;
   1682 	int state;
   1683 
   1684 	state = 0;
   1685 	for (p = plan->p_data[0];
   1686 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1687 
   1688 	if (state)
   1689 		return (1);
   1690 
   1691 	for (p = plan->p_data[1];
   1692 	    p && (state = (p->eval)(p, entry)); p = p->next);
   1693 	return (state);
   1694 }
   1695 
   1696 PLAN *
   1697 c_or(argvp, isok)
   1698 	char ***argvp;
   1699 	int isok;
   1700 {
   1701 
   1702 	return (palloc(N_OR, f_or));
   1703 }
   1704 
   1705 PLAN *
   1706 c_null(argvp, isok)
   1707 	char ***argvp;
   1708 	int isok;
   1709 {
   1710 
   1711 	return (NULL);
   1712 }
   1713 
   1714 static PLAN *
   1715 palloc(t, f)
   1716 	enum ntype t;
   1717 	int (*f) __P((PLAN *, FTSENT *));
   1718 {
   1719 	PLAN *new;
   1720 
   1721 	if ((new = malloc(sizeof(PLAN))) == NULL)
   1722 		err(1, NULL);
   1723 	new->type = t;
   1724 	new->eval = f;
   1725 	new->flags = 0;
   1726 	new->next = NULL;
   1727 	return (new);
   1728 }
   1729