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