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.3 2003/10/04 20:36:44 hmp 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 (char *date, struct timeb *now);
65 #define COMPARE(a, b) do { \
66 switch (plan->flags & F_ELG_MASK) { \
79 palloc(OPTION *option)
83 if ((new = malloc(sizeof(PLAN))) == NULL)
85 new->execute = option->execute;
86 new->flags = option->flags;
93 * Parse a string of the form [+-]# and return the value.
96 find_parsenum(PLAN *plan, char *option, char *vp, char *endch)
99 char *endchar, *str; /* Pointer to character ending conversion. */
101 /* Determine comparison from leading + or -. */
106 plan->flags |= F_GREATER;
110 plan->flags |= F_LESSTHAN;
113 plan->flags |= F_EQUAL;
118 * Convert the string with strtoq(). Note, if strtoq() returns zero
119 * and endchar points to the beginning of the string we know we have
122 value = strtoq(str, &endchar, 10);
123 if (value == 0 && endchar == str)
124 errx(1, "%s: %s: illegal numeric value", option, vp);
125 if (endchar[0] && (endch == NULL || endchar[0] != *endch))
126 errx(1, "%s: %s: illegal trailing character", option, vp);
134 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
137 find_parsetime(PLAN *plan, char *option, char *vp)
139 long long secs, value;
140 char *str, *unit; /* Pointer to character ending conversion. */
142 /* Determine comparison from leading + or -. */
147 plan->flags |= F_GREATER;
151 plan->flags |= F_LESSTHAN;
154 plan->flags |= F_EQUAL;
158 value = strtoq(str, &unit, 10);
159 if (value == 0 && unit == str) {
160 errx(1, "%s: %s: illegal time value", option, vp);
170 case 's': /* seconds */
173 case 'm': /* minutes */
176 case 'h': /* hours */
177 secs += value * 3600;
180 secs += value * 86400;
182 case 'w': /* weeks */
183 secs += value * 604800;
186 errx(1, "%s: %s: bad unit '%c'", option, vp, *unit);
190 if (*str == '\0') /* EOS */
192 value = strtoq(str, &unit, 10);
193 if (value == 0 && unit == str) {
194 errx(1, "%s: %s: illegal time value", option, vp);
198 errx(1, "%s: %s: missing trailing unit", option, vp);
202 plan->flags |= F_EXACTTIME;
208 * Check that another argument still exists, return a pointer to it,
209 * and increment the argument vector pointer.
212 nextarg(OPTION *option, char ***argvp)
216 if ((arg = **argvp) == 0)
217 errx(1, "%s: requires additional arguments", option->name);
223 * The value of n for the inode times (atime, ctime, and mtime) is a range,
224 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
225 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
226 * user wanted. Correct so that -1 is "less than 1".
228 #define TIME_CORRECT(p) \
229 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
233 * -[acm]min n functions --
235 * True if the difference between the
236 * file access time (-amin)
237 * last change of file status information (-cmin)
238 * file modification time (-mmin)
239 * and the current time is n min periods.
242 f_Xmin(PLAN *plan, FTSENT *entry)
246 if (plan->flags & F_TIME_C) {
247 COMPARE((now - entry->fts_statp->st_ctime +
248 60 - 1) / 60, plan->t_data);
249 } else if (plan->flags & F_TIME_A) {
250 COMPARE((now - entry->fts_statp->st_atime +
251 60 - 1) / 60, plan->t_data);
253 COMPARE((now - entry->fts_statp->st_mtime +
254 60 - 1) / 60, plan->t_data);
259 c_Xmin(OPTION *option, char ***argvp)
264 nmins = nextarg(option, argvp);
265 ftsoptions &= ~FTS_NOSTAT;
267 new = palloc(option);
268 new->t_data = find_parsenum(new, option->name, nmins, NULL);
274 * -[acm]time n functions --
276 * True if the difference between the
277 * file access time (-atime)
278 * last change of file status information (-ctime)
279 * file modification time (-mtime)
280 * and the current time is n 24 hour periods.
284 f_Xtime(PLAN *plan, FTSENT *entry)
289 exact_time = plan->flags & F_EXACTTIME;
291 if (plan->flags & F_TIME_C) {
293 COMPARE(now - entry->fts_statp->st_ctime,
296 COMPARE((now - entry->fts_statp->st_ctime +
297 86400 - 1) / 86400, plan->t_data);
298 } else if (plan->flags & F_TIME_A) {
300 COMPARE(now - entry->fts_statp->st_atime,
303 COMPARE((now - entry->fts_statp->st_atime +
304 86400 - 1) / 86400, plan->t_data);
307 COMPARE(now - entry->fts_statp->st_mtime,
310 COMPARE((now - entry->fts_statp->st_mtime +
311 86400 - 1) / 86400, plan->t_data);
316 c_Xtime(OPTION *option, char ***argvp)
321 value = nextarg(option, argvp);
322 ftsoptions &= ~FTS_NOSTAT;
324 new = palloc(option);
325 new->t_data = find_parsetime(new, option->name, value);
326 if (!(new->flags & F_EXACTTIME))
332 * -maxdepth/-mindepth n functions --
334 * Does the same as -prune if the level of the current file is
335 * greater/less than the specified maximum/minimum depth.
337 * Note that -maxdepth and -mindepth are handled specially in
338 * find_execute() so their f_* functions are set to f_always_true().
341 c_mXXdepth(OPTION *option, char ***argvp)
346 dstr = nextarg(option, argvp);
348 /* all other errors handled by find_parsenum() */
349 errx(1, "%s: %s: value must be positive", option->name, dstr);
351 new = palloc(option);
352 if (option->flags & F_MAXDEPTH)
353 maxdepth = find_parsenum(new, option->name, dstr, NULL);
355 mindepth = find_parsenum(new, option->name, dstr, NULL);
360 * -delete functions --
362 * True always. Makes its best shot and continues on regardless.
365 f_delete(PLAN *plan, FTSENT *entry)
367 /* ignore these from fts */
368 if (strcmp(entry->fts_accpath, ".") == 0 ||
369 strcmp(entry->fts_accpath, "..") == 0)
373 if (isdepth == 0 || /* depth off */
374 (ftsoptions & FTS_NOSTAT) || /* not stat()ing */
375 !(ftsoptions & FTS_PHYSICAL) || /* physical off */
376 (ftsoptions & FTS_LOGICAL)) /* or finally, logical on */
377 errx(1, "-delete: insecure options got turned on");
379 /* Potentially unsafe - do not accept relative paths whatsoever */
380 if (strchr(entry->fts_accpath, '/') != NULL)
381 errx(1, "-delete: %s: relative path potentially not safe",
384 /* Turn off user immutable bits if running as root */
385 if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
386 !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
388 chflags(entry->fts_accpath,
389 entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
391 /* rmdir directories, unlink everything else */
392 if (S_ISDIR(entry->fts_statp->st_mode)) {
393 if (rmdir(entry->fts_accpath) < 0 && errno != ENOTEMPTY)
394 warn("-delete: rmdir(%s)", entry->fts_path);
396 if (unlink(entry->fts_accpath) < 0)
397 warn("-delete: unlink(%s)", entry->fts_path);
405 c_delete(OPTION *option, char ***argvp)
408 ftsoptions &= ~FTS_NOSTAT; /* no optimise */
409 ftsoptions |= FTS_PHYSICAL; /* disable -follow */
410 ftsoptions &= ~FTS_LOGICAL; /* disable -follow */
411 isoutput = 1; /* possible output */
412 isdepth = 1; /* -depth implied */
414 return palloc(option);
419 * -depth functions --
421 * Always true, causes descent of the directory hierarchy to be done
422 * so that all entries in a directory are acted on before the directory
426 f_always_true(PLAN *plan, FTSENT *entry)
432 c_depth(OPTION *option, char ***argvp)
436 return palloc(option);
440 * -empty functions --
442 * True if the file or directory is empty
445 f_empty(PLAN *plan, FTSENT *entry)
447 if (S_ISREG(entry->fts_statp->st_mode) &&
448 entry->fts_statp->st_size == 0)
450 if (S_ISDIR(entry->fts_statp->st_mode)) {
456 dir = opendir(entry->fts_accpath);
458 err(1, "%s", entry->fts_accpath);
459 for (dp = readdir(dir); dp; dp = readdir(dir))
460 if (dp->d_name[0] != '.' ||
461 (dp->d_name[1] != '\0' &&
462 (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
473 c_empty(OPTION *option, char ***argvp)
475 ftsoptions &= ~FTS_NOSTAT;
477 return palloc(option);
481 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
483 * True if the executed utility returns a zero value as exit status.
484 * The end of the primary expression is delimited by a semicolon. If
485 * "{}" occurs anywhere, it gets replaced by the current pathname,
486 * or, in the case of -execdir, the current basename (filename
487 * without leading directory prefix). For -exec and -ok,
488 * the current directory for the execution of utility is the same as
489 * the current directory when the find utility was started, whereas
490 * for -execdir, it is the directory the file resides in.
492 * The primary -ok differs from -exec in that it requests affirmation
493 * of the user before executing the utility.
496 f_exec(register PLAN *plan, FTSENT *entry)
504 /* XXX - if file/dir ends in '/' this will not work -- can it? */
505 if ((plan->flags & F_EXECDIR) && \
506 (file = strrchr(entry->fts_path, '/')))
509 file = entry->fts_path;
511 for (cnt = 0; plan->e_argv[cnt]; ++cnt)
512 if (plan->e_len[cnt])
513 brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
514 file, plan->e_len[cnt]);
516 if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
519 /* make sure find output is interspersed correctly with subprocesses */
523 switch (pid = fork()) {
528 /* change dir back from where we started */
529 if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
533 execvp(plan->e_argv[0], plan->e_argv);
534 warn("%s", plan->e_argv[0]);
537 pid = waitpid(pid, &status, 0);
538 return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
542 * c_exec, c_execdir, c_ok --
543 * build three parallel arrays, one with pointers to the strings passed
544 * on the command line, one with (possibly duplicated) pointers to the
545 * argv array, and one with integer values that are lengths of the
546 * strings, but also flags meaning that the string has to be massaged.
549 c_exec(OPTION *option, char ***argvp)
551 PLAN *new; /* node returned */
553 register char **argv, **ap, *p;
555 /* XXX - was in c_execdir, but seems unnecessary!?
556 ftsoptions &= ~FTS_NOSTAT;
560 /* XXX - this is a change from the previous coding */
561 new = palloc(option);
563 for (ap = argv = *argvp;; ++ap) {
566 "%s: no terminating \";\"", option->name);
572 errx(1, "%s: no command specified", option->name);
574 cnt = ap - *argvp + 1;
575 new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *));
576 new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *));
577 new->e_len = (int *)emalloc((u_int)cnt * sizeof(int));
579 for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
580 new->e_orig[cnt] = *argv;
581 for (p = *argv; *p; ++p)
582 if (p[0] == '{' && p[1] == '}') {
583 new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN);
584 new->e_len[cnt] = MAXPATHLEN;
588 new->e_argv[cnt] = *argv;
592 new->e_argv[cnt] = new->e_orig[cnt] = NULL;
599 f_flags(PLAN *plan, FTSENT *entry)
603 flags = entry->fts_statp->st_flags;
604 if (plan->flags & F_ATLEAST)
605 return (flags | plan->fl_flags) == flags &&
606 !(flags & plan->fl_notflags);
607 else if (plan->flags & F_ANY)
608 return (flags & plan->fl_flags) ||
609 (flags | plan->fl_notflags) != flags;
611 return flags == plan->fl_flags &&
612 !(plan->fl_flags & plan->fl_notflags);
616 c_flags(OPTION *option, char ***argvp)
620 u_long flags, notflags;
622 flags_str = nextarg(option, argvp);
623 ftsoptions &= ~FTS_NOSTAT;
625 new = palloc(option);
627 if (*flags_str == '-') {
628 new->flags |= F_ATLEAST;
630 } else if (*flags_str == '+') {
634 if (strtofflags(&flags_str, &flags, ¬flags) == 1)
635 errx(1, "%s: %s: illegal flags string", option->name, flags_str);
637 new->fl_flags = flags;
638 new->fl_notflags = notflags;
643 * -follow functions --
645 * Always true, causes symbolic links to be followed on a global
649 c_follow(OPTION *option, char ***argvp)
651 ftsoptions &= ~FTS_PHYSICAL;
652 ftsoptions |= FTS_LOGICAL;
654 return palloc(option);
658 * -fstype functions --
660 * True if the file is of a certain type.
663 f_fstype(PLAN *plan, FTSENT *entry)
665 static dev_t curdev; /* need a guaranteed illegal dev value */
666 static int first = 1;
668 static int val_type, val_flags;
671 /* Only check when we cross mount point. */
672 if (first || curdev != entry->fts_statp->st_dev) {
673 curdev = entry->fts_statp->st_dev;
676 * Statfs follows symlinks; find wants the link's file system,
677 * not where it points.
679 if (entry->fts_info == FTS_SL ||
680 entry->fts_info == FTS_SLNONE) {
681 if ((p = strrchr(entry->fts_accpath, '/')) != NULL)
684 p = entry->fts_accpath;
692 if (statfs(entry->fts_accpath, &sb))
693 err(1, "%s", entry->fts_accpath);
703 * Further tests may need both of these values, so
704 * always copy both of them.
706 val_flags = sb.f_flags;
707 val_type = sb.f_type;
709 switch (plan->flags & F_MTMASK) {
711 return (val_flags & plan->mt_data) != 0;
713 return (val_type == plan->mt_data);
719 #if !defined(__NetBSD__)
721 c_fstype(OPTION *option, char ***argvp)
727 fsname = nextarg(option, argvp);
728 ftsoptions &= ~FTS_NOSTAT;
730 new = palloc(option);
733 * Check first for a filesystem name.
735 if (getvfsbyname(fsname, &vfc) == 0) {
736 new->flags |= F_MTTYPE;
737 new->mt_data = vfc.vfc_typenum;
743 if (!strcmp(fsname, "local")) {
744 new->flags |= F_MTFLAG;
745 new->mt_data = MNT_LOCAL;
750 if (!strcmp(fsname, "rdonly")) {
751 new->flags |= F_MTFLAG;
752 new->mt_data = MNT_RDONLY;
758 errx(1, "%s: unknown file type", fsname);
761 #endif /* __NetBSD__ */
764 * -group gname functions --
766 * True if the file belongs to the group gname. If gname is numeric and
767 * an equivalent of the getgrnam() function does not return a valid group
768 * name, gname is taken as a group ID.
771 f_group(PLAN *plan, FTSENT *entry)
773 return entry->fts_statp->st_gid == plan->g_data;
777 c_group(OPTION *option, char ***argvp)
784 gname = nextarg(option, argvp);
785 ftsoptions &= ~FTS_NOSTAT;
790 if (gid == 0 && gname[0] != '0')
791 errx(1, "%s: %s: no such group", option->name, gname);
795 new = palloc(option);
801 * -inum n functions --
803 * True if the file has inode # n.
806 f_inum(PLAN *plan, FTSENT *entry)
808 COMPARE(entry->fts_statp->st_ino, plan->i_data);
812 c_inum(OPTION *option, char ***argvp)
817 inum_str = nextarg(option, argvp);
818 ftsoptions &= ~FTS_NOSTAT;
820 new = palloc(option);
821 new->i_data = find_parsenum(new, option->name, inum_str, NULL);
826 * -links n functions --
828 * True if the file has n links.
831 f_links(PLAN *plan, FTSENT *entry)
833 COMPARE(entry->fts_statp->st_nlink, plan->l_data);
837 c_links(OPTION *option, char ***argvp)
842 nlinks = nextarg(option, argvp);
843 ftsoptions &= ~FTS_NOSTAT;
845 new = palloc(option);
846 new->l_data = (nlink_t)find_parsenum(new, option->name, nlinks, NULL);
853 * Always true - prints the current entry to stdout in "ls" format.
856 f_ls(PLAN *plan, FTSENT *entry)
858 printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp);
863 c_ls(OPTION *option, char ***argvp)
865 ftsoptions &= ~FTS_NOSTAT;
868 return palloc(option);
874 * True if the basename of the filename being examined
875 * matches pattern using Pattern Matching Notation S3.14
878 f_name(PLAN *plan, FTSENT *entry)
880 return !fnmatch(plan->c_data, entry->fts_name,
881 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
885 c_name(OPTION *option, char ***argvp)
890 pattern = nextarg(option, argvp);
891 new = palloc(option);
892 new->c_data = pattern;
897 * -newer file functions --
899 * True if the current file has been modified more recently
900 * then the modification time of the file named by the pathname
904 f_newer(PLAN *plan, FTSENT *entry)
906 if (plan->flags & F_TIME_C)
907 return entry->fts_statp->st_ctime > plan->t_data;
908 else if (plan->flags & F_TIME_A)
909 return entry->fts_statp->st_atime > plan->t_data;
911 return entry->fts_statp->st_mtime > plan->t_data;
915 c_newer(OPTION *option, char ***argvp)
921 fn_or_tspec = nextarg(option, argvp);
922 ftsoptions &= ~FTS_NOSTAT;
924 new = palloc(option);
925 /* compare against what */
926 if (option->flags & F_TIME2_T) {
927 new->t_data = get_date(fn_or_tspec, (struct timeb *) 0);
928 if (new->t_data == (time_t) -1)
929 errx(1, "Can't parse date/time: %s", fn_or_tspec);
931 if (stat(fn_or_tspec, &sb))
932 err(1, "%s", fn_or_tspec);
933 if (option->flags & F_TIME2_C)
934 new->t_data = sb.st_ctime;
935 else if (option->flags & F_TIME2_A)
936 new->t_data = sb.st_atime;
938 new->t_data = sb.st_mtime;
944 * -nogroup functions --
946 * True if file belongs to a user ID for which the equivalent
947 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
950 f_nogroup(PLAN *plan, FTSENT *entry)
952 return group_from_gid(entry->fts_statp->st_gid, 1) == NULL;
956 c_nogroup(OPTION *option, char ***argvp)
958 ftsoptions &= ~FTS_NOSTAT;
960 return palloc(option);
964 * -nouser functions --
966 * True if file belongs to a user ID for which the equivalent
967 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
970 f_nouser(PLAN *plan, FTSENT *entry)
972 return user_from_uid(entry->fts_statp->st_uid, 1) == NULL;
976 c_nouser(OPTION *option, char ***argvp)
978 ftsoptions &= ~FTS_NOSTAT;
980 return palloc(option);
986 * True if the path of the filename being examined
987 * matches pattern using Pattern Matching Notation S3.14
990 f_path(PLAN *plan, FTSENT *entry)
992 return !fnmatch(plan->c_data, entry->fts_path,
993 plan->flags & F_IGNCASE ? FNM_CASEFOLD : 0);
996 /* c_path is the same as c_name */
1001 * The mode argument is used to represent file mode bits. If it starts
1002 * with a leading digit, it's treated as an octal mode, otherwise as a
1006 f_perm(PLAN *plan, FTSENT *entry)
1010 mode = entry->fts_statp->st_mode &
1011 (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO);
1012 if (plan->flags & F_ATLEAST)
1013 return (plan->m_data | mode) == mode;
1014 else if (plan->flags & F_ANY)
1015 return (mode & plan->m_data);
1017 return mode == plan->m_data;
1022 c_perm(OPTION *option, char ***argvp)
1028 perm = nextarg(option, argvp);
1029 ftsoptions &= ~FTS_NOSTAT;
1031 new = palloc(option);
1034 new->flags |= F_ATLEAST;
1036 } else if (*perm == '+') {
1037 new->flags |= F_ANY;
1041 if ((set = setmode(perm)) == NULL)
1042 errx(1, "%s: %s: illegal mode string", option->name, perm);
1044 new->m_data = getmode(set, 0);
1050 * -print functions --
1052 * Always true, causes the current pathame to be written to
1056 f_print(PLAN *plan, FTSENT *entry)
1058 (void)puts(entry->fts_path);
1063 c_print(OPTION *option, char ***argvp)
1067 return palloc(option);
1071 * -print0 functions --
1073 * Always true, causes the current pathame to be written to
1074 * standard output followed by a NUL character
1077 f_print0(PLAN *plan, FTSENT *entry)
1079 fputs(entry->fts_path, stdout);
1080 fputc('\0', stdout);
1084 /* c_print0 is the same as c_print */
1087 * -prune functions --
1089 * Prune a portion of the hierarchy.
1092 f_prune(PLAN *plan, FTSENT *entry)
1096 if (fts_set(tree, entry, FTS_SKIP))
1097 err(1, "%s", entry->fts_path);
1101 /* c_prune == c_simple */
1104 * -regex functions --
1106 * True if the whole path of the file matches pattern using
1107 * regular expression.
1110 f_regex(PLAN *plan, FTSENT *entry)
1117 char errbuf[LINE_MAX];
1120 pre = plan->re_data;
1121 str = entry->fts_path;
1128 errcode = regexec(pre, str, 1, &pmatch, REG_STARTEND);
1130 if (errcode != 0 && errcode != REG_NOMATCH) {
1131 regerror(errcode, pre, errbuf, sizeof errbuf);
1133 plan->flags & F_IGNCASE ? "-iregex" : "-regex", errbuf);
1136 if (errcode == 0 && pmatch.rm_so == 0 && pmatch.rm_eo == len)
1143 c_regex(OPTION *option, char ***argvp)
1149 char errbuf[LINE_MAX];
1151 if ((pre = malloc(sizeof(regex_t))) == NULL)
1154 pattern = nextarg(option, argvp);
1156 if ((errcode = regcomp(pre, pattern,
1157 regexp_flags | (option->flags & F_IGNCASE ? REG_ICASE : 0))) != 0) {
1158 regerror(errcode, pre, errbuf, sizeof errbuf);
1159 errx(1, "%s: %s: %s",
1160 option->flags & F_IGNCASE ? "-iregex" : "-regex",
1164 new = palloc(option);
1170 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */
1173 c_simple(OPTION *option, char ***argvp)
1175 return palloc(option);
1179 * -size n[c] functions --
1181 * True if the file size in bytes, divided by an implementation defined
1182 * value and rounded up to the next integer, is n. If n is followed by
1183 * a c, the size is in bytes.
1185 #define FIND_SIZE 512
1186 static int divsize = 1;
1189 f_size(PLAN *plan, FTSENT *entry)
1193 size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) /
1194 FIND_SIZE : entry->fts_statp->st_size;
1195 COMPARE(size, plan->o_data);
1199 c_size(OPTION *option, char ***argvp)
1205 size_str = nextarg(option, argvp);
1206 ftsoptions &= ~FTS_NOSTAT;
1208 new = palloc(option);
1210 new->o_data = find_parsenum(new, option->name, size_str, &endch);
1217 * -type c functions --
1219 * True if the type of the file is c, where c is b, c, d, p, f or w
1220 * for block special file, character special file, directory, FIFO,
1221 * regular file or whiteout respectively.
1224 f_type(PLAN *plan, FTSENT *entry)
1226 return (entry->fts_statp->st_mode & S_IFMT) == plan->m_data;
1230 c_type(OPTION *option, char ***argvp)
1236 typestring = nextarg(option, argvp);
1237 ftsoptions &= ~FTS_NOSTAT;
1239 switch (typestring[0]) {
1264 ftsoptions |= FTS_WHITEOUT;
1266 #endif /* FTS_WHITEOUT */
1268 errx(1, "%s: %s: unknown type", option->name, typestring);
1271 new = palloc(option);
1277 * -user uname functions --
1279 * True if the file belongs to the user uname. If uname is numeric and
1280 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1281 * return a valid user name, uname is taken as a user ID.
1284 f_user(PLAN *plan, FTSENT *entry)
1286 return entry->fts_statp->st_uid == plan->u_data;
1290 c_user(OPTION *option, char ***argvp)
1297 username = nextarg(option, argvp);
1298 ftsoptions &= ~FTS_NOSTAT;
1300 p = getpwnam(username);
1302 uid = atoi(username);
1303 if (uid == 0 && username[0] != '0')
1304 errx(1, "%s: %s: no such user", option->name, username);
1308 new = palloc(option);
1314 * -xdev functions --
1316 * Always true, causes find not to decend past directories that have a
1317 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1320 c_xdev(OPTION *option, char ***argvp)
1322 ftsoptions |= FTS_XDEV;
1324 return palloc(option);
1328 * ( expression ) functions --
1330 * True if expression is true.
1333 f_expr(PLAN *plan, FTSENT *entry)
1336 register int state = 0;
1338 for (p = plan->p_data[0];
1339 p && (state = (p->execute)(p, entry)); p = p->next);
1344 * f_openparen and f_closeparen nodes are temporary place markers. They are
1345 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1346 * to a f_expr node containing the expression and the ')' node is discarded.
1347 * The functions themselves are only used as constants.
1351 f_openparen(PLAN *plan, FTSENT *entry)
1357 f_closeparen(PLAN *plan, FTSENT *entry)
1362 /* c_openparen == c_simple */
1363 /* c_closeparen == c_simple */
1366 * AND operator. Since AND is implicit, no node is allocated.
1369 c_and(OPTION *option, char ***argvp)
1375 * ! expression functions --
1377 * Negation of a primary; the unary NOT operator.
1380 f_not(PLAN *plan, FTSENT *entry)
1383 register int state = 0;
1385 for (p = plan->p_data[0];
1386 p && (state = (p->execute)(p, entry)); p = p->next);
1390 /* c_not == c_simple */
1393 * expression -o expression functions --
1395 * Alternation of primaries; the OR operator. The second expression is
1396 * not evaluated if the first expression is true.
1399 f_or(PLAN *plan, FTSENT *entry)
1402 register int state = 0;
1404 for (p = plan->p_data[0];
1405 p && (state = (p->execute)(p, entry)); p = p->next);
1410 for (p = plan->p_data[1];
1411 p && (state = (p->execute)(p, entry)); p = p->next);
1415 /* c_or == c_simple */