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)
1070 fn = nextarg(option, argvp);
1071 ftsoptions &= ~FTS_NOSTAT;
1073 new = palloc(option);
1076 new->i_data = sb.st_ino;
1081 * -links n functions --
1083 * True if the file has n links.
1086 f_links(PLAN *plan, FTSENT *entry)
1088 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1092 c_links(OPTION *option, char ***argvp)
1097 nlinks = nextarg(option, argvp);
1098 ftsoptions &= ~FTS_NOSTAT;
1100 new = palloc(option);
1101 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1108 * Always true - prints the current entry to stdout in "ls" format.
1111 f_ls(PLAN *plan __unused, FTSENT *entry)
1113 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1118 c_ls(OPTION *option, char ***argvp __unused)
1120 ftsoptions &= ~FTS_NOSTAT;
1123 return palloc(option);
1127 * -name functions --
1129 * True if the basename of the filename being examined
1130 * matches pattern using Pattern Matching Notation S3.14
1133 f_name(PLAN *plan, FTSENT *entry)
1139 if (plan->flags & F_LINK) {
1141 * The below test both avoids obviously useless readlink()
1142 * calls and ensures that symlinks with existent target do
1143 * not match if symlinks are being followed.
1144 * Assumption: fts will stat all symlinks that are to be
1145 * followed and will return the stat information.
1147 if (entry->fts_info != FTS_NSOK && entry->fts_info != FTS_SL &&
1148 entry->fts_info != FTS_SLNONE)
1150 len = readlink(entry->fts_accpath, fn, sizeof(fn) - 1);
1156 name = entry->fts_name;
1157 return !fnmatch(plan->c_data, name,
1158 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1162 c_name(OPTION *option, char ***argvp)
1167 pattern = nextarg(option, argvp);
1168 new = palloc(option);
1169 new->c_data = pattern;
1174 * -newer file functions --
1176 * True if the current file has been modified more recently
1177 * then the modification time of the file named by the pathname
1181 f_newer(PLAN *plan, FTSENT *entry)
1185 if (plan->flags & F_TIME_C)
1186 ft = entry->fts_statp->st_ctim;
1187 else if (plan->flags & F_TIME_A)
1188 ft = entry->fts_statp->st_atim;
1189 else if (plan->flags & F_TIME_B)
1190 ft = entry->fts_statp->st_birthtim;
1192 ft = entry->fts_statp->st_mtim;
1193 return (ft.tv_sec > plan->t_data.tv_sec ||
1194 (ft.tv_sec == plan->t_data.tv_sec &&
1195 ft.tv_nsec > plan->t_data.tv_nsec));
1199 c_newer(OPTION *option, char ***argvp)
1205 fn_or_tspec = nextarg(option, argvp);
1206 ftsoptions &= ~FTS_NOSTAT;
1208 new = palloc(option);
1209 /* compare against what */
1210 if (option->flags & F_TIME2_T) {
1211 new->t_data.tv_sec = get_date(fn_or_tspec);
1212 if (new->t_data.tv_sec == (time_t) -1)
1213 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1214 /* Use the seconds only in the comparison. */
1215 new->t_data.tv_nsec = 999999999;
1217 if (stat(fn_or_tspec, &sb))
1218 err(1, "%s", fn_or_tspec);
1219 if (option->flags & F_TIME2_C)
1220 new->t_data = sb.st_ctim;
1221 else if (option->flags & F_TIME2_A)
1222 new->t_data = sb.st_atim;
1223 else if (option->flags & F_TIME2_B)
1224 new->t_data = sb.st_birthtim;
1226 new->t_data = sb.st_mtim;
1232 * -nogroup functions --
1234 * True if file belongs to a user ID for which the equivalent
1235 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1238 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1240 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1244 c_nogroup(OPTION *option, char ***argvp __unused)
1246 ftsoptions &= ~FTS_NOSTAT;
1248 return palloc(option);
1252 * -nouser functions --
1254 * True if file belongs to a user ID for which the equivalent
1255 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1258 f_nouser(PLAN *plan __unused, FTSENT *entry)
1260 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1264 c_nouser(OPTION *option, char ***argvp __unused)
1266 ftsoptions &= ~FTS_NOSTAT;
1268 return palloc(option);
1272 * -path functions --
1274 * True if the path of the filename being examined
1275 * matches pattern using Pattern Matching Notation S3.14
1278 f_path(PLAN *plan, FTSENT *entry)
1280 return !fnmatch(plan->c_data, entry->fts_path,
1281 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1284 /* c_path is the same as c_name */
1287 * -perm functions --
1289 * The mode argument is used to represent file mode bits. If it starts
1290 * with a leading digit, it's treated as an octal mode, otherwise as a
1294 f_perm(PLAN *plan, FTSENT *entry)
1298 mode = entry->fts_statp->st_mode &
1299 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1300 if (plan->flags & F_ATLEAST)
1301 return (plan->m_data | mode) == mode;
1302 else if (plan->flags & F_ANY)
1303 return (mode & plan->m_data);
1305 return mode == plan->m_data;
1310 c_perm(OPTION *option, char ***argvp)
1316 perm = nextarg(option, argvp);
1317 ftsoptions &= ~FTS_NOSTAT;
1319 new = palloc(option);
1322 new->flags |= F_ATLEAST;
1324 } else if (*perm == '+') {
1325 new->flags |= F_ANY;
1329 if ((set = setmode(perm)) == NULL)
1330 errx(1, "%s: %s: illegal mode string", option->name, perm);
1332 new->m_data = getmode(set, 0);
1338 * -print functions --
1340 * Always true, causes the current pathname to be written to
1344 f_print(PLAN *plan __unused, FTSENT *entry)
1346 (void)puts(entry->fts_path);
1351 c_print(OPTION *option, char ***argvp __unused)
1355 return palloc(option);
1359 * -print0 functions --
1361 * Always true, causes the current pathname to be written to
1362 * standard output followed by a NUL character
1365 f_print0(PLAN *plan __unused, FTSENT *entry)
1367 fputs(entry->fts_path, stdout);
1368 fputc('\0', stdout);
1372 /* c_print0 is the same as c_print */
1375 * -prune functions --
1377 * Prune a portion of the hierarchy.
1380 f_prune(PLAN *plan __unused, FTSENT *entry)
1382 if (fts_set(tree, entry, FTS_SKIP))
1383 err(1, "%s", entry->fts_path);
1387 /* c_prune == c_simple */
1390 * -regex functions --
1392 * True if the whole path of the file matches pattern using
1393 * regular expression.
1396 f_regex(PLAN *plan, FTSENT *entry)
1403 char errbuf[LINE_MAX];
1406 pre = plan->re_data;
1407 str = entry->fts_path;
1414 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1416 if (errcode != 0 && errcode != REG_NOMATCH) {
1417 regerror(errcode, pre, errbuf, sizeof errbuf);
1419 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1422 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1429 c_regex(OPTION *option, char ***argvp)
1435 char errbuf[LINE_MAX];
1437 if ((pre = malloc(sizeof(regex_t))) == NULL)
1440 pattern = nextarg(option, argvp);
1442 if ((errcode = regcomp(pre, pattern,
1443 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1444 regerror(errcode, pre, errbuf, sizeof errbuf);
1445 errx(1, "%s: %s: %s",
1446 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1450 new = palloc(option);
1456 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1459 c_simple(OPTION *option, char ***argvp __unused)
1461 return palloc(option);
1465 * -size n[c] functions --
1467 * True if the file size in bytes, divided by an implementation defined
1468 * value and rounded up to the next integer, is n. If n is followed by
1469 * one of c k M G T P, the size is in bytes, kilobytes,
1470 * megabytes, gigabytes, terabytes or petabytes respectively.
1472 #define FIND_SIZE 512
1473 static int divsize = 1;
1476 f_size(PLAN *plan, FTSENT *entry)
1480 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1481 FIND_SIZE : entry->fts_statp->st_size;
1482 COMPARE(size, plan->o_data);
1486 c_size(OPTION *option, char ***argvp)
1493 size_str = nextarg(option, argvp);
1494 ftsoptions &= ~FTS_NOSTAT;
1496 new = palloc(option);
1498 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1499 if (endch != '\0') {
1503 case 'c': /* characters */
1506 case 'k': /* kilobytes 1<<10 */
1509 case 'M': /* megabytes 1<<20 */
1512 case 'G': /* gigabytes 1<<30 */
1513 scale = 0x40000000LL;
1515 case 'T': /* terabytes 1<<40 */
1516 scale = 0x10000000000LL;
1518 case 'P': /* petabytes 1<<50 */
1519 scale = 0x4000000000000LL;
1522 errx(1, "%s: %s: illegal trailing character",
1523 option->name, size_str);
1526 if (new->o_data > QUAD_MAX / scale)
1527 errx(1, "%s: %s: value too large",
1528 option->name, size_str);
1529 new->o_data *= scale;
1535 * -sparse functions --
1537 * Check if a file is sparse by finding if it occupies fewer blocks
1538 * than we expect based on its size.
1541 f_sparse(PLAN *plan __unused, FTSENT *entry)
1543 off_t expected_blocks;
1545 expected_blocks = (entry->fts_statp->st_size + 511) / 512;
1546 return entry->fts_statp->st_blocks < expected_blocks;
1550 c_sparse(OPTION *option, char ***argvp __unused)
1552 ftsoptions &= ~FTS_NOSTAT;
1554 return palloc(option);
1558 * -type c functions --
1560 * True if the type of the file is c, where c is b, c, d, p, f or w
1561 * for block special file, character special file, directory, FIFO,
1562 * regular file or whiteout respectively.
1565 f_type(PLAN *plan, FTSENT *entry)
1567 if (plan->m_data == S_IFDIR)
1568 return (entry->fts_info == FTS_D || entry->fts_info == FTS_DC ||
1569 entry->fts_info == FTS_DNR || entry->fts_info == FTS_DOT ||
1570 entry->fts_info == FTS_DP);
1572 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1576 c_type(OPTION *option, char ***argvp)
1582 typestring = nextarg(option, argvp);
1583 if (typestring[0] != 'd')
1584 ftsoptions &= ~FTS_NOSTAT;
1586 switch (typestring[0]) {
1611 ftsoptions |= FTS_WHITEOUT;
1613 #endif /* FTS_WHITEOUT */
1615 errx(1, "%s: %s: unknown type", option->name, typestring);
1618 new = palloc(option);
1624 * -user uname functions --
1626 * True if the file belongs to the user uname. If uname is numeric and
1627 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1628 * return a valid user name, uname is taken as a user ID.
1631 f_user(PLAN *plan, FTSENT *entry)
1633 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1637 c_user(OPTION *option, char ***argvp)
1644 username = nextarg(option, argvp);
1645 ftsoptions &= ~FTS_NOSTAT;
1647 new = palloc(option);
1648 p = getpwnam(username);
1650 char* cp = username;
1651 if( username[0] == '-' || username[0] == '+' )
1653 uid = atoi(username);
1654 if (uid == 0 && username[0] != '0')
1655 errx(1, "%s: %s: no such user", option->name, username);
1656 uid = find_parsenum(new, option->name, cp, NULL);
1665 * -xdev functions --
1667 * Always true, causes find not to descend past directories that have a
1668 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1671 c_xdev(OPTION *option, char ***argvp __unused)
1673 ftsoptions |= FTS_XDEV;
1675 return palloc(option);
1679 * ( expression ) functions --
1681 * True if expression is true.
1684 f_expr(PLAN *plan, FTSENT *entry)
1689 for (p = plan->p_data[0];
1690 p && (state = (p->execute)(p, entry)); p = p->next);
1695 * f_openparen and f_closeparen nodes are temporary place markers. They are
1696 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1697 * to a f_expr node containing the expression and the ')' node is discarded.
1698 * The functions themselves are only used as constants.
1702 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1708 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1713 /* c_openparen == c_simple */
1714 /* c_closeparen == c_simple */
1717 * AND operator. Since AND is implicit, no node is allocated.
1720 c_and(OPTION *option __unused, char ***argvp __unused)
1726 * ! expression functions --
1728 * Negation of a primary; the unary NOT operator.
1731 f_not(PLAN *plan, FTSENT *entry)
1736 for (p = plan->p_data[0];
1737 p && (state = (p->execute)(p, entry)); p = p->next);
1741 /* c_not == c_simple */
1744 * expression -o expression functions --
1746 * Alternation of primaries; the OR operator. The second expression is
1747 * not evaluated if the first expression is true.
1750 f_or(PLAN *plan, FTSENT *entry)
1755 for (p = plan->p_data[0];
1756 p && (state = (p->execute)(p, entry)); p = p->next);
1761 for (p = plan->p_data[1];
1762 p && (state = (p->execute)(p, entry)); p = p->next);
1766 /* c_or == c_simple */
1774 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1779 /* c_false == c_simple */
1787 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1793 /* c_quit == c_simple */