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) \
241 ++((p)->t_data.tv_sec);
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.tv_sec);
259 } else if (plan->flags & F_TIME_A) {
260 COMPARE((now - entry->fts_statp->st_atime +
261 60 - 1) / 60, plan->t_data.tv_sec);
262 } else if (plan->flags & F_TIME_B) {
263 COMPARE((now - entry->fts_statp->st_birthtime +
264 60 - 1) / 60, plan->t_data.tv_sec);
266 COMPARE((now - entry->fts_statp->st_mtime +
267 60 - 1) / 60, plan->t_data.tv_sec);
272 c_Xmin(OPTION *option, char ***argvp)
277 nmins = nextarg(option, argvp);
278 ftsoptions &= ~FTS_NOSTAT;
280 new = palloc(option);
281 new->t_data.tv_sec = find_parsenum(new, option->name, nmins, NULL);
282 new->t_data.tv_nsec = 0;
288 * -[acm]time n functions --
290 * True if the difference between the
291 * file access time (-atime)
292 * file birth time (-Btime)
293 * last change of file status information (-ctime)
294 * file modification time (-mtime)
295 * and the current time is n 24 hour periods.
299 f_Xtime(PLAN *plan, FTSENT *entry)
303 if (plan->flags & F_TIME_A)
304 xtime = entry->fts_statp->st_atime;
305 else if (plan->flags & F_TIME_B)
306 xtime = entry->fts_statp->st_birthtime;
307 else if (plan->flags & F_TIME_C)
308 xtime = entry->fts_statp->st_ctime;
310 xtime = entry->fts_statp->st_mtime;
312 if (plan->flags & F_EXACTTIME)
313 COMPARE(now - xtime, plan->t_data.tv_sec);
315 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data.tv_sec);
319 c_Xtime(OPTION *option, char ***argvp)
324 value = nextarg(option, argvp);
325 ftsoptions &= ~FTS_NOSTAT;
327 new = palloc(option);
328 new->t_data.tv_sec = find_parsetime(new, option->name, value);
329 new->t_data.tv_nsec = 0;
330 if (!(new->flags & F_EXACTTIME))
336 * -maxdepth/-mindepth n functions --
338 * Does the same as -prune if the level of the current file is
339 * greater/less than the specified maximum/minimum depth.
341 * Note that -maxdepth and -mindepth are handled specially in
342 * find_execute() so their f_* functions are set to f_always_true().
345 c_mXXdepth(OPTION *option, char ***argvp)
350 dstr = nextarg(option, argvp);
352 /* all other errors handled by find_parsenum() */
353 errx(1, "%s: %s: value must be positive", option->name, dstr);
355 new = palloc(option);
356 if (option->flags & F_MAXDEPTH)
357 maxdepth = find_parsenum(new, option->name, dstr, NULL);
359 mindepth = find_parsenum(new, option->name, dstr, NULL);
366 * Show files with EXTENDED ACL attributes.
369 f_acl(PLAN *plan __unused, FTSENT *entry)
373 int acl_supported = 0, ret, trivial;
375 if (S_ISLNK(entry->fts_statp->st_mode))
377 ret = pathconf(entry->fts_accpath, _PC_ACL_NFS4);
380 acl_type = ACL_TYPE_NFS4;
381 } else if (ret < 0 && errno != EINVAL) {
382 warn("%s", entry->fts_accpath);
385 if (acl_supported == 0) {
386 ret = pathconf(entry->fts_accpath, _PC_ACL_EXTENDED);
389 acl_type = ACL_TYPE_ACCESS;
390 } else if (ret < 0 && errno != EINVAL) {
391 warn("%s", entry->fts_accpath);
395 if (acl_supported == 0)
398 facl = acl_get_file(entry->fts_accpath, acl_type);
400 warn("%s", entry->fts_accpath);
403 ret = acl_is_trivial_np(facl, &trivial);
406 warn("%s", entry->fts_accpath);
416 c_acl(OPTION *option, char ***argvp __unused)
418 ftsoptions &= ~FTS_NOSTAT;
419 return (palloc(option));
423 * -delete functions --
425 * True always. Makes its best shot and continues on regardless.
428 f_delete(PLAN *plan __unused, FTSENT *entry)
430 /* ignore these from fts */
431 if (strcmp(entry->fts_accpath, ".") == 0 ||
432 strcmp(entry->fts_accpath, "..") == 0)
436 if (isdepth == 0 || /* depth off */
437 (ftsoptions & FTS_NOSTAT)) /* not stat()ing */
438 errx(1, "-delete: insecure options got turned on");
440 if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */
441 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
442 errx(1, "-delete: forbidden when symlinks are followed");
444 /* Potentially unsafe - do not accept relative paths whatsoever */
445 if (entry->fts_level > FTS_ROOTLEVEL &&
446 strchr(entry->fts_accpath, '/') != NULL)
447 errx(1, "-delete: %s: relative path potentially not safe",
450 /* Turn off user immutable bits if running as root */
451 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
452 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
454 lchflags(entry->fts_accpath,
455 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
457 /* rmdir directories, unlink everything else */
458 if (S_ISDIR(entry->fts_statp->st_mode)) {
459 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
460 warn("-delete: rmdir(%s)", entry->fts_path);
462 if (unlink(entry->fts_accpath) < 0)
463 warn("-delete: unlink(%s)", entry->fts_path);
471 c_delete(OPTION *option, char ***argvp __unused)
474 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
475 isoutput = 1; /* possible output */
476 isdepth = 1; /* -depth implied */
479 * Try to avoid the confusing error message about relative paths
480 * being potentially not safe.
482 if (ftsoptions & FTS_NOCHDIR)
483 errx(1, "%s: forbidden when the current directory cannot be opened",
486 return palloc(option);
493 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
496 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
502 * -depth functions --
504 * With argument: True if the file is at level n.
505 * Without argument: Always true, causes descent of the directory hierarchy
506 * to be done so that all entries in a directory are acted on before the
510 f_depth(PLAN *plan, FTSENT *entry)
512 if (plan->flags & F_DEPTH)
513 COMPARE(entry->fts_level, plan->d_data);
519 c_depth(OPTION *option, char ***argvp)
524 new = palloc(option);
527 if (str && !(new->flags & F_DEPTH)) {
528 /* skip leading + or - */
529 if (*str == '+' || *str == '-')
532 if (*str == '+' || *str == '-')
535 new->flags |= F_DEPTH;
538 if (new->flags & F_DEPTH) { /* -depth n */
541 ndepth = nextarg(option, argvp);
542 new->d_data = find_parsenum(new, option->name, ndepth, NULL);
551 * -empty functions --
553 * True if the file or directory is empty
556 f_empty(PLAN *plan __unused, FTSENT *entry)
558 if (S_ISREG(entry->fts_statp->st_mode) &&
559 entry->fts_statp->st_size == 0)
561 if (S_ISDIR(entry->fts_statp->st_mode)) {
567 dir = opendir(entry->fts_accpath);
570 for (dp = readdir(dir); dp; dp = readdir(dir))
571 if (dp->d_name[0] != '.' ||
572 (dp->d_name[1] != '\0' &&
573 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
584 c_empty(OPTION *option, char ***argvp __unused)
586 ftsoptions &= ~FTS_NOSTAT;
588 return palloc(option);
592 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
594 * True if the executed utility returns a zero value as exit status.
595 * The end of the primary expression is delimited by a semicolon. If
596 * "{}" occurs anywhere, it gets replaced by the current pathname,
597 * or, in the case of -execdir, the current basename (filename
598 * without leading directory prefix). For -exec and -ok,
599 * the current directory for the execution of utility is the same as
600 * the current directory when the find utility was started, whereas
601 * for -execdir, it is the directory the file resides in.
603 * The primary -ok differs from -exec in that it requests affirmation
604 * of the user before executing the utility.
607 f_exec(PLAN *plan, FTSENT *entry)
614 if (entry == NULL && plan->flags & F_EXECPLUS) {
615 if (plan->e_ppos == plan->e_pbnum)
617 plan->e_argv[plan->e_ppos] = NULL;
621 /* XXX - if file/dir ends in '/' this will not work -- can it? */
622 if ((plan->flags & F_EXECDIR) && \
623 (file = strrchr(entry->fts_path, '/')))
626 file = entry->fts_path;
628 if (plan->flags & F_EXECPLUS) {
629 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
631 plan->e_len[plan->e_ppos] = strlen(file);
632 plan->e_psize += plan->e_len[plan->e_ppos];
633 if (++plan->e_ppos < plan->e_pnummax &&
634 plan->e_psize < plan->e_psizemax)
636 plan->e_argv[plan->e_ppos] = NULL;
638 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
639 if (plan->e_len[cnt])
640 brace_subst(plan->e_orig[cnt],
641 &plan->e_argv[cnt], file,
645 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
648 /* make sure find output is interspersed correctly with subprocesses */
652 switch (pid = fork()) {
657 /* change dir back from where we started */
658 if (!(plan->flags & F_EXECDIR) &&
659 !(ftsoptions & FTS_NOCHDIR) && fchdir(dotfd)) {
663 execvp(plan->e_argv[0], plan->e_argv);
664 warn("%s", plan->e_argv[0]);
667 if (plan->flags & F_EXECPLUS) {
668 while (--plan->e_ppos >= plan->e_pbnum)
669 free(plan->e_argv[plan->e_ppos]);
670 plan->e_ppos = plan->e_pbnum;
671 plan->e_psize = plan->e_pbsize;
673 pid = waitpid(pid, &status, 0);
674 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
678 * c_exec, c_execdir, c_ok --
679 * build three parallel arrays, one with pointers to the strings passed
680 * on the command line, one with (possibly duplicated) pointers to the
681 * argv array, and one with integer values that are lengths of the
682 * strings, but also flags meaning that the string has to be massaged.
685 c_exec(OPTION *option, char ***argvp)
687 PLAN *new; /* node returned */
690 char **argv, **ap, **ep, *p;
692 /* This would defeat -execdir's intended security. */
693 if (option->flags & F_EXECDIR && ftsoptions & FTS_NOCHDIR)
694 errx(1, "%s: forbidden when the current directory cannot be opened",
697 /* XXX - was in c_execdir, but seems unnecessary!?
698 ftsoptions &= ~FTS_NOSTAT;
702 /* XXX - this is a change from the previous coding */
703 new = palloc(option);
705 for (ap = argv = *argvp;; ++ap) {
708 "%s: no terminating \";\" or \"+\"", option->name);
711 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
712 new->flags |= F_EXECPLUS;
718 errx(1, "%s: no command specified", option->name);
720 cnt = ap - *argvp + 1;
721 if (new->flags & F_EXECPLUS) {
722 new->e_ppos = new->e_pbnum = cnt - 2;
723 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
724 warn("sysconf(_SC_ARG_MAX)");
725 argmax = _POSIX_ARG_MAX;
728 for (ep = environ; *ep != NULL; ep++)
729 argmax -= strlen(*ep) + 1 + sizeof(*ep);
730 argmax -= 1 + sizeof(*ep);
732 * Ensure that -execdir ... {} + does not mix files
733 * from different directories in one invocation.
734 * Files from the same directory should be handled
735 * in one invocation but there is no code for it.
737 new->e_pnummax = new->flags & F_EXECDIR ? 1 : argmax / 16;
738 argmax -= sizeof(char *) * new->e_pnummax;
740 errx(1, "no space for arguments");
741 new->e_psizemax = argmax;
743 cnt += new->e_pnummax + 1;
744 new->e_next = lastexecplus;
747 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
749 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
751 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
754 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
755 new->e_orig[cnt] = *argv;
756 if (new->flags & F_EXECPLUS)
757 new->e_pbsize += strlen(*argv) + 1;
758 for (p = *argv; *p; ++p)
759 if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
761 if ((new->e_argv[cnt] =
762 malloc(MAXPATHLEN)) == NULL)
764 new->e_len[cnt] = MAXPATHLEN;
768 new->e_argv[cnt] = *argv;
772 if (new->flags & F_EXECPLUS) {
773 new->e_psize = new->e_pbsize;
775 for (i = 0; i < new->e_pnummax; i++) {
776 new->e_argv[cnt] = NULL;
783 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
785 done: *argvp = argv + 1;
789 /* Finish any pending -exec ... {} + functions. */
791 finish_execplus(void)
797 (p->execute)(p, NULL);
803 f_flags(PLAN *plan, FTSENT *entry)
807 flags = entry->fts_statp->st_flags;
808 if (plan->flags & F_ATLEAST)
809 return (flags | plan->fl_flags) == flags &&
810 !(flags & plan->fl_notflags);
811 else if (plan->flags & F_ANY)
812 return (flags & plan->fl_flags) ||
813 (flags | plan->fl_notflags) != flags;
815 return flags == plan->fl_flags &&
816 !(plan->fl_flags & plan->fl_notflags);
820 c_flags(OPTION *option, char ***argvp)
824 u_long flags, notflags;
826 flags_str = nextarg(option, argvp);
827 ftsoptions &= ~FTS_NOSTAT;
829 new = palloc(option);
831 if (*flags_str == '-') {
832 new->flags |= F_ATLEAST;
834 } else if (*flags_str == '+') {
838 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
839 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
841 new->fl_flags = flags;
842 new->fl_notflags = notflags;
847 * -follow functions --
849 * Always true, causes symbolic links to be followed on a global
853 c_follow(OPTION *option, char ***argvp __unused)
855 ftsoptions &= ~FTS_PHYSICAL;
856 ftsoptions |= FTS_LOGICAL;
858 return palloc(option);
862 * -fstype functions --
864 * True if the file is of a certain type.
867 f_fstype(PLAN *plan, FTSENT *entry)
869 static dev_t curdev; /* need a guaranteed illegal dev value */
870 static int first = 1;
872 static int val_flags;
873 static char fstype[sizeof(sb.f_fstypename)];
874 char *p, save[2] = {0,0};
876 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
879 /* Only check when we cross mount point. */
880 if (first || curdev != entry->fts_statp->st_dev) {
881 curdev = entry->fts_statp->st_dev;
884 * Statfs follows symlinks; find wants the link's filesystem,
885 * not where it points.
887 if (entry->fts_info == FTS_SL ||
888 entry->fts_info == FTS_SLNONE) {
889 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
892 p = entry->fts_accpath;
900 if (statfs(entry->fts_accpath, &sb))
901 err(1, "%s", entry->fts_accpath);
911 * Further tests may need both of these values, so
912 * always copy both of them.
914 val_flags = sb.f_flags;
915 strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
917 switch (plan->flags & F_MTMASK) {
919 return val_flags & plan->mt_data;
921 return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
928 c_fstype(OPTION *option, char ***argvp)
933 fsname = nextarg(option, argvp);
934 ftsoptions &= ~FTS_NOSTAT;
936 new = palloc(option);
939 if (!strcmp(fsname, "local")) {
940 new->flags |= F_MTFLAG;
941 new->mt_data = MNT_LOCAL;
946 if (!strcmp(fsname, "rdonly")) {
947 new->flags |= F_MTFLAG;
948 new->mt_data = MNT_RDONLY;
954 new->flags |= F_MTTYPE;
955 new->c_data = fsname;
960 * -group gname functions --
962 * True if the file belongs to the group gname. If gname is numeric and
963 * an equivalent of the getgrnam() function does not return a valid group
964 * name, gname is taken as a group ID.
967 f_group(PLAN *plan, FTSENT *entry)
969 COMPARE(entry->fts_statp->st_gid, plan->g_data);
973 c_group(OPTION *option, char ***argvp)
980 gname = nextarg(option, argvp);
981 ftsoptions &= ~FTS_NOSTAT;
983 new = palloc(option);
987 if (gname[0] == '-' || gname[0] == '+')
990 if (gid == 0 && gname[0] != '0')
991 errx(1, "%s: %s: no such group", option->name, gname);
992 gid = find_parsenum(new, option->name, cp, NULL);
1001 * -ignore_readdir_race functions --
1003 * Always true. Ignore errors which occur if a file or a directory
1004 * in a starting point gets deleted between reading the name and calling
1005 * stat on it while find is traversing the starting point.
1009 c_ignore_readdir_race(OPTION *option, char ***argvp __unused)
1011 if (strcmp(option->name, "-ignore_readdir_race") == 0)
1012 ignore_readdir_race = 1;
1014 ignore_readdir_race = 0;
1016 return palloc(option);
1020 * -inum n functions --
1022 * True if the file has inode # n.
1025 f_inum(PLAN *plan, FTSENT *entry)
1027 COMPARE(entry->fts_statp->st_ino, plan->i_data);
1031 c_inum(OPTION *option, char ***argvp)
1036 inum_str = nextarg(option, argvp);
1037 ftsoptions &= ~FTS_NOSTAT;
1039 new = palloc(option);
1040 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
1047 * True if the file has the same inode (eg hard link) FN
1050 /* f_samefile is just f_inum */
1052 c_samefile(OPTION *option, char ***argvp)
1058 fn = nextarg(option, argvp);
1059 ftsoptions &= ~FTS_NOSTAT;
1061 new = palloc(option);
1064 new->i_data = sb.st_ino;
1069 * -links n functions --
1071 * True if the file has n links.
1074 f_links(PLAN *plan, FTSENT *entry)
1076 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
1080 c_links(OPTION *option, char ***argvp)
1085 nlinks = nextarg(option, argvp);
1086 ftsoptions &= ~FTS_NOSTAT;
1088 new = palloc(option);
1089 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
1096 * Always true - prints the current entry to stdout in "ls" format.
1099 f_ls(PLAN *plan __unused, FTSENT *entry)
1101 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1106 c_ls(OPTION *option, char ***argvp __unused)
1108 ftsoptions &= ~FTS_NOSTAT;
1111 return palloc(option);
1115 * -name functions --
1117 * True if the basename of the filename being examined
1118 * matches pattern using Pattern Matching Notation S3.14
1121 f_name(PLAN *plan, FTSENT *entry)
1126 if (plan->flags & F_LINK) {
1128 if (readlink(entry->fts_path, fn, sizeof(fn)) == -1)
1131 name = entry->fts_name;
1132 return !fnmatch(plan->c_data, name,
1133 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1137 c_name(OPTION *option, char ***argvp)
1142 pattern = nextarg(option, argvp);
1143 new = palloc(option);
1144 new->c_data = pattern;
1149 * -newer file functions --
1151 * True if the current file has been modified more recently
1152 * then the modification time of the file named by the pathname
1156 f_newer(PLAN *plan, FTSENT *entry)
1160 if (plan->flags & F_TIME_C)
1161 ft = entry->fts_statp->st_ctim;
1162 else if (plan->flags & F_TIME_A)
1163 ft = entry->fts_statp->st_atim;
1164 else if (plan->flags & F_TIME_B)
1165 ft = entry->fts_statp->st_birthtim;
1167 ft = entry->fts_statp->st_mtim;
1168 return (ft.tv_sec > plan->t_data.tv_sec ||
1169 (ft.tv_sec == plan->t_data.tv_sec &&
1170 ft.tv_nsec > plan->t_data.tv_nsec));
1174 c_newer(OPTION *option, char ***argvp)
1180 fn_or_tspec = nextarg(option, argvp);
1181 ftsoptions &= ~FTS_NOSTAT;
1183 new = palloc(option);
1184 /* compare against what */
1185 if (option->flags & F_TIME2_T) {
1186 new->t_data.tv_sec = get_date(fn_or_tspec);
1187 if (new->t_data.tv_sec == (time_t) -1)
1188 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1189 /* Use the seconds only in the comparison. */
1190 new->t_data.tv_nsec = 999999999;
1192 if (stat(fn_or_tspec, &sb))
1193 err(1, "%s", fn_or_tspec);
1194 if (option->flags & F_TIME2_C)
1195 new->t_data = sb.st_ctim;
1196 else if (option->flags & F_TIME2_A)
1197 new->t_data = sb.st_atim;
1198 else if (option->flags & F_TIME2_B)
1199 new->t_data = sb.st_birthtim;
1201 new->t_data = sb.st_mtim;
1207 * -nogroup functions --
1209 * True if file belongs to a user ID for which the equivalent
1210 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1213 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1215 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1219 c_nogroup(OPTION *option, char ***argvp __unused)
1221 ftsoptions &= ~FTS_NOSTAT;
1223 return palloc(option);
1227 * -nouser functions --
1229 * True if file belongs to a user ID for which the equivalent
1230 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1233 f_nouser(PLAN *plan __unused, FTSENT *entry)
1235 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1239 c_nouser(OPTION *option, char ***argvp __unused)
1241 ftsoptions &= ~FTS_NOSTAT;
1243 return palloc(option);
1247 * -path functions --
1249 * True if the path of the filename being examined
1250 * matches pattern using Pattern Matching Notation S3.14
1253 f_path(PLAN *plan, FTSENT *entry)
1255 return !fnmatch(plan->c_data, entry->fts_path,
1256 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1259 /* c_path is the same as c_name */
1262 * -perm functions --
1264 * The mode argument is used to represent file mode bits. If it starts
1265 * with a leading digit, it's treated as an octal mode, otherwise as a
1269 f_perm(PLAN *plan, FTSENT *entry)
1273 mode = entry->fts_statp->st_mode &
1274 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1275 if (plan->flags & F_ATLEAST)
1276 return (plan->m_data | mode) == mode;
1277 else if (plan->flags & F_ANY)
1278 return (mode & plan->m_data);
1280 return mode == plan->m_data;
1285 c_perm(OPTION *option, char ***argvp)
1291 perm = nextarg(option, argvp);
1292 ftsoptions &= ~FTS_NOSTAT;
1294 new = palloc(option);
1297 new->flags |= F_ATLEAST;
1299 } else if (*perm == '+') {
1300 new->flags |= F_ANY;
1304 if ((set = setmode(perm)) == NULL)
1305 errx(1, "%s: %s: illegal mode string", option->name, perm);
1307 new->m_data = getmode(set, 0);
1313 * -print functions --
1315 * Always true, causes the current pathname to be written to
1319 f_print(PLAN *plan __unused, FTSENT *entry)
1321 (void)puts(entry->fts_path);
1326 c_print(OPTION *option, char ***argvp __unused)
1330 return palloc(option);
1334 * -print0 functions --
1336 * Always true, causes the current pathname to be written to
1337 * standard output followed by a NUL character
1340 f_print0(PLAN *plan __unused, FTSENT *entry)
1342 fputs(entry->fts_path, stdout);
1343 fputc('\0', stdout);
1347 /* c_print0 is the same as c_print */
1350 * -prune functions --
1352 * Prune a portion of the hierarchy.
1355 f_prune(PLAN *plan __unused, FTSENT *entry)
1357 if (fts_set(tree, entry, FTS_SKIP))
1358 err(1, "%s", entry->fts_path);
1362 /* c_prune == c_simple */
1365 * -regex functions --
1367 * True if the whole path of the file matches pattern using
1368 * regular expression.
1371 f_regex(PLAN *plan, FTSENT *entry)
1378 char errbuf[LINE_MAX];
1381 pre = plan->re_data;
1382 str = entry->fts_path;
1389 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1391 if (errcode != 0 && errcode != REG_NOMATCH) {
1392 regerror(errcode, pre, errbuf, sizeof errbuf);
1394 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1397 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1404 c_regex(OPTION *option, char ***argvp)
1410 char errbuf[LINE_MAX];
1412 if ((pre = malloc(sizeof(regex_t))) == NULL)
1415 pattern = nextarg(option, argvp);
1417 if ((errcode = regcomp(pre, pattern,
1418 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1419 regerror(errcode, pre, errbuf, sizeof errbuf);
1420 errx(1, "%s: %s: %s",
1421 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1425 new = palloc(option);
1431 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1434 c_simple(OPTION *option, char ***argvp __unused)
1436 return palloc(option);
1440 * -size n[c] functions --
1442 * True if the file size in bytes, divided by an implementation defined
1443 * value and rounded up to the next integer, is n. If n is followed by
1444 * one of c k M G T P, the size is in bytes, kilobytes,
1445 * megabytes, gigabytes, terabytes or petabytes respectively.
1447 #define FIND_SIZE 512
1448 static int divsize = 1;
1451 f_size(PLAN *plan, FTSENT *entry)
1455 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1456 FIND_SIZE : entry->fts_statp->st_size;
1457 COMPARE(size, plan->o_data);
1461 c_size(OPTION *option, char ***argvp)
1468 size_str = nextarg(option, argvp);
1469 ftsoptions &= ~FTS_NOSTAT;
1471 new = palloc(option);
1473 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1474 if (endch != '\0') {
1478 case 'c': /* characters */
1481 case 'k': /* kilobytes 1<<10 */
1484 case 'M': /* megabytes 1<<20 */
1487 case 'G': /* gigabytes 1<<30 */
1488 scale = 0x40000000LL;
1490 case 'T': /* terabytes 1<<40 */
1491 scale = 0x1000000000LL;
1493 case 'P': /* petabytes 1<<50 */
1494 scale = 0x4000000000000LL;
1497 errx(1, "%s: %s: illegal trailing character",
1498 option->name, size_str);
1501 if (new->o_data > QUAD_MAX / scale)
1502 errx(1, "%s: %s: value too large",
1503 option->name, size_str);
1504 new->o_data *= scale;
1510 * -sparse functions --
1512 * Check if a file is sparse by finding if it occupies fewer blocks
1513 * than we expect based on its size.
1516 f_sparse(PLAN *plan __unused, FTSENT *entry)
1518 off_t expected_blocks;
1520 expected_blocks = (entry->fts_statp->st_size + 511) / 512;
1521 return entry->fts_statp->st_blocks < expected_blocks;
1525 c_sparse(OPTION *option, char ***argvp __unused)
1527 ftsoptions &= ~FTS_NOSTAT;
1529 return palloc(option);
1533 * -type c functions --
1535 * True if the type of the file is c, where c is b, c, d, p, f or w
1536 * for block special file, character special file, directory, FIFO,
1537 * regular file or whiteout respectively.
1540 f_type(PLAN *plan, FTSENT *entry)
1542 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1546 c_type(OPTION *option, char ***argvp)
1552 typestring = nextarg(option, argvp);
1553 ftsoptions &= ~FTS_NOSTAT;
1555 switch (typestring[0]) {
1580 ftsoptions |= FTS_WHITEOUT;
1582 #endif /* FTS_WHITEOUT */
1584 errx(1, "%s: %s: unknown type", option->name, typestring);
1587 new = palloc(option);
1593 * -user uname functions --
1595 * True if the file belongs to the user uname. If uname is numeric and
1596 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1597 * return a valid user name, uname is taken as a user ID.
1600 f_user(PLAN *plan, FTSENT *entry)
1602 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1606 c_user(OPTION *option, char ***argvp)
1613 username = nextarg(option, argvp);
1614 ftsoptions &= ~FTS_NOSTAT;
1616 new = palloc(option);
1617 p = getpwnam(username);
1619 char* cp = username;
1620 if( username[0] == '-' || username[0] == '+' )
1622 uid = atoi(username);
1623 if (uid == 0 && username[0] != '0')
1624 errx(1, "%s: %s: no such user", option->name, username);
1625 uid = find_parsenum(new, option->name, cp, NULL);
1634 * -xdev functions --
1636 * Always true, causes find not to descend past directories that have a
1637 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1640 c_xdev(OPTION *option, char ***argvp __unused)
1642 ftsoptions |= FTS_XDEV;
1644 return palloc(option);
1648 * ( expression ) functions --
1650 * True if expression is true.
1653 f_expr(PLAN *plan, FTSENT *entry)
1658 for (p = plan->p_data[0];
1659 p && (state = (p->execute)(p, entry)); p = p->next);
1664 * f_openparen and f_closeparen nodes are temporary place markers. They are
1665 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1666 * to a f_expr node containing the expression and the ')' node is discarded.
1667 * The functions themselves are only used as constants.
1671 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1677 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1682 /* c_openparen == c_simple */
1683 /* c_closeparen == c_simple */
1686 * AND operator. Since AND is implicit, no node is allocated.
1689 c_and(OPTION *option __unused, char ***argvp __unused)
1695 * ! expression functions --
1697 * Negation of a primary; the unary NOT operator.
1700 f_not(PLAN *plan, FTSENT *entry)
1705 for (p = plan->p_data[0];
1706 p && (state = (p->execute)(p, entry)); p = p->next);
1710 /* c_not == c_simple */
1713 * expression -o expression functions --
1715 * Alternation of primaries; the OR operator. The second expression is
1716 * not evaluated if the first expression is true.
1719 f_or(PLAN *plan, FTSENT *entry)
1724 for (p = plan->p_data[0];
1725 p && (state = (p->execute)(p, entry)); p = p->next);
1730 for (p = plan->p_data[1];
1731 p && (state = (p->execute)(p, entry)); p = p->next);
1735 /* c_or == c_simple */
1743 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1748 /* c_false == c_simple */
1756 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1761 /* c_quit == c_simple */