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
32 * @(#)function.c 8.10 (Berkeley) 5/4/95
33 * $FreeBSD: src/usr.bin/find/function.c,v 1.70 2010/12/11 08:32:16 joel Exp $
36 #include <sys/param.h>
37 #include <sys/ucred.h>
40 #include <sys/mount.h>
59 static PLAN *palloc(OPTION *);
60 static long long find_parsenum(PLAN *, const char *, char *, char *);
61 static long long find_parsetime(PLAN *, const char *, char *);
62 static char *nextarg(OPTION *, char ***);
64 extern char **environ;
66 static PLAN *lastexecplus = NULL;
68 #define COMPARE(a, b) do { \
69 switch (plan->flags & F_ELG_MASK) { \
82 palloc(OPTION *option)
86 if ((new = malloc(sizeof(PLAN))) == NULL)
88 new->execute = option->execute;
89 new->flags = option->flags;
96 * Parse a string of the form [+-]# and return the value.
99 find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
102 char *endchar, *str; /* Pointer to character ending conversion. */
104 /* Determine comparison from leading + or -. */
109 plan->flags |= F_GREATER;
113 plan->flags |= F_LESSTHAN;
116 plan->flags |= F_EQUAL;
121 * Convert the string with strtoq(). Note, if strtoq() returns zero
122 * and endchar points to the beginning of the string we know we have
125 value = strtoq(str, &endchar, 10);
126 if (value == 0 && endchar == str)
127 errx(1, "%s: %s: illegal numeric value", option, vp);
128 if (endchar[0] && endch == NULL)
129 errx(1, "%s: %s: illegal trailing character", option, vp);
137 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
140 find_parsetime(PLAN *plan, const char *option, char *vp)
142 long long secs, value;
143 char *str, *unit; /* Pointer to character ending conversion. */
145 /* Determine comparison from leading + or -. */
150 plan->flags |= F_GREATER;
154 plan->flags |= F_LESSTHAN;
157 plan->flags |= F_EQUAL;
161 value = strtoq(str, &unit, 10);
162 if (value == 0 && unit == str) {
163 errx(1, "%s: %s: illegal time value", option, vp);
173 case 's': /* seconds */
176 case 'm': /* minutes */
179 case 'h': /* hours */
180 secs += value * 3600;
183 secs += value * 86400;
185 case 'w': /* weeks */
186 secs += value * 604800;
189 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
193 if (*str == '\0') /* EOS */
195 value = strtoq(str, &unit, 10);
196 if (value == 0 && unit == str) {
197 errx(1, "%s: %s: illegal time value", option, vp);
201 errx(1, "%s: %s: missing trailing unit", option, vp);
205 plan->flags |= F_EXACTTIME;
211 * Check that another argument still exists, return a pointer to it,
212 * and increment the argument vector pointer.
215 nextarg(OPTION *option, char ***argvp)
219 if ((arg = **argvp) == NULL)
220 errx(1, "%s: requires additional arguments", option->name);
226 * The value of n for the inode times (atime, ctime, and mtime) is a range,
227 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
228 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
229 * user wanted. Correct so that -1 is "less than 1".
231 #define TIME_CORRECT(p) \
232 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
236 * -[acm]min n functions --
238 * True if the difference between the
239 * file access time (-amin)
240 * last change of file status information (-cmin)
241 * file modification time (-mmin)
242 * and the current time is n min periods.
245 f_Xmin(PLAN *plan, FTSENT *entry)
247 if (plan->flags & F_TIME_C) {
248 COMPARE((now - entry->fts_statp->st_ctime +
249 60 - 1) / 60, plan->t_data);
250 } else if (plan->flags & F_TIME_A) {
251 COMPARE((now - entry->fts_statp->st_atime +
252 60 - 1) / 60, plan->t_data);
254 COMPARE((now - entry->fts_statp->st_mtime +
255 60 - 1) / 60, plan->t_data);
260 c_Xmin(OPTION *option, char ***argvp)
265 nmins = nextarg(option, argvp);
266 ftsoptions &= ~FTS_NOSTAT;
268 new = palloc(option);
269 new->t_data = find_parsenum(new, option->name, nmins, NULL);
275 * -[acm]time n functions --
277 * True if the difference between the
278 * file access time (-atime)
279 * last change of file status information (-ctime)
280 * file modification time (-mtime)
281 * and the current time is n 24 hour periods.
285 f_Xtime(PLAN *plan, FTSENT *entry)
289 if (plan->flags & F_TIME_A)
290 xtime = entry->fts_statp->st_atime;
291 else if (plan->flags & F_TIME_C)
292 xtime = entry->fts_statp->st_ctime;
294 xtime = entry->fts_statp->st_mtime;
296 if (plan->flags & F_EXACTTIME)
297 COMPARE(now - xtime, plan->t_data);
299 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data);
303 c_Xtime(OPTION *option, char ***argvp)
308 value = nextarg(option, argvp);
309 ftsoptions &= ~FTS_NOSTAT;
311 new = palloc(option);
312 new->t_data = find_parsetime(new, option->name, value);
313 if (!(new->flags & F_EXACTTIME))
319 * -maxdepth/-mindepth n functions --
321 * Does the same as -prune if the level of the current file is
322 * greater/less than the specified maximum/minimum depth.
324 * Note that -maxdepth and -mindepth are handled specially in
325 * find_execute() so their f_* functions are set to f_always_true().
328 c_mXXdepth(OPTION *option, char ***argvp)
333 dstr = nextarg(option, argvp);
335 /* all other errors handled by find_parsenum() */
336 errx(1, "%s: %s: value must be positive", option->name, dstr);
338 new = palloc(option);
339 if (option->flags & F_MAXDEPTH)
340 maxdepth = find_parsenum(new, option->name, dstr, NULL);
342 mindepth = find_parsenum(new, option->name, dstr, NULL);
347 * -delete functions --
349 * True always. Makes its best shot and continues on regardless.
352 f_delete(PLAN *plan __unused, FTSENT *entry)
354 /* ignore these from fts */
355 if (strcmp(entry->fts_accpath, ".") == 0 ||
356 strcmp(entry->fts_accpath, "..") == 0)
360 if (isdepth == 0 || /* depth off */
361 (ftsoptions & FTS_NOSTAT)) /* not stat()ing */
362 errx(1, "-delete: insecure options got turned on");
364 if (!(ftsoptions & FTS_PHYSICAL) || /* physical off */
365 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
366 errx(1, "-delete: forbidden when symlinks are followed");
368 /* Potentially unsafe - do not accept relative paths whatsoever */
369 if (strchr(entry->fts_accpath, '/') != NULL)
370 errx(1, "-delete: %s: relative path potentially not safe",
373 /* Turn off user immutable bits if running as root */
374 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
375 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
377 lchflags(entry->fts_accpath,
378 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
380 /* rmdir directories, unlink everything else */
381 if (S_ISDIR(entry->fts_statp->st_mode)) {
382 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
383 warn("-delete: rmdir(%s)", entry->fts_path);
385 if (unlink(entry->fts_accpath) < 0)
386 warn("-delete: unlink(%s)", entry->fts_path);
394 c_delete(OPTION *option, char ***argvp __unused)
397 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
398 isoutput = 1; /* possible output */
399 isdepth = 1; /* -depth implied */
401 return palloc(option);
408 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
411 f_always_true(PLAN *plan __unused, FTSENT *entry __unused)
417 * -depth functions --
419 * With argument: True if the file is at level n.
420 * Without argument: Always true, causes descent of the directory hierarchy
421 * to be done so that all entries in a directory are acted on before the
425 f_depth(PLAN *plan, FTSENT *entry)
427 if (plan->flags & F_DEPTH)
428 COMPARE(entry->fts_level, plan->d_data);
434 c_depth(OPTION *option, char ***argvp)
439 new = palloc(option);
442 if (str && !(new->flags & F_DEPTH)) {
443 /* skip leading + or - */
444 if (*str == '+' || *str == '-')
447 if (*str == '+' || *str == '-')
450 new->flags |= F_DEPTH;
453 if (new->flags & F_DEPTH) { /* -depth n */
456 ndepth = nextarg(option, argvp);
457 new->d_data = find_parsenum(new, option->name, ndepth, NULL);
466 * -empty functions --
468 * True if the file or directory is empty
471 f_empty(PLAN *plan __unused, FTSENT *entry)
473 if (S_ISREG(entry->fts_statp->st_mode) &&
474 entry->fts_statp->st_size == 0)
476 if (S_ISDIR(entry->fts_statp->st_mode)) {
482 dir = opendir(entry->fts_accpath);
485 for (dp = readdir(dir); dp; dp = readdir(dir))
486 if (dp->d_name[0] != '.' ||
487 (dp->d_name[1] != '\0' &&
488 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
499 c_empty(OPTION *option, char ***argvp __unused)
501 ftsoptions &= ~FTS_NOSTAT;
503 return palloc(option);
507 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
509 * True if the executed utility returns a zero value as exit status.
510 * The end of the primary expression is delimited by a semicolon. If
511 * "{}" occurs anywhere, it gets replaced by the current pathname,
512 * or, in the case of -execdir, the current basename (filename
513 * without leading directory prefix). For -exec and -ok,
514 * the current directory for the execution of utility is the same as
515 * the current directory when the find utility was started, whereas
516 * for -execdir, it is the directory the file resides in.
518 * The primary -ok differs from -exec in that it requests affirmation
519 * of the user before executing the utility.
522 f_exec(PLAN *plan, FTSENT *entry)
529 if (entry == NULL && plan->flags & F_EXECPLUS) {
530 if (plan->e_ppos == plan->e_pbnum)
532 plan->e_argv[plan->e_ppos] = NULL;
536 /* XXX - if file/dir ends in '/' this will not work -- can it? */
537 if ((plan->flags & F_EXECDIR) && \
538 (file = strrchr(entry->fts_path, '/')))
541 file = entry->fts_path;
543 if (plan->flags & F_EXECPLUS) {
544 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
546 plan->e_len[plan->e_ppos] = strlen(file);
547 plan->e_psize += plan->e_len[plan->e_ppos];
548 if (++plan->e_ppos < plan->e_pnummax &&
549 plan->e_psize < plan->e_psizemax)
551 plan->e_argv[plan->e_ppos] = NULL;
553 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
554 if (plan->e_len[cnt])
555 brace_subst(plan->e_orig[cnt],
556 &plan->e_argv[cnt], file,
560 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
563 /* make sure find output is interspersed correctly with subprocesses */
567 switch (pid = fork()) {
572 /* change dir back from where we started */
573 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
577 execvp(plan->e_argv[0], plan->e_argv);
578 warn("%s", plan->e_argv[0]);
581 if (plan->flags & F_EXECPLUS) {
582 while (--plan->e_ppos >= plan->e_pbnum)
583 free(plan->e_argv[plan->e_ppos]);
584 plan->e_ppos = plan->e_pbnum;
585 plan->e_psize = plan->e_pbsize;
587 pid = waitpid(pid, &status, 0);
588 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
592 * c_exec, c_execdir, c_ok --
593 * build three parallel arrays, one with pointers to the strings passed
594 * on the command line, one with (possibly duplicated) pointers to the
595 * argv array, and one with integer values that are lengths of the
596 * strings, but also flags meaning that the string has to be massaged.
599 c_exec(OPTION *option, char ***argvp)
601 PLAN *new; /* node returned */
604 char **argv, **ap, **ep, *p;
606 /* XXX - was in c_execdir, but seems unnecessary!?
607 ftsoptions &= ~FTS_NOSTAT;
611 /* XXX - this is a change from the previous coding */
612 new = palloc(option);
614 for (ap = argv = *argvp;; ++ap) {
617 "%s: no terminating \";\" or \"+\"", option->name);
620 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
621 new->flags |= F_EXECPLUS;
627 errx(1, "%s: no command specified", option->name);
629 cnt = ap - *argvp + 1;
630 if (new->flags & F_EXECPLUS) {
631 new->e_ppos = new->e_pbnum = cnt - 2;
632 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
633 warn("sysconf(_SC_ARG_MAX)");
634 argmax = _POSIX_ARG_MAX;
637 for (ep = environ; *ep != NULL; ep++)
638 argmax -= strlen(*ep) + 1 + sizeof(*ep);
639 argmax -= 1 + sizeof(*ep);
640 new->e_pnummax = argmax / 16;
641 argmax -= sizeof(char *) * new->e_pnummax;
643 errx(1, "no space for arguments");
644 new->e_psizemax = argmax;
646 cnt += new->e_pnummax + 1;
647 new->e_next = lastexecplus;
650 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
652 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
654 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
657 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
658 new->e_orig[cnt] = *argv;
659 if (new->flags & F_EXECPLUS)
660 new->e_pbsize += strlen(*argv) + 1;
661 for (p = *argv; *p; ++p)
662 if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
664 if ((new->e_argv[cnt] =
665 malloc(MAXPATHLEN)) == NULL)
667 new->e_len[cnt] = MAXPATHLEN;
671 new->e_argv[cnt] = *argv;
675 if (new->flags & F_EXECPLUS) {
676 new->e_psize = new->e_pbsize;
678 for (i = 0; i < new->e_pnummax; i++) {
679 new->e_argv[cnt] = NULL;
686 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
688 done: *argvp = argv + 1;
692 /* Finish any pending -exec ... {} + functions. */
694 finish_execplus(void)
700 (p->execute)(p, NULL);
706 f_flags(PLAN *plan, FTSENT *entry)
710 flags = entry->fts_statp->st_flags;
711 if (plan->flags & F_ATLEAST)
712 return (flags | plan->fl_flags) == flags &&
713 !(flags & plan->fl_notflags);
714 else if (plan->flags & F_ANY)
715 return (flags & plan->fl_flags) ||
716 (flags | plan->fl_notflags) != flags;
718 return flags == plan->fl_flags &&
719 !(plan->fl_flags & plan->fl_notflags);
723 c_flags(OPTION *option, char ***argvp)
727 u_long flags, notflags;
729 flags_str = nextarg(option, argvp);
730 ftsoptions &= ~FTS_NOSTAT;
732 new = palloc(option);
734 if (*flags_str == '-') {
735 new->flags |= F_ATLEAST;
737 } else if (*flags_str == '+') {
741 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
742 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
744 new->fl_flags = flags;
745 new->fl_notflags = notflags;
750 * -follow functions --
752 * Always true, causes symbolic links to be followed on a global
756 c_follow(OPTION *option, char ***argvp __unused)
758 ftsoptions &= ~FTS_PHYSICAL;
759 ftsoptions |= FTS_LOGICAL;
761 return palloc(option);
765 * -fstype functions --
767 * True if the file is of a certain type.
770 f_fstype(PLAN *plan, FTSENT *entry)
772 static dev_t curdev; /* need a guaranteed illegal dev value */
773 static int first = 1;
775 static int val_type, val_flags;
776 char *p, save[2] = {0,0};
778 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
781 /* Only check when we cross mount point. */
782 if (first || curdev != entry->fts_statp->st_dev) {
783 curdev = entry->fts_statp->st_dev;
786 * Statfs follows symlinks; find wants the link's filesystem,
787 * not where it points.
789 if (entry->fts_info == FTS_SL ||
790 entry->fts_info == FTS_SLNONE) {
791 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
794 p = entry->fts_accpath;
802 if (statfs(entry->fts_accpath, &sb)) {
803 warn("%s", entry->fts_accpath);
815 * Further tests may need both of these values, so
816 * always copy both of them.
818 val_flags = sb.f_flags;
819 val_type = sb.f_type;
821 switch (plan->flags & F_MTMASK) {
823 return val_flags & plan->mt_data;
825 return val_type == plan->mt_data;
832 c_fstype(OPTION *option, char ***argvp)
838 fsname = nextarg(option, argvp);
839 ftsoptions &= ~FTS_NOSTAT;
841 new = palloc(option);
844 * Check first for a filesystem name.
846 if (getvfsbyname(fsname, &vfc) == 0) {
847 new->flags |= F_MTTYPE;
848 new->mt_data = vfc.vfc_typenum;
854 if (!strcmp(fsname, "local")) {
855 new->flags |= F_MTFLAG;
856 new->mt_data = MNT_LOCAL;
861 if (!strcmp(fsname, "rdonly")) {
862 new->flags |= F_MTFLAG;
863 new->mt_data = MNT_RDONLY;
870 * We need to make filesystem checks for filesystems
871 * that exists but aren't in the kernel work.
873 fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname);
874 new->flags |= F_MTUNKNOWN;
879 * -group gname functions --
881 * True if the file belongs to the group gname. If gname is numeric and
882 * an equivalent of the getgrnam() function does not return a valid group
883 * name, gname is taken as a group ID.
886 f_group(PLAN *plan, FTSENT *entry)
888 COMPARE(entry->fts_statp->st_gid, plan->g_data);
892 c_group(OPTION *option, char ***argvp)
899 gname = nextarg(option, argvp);
900 ftsoptions &= ~FTS_NOSTAT;
902 new = palloc(option);
906 if (gname[0] == '-' || gname[0] == '+')
909 if (gid == 0 && gname[0] != '0')
910 errx(1, "%s: %s: no such group", option->name, gname);
911 gid = find_parsenum(new, option->name, cp, NULL);
920 * -inum n functions --
922 * True if the file has inode # n.
925 f_inum(PLAN *plan, FTSENT *entry)
927 COMPARE(entry->fts_statp->st_ino, plan->i_data);
931 c_inum(OPTION *option, char ***argvp)
936 inum_str = nextarg(option, argvp);
937 ftsoptions &= ~FTS_NOSTAT;
939 new = palloc(option);
940 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
947 * True if the file has the same inode (eg hard link) FN
950 /* f_samefile is just f_inum */
952 c_samefile(OPTION *option, char ***argvp)
958 fn = nextarg(option, argvp);
959 ftsoptions &= ~FTS_NOSTAT;
961 new = palloc(option);
964 new->i_data = sb.st_ino;
969 * -links n functions --
971 * True if the file has n links.
974 f_links(PLAN *plan, FTSENT *entry)
976 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
980 c_links(OPTION *option, char ***argvp)
985 nlinks = nextarg(option, argvp);
986 ftsoptions &= ~FTS_NOSTAT;
988 new = palloc(option);
989 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
996 * Always true - prints the current entry to stdout in "ls" format.
999 f_ls(PLAN *plan __unused, FTSENT *entry)
1001 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
1006 c_ls(OPTION *option, char ***argvp __unused)
1008 ftsoptions &= ~FTS_NOSTAT;
1011 return palloc(option);
1015 * -name functions --
1017 * True if the basename of the filename being examined
1018 * matches pattern using Pattern Matching Notation S3.14
1021 f_name(PLAN *plan, FTSENT *entry)
1026 if (plan->flags & F_LINK) {
1028 if (readlink(entry->fts_path, fn, sizeof(fn)) == -1)
1031 name = entry->fts_name;
1032 return !fnmatch(plan->c_data, name,
1033 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1037 c_name(OPTION *option, char ***argvp)
1042 pattern = nextarg(option, argvp);
1043 new = palloc(option);
1044 new->c_data = pattern;
1049 * -newer file functions --
1051 * True if the current file has been modified more recently
1052 * then the modification time of the file named by the pathname
1056 f_newer(PLAN *plan, FTSENT *entry)
1058 if (plan->flags & F_TIME_C)
1059 return entry->fts_statp->st_ctime > plan->t_data;
1060 else if (plan->flags & F_TIME_A)
1061 return entry->fts_statp->st_atime > plan->t_data;
1063 return entry->fts_statp->st_mtime > plan->t_data;
1067 c_newer(OPTION *option, char ***argvp)
1073 fn_or_tspec = nextarg(option, argvp);
1074 ftsoptions &= ~FTS_NOSTAT;
1076 new = palloc(option);
1077 /* compare against what */
1078 if (option->flags & F_TIME2_T) {
1079 new->t_data = get_date(fn_or_tspec);
1080 if (new->t_data == (time_t) -1)
1081 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1083 if (stat(fn_or_tspec, &sb))
1084 err(1, "%s", fn_or_tspec);
1085 if (option->flags & F_TIME2_C)
1086 new->t_data = sb.st_ctime;
1087 else if (option->flags & F_TIME2_A)
1088 new->t_data = sb.st_atime;
1090 new->t_data = sb.st_mtime;
1096 * -nogroup functions --
1098 * True if file belongs to a user ID for which the equivalent
1099 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1102 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1104 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1108 c_nogroup(OPTION *option, char ***argvp __unused)
1110 ftsoptions &= ~FTS_NOSTAT;
1112 return palloc(option);
1116 * -nouser functions --
1118 * True if file belongs to a user ID for which the equivalent
1119 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1122 f_nouser(PLAN *plan __unused, FTSENT *entry)
1124 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1128 c_nouser(OPTION *option, char ***argvp __unused)
1130 ftsoptions &= ~FTS_NOSTAT;
1132 return palloc(option);
1136 * -path functions --
1138 * True if the path of the filename being examined
1139 * matches pattern using Pattern Matching Notation S3.14
1142 f_path(PLAN *plan, FTSENT *entry)
1144 return !fnmatch(plan->c_data, entry->fts_path,
1145 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1148 /* c_path is the same as c_name */
1151 * -perm functions --
1153 * The mode argument is used to represent file mode bits. If it starts
1154 * with a leading digit, it's treated as an octal mode, otherwise as a
1158 f_perm(PLAN *plan, FTSENT *entry)
1162 mode = entry->fts_statp->st_mode &
1163 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1164 if (plan->flags & F_ATLEAST)
1165 return (plan->m_data | mode) == mode;
1166 else if (plan->flags & F_ANY)
1167 return (mode & plan->m_data);
1169 return mode == plan->m_data;
1174 c_perm(OPTION *option, char ***argvp)
1180 perm = nextarg(option, argvp);
1181 ftsoptions &= ~FTS_NOSTAT;
1183 new = palloc(option);
1186 new->flags |= F_ATLEAST;
1188 } else if (*perm == '+') {
1189 new->flags |= F_ANY;
1193 if ((set = setmode(perm)) == NULL)
1194 errx(1, "%s: %s: illegal mode string", option->name, perm);
1196 new->m_data = getmode(set, 0);
1202 * -print functions --
1204 * Always true, causes the current pathname to be written to
1208 f_print(PLAN *plan __unused, FTSENT *entry)
1210 puts(entry->fts_path);
1215 c_print(OPTION *option, char ***argvp __unused)
1219 return palloc(option);
1223 * -print0 functions --
1225 * Always true, causes the current pathname to be written to
1226 * standard output followed by a NUL character
1229 f_print0(PLAN *plan __unused, FTSENT *entry)
1231 fputs(entry->fts_path, stdout);
1232 fputc('\0', stdout);
1236 /* c_print0 is the same as c_print */
1239 * -prune functions --
1241 * Prune a portion of the hierarchy.
1244 f_prune(PLAN *plan __unused, FTSENT *entry)
1246 if (fts_set(tree, entry, FTS_SKIP))
1247 err(1, "%s", entry->fts_path);
1251 /* c_prune == c_simple */
1254 * -regex functions --
1256 * True if the whole path of the file matches pattern using
1257 * regular expression.
1260 f_regex(PLAN *plan, FTSENT *entry)
1267 char errbuf[LINE_MAX];
1270 pre = plan->re_data;
1271 str = entry->fts_path;
1278 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1280 if (errcode != 0 && errcode != REG_NOMATCH) {
1281 regerror(errcode, pre, errbuf, sizeof errbuf);
1283 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1286 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1293 c_regex(OPTION *option, char ***argvp)
1299 char errbuf[LINE_MAX];
1301 if ((pre = malloc(sizeof(regex_t))) == NULL)
1304 pattern = nextarg(option, argvp);
1306 if ((errcode = regcomp(pre, pattern,
1307 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1308 regerror(errcode, pre, errbuf, sizeof errbuf);
1309 errx(1, "%s: %s: %s",
1310 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1314 new = palloc(option);
1320 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1323 c_simple(OPTION *option, char ***argvp __unused)
1325 return palloc(option);
1329 * -size n[c] functions --
1331 * True if the file size in bytes, divided by an implementation defined
1332 * value and rounded up to the next integer, is n. If n is followed by
1333 * one of c k M G T P, the size is in bytes, kilobytes,
1334 * megabytes, gigabytes, terabytes or petabytes respectively.
1336 #define FIND_SIZE 512
1337 static int divsize = 1;
1340 f_size(PLAN *plan, FTSENT *entry)
1344 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1345 FIND_SIZE : entry->fts_statp->st_size;
1346 COMPARE(size, plan->o_data);
1350 c_size(OPTION *option, char ***argvp)
1357 size_str = nextarg(option, argvp);
1358 ftsoptions &= ~FTS_NOSTAT;
1360 new = palloc(option);
1362 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1363 if (endch != '\0') {
1367 case 'c': /* characters */
1370 case 'k': /* kilobytes 1<<10 */
1373 case 'M': /* megabytes 1<<20 */
1376 case 'G': /* gigabytes 1<<30 */
1377 scale = 0x40000000LL;
1379 case 'T': /* terabytes 1<<40 */
1380 scale = 0x1000000000LL;
1382 case 'P': /* petabytes 1<<50 */
1383 scale = 0x4000000000000LL;
1386 errx(1, "%s: %s: illegal trailing character",
1387 option->name, size_str);
1390 if (new->o_data > QUAD_MAX / scale)
1391 errx(1, "%s: %s: value too large",
1392 option->name, size_str);
1393 new->o_data *= scale;
1399 * -type c functions --
1401 * True if the type of the file is c, where c is b, c, d, p, f or w
1402 * for block special file, character special file, directory, FIFO,
1403 * regular file or whiteout respectively.
1406 f_type(PLAN *plan, FTSENT *entry)
1408 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1412 c_type(OPTION *option, char ***argvp)
1418 typestring = nextarg(option, argvp);
1419 ftsoptions &= ~FTS_NOSTAT;
1421 switch (typestring[0]) {
1446 ftsoptions |= FTS_WHITEOUT;
1448 #endif /* FTS_WHITEOUT */
1450 errx(1, "%s: %s: unknown type", option->name, typestring);
1453 new = palloc(option);
1459 * -user uname functions --
1461 * True if the file belongs to the user uname. If uname is numeric and
1462 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1463 * return a valid user name, uname is taken as a user ID.
1466 f_user(PLAN *plan, FTSENT *entry)
1468 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1472 c_user(OPTION *option, char ***argvp)
1479 username = nextarg(option, argvp);
1480 ftsoptions &= ~FTS_NOSTAT;
1482 new = palloc(option);
1483 p = getpwnam(username);
1485 char* cp = username;
1486 if( username[0] == '-' || username[0] == '+' )
1488 uid = atoi(username);
1489 if (uid == 0 && username[0] != '0')
1490 errx(1, "%s: %s: no such user", option->name, username);
1491 uid = find_parsenum(new, option->name, cp, NULL);
1500 * -xdev functions --
1502 * Always true, causes find not to descend past directories that have a
1503 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1506 c_xdev(OPTION *option, char ***argvp __unused)
1508 ftsoptions |= FTS_XDEV;
1510 return palloc(option);
1514 * ( expression ) functions --
1516 * True if expression is true.
1519 f_expr(PLAN *plan, FTSENT *entry)
1524 for (p = plan->p_data[0];
1525 p && (state = (p->execute)(p, entry)); p = p->next);
1530 * f_openparen and f_closeparen nodes are temporary place markers. They are
1531 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1532 * to a f_expr node containing the expression and the ')' node is discarded.
1533 * The functions themselves are only used as constants.
1537 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1543 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1548 /* c_openparen == c_simple */
1549 /* c_closeparen == c_simple */
1552 * AND operator. Since AND is implicit, no node is allocated.
1555 c_and(OPTION *option __unused, char ***argvp __unused)
1561 * ! expression functions --
1563 * Negation of a primary; the unary NOT operator.
1566 f_not(PLAN *plan, FTSENT *entry)
1571 for (p = plan->p_data[0];
1572 p && (state = (p->execute)(p, entry)); p = p->next);
1576 /* c_not == c_simple */
1579 * expression -o expression functions --
1581 * Alternation of primaries; the OR operator. The second expression is
1582 * not evaluated if the first expression is true.
1585 f_or(PLAN *plan, FTSENT *entry)
1590 for (p = plan->p_data[0];
1591 p && (state = (p->execute)(p, entry)); p = p->next);
1596 for (p = plan->p_data[1];
1597 p && (state = (p->execute)(p, entry)); p = p->next);
1601 /* c_or == c_simple */
1609 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1614 /* c_false == c_simple */
1622 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1627 /* c_quit == c_simple */