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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/ucred.h>
49 #include <sys/types.h>
52 #include <sys/mount.h>
53 #include <sys/timeb.h>
72 static PLAN *palloc(OPTION *);
73 static long long find_parsenum(PLAN *, const char *, char *, char *);
74 static long long find_parsetime(PLAN *, const char *, char *);
75 static char *nextarg(OPTION *, char ***);
77 extern char **environ;
79 static PLAN *lastexecplus = NULL;
81 #define COMPARE(a, b) do { \
82 switch (plan->flags & F_ELG_MASK) { \
95 palloc(OPTION *option)
99 if ((new = malloc(sizeof(PLAN))) == NULL)
101 new->execute = option->execute;
102 new->flags = option->flags;
109 * Parse a string of the form [+-]# and return the value.
112 find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
115 char *endchar, *str; /* Pointer to character ending conversion. */
117 /* Determine comparison from leading + or -. */
122 plan->flags |= F_GREATER;
126 plan->flags |= F_LESSTHAN;
129 plan->flags |= F_EQUAL;
134 * Convert the string with strtoq(). Note, if strtoq() returns zero
135 * and endchar points to the beginning of the string we know we have
138 value = strtoq(str, &endchar, 10);
139 if (value == 0 && endchar == str)
140 errx(1, "%s: %s: illegal numeric value", option, vp);
141 if (endchar[0] && endch == NULL)
142 errx(1, "%s: %s: illegal trailing character", option, vp);
150 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
153 find_parsetime(PLAN *plan, const char *option, char *vp)
155 long long secs, value;
156 char *str, *unit; /* Pointer to character ending conversion. */
158 /* Determine comparison from leading + or -. */
163 plan->flags |= F_GREATER;
167 plan->flags |= F_LESSTHAN;
170 plan->flags |= F_EQUAL;
174 value = strtoq(str, &unit, 10);
175 if (value == 0 && unit == str) {
176 errx(1, "%s: %s: illegal time value", option, vp);
186 case 's': /* seconds */
189 case 'm': /* minutes */
192 case 'h': /* hours */
193 secs += value * 3600;
196 secs += value * 86400;
198 case 'w': /* weeks */
199 secs += value * 604800;
202 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
206 if (*str == '\0') /* EOS */
208 value = strtoq(str, &unit, 10);
209 if (value == 0 && unit == str) {
210 errx(1, "%s: %s: illegal time value", option, vp);
214 errx(1, "%s: %s: missing trailing unit", option, vp);
218 plan->flags |= F_EXACTTIME;
224 * Check that another argument still exists, return a pointer to it,
225 * and increment the argument vector pointer.
228 nextarg(OPTION *option, char ***argvp)
232 if ((arg = **argvp) == 0)
233 errx(1, "%s: requires additional arguments", option->name);
239 * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
240 * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts
241 * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
242 * the user wanted. Correct so that -1 is "less than 1".
244 #define TIME_CORRECT(p) \
245 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
249 * -[acm]min n functions --
251 * True if the difference between the
252 * file access time (-amin)
253 * file birth time (-Bmin)
254 * last change of file status information (-cmin)
255 * file modification time (-mmin)
256 * and the current time is n min periods.
259 f_Xmin(PLAN *plan, FTSENT *entry)
261 if (plan->flags & F_TIME_C) {
262 COMPARE((now - entry->fts_statp->st_ctime +
263 60 - 1) / 60, plan->t_data);
264 } else if (plan->flags & F_TIME_A) {
265 COMPARE((now - entry->fts_statp->st_atime +
266 60 - 1) / 60, plan->t_data);
267 } else if (plan->flags & F_TIME_B) {
268 COMPARE((now - entry->fts_statp->st_birthtime +
269 60 - 1) / 60, plan->t_data);
271 COMPARE((now - entry->fts_statp->st_mtime +
272 60 - 1) / 60, plan->t_data);
277 c_Xmin(OPTION *option, char ***argvp)
282 nmins = nextarg(option, argvp);
283 ftsoptions &= ~FTS_NOSTAT;
285 new = palloc(option);
286 new->t_data = find_parsenum(new, option->name, nmins, NULL);
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 else if (plan->flags & F_TIME_B)
310 xtime = entry->fts_statp->st_birthtime;
311 else if (plan->flags & F_TIME_C)
312 xtime = entry->fts_statp->st_ctime;
314 xtime = entry->fts_statp->st_mtime;
316 if (plan->flags & F_EXACTTIME)
317 COMPARE(now - xtime, plan->t_data);
319 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data);
323 c_Xtime(OPTION *option, char ***argvp)
328 value = nextarg(option, argvp);
329 ftsoptions &= ~FTS_NOSTAT;
331 new = palloc(option);
332 new->t_data = find_parsetime(new, option->name, value);
333 if (!(new->flags & F_EXACTTIME))
339 * -maxdepth/-mindepth n functions --
341 * Does the same as -prune if the level of the current file is
342 * greater/less than the specified maximum/minimum depth.
344 * Note that -maxdepth and -mindepth are handled specially in
345 * find_execute() so their f_* functions are set to f_always_true().
348 c_mXXdepth(OPTION *option, char ***argvp)
353 dstr = nextarg(option, argvp);
355 /* all other errors handled by find_parsenum() */
356 errx(1, "%s: %s: value must be positive", option->name, dstr);
358 new = palloc(option);
359 if (option->flags & F_MAXDEPTH)
360 maxdepth = find_parsenum(new, option->name, dstr, NULL);
362 mindepth = find_parsenum(new, option->name, dstr, NULL);
369 * Show files with EXTENDED ACL attributes.
372 f_acl(PLAN *plan __unused, FTSENT *entry)
376 int acl_supported = 0, ret, trivial;
378 if (S_ISLNK(entry->fts_statp->st_mode))
380 ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4);
383 acl_type = ACL_TYPE_NFS4;
384 } else if (ret < 0 && errno != EINVAL) {
385 warn("%s", entry->fts_accpath);
388 if (acl_supported == 0) {
389 ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED);
392 acl_type = ACL_TYPE_ACCESS;
393 } else if (ret < 0 && errno != EINVAL) {
394 warn("%s", entry->fts_accpath);
398 if (acl_supported == 0)
401 facl = acl_get_file(entry->fts_accpath, acl_type);
403 warn("%s", entry->fts_accpath);
406 ret = acl_is_trivial_np(facl, &trivial);
409 warn("%s", entry->fts_accpath);
419 c_acl(OPTION *option, char ***argvp __unused)
421 ftsoptions &= ~FTS_NOSTAT;
422 return (palloc(option));
426 * -delete functions --
428 * True always. Makes its best shot and continues on regardless.
431 f_delete(PLAN *plan __unused, FTSENT *entry)
433 /* ignore these from fts */
434 if (strcmp(entry->fts_accpath, ".") == 0 ||
435 strcmp(entry->fts_accpath, "..") == 0)
439 if (isdepth == 0 || /* depth off */
440 (ftsoptions & FTS_NOSTAT)) /* not stat()ing */
441 errx(1, "-delete: insecure options got turned on");
443 if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */
444 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
445 errx(1, "-delete: forbidden when symlinks are followed");
447 /* Potentially unsafe - do not accept relative paths whatsoever */
448 if (strchr(entry->fts_accpath, '/') != NULL)
449 errx(1, "-delete: %s: relative path potentially not safe",
452 /* Turn off user immutable bits if running as root */
453 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
454 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
456 lchflags(entry->fts_accpath,
457 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
459 /* rmdir directories, unlink everything else */
460 if (S_ISDIR(entry->fts_statp->st_mode)) {
461 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
462 warn("-delete: rmdir(%s)", entry->fts_path);
464 if (unlink(entry->fts_accpath) < 0)
465 warn("-delete: unlink(%s)", entry->fts_path);
473 c_delete(OPTION *option, char ***argvp __unused)
476 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
477 isoutput = 1; /* possible output */
478 isdepth = 1; /* -depth implied */
480 return palloc(option);
487 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
490 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
496 * -depth functions --
498 * With argument: True if the file is at level n.
499 * Without argument: Always true, causes descent of the directory hierarchy
500 * to be done so that all entries in a directory are acted on before the
504 f_depth(PLAN *plan, FTSENT *entry)
506 if (plan->flags & F_DEPTH)
507 COMPARE(entry->fts_level, plan->d_data);
513 c_depth(OPTION *option, char ***argvp)
518 new = palloc(option);
521 if (str && !(new->flags & F_DEPTH)) {
522 /* skip leading + or - */
523 if (*str == '+' || *str == '-')
526 if (*str == '+' || *str == '-')
529 new->flags |= F_DEPTH;
532 if (new->flags & F_DEPTH) { /* -depth n */
535 ndepth = nextarg(option, argvp);
536 new->d_data = find_parsenum(new, option->name, ndepth, NULL);
545 * -empty functions --
547 * True if the file or directory is empty
550 f_empty(PLAN *plan __unused, FTSENT *entry)
552 if (S_ISREG(entry->fts_statp->st_mode) &&
553 entry->fts_statp->st_size == 0)
555 if (S_ISDIR(entry->fts_statp->st_mode)) {
561 dir = opendir(entry->fts_accpath);
563 err(1, "%s", entry->fts_accpath);
564 for (dp = readdir(dir); dp; dp = readdir(dir))
565 if (dp->d_name[0] != '.' ||
566 (dp->d_name[1] != '\0' &&
567 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
578 c_empty(OPTION *option, char ***argvp __unused)
580 ftsoptions &= ~FTS_NOSTAT;
582 return palloc(option);
586 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
588 * True if the executed utility returns a zero value as exit status.
589 * The end of the primary expression is delimited by a semicolon. If
590 * "{}" occurs anywhere, it gets replaced by the current pathname,
591 * or, in the case of -execdir, the current basename (filename
592 * without leading directory prefix). For -exec and -ok,
593 * the current directory for the execution of utility is the same as
594 * the current directory when the find utility was started, whereas
595 * for -execdir, it is the directory the file resides in.
597 * The primary -ok differs from -exec in that it requests affirmation
598 * of the user before executing the utility.
601 f_exec(PLAN *plan, FTSENT *entry)
608 if (entry == NULL && plan->flags & F_EXECPLUS) {
609 if (plan->e_ppos == plan->e_pbnum)
611 plan->e_argv[plan->e_ppos] = NULL;
615 /* XXX - if file/dir ends in '/' this will not work -- can it? */
616 if ((plan->flags & F_EXECDIR) && \
617 (file = strrchr(entry->fts_path, '/')))
620 file = entry->fts_path;
622 if (plan->flags & F_EXECPLUS) {
623 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
625 plan->e_len[plan->e_ppos] = strlen(file);
626 plan->e_psize += plan->e_len[plan->e_ppos];
627 if (++plan->e_ppos < plan->e_pnummax &&
628 plan->e_psize < plan->e_psizemax)
630 plan->e_argv[plan->e_ppos] = NULL;
632 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
633 if (plan->e_len[cnt])
634 brace_subst(plan->e_orig[cnt],
635 &plan->e_argv[cnt], file,
639 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
642 /* make sure find output is interspersed correctly with subprocesses */
646 switch (pid = fork()) {
651 /* change dir back from where we started */
652 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
656 execvp(plan->e_argv[0], plan->e_argv);
657 warn("%s", plan->e_argv[0]);
660 if (plan->flags & F_EXECPLUS) {
661 while (--plan->e_ppos >= plan->e_pbnum)
662 free(plan->e_argv[plan->e_ppos]);
663 plan->e_ppos = plan->e_pbnum;
664 plan->e_psize = plan->e_pbsize;
666 pid = waitpid(pid, &status, 0);
667 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
671 * c_exec, c_execdir, c_ok --
672 * build three parallel arrays, one with pointers to the strings passed
673 * on the command line, one with (possibly duplicated) pointers to the
674 * argv array, and one with integer values that are lengths of the
675 * strings, but also flags meaning that the string has to be massaged.
678 c_exec(OPTION *option, char ***argvp)
680 PLAN *new; /* node returned */
683 char **argv, **ap, **ep, *p;
685 /* XXX - was in c_execdir, but seems unnecessary!?
686 ftsoptions &= ~FTS_NOSTAT;
690 /* XXX - this is a change from the previous coding */
691 new = palloc(option);
693 for (ap = argv = *argvp;; ++ap) {
696 "%s: no terminating \";\" or \"+\"", option->name);
699 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
700 new->flags |= F_EXECPLUS;
706 errx(1, "%s: no command specified", option->name);
708 cnt = ap - *argvp + 1;
709 if (new->flags & F_EXECPLUS) {
710 new->e_ppos = new->e_pbnum = cnt - 2;
711 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
712 warn("sysconf(_SC_ARG_MAX)");
713 argmax = _POSIX_ARG_MAX;
716 for (ep = environ; *ep != NULL; ep++)
717 argmax -= strlen(*ep) + 1 + sizeof(*ep);
718 argmax -= 1 + sizeof(*ep);
719 new->e_pnummax = argmax / 16;
720 argmax -= sizeof(char *) * new->e_pnummax;
722 errx(1, "no space for arguments");
723 new->e_psizemax = argmax;
725 cnt += new->e_pnummax + 1;
726 new->e_next = lastexecplus;
729 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
731 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
733 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
736 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
737 new->e_orig[cnt] = *argv;
738 if (new->flags & F_EXECPLUS)
739 new->e_pbsize += strlen(*argv) + 1;
740 for (p = *argv; *p; ++p)
741 if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
743 if ((new->e_argv[cnt] =
744 malloc(MAXPATHLEN)) == NULL)
746 new->e_len[cnt] = MAXPATHLEN;
750 new->e_argv[cnt] = *argv;
754 if (new->flags & F_EXECPLUS) {
755 new->e_psize = new->e_pbsize;
757 for (i = 0; i < new->e_pnummax; i++) {
758 new->e_argv[cnt] = NULL;
765 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
767 done: *argvp = argv + 1;
771 /* Finish any pending -exec ... {} + functions. */
779 (p->execute)(p, NULL);
785 f_flags(PLAN *plan, FTSENT *entry)
789 flags = entry->fts_statp->st_flags;
790 if (plan->flags & F_ATLEAST)
791 return (flags | plan->fl_flags) == flags &&
792 !(flags & plan->fl_notflags);
793 else if (plan->flags & F_ANY)
794 return (flags & plan->fl_flags) ||
795 (flags | plan->fl_notflags) != flags;
797 return flags == plan->fl_flags &&
798 !(plan->fl_flags & plan->fl_notflags);
802 c_flags(OPTION *option, char ***argvp)
806 u_long flags, notflags;
808 flags_str = nextarg(option, argvp);
809 ftsoptions &= ~FTS_NOSTAT;
811 new = palloc(option);
813 if (*flags_str == '-') {
814 new->flags |= F_ATLEAST;
816 } else if (*flags_str == '+') {
820 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
821 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
823 new->fl_flags = flags;
824 new->fl_notflags = notflags;
829 * -follow functions --
831 * Always true, causes symbolic links to be followed on a global
835 c_follow(OPTION *option, char ***argvp __unused)
837 ftsoptions &= ~FTS_PHYSICAL;
838 ftsoptions |= FTS_LOGICAL;
840 return palloc(option);
844 * -fstype functions --
846 * True if the file is of a certain type.
849 f_fstype(PLAN *plan, FTSENT *entry)
851 static dev_t curdev; /* need a guaranteed illegal dev value */
852 static int first = 1;
854 static int val_type, val_flags;
855 char *p, save[2] = {0,0};
857 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
860 /* Only check when we cross mount point. */
861 if (first || curdev != entry->fts_statp->st_dev) {
862 curdev = entry->fts_statp->st_dev;
865 * Statfs follows symlinks; find wants the link's filesystem,
866 * not where it points.
868 if (entry->fts_info == FTS_SL ||
869 entry->fts_info == FTS_SLNONE) {
870 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
873 p = entry->fts_accpath;
881 if (statfs(entry->fts_accpath, &sb))
882 err(1, "%s", entry->fts_accpath);
892 * Further tests may need both of these values, so
893 * always copy both of them.
895 val_flags = sb.f_flags;
896 val_type = sb.f_type;
898 switch (plan->flags & F_MTMASK) {
900 return val_flags & plan->mt_data;
902 return val_type == plan->mt_data;
909 c_fstype(OPTION *option, char ***argvp)
915 fsname = nextarg(option, argvp);
916 ftsoptions &= ~FTS_NOSTAT;
918 new = palloc(option);
921 * Check first for a filesystem name.
923 if (getvfsbyname(fsname, &vfc) == 0) {
924 new->flags |= F_MTTYPE;
925 new->mt_data = vfc.vfc_typenum;
931 if (!strcmp(fsname, "local")) {
932 new->flags |= F_MTFLAG;
933 new->mt_data = MNT_LOCAL;
938 if (!strcmp(fsname, "rdonly")) {
939 new->flags |= F_MTFLAG;
940 new->mt_data = MNT_RDONLY;
947 * We need to make filesystem checks for filesystems
948 * that exists but aren't in the kernel work.
950 fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname);
951 new->flags |= F_MTUNKNOWN;
956 * -group gname functions --
958 * True if the file belongs to the group gname. If gname is numeric and
959 * an equivalent of the getgrnam() function does not return a valid group
960 * name, gname is taken as a group ID.
963 f_group(PLAN *plan, FTSENT *entry)
965 COMPARE(entry->fts_statp->st_gid, plan->g_data);
969 c_group(OPTION *option, char ***argvp)
976 gname = nextarg(option, argvp);
977 ftsoptions &= ~FTS_NOSTAT;
979 new = palloc(option);
983 if (gname[0] == '-' || gname[0] == '+')
986 if (gid == 0 && gname[0] != '0')
987 errx(1, "%s: %s: no such group", option->name, gname);
988 gid = find_parsenum(new, option->name, cp, NULL);
997 * -inum n functions --
999 * True if the file has inode # n.
1002 f_inum(PLAN *plan, FTSENT *entry)
1004 COMPARE(entry->fts_statp->st_ino, plan->i_data);
1008 c_inum(OPTION *option, char ***argvp)
1013 inum_str = nextarg(option, argvp);
1014 ftsoptions &= ~FTS_NOSTAT;
1016 new = palloc(option);
1017 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
1024 * True if the file has the same inode (eg hard link) FN
1027 /* f_samefile is just f_inum */
1029 c_samefile(OPTION *option, char ***argvp)
1035 fn = nextarg(option, argvp);
1036 ftsoptions &= ~FTS_NOSTAT;
1038 new = palloc(option);
1041 new->i_data = sb.st_ino;
1046 * -links n functions --
1048 * True if the file has n links.
1051 f_links(PLAN *plan, FTSENT *entry)
1053 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1057 c_links(OPTION *option, char ***argvp)
1062 nlinks = nextarg(option, argvp);
1063 ftsoptions &= ~FTS_NOSTAT;
1065 new = palloc(option);
1066 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1073 * Always true - prints the current entry to stdout in "ls" format.
1076 f_ls(PLAN *plan __unused, FTSENT *entry)
1078 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1083 c_ls(OPTION *option, char ***argvp __unused)
1085 ftsoptions &= ~FTS_NOSTAT;
1088 return palloc(option);
1092 * -name functions --
1094 * True if the basename of the filename being examined
1095 * matches pattern using Pattern Matching Notation S3.14
1098 f_name(PLAN *plan, FTSENT *entry)
1103 if (plan->flags & F_LINK) {
1105 if (readlink(entry->fts_path, fn, sizeof(fn)) == -1)
1108 name = entry->fts_name;
1109 return !fnmatch(plan->c_data, name,
1110 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1114 c_name(OPTION *option, char ***argvp)
1119 pattern = nextarg(option, argvp);
1120 new = palloc(option);
1121 new->c_data = pattern;
1126 * -newer file functions --
1128 * True if the current file has been modified more recently
1129 * then the modification time of the file named by the pathname
1133 f_newer(PLAN *plan, FTSENT *entry)
1135 if (plan->flags & F_TIME_C)
1136 return entry->fts_statp->st_ctime > plan->t_data;
1137 else if (plan->flags & F_TIME_A)
1138 return entry->fts_statp->st_atime > plan->t_data;
1139 else if (plan->flags & F_TIME_B)
1140 return entry->fts_statp->st_birthtime > plan->t_data;
1142 return entry->fts_statp->st_mtime > plan->t_data;
1146 c_newer(OPTION *option, char ***argvp)
1152 fn_or_tspec = nextarg(option, argvp);
1153 ftsoptions &= ~FTS_NOSTAT;
1155 new = palloc(option);
1156 /* compare against what */
1157 if (option->flags & F_TIME2_T) {
1158 new->t_data = get_date(fn_or_tspec, (struct timeb *) 0);
1159 if (new->t_data == (time_t) -1)
1160 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1162 if (stat(fn_or_tspec, &sb))
1163 err(1, "%s", fn_or_tspec);
1164 if (option->flags & F_TIME2_C)
1165 new->t_data = sb.st_ctime;
1166 else if (option->flags & F_TIME2_A)
1167 new->t_data = sb.st_atime;
1168 else if (option->flags & F_TIME2_B)
1169 new->t_data = sb.st_birthtime;
1171 new->t_data = sb.st_mtime;
1177 * -nogroup functions --
1179 * True if file belongs to a user ID for which the equivalent
1180 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1183 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1185 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1189 c_nogroup(OPTION *option, char ***argvp __unused)
1191 ftsoptions &= ~FTS_NOSTAT;
1193 return palloc(option);
1197 * -nouser functions --
1199 * True if file belongs to a user ID for which the equivalent
1200 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1203 f_nouser(PLAN *plan __unused, FTSENT *entry)
1205 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1209 c_nouser(OPTION *option, char ***argvp __unused)
1211 ftsoptions &= ~FTS_NOSTAT;
1213 return palloc(option);
1217 * -path functions --
1219 * True if the path of the filename being examined
1220 * matches pattern using Pattern Matching Notation S3.14
1223 f_path(PLAN *plan, FTSENT *entry)
1225 return !fnmatch(plan->c_data, entry->fts_path,
1226 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1229 /* c_path is the same as c_name */
1232 * -perm functions --
1234 * The mode argument is used to represent file mode bits. If it starts
1235 * with a leading digit, it's treated as an octal mode, otherwise as a
1239 f_perm(PLAN *plan, FTSENT *entry)
1243 mode = entry->fts_statp->st_mode &
1244 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1245 if (plan->flags & F_ATLEAST)
1246 return (plan->m_data | mode) == mode;
1247 else if (plan->flags & F_ANY)
1248 return (mode & plan->m_data);
1250 return mode == plan->m_data;
1255 c_perm(OPTION *option, char ***argvp)
1261 perm = nextarg(option, argvp);
1262 ftsoptions &= ~FTS_NOSTAT;
1264 new = palloc(option);
1267 new->flags |= F_ATLEAST;
1269 } else if (*perm == '+') {
1270 new->flags |= F_ANY;
1274 if ((set = setmode(perm)) == NULL)
1275 errx(1, "%s: %s: illegal mode string", option->name, perm);
1277 new->m_data = getmode(set, 0);
1283 * -print functions --
1285 * Always true, causes the current pathname to be written to
1289 f_print(PLAN *plan __unused, FTSENT *entry)
1291 (void)puts(entry->fts_path);
1296 c_print(OPTION *option, char ***argvp __unused)
1300 return palloc(option);
1304 * -print0 functions --
1306 * Always true, causes the current pathname to be written to
1307 * standard output followed by a NUL character
1310 f_print0(PLAN *plan __unused, FTSENT *entry)
1312 fputs(entry->fts_path, stdout);
1313 fputc('\0', stdout);
1317 /* c_print0 is the same as c_print */
1320 * -prune functions --
1322 * Prune a portion of the hierarchy.
1325 f_prune(PLAN *plan __unused, FTSENT *entry)
1327 if (fts_set(tree, entry, FTS_SKIP))
1328 err(1, "%s", entry->fts_path);
1332 /* c_prune == c_simple */
1335 * -regex functions --
1337 * True if the whole path of the file matches pattern using
1338 * regular expression.
1341 f_regex(PLAN *plan, FTSENT *entry)
1348 char errbuf[LINE_MAX];
1351 pre = plan->re_data;
1352 str = entry->fts_path;
1359 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1361 if (errcode != 0 && errcode != REG_NOMATCH) {
1362 regerror(errcode, pre, errbuf, sizeof errbuf);
1364 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1367 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1374 c_regex(OPTION *option, char ***argvp)
1380 char errbuf[LINE_MAX];
1382 if ((pre = malloc(sizeof(regex_t))) == NULL)
1385 pattern = nextarg(option, argvp);
1387 if ((errcode = regcomp(pre, pattern,
1388 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1389 regerror(errcode, pre, errbuf, sizeof errbuf);
1390 errx(1, "%s: %s: %s",
1391 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1395 new = palloc(option);
1401 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1404 c_simple(OPTION *option, char ***argvp __unused)
1406 return palloc(option);
1410 * -size n[c] functions --
1412 * True if the file size in bytes, divided by an implementation defined
1413 * value and rounded up to the next integer, is n. If n is followed by
1414 * one of c k M G T P, the size is in bytes, kilobytes,
1415 * megabytes, gigabytes, terabytes or petabytes respectively.
1417 #define FIND_SIZE 512
1418 static int divsize = 1;
1421 f_size(PLAN *plan, FTSENT *entry)
1425 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1426 FIND_SIZE : entry->fts_statp->st_size;
1427 COMPARE(size, plan->o_data);
1431 c_size(OPTION *option, char ***argvp)
1438 size_str = nextarg(option, argvp);
1439 ftsoptions &= ~FTS_NOSTAT;
1441 new = palloc(option);
1443 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1444 if (endch != '\0') {
1448 case 'c': /* characters */
1451 case 'k': /* kilobytes 1<<10 */
1454 case 'M': /* megabytes 1<<20 */
1457 case 'G': /* gigabytes 1<<30 */
1458 scale = 0x40000000LL;
1460 case 'T': /* terabytes 1<<40 */
1461 scale = 0x1000000000LL;
1463 case 'P': /* petabytes 1<<50 */
1464 scale = 0x4000000000000LL;
1467 errx(1, "%s: %s: illegal trailing character",
1468 option->name, size_str);
1471 if (new->o_data > QUAD_MAX / scale)
1472 errx(1, "%s: %s: value too large",
1473 option->name, size_str);
1474 new->o_data *= scale;
1480 * -type c functions --
1482 * True if the type of the file is c, where c is b, c, d, p, f or w
1483 * for block special file, character special file, directory, FIFO,
1484 * regular file or whiteout respectively.
1487 f_type(PLAN *plan, FTSENT *entry)
1489 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1493 c_type(OPTION *option, char ***argvp)
1499 typestring = nextarg(option, argvp);
1500 ftsoptions &= ~FTS_NOSTAT;
1502 switch (typestring[0]) {
1527 ftsoptions |= FTS_WHITEOUT;
1529 #endif /* FTS_WHITEOUT */
1531 errx(1, "%s: %s: unknown type", option->name, typestring);
1534 new = palloc(option);
1540 * -user uname functions --
1542 * True if the file belongs to the user uname. If uname is numeric and
1543 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1544 * return a valid user name, uname is taken as a user ID.
1547 f_user(PLAN *plan, FTSENT *entry)
1549 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1553 c_user(OPTION *option, char ***argvp)
1560 username = nextarg(option, argvp);
1561 ftsoptions &= ~FTS_NOSTAT;
1563 new = palloc(option);
1564 p = getpwnam(username);
1566 char* cp = username;
1567 if( username[0] == '-' || username[0] == '+' )
1569 uid = atoi(username);
1570 if (uid == 0 && username[0] != '0')
1571 errx(1, "%s: %s: no such user", option->name, username);
1572 uid = find_parsenum(new, option->name, cp, NULL);
1581 * -xdev functions --
1583 * Always true, causes find not to descend past directories that have a
1584 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1587 c_xdev(OPTION *option, char ***argvp __unused)
1589 ftsoptions |= FTS_XDEV;
1591 return palloc(option);
1595 * ( expression ) functions --
1597 * True if expression is true.
1600 f_expr(PLAN *plan, FTSENT *entry)
1605 for (p = plan->p_data[0];
1606 p && (state = (p->execute)(p, entry)); p = p->next);
1611 * f_openparen and f_closeparen nodes are temporary place markers. They are
1612 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1613 * to a f_expr node containing the expression and the ')' node is discarded.
1614 * The functions themselves are only used as constants.
1618 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1624 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1629 /* c_openparen == c_simple */
1630 /* c_closeparen == c_simple */
1633 * AND operator. Since AND is implicit, no node is allocated.
1636 c_and(OPTION *option __unused, char ***argvp __unused)
1642 * ! expression functions --
1644 * Negation of a primary; the unary NOT operator.
1647 f_not(PLAN *plan, FTSENT *entry)
1652 for (p = plan->p_data[0];
1653 p && (state = (p->execute)(p, entry)); p = p->next);
1657 /* c_not == c_simple */
1660 * expression -o expression functions --
1662 * Alternation of primaries; the OR operator. The second expression is
1663 * not evaluated if the first expression is true.
1666 f_or(PLAN *plan, FTSENT *entry)
1671 for (p = plan->p_data[0];
1672 p && (state = (p->execute)(p, entry)); p = p->next);
1677 for (p = plan->p_data[1];
1678 p && (state = (p->execute)(p, entry)); p = p->next);
1682 /* c_or == c_simple */
1690 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1695 /* c_false == c_simple */
1703 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1708 /* c_quit == c_simple */