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