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>
67 static PLAN *palloc(OPTION *);
68 static long long find_parsenum(PLAN *, const char *, char *, char *);
69 static long long find_parsetime(PLAN *, const char *, char *);
70 static char *nextarg(OPTION *, char ***);
72 #define COMPARE(a, b) do { \
73 switch (plan->flags & F_ELG_MASK) { \
91 if ((new = malloc(sizeof(PLAN))) == NULL)
93 new->execute = option->execute;
94 new->flags = option->flags;
101 * Parse a string of the form [+-]# and return the value.
104 find_parsenum(plan, option, vp, endch)
110 char *endchar, *str; /* Pointer to character ending conversion. */
112 /* Determine comparison from leading + or -. */
117 plan->flags |= F_GREATER;
121 plan->flags |= F_LESSTHAN;
124 plan->flags |= F_EQUAL;
129 * Convert the string with strtoq(). Note, if strtoq() returns zero
130 * and endchar points to the beginning of the string we know we have
133 value = strtoq(str, &endchar, 10);
134 if (value == 0 && endchar == str)
135 errx(1, "%s: %s: illegal numeric value", option, vp);
136 if (endchar[0] && (endch == NULL || endchar[0] != *endch))
137 errx(1, "%s: %s: illegal trailing character", option, vp);
145 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
148 find_parsetime(plan, option, vp)
153 long long secs, value;
154 char *str, *unit; /* Pointer to character ending conversion. */
156 /* Determine comparison from leading + or -. */
161 plan->flags |= F_GREATER;
165 plan->flags |= F_LESSTHAN;
168 plan->flags |= F_EQUAL;
172 value = strtoq(str, &unit, 10);
173 if (value == 0 && unit == str) {
174 errx(1, "%s: %s: illegal time value", option, vp);
184 case 's': /* seconds */
187 case 'm': /* minutes */
190 case 'h': /* hours */
191 secs += value * 3600;
194 secs += value * 86400;
196 case 'w': /* weeks */
197 secs += value * 604800;
200 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
204 if (*str == '\0') /* EOS */
206 value = strtoq(str, &unit, 10);
207 if (value == 0 && unit == str) {
208 errx(1, "%s: %s: illegal time value", option, vp);
212 errx(1, "%s: %s: missing trailing unit", option, vp);
216 plan->flags |= F_EXACTTIME;
222 * Check that another argument still exists, return a pointer to it,
223 * and increment the argument vector pointer.
226 nextarg(option, argvp)
232 if ((arg = **argvp) == 0)
233 errx(1, "%s: requires additional arguments", option->name);
239 * The value of n for the inode times (atime, ctime, and mtime) is a range,
240 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
241 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
242 * user wanted. Correct so that -1 is "less than 1".
244 #define TIME_CORRECT(p) \
245 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
249 * -[acm]min n functions --
251 * True if the difference between the
252 * file access time (-amin)
253 * last change of file status information (-cmin)
254 * file modification time (-mmin)
255 * and the current time is n min periods.
264 if (plan->flags & F_TIME_C) {
265 COMPARE((now - entry->fts_statp->st_ctime +
266 60 - 1) / 60, plan->t_data);
267 } else if (plan->flags & F_TIME_A) {
268 COMPARE((now - entry->fts_statp->st_atime +
269 60 - 1) / 60, plan->t_data);
271 COMPARE((now - entry->fts_statp->st_mtime +
272 60 - 1) / 60, plan->t_data);
277 c_Xmin(option, argvp)
284 nmins = nextarg(option, argvp);
285 ftsoptions &= ~FTS_NOSTAT;
287 new = palloc(option);
288 new->t_data = find_parsenum(new, option->name, nmins, NULL);
294 * -[acm]time n functions --
296 * True if the difference between the
297 * file access time (-atime)
298 * last change of file status information (-ctime)
299 * file modification time (-mtime)
300 * and the current time is n 24 hour periods.
311 if (plan->flags & F_TIME_A)
312 xtime = entry->fts_statp->st_atime;
313 else if (plan->flags & F_TIME_C)
314 xtime = entry->fts_statp->st_ctime;
316 xtime = entry->fts_statp->st_mtime;
318 if (plan->flags & F_EXACTTIME)
319 COMPARE(now - xtime, plan->t_data);
321 COMPARE((now - xtime + 86400 - 1) / 86400, plan->t_data);
325 c_Xtime(option, argvp)
332 value = nextarg(option, argvp);
333 ftsoptions &= ~FTS_NOSTAT;
335 new = palloc(option);
336 new->t_data = find_parsetime(new, option->name, value);
337 if (!(new->flags & F_EXACTTIME))
343 * -maxdepth/-mindepth n functions --
345 * Does the same as -prune if the level of the current file is
346 * greater/less than the specified maximum/minimum depth.
348 * Note that -maxdepth and -mindepth are handled specially in
349 * find_execute() so their f_* functions are set to f_always_true().
352 c_mXXdepth(option, argvp)
359 dstr = nextarg(option, argvp);
361 /* all other errors handled by find_parsenum() */
362 errx(1, "%s: %s: value must be positive", option->name, dstr);
364 new = palloc(option);
365 if (option->flags & F_MAXDEPTH)
366 maxdepth = find_parsenum(new, option->name, dstr, NULL);
368 mindepth = find_parsenum(new, option->name, dstr, NULL);
373 * -delete functions --
375 * True always. Makes its best shot and continues on regardless.
378 f_delete(plan, entry)
382 /* ignore these from fts */
383 if (strcmp(entry->fts_accpath, ".") == 0 ||
384 strcmp(entry->fts_accpath, "..") == 0)
388 if (isdepth == 0 || /* depth off */
389 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */
390 !(ftsoptions & FTS_PHYSICAL) || /* physical off */
391 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
392 errx(1, "-delete: insecure options got turned on");
394 /* Potentially unsafe - do not accept relative paths whatsoever */
395 if (strchr(entry->fts_accpath, '/') != NULL)
396 errx(1, "-delete: %s: relative path potentially not safe",
399 /* Turn off user immutable bits if running as root */
400 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
401 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
403 chflags(entry->fts_accpath,
404 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
406 /* rmdir directories, unlink everything else */
407 if (S_ISDIR(entry->fts_statp->st_mode)) {
408 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
409 warn("-delete: rmdir(%s)", entry->fts_path);
411 if (unlink(entry->fts_accpath) < 0)
412 warn("-delete: unlink(%s)", entry->fts_path);
420 c_delete(option, argvp)
422 char ***argvp __unused;
425 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
426 ftsoptions |= FTS_PHYSICAL; /* disable -follow */
427 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */
428 isoutput = 1; /* possible output */
429 isdepth = 1; /* -depth implied */
431 return palloc(option);
436 * -depth functions --
438 * Always true, causes descent of the directory hierarchy to be done
439 * so that all entries in a directory are acted on before the directory
443 f_always_true(plan, entry)
445 FTSENT *entry __unused;
451 c_depth(option, argvp)
453 char ***argvp __unused;
457 return palloc(option);
461 * -empty functions --
463 * True if the file or directory is empty
470 if (S_ISREG(entry->fts_statp->st_mode) &&
471 entry->fts_statp->st_size == 0)
473 if (S_ISDIR(entry->fts_statp->st_mode)) {
479 dir = opendir(entry->fts_accpath);
481 err(1, "%s", entry->fts_accpath);
482 for (dp = readdir(dir); dp; dp = readdir(dir))
483 if (dp->d_name[0] != '.' ||
484 (dp->d_name[1] != '\0' &&
485 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
496 c_empty(option, argvp)
498 char ***argvp __unused;
500 ftsoptions &= ~FTS_NOSTAT;
502 return palloc(option);
506 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
508 * True if the executed utility returns a zero value as exit status.
509 * The end of the primary expression is delimited by a semicolon. If
510 * "{}" occurs anywhere, it gets replaced by the current pathname,
511 * or, in the case of -execdir, the current basename (filename
512 * without leading directory prefix). For -exec and -ok,
513 * the current directory for the execution of utility is the same as
514 * the current directory when the find utility was started, whereas
515 * for -execdir, it is the directory the file resides in.
517 * The primary -ok differs from -exec in that it requests affirmation
518 * of the user before executing the utility.
531 /* XXX - if file/dir ends in '/' this will not work -- can it? */
532 if ((plan->flags & F_EXECDIR) && \
533 (file = strrchr(entry->fts_path, '/')))
536 file = entry->fts_path;
538 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
539 if (plan->e_len[cnt])
540 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
541 file, plan->e_len[cnt]);
543 if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
546 /* make sure find output is interspersed correctly with subprocesses */
550 switch (pid = fork()) {
555 /* change dir back from where we started */
556 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
560 execvp(plan->e_argv[0], plan->e_argv);
561 warn("%s", plan->e_argv[0]);
564 pid = waitpid(pid, &status, 0);
565 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
569 * c_exec, c_execdir, c_ok --
570 * build three parallel arrays, one with pointers to the strings passed
571 * on the command line, one with (possibly duplicated) pointers to the
572 * argv array, and one with integer values that are lengths of the
573 * strings, but also flags meaning that the string has to be massaged.
576 c_exec(option, argvp)
580 PLAN *new; /* node returned */
582 char **argv, **ap, *p;
584 /* XXX - was in c_execdir, but seems unnecessary!?
585 ftsoptions &= ~FTS_NOSTAT;
589 /* XXX - this is a change from the previous coding */
590 new = palloc(option);
592 for (ap = argv = *argvp;; ++ap) {
595 "%s: no terminating \";\"", option->name);
601 errx(1, "%s: no command specified", option->name);
603 cnt = ap - *argvp + 1;
604 if ((new->e_argv = malloc((u_int)cnt * sizeof(char *))) == NULL)
605 err(1, (char *)NULL);
606 if ((new->e_orig = malloc((u_int)cnt * sizeof(char *))) == NULL)
607 err(1, (char *)NULL);
608 if ((new->e_len = malloc((u_int)cnt * sizeof(int))) == NULL)
609 err(1, (char *)NULL);
611 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
612 new->e_orig[cnt] = *argv;
613 for (p = *argv; *p; ++p)
614 if (p[0] == '{' && p[1] == '}') {
615 if ((new->e_argv[cnt] =
616 malloc((u_int)MAXPATHLEN)) == NULL)
617 err(1, (char *)NULL);
618 new->e_len[cnt] = MAXPATHLEN;
622 new->e_argv[cnt] = *argv;
626 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
639 flags = entry->fts_statp->st_flags;
640 if (plan->flags & F_ATLEAST)
641 return (flags | plan->fl_flags) == flags &&
642 !(flags & plan->fl_notflags);
643 else if (plan->flags & F_ANY)
644 return (flags & plan->fl_flags) ||
645 (flags | plan->fl_notflags) != flags;
647 return flags == plan->fl_flags &&
648 !(plan->fl_flags & plan->fl_notflags);
652 c_flags(option, argvp)
658 u_long flags, notflags;
660 flags_str = nextarg(option, argvp);
661 ftsoptions &= ~FTS_NOSTAT;
663 new = palloc(option);
665 if (*flags_str == '-') {
666 new->flags |= F_ATLEAST;
668 } else if (*flags_str == '+') {
672 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
673 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
675 new->fl_flags = flags;
676 new->fl_notflags = notflags;
681 * -follow functions --
683 * Always true, causes symbolic links to be followed on a global
687 c_follow(option, argvp)
689 char ***argvp __unused;
691 ftsoptions &= ~FTS_PHYSICAL;
692 ftsoptions |= FTS_LOGICAL;
694 return palloc(option);
698 * -fstype functions --
700 * True if the file is of a certain type.
703 f_fstype(plan, entry)
707 static dev_t curdev; /* need a guaranteed illegal dev value */
708 static int first = 1;
710 static int val_type, val_flags;
713 if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
716 /* Only check when we cross mount point. */
717 if (first || curdev != entry->fts_statp->st_dev) {
718 curdev = entry->fts_statp->st_dev;
721 * Statfs follows symlinks; find wants the link's file system,
722 * not where it points.
724 if (entry->fts_info == FTS_SL ||
725 entry->fts_info == FTS_SLNONE) {
726 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
729 p = entry->fts_accpath;
737 if (statfs(entry->fts_accpath, &sb))
738 err(1, "%s", entry->fts_accpath);
748 * Further tests may need both of these values, so
749 * always copy both of them.
751 val_flags = sb.f_flags;
752 val_type = sb.f_type;
754 switch (plan->flags & F_MTMASK) {
756 return val_flags & plan->mt_data;
758 return val_type == plan->mt_data;
764 #if !defined(__NetBSD__)
766 c_fstype(option, argvp)
774 fsname = nextarg(option, argvp);
775 ftsoptions &= ~FTS_NOSTAT;
777 new = palloc(option);
780 * Check first for a filesystem name.
782 if (getvfsbyname(fsname, &vfc) == 0) {
783 new->flags |= F_MTTYPE;
784 new->mt_data = vfc.vfc_typenum;
790 if (!strcmp(fsname, "local")) {
791 new->flags |= F_MTFLAG;
792 new->mt_data = MNT_LOCAL;
797 if (!strcmp(fsname, "rdonly")) {
798 new->flags |= F_MTFLAG;
799 new->mt_data = MNT_RDONLY;
806 * We need to make filesystem checks for filesystems
807 * that exists but aren't in the kernel work.
809 fprintf(stderr, "Warning: Unknown filesystem type %s\n", fsname);
810 new->flags |= F_MTUNKNOWN;
813 #endif /* __NetBSD__ */
816 * -group gname functions --
818 * True if the file belongs to the group gname. If gname is numeric and
819 * an equivalent of the getgrnam() function does not return a valid group
820 * name, gname is taken as a group ID.
827 return entry->fts_statp->st_gid == plan->g_data;
831 c_group(option, argvp)
840 gname = nextarg(option, argvp);
841 ftsoptions &= ~FTS_NOSTAT;
846 if (gid == 0 && gname[0] != '0')
847 errx(1, "%s: %s: no such group", option->name, gname);
851 new = palloc(option);
857 * -inum n functions --
859 * True if the file has inode # n.
866 COMPARE(entry->fts_statp->st_ino, plan->i_data);
870 c_inum(option, argvp)
877 inum_str = nextarg(option, argvp);
878 ftsoptions &= ~FTS_NOSTAT;
880 new = palloc(option);
881 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
886 * -links n functions --
888 * True if the file has n links.
895 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
899 c_links(option, argvp)
906 nlinks = nextarg(option, argvp);
907 ftsoptions &= ~FTS_NOSTAT;
909 new = palloc(option);
910 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
917 * Always true - prints the current entry to stdout in "ls" format.
924 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
931 char ***argvp __unused;
933 ftsoptions &= ~FTS_NOSTAT;
936 return palloc(option);
942 * True if the basename of the filename being examined
943 * matches pattern using Pattern Matching Notation S3.14
950 return !fnmatch(plan->c_data, entry->fts_name,
951 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
955 c_name(option, argvp)
962 pattern = nextarg(option, argvp);
963 new = palloc(option);
964 new->c_data = pattern;
969 * -newer file functions --
971 * True if the current file has been modified more recently
972 * then the modification time of the file named by the pathname
980 if (plan->flags & F_TIME_C)
981 return entry->fts_statp->st_ctime > plan->t_data;
982 else if (plan->flags & F_TIME_A)
983 return entry->fts_statp->st_atime > plan->t_data;
985 return entry->fts_statp->st_mtime > plan->t_data;
989 c_newer(option, argvp)
997 fn_or_tspec = nextarg(option, argvp);
998 ftsoptions &= ~FTS_NOSTAT;
1000 new = palloc(option);
1001 /* compare against what */
1002 if (option->flags & F_TIME2_T) {
1003 new->t_data = get_date(fn_or_tspec, (struct timeb *) 0);
1004 if (new->t_data == (time_t) -1)
1005 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1007 if (stat(fn_or_tspec, &sb))
1008 err(1, "%s", fn_or_tspec);
1009 if (option->flags & F_TIME2_C)
1010 new->t_data = sb.st_ctime;
1011 else if (option->flags & F_TIME2_A)
1012 new->t_data = sb.st_atime;
1014 new->t_data = sb.st_mtime;
1020 * -nogroup functions --
1022 * True if file belongs to a user ID for which the equivalent
1023 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1026 f_nogroup(plan, entry)
1027 PLAN *plan __unused;
1030 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1034 c_nogroup(option, argvp)
1036 char ***argvp __unused;
1038 ftsoptions &= ~FTS_NOSTAT;
1040 return palloc(option);
1044 * -nouser functions --
1046 * True if file belongs to a user ID for which the equivalent
1047 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1050 f_nouser(plan, entry)
1051 PLAN *plan __unused;
1054 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1058 c_nouser(option, argvp)
1060 char ***argvp __unused;
1062 ftsoptions &= ~FTS_NOSTAT;
1064 return palloc(option);
1068 * -path functions --
1070 * True if the path of the filename being examined
1071 * matches pattern using Pattern Matching Notation S3.14
1078 return !fnmatch(plan->c_data, entry->fts_path,
1079 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1082 /* c_path is the same as c_name */
1085 * -perm functions --
1087 * The mode argument is used to represent file mode bits. If it starts
1088 * with a leading digit, it's treated as an octal mode, otherwise as a
1098 mode = entry->fts_statp->st_mode &
1099 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1100 if (plan->flags & F_ATLEAST)
1101 return (plan->m_data | mode) == mode;
1102 else if (plan->flags & F_ANY)
1103 return (mode & plan->m_data);
1105 return mode == plan->m_data;
1110 c_perm(option, argvp)
1118 perm = nextarg(option, argvp);
1119 ftsoptions &= ~FTS_NOSTAT;
1121 new = palloc(option);
1124 new->flags |= F_ATLEAST;
1126 } else if (*perm == '+') {
1127 new->flags |= F_ANY;
1131 if ((set = setmode(perm)) == NULL)
1132 errx(1, "%s: %s: illegal mode string", option->name, perm);
1134 new->m_data = getmode(set, 0);
1140 * -print functions --
1142 * Always true, causes the current pathname to be written to
1146 f_print(plan, entry)
1147 PLAN *plan __unused;
1150 (void)puts(entry->fts_path);
1155 c_print(option, argvp)
1157 char ***argvp __unused;
1161 return palloc(option);
1165 * -print0 functions --
1167 * Always true, causes the current pathname to be written to
1168 * standard output followed by a NUL character
1171 f_print0(plan, entry)
1172 PLAN *plan __unused;
1175 fputs(entry->fts_path, stdout);
1176 fputc('\0', stdout);
1180 /* c_print0 is the same as c_print */
1183 * -prune functions --
1185 * Prune a portion of the hierarchy.
1188 f_prune(plan, entry)
1189 PLAN *plan __unused;
1194 if (fts_set(tree, entry, FTS_SKIP))
1195 err(1, "%s", entry->fts_path);
1199 /* c_prune == c_simple */
1202 * -regex functions --
1204 * True if the whole path of the file matches pattern using
1205 * regular expression.
1208 f_regex(plan, entry)
1217 char errbuf[LINE_MAX];
1220 pre = plan->re_data;
1221 str = entry->fts_path;
1228 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1230 if (errcode != 0 && errcode != REG_NOMATCH) {
1231 regerror(errcode, pre, errbuf, sizeof errbuf);
1233 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1236 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1243 c_regex(option, argvp)
1251 char errbuf[LINE_MAX];
1253 if ((pre = malloc(sizeof(regex_t))) == NULL)
1256 pattern = nextarg(option, argvp);
1258 if ((errcode = regcomp(pre, pattern,
1259 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1260 regerror(errcode, pre, errbuf, sizeof errbuf);
1261 errx(1, "%s: %s: %s",
1262 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1266 new = palloc(option);
1272 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */
1275 c_simple(option, argvp)
1277 char ***argvp __unused;
1279 return palloc(option);
1283 * -size n[c] functions --
1285 * True if the file size in bytes, divided by an implementation defined
1286 * value and rounded up to the next integer, is n. If n is followed by
1287 * a c, the size is in bytes.
1289 #define FIND_SIZE 512
1290 static int divsize = 1;
1299 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1300 FIND_SIZE : entry->fts_statp->st_size;
1301 COMPARE(size, plan->o_data);
1305 c_size(option, argvp)
1313 size_str = nextarg(option, argvp);
1314 ftsoptions &= ~FTS_NOSTAT;
1316 new = palloc(option);
1318 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1325 * -type c functions --
1327 * True if the type of the file is c, where c is b, c, d, p, f or w
1328 * for block special file, character special file, directory, FIFO,
1329 * regular file or whiteout respectively.
1336 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1340 c_type(option, argvp)
1348 typestring = nextarg(option, argvp);
1349 ftsoptions &= ~FTS_NOSTAT;
1351 switch (typestring[0]) {
1376 ftsoptions |= FTS_WHITEOUT;
1378 #endif /* FTS_WHITEOUT */
1380 errx(1, "%s: %s: unknown type", option->name, typestring);
1383 new = palloc(option);
1389 * -user uname functions --
1391 * True if the file belongs to the user uname. If uname is numeric and
1392 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1393 * return a valid user name, uname is taken as a user ID.
1400 return entry->fts_statp->st_uid == plan->u_data;
1404 c_user(option, argvp)
1413 username = nextarg(option, argvp);
1414 ftsoptions &= ~FTS_NOSTAT;
1416 p = getpwnam(username);
1418 uid = atoi(username);
1419 if (uid == 0 && username[0] != '0')
1420 errx(1, "%s: %s: no such user", option->name, username);
1424 new = palloc(option);
1430 * -xdev functions --
1432 * Always true, causes find not to descend past directories that have a
1433 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1436 c_xdev(option, argvp)
1438 char ***argvp __unused;
1440 ftsoptions |= FTS_XDEV;
1442 return palloc(option);
1446 * ( expression ) functions --
1448 * True if expression is true.
1458 for (p = plan->p_data[0];
1459 p && (state = (p->execute)(p, entry)); p = p->next);
1464 * f_openparen and f_closeparen nodes are temporary place markers. They are
1465 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1466 * to a f_expr node containing the expression and the ')' node is discarded.
1467 * The functions themselves are only used as constants.
1471 f_openparen(plan, entry)
1472 PLAN *plan __unused;
1473 FTSENT *entry __unused;
1479 f_closeparen(plan, entry)
1480 PLAN *plan __unused;
1481 FTSENT *entry __unused;
1486 /* c_openparen == c_simple */
1487 /* c_closeparen == c_simple */
1490 * AND operator. Since AND is implicit, no node is allocated.
1493 c_and(option, argvp)
1494 OPTION *option __unused;
1495 char ***argvp __unused;
1501 * ! expression functions --
1503 * Negation of a primary; the unary NOT operator.
1513 for (p = plan->p_data[0];
1514 p && (state = (p->execute)(p, entry)); p = p->next);
1518 /* c_not == c_simple */
1521 * expression -o expression functions --
1523 * Alternation of primaries; the OR operator. The second expression is
1524 * not evaluated if the first expression is true.
1534 for (p = plan->p_data[0];
1535 p && (state = (p->execute)(p, entry)); p = p->next);
1540 for (p = plan->p_data[1];
1541 p && (state = (p->execute)(p, entry)); p = p->next);
1545 /* c_or == c_simple */