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>
71 static PLAN *palloc(OPTION *);
72 static long long find_parsenum(PLAN *, const char *, char *, char *);
73 static long long find_parsetime(PLAN *, const char *, char *);
74 static char *nextarg(OPTION *, char ***);
76 extern char **environ;
78 static PLAN *lastexecplus = NULL;
80 #define COMPARE(a, b) do { \
81 switch (plan->flags & F_ELG_MASK) { \
94 palloc(OPTION *option)
98 if ((new = malloc(sizeof(PLAN))) == NULL)
100 new->execute = option->execute;
101 new->flags = option->flags;
108 * Parse a string of the form [+-]# and return the value.
111 find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
114 char *endchar, *str; /* Pointer to character ending conversion. */
116 /* Determine comparison from leading + or -. */
121 plan->flags |= F_GREATER;
125 plan->flags |= F_LESSTHAN;
128 plan->flags |= F_EQUAL;
133 * Convert the string with strtoq(). Note, if strtoq() returns zero
134 * and endchar points to the beginning of the string we know we have
137 value = strtoq(str, &endchar, 10);
138 if (value == 0 && endchar == str)
139 errx(1, "%s: %s: illegal numeric value", option, vp);
140 if (endchar[0] && endch == NULL)
141 errx(1, "%s: %s: illegal trailing character", option, vp);
149 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
152 find_parsetime(PLAN *plan, const char *option, char *vp)
154 long long secs, value;
155 char *str, *unit; /* Pointer to character ending conversion. */
157 /* Determine comparison from leading + or -. */
162 plan->flags |= F_GREATER;
166 plan->flags |= F_LESSTHAN;
169 plan->flags |= F_EQUAL;
173 value = strtoq(str, &unit, 10);
174 if (value == 0 && unit == str) {
175 errx(1, "%s: %s: illegal time value", option, vp);
185 case 's': /* seconds */
188 case 'm': /* minutes */
191 case 'h': /* hours */
192 secs += value * 3600;
195 secs += value * 86400;
197 case 'w': /* weeks */
198 secs += value * 604800;
201 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
205 if (*str == '\0') /* EOS */
207 value = strtoq(str, &unit, 10);
208 if (value == 0 && unit == str) {
209 errx(1, "%s: %s: illegal time value", option, vp);
213 errx(1, "%s: %s: missing trailing unit", option, vp);
217 plan->flags |= F_EXACTTIME;
223 * Check that another argument still exists, return a pointer to it,
224 * and increment the argument vector pointer.
227 nextarg(OPTION *option, char ***argvp)
231 if ((arg = **argvp) == 0)
232 errx(1, "%s: requires additional arguments", option->name);
238 * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
239 * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts
240 * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
241 * the user wanted. Correct so that -1 is "less than 1".
243 #define TIME_CORRECT(p) \
244 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
248 * -[acm]min n functions --
250 * True if the difference between the
251 * file access time (-amin)
252 * file birth time (-Bmin)
253 * last change of file status information (-cmin)
254 * file modification time (-mmin)
255 * and the current time is n min periods.
258 f_Xmin(PLAN *plan, FTSENT *entry)
260 if (plan->flags & F_TIME_C) {
261 COMPARE((now - entry->fts_statp->st_ctime +
262 60 - 1) / 60, plan->t_data);
263 } else if (plan->flags & F_TIME_A) {
264 COMPARE((now - entry->fts_statp->st_atime +
265 60 - 1) / 60, plan->t_data);
266 } else if (plan->flags & F_TIME_B) {
267 COMPARE((now - entry->fts_statp->st_birthtime +
268 60 - 1) / 60, plan->t_data);
270 COMPARE((now - entry->fts_statp->st_mtime +
271 60 - 1) / 60, plan->t_data);
276 c_Xmin(OPTION *option, char ***argvp)
281 nmins = nextarg(option, argvp);
282 ftsoptions &= ~FTS_NOSTAT;
284 new = palloc(option);
285 new->t_data = find_parsenum(new, option->name, nmins, NULL);
291 * -[acm]time n functions --
293 * True if the difference between the
294 * file access time (-atime)
295 * file birth time (-Btime)
296 * last change of file status information (-ctime)
297 * file modification time (-mtime)
298 * and the current time is n 24 hour periods.
302 f_Xtime(PLAN *plan, FTSENT *entry)
306 if (plan->flags & F_TIME_A)
307 xtime = entry->fts_statp->st_atime;
308 else if (plan->flags & F_TIME_B)
309 xtime = entry->fts_statp->st_birthtime;
310 else if (plan->flags & F_TIME_C)
311 xtime = entry->fts_statp->st_ctime;
313 xtime = entry->fts_statp->st_mtime;
315 if (plan->flags & F_EXACTTIME)
316 COMPARE(now - xtime, plan->t_data);
318 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data);
322 c_Xtime(OPTION *option, char ***argvp)
327 value = nextarg(option, argvp);
328 ftsoptions &= ~FTS_NOSTAT;
330 new = palloc(option);
331 new->t_data = find_parsetime(new, option->name, value);
332 if (!(new->flags & F_EXACTTIME))
338 * -maxdepth/-mindepth n functions --
340 * Does the same as -prune if the level of the current file is
341 * greater/less than the specified maximum/minimum depth.
343 * Note that -maxdepth and -mindepth are handled specially in
344 * find_execute() so their f_* functions are set to f_always_true().
347 c_mXXdepth(OPTION *option, char ***argvp)
352 dstr = nextarg(option, argvp);
354 /* all other errors handled by find_parsenum() */
355 errx(1, "%s: %s: value must be positive", option->name, dstr);
357 new = palloc(option);
358 if (option->flags & F_MAXDEPTH)
359 maxdepth = find_parsenum(new, option->name, dstr, NULL);
361 mindepth = find_parsenum(new, option->name, dstr, NULL);
368 * Show files with EXTENDED ACL attributes.
371 f_acl(PLAN *plan __unused, FTSENT *entry)
375 int acl_supported = 0, ret, trivial;
377 if (S_ISLNK(entry->fts_statp->st_mode))
379 ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4);
382 acl_type = ACL_TYPE_NFS4;
383 } else if (ret < 0 && errno != EINVAL) {
384 warn("%s", entry->fts_accpath);
387 if (acl_supported == 0) {
388 ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED);
391 acl_type = ACL_TYPE_ACCESS;
392 } else if (ret < 0 && errno != EINVAL) {
393 warn("%s", entry->fts_accpath);
397 if (acl_supported == 0)
400 facl = acl_get_file(entry->fts_accpath, acl_type);
402 warn("%s", entry->fts_accpath);
405 ret = acl_is_trivial_np(facl, &trivial);
408 warn("%s", entry->fts_accpath);
418 c_acl(OPTION *option, char ***argvp __unused)
420 ftsoptions &= ~FTS_NOSTAT;
421 return (palloc(option));
425 * -delete functions --
427 * True always. Makes its best shot and continues on regardless.
430 f_delete(PLAN *plan __unused, FTSENT *entry)
432 /* ignore these from fts */
433 if (strcmp(entry->fts_accpath, ".") == 0 ||
434 strcmp(entry->fts_accpath, "..") == 0)
438 if (isdepth == 0 || /* depth off */
439 (ftsoptions & FTS_NOSTAT)) /* not stat()ing */
440 errx(1, "-delete: insecure options got turned on");
442 if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */
443 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
444 errx(1, "-delete: forbidden when symlinks are followed");
446 /* Potentially unsafe - do not accept relative paths whatsoever */
447 if (strchr(entry->fts_accpath, '/') != NULL)
448 errx(1, "-delete: %s: relative path potentially not safe",
451 /* Turn off user immutable bits if running as root */
452 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
453 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
455 lchflags(entry->fts_accpath,
456 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
458 /* rmdir directories, unlink everything else */
459 if (S_ISDIR(entry->fts_statp->st_mode)) {
460 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
461 warn("-delete: rmdir(%s)", entry->fts_path);
463 if (unlink(entry->fts_accpath) < 0)
464 warn("-delete: unlink(%s)", entry->fts_path);
472 c_delete(OPTION *option, char ***argvp __unused)
475 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
476 isoutput = 1; /* possible output */
477 isdepth = 1; /* -depth implied */
479 return palloc(option);
486 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
489 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
495 * -depth functions --
497 * With argument: True if the file is at level n.
498 * Without argument: Always true, causes descent of the directory hierarchy
499 * to be done so that all entries in a directory are acted on before the
503 f_depth(PLAN *plan, FTSENT *entry)
505 if (plan->flags & F_DEPTH)
506 COMPARE(entry->fts_level, plan->d_data);
512 c_depth(OPTION *option, char ***argvp)
517 new = palloc(option);
520 if (str && !(new->flags & F_DEPTH)) {
521 /* skip leading + or - */
522 if (*str == '+' || *str == '-')
525 if (*str == '+' || *str == '-')
528 new->flags |= F_DEPTH;
531 if (new->flags & F_DEPTH) { /* -depth n */
534 ndepth = nextarg(option, argvp);
535 new->d_data = find_parsenum(new, option->name, ndepth, NULL);
544 * -empty functions --
546 * True if the file or directory is empty
549 f_empty(PLAN *plan __unused, FTSENT *entry)
551 if (S_ISREG(entry->fts_statp->st_mode) &&
552 entry->fts_statp->st_size == 0)
554 if (S_ISDIR(entry->fts_statp->st_mode)) {
560 dir = opendir(entry->fts_accpath);
562 err(1, "%s", entry->fts_accpath);
563 for (dp = readdir(dir); dp; dp = readdir(dir))
564 if (dp->d_name[0] != '.' ||
565 (dp->d_name[1] != '\0' &&
566 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
577 c_empty(OPTION *option, char ***argvp __unused)
579 ftsoptions &= ~FTS_NOSTAT;
581 return palloc(option);
585 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
587 * True if the executed utility returns a zero value as exit status.
588 * The end of the primary expression is delimited by a semicolon. If
589 * "{}" occurs anywhere, it gets replaced by the current pathname,
590 * or, in the case of -execdir, the current basename (filename
591 * without leading directory prefix). For -exec and -ok,
592 * the current directory for the execution of utility is the same as
593 * the current directory when the find utility was started, whereas
594 * for -execdir, it is the directory the file resides in.
596 * The primary -ok differs from -exec in that it requests affirmation
597 * of the user before executing the utility.
600 f_exec(PLAN *plan, FTSENT *entry)
607 if (entry == NULL && plan->flags & F_EXECPLUS) {
608 if (plan->e_ppos == plan->e_pbnum)
610 plan->e_argv[plan->e_ppos] = NULL;
614 /* XXX - if file/dir ends in '/' this will not work -- can it? */
615 if ((plan->flags & F_EXECDIR) && \
616 (file = strrchr(entry->fts_path, '/')))
619 file = entry->fts_path;
621 if (plan->flags & F_EXECPLUS) {
622 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
624 plan->e_len[plan->e_ppos] = strlen(file);
625 plan->e_psize += plan->e_len[plan->e_ppos];
626 if (++plan->e_ppos < plan->e_pnummax &&
627 plan->e_psize < plan->e_psizemax)
629 plan->e_argv[plan->e_ppos] = NULL;
631 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
632 if (plan->e_len[cnt])
633 brace_subst(plan->e_orig[cnt],
634 &plan->e_argv[cnt], file,
638 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
641 /* make sure find output is interspersed correctly with subprocesses */
645 switch (pid = fork()) {
650 /* change dir back from where we started */
651 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
655 execvp(plan->e_argv[0], plan->e_argv);
656 warn("%s", plan->e_argv[0]);
659 if (plan->flags & F_EXECPLUS) {
660 while (--plan->e_ppos >= plan->e_pbnum)
661 free(plan->e_argv[plan->e_ppos]);
662 plan->e_ppos = plan->e_pbnum;
663 plan->e_psize = plan->e_pbsize;
665 pid = waitpid(pid, &status, 0);
666 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
670 * c_exec, c_execdir, c_ok --
671 * build three parallel arrays, one with pointers to the strings passed
672 * on the command line, one with (possibly duplicated) pointers to the
673 * argv array, and one with integer values that are lengths of the
674 * strings, but also flags meaning that the string has to be massaged.
677 c_exec(OPTION *option, char ***argvp)
679 PLAN *new; /* node returned */
682 char **argv, **ap, **ep, *p;
684 /* XXX - was in c_execdir, but seems unnecessary!?
685 ftsoptions &= ~FTS_NOSTAT;
689 /* XXX - this is a change from the previous coding */
690 new = palloc(option);
692 for (ap = argv = *argvp;; ++ap) {
695 "%s: no terminating \";\" or \"+\"", option->name);
698 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
699 new->flags |= F_EXECPLUS;
705 errx(1, "%s: no command specified", option->name);
707 cnt = ap - *argvp + 1;
708 if (new->flags & F_EXECPLUS) {
709 new->e_ppos = new->e_pbnum = cnt - 2;
710 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
711 warn("sysconf(_SC_ARG_MAX)");
712 argmax = _POSIX_ARG_MAX;
715 for (ep = environ; *ep != NULL; ep++)
716 argmax -= strlen(*ep) + 1 + sizeof(*ep);
717 argmax -= 1 + sizeof(*ep);
718 new->e_pnummax = argmax / 16;
719 argmax -= sizeof(char *) * new->e_pnummax;
721 errx(1, "no space for arguments");
722 new->e_psizemax = argmax;
724 cnt += new->e_pnummax + 1;
725 new->e_next = lastexecplus;
728 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
730 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
732 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
735 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
736 new->e_orig[cnt] = *argv;
737 if (new->flags & F_EXECPLUS)
738 new->e_pbsize += strlen(*argv) + 1;
739 for (p = *argv; *p; ++p)
740 if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
742 if ((new->e_argv[cnt] =
743 malloc(MAXPATHLEN)) == NULL)
745 new->e_len[cnt] = MAXPATHLEN;
749 new->e_argv[cnt] = *argv;
753 if (new->flags & F_EXECPLUS) {
754 new->e_psize = new->e_pbsize;
756 for (i = 0; i < new->e_pnummax; i++) {
757 new->e_argv[cnt] = NULL;
764 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
766 done: *argvp = argv + 1;
770 /* Finish any pending -exec ... {} + functions. */
772 finish_execplus(void)
778 (p->execute)(p, NULL);
784 f_flags(PLAN *plan, FTSENT *entry)
788 flags = entry->fts_statp->st_flags;
789 if (plan->flags & F_ATLEAST)
790 return (flags | plan->fl_flags) == flags &&
791 !(flags & plan->fl_notflags);
792 else if (plan->flags & F_ANY)
793 return (flags & plan->fl_flags) ||
794 (flags | plan->fl_notflags) != flags;
796 return flags == plan->fl_flags &&
797 !(plan->fl_flags & plan->fl_notflags);
801 c_flags(OPTION *option, char ***argvp)
805 u_long flags, notflags;
807 flags_str = nextarg(option, argvp);
808 ftsoptions &= ~FTS_NOSTAT;
810 new = palloc(option);
812 if (*flags_str == '-') {
813 new->flags |= F_ATLEAST;
815 } else if (*flags_str == '+') {
819 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
820 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
822 new->fl_flags = flags;
823 new->fl_notflags = notflags;
828 * -follow functions --
830 * Always true, causes symbolic links to be followed on a global
834 c_follow(OPTION *option, char ***argvp __unused)
836 ftsoptions &= ~FTS_PHYSICAL;
837 ftsoptions |= FTS_LOGICAL;
839 return palloc(option);
843 * -fstype functions --
845 * True if the file is of a certain type.
848 f_fstype(PLAN *plan, FTSENT *entry)
850 static dev_t curdev; /* need a guaranteed illegal dev value */
851 static int first = 1;
853 static int val_type, val_flags;
854 char *p, save[2] = {0,0};
856 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
859 /* Only check when we cross mount point. */
860 if (first || curdev != entry->fts_statp->st_dev) {
861 curdev = entry->fts_statp->st_dev;
864 * Statfs follows symlinks; find wants the link's filesystem,
865 * not where it points.
867 if (entry->fts_info == FTS_SL ||
868 entry->fts_info == FTS_SLNONE) {
869 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
872 p = entry->fts_accpath;
880 if (statfs(entry->fts_accpath, &sb))
881 err(1, "%s", entry->fts_accpath);
891 * Further tests may need both of these values, so
892 * always copy both of them.
894 val_flags = sb.f_flags;
895 val_type = sb.f_type;
897 switch (plan->flags & F_MTMASK) {
899 return val_flags & plan->mt_data;
901 return val_type == plan->mt_data;
908 c_fstype(OPTION *option, char ***argvp)
914 fsname = nextarg(option, argvp);
915 ftsoptions &= ~FTS_NOSTAT;
917 new = palloc(option);
920 * Check first for a filesystem name.
922 if (getvfsbyname(fsname, &vfc) == 0) {
923 new->flags |= F_MTTYPE;
924 new->mt_data = vfc.vfc_typenum;
930 if (!strcmp(fsname, "local")) {
931 new->flags |= F_MTFLAG;
932 new->mt_data = MNT_LOCAL;
937 if (!strcmp(fsname, "rdonly")) {
938 new->flags |= F_MTFLAG;
939 new->mt_data = MNT_RDONLY;
946 * We need to make filesystem checks for filesystems
947 * that exists but aren't in the kernel work.
949 fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname);
950 new->flags |= F_MTUNKNOWN;
955 * -group gname functions --
957 * True if the file belongs to the group gname. If gname is numeric and
958 * an equivalent of the getgrnam() function does not return a valid group
959 * name, gname is taken as a group ID.
962 f_group(PLAN *plan, FTSENT *entry)
964 COMPARE(entry->fts_statp->st_gid, plan->g_data);
968 c_group(OPTION *option, char ***argvp)
975 gname = nextarg(option, argvp);
976 ftsoptions &= ~FTS_NOSTAT;
978 new = palloc(option);
982 if (gname[0] == '-' || gname[0] == '+')
985 if (gid == 0 && gname[0] != '0')
986 errx(1, "%s: %s: no such group", option->name, gname);
987 gid = find_parsenum(new, option->name, cp, NULL);
996 * -inum n functions --
998 * True if the file has inode # n.
1001 f_inum(PLAN *plan, FTSENT *entry)
1003 COMPARE(entry->fts_statp->st_ino, plan->i_data);
1007 c_inum(OPTION *option, char ***argvp)
1012 inum_str = nextarg(option, argvp);
1013 ftsoptions &= ~FTS_NOSTAT;
1015 new = palloc(option);
1016 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
1023 * True if the file has the same inode (eg hard link) FN
1026 /* f_samefile is just f_inum */
1028 c_samefile(OPTION *option, char ***argvp)
1034 fn = nextarg(option, argvp);
1035 ftsoptions &= ~FTS_NOSTAT;
1037 new = palloc(option);
1040 new->i_data = sb.st_ino;
1045 * -links n functions --
1047 * True if the file has n links.
1050 f_links(PLAN *plan, FTSENT *entry)
1052 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1056 c_links(OPTION *option, char ***argvp)
1061 nlinks = nextarg(option, argvp);
1062 ftsoptions &= ~FTS_NOSTAT;
1064 new = palloc(option);
1065 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1072 * Always true - prints the current entry to stdout in "ls" format.
1075 f_ls(PLAN *plan __unused, FTSENT *entry)
1077 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1082 c_ls(OPTION *option, char ***argvp __unused)
1084 ftsoptions &= ~FTS_NOSTAT;
1087 return palloc(option);
1091 * -name functions --
1093 * True if the basename of the filename being examined
1094 * matches pattern using Pattern Matching Notation S3.14
1097 f_name(PLAN *plan, FTSENT *entry)
1102 if (plan->flags & F_LINK) {
1104 if (readlink(entry->fts_path, fn, sizeof(fn)) == -1)
1107 name = entry->fts_name;
1108 return !fnmatch(plan->c_data, name,
1109 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1113 c_name(OPTION *option, char ***argvp)
1118 pattern = nextarg(option, argvp);
1119 new = palloc(option);
1120 new->c_data = pattern;
1125 * -newer file functions --
1127 * True if the current file has been modified more recently
1128 * then the modification time of the file named by the pathname
1132 f_newer(PLAN *plan, FTSENT *entry)
1134 if (plan->flags & F_TIME_C)
1135 return entry->fts_statp->st_ctime > plan->t_data;
1136 else if (plan->flags & F_TIME_A)
1137 return entry->fts_statp->st_atime > plan->t_data;
1138 else if (plan->flags & F_TIME_B)
1139 return entry->fts_statp->st_birthtime > plan->t_data;
1141 return entry->fts_statp->st_mtime > plan->t_data;
1145 c_newer(OPTION *option, char ***argvp)
1151 fn_or_tspec = nextarg(option, argvp);
1152 ftsoptions &= ~FTS_NOSTAT;
1154 new = palloc(option);
1155 /* compare against what */
1156 if (option->flags & F_TIME2_T) {
1157 new->t_data = get_date(fn_or_tspec);
1158 if (new->t_data == (time_t) -1)
1159 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1161 if (stat(fn_or_tspec, &sb))
1162 err(1, "%s", fn_or_tspec);
1163 if (option->flags & F_TIME2_C)
1164 new->t_data = sb.st_ctime;
1165 else if (option->flags & F_TIME2_A)
1166 new->t_data = sb.st_atime;
1167 else if (option->flags & F_TIME2_B)
1168 new->t_data = sb.st_birthtime;
1170 new->t_data = sb.st_mtime;
1176 * -nogroup functions --
1178 * True if file belongs to a user ID for which the equivalent
1179 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1182 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1184 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1188 c_nogroup(OPTION *option, char ***argvp __unused)
1190 ftsoptions &= ~FTS_NOSTAT;
1192 return palloc(option);
1196 * -nouser functions --
1198 * True if file belongs to a user ID for which the equivalent
1199 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1202 f_nouser(PLAN *plan __unused, FTSENT *entry)
1204 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1208 c_nouser(OPTION *option, char ***argvp __unused)
1210 ftsoptions &= ~FTS_NOSTAT;
1212 return palloc(option);
1216 * -path functions --
1218 * True if the path of the filename being examined
1219 * matches pattern using Pattern Matching Notation S3.14
1222 f_path(PLAN *plan, FTSENT *entry)
1224 return !fnmatch(plan->c_data, entry->fts_path,
1225 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1228 /* c_path is the same as c_name */
1231 * -perm functions --
1233 * The mode argument is used to represent file mode bits. If it starts
1234 * with a leading digit, it's treated as an octal mode, otherwise as a
1238 f_perm(PLAN *plan, FTSENT *entry)
1242 mode = entry->fts_statp->st_mode &
1243 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1244 if (plan->flags & F_ATLEAST)
1245 return (plan->m_data | mode) == mode;
1246 else if (plan->flags & F_ANY)
1247 return (mode & plan->m_data);
1249 return mode == plan->m_data;
1254 c_perm(OPTION *option, char ***argvp)
1260 perm = nextarg(option, argvp);
1261 ftsoptions &= ~FTS_NOSTAT;
1263 new = palloc(option);
1266 new->flags |= F_ATLEAST;
1268 } else if (*perm == '+') {
1269 new->flags |= F_ANY;
1273 if ((set = setmode(perm)) == NULL)
1274 errx(1, "%s: %s: illegal mode string", option->name, perm);
1276 new->m_data = getmode(set, 0);
1282 * -print functions --
1284 * Always true, causes the current pathname to be written to
1288 f_print(PLAN *plan __unused, FTSENT *entry)
1290 (void)puts(entry->fts_path);
1295 c_print(OPTION *option, char ***argvp __unused)
1299 return palloc(option);
1303 * -print0 functions --
1305 * Always true, causes the current pathname to be written to
1306 * standard output followed by a NUL character
1309 f_print0(PLAN *plan __unused, FTSENT *entry)
1311 fputs(entry->fts_path, stdout);
1312 fputc('\0', stdout);
1316 /* c_print0 is the same as c_print */
1319 * -prune functions --
1321 * Prune a portion of the hierarchy.
1324 f_prune(PLAN *plan __unused, FTSENT *entry)
1326 if (fts_set(tree, entry, FTS_SKIP))
1327 err(1, "%s", entry->fts_path);
1331 /* c_prune == c_simple */
1334 * -regex functions --
1336 * True if the whole path of the file matches pattern using
1337 * regular expression.
1340 f_regex(PLAN *plan, FTSENT *entry)
1347 char errbuf[LINE_MAX];
1350 pre = plan->re_data;
1351 str = entry->fts_path;
1358 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1360 if (errcode != 0 && errcode != REG_NOMATCH) {
1361 regerror(errcode, pre, errbuf, sizeof errbuf);
1363 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1366 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1373 c_regex(OPTION *option, char ***argvp)
1379 char errbuf[LINE_MAX];
1381 if ((pre = malloc(sizeof(regex_t))) == NULL)
1384 pattern = nextarg(option, argvp);
1386 if ((errcode = regcomp(pre, pattern,
1387 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1388 regerror(errcode, pre, errbuf, sizeof errbuf);
1389 errx(1, "%s: %s: %s",
1390 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1394 new = palloc(option);
1400 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1403 c_simple(OPTION *option, char ***argvp __unused)
1405 return palloc(option);
1409 * -size n[c] functions --
1411 * True if the file size in bytes, divided by an implementation defined
1412 * value and rounded up to the next integer, is n. If n is followed by
1413 * one of c k M G T P, the size is in bytes, kilobytes,
1414 * megabytes, gigabytes, terabytes or petabytes respectively.
1416 #define FIND_SIZE 512
1417 static int divsize = 1;
1420 f_size(PLAN *plan, FTSENT *entry)
1424 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1425 FIND_SIZE : entry->fts_statp->st_size;
1426 COMPARE(size, plan->o_data);
1430 c_size(OPTION *option, char ***argvp)
1437 size_str = nextarg(option, argvp);
1438 ftsoptions &= ~FTS_NOSTAT;
1440 new = palloc(option);
1442 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1443 if (endch != '\0') {
1447 case 'c': /* characters */
1450 case 'k': /* kilobytes 1<<10 */
1453 case 'M': /* megabytes 1<<20 */
1456 case 'G': /* gigabytes 1<<30 */
1457 scale = 0x40000000LL;
1459 case 'T': /* terabytes 1<<40 */
1460 scale = 0x1000000000LL;
1462 case 'P': /* petabytes 1<<50 */
1463 scale = 0x4000000000000LL;
1466 errx(1, "%s: %s: illegal trailing character",
1467 option->name, size_str);
1470 if (new->o_data > QUAD_MAX / scale)
1471 errx(1, "%s: %s: value too large",
1472 option->name, size_str);
1473 new->o_data *= scale;
1479 * -type c functions --
1481 * True if the type of the file is c, where c is b, c, d, p, f or w
1482 * for block special file, character special file, directory, FIFO,
1483 * regular file or whiteout respectively.
1486 f_type(PLAN *plan, FTSENT *entry)
1488 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1492 c_type(OPTION *option, char ***argvp)
1498 typestring = nextarg(option, argvp);
1499 ftsoptions &= ~FTS_NOSTAT;
1501 switch (typestring[0]) {
1526 ftsoptions |= FTS_WHITEOUT;
1528 #endif /* FTS_WHITEOUT */
1530 errx(1, "%s: %s: unknown type", option->name, typestring);
1533 new = palloc(option);
1539 * -user uname functions --
1541 * True if the file belongs to the user uname. If uname is numeric and
1542 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1543 * return a valid user name, uname is taken as a user ID.
1546 f_user(PLAN *plan, FTSENT *entry)
1548 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1552 c_user(OPTION *option, char ***argvp)
1559 username = nextarg(option, argvp);
1560 ftsoptions &= ~FTS_NOSTAT;
1562 new = palloc(option);
1563 p = getpwnam(username);
1565 char* cp = username;
1566 if( username[0] == '-' || username[0] == '+' )
1568 uid = atoi(username);
1569 if (uid == 0 && username[0] != '0')
1570 errx(1, "%s: %s: no such user", option->name, username);
1571 uid = find_parsenum(new, option->name, cp, NULL);
1580 * -xdev functions --
1582 * Always true, causes find not to descend past directories that have a
1583 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1586 c_xdev(OPTION *option, char ***argvp __unused)
1588 ftsoptions |= FTS_XDEV;
1590 return palloc(option);
1594 * ( expression ) functions --
1596 * True if expression is true.
1599 f_expr(PLAN *plan, FTSENT *entry)
1604 for (p = plan->p_data[0];
1605 p && (state = (p->execute)(p, entry)); p = p->next);
1610 * f_openparen and f_closeparen nodes are temporary place markers. They are
1611 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1612 * to a f_expr node containing the expression and the ')' node is discarded.
1613 * The functions themselves are only used as constants.
1617 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1623 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1628 /* c_openparen == c_simple */
1629 /* c_closeparen == c_simple */
1632 * AND operator. Since AND is implicit, no node is allocated.
1635 c_and(OPTION *option __unused, char ***argvp __unused)
1641 * ! expression functions --
1643 * Negation of a primary; the unary NOT operator.
1646 f_not(PLAN *plan, FTSENT *entry)
1651 for (p = plan->p_data[0];
1652 p && (state = (p->execute)(p, entry)); p = p->next);
1656 /* c_not == c_simple */
1659 * expression -o expression functions --
1661 * Alternation of primaries; the OR operator. The second expression is
1662 * not evaluated if the first expression is true.
1665 f_or(PLAN *plan, FTSENT *entry)
1670 for (p = plan->p_data[0];
1671 p && (state = (p->execute)(p, entry)); p = p->next);
1676 for (p = plan->p_data[1];
1677 p && (state = (p->execute)(p, entry)); p = p->next);
1681 /* c_or == c_simple */
1689 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1694 /* c_false == c_simple */
1702 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1707 /* c_quit == c_simple */