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";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/ucred.h>
49 #include <sys/mount.h>
50 #include <sys/timeb.h>
68 static PLAN *palloc(OPTION *);
69 static long long find_parsenum(PLAN *, const char *, char *, char *);
70 static long long find_parsetime(PLAN *, const char *, char *);
71 static char *nextarg(OPTION *, char ***);
73 #define COMPARE(a, b) do { \
74 switch (plan->flags & F_ELG_MASK) { \
92 if ((new = malloc(sizeof(PLAN))) == NULL)
94 new->execute = option->execute;
95 new->flags = option->flags;
102 * Parse a string of the form [+-]# and return the value.
105 find_parsenum(plan, option, vp, endch)
111 char *endchar, *str; /* Pointer to character ending conversion. */
113 /* Determine comparison from leading + or -. */
118 plan->flags |= F_GREATER;
122 plan->flags |= F_LESSTHAN;
125 plan->flags |= F_EQUAL;
130 * Convert the string with strtoq(). Note, if strtoq() returns zero
131 * and endchar points to the beginning of the string we know we have
134 value = strtoq(str, &endchar, 10);
135 if (value == 0 && endchar == str)
136 errx(1, "%s: %s: illegal numeric value", option, vp);
137 if (endchar[0] && (endch == NULL || endchar[0] != *endch))
138 errx(1, "%s: %s: illegal trailing character", option, vp);
146 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
149 find_parsetime(plan, option, 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, argvp)
233 if ((arg = **argvp) == 0)
234 errx(1, "%s: requires additional arguments", option->name);
240 * The value of n for the inode times (atime, ctime, and mtime) is a range,
241 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
242 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
243 * user wanted. Correct so that -1 is "less than 1".
245 #define TIME_CORRECT(p) \
246 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
250 * -[acm]min n functions --
252 * True if the difference between the
253 * file access time (-amin)
254 * last change of file status information (-cmin)
255 * file modification time (-mmin)
256 * and the current time is n min periods.
265 if (plan->flags & F_TIME_C) {
266 COMPARE((now - entry->fts_statp->st_ctime +
267 60 - 1) / 60, plan->t_data);
268 } else if (plan->flags & F_TIME_A) {
269 COMPARE((now - entry->fts_statp->st_atime +
270 60 - 1) / 60, plan->t_data);
272 COMPARE((now - entry->fts_statp->st_mtime +
273 60 - 1) / 60, plan->t_data);
278 c_Xmin(option, argvp)
285 nmins = nextarg(option, argvp);
286 ftsoptions &= ~FTS_NOSTAT;
288 new = palloc(option);
289 new->t_data = find_parsenum(new, option->name, nmins, NULL);
295 * -[acm]time n functions --
297 * True if the difference between the
298 * file access time (-atime)
299 * last change of file status information (-ctime)
300 * file modification time (-mtime)
301 * and the current time is n 24 hour periods.
312 if (plan->flags & F_TIME_A)
313 xtime = entry->fts_statp->st_atime;
314 else if (plan->flags & F_TIME_C)
315 xtime = entry->fts_statp->st_ctime;
317 xtime = entry->fts_statp->st_mtime;
319 if (plan->flags & F_EXACTTIME)
320 COMPARE(now - xtime, plan->t_data);
322 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data);
326 c_Xtime(option, argvp)
333 value = nextarg(option, argvp);
334 ftsoptions &= ~FTS_NOSTAT;
336 new = palloc(option);
337 new->t_data = find_parsetime(new, option->name, value);
338 if (!(new->flags & F_EXACTTIME))
344 * -maxdepth/-mindepth n functions --
346 * Does the same as -prune if the level of the current file is
347 * greater/less than the specified maximum/minimum depth.
349 * Note that -maxdepth and -mindepth are handled specially in
350 * find_execute() so their f_* functions are set to f_always_true().
353 c_mXXdepth(option, argvp)
360 dstr = nextarg(option, argvp);
362 /* all other errors handled by find_parsenum() */
363 errx(1, "%s: %s: value must be positive", option->name, dstr);
365 new = palloc(option);
366 if (option->flags & F_MAXDEPTH)
367 maxdepth = find_parsenum(new, option->name, dstr, NULL);
369 mindepth = find_parsenum(new, option->name, dstr, NULL);
374 * -delete functions --
376 * True always. Makes its best shot and continues on regardless.
379 f_delete(plan, entry)
383 /* ignore these from fts */
384 if (strcmp(entry->fts_accpath, ".") == 0 ||
385 strcmp(entry->fts_accpath, "..") == 0)
389 if (isdepth == 0 || /* depth off */
390 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */
391 !(ftsoptions & FTS_PHYSICAL) || /* physical off */
392 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
393 errx(1, "-delete: insecure options got turned on");
395 /* Potentially unsafe - do not accept relative paths whatsoever */
396 if (strchr(entry->fts_accpath, '/') != NULL)
397 errx(1, "-delete: %s: relative path potentially not safe",
400 /* Turn off user immutable bits if running as root */
401 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
402 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
404 chflags(entry->fts_accpath,
405 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
407 /* rmdir directories, unlink everything else */
408 if (S_ISDIR(entry->fts_statp->st_mode)) {
409 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
410 warn("-delete: rmdir(%s)", entry->fts_path);
412 if (unlink(entry->fts_accpath) < 0)
413 warn("-delete: unlink(%s)", entry->fts_path);
421 c_delete(option, argvp)
423 char ***argvp __unused;
426 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
427 ftsoptions |= FTS_PHYSICAL; /* disable -follow */
428 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */
429 isoutput = 1; /* possible output */
430 isdepth = 1; /* -depth implied */
432 return palloc(option);
437 * -depth functions --
439 * Always true, causes descent of the directory hierarchy to be done
440 * so that all entries in a directory are acted on before the directory
444 f_always_true(plan, entry)
446 FTSENT *entry __unused;
452 c_depth(option, argvp)
454 char ***argvp __unused;
458 return palloc(option);
462 * -empty functions --
464 * True if the file or directory is empty
471 if (S_ISREG(entry->fts_statp->st_mode) &&
472 entry->fts_statp->st_size == 0)
474 if (S_ISDIR(entry->fts_statp->st_mode)) {
480 dir = opendir(entry->fts_accpath);
482 err(1, "%s", entry->fts_accpath);
483 for (dp = readdir(dir); dp; dp = readdir(dir))
484 if (dp->d_name[0] != '.' ||
485 (dp->d_name[1] != '\0' &&
486 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
497 c_empty(option, argvp)
499 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.
532 if (entry == NULL && plan->flags & F_EXECPLUS) {
533 if (plan->e_ppos == plan->e_pbnum)
535 plan->e_argv[plan->e_ppos] = NULL;
539 /* XXX - if file/dir ends in '/' this will not work -- can it? */
540 if ((plan->flags & F_EXECDIR) && \
541 (file = strrchr(entry->fts_path, '/')))
544 file = entry->fts_path;
546 if (plan->flags & F_EXECPLUS) {
547 if ((plan->e_argv[plan->e_ppos] = strdup(file)) == NULL)
549 plan->e_len[plan->e_ppos] = strlen(file);
550 plan->e_psize += plan->e_len[plan->e_ppos];
551 if (++plan->e_ppos < plan->e_pnummax &&
552 plan->e_psize < plan->e_psizemax)
554 plan->e_argv[plan->e_ppos] = NULL;
556 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
557 if (plan->e_len[cnt])
558 brace_subst(plan->e_orig[cnt],
559 &plan->e_argv[cnt], file,
563 doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
566 /* make sure find output is interspersed correctly with subprocesses */
570 switch (pid = fork()) {
575 /* change dir back from where we started */
576 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
580 execvp(plan->e_argv[0], plan->e_argv);
581 warn("%s", plan->e_argv[0]);
584 if (plan->flags & F_EXECPLUS) {
585 while (--plan->e_ppos >= plan->e_pbnum)
586 free(plan->e_argv[plan->e_ppos]);
587 plan->e_ppos = plan->e_pbnum;
588 plan->e_psize = plan->e_pbsize;
590 pid = waitpid(pid, &status, 0);
591 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
595 * c_exec, c_execdir, c_ok --
596 * build three parallel arrays, one with pointers to the strings passed
597 * on the command line, one with (possibly duplicated) pointers to the
598 * argv array, and one with integer values that are lengths of the
599 * strings, but also flags meaning that the string has to be massaged.
602 c_exec(option, argvp)
606 PLAN *new; /* node returned */
609 char **argv, **ap, *p;
611 /* XXX - was in c_execdir, but seems unnecessary!?
612 ftsoptions &= ~FTS_NOSTAT;
616 /* XXX - this is a change from the previous coding */
617 new = palloc(option);
619 for (ap = argv = *argvp;; ++ap) {
622 "%s: no terminating \";\"", option->name);
625 if (**ap == '+' && ap != argv && strcmp(*(ap - 1), "{}") == 0) {
626 new->flags |= F_EXECPLUS;
632 errx(1, "%s: no command specified", option->name);
634 cnt = ap - *argvp + 1;
635 if (new->flags & F_EXECPLUS) {
636 new->e_ppos = new->e_pbnum = cnt - 2;
637 if ((argmax = sysconf(_SC_ARG_MAX)) == -1) {
638 warn("sysconf(_SC_ARG_MAX)");
639 argmax = _POSIX_ARG_MAX;
642 * Estimate the maximum number of arguments as {ARG_MAX}/10,
643 * and the maximum number of bytes to use for arguments as
646 new->e_pnummax = argmax / 10;
647 new->e_psizemax = (argmax / 4) * 3;
649 cnt += new->e_pnummax + 1;
651 if ((new->e_argv = malloc(cnt * sizeof(char *))) == NULL)
653 if ((new->e_orig = malloc(cnt * sizeof(char *))) == NULL)
655 if ((new->e_len = malloc(cnt * sizeof(int))) == NULL)
658 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
659 new->e_orig[cnt] = *argv;
660 if (new->flags & F_EXECPLUS)
661 new->e_pbsize += strlen(*argv) + 1;
662 for (p = *argv; *p; ++p)
663 if (!(new->flags & F_EXECPLUS) && p[0] == '{' &&
665 if ((new->e_argv[cnt] =
666 malloc(MAXPATHLEN)) == NULL)
668 new->e_len[cnt] = MAXPATHLEN;
672 new->e_argv[cnt] = *argv;
676 if (new->flags & F_EXECPLUS) {
677 new->e_psize = new->e_pbsize;
679 for (i = 0; i < new->e_pnummax; i++) {
680 new->e_argv[cnt] = NULL;
687 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
689 done: *argvp = argv + 1;
700 flags = entry->fts_statp->st_flags;
701 if (plan->flags & F_ATLEAST)
702 return (flags | plan->fl_flags) == flags &&
703 !(flags & plan->fl_notflags);
704 else if (plan->flags & F_ANY)
705 return (flags & plan->fl_flags) ||
706 (flags | plan->fl_notflags) != flags;
708 return flags == plan->fl_flags &&
709 !(plan->fl_flags & plan->fl_notflags);
713 c_flags(option, argvp)
719 u_long flags, notflags;
721 flags_str = nextarg(option, argvp);
722 ftsoptions &= ~FTS_NOSTAT;
724 new = palloc(option);
726 if (*flags_str == '-') {
727 new->flags |= F_ATLEAST;
729 } else if (*flags_str == '+') {
733 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
734 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
736 new->fl_flags = flags;
737 new->fl_notflags = notflags;
742 * -follow functions --
744 * Always true, causes symbolic links to be followed on a global
748 c_follow(option, argvp)
750 char ***argvp __unused;
752 ftsoptions &= ~FTS_PHYSICAL;
753 ftsoptions |= FTS_LOGICAL;
755 return palloc(option);
759 * -fstype functions --
761 * True if the file is of a certain type.
764 f_fstype(plan, entry)
768 static dev_t curdev; /* need a guaranteed illegal dev value */
769 static int first = 1;
771 static int val_type, val_flags;
774 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
777 /* Only check when we cross mount point. */
778 if (first || curdev != entry->fts_statp->st_dev) {
779 curdev = entry->fts_statp->st_dev;
782 * Statfs follows symlinks; find wants the link's filesystem,
783 * not where it points.
785 if (entry->fts_info == FTS_SL ||
786 entry->fts_info == FTS_SLNONE) {
787 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
790 p = entry->fts_accpath;
798 if (statfs(entry->fts_accpath, &sb))
799 err(1, "%s", entry->fts_accpath);
809 * Further tests may need both of these values, so
810 * always copy both of them.
812 val_flags = sb.f_flags;
813 val_type = sb.f_type;
815 switch (plan->flags & F_MTMASK) {
817 return val_flags & plan->mt_data;
819 return val_type == plan->mt_data;
825 #if !defined(__NetBSD__)
827 c_fstype(option, argvp)
835 fsname = nextarg(option, argvp);
836 ftsoptions &= ~FTS_NOSTAT;
838 new = palloc(option);
841 * Check first for a filesystem name.
843 if (getvfsbyname(fsname, &vfc) == 0) {
844 new->flags |= F_MTTYPE;
845 new->mt_data = vfc.vfc_typenum;
851 if (!strcmp(fsname, "local")) {
852 new->flags |= F_MTFLAG;
853 new->mt_data = MNT_LOCAL;
858 if (!strcmp(fsname, "rdonly")) {
859 new->flags |= F_MTFLAG;
860 new->mt_data = MNT_RDONLY;
867 * We need to make filesystem checks for filesystems
868 * that exists but aren't in the kernel work.
870 fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname);
871 new->flags |= F_MTUNKNOWN;
874 #endif /* __NetBSD__ */
877 * -group gname functions --
879 * True if the file belongs to the group gname. If gname is numeric and
880 * an equivalent of the getgrnam() function does not return a valid group
881 * name, gname is taken as a group ID.
888 return entry->fts_statp->st_gid == plan->g_data;
892 c_group(option, argvp)
901 gname = nextarg(option, argvp);
902 ftsoptions &= ~FTS_NOSTAT;
907 if (gid == 0 && gname[0] != '0')
908 errx(1, "%s: %s: no such group", option->name, gname);
912 new = palloc(option);
918 * -inum n functions --
920 * True if the file has inode # n.
927 COMPARE(entry->fts_statp->st_ino, plan->i_data);
931 c_inum(option, argvp)
938 inum_str = nextarg(option, argvp);
939 ftsoptions &= ~FTS_NOSTAT;
941 new = palloc(option);
942 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
947 * -links n functions --
949 * True if the file has n links.
956 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
960 c_links(option, argvp)
967 nlinks = nextarg(option, argvp);
968 ftsoptions &= ~FTS_NOSTAT;
970 new = palloc(option);
971 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
978 * Always true - prints the current entry to stdout in "ls" format.
985 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
992 char ***argvp __unused;
994 ftsoptions &= ~FTS_NOSTAT;
997 return palloc(option);
1001 * -name functions --
1003 * True if the basename of the filename being examined
1004 * matches pattern using Pattern Matching Notation S3.14
1011 return !fnmatch(plan->c_data, entry->fts_name,
1012 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1016 c_name(option, argvp)
1023 pattern = nextarg(option, argvp);
1024 new = palloc(option);
1025 new->c_data = pattern;
1030 * -newer file functions --
1032 * True if the current file has been modified more recently
1033 * then the modification time of the file named by the pathname
1037 f_newer(plan, entry)
1041 if (plan->flags & F_TIME_C)
1042 return entry->fts_statp->st_ctime > plan->t_data;
1043 else if (plan->flags & F_TIME_A)
1044 return entry->fts_statp->st_atime > plan->t_data;
1046 return entry->fts_statp->st_mtime > plan->t_data;
1050 c_newer(option, argvp)
1058 fn_or_tspec = nextarg(option, argvp);
1059 ftsoptions &= ~FTS_NOSTAT;
1061 new = palloc(option);
1062 /* compare against what */
1063 if (option->flags & F_TIME2_T) {
1064 new->t_data = get_date(fn_or_tspec, (struct timeb *) 0);
1065 if (new->t_data == (time_t) -1)
1066 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1068 if (stat(fn_or_tspec, &sb))
1069 err(1, "%s", fn_or_tspec);
1070 if (option->flags & F_TIME2_C)
1071 new->t_data = sb.st_ctime;
1072 else if (option->flags & F_TIME2_A)
1073 new->t_data = sb.st_atime;
1075 new->t_data = sb.st_mtime;
1081 * -nogroup functions --
1083 * True if file belongs to a user ID for which the equivalent
1084 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1087 f_nogroup(plan, entry)
1088 PLAN *plan __unused;
1091 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1095 c_nogroup(option, argvp)
1097 char ***argvp __unused;
1099 ftsoptions &= ~FTS_NOSTAT;
1101 return palloc(option);
1105 * -nouser functions --
1107 * True if file belongs to a user ID for which the equivalent
1108 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1111 f_nouser(plan, entry)
1112 PLAN *plan __unused;
1115 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1119 c_nouser(option, argvp)
1121 char ***argvp __unused;
1123 ftsoptions &= ~FTS_NOSTAT;
1125 return palloc(option);
1129 * -path functions --
1131 * True if the path of the filename being examined
1132 * matches pattern using Pattern Matching Notation S3.14
1139 return !fnmatch(plan->c_data, entry->fts_path,
1140 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1143 /* c_path is the same as c_name */
1146 * -perm functions --
1148 * The mode argument is used to represent file mode bits. If it starts
1149 * with a leading digit, it's treated as an octal mode, otherwise as a
1159 mode = entry->fts_statp->st_mode &
1160 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1161 if (plan->flags & F_ATLEAST)
1162 return (plan->m_data | mode) == mode;
1163 else if (plan->flags & F_ANY)
1164 return (mode & plan->m_data);
1166 return mode == plan->m_data;
1171 c_perm(option, argvp)
1179 perm = nextarg(option, argvp);
1180 ftsoptions &= ~FTS_NOSTAT;
1182 new = palloc(option);
1185 new->flags |= F_ATLEAST;
1187 } else if (*perm == '+') {
1188 new->flags |= F_ANY;
1192 if ((set = setmode(perm)) == NULL)
1193 errx(1, "%s: %s: illegal mode string", option->name, perm);
1195 new->m_data = getmode(set, 0);
1201 * -print functions --
1203 * Always true, causes the current pathname to be written to
1207 f_print(plan, entry)
1208 PLAN *plan __unused;
1211 (void)puts(entry->fts_path);
1216 c_print(option, argvp)
1218 char ***argvp __unused;
1222 return palloc(option);
1226 * -print0 functions --
1228 * Always true, causes the current pathname to be written to
1229 * standard output followed by a NUL character
1232 f_print0(plan, entry)
1233 PLAN *plan __unused;
1236 fputs(entry->fts_path, stdout);
1237 fputc('\0', stdout);
1241 /* c_print0 is the same as c_print */
1244 * -prune functions --
1246 * Prune a portion of the hierarchy.
1249 f_prune(plan, entry)
1250 PLAN *plan __unused;
1255 if (fts_set(tree, entry, FTS_SKIP))
1256 err(1, "%s", entry->fts_path);
1260 /* c_prune == c_simple */
1263 * -regex functions --
1265 * True if the whole path of the file matches pattern using
1266 * regular expression.
1269 f_regex(plan, entry)
1278 char errbuf[LINE_MAX];
1281 pre = plan->re_data;
1282 str = entry->fts_path;
1289 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1291 if (errcode != 0 && errcode != REG_NOMATCH) {
1292 regerror(errcode, pre, errbuf, sizeof errbuf);
1294 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1297 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1304 c_regex(option, argvp)
1312 char errbuf[LINE_MAX];
1314 if ((pre = malloc(sizeof(regex_t))) == NULL)
1317 pattern = nextarg(option, argvp);
1319 if ((errcode = regcomp(pre, pattern,
1320 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1321 regerror(errcode, pre, errbuf, sizeof errbuf);
1322 errx(1, "%s: %s: %s",
1323 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1327 new = palloc(option);
1333 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */
1336 c_simple(option, argvp)
1338 char ***argvp __unused;
1340 return palloc(option);
1344 * -size n[c] functions --
1346 * True if the file size in bytes, divided by an implementation defined
1347 * value and rounded up to the next integer, is n. If n is followed by
1348 * a c, the size is in bytes.
1350 #define FIND_SIZE 512
1351 static int divsize = 1;
1360 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1361 FIND_SIZE : entry->fts_statp->st_size;
1362 COMPARE(size, plan->o_data);
1366 c_size(option, argvp)
1374 size_str = nextarg(option, argvp);
1375 ftsoptions &= ~FTS_NOSTAT;
1377 new = palloc(option);
1379 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1386 * -type c functions --
1388 * True if the type of the file is c, where c is b, c, d, p, f or w
1389 * for block special file, character special file, directory, FIFO,
1390 * regular file or whiteout respectively.
1397 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1401 c_type(option, argvp)
1409 typestring = nextarg(option, argvp);
1410 ftsoptions &= ~FTS_NOSTAT;
1412 switch (typestring[0]) {
1437 ftsoptions |= FTS_WHITEOUT;
1439 #endif /* FTS_WHITEOUT */
1441 errx(1, "%s: %s: unknown type", option->name, typestring);
1444 new = palloc(option);
1450 * -user uname functions --
1452 * True if the file belongs to the user uname. If uname is numeric and
1453 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1454 * return a valid user name, uname is taken as a user ID.
1461 return entry->fts_statp->st_uid == plan->u_data;
1465 c_user(option, argvp)
1474 username = nextarg(option, argvp);
1475 ftsoptions &= ~FTS_NOSTAT;
1477 p = getpwnam(username);
1479 uid = atoi(username);
1480 if (uid == 0 && username[0] != '0')
1481 errx(1, "%s: %s: no such user", option->name, username);
1485 new = palloc(option);
1491 * -xdev functions --
1493 * Always true, causes find not to descend past directories that have a
1494 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1497 c_xdev(option, argvp)
1499 char ***argvp __unused;
1501 ftsoptions |= FTS_XDEV;
1503 return palloc(option);
1507 * ( expression ) functions --
1509 * True if expression is true.
1519 for (p = plan->p_data[0];
1520 p && (state = (p->execute)(p, entry)); p = p->next);
1525 * f_openparen and f_closeparen nodes are temporary place markers. They are
1526 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1527 * to a f_expr node containing the expression and the ')' node is discarded.
1528 * The functions themselves are only used as constants.
1532 f_openparen(plan, entry)
1533 PLAN *plan __unused;
1534 FTSENT *entry __unused;
1540 f_closeparen(plan, entry)
1541 PLAN *plan __unused;
1542 FTSENT *entry __unused;
1547 /* c_openparen == c_simple */
1548 /* c_closeparen == c_simple */
1551 * AND operator. Since AND is implicit, no node is allocated.
1554 c_and(option, argvp)
1555 OPTION *option __unused;
1556 char ***argvp __unused;
1562 * ! expression functions --
1564 * Negation of a primary; the unary NOT operator.
1574 for (p = plan->p_data[0];
1575 p && (state = (p->execute)(p, entry)); p = p->next);
1579 /* c_not == c_simple */
1582 * expression -o expression functions --
1584 * Alternation of primaries; the OR operator. The second expression is
1585 * not evaluated if the first expression is true.
1595 for (p = plan->p_data[0];
1596 p && (state = (p->execute)(p, entry)); p = p->next);
1601 for (p = plan->p_data[1];
1602 p && (state = (p->execute)(p, entry)); p = p->next);
1606 /* c_or == c_simple */