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
37 static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/ucred.h>
47 #include <sys/types.h>
50 #include <sys/mount.h>
69 static PLAN *palloc(OPTION *);
70 static long long find_parsenum(PLAN *, const char *, char *, char *);
71 static long long find_parsetime(PLAN *, const char *, char *);
72 static char *nextarg(OPTION *, char ***);
74 extern char **environ;
76 static PLAN *lastexecplus = NULL;
78 #define COMPARE(a, b) do { \
79 switch (plan->flags & F_ELG_MASK) { \
92 palloc(OPTION *option)
96 if ((new = malloc(sizeof(PLAN))) == NULL)
98 new->execute = option->execute;
99 new->flags = option->flags;
106 * Parse a string of the form [+-]# and return the value.
109 find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
112 char *endchar, *str; /* Pointer to character ending conversion. */
114 /* Determine comparison from leading + or -. */
119 plan->flags |= F_GREATER;
123 plan->flags |= F_LESSTHAN;
126 plan->flags |= F_EQUAL;
131 * Convert the string with strtoq(). Note, if strtoq() returns zero
132 * and endchar points to the beginning of the string we know we have
135 value = strtoq(str, &endchar, 10);
136 if (value == 0 && endchar == str)
137 errx(1, "%s: %s: illegal numeric value", option, vp);
138 if (endchar[0] && endch == NULL)
139 errx(1, "%s: %s: illegal trailing character", option, vp);
147 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
150 find_parsetime(PLAN *plan, const char *option, char *vp)
152 long long secs, value;
153 char *str, *unit; /* Pointer to character ending conversion. */
155 /* Determine comparison from leading + or -. */
160 plan->flags |= F_GREATER;
164 plan->flags |= F_LESSTHAN;
167 plan->flags |= F_EQUAL;
171 value = strtoq(str, &unit, 10);
172 if (value == 0 && unit == str) {
173 errx(1, "%s: %s: illegal time value", option, vp);
183 case 's': /* seconds */
186 case 'm': /* minutes */
189 case 'h': /* hours */
190 secs += value * 3600;
193 secs += value * 86400;
195 case 'w': /* weeks */
196 secs += value * 604800;
199 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
203 if (*str == '\0') /* EOS */
205 value = strtoq(str, &unit, 10);
206 if (value == 0 && unit == str) {
207 errx(1, "%s: %s: illegal time value", option, vp);
211 errx(1, "%s: %s: missing trailing unit", option, vp);
215 plan->flags |= F_EXACTTIME;
221 * Check that another argument still exists, return a pointer to it,
222 * and increment the argument vector pointer.
225 nextarg(OPTION *option, char ***argvp)
229 if ((arg = **argvp) == NULL)
230 errx(1, "%s: requires additional arguments", option->name);
236 * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
237 * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts
238 * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
239 * the user wanted. Correct so that -1 is "less than 1".
241 #define TIME_CORRECT(p) \
242 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
243 ++((p)->t_data.tv_sec);
246 * -[acm]min n functions --
248 * True if the difference between the
249 * file access time (-amin)
250 * file birth time (-Bmin)
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.tv_sec);
261 } else if (plan->flags & F_TIME_A) {
262 COMPARE((now - entry->fts_statp->st_atime +
263 60 - 1) / 60, plan->t_data.tv_sec);
264 } else if (plan->flags & F_TIME_B) {
265 COMPARE((now - entry->fts_statp->st_birthtime +
266 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 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.tv_sec);
317 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data.tv_sec);
321 c_Xtime(OPTION *option, char ***argvp)
326 value = nextarg(option, argvp);
327 ftsoptions &= ~FTS_NOSTAT;
329 new = palloc(option);
330 new->t_data.tv_sec = find_parsetime(new, option->name, value);
331 new->t_data.tv_nsec = 0;
332 if (!(new->flags & F_EXACTTIME))
338 * -maxdepth/-mindepth n functions --
340 * Does the same as -prune if the level of the current file is
341 * greater/less than the specified maximum/minimum depth.
343 * Note that -maxdepth and -mindepth are handled specially in
344 * find_execute() so their f_* functions are set to f_always_true().
347 c_mXXdepth(OPTION *option, char ***argvp)
352 dstr = nextarg(option, argvp);
354 /* all other errors handled by find_parsenum() */
355 errx(1, "%s: %s: value must be positive", option->name, dstr);
357 new = palloc(option);
358 if (option->flags & F_MAXDEPTH)
359 maxdepth = find_parsenum(new, option->name, dstr, NULL);
361 mindepth = find_parsenum(new, option->name, dstr, NULL);
368 * Show files with EXTENDED ACL attributes.
371 f_acl(PLAN *plan __unused, FTSENT *entry)
375 int acl_supported = 0, ret, trivial;
377 if (S_ISLNK(entry->fts_statp->st_mode))
379 ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4);
382 acl_type = ACL_TYPE_NFS4;
383 } else if (ret < 0 && errno != EINVAL) {
384 warn("%s", entry->fts_accpath);
387 if (acl_supported == 0) {
388 ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED);
391 acl_type = ACL_TYPE_ACCESS;
392 } else if (ret < 0 && errno != EINVAL) {
393 warn("%s", entry->fts_accpath);
397 if (acl_supported == 0)
400 facl = acl_get_file(entry->fts_accpath, acl_type);
402 warn("%s", entry->fts_accpath);
405 ret = acl_is_trivial_np(facl, &trivial);
408 warn("%s", entry->fts_accpath);
417 c_acl(OPTION *option, char ***argvp __unused)
419 ftsoptions &= ~FTS_NOSTAT;
420 return (palloc(option));
424 * -delete functions --
426 * True always. Makes its best shot and continues on regardless.
429 f_delete(PLAN *plan __unused, FTSENT *entry)
431 /* ignore these from fts */
432 if (strcmp(entry->fts_accpath, ".") == 0 ||
433 strcmp(entry->fts_accpath, "..") == 0)
437 if (isdepth == 0 || /* depth off */
438 (ftsoptions & FTS_NOSTAT)) /* not stat()ing */
439 errx(1, "-delete: insecure options got turned on");
441 if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */
442 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
443 errx(1, "-delete: forbidden when symlinks are followed");
445 /* Potentially unsafe - do not accept relative paths whatsoever */
446 if (entry->fts_level > FTS_ROOTLEVEL &&
447 strchr(entry->fts_accpath, '/') != NULL)
448 errx(1, "-delete: %s: relative path potentially not safe",
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)) &&
455 lchflags(entry->fts_accpath,
456 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
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);
463 if (unlink(entry->fts_accpath) < 0)
464 warn("-delete: unlink(%s)", entry->fts_path);
472 c_delete(OPTION *option, char ***argvp __unused)
475 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
476 isoutput = 1; /* possible output */
477 isdepth = 1; /* -depth implied */
480 * Try to avoid the confusing error message about relative paths
481 * being potentially not safe.
483 if (ftsoptions & FTS_NOCHDIR)
484 errx(1, "%s: forbidden when the current directory cannot be opened",
487 return palloc(option);
494 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
497 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
503 * -depth functions --
505 * With argument: True if the file is at level n.
506 * Without argument: Always true, causes descent of the directory hierarchy
507 * to be done so that all entries in a directory are acted on before the
511 f_depth(PLAN *plan, FTSENT *entry)
513 if (plan->flags & F_DEPTH)
514 COMPARE(entry->fts_level, plan->d_data);
520 c_depth(OPTION *option, char ***argvp)
525 new = palloc(option);
528 if (str && !(new->flags & F_DEPTH)) {
529 /* skip leading + or - */
530 if (*str == '+' || *str == '-')
533 if (*str == '+' || *str == '-')
536 new->flags |= F_DEPTH;
539 if (new->flags & F_DEPTH) { /* -depth n */
542 ndepth = nextarg(option, argvp);
543 new->d_data = find_parsenum(new, option->name, ndepth, NULL);
552 * -empty functions --
554 * True if the file or directory is empty
557 f_empty(PLAN *plan __unused, FTSENT *entry)
559 if (S_ISREG(entry->fts_statp->st_mode) &&
560 entry->fts_statp->st_size == 0)
562 if (S_ISDIR(entry->fts_statp->st_mode)) {
568 dir = opendir(entry->fts_accpath);
571 for (dp = readdir(dir); dp; dp = readdir(dir))
572 if (dp->d_name[0] != '.' ||
573 (dp->d_name[1] != '\0' &&
574 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
585 c_empty(OPTION *option, char ***argvp __unused)
587 ftsoptions &= ~FTS_NOSTAT;
589 return palloc(option);
593 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
595 * True if the executed utility returns a zero value as exit status.
596 * The end of the primary expression is delimited by a semicolon. If
597 * "{}" occurs anywhere, it gets replaced by the current pathname,
598 * or, in the case of -execdir, the current basename (filename
599 * without leading directory prefix). For -exec and -ok,
600 * the current directory for the execution of utility is the same as
601 * the current directory when the find utility was started, whereas
602 * for -execdir, it is the directory the file resides in.
604 * The primary -ok differs from -exec in that it requests affirmation
605 * of the user before executing the utility.
608 f_exec(PLAN *plan, FTSENT *entry)
615 if (entry == NULL && plan->flags & F_EXECPLUS) {
616 if (plan->e_ppos == plan->e_pbnum)
618 plan->e_argv[plan->e_ppos] = NULL;
622 /* XXX - if file/dir ends in '/' this will not work -- can it? */
623 if ((plan->flags & F_EXECDIR) && \
624 (file = strrchr(entry->fts_path, '/')))
627 file = entry->fts_path;
629 if (plan->flags & F_EXECPLUS) {
630 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
632 plan->e_len[plan->e_ppos] = strlen(file);
633 plan->e_psize += plan->e_len[plan->e_ppos];
634 if (++plan->e_ppos < plan->e_pnummax &&
635 plan->e_psize < plan->e_psizemax)
637 plan->e_argv[plan->e_ppos] = NULL;
639 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
640 if (plan->e_len[cnt])
641 brace_subst(plan->e_orig[cnt],
642 &plan->e_argv[cnt], file,
646 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
649 /* make sure find output is interspersed correctly with subprocesses */
653 switch (pid = fork()) {
658 /* change dir back from where we started */
659 if (!(plan->flags & F_EXECDIR) &&
660 !(ftsoptions & FTS_NOCHDIR) && fchdir(dotfd)) {
664 execvp(plan->e_argv[0], plan->e_argv);
665 warn("%s", plan->e_argv[0]);
668 if (plan->flags & F_EXECPLUS) {
669 while (--plan->e_ppos >= plan->e_pbnum)
670 free(plan->e_argv[plan->e_ppos]);
671 plan->e_ppos = plan->e_pbnum;
672 plan->e_psize = plan->e_pbsize;
674 pid = waitpid(pid, &status, 0);
675 if (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status))
677 if (plan->flags & F_EXECPLUS) {
685 * c_exec, c_execdir, c_ok --
686 * build three parallel arrays, one with pointers to the strings passed
687 * on the command line, one with (possibly duplicated) pointers to the
688 * argv array, and one with integer values that are lengths of the
689 * strings, but also flags meaning that the string has to be massaged.
692 c_exec(OPTION *option, char ***argvp)
694 PLAN *new; /* node returned */
697 char **argv, **ap, **ep, *p;
699 /* This would defeat -execdir's intended security. */
700 if (option->flags & F_EXECDIR && ftsoptions & FTS_NOCHDIR)
701 errx(1, "%s: forbidden when the current directory cannot be opened",
704 /* XXX - was in c_execdir, but seems unnecessary!?
705 ftsoptions &= ~FTS_NOSTAT;
709 /* XXX - this is a change from the previous coding */
710 new = palloc(option);
712 for (ap = argv = *argvp;; ++ap) {
715 "%s: no terminating \";\" or \"+\"", option->name);
718 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
719 new->flags |= F_EXECPLUS;
725 errx(1, "%s: no command specified", option->name);
727 cnt = ap - *argvp + 1;
728 if (new->flags & F_EXECPLUS) {
729 new->e_ppos = new->e_pbnum = cnt - 2;
730 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
731 warn("sysconf(_SC_ARG_MAX)");
732 argmax = _POSIX_ARG_MAX;
735 for (ep = environ; *ep != NULL; ep++)
736 argmax -= strlen(*ep) + 1 + sizeof(*ep);
737 argmax -= 1 + sizeof(*ep);
739 * Ensure that -execdir ... {} + does not mix files
740 * from different directories in one invocation.
741 * Files from the same directory should be handled
742 * in one invocation but there is no code for it.
744 new->e_pnummax = new->flags & F_EXECDIR ? 1 : argmax / 16;
745 argmax -= sizeof(char *) * new->e_pnummax;
747 errx(1, "no space for arguments");
748 new->e_psizemax = argmax;
750 cnt += new->e_pnummax + 1;
751 new->e_next = lastexecplus;
754 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
756 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
758 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
761 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
762 new->e_orig[cnt] = *argv;
763 if (new->flags & F_EXECPLUS)
764 new->e_pbsize += strlen(*argv) + 1;
765 for (p = *argv; *p; ++p)
766 if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
768 if ((new->e_argv[cnt] =
769 malloc(MAXPATHLEN)) == NULL)
771 new->e_len[cnt] = MAXPATHLEN;
775 new->e_argv[cnt] = *argv;
779 if (new->flags & F_EXECPLUS) {
780 new->e_psize = new->e_pbsize;
782 for (i = 0; i < new->e_pnummax; i++) {
783 new->e_argv[cnt] = NULL;
790 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
792 done: *argvp = argv + 1;
796 /* Finish any pending -exec ... {} + functions. */
798 finish_execplus(void)
804 (p->execute)(p, NULL);
810 f_flags(PLAN *plan, FTSENT *entry)
814 flags = entry->fts_statp->st_flags;
815 if (plan->flags & F_ATLEAST)
816 return (flags | plan->fl_flags) == flags &&
817 !(flags & plan->fl_notflags);
818 else if (plan->flags & F_ANY)
819 return (flags & plan->fl_flags) ||
820 (flags | plan->fl_notflags) != flags;
822 return flags == plan->fl_flags &&
823 !(plan->fl_flags & plan->fl_notflags);
827 c_flags(OPTION *option, char ***argvp)
831 u_long flags, notflags;
833 flags_str = nextarg(option, argvp);
834 ftsoptions &= ~FTS_NOSTAT;
836 new = palloc(option);
838 if (*flags_str == '-') {
839 new->flags |= F_ATLEAST;
841 } else if (*flags_str == '+') {
845 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
846 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
848 new->fl_flags = flags;
849 new->fl_notflags = notflags;
854 * -follow functions --
856 * Always true, causes symbolic links to be followed on a global
860 c_follow(OPTION *option, char ***argvp __unused)
862 ftsoptions &= ~FTS_PHYSICAL;
863 ftsoptions |= FTS_LOGICAL;
865 return palloc(option);
869 * -fstype functions --
871 * True if the file is of a certain type.
874 f_fstype(PLAN *plan, FTSENT *entry)
876 static dev_t curdev; /* need a guaranteed illegal dev value */
877 static int first = 1;
879 static int val_flags;
880 static char fstype[sizeof(sb.f_fstypename)];
881 char *p, save[2] = {0,0};
883 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
886 /* Only check when we cross mount point. */
887 if (first || curdev != entry->fts_statp->st_dev) {
888 curdev = entry->fts_statp->st_dev;
891 * Statfs follows symlinks; find wants the link's filesystem,
892 * not where it points.
894 if (entry->fts_info == FTS_SL ||
895 entry->fts_info == FTS_SLNONE) {
896 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
899 p = entry->fts_accpath;
907 if (statfs(entry->fts_accpath, &sb)) {
908 if (!ignore_readdir_race || errno != ENOENT) {
909 warn("statfs: %s", entry->fts_accpath);
923 * Further tests may need both of these values, so
924 * always copy both of them.
926 val_flags = sb.f_flags;
927 strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
929 switch (plan->flags & F_MTMASK) {
931 return val_flags & plan->mt_data;
933 return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
940 c_fstype(OPTION *option, char ***argvp)
945 fsname = nextarg(option, argvp);
946 ftsoptions &= ~FTS_NOSTAT;
948 new = palloc(option);
951 if (!strcmp(fsname, "local")) {
952 new->flags |= F_MTFLAG;
953 new->mt_data = MNT_LOCAL;
958 if (!strcmp(fsname, "rdonly")) {
959 new->flags |= F_MTFLAG;
960 new->mt_data = MNT_RDONLY;
966 new->flags |= F_MTTYPE;
967 new->c_data = fsname;
972 * -group gname functions --
974 * True if the file belongs to the group gname. If gname is numeric and
975 * an equivalent of the getgrnam() function does not return a valid group
976 * name, gname is taken as a group ID.
979 f_group(PLAN *plan, FTSENT *entry)
981 COMPARE(entry->fts_statp->st_gid, plan->g_data);
985 c_group(OPTION *option, char ***argvp)
992 gname = nextarg(option, argvp);
993 ftsoptions &= ~FTS_NOSTAT;
995 new = palloc(option);
999 if (gname[0] == '-' || gname[0] == '+')
1002 if (gid == 0 && gname[0] != '0')
1003 errx(1, "%s: %s: no such group", option->name, gname);
1004 gid = find_parsenum(new, option->name, cp, NULL);
1013 * -ignore_readdir_race functions --
1015 * Always true. Ignore errors which occur if a file or a directory
1016 * in a starting point gets deleted between reading the name and calling
1017 * stat on it while find is traversing the starting point.
1021 c_ignore_readdir_race(OPTION *option, char ***argvp __unused)
1023 if (strcmp(option->name, "-ignore_readdir_race") == 0)
1024 ignore_readdir_race = 1;
1026 ignore_readdir_race = 0;
1028 return palloc(option);
1032 * -inum n functions --
1034 * True if the file has inode # n.
1037 f_inum(PLAN *plan, FTSENT *entry)
1039 COMPARE(entry->fts_statp->st_ino, plan->i_data);
1043 c_inum(OPTION *option, char ***argvp)
1048 inum_str = nextarg(option, argvp);
1049 ftsoptions &= ~FTS_NOSTAT;
1051 new = palloc(option);
1052 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
1059 * True if the file has the same inode (eg hard link) FN
1062 /* f_samefile is just f_inum */
1064 c_samefile(OPTION *option, char ***argvp)
1071 fn = nextarg(option, argvp);
1072 ftsoptions &= ~FTS_NOSTAT;
1074 new = palloc(option);
1075 if (ftsoptions & FTS_PHYSICAL)
1076 error = lstat(fn, &sb);
1078 error = stat(fn, &sb);
1081 new->i_data = sb.st_ino;
1086 * -links n functions --
1088 * True if the file has n links.
1091 f_links(PLAN *plan, FTSENT *entry)
1093 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1097 c_links(OPTION *option, char ***argvp)
1102 nlinks = nextarg(option, argvp);
1103 ftsoptions &= ~FTS_NOSTAT;
1105 new = palloc(option);
1106 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1113 * Always true - prints the current entry to stdout in "ls" format.
1116 f_ls(PLAN *plan __unused, FTSENT *entry)
1118 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1123 c_ls(OPTION *option, char ***argvp __unused)
1125 ftsoptions &= ~FTS_NOSTAT;
1128 return palloc(option);
1132 * -name functions --
1134 * True if the basename of the filename being examined
1135 * matches pattern using Pattern Matching Notation S3.14
1138 f_name(PLAN *plan, FTSENT *entry)
1144 if (plan->flags & F_LINK) {
1146 * The below test both avoids obviously useless readlink()
1147 * calls and ensures that symlinks with existent target do
1148 * not match if symlinks are being followed.
1149 * Assumption: fts will stat all symlinks that are to be
1150 * followed and will return the stat information.
1152 if (entry->fts_info != FTS_NSOK && entry->fts_info != FTS_SL &&
1153 entry->fts_info != FTS_SLNONE)
1155 len = readlink(entry->fts_accpath, fn, sizeof(fn) - 1);
1161 name = entry->fts_name;
1162 return !fnmatch(plan->c_data, name,
1163 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1167 c_name(OPTION *option, char ***argvp)
1172 pattern = nextarg(option, argvp);
1173 new = palloc(option);
1174 new->c_data = pattern;
1179 * -newer file functions --
1181 * True if the current file has been modified more recently
1182 * then the modification time of the file named by the pathname
1186 f_newer(PLAN *plan, FTSENT *entry)
1190 if (plan->flags & F_TIME_C)
1191 ft = entry->fts_statp->st_ctim;
1192 else if (plan->flags & F_TIME_A)
1193 ft = entry->fts_statp->st_atim;
1194 else if (plan->flags & F_TIME_B)
1195 ft = entry->fts_statp->st_birthtim;
1197 ft = entry->fts_statp->st_mtim;
1198 return (ft.tv_sec > plan->t_data.tv_sec ||
1199 (ft.tv_sec == plan->t_data.tv_sec &&
1200 ft.tv_nsec > plan->t_data.tv_nsec));
1204 c_newer(OPTION *option, char ***argvp)
1211 fn_or_tspec = nextarg(option, argvp);
1212 ftsoptions &= ~FTS_NOSTAT;
1214 new = palloc(option);
1215 /* compare against what */
1216 if (option->flags & F_TIME2_T) {
1217 new->t_data.tv_sec = get_date(fn_or_tspec);
1218 if (new->t_data.tv_sec == (time_t) -1)
1219 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1220 /* Use the seconds only in the comparison. */
1221 new->t_data.tv_nsec = 999999999;
1223 if (ftsoptions & FTS_PHYSICAL)
1224 error = lstat(fn_or_tspec, &sb);
1226 error = stat(fn_or_tspec, &sb);
1228 err(1, "%s", fn_or_tspec);
1229 if (option->flags & F_TIME2_C)
1230 new->t_data = sb.st_ctim;
1231 else if (option->flags & F_TIME2_A)
1232 new->t_data = sb.st_atim;
1233 else if (option->flags & F_TIME2_B)
1234 new->t_data = sb.st_birthtim;
1236 new->t_data = sb.st_mtim;
1242 * -nogroup functions --
1244 * True if file belongs to a user ID for which the equivalent
1245 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1248 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1250 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1254 c_nogroup(OPTION *option, char ***argvp __unused)
1256 ftsoptions &= ~FTS_NOSTAT;
1258 return palloc(option);
1262 * -nouser functions --
1264 * True if file belongs to a user ID for which the equivalent
1265 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1268 f_nouser(PLAN *plan __unused, FTSENT *entry)
1270 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1274 c_nouser(OPTION *option, char ***argvp __unused)
1276 ftsoptions &= ~FTS_NOSTAT;
1278 return palloc(option);
1282 * -path functions --
1284 * True if the path of the filename being examined
1285 * matches pattern using Pattern Matching Notation S3.14
1288 f_path(PLAN *plan, FTSENT *entry)
1290 return !fnmatch(plan->c_data, entry->fts_path,
1291 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1294 /* c_path is the same as c_name */
1297 * -perm functions --
1299 * The mode argument is used to represent file mode bits. If it starts
1300 * with a leading digit, it's treated as an octal mode, otherwise as a
1304 f_perm(PLAN *plan, FTSENT *entry)
1308 mode = entry->fts_statp->st_mode &
1309 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1310 if (plan->flags & F_ATLEAST)
1311 return (plan->m_data | mode) == mode;
1312 else if (plan->flags & F_ANY)
1313 return (mode & plan->m_data);
1315 return mode == plan->m_data;
1320 c_perm(OPTION *option, char ***argvp)
1326 perm = nextarg(option, argvp);
1327 ftsoptions &= ~FTS_NOSTAT;
1329 new = palloc(option);
1332 new->flags |= F_ATLEAST;
1334 } else if (*perm == '+') {
1335 new->flags |= F_ANY;
1339 if ((set = setmode(perm)) == NULL)
1340 errx(1, "%s: %s: illegal mode string", option->name, perm);
1342 new->m_data = getmode(set, 0);
1348 * -print functions --
1350 * Always true, causes the current pathname to be written to
1354 f_print(PLAN *plan __unused, FTSENT *entry)
1356 (void)puts(entry->fts_path);
1361 c_print(OPTION *option, char ***argvp __unused)
1365 return palloc(option);
1369 * -print0 functions --
1371 * Always true, causes the current pathname to be written to
1372 * standard output followed by a NUL character
1375 f_print0(PLAN *plan __unused, FTSENT *entry)
1377 fputs(entry->fts_path, stdout);
1378 fputc('\0', stdout);
1382 /* c_print0 is the same as c_print */
1385 * -prune functions --
1387 * Prune a portion of the hierarchy.
1390 f_prune(PLAN *plan __unused, FTSENT *entry)
1392 if (fts_set(tree, entry, FTS_SKIP))
1393 err(1, "%s", entry->fts_path);
1397 /* c_prune == c_simple */
1400 * -regex functions --
1402 * True if the whole path of the file matches pattern using
1403 * regular expression.
1406 f_regex(PLAN *plan, FTSENT *entry)
1413 char errbuf[LINE_MAX];
1416 pre = plan->re_data;
1417 str = entry->fts_path;
1424 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1426 if (errcode != 0 && errcode != REG_NOMATCH) {
1427 regerror(errcode, pre, errbuf, sizeof errbuf);
1429 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1432 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1439 c_regex(OPTION *option, char ***argvp)
1445 char errbuf[LINE_MAX];
1447 if ((pre = malloc(sizeof(regex_t))) == NULL)
1450 pattern = nextarg(option, argvp);
1452 if ((errcode = regcomp(pre, pattern,
1453 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1454 regerror(errcode, pre, errbuf, sizeof errbuf);
1455 errx(1, "%s: %s: %s",
1456 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1460 new = palloc(option);
1466 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1469 c_simple(OPTION *option, char ***argvp __unused)
1471 return palloc(option);
1475 * -size n[c] functions --
1477 * True if the file size in bytes, divided by an implementation defined
1478 * value and rounded up to the next integer, is n. If n is followed by
1479 * one of c k M G T P, the size is in bytes, kilobytes,
1480 * megabytes, gigabytes, terabytes or petabytes respectively.
1482 #define FIND_SIZE 512
1483 static int divsize = 1;
1486 f_size(PLAN *plan, FTSENT *entry)
1490 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1491 FIND_SIZE : entry->fts_statp->st_size;
1492 COMPARE(size, plan->o_data);
1496 c_size(OPTION *option, char ***argvp)
1503 size_str = nextarg(option, argvp);
1504 ftsoptions &= ~FTS_NOSTAT;
1506 new = palloc(option);
1508 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1509 if (endch != '\0') {
1513 case 'c': /* characters */
1516 case 'k': /* kilobytes 1<<10 */
1519 case 'M': /* megabytes 1<<20 */
1522 case 'G': /* gigabytes 1<<30 */
1523 scale = 0x40000000LL;
1525 case 'T': /* terabytes 1<<40 */
1526 scale = 0x10000000000LL;
1528 case 'P': /* petabytes 1<<50 */
1529 scale = 0x4000000000000LL;
1532 errx(1, "%s: %s: illegal trailing character",
1533 option->name, size_str);
1536 if (new->o_data > QUAD_MAX / scale)
1537 errx(1, "%s: %s: value too large",
1538 option->name, size_str);
1539 new->o_data *= scale;
1545 * -sparse functions --
1547 * Check if a file is sparse by finding if it occupies fewer blocks
1548 * than we expect based on its size.
1551 f_sparse(PLAN *plan __unused, FTSENT *entry)
1553 off_t expected_blocks;
1555 expected_blocks = (entry->fts_statp->st_size + 511) / 512;
1556 return entry->fts_statp->st_blocks < expected_blocks;
1560 c_sparse(OPTION *option, char ***argvp __unused)
1562 ftsoptions &= ~FTS_NOSTAT;
1564 return palloc(option);
1568 * -type c functions --
1570 * True if the type of the file is c, where c is b, c, d, p, f or w
1571 * for block special file, character special file, directory, FIFO,
1572 * regular file or whiteout respectively.
1575 f_type(PLAN *plan, FTSENT *entry)
1577 if (plan->m_data == S_IFDIR)
1578 return (entry->fts_info == FTS_D || entry->fts_info == FTS_DC ||
1579 entry->fts_info == FTS_DNR || entry->fts_info == FTS_DOT ||
1580 entry->fts_info == FTS_DP);
1582 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1586 c_type(OPTION *option, char ***argvp)
1592 typestring = nextarg(option, argvp);
1593 if (typestring[0] != 'd')
1594 ftsoptions &= ~FTS_NOSTAT;
1596 switch (typestring[0]) {
1621 ftsoptions |= FTS_WHITEOUT;
1623 #endif /* FTS_WHITEOUT */
1625 errx(1, "%s: %s: unknown type", option->name, typestring);
1628 new = palloc(option);
1634 * -user uname functions --
1636 * True if the file belongs to the user uname. If uname is numeric and
1637 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1638 * return a valid user name, uname is taken as a user ID.
1641 f_user(PLAN *plan, FTSENT *entry)
1643 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1647 c_user(OPTION *option, char ***argvp)
1654 username = nextarg(option, argvp);
1655 ftsoptions &= ~FTS_NOSTAT;
1657 new = palloc(option);
1658 p = getpwnam(username);
1660 char* cp = username;
1661 if( username[0] == '-' || username[0] == '+' )
1663 uid = atoi(username);
1664 if (uid == 0 && username[0] != '0')
1665 errx(1, "%s: %s: no such user", option->name, username);
1666 uid = find_parsenum(new, option->name, cp, NULL);
1675 * -xdev functions --
1677 * Always true, causes find not to descend past directories that have a
1678 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1681 c_xdev(OPTION *option, char ***argvp __unused)
1683 ftsoptions |= FTS_XDEV;
1685 return palloc(option);
1689 * ( expression ) functions --
1691 * True if expression is true.
1694 f_expr(PLAN *plan, FTSENT *entry)
1699 for (p = plan->p_data[0];
1700 p && (state = (p->execute)(p, entry)); p = p->next);
1705 * f_openparen and f_closeparen nodes are temporary place markers. They are
1706 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1707 * to a f_expr node containing the expression and the ')' node is discarded.
1708 * The functions themselves are only used as constants.
1712 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1718 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1723 /* c_openparen == c_simple */
1724 /* c_closeparen == c_simple */
1727 * AND operator. Since AND is implicit, no node is allocated.
1730 c_and(OPTION *option __unused, char ***argvp __unused)
1736 * ! expression functions --
1738 * Negation of a primary; the unary NOT operator.
1741 f_not(PLAN *plan, FTSENT *entry)
1746 for (p = plan->p_data[0];
1747 p && (state = (p->execute)(p, entry)); p = p->next);
1751 /* c_not == c_simple */
1754 * expression -o expression functions --
1756 * Alternation of primaries; the OR operator. The second expression is
1757 * not evaluated if the first expression is true.
1760 f_or(PLAN *plan, FTSENT *entry)
1765 for (p = plan->p_data[0];
1766 p && (state = (p->execute)(p, entry)); p = p->next);
1771 for (p = plan->p_data[1];
1772 p && (state = (p->execute)(p, entry)); p = p->next);
1776 /* c_or == c_simple */
1784 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1789 /* c_false == c_simple */
1797 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1803 /* c_quit == c_simple */