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, birthtime, ctime, mtime) is a
238 * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts
239 * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
240 * the 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 * file birth time (-Bmin)
252 * last change of file status information (-cmin)
253 * file modification time (-mmin)
254 * and the current time is n min periods.
257 f_Xmin(PLAN *plan, FTSENT *entry)
259 if (plan->flags & F_TIME_C) {
260 COMPARE((now - entry->fts_statp->st_ctime +
261 60 - 1) / 60, plan->t_data);
262 } else if (plan->flags & F_TIME_A) {
263 COMPARE((now - entry->fts_statp->st_atime +
264 60 - 1) / 60, plan->t_data);
265 } else if (plan->flags & F_TIME_B) {
266 COMPARE((now - entry->fts_statp->st_birthtime +
267 60 - 1) / 60, plan->t_data);
269 COMPARE((now - entry->fts_statp->st_mtime +
270 60 - 1) / 60, plan->t_data);
275 c_Xmin(OPTION *option, char ***argvp)
280 nmins = nextarg(option, argvp);
281 ftsoptions &= ~FTS_NOSTAT;
283 new = palloc(option);
284 new->t_data = find_parsenum(new, option->name, nmins, NULL);
290 * -[acm]time n functions --
292 * True if the difference between the
293 * file access time (-atime)
294 * file birth time (-Btime)
295 * last change of file status information (-ctime)
296 * file modification time (-mtime)
297 * and the current time is n 24 hour periods.
301 f_Xtime(PLAN *plan, FTSENT *entry)
305 if (plan->flags & F_TIME_A)
306 xtime = entry->fts_statp->st_atime;
307 else if (plan->flags & F_TIME_B)
308 xtime = entry->fts_statp->st_birthtime;
309 else if (plan->flags & F_TIME_C)
310 xtime = entry->fts_statp->st_ctime;
312 xtime = entry->fts_statp->st_mtime;
314 if (plan->flags & F_EXACTTIME)
315 COMPARE(now - xtime, plan->t_data);
317 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data);
321 c_Xtime(OPTION *option, char ***argvp)
326 value = nextarg(option, argvp);
327 ftsoptions &= ~FTS_NOSTAT;
329 new = palloc(option);
330 new->t_data = find_parsetime(new, option->name, value);
331 if (!(new->flags & F_EXACTTIME))
337 * -maxdepth/-mindepth n functions --
339 * Does the same as -prune if the level of the current file is
340 * greater/less than the specified maximum/minimum depth.
342 * Note that -maxdepth and -mindepth are handled specially in
343 * find_execute() so their f_* functions are set to f_always_true().
346 c_mXXdepth(OPTION *option, char ***argvp)
351 dstr = nextarg(option, argvp);
353 /* all other errors handled by find_parsenum() */
354 errx(1, "%s: %s: value must be positive", option->name, dstr);
356 new = palloc(option);
357 if (option->flags & F_MAXDEPTH)
358 maxdepth = find_parsenum(new, option->name, dstr, NULL);
360 mindepth = find_parsenum(new, option->name, dstr, NULL);
367 * Show files with EXTENDED ACL attributes.
370 f_acl(PLAN *plan __unused, FTSENT *entry)
376 if (S_ISLNK(entry->fts_statp->st_mode))
378 if ((match = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED)) <= 0) {
379 if (match < 0 && errno != EINVAL)
380 warn("%s", entry->fts_accpath);
385 if ((facl = acl_get_file(entry->fts_accpath,ACL_TYPE_ACCESS)) != NULL) {
386 if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 1) {
388 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS
389 * must have at least three entries (owner, group,
393 while (acl_get_entry(facl, ACL_NEXT_ENTRY, &ae) == 1) {
402 warn("%s", entry->fts_accpath);
407 c_acl(OPTION *option, char ***argvp __unused)
409 ftsoptions &= ~FTS_NOSTAT;
410 return (palloc(option));
414 * -delete functions --
416 * True always. Makes its best shot and continues on regardless.
419 f_delete(PLAN *plan __unused, FTSENT *entry)
421 /* ignore these from fts */
422 if (strcmp(entry->fts_accpath, ".") == 0 ||
423 strcmp(entry->fts_accpath, "..") == 0)
427 if (isdepth == 0 || /* depth off */
428 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */
429 !(ftsoptions & FTS_PHYSICAL) || /* physical off */
430 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
431 errx(1, "-delete: insecure options got turned on");
433 /* Potentially unsafe - do not accept relative paths whatsoever */
434 if (strchr(entry->fts_accpath, '/') != NULL)
435 errx(1, "-delete: %s: relative path potentially not safe",
438 /* Turn off user immutable bits if running as root */
439 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
440 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
442 chflags(entry->fts_accpath,
443 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
445 /* rmdir directories, unlink everything else */
446 if (S_ISDIR(entry->fts_statp->st_mode)) {
447 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
448 warn("-delete: rmdir(%s)", entry->fts_path);
450 if (unlink(entry->fts_accpath) < 0)
451 warn("-delete: unlink(%s)", entry->fts_path);
459 c_delete(OPTION *option, char ***argvp __unused)
462 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
463 ftsoptions |= FTS_PHYSICAL; /* disable -follow */
464 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */
465 isoutput = 1; /* possible output */
466 isdepth = 1; /* -depth implied */
468 return palloc(option);
475 * Always true, used for -maxdepth, -mindepth, -xdev and -follow
478 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
484 * -depth functions --
486 * With argument: True if the file is at level n.
487 * Without argument: Always true, causes descent of the directory hierarchy
488 * to be done so that all entries in a directory are acted on before the
492 f_depth(PLAN *plan, FTSENT *entry)
494 if (plan->flags & F_DEPTH)
495 COMPARE(entry->fts_level, plan->d_data);
501 c_depth(OPTION *option, char ***argvp)
506 new = palloc(option);
509 if (str && !(new->flags & F_DEPTH)) {
510 /* skip leading + or - */
511 if (*str == '+' || *str == '-')
514 if (*str == '+' || *str == '-')
517 new->flags |= F_DEPTH;
520 if (new->flags & F_DEPTH) { /* -depth n */
523 ndepth = nextarg(option, argvp);
524 new->d_data = find_parsenum(new, option->name, ndepth, NULL);
533 * -empty functions --
535 * True if the file or directory is empty
538 f_empty(PLAN *plan __unused, FTSENT *entry)
540 if (S_ISREG(entry->fts_statp->st_mode) &&
541 entry->fts_statp->st_size == 0)
543 if (S_ISDIR(entry->fts_statp->st_mode)) {
549 dir = opendir(entry->fts_accpath);
551 err(1, "%s", entry->fts_accpath);
552 for (dp = readdir(dir); dp; dp = readdir(dir))
553 if (dp->d_name[0] != '.' ||
554 (dp->d_name[1] != '\0' &&
555 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
566 c_empty(OPTION *option, char ***argvp __unused)
568 ftsoptions &= ~FTS_NOSTAT;
570 return palloc(option);
574 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
576 * True if the executed utility returns a zero value as exit status.
577 * The end of the primary expression is delimited by a semicolon. If
578 * "{}" occurs anywhere, it gets replaced by the current pathname,
579 * or, in the case of -execdir, the current basename (filename
580 * without leading directory prefix). For -exec and -ok,
581 * the current directory for the execution of utility is the same as
582 * the current directory when the find utility was started, whereas
583 * for -execdir, it is the directory the file resides in.
585 * The primary -ok differs from -exec in that it requests affirmation
586 * of the user before executing the utility.
589 f_exec(PLAN *plan, FTSENT *entry)
596 if (entry == NULL && plan->flags & F_EXECPLUS) {
597 if (plan->e_ppos == plan->e_pbnum)
599 plan->e_argv[plan->e_ppos] = NULL;
603 /* XXX - if file/dir ends in '/' this will not work -- can it? */
604 if ((plan->flags & F_EXECDIR) && \
605 (file = strrchr(entry->fts_path, '/')))
608 file = entry->fts_path;
610 if (plan->flags & F_EXECPLUS) {
611 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
613 plan->e_len[plan->e_ppos] = strlen(file);
614 plan->e_psize += plan->e_len[plan->e_ppos];
615 if (++plan->e_ppos < plan->e_pnummax &&
616 plan->e_psize < plan->e_psizemax)
618 plan->e_argv[plan->e_ppos] = NULL;
620 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
621 if (plan->e_len[cnt])
622 brace_subst(plan->e_orig[cnt],
623 &plan->e_argv[cnt], file,
627 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
630 /* make sure find output is interspersed correctly with subprocesses */
634 switch (pid = fork()) {
639 /* change dir back from where we started */
640 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
644 execvp(plan->e_argv[0], plan->e_argv);
645 warn("%s", plan->e_argv[0]);
648 if (plan->flags & F_EXECPLUS) {
649 while (--plan->e_ppos >= plan->e_pbnum)
650 free(plan->e_argv[plan->e_ppos]);
651 plan->e_ppos = plan->e_pbnum;
652 plan->e_psize = plan->e_pbsize;
654 pid = waitpid(pid, &status, 0);
655 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
659 * c_exec, c_execdir, c_ok --
660 * build three parallel arrays, one with pointers to the strings passed
661 * on the command line, one with (possibly duplicated) pointers to the
662 * argv array, and one with integer values that are lengths of the
663 * strings, but also flags meaning that the string has to be massaged.
666 c_exec(OPTION *option, char ***argvp)
668 PLAN *new; /* node returned */
671 char **argv, **ap, **ep, *p;
673 /* XXX - was in c_execdir, but seems unnecessary!?
674 ftsoptions &= ~FTS_NOSTAT;
678 /* XXX - this is a change from the previous coding */
679 new = palloc(option);
681 for (ap = argv = *argvp;; ++ap) {
684 "%s: no terminating \";\" or \"+\"", option->name);
687 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
688 new->flags |= F_EXECPLUS;
694 errx(1, "%s: no command specified", option->name);
696 cnt = ap - *argvp + 1;
697 if (new->flags & F_EXECPLUS) {
698 new->e_ppos = new->e_pbnum = cnt - 2;
699 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
700 warn("sysconf(_SC_ARG_MAX)");
701 argmax = _POSIX_ARG_MAX;
704 for (ep = environ; *ep != NULL; ep++)
705 argmax -= strlen(*ep) + 1 + sizeof(*ep);
706 argmax -= 1 + sizeof(*ep);
707 new->e_pnummax = argmax / 16;
708 argmax -= sizeof(char *) * new->e_pnummax;
710 errx(1, "no space for arguments");
711 new->e_psizemax = argmax;
713 cnt += new->e_pnummax + 1;
715 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
717 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
719 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
722 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
723 new->e_orig[cnt] = *argv;
724 if (new->flags & F_EXECPLUS)
725 new->e_pbsize += strlen(*argv) + 1;
726 for (p = *argv; *p; ++p)
727 if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
729 if ((new->e_argv[cnt] =
730 malloc(MAXPATHLEN)) == NULL)
732 new->e_len[cnt] = MAXPATHLEN;
736 new->e_argv[cnt] = *argv;
740 if (new->flags & F_EXECPLUS) {
741 new->e_psize = new->e_pbsize;
743 for (i = 0; i < new->e_pnummax; i++) {
744 new->e_argv[cnt] = NULL;
751 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
753 done: *argvp = argv + 1;
758 f_flags(PLAN *plan, FTSENT *entry)
762 flags = entry->fts_statp->st_flags;
763 if (plan->flags & F_ATLEAST)
764 return (flags | plan->fl_flags) == flags &&
765 !(flags & plan->fl_notflags);
766 else if (plan->flags & F_ANY)
767 return (flags & plan->fl_flags) ||
768 (flags | plan->fl_notflags) != flags;
770 return flags == plan->fl_flags &&
771 !(plan->fl_flags & plan->fl_notflags);
775 c_flags(OPTION *option, char ***argvp)
779 u_long flags, notflags;
781 flags_str = nextarg(option, argvp);
782 ftsoptions &= ~FTS_NOSTAT;
784 new = palloc(option);
786 if (*flags_str == '-') {
787 new->flags |= F_ATLEAST;
789 } else if (*flags_str == '+') {
793 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
794 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
796 new->fl_flags = flags;
797 new->fl_notflags = notflags;
802 * -follow functions --
804 * Always true, causes symbolic links to be followed on a global
808 c_follow(OPTION *option, char ***argvp __unused)
810 ftsoptions &= ~FTS_PHYSICAL;
811 ftsoptions |= FTS_LOGICAL;
813 return palloc(option);
817 * -fstype functions --
819 * True if the file is of a certain type.
822 f_fstype(PLAN *plan, FTSENT *entry)
824 static dev_t curdev; /* need a guaranteed illegal dev value */
825 static int first = 1;
827 static int val_type, val_flags;
830 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
833 /* Only check when we cross mount point. */
834 if (first || curdev != entry->fts_statp->st_dev) {
835 curdev = entry->fts_statp->st_dev;
838 * Statfs follows symlinks; find wants the link's filesystem,
839 * not where it points.
841 if (entry->fts_info == FTS_SL ||
842 entry->fts_info == FTS_SLNONE) {
843 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
846 p = entry->fts_accpath;
854 if (statfs(entry->fts_accpath, &sb))
855 err(1, "%s", entry->fts_accpath);
865 * Further tests may need both of these values, so
866 * always copy both of them.
868 val_flags = sb.f_flags;
869 val_type = sb.f_type;
871 switch (plan->flags & F_MTMASK) {
873 return val_flags & plan->mt_data;
875 return val_type == plan->mt_data;
882 c_fstype(OPTION *option, char ***argvp)
888 fsname = nextarg(option, argvp);
889 ftsoptions &= ~FTS_NOSTAT;
891 new = palloc(option);
894 * Check first for a filesystem name.
896 if (getvfsbyname(fsname, &vfc) == 0) {
897 new->flags |= F_MTTYPE;
898 new->mt_data = vfc.vfc_typenum;
904 if (!strcmp(fsname, "local")) {
905 new->flags |= F_MTFLAG;
906 new->mt_data = MNT_LOCAL;
911 if (!strcmp(fsname, "rdonly")) {
912 new->flags |= F_MTFLAG;
913 new->mt_data = MNT_RDONLY;
920 * We need to make filesystem checks for filesystems
921 * that exists but aren't in the kernel work.
923 fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname);
924 new->flags |= F_MTUNKNOWN;
929 * -group gname functions --
931 * True if the file belongs to the group gname. If gname is numeric and
932 * an equivalent of the getgrnam() function does not return a valid group
933 * name, gname is taken as a group ID.
936 f_group(PLAN *plan, FTSENT *entry)
938 return entry->fts_statp->st_gid == plan->g_data;
942 c_group(OPTION *option, char ***argvp)
949 gname = nextarg(option, argvp);
950 ftsoptions &= ~FTS_NOSTAT;
955 if (gid == 0 && gname[0] != '0')
956 errx(1, "%s: %s: no such group", option->name, gname);
960 new = palloc(option);
966 * -inum n functions --
968 * True if the file has inode # n.
971 f_inum(PLAN *plan, FTSENT *entry)
973 COMPARE(entry->fts_statp->st_ino, plan->i_data);
977 c_inum(OPTION *option, char ***argvp)
982 inum_str = nextarg(option, argvp);
983 ftsoptions &= ~FTS_NOSTAT;
985 new = palloc(option);
986 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
991 * -links n functions --
993 * True if the file has n links.
996 f_links(PLAN *plan, FTSENT *entry)
998 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1002 c_links(OPTION *option, char ***argvp)
1007 nlinks = nextarg(option, argvp);
1008 ftsoptions &= ~FTS_NOSTAT;
1010 new = palloc(option);
1011 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1018 * Always true - prints the current entry to stdout in "ls" format.
1021 f_ls(PLAN *plan __unused, FTSENT *entry)
1023 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1028 c_ls(OPTION *option, char ***argvp __unused)
1030 ftsoptions &= ~FTS_NOSTAT;
1033 return palloc(option);
1037 * -name functions --
1039 * True if the basename of the filename being examined
1040 * matches pattern using Pattern Matching Notation S3.14
1043 f_name(PLAN *plan, FTSENT *entry)
1045 return !fnmatch(plan->c_data, entry->fts_name,
1046 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1050 c_name(OPTION *option, char ***argvp)
1055 pattern = nextarg(option, argvp);
1056 new = palloc(option);
1057 new->c_data = pattern;
1062 * -newer file functions --
1064 * True if the current file has been modified more recently
1065 * then the modification time of the file named by the pathname
1069 f_newer(PLAN *plan, FTSENT *entry)
1071 if (plan->flags & F_TIME_C)
1072 return entry->fts_statp->st_ctime > plan->t_data;
1073 else if (plan->flags & F_TIME_A)
1074 return entry->fts_statp->st_atime > plan->t_data;
1075 else if (plan->flags & F_TIME_B)
1076 return entry->fts_statp->st_birthtime > plan->t_data;
1078 return entry->fts_statp->st_mtime > plan->t_data;
1082 c_newer(OPTION *option, char ***argvp)
1088 fn_or_tspec = nextarg(option, argvp);
1089 ftsoptions &= ~FTS_NOSTAT;
1091 new = palloc(option);
1092 /* compare against what */
1093 if (option->flags & F_TIME2_T) {
1094 new->t_data = get_date(fn_or_tspec, (struct timeb *) 0);
1095 if (new->t_data == (time_t) -1)
1096 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1098 if (stat(fn_or_tspec, &sb))
1099 err(1, "%s", fn_or_tspec);
1100 if (option->flags & F_TIME2_C)
1101 new->t_data = sb.st_ctime;
1102 else if (option->flags & F_TIME2_A)
1103 new->t_data = sb.st_atime;
1105 new->t_data = sb.st_mtime;
1111 * -nogroup functions --
1113 * True if file belongs to a user ID for which the equivalent
1114 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1117 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1119 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1123 c_nogroup(OPTION *option, char ***argvp __unused)
1125 ftsoptions &= ~FTS_NOSTAT;
1127 return palloc(option);
1131 * -nouser functions --
1133 * True if file belongs to a user ID for which the equivalent
1134 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1137 f_nouser(PLAN *plan __unused, FTSENT *entry)
1139 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1143 c_nouser(OPTION *option, char ***argvp __unused)
1145 ftsoptions &= ~FTS_NOSTAT;
1147 return palloc(option);
1151 * -path functions --
1153 * True if the path of the filename being examined
1154 * matches pattern using Pattern Matching Notation S3.14
1157 f_path(PLAN *plan, FTSENT *entry)
1159 return !fnmatch(plan->c_data, entry->fts_path,
1160 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1163 /* c_path is the same as c_name */
1166 * -perm functions --
1168 * The mode argument is used to represent file mode bits. If it starts
1169 * with a leading digit, it's treated as an octal mode, otherwise as a
1173 f_perm(PLAN *plan, FTSENT *entry)
1177 mode = entry->fts_statp->st_mode &
1178 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1179 if (plan->flags & F_ATLEAST)
1180 return (plan->m_data | mode) == mode;
1181 else if (plan->flags & F_ANY)
1182 return (mode & plan->m_data);
1184 return mode == plan->m_data;
1189 c_perm(OPTION *option, char ***argvp)
1195 perm = nextarg(option, argvp);
1196 ftsoptions &= ~FTS_NOSTAT;
1198 new = palloc(option);
1201 new->flags |= F_ATLEAST;
1203 } else if (*perm == '+') {
1204 new->flags |= F_ANY;
1208 if ((set = setmode(perm)) == NULL)
1209 errx(1, "%s: %s: illegal mode string", option->name, perm);
1211 new->m_data = getmode(set, 0);
1217 * -print functions --
1219 * Always true, causes the current pathname to be written to
1223 f_print(PLAN *plan __unused, FTSENT *entry)
1225 (void)puts(entry->fts_path);
1230 c_print(OPTION *option, char ***argvp __unused)
1234 return palloc(option);
1238 * -print0 functions --
1240 * Always true, causes the current pathname to be written to
1241 * standard output followed by a NUL character
1244 f_print0(PLAN *plan __unused, FTSENT *entry)
1246 fputs(entry->fts_path, stdout);
1247 fputc('\0', stdout);
1251 /* c_print0 is the same as c_print */
1254 * -prune functions --
1256 * Prune a portion of the hierarchy.
1259 f_prune(PLAN *plan __unused, FTSENT *entry)
1261 if (fts_set(tree, entry, FTS_SKIP))
1262 err(1, "%s", entry->fts_path);
1266 /* c_prune == c_simple */
1269 * -regex functions --
1271 * True if the whole path of the file matches pattern using
1272 * regular expression.
1275 f_regex(PLAN *plan, FTSENT *entry)
1282 char errbuf[LINE_MAX];
1285 pre = plan->re_data;
1286 str = entry->fts_path;
1293 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1295 if (errcode != 0 && errcode != REG_NOMATCH) {
1296 regerror(errcode, pre, errbuf, sizeof errbuf);
1298 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1301 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1308 c_regex(OPTION *option, char ***argvp)
1314 char errbuf[LINE_MAX];
1316 if ((pre = malloc(sizeof(regex_t))) == NULL)
1319 pattern = nextarg(option, argvp);
1321 if ((errcode = regcomp(pre, pattern,
1322 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1323 regerror(errcode, pre, errbuf, sizeof errbuf);
1324 errx(1, "%s: %s: %s",
1325 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1329 new = palloc(option);
1335 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */
1338 c_simple(OPTION *option, char ***argvp __unused)
1340 return palloc(option);
1344 * -size n[c] functions --
1346 * True if the file size in bytes, divided by an implementation defined
1347 * value and rounded up to the next integer, is n. If n is followed by
1348 * a c, the size is in bytes.
1350 #define FIND_SIZE 512
1351 static int divsize = 1;
1354 f_size(PLAN *plan, FTSENT *entry)
1358 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1359 FIND_SIZE : entry->fts_statp->st_size;
1360 COMPARE(size, plan->o_data);
1364 c_size(OPTION *option, char ***argvp)
1370 size_str = nextarg(option, argvp);
1371 ftsoptions &= ~FTS_NOSTAT;
1373 new = palloc(option);
1375 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1382 * -type c functions --
1384 * True if the type of the file is c, where c is b, c, d, p, f or w
1385 * for block special file, character special file, directory, FIFO,
1386 * regular file or whiteout respectively.
1389 f_type(PLAN *plan, FTSENT *entry)
1391 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1395 c_type(OPTION *option, char ***argvp)
1401 typestring = nextarg(option, argvp);
1402 ftsoptions &= ~FTS_NOSTAT;
1404 switch (typestring[0]) {
1429 ftsoptions |= FTS_WHITEOUT;
1431 #endif /* FTS_WHITEOUT */
1433 errx(1, "%s: %s: unknown type", option->name, typestring);
1436 new = palloc(option);
1442 * -user uname functions --
1444 * True if the file belongs to the user uname. If uname is numeric and
1445 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1446 * return a valid user name, uname is taken as a user ID.
1449 f_user(PLAN *plan, FTSENT *entry)
1451 return entry->fts_statp->st_uid == plan->u_data;
1455 c_user(OPTION *option, char ***argvp)
1462 username = nextarg(option, argvp);
1463 ftsoptions &= ~FTS_NOSTAT;
1465 p = getpwnam(username);
1467 uid = atoi(username);
1468 if (uid == 0 && username[0] != '0')
1469 errx(1, "%s: %s: no such user", option->name, username);
1473 new = palloc(option);
1479 * -xdev functions --
1481 * Always true, causes find not to descend past directories that have a
1482 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1485 c_xdev(OPTION *option, char ***argvp __unused)
1487 ftsoptions |= FTS_XDEV;
1489 return palloc(option);
1493 * ( expression ) functions --
1495 * True if expression is true.
1498 f_expr(PLAN *plan, FTSENT *entry)
1503 for (p = plan->p_data[0];
1504 p && (state = (p->execute)(p, entry)); p = p->next);
1509 * f_openparen and f_closeparen nodes are temporary place markers. They are
1510 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1511 * to a f_expr node containing the expression and the ')' node is discarded.
1512 * The functions themselves are only used as constants.
1516 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1522 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1527 /* c_openparen == c_simple */
1528 /* c_closeparen == c_simple */
1531 * AND operator. Since AND is implicit, no node is allocated.
1534 c_and(OPTION *option __unused, char ***argvp __unused)
1540 * ! expression functions --
1542 * Negation of a primary; the unary NOT operator.
1545 f_not(PLAN *plan, FTSENT *entry)
1550 for (p = plan->p_data[0];
1551 p && (state = (p->execute)(p, entry)); p = p->next);
1555 /* c_not == c_simple */
1558 * expression -o expression functions --
1560 * Alternation of primaries; the OR operator. The second expression is
1561 * not evaluated if the first expression is true.
1564 f_or(PLAN *plan, FTSENT *entry)
1569 for (p = plan->p_data[0];
1570 p && (state = (p->execute)(p, entry)); p = p->next);
1575 for (p = plan->p_data[1];
1576 p && (state = (p->execute)(p, entry)); p = p->next);
1580 /* c_or == c_simple */