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[] =
42 "$FreeBSD: src/usr.bin/find/function.c,v 1.22.2.11 2002/11/15 11:38:15 sheldonh Exp $";
46 #include <sys/param.h>
47 #include <sys/ucred.h>
50 #include <sys/mount.h>
51 #include <sys/timeb.h>
68 time_t get_date __P((char *date, struct timeb *now));
70 #define COMPARE(a, b) do { \
71 switch (plan->flags & F_ELG_MASK) { \
89 if ((new = malloc(sizeof(PLAN))) == NULL)
91 new->execute = option->execute;
92 new->flags = option->flags;
99 * Parse a string of the form [+-]# and return the value.
102 find_parsenum(plan, option, vp, endch)
104 char *option, *vp, *endch;
107 char *endchar, *str; /* Pointer to character ending conversion. */
109 /* Determine comparison from leading + or -. */
114 plan->flags |= F_GREATER;
118 plan->flags |= F_LESSTHAN;
121 plan->flags |= F_EQUAL;
126 * Convert the string with strtoq(). Note, if strtoq() returns zero
127 * and endchar points to the beginning of the string we know we have
130 value = strtoq(str, &endchar, 10);
131 if (value == 0 && endchar == str)
132 errx(1, "%s: %s: illegal numeric value", option, vp);
133 if (endchar[0] && (endch == NULL || endchar[0] != *endch))
134 errx(1, "%s: %s: illegal trailing character", option, vp);
142 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
145 find_parsetime(plan, option, vp)
149 long long secs, value;
150 char *str, *unit; /* Pointer to character ending conversion. */
152 /* Determine comparison from leading + or -. */
157 plan->flags |= F_GREATER;
161 plan->flags |= F_LESSTHAN;
164 plan->flags |= F_EQUAL;
168 value = strtoq(str, &unit, 10);
169 if (value == 0 && unit == str) {
170 errx(1, "%s: %s: illegal time value", option, vp);
180 case 's': /* seconds */
183 case 'm': /* minutes */
186 case 'h': /* hours */
187 secs += value * 3600;
190 secs += value * 86400;
192 case 'w': /* weeks */
193 secs += value * 604800;
196 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
200 if (*str == '\0') /* EOS */
202 value = strtoq(str, &unit, 10);
203 if (value == 0 && unit == str) {
204 errx(1, "%s: %s: illegal time value", option, vp);
208 errx(1, "%s: %s: missing trailing unit", option, vp);
212 plan->flags |= F_EXACTTIME;
218 * Check that another argument still exists, return a pointer to it,
219 * and increment the argument vector pointer.
222 nextarg(option, argvp)
228 if ((arg = **argvp) == 0)
229 errx(1, "%s: requires additional arguments", option->name);
235 * The value of n for the inode times (atime, ctime, and mtime) is a range,
236 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
237 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
238 * user wanted. Correct so that -1 is "less than 1".
240 #define TIME_CORRECT(p) \
241 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
245 * -[acm]min n functions --
247 * True if the difference between the
248 * file access time (-amin)
249 * last change of file status information (-cmin)
250 * file modification time (-mmin)
251 * and the current time is n min periods.
260 if (plan->flags & F_TIME_C) {
261 COMPARE((now - entry->fts_statp->st_ctime +
262 60 - 1) / 60, plan->t_data);
263 } else if (plan->flags & F_TIME_A) {
264 COMPARE((now - entry->fts_statp->st_atime +
265 60 - 1) / 60, plan->t_data);
267 COMPARE((now - entry->fts_statp->st_mtime +
268 60 - 1) / 60, plan->t_data);
273 c_Xmin(option, argvp)
280 nmins = nextarg(option, argvp);
281 ftsoptions &= ~FTS_NOSTAT;
283 new = palloc(option);
284 new->t_data = find_parsenum(new, option->name, nmins, NULL);
290 * -[acm]time n functions --
292 * True if the difference between the
293 * file access time (-atime)
294 * last change of file status information (-ctime)
295 * file modification time (-mtime)
296 * and the current time is n 24 hour periods.
307 exact_time = plan->flags & F_EXACTTIME;
309 if (plan->flags & F_TIME_C) {
311 COMPARE(now - entry->fts_statp->st_ctime,
314 COMPARE((now - entry->fts_statp->st_ctime +
315 86400 - 1) / 86400, plan->t_data);
316 } else if (plan->flags & F_TIME_A) {
318 COMPARE(now - entry->fts_statp->st_atime,
321 COMPARE((now - entry->fts_statp->st_atime +
322 86400 - 1) / 86400, plan->t_data);
325 COMPARE(now - entry->fts_statp->st_mtime,
328 COMPARE((now - entry->fts_statp->st_mtime +
329 86400 - 1) / 86400, plan->t_data);
334 c_Xtime(option, argvp)
341 value = nextarg(option, argvp);
342 ftsoptions &= ~FTS_NOSTAT;
344 new = palloc(option);
345 new->t_data = find_parsetime(new, option->name, value);
346 if (!(new->flags & F_EXACTTIME))
352 * -maxdepth/-mindepth n functions --
354 * Does the same as -prune if the level of the current file is
355 * greater/less than the specified maximum/minimum depth.
357 * Note that -maxdepth and -mindepth are handled specially in
358 * find_execute() so their f_* functions are set to f_always_true().
361 c_mXXdepth(option, argvp)
368 dstr = nextarg(option, argvp);
370 /* all other errors handled by find_parsenum() */
371 errx(1, "%s: %s: value must be positive", option->name, dstr);
373 new = palloc(option);
374 if (option->flags & F_MAXDEPTH)
375 maxdepth = find_parsenum(new, option->name, dstr, NULL);
377 mindepth = find_parsenum(new, option->name, dstr, NULL);
382 * -delete functions --
384 * True always. Makes its best shot and continues on regardless.
387 f_delete(plan, entry)
391 /* ignore these from fts */
392 if (strcmp(entry->fts_accpath, ".") == 0 ||
393 strcmp(entry->fts_accpath, "..") == 0)
397 if (isdepth == 0 || /* depth off */
398 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */
399 !(ftsoptions & FTS_PHYSICAL) || /* physical off */
400 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
401 errx(1, "-delete: insecure options got turned on");
403 /* Potentially unsafe - do not accept relative paths whatsoever */
404 if (strchr(entry->fts_accpath, '/') != NULL)
405 errx(1, "-delete: %s: relative path potentially not safe",
408 /* Turn off user immutable bits if running as root */
409 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
410 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
412 chflags(entry->fts_accpath,
413 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
415 /* rmdir directories, unlink everything else */
416 if (S_ISDIR(entry->fts_statp->st_mode)) {
417 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
418 warn("-delete: rmdir(%s)", entry->fts_path);
420 if (unlink(entry->fts_accpath) < 0)
421 warn("-delete: unlink(%s)", entry->fts_path);
429 c_delete(option, argvp)
434 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
435 ftsoptions |= FTS_PHYSICAL; /* disable -follow */
436 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */
437 isoutput = 1; /* possible output */
438 isdepth = 1; /* -depth implied */
440 return palloc(option);
445 * -depth functions --
447 * Always true, causes descent of the directory hierarchy to be done
448 * so that all entries in a directory are acted on before the directory
452 f_always_true(plan, entry)
460 c_depth(option, argvp)
466 return palloc(option);
470 * -empty functions --
472 * True if the file or directory is empty
479 if (S_ISREG(entry->fts_statp->st_mode) &&
480 entry->fts_statp->st_size == 0)
482 if (S_ISDIR(entry->fts_statp->st_mode)) {
488 dir = opendir(entry->fts_accpath);
490 err(1, "%s", entry->fts_accpath);
491 for (dp = readdir(dir); dp; dp = readdir(dir))
492 if (dp->d_name[0] != '.' ||
493 (dp->d_name[1] != '\0' &&
494 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
505 c_empty(option, argvp)
509 ftsoptions &= ~FTS_NOSTAT;
511 return palloc(option);
515 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
517 * True if the executed utility returns a zero value as exit status.
518 * The end of the primary expression is delimited by a semicolon. If
519 * "{}" occurs anywhere, it gets replaced by the current pathname,
520 * or, in the case of -execdir, the current basename (filename
521 * without leading directory prefix). For -exec and -ok,
522 * the current directory for the execution of utility is the same as
523 * the current directory when the find utility was started, whereas
524 * for -execdir, it is the directory the file resides in.
526 * The primary -ok differs from -exec in that it requests affirmation
527 * of the user before executing the utility.
540 /* XXX - if file/dir ends in '/' this will not work -- can it? */
541 if ((plan->flags & F_EXECDIR) && \
542 (file = strrchr(entry->fts_path, '/')))
545 file = entry->fts_path;
547 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
548 if (plan->e_len[cnt])
549 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
550 file, plan->e_len[cnt]);
552 if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
555 /* make sure find output is interspersed correctly with subprocesses */
559 switch (pid = fork()) {
564 /* change dir back from where we started */
565 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
569 execvp(plan->e_argv[0], plan->e_argv);
570 warn("%s", plan->e_argv[0]);
573 pid = waitpid(pid, &status, 0);
574 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
578 * c_exec, c_execdir, c_ok --
579 * build three parallel arrays, one with pointers to the strings passed
580 * on the command line, one with (possibly duplicated) pointers to the
581 * argv array, and one with integer values that are lengths of the
582 * strings, but also flags meaning that the string has to be massaged.
585 c_exec(option, argvp)
589 PLAN *new; /* node returned */
591 register char **argv, **ap, *p;
593 /* XXX - was in c_execdir, but seems unnecessary!?
594 ftsoptions &= ~FTS_NOSTAT;
598 /* XXX - this is a change from the previous coding */
599 new = palloc(option);
601 for (ap = argv = *argvp;; ++ap) {
604 "%s: no terminating \";\"", option->name);
610 errx(1, "%s: no command specified", option->name);
612 cnt = ap - *argvp + 1;
613 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
614 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
615 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
617 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
618 new->e_orig[cnt] = *argv;
619 for (p = *argv; *p; ++p)
620 if (p[0] == '{' && p[1] == '}') {
621 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
622 new->e_len[cnt] = MAXPATHLEN;
626 new->e_argv[cnt] = *argv;
630 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
643 flags = entry->fts_statp->st_flags;
644 if (plan->flags & F_ATLEAST)
645 return (flags | plan->fl_flags) == flags &&
646 !(flags & plan->fl_notflags);
647 else if (plan->flags & F_ANY)
648 return (flags & plan->fl_flags) ||
649 (flags | plan->fl_notflags) != flags;
651 return flags == plan->fl_flags &&
652 !(plan->fl_flags & plan->fl_notflags);
656 c_flags(option, argvp)
662 u_long flags, notflags;
664 flags_str = nextarg(option, argvp);
665 ftsoptions &= ~FTS_NOSTAT;
667 new = palloc(option);
669 if (*flags_str == '-') {
670 new->flags |= F_ATLEAST;
672 } else if (*flags_str == '+') {
676 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
677 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
679 new->fl_flags = flags;
680 new->fl_notflags = notflags;
685 * -follow functions --
687 * Always true, causes symbolic links to be followed on a global
691 c_follow(option, argvp)
695 ftsoptions &= ~FTS_PHYSICAL;
696 ftsoptions |= FTS_LOGICAL;
698 return palloc(option);
702 * -fstype functions --
704 * True if the file is of a certain type.
707 f_fstype(plan, entry)
711 static dev_t curdev; /* need a guaranteed illegal dev value */
712 static int first = 1;
714 static int val_type, val_flags;
717 /* Only check when we cross mount point. */
718 if (first || curdev != entry->fts_statp->st_dev) {
719 curdev = entry->fts_statp->st_dev;
722 * Statfs follows symlinks; find wants the link's file system,
723 * not where it points.
725 if (entry->fts_info == FTS_SL ||
726 entry->fts_info == FTS_SLNONE) {
727 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
730 p = entry->fts_accpath;
738 if (statfs(entry->fts_accpath, &sb))
739 err(1, "%s", entry->fts_accpath);
749 * Further tests may need both of these values, so
750 * always copy both of them.
752 val_flags = sb.f_flags;
753 val_type = sb.f_type;
755 switch (plan->flags & F_MTMASK) {
757 return (val_flags & plan->mt_data) != 0;
759 return (val_type == plan->mt_data);
765 #if !defined(__NetBSD__)
767 c_fstype(option, argvp)
775 fsname = nextarg(option, argvp);
776 ftsoptions &= ~FTS_NOSTAT;
778 new = palloc(option);
781 * Check first for a filesystem name.
783 if (getvfsbyname(fsname, &vfc) == 0) {
784 new->flags |= F_MTTYPE;
785 new->mt_data = vfc.vfc_typenum;
791 if (!strcmp(fsname, "local")) {
792 new->flags |= F_MTFLAG;
793 new->mt_data = MNT_LOCAL;
798 if (!strcmp(fsname, "rdonly")) {
799 new->flags |= F_MTFLAG;
800 new->mt_data = MNT_RDONLY;
806 errx(1, "%s: unknown file type", fsname);
809 #endif /* __NetBSD__ */
812 * -group gname functions --
814 * True if the file belongs to the group gname. If gname is numeric and
815 * an equivalent of the getgrnam() function does not return a valid group
816 * name, gname is taken as a group ID.
823 return entry->fts_statp->st_gid == plan->g_data;
827 c_group(option, argvp)
836 gname = nextarg(option, argvp);
837 ftsoptions &= ~FTS_NOSTAT;
842 if (gid == 0 && gname[0] != '0')
843 errx(1, "%s: %s: no such group", option->name, gname);
847 new = palloc(option);
853 * -inum n functions --
855 * True if the file has inode # n.
862 COMPARE(entry->fts_statp->st_ino, plan->i_data);
866 c_inum(option, argvp)
873 inum_str = nextarg(option, argvp);
874 ftsoptions &= ~FTS_NOSTAT;
876 new = palloc(option);
877 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
882 * -links n functions --
884 * True if the file has n links.
891 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
895 c_links(option, argvp)
902 nlinks = nextarg(option, argvp);
903 ftsoptions &= ~FTS_NOSTAT;
905 new = palloc(option);
906 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
913 * Always true - prints the current entry to stdout in "ls" format.
920 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
929 ftsoptions &= ~FTS_NOSTAT;
932 return palloc(option);
938 * True if the basename of the filename being examined
939 * matches pattern using Pattern Matching Notation S3.14
946 return !fnmatch(plan->c_data, entry->fts_name,
947 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
951 c_name(option, argvp)
958 pattern = nextarg(option, argvp);
959 new = palloc(option);
960 new->c_data = pattern;
965 * -newer file functions --
967 * True if the current file has been modified more recently
968 * then the modification time of the file named by the pathname
976 if (plan->flags & F_TIME_C)
977 return entry->fts_statp->st_ctime > plan->t_data;
978 else if (plan->flags & F_TIME_A)
979 return entry->fts_statp->st_atime > plan->t_data;
981 return entry->fts_statp->st_mtime > plan->t_data;
985 c_newer(option, argvp)
993 fn_or_tspec = nextarg(option, argvp);
994 ftsoptions &= ~FTS_NOSTAT;
996 new = palloc(option);
997 /* compare against what */
998 if (option->flags & F_TIME2_T) {
999 new->t_data = get_date(fn_or_tspec, (struct timeb *) 0);
1000 if (new->t_data == (time_t) -1)
1001 errx(1, "Can't parse date/time: %s", fn_or_tspec);
1003 if (stat(fn_or_tspec, &sb))
1004 err(1, "%s", fn_or_tspec);
1005 if (option->flags & F_TIME2_C)
1006 new->t_data = sb.st_ctime;
1007 else if (option->flags & F_TIME2_A)
1008 new->t_data = sb.st_atime;
1010 new->t_data = sb.st_mtime;
1016 * -nogroup functions --
1018 * True if file belongs to a user ID for which the equivalent
1019 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1022 f_nogroup(plan, entry)
1026 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
1030 c_nogroup(option, argvp)
1034 ftsoptions &= ~FTS_NOSTAT;
1036 return palloc(option);
1040 * -nouser functions --
1042 * True if file belongs to a user ID for which the equivalent
1043 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1046 f_nouser(plan, entry)
1050 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
1054 c_nouser(option, argvp)
1058 ftsoptions &= ~FTS_NOSTAT;
1060 return palloc(option);
1064 * -path functions --
1066 * True if the path of the filename being examined
1067 * matches pattern using Pattern Matching Notation S3.14
1074 return !fnmatch(plan->c_data, entry->fts_path,
1075 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
1078 /* c_path is the same as c_name */
1081 * -perm functions --
1083 * The mode argument is used to represent file mode bits. If it starts
1084 * with a leading digit, it's treated as an octal mode, otherwise as a
1094 mode = entry->fts_statp->st_mode &
1095 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1096 if (plan->flags & F_ATLEAST)
1097 return (plan->m_data | mode) == mode;
1098 else if (plan->flags & F_ANY)
1099 return (mode & plan->m_data);
1101 return mode == plan->m_data;
1106 c_perm(option, argvp)
1114 perm = nextarg(option, argvp);
1115 ftsoptions &= ~FTS_NOSTAT;
1117 new = palloc(option);
1120 new->flags |= F_ATLEAST;
1122 } else if (*perm == '+') {
1123 new->flags |= F_ANY;
1127 if ((set = setmode(perm)) == NULL)
1128 errx(1, "%s: %s: illegal mode string", option->name, perm);
1130 new->m_data = getmode(set, 0);
1136 * -print functions --
1138 * Always true, causes the current pathame to be written to
1142 f_print(plan, entry)
1146 (void)puts(entry->fts_path);
1151 c_print(option, argvp)
1157 return palloc(option);
1161 * -print0 functions --
1163 * Always true, causes the current pathame to be written to
1164 * standard output followed by a NUL character
1167 f_print0(plan, entry)
1171 fputs(entry->fts_path, stdout);
1172 fputc('\0', stdout);
1176 /* c_print0 is the same as c_print */
1179 * -prune functions --
1181 * Prune a portion of the hierarchy.
1184 f_prune(plan, entry)
1190 if (fts_set(tree, entry, FTS_SKIP))
1191 err(1, "%s", entry->fts_path);
1195 /* c_prune == c_simple */
1198 * -regex functions --
1200 * True if the whole path of the file matches pattern using
1201 * regular expression.
1204 f_regex(plan, entry)
1213 char errbuf[LINE_MAX];
1216 pre = plan->re_data;
1217 str = entry->fts_path;
1224 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1226 if (errcode != 0 && errcode != REG_NOMATCH) {
1227 regerror(errcode, pre, errbuf, sizeof errbuf);
1229 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1232 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1239 c_regex(option, argvp)
1247 char errbuf[LINE_MAX];
1249 if ((pre = malloc(sizeof(regex_t))) == NULL)
1252 pattern = nextarg(option, argvp);
1254 if ((errcode = regcomp(pre, pattern,
1255 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1256 regerror(errcode, pre, errbuf, sizeof errbuf);
1257 errx(1, "%s: %s: %s",
1258 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1262 new = palloc(option);
1268 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */
1271 c_simple(option, argvp)
1275 return palloc(option);
1279 * -size n[c] functions --
1281 * True if the file size in bytes, divided by an implementation defined
1282 * value and rounded up to the next integer, is n. If n is followed by
1283 * a c, the size is in bytes.
1285 #define FIND_SIZE 512
1286 static int divsize = 1;
1295 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1296 FIND_SIZE : entry->fts_statp->st_size;
1297 COMPARE(size, plan->o_data);
1301 c_size(option, argvp)
1309 size_str = nextarg(option, argvp);
1310 ftsoptions &= ~FTS_NOSTAT;
1312 new = palloc(option);
1314 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1321 * -type c functions --
1323 * True if the type of the file is c, where c is b, c, d, p, f or w
1324 * for block special file, character special file, directory, FIFO,
1325 * regular file or whiteout respectively.
1332 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1336 c_type(option, argvp)
1344 typestring = nextarg(option, argvp);
1345 ftsoptions &= ~FTS_NOSTAT;
1347 switch (typestring[0]) {
1372 ftsoptions |= FTS_WHITEOUT;
1374 #endif /* FTS_WHITEOUT */
1376 errx(1, "%s: %s: unknown type", option->name, typestring);
1379 new = palloc(option);
1385 * -user uname functions --
1387 * True if the file belongs to the user uname. If uname is numeric and
1388 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1389 * return a valid user name, uname is taken as a user ID.
1396 return entry->fts_statp->st_uid == plan->u_data;
1400 c_user(option, argvp)
1409 username = nextarg(option, argvp);
1410 ftsoptions &= ~FTS_NOSTAT;
1412 p = getpwnam(username);
1414 uid = atoi(username);
1415 if (uid == 0 && username[0] != '0')
1416 errx(1, "%s: %s: no such user", option->name, username);
1420 new = palloc(option);
1426 * -xdev functions --
1428 * Always true, causes find not to decend past directories that have a
1429 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1432 c_xdev(option, argvp)
1436 ftsoptions |= FTS_XDEV;
1438 return palloc(option);
1442 * ( expression ) functions --
1444 * True if expression is true.
1452 register int state = 0;
1454 for (p = plan->p_data[0];
1455 p && (state = (p->execute)(p, entry)); p = p->next);
1460 * f_openparen and f_closeparen nodes are temporary place markers. They are
1461 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1462 * to a f_expr node containing the expression and the ')' node is discarded.
1463 * The functions themselves are only used as constants.
1467 f_openparen(plan, entry)
1475 f_closeparen(plan, entry)
1482 /* c_openparen == c_simple */
1483 /* c_closeparen == c_simple */
1486 * AND operator. Since AND is implicit, no node is allocated.
1489 c_and(option, argvp)
1497 * ! expression functions --
1499 * Negation of a primary; the unary NOT operator.
1507 register int state = 0;
1509 for (p = plan->p_data[0];
1510 p && (state = (p->execute)(p, entry)); p = p->next);
1514 /* c_not == c_simple */
1517 * expression -o expression functions --
1519 * Alternation of primaries; the OR operator. The second expression is
1520 * not evaluated if the first expression is true.
1528 register int state = 0;
1530 for (p = plan->p_data[0];
1531 p && (state = (p->execute)(p, entry)); p = p->next);
1536 for (p = plan->p_data[1];
1537 p && (state = (p->execute)(p, entry)); p = p->next);
1541 /* c_or == c_simple */