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 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
265 } else if (plan->flags & F_TIME_B) {
266 COMPARE((now - entry->fts_statp->st_birthtime +
267 60 - 1) / 60, plan->t_data.tv_sec);
270 COMPARE((now - entry->fts_statp->st_mtime +
271 60 - 1) / 60, plan->t_data.tv_sec);
276 c_Xmin(OPTION *option, char ***argvp)
281 nmins = nextarg(option, argvp);
282 ftsoptions &= ~FTS_NOSTAT;
284 new = palloc(option);
285 new->t_data.tv_sec = find_parsenum(new, option->name, nmins, NULL);
286 new->t_data.tv_nsec = 0;
292 * -[acm]time n functions --
294 * True if the difference between the
295 * file access time (-atime)
296 * file birth time (-Btime)
297 * last change of file status information (-ctime)
298 * file modification time (-mtime)
299 * and the current time is n 24 hour periods.
303 f_Xtime(PLAN *plan, FTSENT *entry)
307 if (plan->flags & F_TIME_A)
308 xtime = entry->fts_statp->st_atime;
309 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
310 else if (plan->flags & F_TIME_B)
311 xtime = entry->fts_statp->st_birthtime;
313 else if (plan->flags & F_TIME_C)
314 xtime = entry->fts_statp->st_ctime;
316 xtime = entry->fts_statp->st_mtime;
318 if (plan->flags & F_EXACTTIME)
319 COMPARE(now - xtime, plan->t_data.tv_sec);
321 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data.tv_sec);
325 c_Xtime(OPTION *option, char ***argvp)
330 value = nextarg(option, argvp);
331 ftsoptions &= ~FTS_NOSTAT;
333 new = palloc(option);
334 new->t_data.tv_sec = find_parsetime(new, option->name, value);
335 new->t_data.tv_nsec = 0;
336 if (!(new->flags & F_EXACTTIME))
342 * -maxdepth/-mindepth n functions --
344 * Does the same as -prune if the level of the current file is
345 * greater/less than the specified maximum/minimum depth.
347 * Note that -maxdepth and -mindepth are handled specially in
348 * find_execute() so their f_* functions are set to f_always_true().
351 c_mXXdepth(OPTION *option, char ***argvp)
356 dstr = nextarg(option, argvp);
358 /* all other errors handled by find_parsenum() */
359 errx(1, "%s: %s: value must be positive", option->name, dstr);
361 new = palloc(option);
362 if (option->flags & F_MAXDEPTH)
363 maxdepth = find_parsenum(new, option->name, dstr, NULL);
365 mindepth = find_parsenum(new, option->name, dstr, NULL);
373 * Show files with EXTENDED ACL attributes.
376 f_acl(PLAN *plan __unused, FTSENT *entry)
380 int acl_supported = 0, ret, trivial;
382 if (S_ISLNK(entry->fts_statp->st_mode))
384 ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4);
387 acl_type = ACL_TYPE_NFS4;
388 } else if (ret < 0 && errno != EINVAL) {
389 warn("%s", entry->fts_accpath);
392 if (acl_supported == 0) {
393 ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED);
396 acl_type = ACL_TYPE_ACCESS;
397 } else if (ret < 0 && errno != EINVAL) {
398 warn("%s", entry->fts_accpath);
402 if (acl_supported == 0)
405 facl = acl_get_file(entry->fts_accpath, acl_type);
407 warn("%s", entry->fts_accpath);
410 ret = acl_is_trivial_np(facl, &trivial);
413 warn("%s", entry->fts_accpath);
423 c_acl(OPTION *option, char ***argvp __unused)
425 ftsoptions &= ~FTS_NOSTAT;
426 return (palloc(option));
430 * -delete functions --
432 * True always. Makes its best shot and continues on regardless.
435 f_delete(PLAN *plan __unused, FTSENT *entry)
437 /* ignore these from fts */
438 if (strcmp(entry->fts_accpath, ".") == 0 ||
439 strcmp(entry->fts_accpath, "..") == 0)
443 if (isdepth == 0 || /* depth off */
444 (ftsoptions & FTS_NOSTAT)) /* not stat()ing */
445 errx(1, "-delete: insecure options got turned on");
447 if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */
448 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
449 errx(1, "-delete: forbidden when symlinks are followed");
451 /* Potentially unsafe - do not accept relative paths whatsoever */
452 if (entry->fts_level > FTS_ROOTLEVEL &&
453 strchr(entry->fts_accpath, '/') != NULL)
454 errx(1, "-delete: %s: relative path potentially not safe",
457 #if HAVE_STRUCT_STAT_ST_FLAGS
458 /* Turn off user immutable bits if running as root */
459 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
460 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
462 lchflags(entry->fts_accpath,
463 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
466 /* rmdir directories, unlink everything else */
467 if (S_ISDIR(entry->fts_statp->st_mode)) {
468 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
469 warn("-delete: rmdir(%s)", entry->fts_path);
471 if (unlink(entry->fts_accpath) < 0)
472 warn("-delete: unlink(%s)", entry->fts_path);
480 c_delete(OPTION *option, char ***argvp __unused)
483 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
484 isoutput = 1; /* possible output */
485 isdepth = 1; /* -depth implied */
488 * Try to avoid the confusing error message about relative paths
489 * being potentially not safe.
491 if (ftsoptions & FTS_NOCHDIR)
492 errx(1, "%s: forbidden when the current directory cannot be opened",
495 return palloc(option);
502 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
505 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
511 * -depth functions --
513 * With argument: True if the file is at level n.
514 * Without argument: Always true, causes descent of the directory hierarchy
515 * to be done so that all entries in a directory are acted on before the
519 f_depth(PLAN *plan, FTSENT *entry)
521 if (plan->flags & F_DEPTH)
522 COMPARE(entry->fts_level, plan->d_data);
528 c_depth(OPTION *option, char ***argvp)
533 new = palloc(option);
536 if (str && !(new->flags & F_DEPTH)) {
537 /* skip leading + or - */
538 if (*str == '+' || *str == '-')
541 if (*str == '+' || *str == '-')
544 new->flags |= F_DEPTH;
547 if (new->flags & F_DEPTH) { /* -depth n */
550 ndepth = nextarg(option, argvp);
551 new->d_data = find_parsenum(new, option->name, ndepth, NULL);
560 * -empty functions --
562 * True if the file or directory is empty
565 f_empty(PLAN *plan __unused, FTSENT *entry)
567 if (S_ISREG(entry->fts_statp->st_mode) &&
568 entry->fts_statp->st_size == 0)
570 if (S_ISDIR(entry->fts_statp->st_mode)) {
576 dir = opendir(entry->fts_accpath);
579 for (dp = readdir(dir); dp; dp = readdir(dir))
580 if (dp->d_name[0] != '.' ||
581 (dp->d_name[1] != '\0' &&
582 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
593 c_empty(OPTION *option, char ***argvp __unused)
595 ftsoptions &= ~FTS_NOSTAT;
597 return palloc(option);
601 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
603 * True if the executed utility returns a zero value as exit status.
604 * The end of the primary expression is delimited by a semicolon. If
605 * "{}" occurs anywhere, it gets replaced by the current pathname,
606 * or, in the case of -execdir, the current basename (filename
607 * without leading directory prefix). For -exec and -ok,
608 * the current directory for the execution of utility is the same as
609 * the current directory when the find utility was started, whereas
610 * for -execdir, it is the directory the file resides in.
612 * The primary -ok differs from -exec in that it requests affirmation
613 * of the user before executing the utility.
616 f_exec(PLAN *plan, FTSENT *entry)
623 if (entry == NULL && plan->flags & F_EXECPLUS) {
624 if (plan->e_ppos == plan->e_pbnum)
626 plan->e_argv[plan->e_ppos] = NULL;
630 /* XXX - if file/dir ends in '/' this will not work -- can it? */
631 if ((plan->flags & F_EXECDIR) && \
632 (file = strrchr(entry->fts_path, '/')))
635 file = entry->fts_path;
637 if (plan->flags & F_EXECPLUS) {
638 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
640 plan->e_len[plan->e_ppos] = strlen(file);
641 plan->e_psize += plan->e_len[plan->e_ppos];
642 if (++plan->e_ppos < plan->e_pnummax &&
643 plan->e_psize < plan->e_psizemax)
645 plan->e_argv[plan->e_ppos] = NULL;
647 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
648 if (plan->e_len[cnt])
649 brace_subst(plan->e_orig[cnt],
650 &plan->e_argv[cnt], file,
654 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
657 /* make sure find output is interspersed correctly with subprocesses */
661 switch (pid = fork()) {
666 /* change dir back from where we started */
667 if (!(plan->flags & F_EXECDIR) &&
668 !(ftsoptions & FTS_NOCHDIR) && fchdir(dotfd)) {
672 execvp(plan->e_argv[0], plan->e_argv);
673 warn("%s", plan->e_argv[0]);
676 if (plan->flags & F_EXECPLUS) {
677 while (--plan->e_ppos >= plan->e_pbnum)
678 free(plan->e_argv[plan->e_ppos]);
679 plan->e_ppos = plan->e_pbnum;
680 plan->e_psize = plan->e_pbsize;
682 pid = waitpid(pid, &status, 0);
683 if (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status))
685 if (plan->flags & F_EXECPLUS) {
693 * c_exec, c_execdir, c_ok --
694 * build three parallel arrays, one with pointers to the strings passed
695 * on the command line, one with (possibly duplicated) pointers to the
696 * argv array, and one with integer values that are lengths of the
697 * strings, but also flags meaning that the string has to be massaged.
700 c_exec(OPTION *option, char ***argvp)
702 PLAN *new; /* node returned */
705 char **argv, **ap, **ep, *p;
707 /* This would defeat -execdir's intended security. */
708 if (option->flags & F_EXECDIR && ftsoptions & FTS_NOCHDIR)
709 errx(1, "%s: forbidden when the current directory cannot be opened",
712 /* XXX - was in c_execdir, but seems unnecessary!?
713 ftsoptions &= ~FTS_NOSTAT;
717 /* XXX - this is a change from the previous coding */
718 new = palloc(option);
720 for (ap = argv = *argvp;; ++ap) {
723 "%s: no terminating \";\" or \"+\"", option->name);
726 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
727 new->flags |= F_EXECPLUS;
733 errx(1, "%s: no command specified", option->name);
735 cnt = ap - *argvp + 1;
736 if (new->flags & F_EXECPLUS) {
737 new->e_ppos = new->e_pbnum = cnt - 2;
738 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
739 warn("sysconf(_SC_ARG_MAX)");
740 argmax = _POSIX_ARG_MAX;
743 for (ep = environ; *ep != NULL; ep++)
744 argmax -= strlen(*ep) + 1 + sizeof(*ep);
745 argmax -= 1 + sizeof(*ep);
747 * Ensure that -execdir ... {} + does not mix files
748 * from different directories in one invocation.
749 * Files from the same directory should be handled
750 * in one invocation but there is no code for it.
752 new->e_pnummax = new->flags & F_EXECDIR ? 1 : argmax / 16;
753 argmax -= sizeof(char *) * new->e_pnummax;
755 errx(1, "no space for arguments");
756 new->e_psizemax = argmax;
758 cnt += new->e_pnummax + 1;
759 new->e_next = lastexecplus;
762 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
764 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
766 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
769 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
770 new->e_orig[cnt] = *argv;
771 if (new->flags & F_EXECPLUS)
772 new->e_pbsize += strlen(*argv) + 1;
773 for (p = *argv; *p; ++p)
774 if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
776 if ((new->e_argv[cnt] =
777 malloc(MAXPATHLEN)) == NULL)
779 new->e_len[cnt] = MAXPATHLEN;
783 new->e_argv[cnt] = *argv;
787 if (new->flags & F_EXECPLUS) {
788 new->e_psize = new->e_pbsize;
790 for (i = 0; i < new->e_pnummax; i++) {
791 new->e_argv[cnt] = NULL;
798 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
800 done: *argvp = argv + 1;
804 /* Finish any pending -exec ... {} + functions. */
806 finish_execplus(void)
812 (p->execute)(p, NULL);
817 #if HAVE_STRUCT_STAT_ST_FLAGS
819 f_flags(PLAN *plan, FTSENT *entry)
823 flags = entry->fts_statp->st_flags;
824 if (plan->flags & F_ATLEAST)
825 return (flags | plan->fl_flags) == flags &&
826 !(flags & plan->fl_notflags);
827 else if (plan->flags & F_ANY)
828 return (flags & plan->fl_flags) ||
829 (flags | plan->fl_notflags) != flags;
831 return flags == plan->fl_flags &&
832 !(plan->fl_flags & plan->fl_notflags);
836 c_flags(OPTION *option, char ***argvp)
840 u_long flags, notflags;
842 flags_str = nextarg(option, argvp);
843 ftsoptions &= ~FTS_NOSTAT;
845 new = palloc(option);
847 if (*flags_str == '-') {
848 new->flags |= F_ATLEAST;
850 } else if (*flags_str == '+') {
854 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
855 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
857 new->fl_flags = flags;
858 new->fl_notflags = notflags;
864 * -follow functions --
866 * Always true, causes symbolic links to be followed on a global
870 c_follow(OPTION *option, char ***argvp __unused)
872 ftsoptions &= ~FTS_PHYSICAL;
873 ftsoptions |= FTS_LOGICAL;
875 return palloc(option);
878 #if HAVE_STRUCT_STATFS_F_FSTYPENAME
880 * -fstype functions --
882 * True if the file is of a certain type.
885 f_fstype(PLAN *plan, FTSENT *entry)
887 static dev_t curdev; /* need a guaranteed illegal dev value */
888 static int first = 1;
890 static int val_flags;
891 static char fstype[sizeof(sb.f_fstypename)];
892 char *p, save[2] = {0,0};
894 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
897 /* Only check when we cross mount point. */
898 if (first || curdev != entry->fts_statp->st_dev) {
899 curdev = entry->fts_statp->st_dev;
902 * Statfs follows symlinks; find wants the link's filesystem,
903 * not where it points.
905 if (entry->fts_info == FTS_SL ||
906 entry->fts_info == FTS_SLNONE) {
907 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
910 p = entry->fts_accpath;
918 if (statfs(entry->fts_accpath, &sb)) {
919 if (!ignore_readdir_race || errno != ENOENT) {
920 warn("statfs: %s", entry->fts_accpath);
934 * Further tests may need both of these values, so
935 * always copy both of them.
937 val_flags = sb.f_flags;
938 strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
940 switch (plan->flags & F_MTMASK) {
942 return val_flags & plan->mt_data;
944 return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
951 c_fstype(OPTION *option, char ***argvp)
956 fsname = nextarg(option, argvp);
957 ftsoptions &= ~FTS_NOSTAT;
959 new = palloc(option);
962 if (!strcmp(fsname, "local")) {
963 new->flags |= F_MTFLAG;
964 new->mt_data = MNT_LOCAL;
969 if (!strcmp(fsname, "rdonly")) {
970 new->flags |= F_MTFLAG;
971 new->mt_data = MNT_RDONLY;
977 new->flags |= F_MTTYPE;
978 new->c_data = fsname;
984 * -group gname functions --
986 * True if the file belongs to the group gname. If gname is numeric and
987 * an equivalent of the getgrnam() function does not return a valid group
988 * name, gname is taken as a group ID.
991 f_group(PLAN *plan, FTSENT *entry)
993 COMPARE(entry->fts_statp->st_gid, plan->g_data);
997 c_group(OPTION *option, char ***argvp)
1004 gname = nextarg(option, argvp);
1005 ftsoptions &= ~FTS_NOSTAT;
1007 new = palloc(option);
1008 g = getgrnam(gname);
1011 if (gname[0] == '-' || gname[0] == '+')
1014 if (gid == 0 && gname[0] != '0')
1015 errx(1, "%s: %s: no such group", option->name, gname);
1016 gid = find_parsenum(new, option->name, cp, NULL);
1025 * -ignore_readdir_race functions --
1027 * Always true. Ignore errors which occur if a file or a directory
1028 * in a starting point gets deleted between reading the name and calling
1029 * stat on it while find is traversing the starting point.
1033 c_ignore_readdir_race(OPTION *option, char ***argvp __unused)
1035 if (strcmp(option->name, "-ignore_readdir_race") == 0)
1036 ignore_readdir_race = 1;
1038 ignore_readdir_race = 0;
1040 return palloc(option);
1044 * -inum n functions --
1046 * True if the file has inode # n.
1049 f_inum(PLAN *plan, FTSENT *entry)
1051 COMPARE(entry->fts_statp->st_ino, plan->i_data);
1055 c_inum(OPTION *option, char ***argvp)
1060 inum_str = nextarg(option, argvp);
1061 ftsoptions &= ~FTS_NOSTAT;
1063 new = palloc(option);
1064 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
1071 * True if the file has the same inode (eg hard link) FN
1074 /* f_samefile is just f_inum */
1076 c_samefile(OPTION *option, char ***argvp)
1083 fn = nextarg(option, argvp);
1084 ftsoptions &= ~FTS_NOSTAT;
1086 new = palloc(option);
1087 if (ftsoptions & FTS_PHYSICAL)
1088 error = lstat(fn, &sb);
1090 error = stat(fn, &sb);
1093 new->i_data = sb.st_ino;
1098 * -links n functions --
1100 * True if the file has n links.
1103 f_links(PLAN *plan, FTSENT *entry)
1105 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1109 c_links(OPTION *option, char ***argvp)
1114 nlinks = nextarg(option, argvp);
1115 ftsoptions &= ~FTS_NOSTAT;
1117 new = palloc(option);
1118 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1125 * Always true - prints the current entry to stdout in "ls" format.
1128 f_ls(PLAN *plan __unused, FTSENT *entry)
1130 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1135 c_ls(OPTION *option, char ***argvp __unused)
1137 ftsoptions &= ~FTS_NOSTAT;
1140 return palloc(option);
1144 * -name functions --
1146 * True if the basename of the filename being examined
1147 * matches pattern using Pattern Matching Notation S3.14
1150 f_name(PLAN *plan, FTSENT *entry)
1156 if (plan->flags & F_LINK) {
1158 * The below test both avoids obviously useless readlink()
1159 * calls and ensures that symlinks with existent target do
1160 * not match if symlinks are being followed.
1161 * Assumption: fts will stat all symlinks that are to be
1162 * followed and will return the stat information.
1164 if (entry->fts_info != FTS_NSOK && entry->fts_info != FTS_SL &&
1165 entry->fts_info != FTS_SLNONE)
1167 len = readlink(entry->fts_accpath, fn, sizeof(fn) - 1);
1173 name = entry->fts_name;
1174 return !fnmatch(plan->c_data, name,
1175 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1179 c_name(OPTION *option, char ***argvp)
1184 pattern = nextarg(option, argvp);
1185 new = palloc(option);
1186 new->c_data = pattern;
1191 * -newer file functions --
1193 * True if the current file has been modified more recently
1194 * then the modification time of the file named by the pathname
1198 f_newer(PLAN *plan, FTSENT *entry)
1202 if (plan->flags & F_TIME_C)
1203 ft = entry->fts_statp->st_ctim;
1204 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
1205 else if (plan->flags & F_TIME_A)
1206 ft = entry->fts_statp->st_atim;
1207 else if (plan->flags & F_TIME_B)
1208 ft = entry->fts_statp->st_birthtim;
1211 ft = entry->fts_statp->st_mtim;
1212 return (ft.tv_sec > plan->t_data.tv_sec ||
1213 (ft.tv_sec == plan->t_data.tv_sec &&
1214 ft.tv_nsec > plan->t_data.tv_nsec));
1218 c_newer(OPTION *option, char ***argvp)
1225 fn_or_tspec = nextarg(option, argvp);
1226 ftsoptions &= ~FTS_NOSTAT;
1228 new = palloc(option);
1229 /* compare against what */
1230 if (option->flags & F_TIME2_T) {
1231 new->t_data.tv_sec = get_date(fn_or_tspec);
1232 if (new->t_data.tv_sec == (time_t) -1)
1233 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1234 /* Use the seconds only in the comparison. */
1235 new->t_data.tv_nsec = 999999999;
1237 if (ftsoptions & FTS_PHYSICAL)
1238 error = lstat(fn_or_tspec, &sb);
1240 error = stat(fn_or_tspec, &sb);
1242 err(1, "%s", fn_or_tspec);
1243 if (option->flags & F_TIME2_C)
1244 new->t_data = sb.st_ctim;
1245 else if (option->flags & F_TIME2_A)
1246 new->t_data = sb.st_atim;
1247 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
1248 else if (option->flags & F_TIME2_B)
1249 new->t_data = sb.st_birthtim;
1252 new->t_data = sb.st_mtim;
1258 * -nogroup functions --
1260 * True if file belongs to a user ID for which the equivalent
1261 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1264 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1266 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1270 c_nogroup(OPTION *option, char ***argvp __unused)
1272 ftsoptions &= ~FTS_NOSTAT;
1274 return palloc(option);
1278 * -nouser functions --
1280 * True if file belongs to a user ID for which the equivalent
1281 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1284 f_nouser(PLAN *plan __unused, FTSENT *entry)
1286 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1290 c_nouser(OPTION *option, char ***argvp __unused)
1292 ftsoptions &= ~FTS_NOSTAT;
1294 return palloc(option);
1298 * -path functions --
1300 * True if the path of the filename being examined
1301 * matches pattern using Pattern Matching Notation S3.14
1304 f_path(PLAN *plan, FTSENT *entry)
1306 return !fnmatch(plan->c_data, entry->fts_path,
1307 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1310 /* c_path is the same as c_name */
1313 * -perm functions --
1315 * The mode argument is used to represent file mode bits. If it starts
1316 * with a leading digit, it's treated as an octal mode, otherwise as a
1320 f_perm(PLAN *plan, FTSENT *entry)
1324 mode = entry->fts_statp->st_mode &
1325 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1326 if (plan->flags & F_ATLEAST)
1327 return (plan->m_data | mode) == mode;
1328 else if (plan->flags & F_ANY)
1329 return (mode & plan->m_data);
1331 return mode == plan->m_data;
1336 c_perm(OPTION *option, char ***argvp)
1342 perm = nextarg(option, argvp);
1343 ftsoptions &= ~FTS_NOSTAT;
1345 new = palloc(option);
1348 new->flags |= F_ATLEAST;
1350 } else if (*perm == '+') {
1351 new->flags |= F_ANY;
1355 if ((set = setmode(perm)) == NULL)
1356 errx(1, "%s: %s: illegal mode string", option->name, perm);
1358 new->m_data = getmode(set, 0);
1364 * -print functions --
1366 * Always true, causes the current pathname to be written to
1370 f_print(PLAN *plan __unused, FTSENT *entry)
1372 (void)puts(entry->fts_path);
1377 c_print(OPTION *option, char ***argvp __unused)
1381 return palloc(option);
1385 * -print0 functions --
1387 * Always true, causes the current pathname to be written to
1388 * standard output followed by a NUL character
1391 f_print0(PLAN *plan __unused, FTSENT *entry)
1393 fputs(entry->fts_path, stdout);
1394 fputc('\0', stdout);
1398 /* c_print0 is the same as c_print */
1401 * -prune functions --
1403 * Prune a portion of the hierarchy.
1406 f_prune(PLAN *plan __unused, FTSENT *entry)
1408 if (fts_set(tree, entry, FTS_SKIP))
1409 err(1, "%s", entry->fts_path);
1413 /* c_prune == c_simple */
1416 * -regex functions --
1418 * True if the whole path of the file matches pattern using
1419 * regular expression.
1422 f_regex(PLAN *plan, FTSENT *entry)
1429 char errbuf[LINE_MAX];
1432 pre = plan->re_data;
1433 str = entry->fts_path;
1440 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1442 if (errcode != 0 && errcode != REG_NOMATCH) {
1443 regerror(errcode, pre, errbuf, sizeof errbuf);
1445 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1448 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1455 c_regex(OPTION *option, char ***argvp)
1461 char errbuf[LINE_MAX];
1463 if ((pre = malloc(sizeof(regex_t))) == NULL)
1466 pattern = nextarg(option, argvp);
1468 if ((errcode = regcomp(pre, pattern,
1469 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1470 regerror(errcode, pre, errbuf, sizeof errbuf);
1471 errx(1, "%s: %s: %s",
1472 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1476 new = palloc(option);
1482 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1485 c_simple(OPTION *option, char ***argvp __unused)
1487 return palloc(option);
1491 * -size n[c] functions --
1493 * True if the file size in bytes, divided by an implementation defined
1494 * value and rounded up to the next integer, is n. If n is followed by
1495 * one of c k M G T P, the size is in bytes, kilobytes,
1496 * megabytes, gigabytes, terabytes or petabytes respectively.
1498 #define FIND_SIZE 512
1499 static int divsize = 1;
1502 f_size(PLAN *plan, FTSENT *entry)
1506 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1507 FIND_SIZE : entry->fts_statp->st_size;
1508 COMPARE(size, plan->o_data);
1512 c_size(OPTION *option, char ***argvp)
1519 size_str = nextarg(option, argvp);
1520 ftsoptions &= ~FTS_NOSTAT;
1522 new = palloc(option);
1524 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1525 if (endch != '\0') {
1529 case 'c': /* characters */
1532 case 'k': /* kilobytes 1<<10 */
1535 case 'M': /* megabytes 1<<20 */
1538 case 'G': /* gigabytes 1<<30 */
1539 scale = 0x40000000LL;
1541 case 'T': /* terabytes 1<<40 */
1542 scale = 0x10000000000LL;
1544 case 'P': /* petabytes 1<<50 */
1545 scale = 0x4000000000000LL;
1548 errx(1, "%s: %s: illegal trailing character",
1549 option->name, size_str);
1552 if (new->o_data > QUAD_MAX / scale)
1553 errx(1, "%s: %s: value too large",
1554 option->name, size_str);
1555 new->o_data *= scale;
1561 * -sparse functions --
1563 * Check if a file is sparse by finding if it occupies fewer blocks
1564 * than we expect based on its size.
1567 f_sparse(PLAN *plan __unused, FTSENT *entry)
1569 off_t expected_blocks;
1571 expected_blocks = (entry->fts_statp->st_size + 511) / 512;
1572 return entry->fts_statp->st_blocks < expected_blocks;
1576 c_sparse(OPTION *option, char ***argvp __unused)
1578 ftsoptions &= ~FTS_NOSTAT;
1580 return palloc(option);
1584 * -type c functions --
1586 * True if the type of the file is c, where c is b, c, d, p, f or w
1587 * for block special file, character special file, directory, FIFO,
1588 * regular file or whiteout respectively.
1591 f_type(PLAN *plan, FTSENT *entry)
1593 if (plan->m_data == S_IFDIR)
1594 return (entry->fts_info == FTS_D || entry->fts_info == FTS_DC ||
1595 entry->fts_info == FTS_DNR || entry->fts_info == FTS_DOT ||
1596 entry->fts_info == FTS_DP);
1598 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1602 c_type(OPTION *option, char ***argvp)
1608 typestring = nextarg(option, argvp);
1609 if (typestring[0] != 'd')
1610 ftsoptions &= ~FTS_NOSTAT;
1612 switch (typestring[0]) {
1634 #if defined(FTS_WHITEOUT) && defined(S_IFWHT)
1637 ftsoptions |= FTS_WHITEOUT;
1639 #endif /* FTS_WHITEOUT */
1641 errx(1, "%s: %s: unknown type", option->name, typestring);
1644 new = palloc(option);
1650 * -user uname functions --
1652 * True if the file belongs to the user uname. If uname is numeric and
1653 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1654 * return a valid user name, uname is taken as a user ID.
1657 f_user(PLAN *plan, FTSENT *entry)
1659 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1663 c_user(OPTION *option, char ***argvp)
1670 username = nextarg(option, argvp);
1671 ftsoptions &= ~FTS_NOSTAT;
1673 new = palloc(option);
1674 p = getpwnam(username);
1676 char* cp = username;
1677 if( username[0] == '-' || username[0] == '+' )
1679 uid = atoi(username);
1680 if (uid == 0 && username[0] != '0')
1681 errx(1, "%s: %s: no such user", option->name, username);
1682 uid = find_parsenum(new, option->name, cp, NULL);
1691 * -xdev functions --
1693 * Always true, causes find not to descend past directories that have a
1694 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1697 c_xdev(OPTION *option, char ***argvp __unused)
1699 ftsoptions |= FTS_XDEV;
1701 return palloc(option);
1705 * ( expression ) functions --
1707 * True if expression is true.
1710 f_expr(PLAN *plan, FTSENT *entry)
1715 for (p = plan->p_data[0];
1716 p && (state = (p->execute)(p, entry)); p = p->next);
1721 * f_openparen and f_closeparen nodes are temporary place markers. They are
1722 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1723 * to a f_expr node containing the expression and the ')' node is discarded.
1724 * The functions themselves are only used as constants.
1728 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1734 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1739 /* c_openparen == c_simple */
1740 /* c_closeparen == c_simple */
1743 * AND operator. Since AND is implicit, no node is allocated.
1746 c_and(OPTION *option __unused, char ***argvp __unused)
1752 * ! expression functions --
1754 * Negation of a primary; the unary NOT operator.
1757 f_not(PLAN *plan, FTSENT *entry)
1762 for (p = plan->p_data[0];
1763 p && (state = (p->execute)(p, entry)); p = p->next);
1767 /* c_not == c_simple */
1770 * expression -o expression functions --
1772 * Alternation of primaries; the OR operator. The second expression is
1773 * not evaluated if the first expression is true.
1776 f_or(PLAN *plan, FTSENT *entry)
1781 for (p = plan->p_data[0];
1782 p && (state = (p->execute)(p, entry)); p = p->next);
1787 for (p = plan->p_data[1];
1788 p && (state = (p->execute)(p, entry)); p = p->next);
1792 /* c_or == c_simple */
1800 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1805 /* c_false == c_simple */
1813 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1819 /* c_quit == c_simple */