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