]>
git.saurik.com Git - apple/shell_cmds.git/blob - find/function.c
eb2c65ad5baf823cff5cb1d71e4f1863661ebc41
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.9 2001/09/19 09:44:24 ru Exp $";
46 #include <sys/param.h>
47 #include <sys/ucred.h>
50 #include <sys/mount.h>
51 #include <sys/timeb.h>
52 #include <sys/types.h>
53 #include <sys/sysctl.h>
71 #include "get_compat.h"
73 #define COMPAT_MODE(func, mode) 1
76 time_t get_date
__P((char *date
, struct timeb
*now
));
78 #define COMPARE(a, b) { \
79 switch (plan->flags & F_ELG_MASK) { \
97 if ((new = malloc(sizeof(PLAN
))) == NULL
)
99 new->execute
= option
->execute
;
100 new->flags
= option
->flags
;
107 * Parse a string of the form [+-]# and return the value.
110 find_parsenum(plan
, option
, vp
, endch
)
112 char *option
, *vp
, *endch
;
115 char *endchar
, *str
; /* Pointer to character ending conversion. */
117 /* Determine comparison from leading + or -. */
122 plan
->flags
|= F_GREATER
;
126 plan
->flags
|= F_LESSTHAN
;
129 plan
->flags
|= F_EQUAL
;
134 * Convert the string with strtoq(). Note, if strtoq() returns zero
135 * and endchar points to the beginning of the string we know we have
138 value
= strtoq(str
, &endchar
, 10);
139 if (value
== 0 && endchar
== str
)
140 errx(1, "%s: %s: illegal numeric value", option
, vp
);
141 if (endchar
[0] && (endch
== NULL
|| endchar
[0] != *endch
))
142 errx(1, "%s: %s: illegal trailing character", option
, vp
);
150 * Check that another argument still exists, return a pointer to it,
151 * and increment the argument vector pointer.
154 nextarg(option
, argvp
)
160 if ((arg
= **argvp
) == 0)
161 errx(1, "%s: requires additional arguments", option
->name
);
167 * The value of n for the inode times (atime, ctime, and mtime) is a range,
168 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
169 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
170 * user wanted. Correct so that -1 is "less than 1".
172 #define TIME_CORRECT(p) \
173 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
177 * -[acm]min n functions --
179 * True if the difference between the
180 * file access time (-amin)
181 * last change of file status information (-cmin)
182 * file modification time (-mmin)
183 * and the current time is n min periods.
192 if (plan
->flags
& F_TIME_C
) {
193 COMPARE((now
- entry
->fts_statp
->st_ctime
+
194 60 - 1) / 60, plan
->t_data
);
195 } else if (plan
->flags
& F_TIME_A
) {
196 COMPARE((now
- entry
->fts_statp
->st_atime
+
197 60 - 1) / 60, plan
->t_data
);
199 COMPARE((now
- entry
->fts_statp
->st_mtime
+
200 60 - 1) / 60, plan
->t_data
);
205 c_Xmin(option
, argvp
)
212 nmins
= nextarg(option
, argvp
);
213 ftsoptions
&= ~FTS_NOSTAT
;
215 new = palloc(option
);
216 new->t_data
= find_parsenum(new, option
->name
, nmins
, NULL
);
222 * -[acm]time n functions --
224 * True if the difference between the
225 * file access time (-atime)
226 * last change of file status information (-ctime)
227 * file modification time (-mtime)
228 * and the current time is n 24 hour periods.
238 int fudge
= COMPAT_MODE("bin/find", "unix2003") ? 0 : 86400 - 1;
240 if (plan
->flags
& F_TIME_C
) {
241 COMPARE((now
- entry
->fts_statp
->st_ctime
+
242 fudge
) / 86400, plan
->t_data
);
243 } else if (plan
->flags
& F_TIME_A
) {
244 COMPARE((now
- entry
->fts_statp
->st_atime
+
245 fudge
) / 86400, plan
->t_data
);
247 COMPARE((now
- entry
->fts_statp
->st_mtime
+
248 fudge
) / 86400, plan
->t_data
);
253 c_Xtime(option
, argvp
)
260 ndays
= nextarg(option
, argvp
);
261 ftsoptions
&= ~FTS_NOSTAT
;
263 new = palloc(option
);
264 new->t_data
= find_parsenum(new, option
->name
, ndays
, NULL
);
270 * -maxdepth/-mindepth n functions --
272 * Does the same as -prune if the level of the current file is
273 * greater/less than the specified maximum/minimum depth.
275 * Note that -maxdepth and -mindepth are handled specially in
276 * find_execute() so their f_* functions are set to f_always_true().
279 c_mXXdepth(option
, argvp
)
286 dstr
= nextarg(option
, argvp
);
288 /* all other errors handled by find_parsenum() */
289 errx(1, "%s: %s: value must be positive", option
->name
, dstr
);
291 new = palloc(option
);
292 if (option
->flags
& F_MAXDEPTH
)
293 maxdepth
= find_parsenum(new, option
->name
, dstr
, NULL
);
295 mindepth
= find_parsenum(new, option
->name
, dstr
, NULL
);
300 * -delete functions --
302 * True always. Makes its best shot and continues on regardless.
305 f_delete(plan
, entry
)
309 /* ignore these from fts */
310 if (strcmp(entry
->fts_accpath
, ".") == 0 ||
311 strcmp(entry
->fts_accpath
, "..") == 0)
315 if (isdepth
== 0 || /* depth off */
316 (ftsoptions
& FTS_NOSTAT
) || /* not stat()ing */
317 !(ftsoptions
& FTS_PHYSICAL
) || /* physical off */
318 (ftsoptions
& FTS_LOGICAL
)) /* or finally, logical on */
319 errx(1, "-delete: insecure options got turned on");
321 /* Potentially unsafe - do not accept relative paths whatsoever */
322 if (strchr(entry
->fts_accpath
, '/') != NULL
)
323 errx(1, "-delete: %s: relative path potentially not safe",
326 /* Turn off user immutable bits if running as root */
327 if ((entry
->fts_statp
->st_flags
& (UF_APPEND
|UF_IMMUTABLE
)) &&
328 !(entry
->fts_statp
->st_flags
& (SF_APPEND
|SF_IMMUTABLE
)) &&
330 chflags(entry
->fts_accpath
,
331 entry
->fts_statp
->st_flags
&= ~(UF_APPEND
|UF_IMMUTABLE
));
333 /* rmdir directories, unlink everything else */
334 if (S_ISDIR(entry
->fts_statp
->st_mode
)) {
335 if (rmdir(entry
->fts_accpath
) < 0 && errno
!= ENOTEMPTY
)
336 warn("-delete: rmdir(%s)", entry
->fts_path
);
338 if (unlink(entry
->fts_accpath
) < 0)
339 warn("-delete: unlink(%s)", entry
->fts_path
);
347 c_delete(option
, argvp
)
352 ftsoptions
&= ~FTS_NOSTAT
; /* no optimise */
353 ftsoptions
|= FTS_PHYSICAL
; /* disable -follow */
354 ftsoptions
&= ~FTS_LOGICAL
; /* disable -follow */
355 isoutput
= 1; /* possible output */
356 isdepth
= 1; /* -depth implied */
358 return palloc(option
);
363 * -depth functions --
365 * Always true, causes descent of the directory hierarchy to be done
366 * so that all entries in a directory are acted on before the directory
370 f_always_true(plan
, entry
)
378 c_depth(option
, argvp
)
384 return palloc(option
);
388 * -empty functions --
390 * True if the file or directory is empty
397 if (S_ISREG(entry
->fts_statp
->st_mode
) &&
398 entry
->fts_statp
->st_size
== 0)
400 if (S_ISDIR(entry
->fts_statp
->st_mode
)) {
406 dir
= opendir(entry
->fts_accpath
);
408 err(1, "%s", entry
->fts_accpath
);
409 for (dp
= readdir(dir
); dp
; dp
= readdir(dir
))
410 if (dp
->d_name
[0] != '.' ||
411 (dp
->d_name
[1] != '\0' &&
412 (dp
->d_name
[1] != '.' || dp
->d_name
[2] != '\0'))) {
423 c_empty(option
, argvp
)
427 ftsoptions
&= ~FTS_NOSTAT
;
429 return palloc(option
);
433 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
435 * True if the executed utility returns a zero value as exit status.
436 * The end of the primary expression is delimited by a semicolon. If
437 * "{}" occurs anywhere, it gets replaced by the current pathname,
438 * or, in the case of -execdir, the current basename (filename
439 * without leading directory prefix). For -exec and -ok,
440 * the current directory for the execution of utility is the same as
441 * the current directory when the find utility was started, whereas
442 * for -execdir, it is the directory the file resides in.
444 * The primary -ok differs from -exec in that it requests affirmation
445 * of the user before executing the utility.
457 static char **plus_path
= NULL
;
458 static int plus_path_cnt
= 0;
459 static unsigned long exec_after
= 0;
460 static unsigned long path_so_far
= 0;
464 /* XXX - if file/dir ends in '/' this will not work -- can it? */
465 if ((plan
->flags
& F_EXECDIR
) &&
466 (file
= strrchr(entry
->fts_path
, '/'))) {
469 file
= entry
->fts_path
;
472 if (plan
->flags
& F_CLEANUP
) {
473 plus_path
= realloc(plus_path
, sizeof(char *) * ++plus_path_cnt
);
474 if (!plus_path
) errx(1, "out of memory");
475 plus_path
[plus_path_cnt
-1] = strdup(file
);
476 if (!plus_path
[plus_path_cnt
-1]) errx(1, "out of memory");
478 path_so_far
+= sizeof(char *) + strlen(file
);
481 size_t l
= sizeof(exec_after
);
483 sysctlbyname("kern.argmax", &exec_after
, &l
, NULL
, NULL
);
485 /* Something conservitave */
486 exec_after
= 16 * 1024;
489 if (path_so_far
>= exec_after
) {
490 return f_exec(plan
, NULL
);
496 if (!plus_path
) return 1;
497 saved_ex
= plan
->p_un
.ex
;
500 for(cnt
= 0; plan
->e_argv
[cnt
]; ++cnt
) {
501 if (plan
->e_len
[cnt
]) {
502 new_len
+= plus_path_cnt
;
509 plan
->e_argv
= malloc(sizeof(char *) * new_len
);
510 if (!plan
->e_argv
) errx(1, "out of memory");
512 for(ocnt
= cnt
= 0; saved_ex
._e_argv
[ocnt
]; ++ocnt
) {
513 if (saved_ex
._e_len
[ocnt
]) {
515 for(ppi
= 0; ppi
< plus_path_cnt
; ++ppi
) {
516 plan
->e_argv
[cnt
++] = plus_path
[ppi
];
519 memmove(plan
->e_argv
+ cnt
, *plus_path
,
520 sizeof(char *) * plus_path_cnt
);
521 cnt
+= plus_path_cnt
;
524 plan
->e_argv
[cnt
++] = saved_ex
._e_argv
[ocnt
];
527 plan
->e_argv
[cnt
++] = NULL
;
530 if (!(plan
->flags
& F_CLEANUP
)) for (cnt
= 0; plan
->e_argv
[cnt
]; ++cnt
)
531 if (plan
->e_len
[cnt
])
532 brace_subst(plan
->e_orig
[cnt
], &plan
->e_argv
[cnt
],
533 file
, plan
->e_len
[cnt
]);
535 if ((plan
->flags
& F_NEEDOK
) && !queryuser(plan
->e_argv
))
538 /* make sure find output is interspersed correctly with subprocesses */
542 switch (pid
= fork()) {
547 /* change dir back from where we started */
548 if (!(plan
->flags
& F_EXECDIR
) && fchdir(dotfd
)) {
552 execvp(plan
->e_argv
[0], plan
->e_argv
);
553 warn("%s", plan
->e_argv
[0]);
556 pid
= waitpid(pid
, &status
, 0);
559 for(i
= 0; i
< plus_path_cnt
; ++i
) {
565 plan
->p_un
.ex
= saved_ex
;
567 if (WIFEXITED(status
) && WEXITSTATUS(status
)) {
568 exit(WEXITSTATUS(status
));
571 return (pid
!= -1 && WIFEXITED(status
) && !WEXITSTATUS(status
));
575 * c_exec, c_execdir, c_ok --
576 * build three parallel arrays, one with pointers to the strings passed
577 * on the command line, one with (possibly duplicated) pointers to the
578 * argv array, and one with integer values that are lengths of the
579 * strings, but also flags meaning that the string has to be massaged.
582 c_exec(option
, argvp
)
586 PLAN
*new; /* node returned */
588 register char **argv
, **ap
, *p
;
591 /* XXX - was in c_execdir, but seems unnecessary!?
592 ftsoptions &= ~FTS_NOSTAT;
596 /* XXX - this is a change from the previous coding */
597 new = palloc(option
);
599 for (ap
= argv
= *argvp
;; ++ap
) {
602 "%s: no terminating \";\"", option
->name
);
603 if (**ap
== '{' && ap
[0][1] == '}' && ap
[0][2] == '\0') {
604 plus_armed
= COMPAT_MODE("bin/find", "unix2003");
610 if (plus_armed
&& **ap
== '+') {
611 new->flags
|= F_CLEANUP
;
617 cnt
= ap
- *argvp
+ 1;
618 new->e_argv
= (char **)emalloc((u_int
)cnt
* sizeof(char *));
619 new->e_orig
= (char **)emalloc((u_int
)cnt
* sizeof(char *));
620 new->e_len
= (int *)emalloc((u_int
)cnt
* sizeof(int));
622 for (argv
= *argvp
, cnt
= 0; argv
< ap
; ++argv
, ++cnt
) {
623 new->e_orig
[cnt
] = *argv
;
624 for (p
= *argv
; *p
; ++p
)
625 if (p
[0] == '{' && p
[1] == '}') {
626 new->e_argv
[cnt
] = emalloc((u_int
)MAXPATHLEN
);
627 new->e_len
[cnt
] = MAXPATHLEN
;
631 new->e_argv
[cnt
] = *argv
;
635 new->e_argv
[cnt
] = new->e_orig
[cnt
] = NULL
;
648 flags
= entry
->fts_statp
->st_flags
;
649 if (plan
->flags
& F_ATLEAST
)
650 return (flags
| plan
->fl_flags
) == flags
&&
651 !(flags
& plan
->fl_notflags
);
652 else if (plan
->flags
& F_ANY
)
653 return (flags
& plan
->fl_flags
) ||
654 (flags
| plan
->fl_notflags
) != flags
;
656 return flags
== plan
->fl_flags
&&
657 !(plan
->fl_flags
& plan
->fl_notflags
);
661 c_flags(option
, argvp
)
667 u_long flags
, notflags
;
669 flags_str
= nextarg(option
, argvp
);
670 ftsoptions
&= ~FTS_NOSTAT
;
672 new = palloc(option
);
674 if (*flags_str
== '-') {
675 new->flags
|= F_ATLEAST
;
677 } else if (*flags_str
== '+') {
681 if (strtofflags(&flags_str
, &flags
, ¬flags
) == 1)
682 errx(1, "%s: %s: illegal flags string", option
->name
, flags_str
);
684 new->fl_flags
= flags
;
685 new->fl_notflags
= notflags
;
690 * -follow functions --
692 * Always true, causes symbolic links to be followed on a global
696 c_follow(option
, argvp
)
700 ftsoptions
&= ~FTS_PHYSICAL
;
701 ftsoptions
|= FTS_LOGICAL
;
703 return palloc(option
);
707 * -fstype functions --
709 * True if the file is of a certain type.
712 f_fstype(plan
, entry
)
716 static dev_t curdev
; /* need a guaranteed illegal dev value */
717 static int first
= 1;
719 static int val_type
, val_flags
;
722 /* Only check when we cross mount point. */
723 if (first
|| curdev
!= entry
->fts_statp
->st_dev
) {
724 curdev
= entry
->fts_statp
->st_dev
;
727 * Statfs follows symlinks; find wants the link's file system,
728 * not where it points.
730 if (entry
->fts_info
== FTS_SL
||
731 entry
->fts_info
== FTS_SLNONE
) {
732 if ((p
= strrchr(entry
->fts_accpath
, '/')) != NULL
)
735 p
= entry
->fts_accpath
;
743 if (statfs(entry
->fts_accpath
, &sb
))
744 err(1, "%s", entry
->fts_accpath
);
754 * Further tests may need both of these values, so
755 * always copy both of them.
757 val_flags
= sb
.f_flags
;
758 val_type
= sb
.f_type
;
760 switch (plan
->flags
& F_MTMASK
) {
762 return (val_flags
& plan
->mt_data
) != 0;
764 return (val_type
== plan
->mt_data
);
770 #if !defined(__NetBSD__)
772 c_fstype(option
, argvp
)
780 fsname
= nextarg(option
, argvp
);
781 ftsoptions
&= ~FTS_NOSTAT
;
783 new = palloc(option
);
786 * Check first for a filesystem name.
788 if (getvfsbyname(fsname
, &vfc
) == 0) {
789 new->flags
|= F_MTTYPE
;
790 new->mt_data
= vfc
.vfc_typenum
;
796 if (!strcmp(fsname
, "local")) {
797 new->flags
|= F_MTFLAG
;
798 new->mt_data
= MNT_LOCAL
;
803 if (!strcmp(fsname
, "rdonly")) {
804 new->flags
|= F_MTFLAG
;
805 new->mt_data
= MNT_RDONLY
;
811 errx(1, "%s: unknown file type", fsname
);
814 #endif /* __NetBSD__ */
817 * -group gname functions --
819 * True if the file belongs to the group gname. If gname is numeric and
820 * an equivalent of the getgrnam() function does not return a valid group
821 * name, gname is taken as a group ID.
828 return entry
->fts_statp
->st_gid
== plan
->g_data
;
832 c_group(option
, argvp
)
841 gname
= nextarg(option
, argvp
);
842 ftsoptions
&= ~FTS_NOSTAT
;
847 if (gid
== 0 && gname
[0] != '0')
848 errx(1, "%s: %s: no such group", option
->name
, gname
);
852 new = palloc(option
);
858 * -inum n functions --
860 * True if the file has inode # n.
867 COMPARE(entry
->fts_statp
->st_ino
, plan
->i_data
);
871 c_inum(option
, argvp
)
878 inum_str
= nextarg(option
, argvp
);
879 ftsoptions
&= ~FTS_NOSTAT
;
881 new = palloc(option
);
882 new->i_data
= find_parsenum(new, option
->name
, inum_str
, NULL
);
887 * -links n functions --
889 * True if the file has n links.
896 COMPARE(entry
->fts_statp
->st_nlink
, plan
->l_data
);
900 c_links(option
, argvp
)
907 nlinks
= nextarg(option
, argvp
);
908 ftsoptions
&= ~FTS_NOSTAT
;
910 new = palloc(option
);
911 new->l_data
= (nlink_t
)find_parsenum(new, option
->name
, nlinks
, NULL
);
918 * Always true - prints the current entry to stdout in "ls" format.
925 printlong(entry
->fts_path
, entry
->fts_accpath
, entry
->fts_statp
);
934 ftsoptions
&= ~FTS_NOSTAT
;
937 return palloc(option
);
943 * True if the basename of the filename being examined
944 * matches pattern using Pattern Matching Notation S3.14
951 return !fnmatch(plan
->c_data
, entry
->fts_name
,
952 plan
->flags
& F_IGNCASE
? FNM_CASEFOLD
: 0);
956 c_name(option
, argvp
)
963 pattern
= nextarg(option
, argvp
);
964 new = palloc(option
);
965 new->c_data
= pattern
;
970 * -newer file functions --
972 * True if the current file has been modified more recently
973 * then the modification time of the file named by the pathname
981 if (plan
->flags
& F_TIME_C
)
982 return entry
->fts_statp
->st_ctime
> plan
->t_data
;
983 else if (plan
->flags
& F_TIME_A
)
984 return entry
->fts_statp
->st_atime
> plan
->t_data
;
986 return entry
->fts_statp
->st_mtime
> plan
->t_data
;
990 c_newer(option
, argvp
)
998 fn_or_tspec
= nextarg(option
, argvp
);
999 ftsoptions
&= ~FTS_NOSTAT
;
1001 new = palloc(option
);
1002 /* compare against what */
1003 if (option
->flags
& F_TIME2_T
) {
1004 new->t_data
= get_date(fn_or_tspec
, (struct timeb
*) 0);
1005 if (new->t_data
== (time_t) -1)
1006 errx(1, "Can't parse date/time: %s", fn_or_tspec
);
1008 if (stat(fn_or_tspec
, &sb
))
1009 err(1, "%s", fn_or_tspec
);
1010 if (option
->flags
& F_TIME2_C
)
1011 new->t_data
= sb
.st_ctime
;
1012 else if (option
->flags
& F_TIME2_A
)
1013 new->t_data
= sb
.st_atime
;
1015 new->t_data
= sb
.st_mtime
;
1021 * -nogroup functions --
1023 * True if file belongs to a user ID for which the equivalent
1024 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1027 f_nogroup(plan
, entry
)
1031 return group_from_gid(entry
->fts_statp
->st_gid
, 1) == NULL
;
1035 c_nogroup(option
, argvp
)
1039 ftsoptions
&= ~FTS_NOSTAT
;
1041 return palloc(option
);
1045 * -nouser functions --
1047 * True if file belongs to a user ID for which the equivalent
1048 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1051 f_nouser(plan
, entry
)
1055 return user_from_uid(entry
->fts_statp
->st_uid
, 1) == NULL
;
1059 c_nouser(option
, argvp
)
1063 ftsoptions
&= ~FTS_NOSTAT
;
1065 return palloc(option
);
1069 * -path functions --
1071 * True if the path of the filename being examined
1072 * matches pattern using Pattern Matching Notation S3.14
1079 return !fnmatch(plan
->c_data
, entry
->fts_path
,
1080 plan
->flags
& F_IGNCASE
? FNM_CASEFOLD
: 0);
1083 /* c_path is the same as c_name */
1086 * -perm functions --
1088 * The mode argument is used to represent file mode bits. If it starts
1089 * with a leading digit, it's treated as an octal mode, otherwise as a
1099 mode
= entry
->fts_statp
->st_mode
&
1100 (S_ISUID
|S_ISGID
|S_ISTXT
|S_IRWXU
|S_IRWXG
|S_IRWXO
);
1101 if (plan
->flags
& F_ATLEAST
)
1102 return (plan
->m_data
| mode
) == mode
;
1103 else if (plan
->flags
& F_ANY
)
1104 return (mode
& plan
->m_data
);
1106 return mode
== plan
->m_data
;
1111 c_perm(option
, argvp
)
1119 perm
= nextarg(option
, argvp
);
1120 ftsoptions
&= ~FTS_NOSTAT
;
1122 new = palloc(option
);
1125 new->flags
|= F_ATLEAST
;
1127 } else if (*perm
== '+') {
1128 if ((set
= setmode(perm
+1)) != NULL
) {
1129 new->flags
|= F_ANY
;
1135 if ((set
= setmode(perm
)) == NULL
)
1136 errx(1, "%s: %s: illegal mode string", option
->name
, perm
);
1138 new->m_data
= getmode(set
, 0);
1144 * -print functions --
1146 * Always true, causes the current pathame to be written to
1150 f_print(plan
, entry
)
1154 (void)puts(entry
->fts_path
);
1159 c_print(option
, argvp
)
1165 return palloc(option
);
1169 * -print0 functions --
1171 * Always true, causes the current pathame to be written to
1172 * standard output followed by a NUL character
1175 f_print0(plan
, entry
)
1179 fputs(entry
->fts_path
, stdout
);
1180 fputc('\0', stdout
);
1184 /* c_print0 is the same as c_print */
1187 * -prune functions --
1189 * Prune a portion of the hierarchy.
1192 f_prune(plan
, entry
)
1198 if (fts_set(tree
, entry
, FTS_SKIP
))
1199 err(1, "%s", entry
->fts_path
);
1203 /* c_prune == c_simple */
1206 * -regex functions --
1208 * True if the whole path of the file matches pattern using
1209 * regular expression.
1212 f_regex(plan
, entry
)
1221 char errbuf
[LINE_MAX
];
1224 pre
= plan
->re_data
;
1225 str
= entry
->fts_path
;
1232 errcode
= regexec(pre
, str
, 1, &pmatch
, REG_STARTEND
);
1234 if (errcode
!= 0 && errcode
!= REG_NOMATCH
) {
1235 regerror(errcode
, pre
, errbuf
, sizeof errbuf
);
1237 plan
->flags
& F_IGNCASE
? "-iregex" : "-regex", errbuf
);
1240 if (errcode
== 0 && pmatch
.rm_so
== 0 && pmatch
.rm_eo
== len
)
1247 c_regex(option
, argvp
)
1255 char errbuf
[LINE_MAX
];
1257 if ((pre
= malloc(sizeof(regex_t
))) == NULL
)
1260 pattern
= nextarg(option
, argvp
);
1262 if ((errcode
= regcomp(pre
, pattern
,
1263 regexp_flags
| (option
->flags
& F_IGNCASE
? REG_ICASE
: 0))) != 0) {
1264 regerror(errcode
, pre
, errbuf
, sizeof errbuf
);
1265 errx(1, "%s: %s: %s",
1266 option
->flags
& F_IGNCASE
? "-iregex" : "-regex",
1270 new = palloc(option
);
1276 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */
1279 c_simple(option
, argvp
)
1283 return palloc(option
);
1287 * -size n[c] functions --
1289 * True if the file size in bytes, divided by an implementation defined
1290 * value and rounded up to the next integer, is n. If n is followed by
1291 * a c, the size is in bytes.
1293 #define FIND_SIZE 512
1294 static int divsize
= 1;
1303 size
= divsize
? (entry
->fts_statp
->st_size
+ FIND_SIZE
- 1) /
1304 FIND_SIZE
: entry
->fts_statp
->st_size
;
1305 COMPARE(size
, plan
->o_data
);
1309 c_size(option
, argvp
)
1317 size_str
= nextarg(option
, argvp
);
1318 ftsoptions
&= ~FTS_NOSTAT
;
1320 new = palloc(option
);
1322 new->o_data
= find_parsenum(new, option
->name
, size_str
, &endch
);
1329 * -type c functions --
1331 * True if the type of the file is c, where c is b, c, d, p, f or w
1332 * for block special file, character special file, directory, FIFO,
1333 * regular file or whiteout respectively.
1340 return (entry
->fts_statp
->st_mode
& S_IFMT
) == plan
->m_data
;
1344 c_type(option
, argvp
)
1352 typestring
= nextarg(option
, argvp
);
1353 ftsoptions
&= ~FTS_NOSTAT
;
1355 switch (typestring
[0]) {
1380 ftsoptions
|= FTS_WHITEOUT
;
1382 #endif /* FTS_WHITEOUT */
1384 errx(1, "%s: %s: unknown type", option
->name
, typestring
);
1387 new = palloc(option
);
1393 * -user uname functions --
1395 * True if the file belongs to the user uname. If uname is numeric and
1396 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1397 * return a valid user name, uname is taken as a user ID.
1404 return entry
->fts_statp
->st_uid
== plan
->u_data
;
1408 c_user(option
, argvp
)
1417 username
= nextarg(option
, argvp
);
1418 ftsoptions
&= ~FTS_NOSTAT
;
1420 p
= getpwnam(username
);
1422 uid
= atoi(username
);
1423 if (uid
== 0 && username
[0] != '0')
1424 errx(1, "%s: %s: no such user", option
->name
, username
);
1428 new = palloc(option
);
1434 * -xdev functions --
1436 * Always true, causes find not to decend past directories that have a
1437 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1440 c_xdev(option
, argvp
)
1444 ftsoptions
|= FTS_XDEV
;
1446 return palloc(option
);
1450 * ( expression ) functions --
1452 * True if expression is true.
1460 register int state
= 0;
1462 for (p
= plan
->p_data
[0];
1463 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1468 * f_openparen and f_closeparen nodes are temporary place markers. They are
1469 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1470 * to a f_expr node containing the expression and the ')' node is discarded.
1471 * The functions themselves are only used as constants.
1475 f_openparen(plan
, entry
)
1483 f_closeparen(plan
, entry
)
1490 /* c_openparen == c_simple */
1491 /* c_closeparen == c_simple */
1494 * AND operator. Since AND is implicit, no node is allocated.
1497 c_and(option
, argvp
)
1505 * ! expression functions --
1507 * Negation of a primary; the unary NOT operator.
1515 register int state
= 0;
1517 for (p
= plan
->p_data
[0];
1518 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1522 /* c_not == c_simple */
1525 * expression -o expression functions --
1527 * Alternation of primaries; the OR operator. The second expression is
1528 * not evaluated if the first expression is true.
1536 register int state
= 0;
1538 for (p
= plan
->p_data
[0];
1539 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1544 for (p
= plan
->p_data
[1];
1545 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1549 /* c_or == c_simple */