2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Cimarron D. Taylor of the University of California, Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/ucred.h>
45 #include <sys/types.h>
48 #include <sys/mount.h>
67 static PLAN *palloc(OPTION *);
68 static long long find_parsenum(PLAN *, const char *, char *, char *);
69 static long long find_parsetime(PLAN *, const char *, char *);
70 static char *nextarg(OPTION *, char ***);
72 extern char **environ;
74 static PLAN *lastexecplus = NULL;
76 #define COMPARE(a, b) do { \
77 switch (plan->flags & F_ELG_MASK) { \
90 palloc(OPTION *option)
94 if ((new = malloc(sizeof(PLAN))) == NULL)
96 new->execute = option->execute;
97 new->flags = option->flags;
104 * Parse a string of the form [+-]# and return the value.
107 find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
110 char *endchar, *str; /* Pointer to character ending conversion. */
112 /* Determine comparison from leading + or -. */
117 plan->flags |= F_GREATER;
121 plan->flags |= F_LESSTHAN;
124 plan->flags |= F_EQUAL;
129 * Convert the string with strtoq(). Note, if strtoq() returns zero
130 * and endchar points to the beginning of the string we know we have
133 value = strtoq(str, &endchar, 10);
134 if (value == 0 && endchar == str)
135 errx(1, "%s: %s: illegal numeric value", option, vp);
136 if (endchar[0] && endch == NULL)
137 errx(1, "%s: %s: illegal trailing character", option, vp);
145 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
148 find_parsetime(PLAN *plan, const char *option, char *vp)
150 long long secs, value;
151 char *str, *unit; /* Pointer to character ending conversion. */
153 /* Determine comparison from leading + or -. */
158 plan->flags |= F_GREATER;
162 plan->flags |= F_LESSTHAN;
165 plan->flags |= F_EQUAL;
169 value = strtoq(str, &unit, 10);
170 if (value == 0 && unit == str) {
171 errx(1, "%s: %s: illegal time value", option, vp);
181 case 's': /* seconds */
184 case 'm': /* minutes */
187 case 'h': /* hours */
188 secs += value * 3600;
191 secs += value * 86400;
193 case 'w': /* weeks */
194 secs += value * 604800;
197 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
201 if (*str == '\0') /* EOS */
203 value = strtoq(str, &unit, 10);
204 if (value == 0 && unit == str) {
205 errx(1, "%s: %s: illegal time value", option, vp);
209 errx(1, "%s: %s: missing trailing unit", option, vp);
213 plan->flags |= F_EXACTTIME;
219 * Check that another argument still exists, return a pointer to it,
220 * and increment the argument vector pointer.
223 nextarg(OPTION *option, char ***argvp)
227 if ((arg = **argvp) == NULL)
228 errx(1, "%s: requires additional arguments", option->name);
234 * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
235 * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts
236 * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
237 * the user wanted. Correct so that -1 is "less than 1".
239 #define TIME_CORRECT(p) \
240 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
241 ++((p)->t_data.tv_sec);
244 * -[acm]min n functions --
246 * True if the difference between the
247 * file access time (-amin)
248 * file birth time (-Bmin)
249 * last change of file status information (-cmin)
250 * file modification time (-mmin)
251 * and the current time is n min periods.
254 f_Xmin(PLAN *plan, FTSENT *entry)
256 if (plan->flags & F_TIME_C) {
257 COMPARE((now - entry->fts_statp->st_ctime +
258 60 - 1) / 60, plan->t_data.tv_sec);
259 } else if (plan->flags & F_TIME_A) {
260 COMPARE((now - entry->fts_statp->st_atime +
261 60 - 1) / 60, plan->t_data.tv_sec);
262 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
263 } else if (plan->flags & F_TIME_B) {
264 COMPARE((now - entry->fts_statp->st_birthtime +
265 60 - 1) / 60, plan->t_data.tv_sec);
268 COMPARE((now - entry->fts_statp->st_mtime +
269 60 - 1) / 60, plan->t_data.tv_sec);
274 c_Xmin(OPTION *option, char ***argvp)
279 nmins = nextarg(option, argvp);
280 ftsoptions &= ~FTS_NOSTAT;
282 new = palloc(option);
283 new->t_data.tv_sec = find_parsenum(new, option->name, nmins, NULL);
284 new->t_data.tv_nsec = 0;
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 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
308 else if (plan->flags & F_TIME_B)
309 xtime = entry->fts_statp->st_birthtime;
311 else if (plan->flags & F_TIME_C)
312 xtime = entry->fts_statp->st_ctime;
314 xtime = entry->fts_statp->st_mtime;
316 if (plan->flags & F_EXACTTIME)
317 COMPARE(now - xtime, plan->t_data.tv_sec);
319 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data.tv_sec);
323 c_Xtime(OPTION *option, char ***argvp)
328 value = nextarg(option, argvp);
329 ftsoptions &= ~FTS_NOSTAT;
331 new = palloc(option);
332 new->t_data.tv_sec = find_parsetime(new, option->name, value);
333 new->t_data.tv_nsec = 0;
334 if (!(new->flags & F_EXACTTIME))
340 * -maxdepth/-mindepth n functions --
342 * Does the same as -prune if the level of the current file is
343 * greater/less than the specified maximum/minimum depth.
345 * Note that -maxdepth and -mindepth are handled specially in
346 * find_execute() so their f_* functions are set to f_always_true().
349 c_mXXdepth(OPTION *option, char ***argvp)
354 dstr = nextarg(option, argvp);
356 /* all other errors handled by find_parsenum() */
357 errx(1, "%s: %s: value must be positive", option->name, dstr);
359 new = palloc(option);
360 if (option->flags & F_MAXDEPTH)
361 maxdepth = find_parsenum(new, option->name, dstr, NULL);
363 mindepth = find_parsenum(new, option->name, dstr, NULL);
371 * Show files with EXTENDED ACL attributes.
374 f_acl(PLAN *plan __unused, FTSENT *entry)
378 int acl_supported = 0, ret, trivial;
380 if (S_ISLNK(entry->fts_statp->st_mode))
382 ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4);
385 acl_type = ACL_TYPE_NFS4;
386 } else if (ret < 0 && errno != EINVAL) {
387 warn("%s", entry->fts_accpath);
390 if (acl_supported == 0) {
391 ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED);
394 acl_type = ACL_TYPE_ACCESS;
395 } else if (ret < 0 && errno != EINVAL) {
396 warn("%s", entry->fts_accpath);
400 if (acl_supported == 0)
403 facl = acl_get_file(entry->fts_accpath, acl_type);
405 warn("%s", entry->fts_accpath);
408 ret = acl_is_trivial_np(facl, &trivial);
411 warn("%s", entry->fts_accpath);
421 c_acl(OPTION *option, char ***argvp __unused)
423 ftsoptions &= ~FTS_NOSTAT;
424 return (palloc(option));
428 * -delete functions --
430 * True always. Makes its best shot and continues on regardless.
433 f_delete(PLAN *plan __unused, FTSENT *entry)
435 /* ignore these from fts */
436 if (strcmp(entry->fts_accpath, ".") == 0 ||
437 strcmp(entry->fts_accpath, "..") == 0)
441 if (isdepth == 0 || /* depth off */
442 (ftsoptions & FTS_NOSTAT)) /* not stat()ing */
443 errx(1, "-delete: insecure options got turned on");
445 if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */
446 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
447 errx(1, "-delete: forbidden when symlinks are followed");
449 /* Potentially unsafe - do not accept relative paths whatsoever */
450 if (entry->fts_level > FTS_ROOTLEVEL &&
451 strchr(entry->fts_accpath, '/') != NULL)
452 errx(1, "-delete: %s: relative path potentially not safe",
455 #if HAVE_STRUCT_STAT_ST_FLAGS
456 /* Turn off user immutable bits if running as root */
457 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
458 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
460 lchflags(entry->fts_accpath,
461 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
464 /* rmdir directories, unlink everything else */
465 if (S_ISDIR(entry->fts_statp->st_mode)) {
466 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
467 warn("-delete: rmdir(%s)", entry->fts_path);
469 if (unlink(entry->fts_accpath) < 0)
470 warn("-delete: unlink(%s)", entry->fts_path);
478 c_delete(OPTION *option, char ***argvp __unused)
481 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
482 isoutput = 1; /* possible output */
483 isdepth = 1; /* -depth implied */
486 * Try to avoid the confusing error message about relative paths
487 * being potentially not safe.
489 if (ftsoptions & FTS_NOCHDIR)
490 errx(1, "%s: forbidden when the current directory cannot be opened",
493 return palloc(option);
500 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
503 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
509 * -depth functions --
511 * With argument: True if the file is at level n.
512 * Without argument: Always true, causes descent of the directory hierarchy
513 * to be done so that all entries in a directory are acted on before the
517 f_depth(PLAN *plan, FTSENT *entry)
519 if (plan->flags & F_DEPTH)
520 COMPARE(entry->fts_level, plan->d_data);
526 c_depth(OPTION *option, char ***argvp)
531 new = palloc(option);
534 if (str && !(new->flags & F_DEPTH)) {
535 /* skip leading + or - */
536 if (*str == '+' || *str == '-')
539 if (*str == '+' || *str == '-')
542 new->flags |= F_DEPTH;
545 if (new->flags & F_DEPTH) { /* -depth n */
548 ndepth = nextarg(option, argvp);
549 new->d_data = find_parsenum(new, option->name, ndepth, NULL);
558 * -empty functions --
560 * True if the file or directory is empty
563 f_empty(PLAN *plan __unused, FTSENT *entry)
565 if (S_ISREG(entry->fts_statp->st_mode) &&
566 entry->fts_statp->st_size == 0)
568 if (S_ISDIR(entry->fts_statp->st_mode)) {
574 dir = opendir(entry->fts_accpath);
577 for (dp = readdir(dir); dp; dp = readdir(dir))
578 if (dp->d_name[0] != '.' ||
579 (dp->d_name[1] != '\0' &&
580 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
591 c_empty(OPTION *option, char ***argvp __unused)
593 ftsoptions &= ~FTS_NOSTAT;
595 return palloc(option);
599 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
601 * True if the executed utility returns a zero value as exit status.
602 * The end of the primary expression is delimited by a semicolon. If
603 * "{}" occurs anywhere, it gets replaced by the current pathname,
604 * or, in the case of -execdir, the current basename (filename
605 * without leading directory prefix). For -exec and -ok,
606 * the current directory for the execution of utility is the same as
607 * the current directory when the find utility was started, whereas
608 * for -execdir, it is the directory the file resides in.
610 * The primary -ok differs from -exec in that it requests affirmation
611 * of the user before executing the utility.
614 f_exec(PLAN *plan, FTSENT *entry)
621 if (entry == NULL && plan->flags & F_EXECPLUS) {
622 if (plan->e_ppos == plan->e_pbnum)
624 plan->e_argv[plan->e_ppos] = NULL;
628 /* XXX - if file/dir ends in '/' this will not work -- can it? */
629 if ((plan->flags & F_EXECDIR) && \
630 (file = strrchr(entry->fts_path, '/')))
633 file = entry->fts_path;
635 if (plan->flags & F_EXECPLUS) {
636 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
638 plan->e_len[plan->e_ppos] = strlen(file);
639 plan->e_psize += plan->e_len[plan->e_ppos];
640 if (++plan->e_ppos < plan->e_pnummax &&
641 plan->e_psize < plan->e_psizemax)
643 plan->e_argv[plan->e_ppos] = NULL;
645 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
646 if (plan->e_len[cnt])
647 brace_subst(plan->e_orig[cnt],
648 &plan->e_argv[cnt], file,
652 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
655 /* make sure find output is interspersed correctly with subprocesses */
659 switch (pid = fork()) {
664 /* change dir back from where we started */
665 if (!(plan->flags & F_EXECDIR) &&
666 !(ftsoptions & FTS_NOCHDIR) && fchdir(dotfd)) {
670 execvp(plan->e_argv[0], plan->e_argv);
671 warn("%s", plan->e_argv[0]);
674 if (plan->flags & F_EXECPLUS) {
675 while (--plan->e_ppos >= plan->e_pbnum)
676 free(plan->e_argv[plan->e_ppos]);
677 plan->e_ppos = plan->e_pbnum;
678 plan->e_psize = plan->e_pbsize;
680 pid = waitpid(pid, &status, 0);
681 if (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status))
683 if (plan->flags & F_EXECPLUS) {
691 * c_exec, c_execdir, c_ok --
692 * build three parallel arrays, one with pointers to the strings passed
693 * on the command line, one with (possibly duplicated) pointers to the
694 * argv array, and one with integer values that are lengths of the
695 * strings, but also flags meaning that the string has to be massaged.
698 c_exec(OPTION *option, char ***argvp)
700 PLAN *new; /* node returned */
703 char **argv, **ap, **ep, *p;
705 /* This would defeat -execdir's intended security. */
706 if (option->flags & F_EXECDIR && ftsoptions & FTS_NOCHDIR)
707 errx(1, "%s: forbidden when the current directory cannot be opened",
710 /* XXX - was in c_execdir, but seems unnecessary!?
711 ftsoptions &= ~FTS_NOSTAT;
715 /* XXX - this is a change from the previous coding */
716 new = palloc(option);
718 for (ap = argv = *argvp;; ++ap) {
721 "%s: no terminating \";\" or \"+\"", option->name);
724 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
725 new->flags |= F_EXECPLUS;
731 errx(1, "%s: no command specified", option->name);
733 cnt = ap - *argvp + 1;
734 if (new->flags & F_EXECPLUS) {
735 new->e_ppos = new->e_pbnum = cnt - 2;
736 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
737 warn("sysconf(_SC_ARG_MAX)");
738 argmax = _POSIX_ARG_MAX;
741 for (ep = environ; *ep != NULL; ep++)
742 argmax -= strlen(*ep) + 1 + sizeof(*ep);
743 argmax -= 1 + sizeof(*ep);
745 * Ensure that -execdir ... {} + does not mix files
746 * from different directories in one invocation.
747 * Files from the same directory should be handled
748 * in one invocation but there is no code for it.
750 new->e_pnummax = new->flags & F_EXECDIR ? 1 : argmax / 16;
751 argmax -= sizeof(char *) * new->e_pnummax;
753 errx(1, "no space for arguments");
754 new->e_psizemax = argmax;
756 cnt += new->e_pnummax + 1;
757 new->e_next = lastexecplus;
760 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
762 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
764 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
767 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
768 new->e_orig[cnt] = *argv;
769 if (new->flags & F_EXECPLUS)
770 new->e_pbsize += strlen(*argv) + 1;
771 for (p = *argv; *p; ++p)
772 if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
774 if ((new->e_argv[cnt] =
775 malloc(MAXPATHLEN)) == NULL)
777 new->e_len[cnt] = MAXPATHLEN;
781 new->e_argv[cnt] = *argv;
785 if (new->flags & F_EXECPLUS) {
786 new->e_psize = new->e_pbsize;
788 for (i = 0; i < new->e_pnummax; i++) {
789 new->e_argv[cnt] = NULL;
796 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
798 done: *argvp = argv + 1;
802 /* Finish any pending -exec ... {} + functions. */
804 finish_execplus(void)
810 (p->execute)(p, NULL);
815 #if HAVE_STRUCT_STAT_ST_FLAGS
817 f_flags(PLAN *plan, FTSENT *entry)
821 flags = entry->fts_statp->st_flags;
822 if (plan->flags & F_ATLEAST)
823 return (flags | plan->fl_flags) == flags &&
824 !(flags & plan->fl_notflags);
825 else if (plan->flags & F_ANY)
826 return (flags & plan->fl_flags) ||
827 (flags | plan->fl_notflags) != flags;
829 return flags == plan->fl_flags &&
830 !(plan->fl_flags & plan->fl_notflags);
834 c_flags(OPTION *option, char ***argvp)
838 u_long flags, notflags;
840 flags_str = nextarg(option, argvp);
841 ftsoptions &= ~FTS_NOSTAT;
843 new = palloc(option);
845 if (*flags_str == '-') {
846 new->flags |= F_ATLEAST;
848 } else if (*flags_str == '+') {
852 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
853 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
855 new->fl_flags = flags;
856 new->fl_notflags = notflags;
862 * -follow functions --
864 * Always true, causes symbolic links to be followed on a global
868 c_follow(OPTION *option, char ***argvp __unused)
870 ftsoptions &= ~FTS_PHYSICAL;
871 ftsoptions |= FTS_LOGICAL;
873 return palloc(option);
876 #if HAVE_STRUCT_STATFS_F_FSTYPENAME
878 * -fstype functions --
880 * True if the file is of a certain type.
883 f_fstype(PLAN *plan, FTSENT *entry)
885 static dev_t curdev; /* need a guaranteed illegal dev value */
886 static int first = 1;
888 static int val_flags;
889 static char fstype[sizeof(sb.f_fstypename)];
890 char *p, save[2] = {0,0};
892 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
895 /* Only check when we cross mount point. */
896 if (first || curdev != entry->fts_statp->st_dev) {
897 curdev = entry->fts_statp->st_dev;
900 * Statfs follows symlinks; find wants the link's filesystem,
901 * not where it points.
903 if (entry->fts_info == FTS_SL ||
904 entry->fts_info == FTS_SLNONE) {
905 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
908 p = entry->fts_accpath;
916 if (statfs(entry->fts_accpath, &sb)) {
917 if (!ignore_readdir_race || errno != ENOENT) {
918 warn("statfs: %s", entry->fts_accpath);
932 * Further tests may need both of these values, so
933 * always copy both of them.
935 val_flags = sb.f_flags;
936 strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
938 switch (plan->flags & F_MTMASK) {
940 return val_flags & plan->mt_data;
942 return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
949 c_fstype(OPTION *option, char ***argvp)
954 fsname = nextarg(option, argvp);
955 ftsoptions &= ~FTS_NOSTAT;
957 new = palloc(option);
960 if (!strcmp(fsname, "local")) {
961 new->flags |= F_MTFLAG;
962 new->mt_data = MNT_LOCAL;
967 if (!strcmp(fsname, "rdonly")) {
968 new->flags |= F_MTFLAG;
969 new->mt_data = MNT_RDONLY;
975 new->flags |= F_MTTYPE;
976 new->c_data = fsname;
982 * -group gname functions --
984 * True if the file belongs to the group gname. If gname is numeric and
985 * an equivalent of the getgrnam() function does not return a valid group
986 * name, gname is taken as a group ID.
989 f_group(PLAN *plan, FTSENT *entry)
991 COMPARE(entry->fts_statp->st_gid, plan->g_data);
995 c_group(OPTION *option, char ***argvp)
1002 gname = nextarg(option, argvp);
1003 ftsoptions &= ~FTS_NOSTAT;
1005 new = palloc(option);
1006 g = getgrnam(gname);
1009 if (gname[0] == '-' || gname[0] == '+')
1012 if (gid == 0 && gname[0] != '0')
1013 errx(1, "%s: %s: no such group", option->name, gname);
1014 gid = find_parsenum(new, option->name, cp, NULL);
1023 * -ignore_readdir_race functions --
1025 * Always true. Ignore errors which occur if a file or a directory
1026 * in a starting point gets deleted between reading the name and calling
1027 * stat on it while find is traversing the starting point.
1031 c_ignore_readdir_race(OPTION *option, char ***argvp __unused)
1033 if (strcmp(option->name, "-ignore_readdir_race") == 0)
1034 ignore_readdir_race = 1;
1036 ignore_readdir_race = 0;
1038 return palloc(option);
1042 * -inum n functions --
1044 * True if the file has inode # n.
1047 f_inum(PLAN *plan, FTSENT *entry)
1049 COMPARE(entry->fts_statp->st_ino, plan->i_data);
1053 c_inum(OPTION *option, char ***argvp)
1058 inum_str = nextarg(option, argvp);
1059 ftsoptions &= ~FTS_NOSTAT;
1061 new = palloc(option);
1062 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
1069 * True if the file has the same inode (eg hard link) FN
1072 /* f_samefile is just f_inum */
1074 c_samefile(OPTION *option, char ***argvp)
1081 fn = nextarg(option, argvp);
1082 ftsoptions &= ~FTS_NOSTAT;
1084 new = palloc(option);
1085 if (ftsoptions & FTS_PHYSICAL)
1086 error = lstat(fn, &sb);
1088 error = stat(fn, &sb);
1091 new->i_data = sb.st_ino;
1096 * -links n functions --
1098 * True if the file has n links.
1101 f_links(PLAN *plan, FTSENT *entry)
1103 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1107 c_links(OPTION *option, char ***argvp)
1112 nlinks = nextarg(option, argvp);
1113 ftsoptions &= ~FTS_NOSTAT;
1115 new = palloc(option);
1116 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1123 * Always true - prints the current entry to stdout in "ls" format.
1126 f_ls(PLAN *plan __unused, FTSENT *entry)
1128 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1133 c_ls(OPTION *option, char ***argvp __unused)
1135 ftsoptions &= ~FTS_NOSTAT;
1138 return palloc(option);
1142 * -name functions --
1144 * True if the basename of the filename being examined
1145 * matches pattern using Pattern Matching Notation S3.14
1148 f_name(PLAN *plan, FTSENT *entry)
1154 if (plan->flags & F_LINK) {
1156 * The below test both avoids obviously useless readlink()
1157 * calls and ensures that symlinks with existent target do
1158 * not match if symlinks are being followed.
1159 * Assumption: fts will stat all symlinks that are to be
1160 * followed and will return the stat information.
1162 if (entry->fts_info != FTS_NSOK && entry->fts_info != FTS_SL &&
1163 entry->fts_info != FTS_SLNONE)
1165 len = readlink(entry->fts_accpath, fn, sizeof(fn) - 1);
1171 name = entry->fts_name;
1172 return !fnmatch(plan->c_data, name,
1173 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1177 c_name(OPTION *option, char ***argvp)
1182 pattern = nextarg(option, argvp);
1183 new = palloc(option);
1184 new->c_data = pattern;
1189 * -newer file functions --
1191 * True if the current file has been modified more recently
1192 * then the modification time of the file named by the pathname
1196 f_newer(PLAN *plan, FTSENT *entry)
1200 if (plan->flags & F_TIME_C)
1201 ft = entry->fts_statp->st_ctim;
1202 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
1203 else if (plan->flags & F_TIME_A)
1204 ft = entry->fts_statp->st_atim;
1205 else if (plan->flags & F_TIME_B)
1206 ft = entry->fts_statp->st_birthtim;
1209 ft = entry->fts_statp->st_mtim;
1210 return (ft.tv_sec > plan->t_data.tv_sec ||
1211 (ft.tv_sec == plan->t_data.tv_sec &&
1212 ft.tv_nsec > plan->t_data.tv_nsec));
1216 c_newer(OPTION *option, char ***argvp)
1223 fn_or_tspec = nextarg(option, argvp);
1224 ftsoptions &= ~FTS_NOSTAT;
1226 new = palloc(option);
1227 /* compare against what */
1228 if (option->flags & F_TIME2_T) {
1229 new->t_data.tv_sec = get_date(fn_or_tspec);
1230 if (new->t_data.tv_sec == (time_t) -1)
1231 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1232 /* Use the seconds only in the comparison. */
1233 new->t_data.tv_nsec = 999999999;
1235 if (ftsoptions & FTS_PHYSICAL)
1236 error = lstat(fn_or_tspec, &sb);
1238 error = stat(fn_or_tspec, &sb);
1240 err(1, "%s", fn_or_tspec);
1241 if (option->flags & F_TIME2_C)
1242 new->t_data = sb.st_ctim;
1243 else if (option->flags & F_TIME2_A)
1244 new->t_data = sb.st_atim;
1245 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
1246 else if (option->flags & F_TIME2_B)
1247 new->t_data = sb.st_birthtim;
1250 new->t_data = sb.st_mtim;
1256 * -nogroup functions --
1258 * True if file belongs to a user ID for which the equivalent
1259 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1262 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1264 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1268 c_nogroup(OPTION *option, char ***argvp __unused)
1270 ftsoptions &= ~FTS_NOSTAT;
1272 return palloc(option);
1276 * -nouser functions --
1278 * True if file belongs to a user ID for which the equivalent
1279 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1282 f_nouser(PLAN *plan __unused, FTSENT *entry)
1284 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1288 c_nouser(OPTION *option, char ***argvp __unused)
1290 ftsoptions &= ~FTS_NOSTAT;
1292 return palloc(option);
1296 * -path functions --
1298 * True if the path of the filename being examined
1299 * matches pattern using Pattern Matching Notation S3.14
1302 f_path(PLAN *plan, FTSENT *entry)
1304 return !fnmatch(plan->c_data, entry->fts_path,
1305 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1308 /* c_path is the same as c_name */
1311 * -perm functions --
1313 * The mode argument is used to represent file mode bits. If it starts
1314 * with a leading digit, it's treated as an octal mode, otherwise as a
1318 f_perm(PLAN *plan, FTSENT *entry)
1322 mode = entry->fts_statp->st_mode &
1323 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1324 if (plan->flags & F_ATLEAST)
1325 return (plan->m_data | mode) == mode;
1326 else if (plan->flags & F_ANY)
1327 return (mode & plan->m_data);
1329 return mode == plan->m_data;
1334 c_perm(OPTION *option, char ***argvp)
1340 perm = nextarg(option, argvp);
1341 ftsoptions &= ~FTS_NOSTAT;
1343 new = palloc(option);
1346 new->flags |= F_ATLEAST;
1348 } else if (*perm == '+') {
1349 new->flags |= F_ANY;
1353 if ((set = setmode(perm)) == NULL)
1354 errx(1, "%s: %s: illegal mode string", option->name, perm);
1356 new->m_data = getmode(set, 0);
1362 * -print functions --
1364 * Always true, causes the current pathname to be written to
1368 f_print(PLAN *plan __unused, FTSENT *entry)
1370 (void)puts(entry->fts_path);
1375 c_print(OPTION *option, char ***argvp __unused)
1379 return palloc(option);
1383 * -print0 functions --
1385 * Always true, causes the current pathname to be written to
1386 * standard output followed by a NUL character
1389 f_print0(PLAN *plan __unused, FTSENT *entry)
1391 fputs(entry->fts_path, stdout);
1392 fputc('\0', stdout);
1396 /* c_print0 is the same as c_print */
1399 * -prune functions --
1401 * Prune a portion of the hierarchy.
1404 f_prune(PLAN *plan __unused, FTSENT *entry)
1406 if (fts_set(tree, entry, FTS_SKIP))
1407 err(1, "%s", entry->fts_path);
1411 /* c_prune == c_simple */
1414 * -regex functions --
1416 * True if the whole path of the file matches pattern using
1417 * regular expression.
1420 f_regex(PLAN *plan, FTSENT *entry)
1427 char errbuf[LINE_MAX];
1430 pre = plan->re_data;
1431 str = entry->fts_path;
1438 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1440 if (errcode != 0 && errcode != REG_NOMATCH) {
1441 regerror(errcode, pre, errbuf, sizeof errbuf);
1443 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1446 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1453 c_regex(OPTION *option, char ***argvp)
1459 char errbuf[LINE_MAX];
1461 if ((pre = malloc(sizeof(regex_t))) == NULL)
1464 pattern = nextarg(option, argvp);
1466 if ((errcode = regcomp(pre, pattern,
1467 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1468 regerror(errcode, pre, errbuf, sizeof errbuf);
1469 errx(1, "%s: %s: %s",
1470 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1474 new = palloc(option);
1480 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1483 c_simple(OPTION *option, char ***argvp __unused)
1485 return palloc(option);
1489 * -size n[c] functions --
1491 * True if the file size in bytes, divided by an implementation defined
1492 * value and rounded up to the next integer, is n. If n is followed by
1493 * one of c k M G T P, the size is in bytes, kilobytes,
1494 * megabytes, gigabytes, terabytes or petabytes respectively.
1496 #define FIND_SIZE 512
1497 static int divsize = 1;
1500 f_size(PLAN *plan, FTSENT *entry)
1504 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1505 FIND_SIZE : entry->fts_statp->st_size;
1506 COMPARE(size, plan->o_data);
1510 c_size(OPTION *option, char ***argvp)
1517 size_str = nextarg(option, argvp);
1518 ftsoptions &= ~FTS_NOSTAT;
1520 new = palloc(option);
1522 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1523 if (endch != '\0') {
1527 case 'c': /* characters */
1530 case 'k': /* kilobytes 1<<10 */
1533 case 'M': /* megabytes 1<<20 */
1536 case 'G': /* gigabytes 1<<30 */
1537 scale = 0x40000000LL;
1539 case 'T': /* terabytes 1<<40 */
1540 scale = 0x10000000000LL;
1542 case 'P': /* petabytes 1<<50 */
1543 scale = 0x4000000000000LL;
1546 errx(1, "%s: %s: illegal trailing character",
1547 option->name, size_str);
1550 if (new->o_data > QUAD_MAX / scale)
1551 errx(1, "%s: %s: value too large",
1552 option->name, size_str);
1553 new->o_data *= scale;
1559 * -sparse functions --
1561 * Check if a file is sparse by finding if it occupies fewer blocks
1562 * than we expect based on its size.
1565 f_sparse(PLAN *plan __unused, FTSENT *entry)
1567 off_t expected_blocks;
1569 expected_blocks = (entry->fts_statp->st_size + 511) / 512;
1570 return entry->fts_statp->st_blocks < expected_blocks;
1574 c_sparse(OPTION *option, char ***argvp __unused)
1576 ftsoptions &= ~FTS_NOSTAT;
1578 return palloc(option);
1582 * -type c functions --
1584 * True if the type of the file is c, where c is b, c, d, p, f or w
1585 * for block special file, character special file, directory, FIFO,
1586 * regular file or whiteout respectively.
1589 f_type(PLAN *plan, FTSENT *entry)
1591 if (plan->m_data == S_IFDIR)
1592 return (entry->fts_info == FTS_D || entry->fts_info == FTS_DC ||
1593 entry->fts_info == FTS_DNR || entry->fts_info == FTS_DOT ||
1594 entry->fts_info == FTS_DP);
1596 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1600 c_type(OPTION *option, char ***argvp)
1606 typestring = nextarg(option, argvp);
1607 if (typestring[0] != 'd')
1608 ftsoptions &= ~FTS_NOSTAT;
1610 switch (typestring[0]) {
1632 #if defined(FTS_WHITEOUT) && defined(S_IFWHT)
1635 ftsoptions |= FTS_WHITEOUT;
1637 #endif /* FTS_WHITEOUT */
1639 errx(1, "%s: %s: unknown type", option->name, typestring);
1642 new = palloc(option);
1648 * -user uname functions --
1650 * True if the file belongs to the user uname. If uname is numeric and
1651 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1652 * return a valid user name, uname is taken as a user ID.
1655 f_user(PLAN *plan, FTSENT *entry)
1657 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1661 c_user(OPTION *option, char ***argvp)
1668 username = nextarg(option, argvp);
1669 ftsoptions &= ~FTS_NOSTAT;
1671 new = palloc(option);
1672 p = getpwnam(username);
1674 char* cp = username;
1675 if( username[0] == '-' || username[0] == '+' )
1677 uid = atoi(username);
1678 if (uid == 0 && username[0] != '0')
1679 errx(1, "%s: %s: no such user", option->name, username);
1680 uid = find_parsenum(new, option->name, cp, NULL);
1689 * -xdev functions --
1691 * Always true, causes find not to descend past directories that have a
1692 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1695 c_xdev(OPTION *option, char ***argvp __unused)
1697 ftsoptions |= FTS_XDEV;
1699 return palloc(option);
1703 * ( expression ) functions --
1705 * True if expression is true.
1708 f_expr(PLAN *plan, FTSENT *entry)
1713 for (p = plan->p_data[0];
1714 p && (state = (p->execute)(p, entry)); p = p->next);
1719 * f_openparen and f_closeparen nodes are temporary place markers. They are
1720 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1721 * to a f_expr node containing the expression and the ')' node is discarded.
1722 * The functions themselves are only used as constants.
1726 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1732 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1737 /* c_openparen == c_simple */
1738 /* c_closeparen == c_simple */
1741 * AND operator. Since AND is implicit, no node is allocated.
1744 c_and(OPTION *option __unused, char ***argvp __unused)
1750 * ! expression functions --
1752 * Negation of a primary; the unary NOT operator.
1755 f_not(PLAN *plan, FTSENT *entry)
1760 for (p = plan->p_data[0];
1761 p && (state = (p->execute)(p, entry)); p = p->next);
1765 /* c_not == c_simple */
1768 * expression -o expression functions --
1770 * Alternation of primaries; the OR operator. The second expression is
1771 * not evaluated if the first expression is true.
1774 f_or(PLAN *plan, FTSENT *entry)
1779 for (p = plan->p_data[0];
1780 p && (state = (p->execute)(p, entry)); p = p->next);
1785 for (p = plan->p_data[1];
1786 p && (state = (p->execute)(p, entry)); p = p->next);
1790 /* c_or == c_simple */
1798 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1803 /* c_false == c_simple */
1811 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1817 /* c_quit == c_simple */