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";
41 static const char rcsid[] =
46 #include <sys/param.h>
47 #include <sys/ucred.h>
50 #include <sys/mount.h>
51 #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 ***);
72 time_t get_date(char *, struct timeb *);
74 #define COMPARE(a, b) do { \
75 switch (plan->flags & F_ELG_MASK) { \
93 if ((new = malloc(sizeof(PLAN))) == NULL)
95 new->execute = option->execute;
96 new->flags = option->flags;
103 * Parse a string of the form [+-]# and return the value.
106 find_parsenum(plan, option, vp, endch)
112 char *endchar, *str; /* Pointer to character ending conversion. */
114 /* Determine comparison from leading + or -. */
119 plan->flags |= F_GREATER;
123 plan->flags |= F_LESSTHAN;
126 plan->flags |= F_EQUAL;
131 * Convert the string with strtoq(). Note, if strtoq() returns zero
132 * and endchar points to the beginning of the string we know we have
135 value = strtoq(str, &endchar, 10);
136 if (value == 0 && endchar == str)
137 errx(1, "%s: %s: illegal numeric value", option, vp);
138 if (endchar[0] && (endch == NULL || endchar[0] != *endch))
139 errx(1, "%s: %s: illegal trailing character", option, vp);
147 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
150 find_parsetime(plan, option, vp)
155 long long secs, value;
156 char *str, *unit; /* Pointer to character ending conversion. */
158 /* Determine comparison from leading + or -. */
163 plan->flags |= F_GREATER;
167 plan->flags |= F_LESSTHAN;
170 plan->flags |= F_EQUAL;
174 value = strtoq(str, &unit, 10);
175 if (value == 0 && unit == str) {
176 errx(1, "%s: %s: illegal time value", option, vp);
186 case 's': /* seconds */
189 case 'm': /* minutes */
192 case 'h': /* hours */
193 secs += value * 3600;
196 secs += value * 86400;
198 case 'w': /* weeks */
199 secs += value * 604800;
202 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
206 if (*str == '\0') /* EOS */
208 value = strtoq(str, &unit, 10);
209 if (value == 0 && unit == str) {
210 errx(1, "%s: %s: illegal time value", option, vp);
214 errx(1, "%s: %s: missing trailing unit", option, vp);
218 plan->flags |= F_EXACTTIME;
224 * Check that another argument still exists, return a pointer to it,
225 * and increment the argument vector pointer.
228 nextarg(option, argvp)
234 if ((arg = **argvp) == 0)
235 errx(1, "%s: requires additional arguments", option->name);
241 * The value of n for the inode times (atime, ctime, and mtime) is a range,
242 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
243 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
244 * user wanted. Correct so that -1 is "less than 1".
246 #define TIME_CORRECT(p) \
247 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
251 * -[acm]min n functions --
253 * True if the difference between the
254 * file access time (-amin)
255 * last change of file status information (-cmin)
256 * file modification time (-mmin)
257 * and the current time is n min periods.
266 if (plan->flags & F_TIME_C) {
267 COMPARE((now - entry->fts_statp->st_ctime +
268 60 - 1) / 60, plan->t_data);
269 } else if (plan->flags & F_TIME_A) {
270 COMPARE((now - entry->fts_statp->st_atime +
271 60 - 1) / 60, plan->t_data);
273 COMPARE((now - entry->fts_statp->st_mtime +
274 60 - 1) / 60, plan->t_data);
279 c_Xmin(option, argvp)
286 nmins = nextarg(option, argvp);
287 ftsoptions &= ~FTS_NOSTAT;
289 new = palloc(option);
290 new->t_data = find_parsenum(new, option->name, nmins, NULL);
296 * -[acm]time n functions --
298 * True if the difference between the
299 * file access time (-atime)
300 * last change of file status information (-ctime)
301 * file modification time (-mtime)
302 * and the current time is n 24 hour periods.
313 if (plan->flags & F_TIME_A)
314 xtime = entry->fts_statp->st_atime;
315 else if (plan->flags & F_TIME_C)
316 xtime = entry->fts_statp->st_ctime;
318 xtime = entry->fts_statp->st_mtime;
320 if (plan->flags & F_EXACTTIME)
321 COMPARE(now - xtime, plan->t_data);
323 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data);
327 c_Xtime(option, argvp)
334 value = nextarg(option, argvp);
335 ftsoptions &= ~FTS_NOSTAT;
337 new = palloc(option);
338 new->t_data = find_parsetime(new, option->name, value);
339 if (!(new->flags & F_EXACTTIME))
345 * -maxdepth/-mindepth n functions --
347 * Does the same as -prune if the level of the current file is
348 * greater/less than the specified maximum/minimum depth.
350 * Note that -maxdepth and -mindepth are handled specially in
351 * find_execute() so their f_* functions are set to f_always_true().
354 c_mXXdepth(option, argvp)
361 dstr = nextarg(option, argvp);
363 /* all other errors handled by find_parsenum() */
364 errx(1, "%s: %s: value must be positive", option->name, dstr);
366 new = palloc(option);
367 if (option->flags & F_MAXDEPTH)
368 maxdepth = find_parsenum(new, option->name, dstr, NULL);
370 mindepth = find_parsenum(new, option->name, dstr, NULL);
375 * -delete functions --
377 * True always. Makes its best shot and continues on regardless.
380 f_delete(plan, entry)
384 /* ignore these from fts */
385 if (strcmp(entry->fts_accpath, ".") == 0 ||
386 strcmp(entry->fts_accpath, "..") == 0)
390 if (isdepth == 0 || /* depth off */
391 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */
392 !(ftsoptions & FTS_PHYSICAL) || /* physical off */
393 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
394 errx(1, "-delete: insecure options got turned on");
396 /* Potentially unsafe - do not accept relative paths whatsoever */
397 if (strchr(entry->fts_accpath, '/') != NULL)
398 errx(1, "-delete: %s: relative path potentially not safe",
401 /* Turn off user immutable bits if running as root */
402 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
403 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
405 chflags(entry->fts_accpath,
406 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
408 /* rmdir directories, unlink everything else */
409 if (S_ISDIR(entry->fts_statp->st_mode)) {
410 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
411 warn("-delete: rmdir(%s)", entry->fts_path);
413 if (unlink(entry->fts_accpath) < 0)
414 warn("-delete: unlink(%s)", entry->fts_path);
422 c_delete(option, argvp)
424 char ***argvp __unused;
427 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
428 ftsoptions |= FTS_PHYSICAL; /* disable -follow */
429 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */
430 isoutput = 1; /* possible output */
431 isdepth = 1; /* -depth implied */
433 return palloc(option);
438 * -depth functions --
440 * Always true, causes descent of the directory hierarchy to be done
441 * so that all entries in a directory are acted on before the directory
445 f_always_true(plan, entry)
447 FTSENT *entry __unused;
453 c_depth(option, argvp)
455 char ***argvp __unused;
459 return palloc(option);
463 * -empty functions --
465 * True if the file or directory is empty
472 if (S_ISREG(entry->fts_statp->st_mode) &&
473 entry->fts_statp->st_size == 0)
475 if (S_ISDIR(entry->fts_statp->st_mode)) {
481 dir = opendir(entry->fts_accpath);
483 err(1, "%s", entry->fts_accpath);
484 for (dp = readdir(dir); dp; dp = readdir(dir))
485 if (dp->d_name[0] != '.' ||
486 (dp->d_name[1] != '\0' &&
487 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
498 c_empty(option, argvp)
500 char ***argvp __unused;
502 ftsoptions &= ~FTS_NOSTAT;
504 return palloc(option);
508 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
510 * True if the executed utility returns a zero value as exit status.
511 * The end of the primary expression is delimited by a semicolon. If
512 * "{}" occurs anywhere, it gets replaced by the current pathname,
513 * or, in the case of -execdir, the current basename (filename
514 * without leading directory prefix). For -exec and -ok,
515 * the current directory for the execution of utility is the same as
516 * the current directory when the find utility was started, whereas
517 * for -execdir, it is the directory the file resides in.
519 * The primary -ok differs from -exec in that it requests affirmation
520 * of the user before executing the utility.
533 /* XXX - if file/dir ends in '/' this will not work -- can it? */
534 if ((plan->flags & F_EXECDIR) && \
535 (file = strrchr(entry->fts_path, '/')))
538 file = entry->fts_path;
540 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
541 if (plan->e_len[cnt])
542 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
543 file, plan->e_len[cnt]);
545 if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
548 /* make sure find output is interspersed correctly with subprocesses */
552 switch (pid = fork()) {
557 /* change dir back from where we started */
558 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
562 execvp(plan->e_argv[0], plan->e_argv);
563 warn("%s", plan->e_argv[0]);
566 pid = waitpid(pid, &status, 0);
567 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
571 * c_exec, c_execdir, c_ok --
572 * build three parallel arrays, one with pointers to the strings passed
573 * on the command line, one with (possibly duplicated) pointers to the
574 * argv array, and one with integer values that are lengths of the
575 * strings, but also flags meaning that the string has to be massaged.
578 c_exec(option, argvp)
582 PLAN *new; /* node returned */
584 char **argv, **ap, *p;
586 /* XXX - was in c_execdir, but seems unnecessary!?
587 ftsoptions &= ~FTS_NOSTAT;
591 /* XXX - this is a change from the previous coding */
592 new = palloc(option);
594 for (ap = argv = *argvp;; ++ap) {
597 "%s: no terminating \";\"", option->name);
602 cnt = ap - *argvp + 1;
603 if ((new->e_argv = malloc((u_int)cnt * sizeof(char *))) == NULL)
604 err(1, (char *)NULL);
605 if ((new->e_orig = malloc((u_int)cnt * sizeof(char *))) == NULL)
606 err(1, (char *)NULL);
607 if ((new->e_len = malloc((u_int)cnt * sizeof(int))) == NULL)
608 err(1, (char *)NULL);
610 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
611 new->e_orig[cnt] = *argv;
612 for (p = *argv; *p; ++p)
613 if (p[0] == '{' && p[1] == '}') {
614 if ((new->e_argv[cnt] =
615 malloc((u_int)MAXPATHLEN)) == NULL)
616 err(1, (char *)NULL);
617 new->e_len[cnt] = MAXPATHLEN;
621 new->e_argv[cnt] = *argv;
625 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
638 flags = entry->fts_statp->st_flags;
639 if (plan->flags & F_ATLEAST)
640 return (flags | plan->fl_flags) == flags &&
641 !(flags & plan->fl_notflags);
642 else if (plan->flags & F_ANY)
643 return (flags & plan->fl_flags) ||
644 (flags | plan->fl_notflags) != flags;
646 return flags == plan->fl_flags &&
647 !(plan->fl_flags & plan->fl_notflags);
651 c_flags(option, argvp)
657 u_long flags, notflags;
659 flags_str = nextarg(option, argvp);
660 ftsoptions &= ~FTS_NOSTAT;
662 new = palloc(option);
664 if (*flags_str == '-') {
665 new->flags |= F_ATLEAST;
667 } else if (*flags_str == '+') {
671 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
672 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
674 new->fl_flags = flags;
675 new->fl_notflags = notflags;
680 * -follow functions --
682 * Always true, causes symbolic links to be followed on a global
686 c_follow(option, argvp)
688 char ***argvp __unused;
690 ftsoptions &= ~FTS_PHYSICAL;
691 ftsoptions |= FTS_LOGICAL;
693 return palloc(option);
697 * -fstype functions --
699 * True if the file is of a certain type.
702 f_fstype(plan, entry)
706 static dev_t curdev; /* need a guaranteed illegal dev value */
707 static int first = 1;
709 static int val_type, val_flags;
712 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
715 /* Only check when we cross mount point. */
716 if (first || curdev != entry->fts_statp->st_dev) {
717 curdev = entry->fts_statp->st_dev;
720 * Statfs follows symlinks; find wants the link's file system,
721 * not where it points.
723 if (entry->fts_info == FTS_SL ||
724 entry->fts_info == FTS_SLNONE) {
725 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
728 p = entry->fts_accpath;
736 if (statfs(entry->fts_accpath, &sb))
737 err(1, "%s", entry->fts_accpath);
747 * Further tests may need both of these values, so
748 * always copy both of them.
750 val_flags = sb.f_flags;
751 val_type = sb.f_type;
753 switch (plan->flags & F_MTMASK) {
755 return val_flags & plan->mt_data;
757 return val_type == plan->mt_data;
763 #if !defined(__NetBSD__)
765 c_fstype(option, argvp)
773 fsname = nextarg(option, argvp);
774 ftsoptions &= ~FTS_NOSTAT;
776 new = palloc(option);
779 * Check first for a filesystem name.
781 if (getvfsbyname(fsname, &vfc) == 0) {
782 new->flags |= F_MTTYPE;
783 new->mt_data = vfc.vfc_typenum;
789 if (!strcmp(fsname, "local")) {
790 new->flags |= F_MTFLAG;
791 new->mt_data = MNT_LOCAL;
796 if (!strcmp(fsname, "rdonly")) {
797 new->flags |= F_MTFLAG;
798 new->mt_data = MNT_RDONLY;
805 * We need to make filesystem checks for filesystems
806 * that exists but aren't in the kernel work.
808 fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname);
809 new->flags |= F_MTUNKNOWN;
812 #endif /* __NetBSD__ */
815 * -group gname functions --
817 * True if the file belongs to the group gname. If gname is numeric and
818 * an equivalent of the getgrnam() function does not return a valid group
819 * name, gname is taken as a group ID.
826 return entry->fts_statp->st_gid == plan->g_data;
830 c_group(option, argvp)
839 gname = nextarg(option, argvp);
840 ftsoptions &= ~FTS_NOSTAT;
845 if (gid == 0 && gname[0] != '0')
846 errx(1, "%s: %s: no such group", option->name, gname);
850 new = palloc(option);
856 * -inum n functions --
858 * True if the file has inode # n.
865 COMPARE(entry->fts_statp->st_ino, plan->i_data);
869 c_inum(option, argvp)
876 inum_str = nextarg(option, argvp);
877 ftsoptions &= ~FTS_NOSTAT;
879 new = palloc(option);
880 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
885 * -links n functions --
887 * True if the file has n links.
894 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
898 c_links(option, argvp)
905 nlinks = nextarg(option, argvp);
906 ftsoptions &= ~FTS_NOSTAT;
908 new = palloc(option);
909 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
916 * Always true - prints the current entry to stdout in "ls" format.
923 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
930 char ***argvp __unused;
932 ftsoptions &= ~FTS_NOSTAT;
935 return palloc(option);
941 * True if the basename of the filename being examined
942 * matches pattern using Pattern Matching Notation S3.14
949 return !fnmatch(plan->c_data, entry->fts_name,
950 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
954 c_name(option, argvp)
961 pattern = nextarg(option, argvp);
962 new = palloc(option);
963 new->c_data = pattern;
968 * -newer file functions --
970 * True if the current file has been modified more recently
971 * then the modification time of the file named by the pathname
979 if (plan->flags & F_TIME_C)
980 return entry->fts_statp->st_ctime > plan->t_data;
981 else if (plan->flags & F_TIME_A)
982 return entry->fts_statp->st_atime > plan->t_data;
984 return entry->fts_statp->st_mtime > plan->t_data;
988 c_newer(option, argvp)
996 fn_or_tspec = nextarg(option, argvp);
997 ftsoptions &= ~FTS_NOSTAT;
999 new = palloc(option);
1000 /* compare against what */
1001 if (option->flags & F_TIME2_T) {
1002 new->t_data = get_date(fn_or_tspec, (struct timeb *) 0);
1003 if (new->t_data == (time_t) -1)
1004 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1006 if (stat(fn_or_tspec, &sb))
1007 err(1, "%s", fn_or_tspec);
1008 if (option->flags & F_TIME2_C)
1009 new->t_data = sb.st_ctime;
1010 else if (option->flags & F_TIME2_A)
1011 new->t_data = sb.st_atime;
1013 new->t_data = sb.st_mtime;
1019 * -nogroup functions --
1021 * True if file belongs to a user ID for which the equivalent
1022 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1025 f_nogroup(plan, entry)
1026 PLAN *plan __unused;
1029 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1033 c_nogroup(option, argvp)
1035 char ***argvp __unused;
1037 ftsoptions &= ~FTS_NOSTAT;
1039 return palloc(option);
1043 * -nouser functions --
1045 * True if file belongs to a user ID for which the equivalent
1046 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1049 f_nouser(plan, entry)
1050 PLAN *plan __unused;
1053 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1057 c_nouser(option, argvp)
1059 char ***argvp __unused;
1061 ftsoptions &= ~FTS_NOSTAT;
1063 return palloc(option);
1067 * -path functions --
1069 * True if the path of the filename being examined
1070 * matches pattern using Pattern Matching Notation S3.14
1077 return !fnmatch(plan->c_data, entry->fts_path,
1078 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1081 /* c_path is the same as c_name */
1084 * -perm functions --
1086 * The mode argument is used to represent file mode bits. If it starts
1087 * with a leading digit, it's treated as an octal mode, otherwise as a
1097 mode = entry->fts_statp->st_mode &
1098 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1099 if (plan->flags & F_ATLEAST)
1100 return (plan->m_data | mode) == mode;
1101 else if (plan->flags & F_ANY)
1102 return (mode & plan->m_data);
1104 return mode == plan->m_data;
1109 c_perm(option, argvp)
1117 perm = nextarg(option, argvp);
1118 ftsoptions &= ~FTS_NOSTAT;
1120 new = palloc(option);
1123 new->flags |= F_ATLEAST;
1125 } else if (*perm == '+') {
1126 new->flags |= F_ANY;
1130 if ((set = setmode(perm)) == NULL)
1131 errx(1, "%s: %s: illegal mode string", option->name, perm);
1133 new->m_data = getmode(set, 0);
1139 * -print functions --
1141 * Always true, causes the current pathame to be written to
1145 f_print(plan, entry)
1146 PLAN *plan __unused;
1149 (void)puts(entry->fts_path);
1154 c_print(option, argvp)
1156 char ***argvp __unused;
1160 return palloc(option);
1164 * -print0 functions --
1166 * Always true, causes the current pathame to be written to
1167 * standard output followed by a NUL character
1170 f_print0(plan, entry)
1171 PLAN *plan __unused;
1174 fputs(entry->fts_path, stdout);
1175 fputc('\0', stdout);
1179 /* c_print0 is the same as c_print */
1182 * -prune functions --
1184 * Prune a portion of the hierarchy.
1187 f_prune(plan, entry)
1188 PLAN *plan __unused;
1193 if (fts_set(tree, entry, FTS_SKIP))
1194 err(1, "%s", entry->fts_path);
1198 /* c_prune == c_simple */
1201 * -regex functions --
1203 * True if the whole path of the file matches pattern using
1204 * regular expression.
1207 f_regex(plan, entry)
1216 char errbuf[LINE_MAX];
1219 pre = plan->re_data;
1220 str = entry->fts_path;
1227 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1229 if (errcode != 0 && errcode != REG_NOMATCH) {
1230 regerror(errcode, pre, errbuf, sizeof errbuf);
1232 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1235 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1242 c_regex(option, argvp)
1250 char errbuf[LINE_MAX];
1252 if ((pre = malloc(sizeof(regex_t))) == NULL)
1255 pattern = nextarg(option, argvp);
1257 if ((errcode = regcomp(pre, pattern,
1258 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1259 regerror(errcode, pre, errbuf, sizeof errbuf);
1260 errx(1, "%s: %s: %s",
1261 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1265 new = palloc(option);
1271 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */
1274 c_simple(option, argvp)
1276 char ***argvp __unused;
1278 return palloc(option);
1282 * -size n[c] functions --
1284 * True if the file size in bytes, divided by an implementation defined
1285 * value and rounded up to the next integer, is n. If n is followed by
1286 * a c, the size is in bytes.
1288 #define FIND_SIZE 512
1289 static int divsize = 1;
1298 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1299 FIND_SIZE : entry->fts_statp->st_size;
1300 COMPARE(size, plan->o_data);
1304 c_size(option, argvp)
1312 size_str = nextarg(option, argvp);
1313 ftsoptions &= ~FTS_NOSTAT;
1315 new = palloc(option);
1317 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1324 * -type c functions --
1326 * True if the type of the file is c, where c is b, c, d, p, f or w
1327 * for block special file, character special file, directory, FIFO,
1328 * regular file or whiteout respectively.
1335 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1339 c_type(option, argvp)
1347 typestring = nextarg(option, argvp);
1348 ftsoptions &= ~FTS_NOSTAT;
1350 switch (typestring[0]) {
1375 ftsoptions |= FTS_WHITEOUT;
1377 #endif /* FTS_WHITEOUT */
1379 errx(1, "%s: %s: unknown type", option->name, typestring);
1382 new = palloc(option);
1388 * -user uname functions --
1390 * True if the file belongs to the user uname. If uname is numeric and
1391 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1392 * return a valid user name, uname is taken as a user ID.
1399 return entry->fts_statp->st_uid == plan->u_data;
1403 c_user(option, argvp)
1412 username = nextarg(option, argvp);
1413 ftsoptions &= ~FTS_NOSTAT;
1415 p = getpwnam(username);
1417 uid = atoi(username);
1418 if (uid == 0 && username[0] != '0')
1419 errx(1, "%s: %s: no such user", option->name, username);
1423 new = palloc(option);
1429 * -xdev functions --
1431 * Always true, causes find not to decend past directories that have a
1432 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1435 c_xdev(option, argvp)
1437 char ***argvp __unused;
1439 ftsoptions |= FTS_XDEV;
1441 return palloc(option);
1445 * ( expression ) functions --
1447 * True if expression is true.
1457 for (p = plan->p_data[0];
1458 p && (state = (p->execute)(p, entry)); p = p->next);
1463 * f_openparen and f_closeparen nodes are temporary place markers. They are
1464 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1465 * to a f_expr node containing the expression and the ')' node is discarded.
1466 * The functions themselves are only used as constants.
1470 f_openparen(plan, entry)
1471 PLAN *plan __unused;
1472 FTSENT *entry __unused;
1478 f_closeparen(plan, entry)
1479 PLAN *plan __unused;
1480 FTSENT *entry __unused;
1485 /* c_openparen == c_simple */
1486 /* c_closeparen == c_simple */
1489 * AND operator. Since AND is implicit, no node is allocated.
1492 c_and(option, argvp)
1493 OPTION *option __unused;
1494 char ***argvp __unused;
1500 * ! expression functions --
1502 * Negation of a primary; the unary NOT operator.
1512 for (p = plan->p_data[0];
1513 p && (state = (p->execute)(p, entry)); p = p->next);
1517 /* c_not == c_simple */
1520 * expression -o expression functions --
1522 * Alternation of primaries; the OR operator. The second expression is
1523 * not evaluated if the first expression is true.
1533 for (p = plan->p_data[0];
1534 p && (state = (p->execute)(p, entry)); p = p->next);
1539 for (p = plan->p_data[1];
1540 p && (state = (p->execute)(p, entry)); p = p->next);
1544 /* c_or == c_simple */