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