2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Cimarron D. Taylor of the University of California, Berkeley.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
39 static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/ucred.h>
49 #include <sys/types.h>
52 #include <sys/mount.h>
53 #include <sys/timeb.h>
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 ***);
77 extern char **environ;
79 #define COMPARE(a, b) do { \
80 switch (plan->flags & F_ELG_MASK) { \
93 palloc(OPTION *option)
97 if ((new = malloc(sizeof(PLAN))) == NULL)
99 new->execute = option->execute;
100 new->flags = option->flags;
107 * Parse a string of the form [+-]# and return the value.
110 find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
113 char *endchar, *str; /* Pointer to character ending conversion. */
115 /* Determine comparison from leading + or -. */
120 plan->flags |= F_GREATER;
124 plan->flags |= F_LESSTHAN;
127 plan->flags |= F_EQUAL;
132 * Convert the string with strtoq(). Note, if strtoq() returns zero
133 * and endchar points to the beginning of the string we know we have
136 value = strtoq(str, &endchar, 10);
137 if (value == 0 && endchar == str)
138 errx(1, "%s: %s: illegal numeric value", option, vp);
139 if (endchar[0] && (endch == NULL || endchar[0] != *endch))
140 errx(1, "%s: %s: illegal trailing character", option, vp);
148 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
151 find_parsetime(PLAN *plan, const char *option, char *vp)
153 long long secs, value;
154 char *str, *unit; /* Pointer to character ending conversion. */
156 /* Determine comparison from leading + or -. */
161 plan->flags |= F_GREATER;
165 plan->flags |= F_LESSTHAN;
168 plan->flags |= F_EQUAL;
172 value = strtoq(str, &unit, 10);
173 if (value == 0 && unit == str) {
174 errx(1, "%s: %s: illegal time value", option, vp);
184 case 's': /* seconds */
187 case 'm': /* minutes */
190 case 'h': /* hours */
191 secs += value * 3600;
194 secs += value * 86400;
196 case 'w': /* weeks */
197 secs += value * 604800;
200 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
204 if (*str == '\0') /* EOS */
206 value = strtoq(str, &unit, 10);
207 if (value == 0 && unit == str) {
208 errx(1, "%s: %s: illegal time value", option, vp);
212 errx(1, "%s: %s: missing trailing unit", option, vp);
216 plan->flags |= F_EXACTTIME;
222 * Check that another argument still exists, return a pointer to it,
223 * and increment the argument vector pointer.
226 nextarg(OPTION *option, char ***argvp)
230 if ((arg = **argvp) == 0)
231 errx(1, "%s: requires additional arguments", option->name);
237 * The value of n for the inode times (atime, ctime, and mtime) is a range,
238 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
239 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
240 * user wanted. Correct so that -1 is "less than 1".
242 #define TIME_CORRECT(p) \
243 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
247 * -[acm]min n functions --
249 * True if the difference between the
250 * file access time (-amin)
251 * last change of file status information (-cmin)
252 * file modification time (-mmin)
253 * and the current time is n min periods.
256 f_Xmin(PLAN *plan, FTSENT *entry)
258 if (plan->flags & F_TIME_C) {
259 COMPARE((now - entry->fts_statp->st_ctime +
260 60 - 1) / 60, plan->t_data);
261 } else if (plan->flags & F_TIME_A) {
262 COMPARE((now - entry->fts_statp->st_atime +
263 60 - 1) / 60, plan->t_data);
265 COMPARE((now - entry->fts_statp->st_mtime +
266 60 - 1) / 60, plan->t_data);
271 c_Xmin(OPTION *option, char ***argvp)
276 nmins = nextarg(option, argvp);
277 ftsoptions &= ~FTS_NOSTAT;
279 new = palloc(option);
280 new->t_data = find_parsenum(new, option->name, nmins, NULL);
286 * -[acm]time n functions --
288 * True if the difference between the
289 * file access time (-atime)
290 * last change of file status information (-ctime)
291 * file modification time (-mtime)
292 * and the current time is n 24 hour periods.
296 f_Xtime(PLAN *plan, FTSENT *entry)
300 if (plan->flags & F_TIME_A)
301 xtime = entry->fts_statp->st_atime;
302 else if (plan->flags & F_TIME_C)
303 xtime = entry->fts_statp->st_ctime;
305 xtime = entry->fts_statp->st_mtime;
307 if (plan->flags & F_EXACTTIME)
308 COMPARE(now - xtime, plan->t_data);
310 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data);
314 c_Xtime(OPTION *option, char ***argvp)
319 value = nextarg(option, argvp);
320 ftsoptions &= ~FTS_NOSTAT;
322 new = palloc(option);
323 new->t_data = find_parsetime(new, option->name, value);
324 if (!(new->flags & F_EXACTTIME))
330 * -maxdepth/-mindepth n functions --
332 * Does the same as -prune if the level of the current file is
333 * greater/less than the specified maximum/minimum depth.
335 * Note that -maxdepth and -mindepth are handled specially in
336 * find_execute() so their f_* functions are set to f_always_true().
339 c_mXXdepth(OPTION *option, char ***argvp)
344 dstr = nextarg(option, argvp);
346 /* all other errors handled by find_parsenum() */
347 errx(1, "%s: %s: value must be positive", option->name, dstr);
349 new = palloc(option);
350 if (option->flags & F_MAXDEPTH)
351 maxdepth = find_parsenum(new, option->name, dstr, NULL);
353 mindepth = find_parsenum(new, option->name, dstr, NULL);
360 * Show files with EXTENDED ACL attributes.
363 f_acl(PLAN *plan __unused, FTSENT *entry)
369 if (S_ISLNK(entry->fts_statp->st_mode))
371 if ((match = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED)) <= 0) {
372 if (match < 0 && errno != EINVAL)
373 warn("%s", entry->fts_accpath);
378 if ((facl = acl_get_file(entry->fts_accpath,ACL_TYPE_ACCESS)) != NULL) {
379 if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 1) {
381 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS
382 * must have at least three entries (owner, group,
386 while (acl_get_entry(facl, ACL_NEXT_ENTRY, &ae) == 1) {
395 warn("%s", entry->fts_accpath);
400 c_acl(OPTION *option, char ***argvp __unused)
402 ftsoptions &= ~FTS_NOSTAT;
403 return (palloc(option));
407 * -delete functions --
409 * True always. Makes its best shot and continues on regardless.
412 f_delete(PLAN *plan __unused, FTSENT *entry)
414 /* ignore these from fts */
415 if (strcmp(entry->fts_accpath, ".") == 0 ||
416 strcmp(entry->fts_accpath, "..") == 0)
420 if (isdepth == 0 || /* depth off */
421 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */
422 !(ftsoptions & FTS_PHYSICAL) || /* physical off */
423 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
424 errx(1, "-delete: insecure options got turned on");
426 /* Potentially unsafe - do not accept relative paths whatsoever */
427 if (strchr(entry->fts_accpath, '/') != NULL)
428 errx(1, "-delete: %s: relative path potentially not safe",
431 /* Turn off user immutable bits if running as root */
432 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
433 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
435 chflags(entry->fts_accpath,
436 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
438 /* rmdir directories, unlink everything else */
439 if (S_ISDIR(entry->fts_statp->st_mode)) {
440 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
441 warn("-delete: rmdir(%s)", entry->fts_path);
443 if (unlink(entry->fts_accpath) < 0)
444 warn("-delete: unlink(%s)", entry->fts_path);
452 c_delete(OPTION *option, char ***argvp __unused)
455 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
456 ftsoptions |= FTS_PHYSICAL; /* disable -follow */
457 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */
458 isoutput = 1; /* possible output */
459 isdepth = 1; /* -depth implied */
461 return palloc(option);
468 * Always true, used for -maxdepth, -mindepth, -xdev and -follow
471 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
477 * -depth functions --
479 * With argument: True if the file is at level n.
480 * Without argument: Always true, causes descent of the directory hierarchy
481 * to be done so that all entries in a directory are acted on before the
485 f_depth(PLAN *plan, FTSENT *entry)
487 if (plan->flags & F_DEPTH)
488 COMPARE(entry->fts_level, plan->d_data);
494 c_depth(OPTION *option, char ***argvp)
499 new = palloc(option);
502 if (str && !(new->flags & F_DEPTH)) {
503 /* skip leading + or - */
504 if (*str == '+' || *str == '-')
507 if (*str == '+' || *str == '-')
510 new->flags |= F_DEPTH;
513 if (new->flags & F_DEPTH) { /* -depth n */
516 ndepth = nextarg(option, argvp);
517 new->d_data = find_parsenum(new, option->name, ndepth, NULL);
526 * -empty functions --
528 * True if the file or directory is empty
531 f_empty(PLAN *plan __unused, FTSENT *entry)
533 if (S_ISREG(entry->fts_statp->st_mode) &&
534 entry->fts_statp->st_size == 0)
536 if (S_ISDIR(entry->fts_statp->st_mode)) {
542 dir = opendir(entry->fts_accpath);
544 err(1, "%s", entry->fts_accpath);
545 for (dp = readdir(dir); dp; dp = readdir(dir))
546 if (dp->d_name[0] != '.' ||
547 (dp->d_name[1] != '\0' &&
548 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
559 c_empty(OPTION *option, char ***argvp __unused)
561 ftsoptions &= ~FTS_NOSTAT;
563 return palloc(option);
567 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
569 * True if the executed utility returns a zero value as exit status.
570 * The end of the primary expression is delimited by a semicolon. If
571 * "{}" occurs anywhere, it gets replaced by the current pathname,
572 * or, in the case of -execdir, the current basename (filename
573 * without leading directory prefix). For -exec and -ok,
574 * the current directory for the execution of utility is the same as
575 * the current directory when the find utility was started, whereas
576 * for -execdir, it is the directory the file resides in.
578 * The primary -ok differs from -exec in that it requests affirmation
579 * of the user before executing the utility.
582 f_exec(PLAN *plan, FTSENT *entry)
589 if (entry == NULL && plan->flags & F_EXECPLUS) {
590 if (plan->e_ppos == plan->e_pbnum)
592 plan->e_argv[plan->e_ppos] = NULL;
596 /* XXX - if file/dir ends in '/' this will not work -- can it? */
597 if ((plan->flags & F_EXECDIR) && \
598 (file = strrchr(entry->fts_path, '/')))
601 file = entry->fts_path;
603 if (plan->flags & F_EXECPLUS) {
604 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
606 plan->e_len[plan->e_ppos] = strlen(file);
607 plan->e_psize += plan->e_len[plan->e_ppos];
608 if (++plan->e_ppos < plan->e_pnummax &&
609 plan->e_psize < plan->e_psizemax)
611 plan->e_argv[plan->e_ppos] = NULL;
613 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
614 if (plan->e_len[cnt])
615 brace_subst(plan->e_orig[cnt],
616 &plan->e_argv[cnt], file,
620 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
623 /* make sure find output is interspersed correctly with subprocesses */
627 switch (pid = fork()) {
632 /* change dir back from where we started */
633 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
637 execvp(plan->e_argv[0], plan->e_argv);
638 warn("%s", plan->e_argv[0]);
641 if (plan->flags & F_EXECPLUS) {
642 while (--plan->e_ppos >= plan->e_pbnum)
643 free(plan->e_argv[plan->e_ppos]);
644 plan->e_ppos = plan->e_pbnum;
645 plan->e_psize = plan->e_pbsize;
647 pid = waitpid(pid, &status, 0);
648 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
652 * c_exec, c_execdir, c_ok --
653 * build three parallel arrays, one with pointers to the strings passed
654 * on the command line, one with (possibly duplicated) pointers to the
655 * argv array, and one with integer values that are lengths of the
656 * strings, but also flags meaning that the string has to be massaged.
659 c_exec(OPTION *option, char ***argvp)
661 PLAN *new; /* node returned */
664 char **argv, **ap, **ep, *p;
666 /* XXX - was in c_execdir, but seems unnecessary!?
667 ftsoptions &= ~FTS_NOSTAT;
671 /* XXX - this is a change from the previous coding */
672 new = palloc(option);
674 for (ap = argv = *argvp;; ++ap) {
677 "%s: no terminating \";\" or \"+\"", option->name);
680 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
681 new->flags |= F_EXECPLUS;
687 errx(1, "%s: no command specified", option->name);
689 cnt = ap - *argvp + 1;
690 if (new->flags & F_EXECPLUS) {
691 new->e_ppos = new->e_pbnum = cnt - 2;
692 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
693 warn("sysconf(_SC_ARG_MAX)");
694 argmax = _POSIX_ARG_MAX;
697 for (ep = environ; *ep != NULL; ep++)
698 argmax -= strlen(*ep) + 1 + sizeof(*ep);
699 argmax -= 1 + sizeof(*ep);
700 new->e_pnummax = argmax / 16;
701 argmax -= sizeof(char *) * new->e_pnummax;
703 errx(1, "no space for arguments");
704 new->e_psizemax = argmax;
706 cnt += new->e_pnummax + 1;
708 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
710 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
712 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
715 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
716 new->e_orig[cnt] = *argv;
717 if (new->flags & F_EXECPLUS)
718 new->e_pbsize += strlen(*argv) + 1;
719 for (p = *argv; *p; ++p)
720 if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
722 if ((new->e_argv[cnt] =
723 malloc(MAXPATHLEN)) == NULL)
725 new->e_len[cnt] = MAXPATHLEN;
729 new->e_argv[cnt] = *argv;
733 if (new->flags & F_EXECPLUS) {
734 new->e_psize = new->e_pbsize;
736 for (i = 0; i < new->e_pnummax; i++) {
737 new->e_argv[cnt] = NULL;
744 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
746 done: *argvp = argv + 1;
751 f_flags(PLAN *plan, FTSENT *entry)
755 flags = entry->fts_statp->st_flags;
756 if (plan->flags & F_ATLEAST)
757 return (flags | plan->fl_flags) == flags &&
758 !(flags & plan->fl_notflags);
759 else if (plan->flags & F_ANY)
760 return (flags & plan->fl_flags) ||
761 (flags | plan->fl_notflags) != flags;
763 return flags == plan->fl_flags &&
764 !(plan->fl_flags & plan->fl_notflags);
768 c_flags(OPTION *option, char ***argvp)
772 u_long flags, notflags;
774 flags_str = nextarg(option, argvp);
775 ftsoptions &= ~FTS_NOSTAT;
777 new = palloc(option);
779 if (*flags_str == '-') {
780 new->flags |= F_ATLEAST;
782 } else if (*flags_str == '+') {
786 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
787 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
789 new->fl_flags = flags;
790 new->fl_notflags = notflags;
795 * -follow functions --
797 * Always true, causes symbolic links to be followed on a global
801 c_follow(OPTION *option, char ***argvp __unused)
803 ftsoptions &= ~FTS_PHYSICAL;
804 ftsoptions |= FTS_LOGICAL;
806 return palloc(option);
810 * -fstype functions --
812 * True if the file is of a certain type.
815 f_fstype(PLAN *plan, FTSENT *entry)
817 static dev_t curdev; /* need a guaranteed illegal dev value */
818 static int first = 1;
820 static int val_type, val_flags;
823 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
826 /* Only check when we cross mount point. */
827 if (first || curdev != entry->fts_statp->st_dev) {
828 curdev = entry->fts_statp->st_dev;
831 * Statfs follows symlinks; find wants the link's filesystem,
832 * not where it points.
834 if (entry->fts_info == FTS_SL ||
835 entry->fts_info == FTS_SLNONE) {
836 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
839 p = entry->fts_accpath;
847 if (statfs(entry->fts_accpath, &sb))
848 err(1, "%s", entry->fts_accpath);
858 * Further tests may need both of these values, so
859 * always copy both of them.
861 val_flags = sb.f_flags;
862 val_type = sb.f_type;
864 switch (plan->flags & F_MTMASK) {
866 return val_flags & plan->mt_data;
868 return val_type == plan->mt_data;
875 c_fstype(OPTION *option, char ***argvp)
881 fsname = nextarg(option, argvp);
882 ftsoptions &= ~FTS_NOSTAT;
884 new = palloc(option);
887 * Check first for a filesystem name.
889 if (getvfsbyname(fsname, &vfc) == 0) {
890 new->flags |= F_MTTYPE;
891 new->mt_data = vfc.vfc_typenum;
897 if (!strcmp(fsname, "local")) {
898 new->flags |= F_MTFLAG;
899 new->mt_data = MNT_LOCAL;
904 if (!strcmp(fsname, "rdonly")) {
905 new->flags |= F_MTFLAG;
906 new->mt_data = MNT_RDONLY;
913 * We need to make filesystem checks for filesystems
914 * that exists but aren't in the kernel work.
916 fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname);
917 new->flags |= F_MTUNKNOWN;
922 * -group gname functions --
924 * True if the file belongs to the group gname. If gname is numeric and
925 * an equivalent of the getgrnam() function does not return a valid group
926 * name, gname is taken as a group ID.
929 f_group(PLAN *plan, FTSENT *entry)
931 return entry->fts_statp->st_gid == plan->g_data;
935 c_group(OPTION *option, char ***argvp)
942 gname = nextarg(option, argvp);
943 ftsoptions &= ~FTS_NOSTAT;
948 if (gid == 0 && gname[0] != '0')
949 errx(1, "%s: %s: no such group", option->name, gname);
953 new = palloc(option);
959 * -inum n functions --
961 * True if the file has inode # n.
964 f_inum(PLAN *plan, FTSENT *entry)
966 COMPARE(entry->fts_statp->st_ino, plan->i_data);
970 c_inum(OPTION *option, char ***argvp)
975 inum_str = nextarg(option, argvp);
976 ftsoptions &= ~FTS_NOSTAT;
978 new = palloc(option);
979 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
984 * -links n functions --
986 * True if the file has n links.
989 f_links(PLAN *plan, FTSENT *entry)
991 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
995 c_links(OPTION *option, char ***argvp)
1000 nlinks = nextarg(option, argvp);
1001 ftsoptions &= ~FTS_NOSTAT;
1003 new = palloc(option);
1004 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1011 * Always true - prints the current entry to stdout in "ls" format.
1014 f_ls(PLAN *plan __unused, FTSENT *entry)
1016 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1021 c_ls(OPTION *option, char ***argvp __unused)
1023 ftsoptions &= ~FTS_NOSTAT;
1026 return palloc(option);
1030 * -name functions --
1032 * True if the basename of the filename being examined
1033 * matches pattern using Pattern Matching Notation S3.14
1036 f_name(PLAN *plan, FTSENT *entry)
1038 return !fnmatch(plan->c_data, entry->fts_name,
1039 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1043 c_name(OPTION *option, char ***argvp)
1048 pattern = nextarg(option, argvp);
1049 new = palloc(option);
1050 new->c_data = pattern;
1055 * -newer file functions --
1057 * True if the current file has been modified more recently
1058 * then the modification time of the file named by the pathname
1062 f_newer(PLAN *plan, FTSENT *entry)
1064 if (plan->flags & F_TIME_C)
1065 return entry->fts_statp->st_ctime > plan->t_data;
1066 else if (plan->flags & F_TIME_A)
1067 return entry->fts_statp->st_atime > plan->t_data;
1069 return entry->fts_statp->st_mtime > plan->t_data;
1073 c_newer(OPTION *option, char ***argvp)
1079 fn_or_tspec = nextarg(option, argvp);
1080 ftsoptions &= ~FTS_NOSTAT;
1082 new = palloc(option);
1083 /* compare against what */
1084 if (option->flags & F_TIME2_T) {
1085 new->t_data = get_date(fn_or_tspec, (struct timeb *) 0);
1086 if (new->t_data == (time_t) -1)
1087 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1089 if (stat(fn_or_tspec, &sb))
1090 err(1, "%s", fn_or_tspec);
1091 if (option->flags & F_TIME2_C)
1092 new->t_data = sb.st_ctime;
1093 else if (option->flags & F_TIME2_A)
1094 new->t_data = sb.st_atime;
1096 new->t_data = sb.st_mtime;
1102 * -nogroup functions --
1104 * True if file belongs to a user ID for which the equivalent
1105 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1108 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1110 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1114 c_nogroup(OPTION *option, char ***argvp __unused)
1116 ftsoptions &= ~FTS_NOSTAT;
1118 return palloc(option);
1122 * -nouser functions --
1124 * True if file belongs to a user ID for which the equivalent
1125 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1128 f_nouser(PLAN *plan __unused, FTSENT *entry)
1130 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1134 c_nouser(OPTION *option, char ***argvp __unused)
1136 ftsoptions &= ~FTS_NOSTAT;
1138 return palloc(option);
1142 * -path functions --
1144 * True if the path of the filename being examined
1145 * matches pattern using Pattern Matching Notation S3.14
1148 f_path(PLAN *plan, FTSENT *entry)
1150 return !fnmatch(plan->c_data, entry->fts_path,
1151 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1154 /* c_path is the same as c_name */
1157 * -perm functions --
1159 * The mode argument is used to represent file mode bits. If it starts
1160 * with a leading digit, it's treated as an octal mode, otherwise as a
1164 f_perm(PLAN *plan, FTSENT *entry)
1168 mode = entry->fts_statp->st_mode &
1169 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1170 if (plan->flags & F_ATLEAST)
1171 return (plan->m_data | mode) == mode;
1172 else if (plan->flags & F_ANY)
1173 return (mode & plan->m_data);
1175 return mode == plan->m_data;
1180 c_perm(OPTION *option, char ***argvp)
1186 perm = nextarg(option, argvp);
1187 ftsoptions &= ~FTS_NOSTAT;
1189 new = palloc(option);
1192 new->flags |= F_ATLEAST;
1194 } else if (*perm == '+') {
1195 new->flags |= F_ANY;
1199 if ((set = setmode(perm)) == NULL)
1200 errx(1, "%s: %s: illegal mode string", option->name, perm);
1202 new->m_data = getmode(set, 0);
1208 * -print functions --
1210 * Always true, causes the current pathname to be written to
1214 f_print(PLAN *plan __unused, FTSENT *entry)
1216 (void)puts(entry->fts_path);
1221 c_print(OPTION *option, char ***argvp __unused)
1225 return palloc(option);
1229 * -print0 functions --
1231 * Always true, causes the current pathname to be written to
1232 * standard output followed by a NUL character
1235 f_print0(PLAN *plan __unused, FTSENT *entry)
1237 fputs(entry->fts_path, stdout);
1238 fputc('\0', stdout);
1242 /* c_print0 is the same as c_print */
1245 * -prune functions --
1247 * Prune a portion of the hierarchy.
1250 f_prune(PLAN *plan __unused, FTSENT *entry)
1252 if (fts_set(tree, entry, FTS_SKIP))
1253 err(1, "%s", entry->fts_path);
1257 /* c_prune == c_simple */
1260 * -regex functions --
1262 * True if the whole path of the file matches pattern using
1263 * regular expression.
1266 f_regex(PLAN *plan, FTSENT *entry)
1273 char errbuf[LINE_MAX];
1276 pre = plan->re_data;
1277 str = entry->fts_path;
1284 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1286 if (errcode != 0 && errcode != REG_NOMATCH) {
1287 regerror(errcode, pre, errbuf, sizeof errbuf);
1289 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1292 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1299 c_regex(OPTION *option, char ***argvp)
1305 char errbuf[LINE_MAX];
1307 if ((pre = malloc(sizeof(regex_t))) == NULL)
1310 pattern = nextarg(option, argvp);
1312 if ((errcode = regcomp(pre, pattern,
1313 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1314 regerror(errcode, pre, errbuf, sizeof errbuf);
1315 errx(1, "%s: %s: %s",
1316 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1320 new = palloc(option);
1326 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */
1329 c_simple(OPTION *option, char ***argvp __unused)
1331 return palloc(option);
1335 * -size n[c] functions --
1337 * True if the file size in bytes, divided by an implementation defined
1338 * value and rounded up to the next integer, is n. If n is followed by
1339 * a c, the size is in bytes.
1341 #define FIND_SIZE 512
1342 static int divsize = 1;
1345 f_size(PLAN *plan, FTSENT *entry)
1349 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1350 FIND_SIZE : entry->fts_statp->st_size;
1351 COMPARE(size, plan->o_data);
1355 c_size(OPTION *option, char ***argvp)
1361 size_str = nextarg(option, argvp);
1362 ftsoptions &= ~FTS_NOSTAT;
1364 new = palloc(option);
1366 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1373 * -type c functions --
1375 * True if the type of the file is c, where c is b, c, d, p, f or w
1376 * for block special file, character special file, directory, FIFO,
1377 * regular file or whiteout respectively.
1380 f_type(PLAN *plan, FTSENT *entry)
1382 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1386 c_type(OPTION *option, char ***argvp)
1392 typestring = nextarg(option, argvp);
1393 ftsoptions &= ~FTS_NOSTAT;
1395 switch (typestring[0]) {
1420 ftsoptions |= FTS_WHITEOUT;
1422 #endif /* FTS_WHITEOUT */
1424 errx(1, "%s: %s: unknown type", option->name, typestring);
1427 new = palloc(option);
1433 * -user uname functions --
1435 * True if the file belongs to the user uname. If uname is numeric and
1436 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1437 * return a valid user name, uname is taken as a user ID.
1440 f_user(PLAN *plan, FTSENT *entry)
1442 return entry->fts_statp->st_uid == plan->u_data;
1446 c_user(OPTION *option, char ***argvp)
1453 username = nextarg(option, argvp);
1454 ftsoptions &= ~FTS_NOSTAT;
1456 p = getpwnam(username);
1458 uid = atoi(username);
1459 if (uid == 0 && username[0] != '0')
1460 errx(1, "%s: %s: no such user", option->name, username);
1464 new = palloc(option);
1470 * -xdev functions --
1472 * Always true, causes find not to descend past directories that have a
1473 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1476 c_xdev(OPTION *option, char ***argvp __unused)
1478 ftsoptions |= FTS_XDEV;
1480 return palloc(option);
1484 * ( expression ) functions --
1486 * True if expression is true.
1489 f_expr(PLAN *plan, FTSENT *entry)
1494 for (p = plan->p_data[0];
1495 p && (state = (p->execute)(p, entry)); p = p->next);
1500 * f_openparen and f_closeparen nodes are temporary place markers. They are
1501 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1502 * to a f_expr node containing the expression and the ')' node is discarded.
1503 * The functions themselves are only used as constants.
1507 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1513 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1518 /* c_openparen == c_simple */
1519 /* c_closeparen == c_simple */
1522 * AND operator. Since AND is implicit, no node is allocated.
1525 c_and(OPTION *option __unused, char ***argvp __unused)
1531 * ! expression functions --
1533 * Negation of a primary; the unary NOT operator.
1536 f_not(PLAN *plan, FTSENT *entry)
1541 for (p = plan->p_data[0];
1542 p && (state = (p->execute)(p, entry)); p = p->next);
1546 /* c_not == c_simple */
1549 * expression -o expression functions --
1551 * Alternation of primaries; the OR operator. The second expression is
1552 * not evaluated if the first expression is true.
1555 f_or(PLAN *plan, FTSENT *entry)
1560 for (p = plan->p_data[0];
1561 p && (state = (p->execute)(p, entry)); p = p->next);
1566 for (p = plan->p_data[1];
1567 p && (state = (p->execute)(p, entry)); p = p->next);
1571 /* c_or == c_simple */