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.71 2011/06/13 05:22:07 avatar 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_flags;
776 static char fstype[sizeof(sb.f_fstypename)];
777 char *p, save[2] = {0,0};
779 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
782 /* Only check when we cross mount point. */
783 if (first || curdev != entry->fts_statp->st_dev) {
784 curdev = entry->fts_statp->st_dev;
787 * Statfs follows symlinks; find wants the link's filesystem,
788 * not where it points.
790 if (entry->fts_info == FTS_SL ||
791 entry->fts_info == FTS_SLNONE) {
792 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
795 p = entry->fts_accpath;
803 if (statfs(entry->fts_accpath, &sb))
804 err(1, "%s", entry->fts_accpath);
814 * Further tests may need both of these values, so
815 * always copy both of them.
817 val_flags = sb.f_flags;
818 strlcpy(fstype, sb.f_fstypename, sizeof(fstype));
820 switch (plan->flags & F_MTMASK) {
822 return val_flags & plan->mt_data;
824 return (strncmp(fstype, plan->c_data, sizeof(fstype)) == 0);
831 c_fstype(OPTION *option, char ***argvp)
836 fsname = nextarg(option, argvp);
837 ftsoptions &= ~FTS_NOSTAT;
839 new = palloc(option);
842 if (!strcmp(fsname, "local")) {
843 new->flags |= F_MTFLAG;
844 new->mt_data = MNT_LOCAL;
849 if (!strcmp(fsname, "rdonly")) {
850 new->flags |= F_MTFLAG;
851 new->mt_data = MNT_RDONLY;
857 new->flags |= F_MTTYPE;
858 new->c_data = fsname;
863 * -group gname functions --
865 * True if the file belongs to the group gname. If gname is numeric and
866 * an equivalent of the getgrnam() function does not return a valid group
867 * name, gname is taken as a group ID.
870 f_group(PLAN *plan, FTSENT *entry)
872 COMPARE(entry->fts_statp->st_gid, plan->g_data);
876 c_group(OPTION *option, char ***argvp)
883 gname = nextarg(option, argvp);
884 ftsoptions &= ~FTS_NOSTAT;
886 new = palloc(option);
890 if (gname[0] == '-' || gname[0] == '+')
893 if (gid == 0 && gname[0] != '0')
894 errx(1, "%s: %s: no such group", option->name, gname);
895 gid = find_parsenum(new, option->name, cp, NULL);
904 * -inum n functions --
906 * True if the file has inode # n.
909 f_inum(PLAN *plan, FTSENT *entry)
911 COMPARE(entry->fts_statp->st_ino, plan->i_data);
915 c_inum(OPTION *option, char ***argvp)
920 inum_str = nextarg(option, argvp);
921 ftsoptions &= ~FTS_NOSTAT;
923 new = palloc(option);
924 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
931 * True if the file has the same inode (eg hard link) FN
934 /* f_samefile is just f_inum */
936 c_samefile(OPTION *option, char ***argvp)
942 fn = nextarg(option, argvp);
943 ftsoptions &= ~FTS_NOSTAT;
945 new = palloc(option);
948 new->i_data = sb.st_ino;
953 * -links n functions --
955 * True if the file has n links.
958 f_links(PLAN *plan, FTSENT *entry)
960 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
964 c_links(OPTION *option, char ***argvp)
969 nlinks = nextarg(option, argvp);
970 ftsoptions &= ~FTS_NOSTAT;
972 new = palloc(option);
973 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
980 * Always true - prints the current entry to stdout in "ls" format.
983 f_ls(PLAN *plan __unused, FTSENT *entry)
985 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
990 c_ls(OPTION *option, char ***argvp __unused)
992 ftsoptions &= ~FTS_NOSTAT;
995 return palloc(option);
1001 * True if the basename of the filename being examined
1002 * matches pattern using Pattern Matching Notation S3.14
1005 f_name(PLAN *plan, FTSENT *entry)
1010 if (plan->flags & F_LINK) {
1012 if (readlink(entry->fts_path, fn, sizeof(fn)) == -1)
1015 name = entry->fts_name;
1016 return !fnmatch(plan->c_data, name,
1017 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1021 c_name(OPTION *option, char ***argvp)
1026 pattern = nextarg(option, argvp);
1027 new = palloc(option);
1028 new->c_data = pattern;
1033 * -newer file functions --
1035 * True if the current file has been modified more recently
1036 * then the modification time of the file named by the pathname
1040 f_newer(PLAN *plan, FTSENT *entry)
1042 if (plan->flags & F_TIME_C)
1043 return entry->fts_statp->st_ctime > plan->t_data;
1044 else if (plan->flags & F_TIME_A)
1045 return entry->fts_statp->st_atime > plan->t_data;
1047 return entry->fts_statp->st_mtime > plan->t_data;
1051 c_newer(OPTION *option, char ***argvp)
1057 fn_or_tspec = nextarg(option, argvp);
1058 ftsoptions &= ~FTS_NOSTAT;
1060 new = palloc(option);
1061 /* compare against what */
1062 if (option->flags & F_TIME2_T) {
1063 new->t_data = get_date(fn_or_tspec);
1064 if (new->t_data == (time_t) -1)
1065 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1067 if (stat(fn_or_tspec, &sb))
1068 err(1, "%s", fn_or_tspec);
1069 if (option->flags & F_TIME2_C)
1070 new->t_data = sb.st_ctime;
1071 else if (option->flags & F_TIME2_A)
1072 new->t_data = sb.st_atime;
1074 new->t_data = sb.st_mtime;
1080 * -nogroup functions --
1082 * True if file belongs to a user ID for which the equivalent
1083 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1086 f_nogroup(PLAN *plan __unused, FTSENT *entry)
1088 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1092 c_nogroup(OPTION *option, char ***argvp __unused)
1094 ftsoptions &= ~FTS_NOSTAT;
1096 return palloc(option);
1100 * -nouser functions --
1102 * True if file belongs to a user ID for which the equivalent
1103 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1106 f_nouser(PLAN *plan __unused, FTSENT *entry)
1108 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1112 c_nouser(OPTION *option, char ***argvp __unused)
1114 ftsoptions &= ~FTS_NOSTAT;
1116 return palloc(option);
1120 * -path functions --
1122 * True if the path of the filename being examined
1123 * matches pattern using Pattern Matching Notation S3.14
1126 f_path(PLAN *plan, FTSENT *entry)
1128 return !fnmatch(plan->c_data, entry->fts_path,
1129 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1132 /* c_path is the same as c_name */
1135 * -perm functions --
1137 * The mode argument is used to represent file mode bits. If it starts
1138 * with a leading digit, it's treated as an octal mode, otherwise as a
1142 f_perm(PLAN *plan, FTSENT *entry)
1146 mode = entry->fts_statp->st_mode &
1147 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1148 if (plan->flags & F_ATLEAST)
1149 return (plan->m_data | mode) == mode;
1150 else if (plan->flags & F_ANY)
1151 return (mode & plan->m_data);
1153 return mode == plan->m_data;
1158 c_perm(OPTION *option, char ***argvp)
1164 perm = nextarg(option, argvp);
1165 ftsoptions &= ~FTS_NOSTAT;
1167 new = palloc(option);
1170 new->flags |= F_ATLEAST;
1172 } else if (*perm == '+') {
1173 new->flags |= F_ANY;
1177 if ((set = setmode(perm)) == NULL)
1178 errx(1, "%s: %s: illegal mode string", option->name, perm);
1180 new->m_data = getmode(set, 0);
1186 * -print functions --
1188 * Always true, causes the current pathname to be written to
1192 f_print(PLAN *plan __unused, FTSENT *entry)
1194 puts(entry->fts_path);
1199 c_print(OPTION *option, char ***argvp __unused)
1203 return palloc(option);
1207 * -print0 functions --
1209 * Always true, causes the current pathname to be written to
1210 * standard output followed by a NUL character
1213 f_print0(PLAN *plan __unused, FTSENT *entry)
1215 fputs(entry->fts_path, stdout);
1216 fputc('\0', stdout);
1220 /* c_print0 is the same as c_print */
1223 * -prune functions --
1225 * Prune a portion of the hierarchy.
1228 f_prune(PLAN *plan __unused, FTSENT *entry)
1230 if (fts_set(tree, entry, FTS_SKIP))
1231 err(1, "%s", entry->fts_path);
1235 /* c_prune == c_simple */
1238 * -regex functions --
1240 * True if the whole path of the file matches pattern using
1241 * regular expression.
1244 f_regex(PLAN *plan, FTSENT *entry)
1251 char errbuf[LINE_MAX];
1254 pre = plan->re_data;
1255 str = entry->fts_path;
1262 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1264 if (errcode != 0 && errcode != REG_NOMATCH) {
1265 regerror(errcode, pre, errbuf, sizeof errbuf);
1267 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1270 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1277 c_regex(OPTION *option, char ***argvp)
1283 char errbuf[LINE_MAX];
1285 if ((pre = malloc(sizeof(regex_t))) == NULL)
1288 pattern = nextarg(option, argvp);
1290 if ((errcode = regcomp(pre, pattern,
1291 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1292 regerror(errcode, pre, errbuf, sizeof errbuf);
1293 errx(1, "%s: %s: %s",
1294 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1298 new = palloc(option);
1304 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1307 c_simple(OPTION *option, char ***argvp __unused)
1309 return palloc(option);
1313 * -size n[c] functions --
1315 * True if the file size in bytes, divided by an implementation defined
1316 * value and rounded up to the next integer, is n. If n is followed by
1317 * one of c k M G T P, the size is in bytes, kilobytes,
1318 * megabytes, gigabytes, terabytes or petabytes respectively.
1320 #define FIND_SIZE 512
1321 static int divsize = 1;
1324 f_size(PLAN *plan, FTSENT *entry)
1328 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1329 FIND_SIZE : entry->fts_statp->st_size;
1330 COMPARE(size, plan->o_data);
1334 c_size(OPTION *option, char ***argvp)
1341 size_str = nextarg(option, argvp);
1342 ftsoptions &= ~FTS_NOSTAT;
1344 new = palloc(option);
1346 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1347 if (endch != '\0') {
1351 case 'c': /* characters */
1354 case 'k': /* kilobytes 1<<10 */
1357 case 'M': /* megabytes 1<<20 */
1360 case 'G': /* gigabytes 1<<30 */
1361 scale = 0x40000000LL;
1363 case 'T': /* terabytes 1<<40 */
1364 scale = 0x1000000000LL;
1366 case 'P': /* petabytes 1<<50 */
1367 scale = 0x4000000000000LL;
1370 errx(1, "%s: %s: illegal trailing character",
1371 option->name, size_str);
1374 if (new->o_data > QUAD_MAX / scale)
1375 errx(1, "%s: %s: value too large",
1376 option->name, size_str);
1377 new->o_data *= scale;
1383 * -type c functions --
1385 * True if the type of the file is c, where c is b, c, d, p, f or w
1386 * for block special file, character special file, directory, FIFO,
1387 * regular file or whiteout respectively.
1390 f_type(PLAN *plan, FTSENT *entry)
1392 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1396 c_type(OPTION *option, char ***argvp)
1402 typestring = nextarg(option, argvp);
1403 ftsoptions &= ~FTS_NOSTAT;
1405 switch (typestring[0]) {
1430 ftsoptions |= FTS_WHITEOUT;
1432 #endif /* FTS_WHITEOUT */
1434 errx(1, "%s: %s: unknown type", option->name, typestring);
1437 new = palloc(option);
1443 * -user uname functions --
1445 * True if the file belongs to the user uname. If uname is numeric and
1446 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1447 * return a valid user name, uname is taken as a user ID.
1450 f_user(PLAN *plan, FTSENT *entry)
1452 COMPARE(entry->fts_statp->st_uid, plan->u_data);
1456 c_user(OPTION *option, char ***argvp)
1463 username = nextarg(option, argvp);
1464 ftsoptions &= ~FTS_NOSTAT;
1466 new = palloc(option);
1467 p = getpwnam(username);
1469 char* cp = username;
1470 if( username[0] == '-' || username[0] == '+' )
1472 uid = atoi(username);
1473 if (uid == 0 && username[0] != '0')
1474 errx(1, "%s: %s: no such user", option->name, username);
1475 uid = find_parsenum(new, option->name, cp, NULL);
1484 * -xdev functions --
1486 * Always true, causes find not to descend past directories that have a
1487 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1490 c_xdev(OPTION *option, char ***argvp __unused)
1492 ftsoptions |= FTS_XDEV;
1494 return palloc(option);
1498 * ( expression ) functions --
1500 * True if expression is true.
1503 f_expr(PLAN *plan, FTSENT *entry)
1508 for (p = plan->p_data[0];
1509 p && (state = (p->execute)(p, entry)); p = p->next);
1514 * f_openparen and f_closeparen nodes are temporary place markers. They are
1515 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1516 * to a f_expr node containing the expression and the ')' node is discarded.
1517 * The functions themselves are only used as constants.
1521 f_openparen(PLAN *plan __unused, FTSENT *entry __unused)
1527 f_closeparen(PLAN *plan __unused, FTSENT *entry __unused)
1532 /* c_openparen == c_simple */
1533 /* c_closeparen == c_simple */
1536 * AND operator. Since AND is implicit, no node is allocated.
1539 c_and(OPTION *option __unused, char ***argvp __unused)
1545 * ! expression functions --
1547 * Negation of a primary; the unary NOT operator.
1550 f_not(PLAN *plan, FTSENT *entry)
1555 for (p = plan->p_data[0];
1556 p && (state = (p->execute)(p, entry)); p = p->next);
1560 /* c_not == c_simple */
1563 * expression -o expression functions --
1565 * Alternation of primaries; the OR operator. The second expression is
1566 * not evaluated if the first expression is true.
1569 f_or(PLAN *plan, FTSENT *entry)
1574 for (p = plan->p_data[0];
1575 p && (state = (p->execute)(p, entry)); p = p->next);
1580 for (p = plan->p_data[1];
1581 p && (state = (p->execute)(p, entry)); p = p->next);
1585 /* c_or == c_simple */
1593 f_false(PLAN *plan __unused, FTSENT *entry __unused)
1598 /* c_false == c_simple */
1606 f_quit(PLAN *plan __unused, FTSENT *entry __unused)
1611 /* c_quit == c_simple */