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