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