2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Cimarron D. Taylor of the University of California, Berkeley.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/ucred.h>
45 #include <sys/types.h>
48 #include <sys/mount.h>
67 static PLAN *palloc(OPTION *);
68 static long long find_parsenum(PLAN *, const char *, char *, char *);
69 static long long find_parsetime(PLAN *, const char *, char *);
70 static char *nextarg(OPTION *, char ***);
72 extern char **environ;
74 static PLAN *lastexecplus = NULL;
76 #define COMPARE(a, b) do { \
77 switch (plan->flags & F_ELG_MASK) { \
90 palloc(OPTION *option)
94 if ((new = malloc(sizeof(PLAN))) == NULL)
96 new->execute = option->execute;
97 new->flags = option->flags;
104 * Parse a string of the form [+-]# and return the value.
107 find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
110 char *endchar, *str; /* Pointer to character ending conversion. */
112 /* Determine comparison from leading + or -. */
117 plan->flags |= F_GREATER;
121 plan->flags |= F_LESSTHAN;
124 plan->flags |= F_EQUAL;
129 * Convert the string with strtoq(). Note, if strtoq() returns zero
130 * and endchar points to the beginning of the string we know we have
133 value = strtoq(str, &endchar, 10);
134 if (value == 0 && endchar == str)
135 errx(1, "%s: %s: illegal numeric value", option, vp);
136 if (endchar[0] && endch == NULL)
137 errx(1, "%s: %s: illegal trailing character", option, vp);
145 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
148 find_parsetime(PLAN *plan, const char *option, char *vp)
150 long long secs, value;
151 char *str, *unit; /* Pointer to character ending conversion. */
153 /* Determine comparison from leading + or -. */
158 plan->flags |= F_GREATER;
162 plan->flags |= F_LESSTHAN;
165 plan->flags |= F_EQUAL;
169 value = strtoq(str, &unit, 10);
170 if (value == 0 && unit == str) {
171 errx(1, "%s: %s: illegal time value", option, vp);
181 case 's': /* seconds */
184 case 'm': /* minutes */
187 case 'h': /* hours */
188 secs += value * 3600;
191 secs += value * 86400;
193 case 'w': /* weeks */
194 secs += value * 604800;
197 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
201 if (*str == '\0') /* EOS */
203 value = strtoq(str, &unit, 10);
204 if (value == 0 && unit == str) {
205 errx(1, "%s: %s: illegal time value", option, vp);
209 errx(1, "%s: %s: missing trailing unit", option, vp);
213 plan->flags |= F_EXACTTIME;
219 * Check that another argument still exists, return a pointer to it,
220 * and increment the argument vector pointer.
223 nextarg(OPTION *option, char ***argvp)
227 if ((arg = **argvp) == 0)
228 errx(1, "%s: requires additional arguments", option->name);
234 * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
235 * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts
236 * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
237 * the user wanted. Correct so that -1 is "less than 1".
239 #define TIME_CORRECT(p) \
240 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
244 * -[acm]min n functions --
246 * True if the difference between the
247 * file access time (-amin)
248 * file birth time (-Bmin)
249 * last change of file status information (-cmin)
250 * file modification time (-mmin)
251 * and the current time is n min periods.
254 f_Xmin(PLAN *plan, FTSENT *entry)
256 if (plan->flags & F_TIME_C) {
257 COMPARE((now - entry->fts_statp->st_ctime +
258 60 - 1) / 60, plan->t_data);
259 } else if (plan->flags & F_TIME_A) {
260 COMPARE((now - entry->fts_statp->st_atime +
261 60 - 1) / 60, plan->t_data);
262 } else if (plan->flags & F_TIME_B) {
263 COMPARE((now - entry->fts_statp->st_birthtime +
264 60 - 1) / 60, plan->t_data);
266 COMPARE((now - entry->fts_statp->st_mtime +
267 60 - 1) / 60, plan->t_data);
272 c_Xmin(OPTION *option, char ***argvp)
277 nmins = nextarg(option, argvp);
278 ftsoptions &= ~FTS_NOSTAT;
280 new = palloc(option);
281 new->t_data = find_parsenum(new, option->name, nmins, NULL);
287 * -[acm]time n functions --
289 * True if the difference between the
290 * file access time (-atime)
291 * file birth time (-Btime)
292 * last change of file status information (-ctime)
293 * file modification time (-mtime)
294 * and the current time is n 24 hour periods.
298 f_Xtime(PLAN *plan, FTSENT *entry)
302 if (plan->flags & F_TIME_A)
303 xtime = entry->fts_statp->st_atime;
304 else if (plan->flags & F_TIME_B)
305 xtime = entry->fts_statp->st_birthtime;
306 else if (plan->flags & F_TIME_C)
307 xtime = entry->fts_statp->st_ctime;
309 xtime = entry->fts_statp->st_mtime;
311 if (plan->flags & F_EXACTTIME)
312 COMPARE(now - xtime, plan->t_data);
314 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data);
318 c_Xtime(OPTION *option, char ***argvp)
323 value = nextarg(option, argvp);
324 ftsoptions &= ~FTS_NOSTAT;
326 new = palloc(option);
327 new->t_data = find_parsetime(new, option->name, value);
328 if (!(new->flags & F_EXACTTIME))
334 * -maxdepth/-mindepth n functions --
336 * Does the same as -prune if the level of the current file is
337 * greater/less than the specified maximum/minimum depth.
339 * Note that -maxdepth and -mindepth are handled specially in
340 * find_execute() so their f_* functions are set to f_always_true().
343 c_mXXdepth(OPTION *option, char ***argvp)
348 dstr = nextarg(option, argvp);
350 /* all other errors handled by find_parsenum() */
351 errx(1, "%s: %s: value must be positive", option->name, dstr);
353 new = palloc(option);
354 if (option->flags & F_MAXDEPTH)
355 maxdepth = find_parsenum(new, option->name, dstr, NULL);
357 mindepth = find_parsenum(new, option->name, dstr, NULL);
364 * Show files with EXTENDED ACL attributes.
367 f_acl(PLAN *plan __unused, FTSENT *entry)
371 int acl_supported = 0, ret, trivial;
373 if (S_ISLNK(entry->fts_statp->st_mode))
375 ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4);
378 acl_type = ACL_TYPE_NFS4;
379 } else if (ret < 0 && errno != EINVAL) {
380 warn("%s", entry->fts_accpath);
383 if (acl_supported == 0) {
384 ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED);
387 acl_type = ACL_TYPE_ACCESS;
388 } else if (ret < 0 && errno != EINVAL) {
389 warn("%s", entry->fts_accpath);
393 if (acl_supported == 0)
396 facl = acl_get_file(entry->fts_accpath, acl_type);
398 warn("%s", entry->fts_accpath);
401 ret = acl_is_trivial_np(facl, &trivial);
404 warn("%s", entry->fts_accpath);
414 c_acl(OPTION *option, char ***argvp __unused)
416 ftsoptions &= ~FTS_NOSTAT;
417 return (palloc(option));
421 * -delete functions --
423 * True always. Makes its best shot and continues on regardless.
426 f_delete(PLAN *plan __unused, FTSENT *entry)
428 /* ignore these from fts */
429 if (strcmp(entry->fts_accpath, ".") == 0 ||
430 strcmp(entry->fts_accpath, "..") == 0)
434 if (isdepth == 0 || /* depth off */
435 (ftsoptions & FTS_NOSTAT)) /* not stat()ing */
436 errx(1, "-delete: insecure options got turned on");
438 if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */
439 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
440 errx(1, "-delete: forbidden when symlinks are followed");
442 /* Potentially unsafe - do not accept relative paths whatsoever */
443 if (strchr(entry->fts_accpath, '/') != NULL)
444 errx(1, "-delete: %s: relative path potentially not safe",
447 /* Turn off user immutable bits if running as root */
448 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
449 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
451 lchflags(entry->fts_accpath,
452 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
454 /* rmdir directories, unlink everything else */
455 if (S_ISDIR(entry->fts_statp->st_mode)) {
456 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
457 warn("-delete: rmdir(%s)", entry->fts_path);
459 if (unlink(entry->fts_accpath) < 0)
460 warn("-delete: unlink(%s)", entry->fts_path);
468 c_delete(OPTION *option, char ***argvp __unused)
471 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
472 isoutput = 1; /* possible output */
473 isdepth = 1; /* -depth implied */
475 return palloc(option);
482 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
485 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
491 * -depth functions --
493 * With argument: True if the file is at level n.
494 * Without argument: Always true, causes descent of the directory hierarchy
495 * to be done so that all entries in a directory are acted on before the
499 f_depth(PLAN *plan, FTSENT *entry)
501 if (plan->flags & F_DEPTH)
502 COMPARE(entry->fts_level, plan->d_data);
508 c_depth(OPTION *option, char ***argvp)
513 new = palloc(option);
516 if (str && !(new->flags & F_DEPTH)) {
517 /* skip leading + or - */
518 if (*str == '+' || *str == '-')
521 if (*str == '+' || *str == '-')
524 new->flags |= F_DEPTH;
527 if (new->flags & F_DEPTH) { /* -depth n */
530 ndepth = nextarg(option, argvp);
531 new->d_data = find_parsenum(new, option->name, ndepth, NULL);
540 * -empty functions --
542 * True if the file or directory is empty
545 f_empty(PLAN *plan __unused, FTSENT *entry)
547 if (S_ISREG(entry->fts_statp->st_mode) &&
548 entry->fts_statp->st_size == 0)
550 if (S_ISDIR(entry->fts_statp->st_mode)) {
556 dir = opendir(entry->fts_accpath);
559 for (dp = readdir(dir); dp; dp = readdir(dir))
560 if (dp->d_name[0] != '.' ||
561 (dp->d_name[1] != '\0' &&
562 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
573 c_empty(OPTION *option, char ***argvp __unused)
575 ftsoptions &= ~FTS_NOSTAT;
577 return palloc(option);
581 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
583 * True if the executed utility returns a zero value as exit status.
584 * The end of the primary expression is delimited by a semicolon. If
585 * "{}" occurs anywhere, it gets replaced by the current pathname,
586 * or, in the case of -execdir, the current basename (filename
587 * without leading directory prefix). For -exec and -ok,
588 * the current directory for the execution of utility is the same as
589 * the current directory when the find utility was started, whereas
590 * for -execdir, it is the directory the file resides in.
592 * The primary -ok differs from -exec in that it requests affirmation
593 * of the user before executing the utility.
596 f_exec(PLAN *plan, FTSENT *entry)
603 if (entry == NULL && plan->flags & F_EXECPLUS) {
604 if (plan->e_ppos == plan->e_pbnum)
606 plan->e_argv[plan->e_ppos] = NULL;
610 /* XXX - if file/dir ends in '/' this will not work -- can it? */
611 if ((plan->flags & F_EXECDIR) && \
612 (file = strrchr(entry->fts_path, '/')))
615 file = entry->fts_path;
617 if (plan->flags & F_EXECPLUS) {
618 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
620 plan->e_len[plan->e_ppos] = strlen(file);
621 plan->e_psize += plan->e_len[plan->e_ppos];
622 if (++plan->e_ppos < plan->e_pnummax &&
623 plan->e_psize < plan->e_psizemax)
625 plan->e_argv[plan->e_ppos] = NULL;
627 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
628 if (plan->e_len[cnt])
629 brace_subst(plan->e_orig[cnt],
630 &plan->e_argv[cnt], file,
634 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
637 /* make sure find output is interspersed correctly with subprocesses */
641 switch (pid = fork()) {
646 /* change dir back from where we started */
647 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
651 execvp(plan->e_argv[0], plan->e_argv);
652 warn("%s", plan->e_argv[0]);
655 if (plan->flags & F_EXECPLUS) {
656 while (--plan->e_ppos >= plan->e_pbnum)
657 free(plan->e_argv[plan->e_ppos]);
658 plan->e_ppos = plan->e_pbnum;
659 plan->e_psize = plan->e_pbsize;
661 pid = waitpid(pid, &status, 0);
662 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
666 * c_exec, c_execdir, c_ok --
667 * build three parallel arrays, one with pointers to the strings passed
668 * on the command line, one with (possibly duplicated) pointers to the
669 * argv array, and one with integer values that are lengths of the
670 * strings, but also flags meaning that the string has to be massaged.
673 c_exec(OPTION *option, char ***argvp)
675 PLAN *new; /* node returned */
678 char **argv, **ap, **ep, *p;
680 /* XXX - was in c_execdir, but seems unnecessary!?
681 ftsoptions &= ~FTS_NOSTAT;
685 /* XXX - this is a change from the previous coding */
686 new = palloc(option);
688 for (ap = argv = *argvp;; ++ap) {
691 "%s: no terminating \";\" or \"+\"", option->name);
694 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
695 new->flags |= F_EXECPLUS;
701 errx(1, "%s: no command specified", option->name);
703 cnt = ap - *argvp + 1;
704 if (new->flags & F_EXECPLUS) {
705 new->e_ppos = new->e_pbnum = cnt - 2;
706 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
707 warn("sysconf(_SC_ARG_MAX)");
708 argmax = _POSIX_ARG_MAX;
711 for (ep = environ; *ep != NULL; ep++)
712 argmax -= strlen(*ep) + 1 + sizeof(*ep);
713 argmax -= 1 + sizeof(*ep);
714 new->e_pnummax = argmax / 16;
715 argmax -= sizeof(char *) * new->e_pnummax;
717 errx(1, "no space for arguments");
718 new->e_psizemax = argmax;
720 cnt += new->e_pnummax + 1;
721 new->e_next = lastexecplus;
724 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
726 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
728 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
731 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
732 new->e_orig[cnt] = *argv;
733 if (new->flags & F_EXECPLUS)
734 new->e_pbsize += strlen(*argv) + 1;
735 for (p = *argv; *p; ++p)
736 if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
738 if ((new->e_argv[cnt] =
739 malloc(MAXPATHLEN)) == NULL)
741 new->e_len[cnt] = MAXPATHLEN;
745 new->e_argv[cnt] = *argv;
749 if (new->flags & F_EXECPLUS) {
750 new->e_psize = new->e_pbsize;
752 for (i = 0; i < new->e_pnummax; i++) {
753 new->e_argv[cnt] = NULL;
760 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
762 done: *argvp = argv + 1;
766 /* Finish any pending -exec ... {} + functions. */
768 finish_execplus(void)
774 (p->execute)(p, NULL);
780 f_flags(PLAN *plan, FTSENT *entry)
784 flags = entry->fts_statp->st_flags;
785 if (plan->flags & F_ATLEAST)
786 return (flags | plan->fl_flags) == flags &&
787 !(flags & plan->fl_notflags);
788 else if (plan->flags & F_ANY)
789 return (flags & plan->fl_flags) ||
790 (flags | plan->fl_notflags) != flags;
792 return flags == plan->fl_flags &&
793 !(plan->fl_flags & plan->fl_notflags);
797 c_flags(OPTION *option, char ***argvp)
801 u_long flags, notflags;
803 flags_str = nextarg(option, argvp);
804 ftsoptions &= ~FTS_NOSTAT;
806 new = palloc(option);
808 if (*flags_str == '-') {
809 new->flags |= F_ATLEAST;
811 } else if (*flags_str == '+') {
815 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
816 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
818 new->fl_flags = flags;
819 new->fl_notflags = notflags;
824 * -follow functions --
826 * Always true, causes symbolic links to be followed on a global
830 c_follow(OPTION *option, char ***argvp __unused)
832 ftsoptions &= ~FTS_PHYSICAL;
833 ftsoptions |= FTS_LOGICAL;
835 return palloc(option);
839 * -fstype functions --
841 * True if the file is of a certain type.
844 f_fstype(PLAN *plan, FTSENT *entry)
846 static dev_t curdev; /* need a guaranteed illegal dev value */
847 static int first = 1;
849 static int val_flags;
850 static char fstype[sizeof(sb.f_fstypename)];
851 char *p, save[2] = {0,0};
853 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
856 /* Only check when we cross mount point. */
857 if (first || curdev != entry->fts_statp->st_dev) {
858 curdev = entry->fts_statp->st_dev;
861 * Statfs follows symlinks; find wants the link's filesystem,
862 * not where it points.
864 if (entry->fts_info == FTS_SL ||
865 entry->fts_info == FTS_SLNONE) {
866 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
869 p = entry->fts_accpath;
877 if (statfs(entry->fts_accpath, &sb))
878 err(1, "%s", entry->fts_accpath);
888 * Further tests may need both of these values, so
889 * always copy both of them.
891 val_flags = sb.f_flags;
892 strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
894 switch (plan->flags & F_MTMASK) {
896 return val_flags & plan->mt_data;
898 return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
905 c_fstype(OPTION *option, char ***argvp)
910 fsname = nextarg(option, argvp);
911 ftsoptions &= ~FTS_NOSTAT;
913 new = palloc(option);
916 if (!strcmp(fsname, "local")) {
917 new->flags |= F_MTFLAG;
918 new->mt_data = MNT_LOCAL;
923 if (!strcmp(fsname, "rdonly")) {
924 new->flags |= F_MTFLAG;
925 new->mt_data = MNT_RDONLY;
931 new->flags |= F_MTTYPE;
932 new->c_data = fsname;
937 * -group gname functions --
939 * True if the file belongs to the group gname. If gname is numeric and
940 * an equivalent of the getgrnam() function does not return a valid group
941 * name, gname is taken as a group ID.
944 f_group(PLAN *plan, FTSENT *entry)
946 COMPARE(entry->fts_statp->st_gid, plan->g_data);
950 c_group(OPTION *option, char ***argvp)
957 gname = nextarg(option, argvp);
958 ftsoptions &= ~FTS_NOSTAT;
960 new = palloc(option);
964 if (gname[0] == '-' || gname[0] == '+')
967 if (gid == 0 && gname[0] != '0')
968 errx(1, "%s: %s: no such group", option->name, gname);
969 gid = find_parsenum(new, option->name, cp, NULL);
978 * -inum n functions --
980 * True if the file has inode # n.
983 f_inum(PLAN *plan, FTSENT *entry)
985 COMPARE(entry->fts_statp->st_ino, plan->i_data);
989 c_inum(OPTION *option, char ***argvp)
994 inum_str = nextarg(option, argvp);
995 ftsoptions &= ~FTS_NOSTAT;
997 new = palloc(option);
998 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
1005 * True if the file has the same inode (eg hard link) FN
1008 /* f_samefile is just f_inum */
1010 c_samefile(OPTION *option, char ***argvp)
1016 fn = nextarg(option, argvp);
1017 ftsoptions &= ~FTS_NOSTAT;
1019 new = palloc(option);
1022 new->i_data = sb.st_ino;
1027 * -links n functions --
1029 * True if the file has n links.
1032 f_links(PLAN *plan, FTSENT *entry)
1034 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1038 c_links(OPTION *option, char ***argvp)
1043 nlinks = nextarg(option, argvp);
1044 ftsoptions &= ~FTS_NOSTAT;
1046 new = palloc(option);
1047 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1054 * Always true - prints the current entry to stdout in "ls" format.
1057 f_ls(PLAN *plan __unused, FTSENT *entry)
1059 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1064 c_ls(OPTION *option, char ***argvp __unused)
1066 ftsoptions &= ~FTS_NOSTAT;
1069 return palloc(option);
1073 * -name functions --
1075 * True if the basename of the filename being examined
1076 * matches pattern using Pattern Matching Notation S3.14
1079 f_name(PLAN *plan, FTSENT *entry)
1084 if (plan->flags & F_LINK) {
1086 if (readlink(entry->fts_path, fn, sizeof(fn)) == -1)
1089 name = entry->fts_name;
1090 return !fnmatch(plan->c_data, name,
1091 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1095 c_name(OPTION *option, char ***argvp)
1100 pattern = nextarg(option, argvp);
1101 new = palloc(option);
1102 new->c_data = pattern;
1107 * -newer file functions --
1109 * True if the current file has been modified more recently
1110 * then the modification time of the file named by the pathname
1114 f_newer(PLAN *plan, FTSENT *entry)
1116 if (plan->flags & F_TIME_C)
1117 return entry->fts_statp->st_ctime > plan->t_data;
1118 else if (plan->flags & F_TIME_A)
1119 return entry->fts_statp->st_atime > plan->t_data;
1120 else if (plan->flags & F_TIME_B)
1121 return entry->fts_statp->st_birthtime > plan->t_data;
1123 return entry->fts_statp->st_mtime > plan->t_data;
1127 c_newer(OPTION *option, char ***argvp)
1133 fn_or_tspec = nextarg(option, argvp);
1134 ftsoptions &= ~FTS_NOSTAT;
1136 new = palloc(option);
1137 /* compare against what */
1138 if (option->flags & F_TIME2_T) {
1139 new->t_data = get_date(fn_or_tspec);
1140 if (new->t_data == (time_t) -1)
1141 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1143 if (stat(fn_or_tspec, &sb))
1144 err(1, "%s", fn_or_tspec);
1145 if (option->flags & F_TIME2_C)
1146 new->t_data = sb.st_ctime;
1147 else if (option->flags & F_TIME2_A)
1148 new->t_data = sb.st_atime;
1149 else if (option->flags & F_TIME2_B)
1150 new->t_data = sb.st_birthtime;
1152 new->t_data = sb.st_mtime;
1158 * -nogroup functions --
1160 * True if file belongs to a user ID for which the equivalent
1161 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1164 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1166 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1170 c_nogroup(OPTION *option, char ***argvp __unused)
1172 ftsoptions &= ~FTS_NOSTAT;
1174 return palloc(option);
1178 * -nouser functions --
1180 * True if file belongs to a user ID for which the equivalent
1181 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1184 f_nouser(PLAN *plan __unused, FTSENT *entry)
1186 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1190 c_nouser(OPTION *option, char ***argvp __unused)
1192 ftsoptions &= ~FTS_NOSTAT;
1194 return palloc(option);
1198 * -path functions --
1200 * True if the path of the filename being examined
1201 * matches pattern using Pattern Matching Notation S3.14
1204 f_path(PLAN *plan, FTSENT *entry)
1206 return !fnmatch(plan->c_data, entry->fts_path,
1207 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1210 /* c_path is the same as c_name */
1213 * -perm functions --
1215 * The mode argument is used to represent file mode bits. If it starts
1216 * with a leading digit, it's treated as an octal mode, otherwise as a
1220 f_perm(PLAN *plan, FTSENT *entry)
1224 mode = entry->fts_statp->st_mode &
1225 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1226 if (plan->flags & F_ATLEAST)
1227 return (plan->m_data | mode) == mode;
1228 else if (plan->flags & F_ANY)
1229 return (mode & plan->m_data);
1231 return mode == plan->m_data;
1236 c_perm(OPTION *option, char ***argvp)
1242 perm = nextarg(option, argvp);
1243 ftsoptions &= ~FTS_NOSTAT;
1245 new = palloc(option);
1248 new->flags |= F_ATLEAST;
1250 } else if (*perm == '+') {
1251 new->flags |= F_ANY;
1255 if ((set = setmode(perm)) == NULL)
1256 errx(1, "%s: %s: illegal mode string", option->name, perm);
1258 new->m_data = getmode(set, 0);
1264 * -print functions --
1266 * Always true, causes the current pathname to be written to
1270 f_print(PLAN *plan __unused, FTSENT *entry)
1272 (void)puts(entry->fts_path);
1277 c_print(OPTION *option, char ***argvp __unused)
1281 return palloc(option);
1285 * -print0 functions --
1287 * Always true, causes the current pathname to be written to
1288 * standard output followed by a NUL character
1291 f_print0(PLAN *plan __unused, FTSENT *entry)
1293 fputs(entry->fts_path, stdout);
1294 fputc('\0', stdout);
1298 /* c_print0 is the same as c_print */
1301 * -prune functions --
1303 * Prune a portion of the hierarchy.
1306 f_prune(PLAN *plan __unused, FTSENT *entry)
1308 if (fts_set(tree, entry, FTS_SKIP))
1309 err(1, "%s", entry->fts_path);
1313 /* c_prune == c_simple */
1316 * -regex functions --
1318 * True if the whole path of the file matches pattern using
1319 * regular expression.
1322 f_regex(PLAN *plan, FTSENT *entry)
1329 char errbuf[LINE_MAX];
1332 pre = plan->re_data;
1333 str = entry->fts_path;
1340 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1342 if (errcode != 0 && errcode != REG_NOMATCH) {
1343 regerror(errcode, pre, errbuf, sizeof errbuf);
1345 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1348 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1355 c_regex(OPTION *option, char ***argvp)
1361 char errbuf[LINE_MAX];
1363 if ((pre = malloc(sizeof(regex_t))) == NULL)
1366 pattern = nextarg(option, argvp);
1368 if ((errcode = regcomp(pre, pattern,
1369 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1370 regerror(errcode, pre, errbuf, sizeof errbuf);
1371 errx(1, "%s: %s: %s",
1372 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1376 new = palloc(option);
1382 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1385 c_simple(OPTION *option, char ***argvp __unused)
1387 return palloc(option);
1391 * -size n[c] functions --
1393 * True if the file size in bytes, divided by an implementation defined
1394 * value and rounded up to the next integer, is n. If n is followed by
1395 * one of c k M G T P, the size is in bytes, kilobytes,
1396 * megabytes, gigabytes, terabytes or petabytes respectively.
1398 #define FIND_SIZE 512
1399 static int divsize = 1;
1402 f_size(PLAN *plan, FTSENT *entry)
1406 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1407 FIND_SIZE : entry->fts_statp->st_size;
1408 COMPARE(size, plan->o_data);
1412 c_size(OPTION *option, char ***argvp)
1419 size_str = nextarg(option, argvp);
1420 ftsoptions &= ~FTS_NOSTAT;
1422 new = palloc(option);
1424 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1425 if (endch != '\0') {
1429 case 'c': /* characters */
1432 case 'k': /* kilobytes 1<<10 */
1435 case 'M': /* megabytes 1<<20 */
1438 case 'G': /* gigabytes 1<<30 */
1439 scale = 0x40000000LL;
1441 case 'T': /* terabytes 1<<40 */
1442 scale = 0x1000000000LL;
1444 case 'P': /* petabytes 1<<50 */
1445 scale = 0x4000000000000LL;
1448 errx(1, "%s: %s: illegal trailing character",
1449 option->name, size_str);
1452 if (new->o_data > QUAD_MAX / scale)
1453 errx(1, "%s: %s: value too large",
1454 option->name, size_str);
1455 new->o_data *= scale;
1461 * -type c functions --
1463 * True if the type of the file is c, where c is b, c, d, p, f or w
1464 * for block special file, character special file, directory, FIFO,
1465 * regular file or whiteout respectively.
1468 f_type(PLAN *plan, FTSENT *entry)
1470 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1474 c_type(OPTION *option, char ***argvp)
1480 typestring = nextarg(option, argvp);
1481 ftsoptions &= ~FTS_NOSTAT;
1483 switch (typestring[0]) {
1508 ftsoptions |= FTS_WHITEOUT;
1510 #endif /* FTS_WHITEOUT */
1512 errx(1, "%s: %s: unknown type", option->name, typestring);
1515 new = palloc(option);
1521 * -user uname functions --
1523 * True if the file belongs to the user uname. If uname is numeric and
1524 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1525 * return a valid user name, uname is taken as a user ID.
1528 f_user(PLAN *plan, FTSENT *entry)
1530 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1534 c_user(OPTION *option, char ***argvp)
1541 username = nextarg(option, argvp);
1542 ftsoptions &= ~FTS_NOSTAT;
1544 new = palloc(option);
1545 p = getpwnam(username);
1547 char* cp = username;
1548 if( username[0] == '-' || username[0] == '+' )
1550 uid = atoi(username);
1551 if (uid == 0 && username[0] != '0')
1552 errx(1, "%s: %s: no such user", option->name, username);
1553 uid = find_parsenum(new, option->name, cp, NULL);
1562 * -xdev functions --
1564 * Always true, causes find not to descend past directories that have a
1565 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1568 c_xdev(OPTION *option, char ***argvp __unused)
1570 ftsoptions |= FTS_XDEV;
1572 return palloc(option);
1576 * ( expression ) functions --
1578 * True if expression is true.
1581 f_expr(PLAN *plan, FTSENT *entry)
1586 for (p = plan->p_data[0];
1587 p && (state = (p->execute)(p, entry)); p = p->next);
1592 * f_openparen and f_closeparen nodes are temporary place markers. They are
1593 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1594 * to a f_expr node containing the expression and the ')' node is discarded.
1595 * The functions themselves are only used as constants.
1599 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1605 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1610 /* c_openparen == c_simple */
1611 /* c_closeparen == c_simple */
1614 * AND operator. Since AND is implicit, no node is allocated.
1617 c_and(OPTION *option __unused, char ***argvp __unused)
1623 * ! expression functions --
1625 * Negation of a primary; the unary NOT operator.
1628 f_not(PLAN *plan, FTSENT *entry)
1633 for (p = plan->p_data[0];
1634 p && (state = (p->execute)(p, entry)); p = p->next);
1638 /* c_not == c_simple */
1641 * expression -o expression functions --
1643 * Alternation of primaries; the OR operator. The second expression is
1644 * not evaluated if the first expression is true.
1647 f_or(PLAN *plan, FTSENT *entry)
1652 for (p = plan->p_data[0];
1653 p && (state = (p->execute)(p, entry)); p = p->next);
1658 for (p = plan->p_data[1];
1659 p && (state = (p->execute)(p, entry)); p = p->next);
1663 /* c_or == c_simple */
1671 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1676 /* c_false == c_simple */
1684 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1689 /* c_quit == c_simple */