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
36 * @(#)function.c 8.10 (Berkeley) 5/4/95
37 * $FreeBSD: src/usr.bin/find/function.c,v 1.22.2.11 2002/11/15 11:38:15 sheldonh Exp $
38 * $DragonFly: src/usr.bin/find/function.c,v 1.2 2003/06/17 04:29:26 dillon Exp $
41 #include <sys/param.h>
42 #include <sys/ucred.h>
45 #include <sys/mount.h>
46 #include <sys/timeb.h>
63 time_t get_date __P((char *date, struct timeb *now));
65 #define COMPARE(a, b) do { \
66 switch (plan->flags & F_ELG_MASK) { \
84 if ((new = malloc(sizeof(PLAN))) == NULL)
86 new->execute = option->execute;
87 new->flags = option->flags;
94 * Parse a string of the form [+-]# and return the value.
97 find_parsenum(plan, option, vp, endch)
99 char *option, *vp, *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 || endchar[0] != *endch))
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, option, vp)
144 long long secs, value;
145 char *str, *unit; /* Pointer to character ending conversion. */
147 /* Determine comparison from leading + or -. */
152 plan->flags |= F_GREATER;
156 plan->flags |= F_LESSTHAN;
159 plan->flags |= F_EQUAL;
163 value = strtoq(str, &unit, 10);
164 if (value == 0 && unit == str) {
165 errx(1, "%s: %s: illegal time value", option, vp);
175 case 's': /* seconds */
178 case 'm': /* minutes */
181 case 'h': /* hours */
182 secs += value * 3600;
185 secs += value * 86400;
187 case 'w': /* weeks */
188 secs += value * 604800;
191 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
195 if (*str == '\0') /* EOS */
197 value = strtoq(str, &unit, 10);
198 if (value == 0 && unit == str) {
199 errx(1, "%s: %s: illegal time value", option, vp);
203 errx(1, "%s: %s: missing trailing unit", option, vp);
207 plan->flags |= F_EXACTTIME;
213 * Check that another argument still exists, return a pointer to it,
214 * and increment the argument vector pointer.
217 nextarg(option, argvp)
223 if ((arg = **argvp) == 0)
224 errx(1, "%s: requires additional arguments", option->name);
230 * The value of n for the inode times (atime, ctime, and mtime) is a range,
231 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
232 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
233 * user wanted. Correct so that -1 is "less than 1".
235 #define TIME_CORRECT(p) \
236 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
240 * -[acm]min n functions --
242 * True if the difference between the
243 * file access time (-amin)
244 * last change of file status information (-cmin)
245 * file modification time (-mmin)
246 * and the current time is n min periods.
255 if (plan->flags & F_TIME_C) {
256 COMPARE((now - entry->fts_statp->st_ctime +
257 60 - 1) / 60, plan->t_data);
258 } else if (plan->flags & F_TIME_A) {
259 COMPARE((now - entry->fts_statp->st_atime +
260 60 - 1) / 60, plan->t_data);
262 COMPARE((now - entry->fts_statp->st_mtime +
263 60 - 1) / 60, plan->t_data);
268 c_Xmin(option, argvp)
275 nmins = nextarg(option, argvp);
276 ftsoptions &= ~FTS_NOSTAT;
278 new = palloc(option);
279 new->t_data = find_parsenum(new, option->name, nmins, NULL);
285 * -[acm]time n functions --
287 * True if the difference between the
288 * file access time (-atime)
289 * last change of file status information (-ctime)
290 * file modification time (-mtime)
291 * and the current time is n 24 hour periods.
302 exact_time = plan->flags & F_EXACTTIME;
304 if (plan->flags & F_TIME_C) {
306 COMPARE(now - entry->fts_statp->st_ctime,
309 COMPARE((now - entry->fts_statp->st_ctime +
310 86400 - 1) / 86400, plan->t_data);
311 } else if (plan->flags & F_TIME_A) {
313 COMPARE(now - entry->fts_statp->st_atime,
316 COMPARE((now - entry->fts_statp->st_atime +
317 86400 - 1) / 86400, plan->t_data);
320 COMPARE(now - entry->fts_statp->st_mtime,
323 COMPARE((now - entry->fts_statp->st_mtime +
324 86400 - 1) / 86400, plan->t_data);
329 c_Xtime(option, argvp)
336 value = nextarg(option, argvp);
337 ftsoptions &= ~FTS_NOSTAT;
339 new = palloc(option);
340 new->t_data = find_parsetime(new, option->name, value);
341 if (!(new->flags & F_EXACTTIME))
347 * -maxdepth/-mindepth n functions --
349 * Does the same as -prune if the level of the current file is
350 * greater/less than the specified maximum/minimum depth.
352 * Note that -maxdepth and -mindepth are handled specially in
353 * find_execute() so their f_* functions are set to f_always_true().
356 c_mXXdepth(option, argvp)
363 dstr = nextarg(option, argvp);
365 /* all other errors handled by find_parsenum() */
366 errx(1, "%s: %s: value must be positive", option->name, dstr);
368 new = palloc(option);
369 if (option->flags & F_MAXDEPTH)
370 maxdepth = find_parsenum(new, option->name, dstr, NULL);
372 mindepth = find_parsenum(new, option->name, dstr, NULL);
377 * -delete functions --
379 * True always. Makes its best shot and continues on regardless.
382 f_delete(plan, entry)
386 /* ignore these from fts */
387 if (strcmp(entry->fts_accpath, ".") == 0 ||
388 strcmp(entry->fts_accpath, "..") == 0)
392 if (isdepth == 0 || /* depth off */
393 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */
394 !(ftsoptions & FTS_PHYSICAL) || /* physical off */
395 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
396 errx(1, "-delete: insecure options got turned on");
398 /* Potentially unsafe - do not accept relative paths whatsoever */
399 if (strchr(entry->fts_accpath, '/') != NULL)
400 errx(1, "-delete: %s: relative path potentially not safe",
403 /* Turn off user immutable bits if running as root */
404 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
405 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
407 chflags(entry->fts_accpath,
408 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
410 /* rmdir directories, unlink everything else */
411 if (S_ISDIR(entry->fts_statp->st_mode)) {
412 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
413 warn("-delete: rmdir(%s)", entry->fts_path);
415 if (unlink(entry->fts_accpath) < 0)
416 warn("-delete: unlink(%s)", entry->fts_path);
424 c_delete(option, argvp)
429 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
430 ftsoptions |= FTS_PHYSICAL; /* disable -follow */
431 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */
432 isoutput = 1; /* possible output */
433 isdepth = 1; /* -depth implied */
435 return palloc(option);
440 * -depth functions --
442 * Always true, causes descent of the directory hierarchy to be done
443 * so that all entries in a directory are acted on before the directory
447 f_always_true(plan, entry)
455 c_depth(option, argvp)
461 return palloc(option);
465 * -empty functions --
467 * True if the file or directory is empty
474 if (S_ISREG(entry->fts_statp->st_mode) &&
475 entry->fts_statp->st_size == 0)
477 if (S_ISDIR(entry->fts_statp->st_mode)) {
483 dir = opendir(entry->fts_accpath);
485 err(1, "%s", entry->fts_accpath);
486 for (dp = readdir(dir); dp; dp = readdir(dir))
487 if (dp->d_name[0] != '.' ||
488 (dp->d_name[1] != '\0' &&
489 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
500 c_empty(option, argvp)
504 ftsoptions &= ~FTS_NOSTAT;
506 return palloc(option);
510 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
512 * True if the executed utility returns a zero value as exit status.
513 * The end of the primary expression is delimited by a semicolon. If
514 * "{}" occurs anywhere, it gets replaced by the current pathname,
515 * or, in the case of -execdir, the current basename (filename
516 * without leading directory prefix). For -exec and -ok,
517 * the current directory for the execution of utility is the same as
518 * the current directory when the find utility was started, whereas
519 * for -execdir, it is the directory the file resides in.
521 * The primary -ok differs from -exec in that it requests affirmation
522 * of the user before executing the utility.
535 /* XXX - if file/dir ends in '/' this will not work -- can it? */
536 if ((plan->flags & F_EXECDIR) && \
537 (file = strrchr(entry->fts_path, '/')))
540 file = entry->fts_path;
542 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
543 if (plan->e_len[cnt])
544 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
545 file, plan->e_len[cnt]);
547 if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
550 /* make sure find output is interspersed correctly with subprocesses */
554 switch (pid = fork()) {
559 /* change dir back from where we started */
560 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
564 execvp(plan->e_argv[0], plan->e_argv);
565 warn("%s", plan->e_argv[0]);
568 pid = waitpid(pid, &status, 0);
569 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
573 * c_exec, c_execdir, c_ok --
574 * build three parallel arrays, one with pointers to the strings passed
575 * on the command line, one with (possibly duplicated) pointers to the
576 * argv array, and one with integer values that are lengths of the
577 * strings, but also flags meaning that the string has to be massaged.
580 c_exec(option, argvp)
584 PLAN *new; /* node returned */
586 register char **argv, **ap, *p;
588 /* XXX - was in c_execdir, but seems unnecessary!?
589 ftsoptions &= ~FTS_NOSTAT;
593 /* XXX - this is a change from the previous coding */
594 new = palloc(option);
596 for (ap = argv = *argvp;; ++ap) {
599 "%s: no terminating \";\"", option->name);
605 errx(1, "%s: no command specified", option->name);
607 cnt = ap - *argvp + 1;
608 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
609 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
610 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
612 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
613 new->e_orig[cnt] = *argv;
614 for (p = *argv; *p; ++p)
615 if (p[0] == '{' && p[1] == '}') {
616 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
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)
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 /* Only check when we cross mount point. */
713 if (first || curdev != entry->fts_statp->st_dev) {
714 curdev = entry->fts_statp->st_dev;
717 * Statfs follows symlinks; find wants the link's file system,
718 * not where it points.
720 if (entry->fts_info == FTS_SL ||
721 entry->fts_info == FTS_SLNONE) {
722 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
725 p = entry->fts_accpath;
733 if (statfs(entry->fts_accpath, &sb))
734 err(1, "%s", entry->fts_accpath);
744 * Further tests may need both of these values, so
745 * always copy both of them.
747 val_flags = sb.f_flags;
748 val_type = sb.f_type;
750 switch (plan->flags & F_MTMASK) {
752 return (val_flags & plan->mt_data) != 0;
754 return (val_type == plan->mt_data);
760 #if !defined(__NetBSD__)
762 c_fstype(option, argvp)
770 fsname = nextarg(option, argvp);
771 ftsoptions &= ~FTS_NOSTAT;
773 new = palloc(option);
776 * Check first for a filesystem name.
778 if (getvfsbyname(fsname, &vfc) == 0) {
779 new->flags |= F_MTTYPE;
780 new->mt_data = vfc.vfc_typenum;
786 if (!strcmp(fsname, "local")) {
787 new->flags |= F_MTFLAG;
788 new->mt_data = MNT_LOCAL;
793 if (!strcmp(fsname, "rdonly")) {
794 new->flags |= F_MTFLAG;
795 new->mt_data = MNT_RDONLY;
801 errx(1, "%s: unknown file type", fsname);
804 #endif /* __NetBSD__ */
807 * -group gname functions --
809 * True if the file belongs to the group gname. If gname is numeric and
810 * an equivalent of the getgrnam() function does not return a valid group
811 * name, gname is taken as a group ID.
818 return entry->fts_statp->st_gid == plan->g_data;
822 c_group(option, argvp)
831 gname = nextarg(option, argvp);
832 ftsoptions &= ~FTS_NOSTAT;
837 if (gid == 0 && gname[0] != '0')
838 errx(1, "%s: %s: no such group", option->name, gname);
842 new = palloc(option);
848 * -inum n functions --
850 * True if the file has inode # n.
857 COMPARE(entry->fts_statp->st_ino, plan->i_data);
861 c_inum(option, argvp)
868 inum_str = nextarg(option, argvp);
869 ftsoptions &= ~FTS_NOSTAT;
871 new = palloc(option);
872 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
877 * -links n functions --
879 * True if the file has n links.
886 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
890 c_links(option, argvp)
897 nlinks = nextarg(option, argvp);
898 ftsoptions &= ~FTS_NOSTAT;
900 new = palloc(option);
901 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
908 * Always true - prints the current entry to stdout in "ls" format.
915 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
924 ftsoptions &= ~FTS_NOSTAT;
927 return palloc(option);
933 * True if the basename of the filename being examined
934 * matches pattern using Pattern Matching Notation S3.14
941 return !fnmatch(plan->c_data, entry->fts_name,
942 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
946 c_name(option, argvp)
953 pattern = nextarg(option, argvp);
954 new = palloc(option);
955 new->c_data = pattern;
960 * -newer file functions --
962 * True if the current file has been modified more recently
963 * then the modification time of the file named by the pathname
971 if (plan->flags & F_TIME_C)
972 return entry->fts_statp->st_ctime > plan->t_data;
973 else if (plan->flags & F_TIME_A)
974 return entry->fts_statp->st_atime > plan->t_data;
976 return entry->fts_statp->st_mtime > plan->t_data;
980 c_newer(option, argvp)
988 fn_or_tspec = nextarg(option, argvp);
989 ftsoptions &= ~FTS_NOSTAT;
991 new = palloc(option);
992 /* compare against what */
993 if (option->flags & F_TIME2_T) {
994 new->t_data = get_date(fn_or_tspec, (struct timeb *) 0);
995 if (new->t_data == (time_t) -1)
996 errx(1, "Can't parse date/time: %s", fn_or_tspec);
998 if (stat(fn_or_tspec, &sb))
999 err(1, "%s", fn_or_tspec);
1000 if (option->flags & F_TIME2_C)
1001 new->t_data = sb.st_ctime;
1002 else if (option->flags & F_TIME2_A)
1003 new->t_data = sb.st_atime;
1005 new->t_data = sb.st_mtime;
1011 * -nogroup functions --
1013 * True if file belongs to a user ID for which the equivalent
1014 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1017 f_nogroup(plan, entry)
1021 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1025 c_nogroup(option, argvp)
1029 ftsoptions &= ~FTS_NOSTAT;
1031 return palloc(option);
1035 * -nouser functions --
1037 * True if file belongs to a user ID for which the equivalent
1038 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1041 f_nouser(plan, entry)
1045 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1049 c_nouser(option, argvp)
1053 ftsoptions &= ~FTS_NOSTAT;
1055 return palloc(option);
1059 * -path functions --
1061 * True if the path of the filename being examined
1062 * matches pattern using Pattern Matching Notation S3.14
1069 return !fnmatch(plan->c_data, entry->fts_path,
1070 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1073 /* c_path is the same as c_name */
1076 * -perm functions --
1078 * The mode argument is used to represent file mode bits. If it starts
1079 * with a leading digit, it's treated as an octal mode, otherwise as a
1089 mode = entry->fts_statp->st_mode &
1090 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1091 if (plan->flags & F_ATLEAST)
1092 return (plan->m_data | mode) == mode;
1093 else if (plan->flags & F_ANY)
1094 return (mode & plan->m_data);
1096 return mode == plan->m_data;
1101 c_perm(option, argvp)
1109 perm = nextarg(option, argvp);
1110 ftsoptions &= ~FTS_NOSTAT;
1112 new = palloc(option);
1115 new->flags |= F_ATLEAST;
1117 } else if (*perm == '+') {
1118 new->flags |= F_ANY;
1122 if ((set = setmode(perm)) == NULL)
1123 errx(1, "%s: %s: illegal mode string", option->name, perm);
1125 new->m_data = getmode(set, 0);
1131 * -print functions --
1133 * Always true, causes the current pathame to be written to
1137 f_print(plan, entry)
1141 (void)puts(entry->fts_path);
1146 c_print(option, argvp)
1152 return palloc(option);
1156 * -print0 functions --
1158 * Always true, causes the current pathame to be written to
1159 * standard output followed by a NUL character
1162 f_print0(plan, entry)
1166 fputs(entry->fts_path, stdout);
1167 fputc('\0', stdout);
1171 /* c_print0 is the same as c_print */
1174 * -prune functions --
1176 * Prune a portion of the hierarchy.
1179 f_prune(plan, entry)
1185 if (fts_set(tree, entry, FTS_SKIP))
1186 err(1, "%s", entry->fts_path);
1190 /* c_prune == c_simple */
1193 * -regex functions --
1195 * True if the whole path of the file matches pattern using
1196 * regular expression.
1199 f_regex(plan, entry)
1208 char errbuf[LINE_MAX];
1211 pre = plan->re_data;
1212 str = entry->fts_path;
1219 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1221 if (errcode != 0 && errcode != REG_NOMATCH) {
1222 regerror(errcode, pre, errbuf, sizeof errbuf);
1224 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1227 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1234 c_regex(option, argvp)
1242 char errbuf[LINE_MAX];
1244 if ((pre = malloc(sizeof(regex_t))) == NULL)
1247 pattern = nextarg(option, argvp);
1249 if ((errcode = regcomp(pre, pattern,
1250 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1251 regerror(errcode, pre, errbuf, sizeof errbuf);
1252 errx(1, "%s: %s: %s",
1253 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1257 new = palloc(option);
1263 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */
1266 c_simple(option, argvp)
1270 return palloc(option);
1274 * -size n[c] functions --
1276 * True if the file size in bytes, divided by an implementation defined
1277 * value and rounded up to the next integer, is n. If n is followed by
1278 * a c, the size is in bytes.
1280 #define FIND_SIZE 512
1281 static int divsize = 1;
1290 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1291 FIND_SIZE : entry->fts_statp->st_size;
1292 COMPARE(size, plan->o_data);
1296 c_size(option, argvp)
1304 size_str = nextarg(option, argvp);
1305 ftsoptions &= ~FTS_NOSTAT;
1307 new = palloc(option);
1309 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1316 * -type c functions --
1318 * True if the type of the file is c, where c is b, c, d, p, f or w
1319 * for block special file, character special file, directory, FIFO,
1320 * regular file or whiteout respectively.
1327 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1331 c_type(option, argvp)
1339 typestring = nextarg(option, argvp);
1340 ftsoptions &= ~FTS_NOSTAT;
1342 switch (typestring[0]) {
1367 ftsoptions |= FTS_WHITEOUT;
1369 #endif /* FTS_WHITEOUT */
1371 errx(1, "%s: %s: unknown type", option->name, typestring);
1374 new = palloc(option);
1380 * -user uname functions --
1382 * True if the file belongs to the user uname. If uname is numeric and
1383 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1384 * return a valid user name, uname is taken as a user ID.
1391 return entry->fts_statp->st_uid == plan->u_data;
1395 c_user(option, argvp)
1404 username = nextarg(option, argvp);
1405 ftsoptions &= ~FTS_NOSTAT;
1407 p = getpwnam(username);
1409 uid = atoi(username);
1410 if (uid == 0 && username[0] != '0')
1411 errx(1, "%s: %s: no such user", option->name, username);
1415 new = palloc(option);
1421 * -xdev functions --
1423 * Always true, causes find not to decend past directories that have a
1424 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1427 c_xdev(option, argvp)
1431 ftsoptions |= FTS_XDEV;
1433 return palloc(option);
1437 * ( expression ) functions --
1439 * True if expression is true.
1447 register int state = 0;
1449 for (p = plan->p_data[0];
1450 p && (state = (p->execute)(p, entry)); p = p->next);
1455 * f_openparen and f_closeparen nodes are temporary place markers. They are
1456 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1457 * to a f_expr node containing the expression and the ')' node is discarded.
1458 * The functions themselves are only used as constants.
1462 f_openparen(plan, entry)
1470 f_closeparen(plan, entry)
1477 /* c_openparen == c_simple */
1478 /* c_closeparen == c_simple */
1481 * AND operator. Since AND is implicit, no node is allocated.
1484 c_and(option, argvp)
1492 * ! expression functions --
1494 * Negation of a primary; the unary NOT operator.
1502 register int state = 0;
1504 for (p = plan->p_data[0];
1505 p && (state = (p->execute)(p, entry)); p = p->next);
1509 /* c_not == c_simple */
1512 * expression -o expression functions --
1514 * Alternation of primaries; the OR operator. The second expression is
1515 * not evaluated if the first expression is true.
1523 register int state = 0;
1525 for (p = plan->p_data[0];
1526 p && (state = (p->execute)(p, entry)); p = p->next);
1531 for (p = plan->p_data[1];
1532 p && (state = (p->execute)(p, entry)); p = p->next);
1536 /* c_or == c_simple */