]>
git.saurik.com Git - apple/shell_cmds.git/blob - find/function.c
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>
68 time_t get_date
__P((char *date
, struct timeb
*now
));
70 #define COMPARE(a, b) { \
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 * Check that another argument still exists, return a pointer to it,
143 * and increment the argument vector pointer.
146 nextarg(option
, argvp
)
152 if ((arg
= **argvp
) == 0)
153 errx(1, "%s: requires additional arguments", option
->name
);
159 * The value of n for the inode times (atime, ctime, and mtime) is a range,
160 * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with
161 * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
162 * user wanted. Correct so that -1 is "less than 1".
164 #define TIME_CORRECT(p) \
165 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
169 * -[acm]min n functions --
171 * True if the difference between the
172 * file access time (-amin)
173 * last change of file status information (-cmin)
174 * file modification time (-mmin)
175 * and the current time is n min periods.
184 if (plan
->flags
& F_TIME_C
) {
185 COMPARE((now
- entry
->fts_statp
->st_ctime
+
186 60 - 1) / 60, plan
->t_data
);
187 } else if (plan
->flags
& F_TIME_A
) {
188 COMPARE((now
- entry
->fts_statp
->st_atime
+
189 60 - 1) / 60, plan
->t_data
);
191 COMPARE((now
- entry
->fts_statp
->st_mtime
+
192 60 - 1) / 60, plan
->t_data
);
197 c_Xmin(option
, argvp
)
204 nmins
= nextarg(option
, argvp
);
205 ftsoptions
&= ~FTS_NOSTAT
;
207 new = palloc(option
);
208 new->t_data
= find_parsenum(new, option
->name
, nmins
, NULL
);
214 * -[acm]time n functions --
216 * True if the difference between the
217 * file access time (-atime)
218 * last change of file status information (-ctime)
219 * file modification time (-mtime)
220 * and the current time is n 24 hour periods.
230 if (plan
->flags
& F_TIME_C
) {
231 COMPARE((now
- entry
->fts_statp
->st_ctime
+
232 86400 - 1) / 86400, plan
->t_data
);
233 } else if (plan
->flags
& F_TIME_A
) {
234 COMPARE((now
- entry
->fts_statp
->st_atime
+
235 86400 - 1) / 86400, plan
->t_data
);
237 COMPARE((now
- entry
->fts_statp
->st_mtime
+
238 86400 - 1) / 86400, plan
->t_data
);
243 c_Xtime(option
, argvp
)
250 ndays
= nextarg(option
, argvp
);
251 ftsoptions
&= ~FTS_NOSTAT
;
253 new = palloc(option
);
254 new->t_data
= find_parsenum(new, option
->name
, ndays
, NULL
);
260 * -maxdepth/-mindepth n functions --
262 * Does the same as -prune if the level of the current file is
263 * greater/less than the specified maximum/minimum depth.
265 * Note that -maxdepth and -mindepth are handled specially in
266 * find_execute() so their f_* functions are set to f_always_true().
269 c_mXXdepth(option
, argvp
)
276 dstr
= nextarg(option
, argvp
);
278 /* all other errors handled by find_parsenum() */
279 errx(1, "%s: %s: value must be positive", option
->name
, dstr
);
281 new = palloc(option
);
282 if (option
->flags
& F_MAXDEPTH
)
283 maxdepth
= find_parsenum(new, option
->name
, dstr
, NULL
);
285 mindepth
= find_parsenum(new, option
->name
, dstr
, NULL
);
290 * -delete functions --
292 * True always. Makes its best shot and continues on regardless.
295 f_delete(plan
, entry
)
299 /* ignore these from fts */
300 if (strcmp(entry
->fts_accpath
, ".") == 0 ||
301 strcmp(entry
->fts_accpath
, "..") == 0)
305 if (isdepth
== 0 || /* depth off */
306 (ftsoptions
& FTS_NOSTAT
) || /* not stat()ing */
307 !(ftsoptions
& FTS_PHYSICAL
) || /* physical off */
308 (ftsoptions
& FTS_LOGICAL
)) /* or finally, logical on */
309 errx(1, "-delete: insecure options got turned on");
311 /* Potentially unsafe - do not accept relative paths whatsoever */
312 if (strchr(entry
->fts_accpath
, '/') != NULL
)
313 errx(1, "-delete: %s: relative path potentially not safe",
316 /* Turn off user immutable bits if running as root */
317 if ((entry
->fts_statp
->st_flags
& (UF_APPEND
|UF_IMMUTABLE
)) &&
318 !(entry
->fts_statp
->st_flags
& (SF_APPEND
|SF_IMMUTABLE
)) &&
320 chflags(entry
->fts_accpath
,
321 entry
->fts_statp
->st_flags
&= ~(UF_APPEND
|UF_IMMUTABLE
));
323 /* rmdir directories, unlink everything else */
324 if (S_ISDIR(entry
->fts_statp
->st_mode
)) {
325 if (rmdir(entry
->fts_accpath
) < 0 && errno
!= ENOTEMPTY
)
326 warn("-delete: rmdir(%s)", entry
->fts_path
);
328 if (unlink(entry
->fts_accpath
) < 0)
329 warn("-delete: unlink(%s)", entry
->fts_path
);
337 c_delete(option
, argvp
)
342 ftsoptions
&= ~FTS_NOSTAT
; /* no optimise */
343 ftsoptions
|= FTS_PHYSICAL
; /* disable -follow */
344 ftsoptions
&= ~FTS_LOGICAL
; /* disable -follow */
345 isoutput
= 1; /* possible output */
346 isdepth
= 1; /* -depth implied */
348 return palloc(option
);
353 * -depth functions --
355 * Always true, causes descent of the directory hierarchy to be done
356 * so that all entries in a directory are acted on before the directory
360 f_always_true(plan
, entry
)
368 c_depth(option
, argvp
)
374 return palloc(option
);
378 * -empty functions --
380 * True if the file or directory is empty
387 if (S_ISREG(entry
->fts_statp
->st_mode
) &&
388 entry
->fts_statp
->st_size
== 0)
390 if (S_ISDIR(entry
->fts_statp
->st_mode
)) {
396 dir
= opendir(entry
->fts_accpath
);
398 err(1, "%s", entry
->fts_accpath
);
399 for (dp
= readdir(dir
); dp
; dp
= readdir(dir
))
400 if (dp
->d_name
[0] != '.' ||
401 (dp
->d_name
[1] != '\0' &&
402 (dp
->d_name
[1] != '.' || dp
->d_name
[2] != '\0'))) {
413 c_empty(option
, argvp
)
417 ftsoptions
&= ~FTS_NOSTAT
;
419 return palloc(option
);
423 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
425 * True if the executed utility returns a zero value as exit status.
426 * The end of the primary expression is delimited by a semicolon. If
427 * "{}" occurs anywhere, it gets replaced by the current pathname,
428 * or, in the case of -execdir, the current basename (filename
429 * without leading directory prefix). For -exec and -ok,
430 * the current directory for the execution of utility is the same as
431 * the current directory when the find utility was started, whereas
432 * for -execdir, it is the directory the file resides in.
434 * The primary -ok differs from -exec in that it requests affirmation
435 * of the user before executing the utility.
448 /* XXX - if file/dir ends in '/' this will not work -- can it? */
449 if ((plan
->flags
& F_EXECDIR
) && \
450 (file
= strrchr(entry
->fts_path
, '/')))
453 file
= entry
->fts_path
;
455 for (cnt
= 0; plan
->e_argv
[cnt
]; ++cnt
)
456 if (plan
->e_len
[cnt
])
457 brace_subst(plan
->e_orig
[cnt
], &plan
->e_argv
[cnt
],
458 file
, plan
->e_len
[cnt
]);
460 if ((plan
->flags
& F_NEEDOK
) && !queryuser(plan
->e_argv
))
463 /* make sure find output is interspersed correctly with subprocesses */
467 switch (pid
= fork()) {
472 /* change dir back from where we started */
473 if (!(plan
->flags
& F_EXECDIR
) && fchdir(dotfd
)) {
477 execvp(plan
->e_argv
[0], plan
->e_argv
);
478 warn("%s", plan
->e_argv
[0]);
481 pid
= waitpid(pid
, &status
, 0);
482 return (pid
!= -1 && WIFEXITED(status
) && !WEXITSTATUS(status
));
486 * c_exec, c_execdir, c_ok --
487 * build three parallel arrays, one with pointers to the strings passed
488 * on the command line, one with (possibly duplicated) pointers to the
489 * argv array, and one with integer values that are lengths of the
490 * strings, but also flags meaning that the string has to be massaged.
493 c_exec(option
, argvp
)
497 PLAN
*new; /* node returned */
499 register char **argv
, **ap
, *p
;
501 /* XXX - was in c_execdir, but seems unnecessary!?
502 ftsoptions &= ~FTS_NOSTAT;
506 /* XXX - this is a change from the previous coding */
507 new = palloc(option
);
509 for (ap
= argv
= *argvp
;; ++ap
) {
512 "%s: no terminating \";\"", option
->name
);
517 cnt
= ap
- *argvp
+ 1;
518 new->e_argv
= (char **)emalloc((u_int
)cnt
* sizeof(char *));
519 new->e_orig
= (char **)emalloc((u_int
)cnt
* sizeof(char *));
520 new->e_len
= (int *)emalloc((u_int
)cnt
* sizeof(int));
522 for (argv
= *argvp
, cnt
= 0; argv
< ap
; ++argv
, ++cnt
) {
523 new->e_orig
[cnt
] = *argv
;
524 for (p
= *argv
; *p
; ++p
)
525 if (p
[0] == '{' && p
[1] == '}') {
526 new->e_argv
[cnt
] = emalloc((u_int
)MAXPATHLEN
);
527 new->e_len
[cnt
] = MAXPATHLEN
;
531 new->e_argv
[cnt
] = *argv
;
535 new->e_argv
[cnt
] = new->e_orig
[cnt
] = NULL
;
548 flags
= entry
->fts_statp
->st_flags
;
549 if (plan
->flags
& F_ATLEAST
)
550 return (flags
| plan
->fl_flags
) == flags
&&
551 !(flags
& plan
->fl_notflags
);
552 else if (plan
->flags
& F_ANY
)
553 return (flags
& plan
->fl_flags
) ||
554 (flags
| plan
->fl_notflags
) != flags
;
556 return flags
== plan
->fl_flags
&&
557 !(plan
->fl_flags
& plan
->fl_notflags
);
561 c_flags(option
, argvp
)
567 u_long flags
, notflags
;
569 flags_str
= nextarg(option
, argvp
);
570 ftsoptions
&= ~FTS_NOSTAT
;
572 new = palloc(option
);
574 if (*flags_str
== '-') {
575 new->flags
|= F_ATLEAST
;
577 } else if (*flags_str
== '+') {
581 if (strtofflags(&flags_str
, &flags
, ¬flags
) == 1)
582 errx(1, "%s: %s: illegal flags string", option
->name
, flags_str
);
584 new->fl_flags
= flags
;
585 new->fl_notflags
= notflags
;
590 * -follow functions --
592 * Always true, causes symbolic links to be followed on a global
596 c_follow(option
, argvp
)
600 ftsoptions
&= ~FTS_PHYSICAL
;
601 ftsoptions
|= FTS_LOGICAL
;
603 return palloc(option
);
607 * -fstype functions --
609 * True if the file is of a certain type.
612 f_fstype(plan
, entry
)
616 static dev_t curdev
; /* need a guaranteed illegal dev value */
617 static int first
= 1;
619 static int val_type
, val_flags
;
622 /* Only check when we cross mount point. */
623 if (first
|| curdev
!= entry
->fts_statp
->st_dev
) {
624 curdev
= entry
->fts_statp
->st_dev
;
627 * Statfs follows symlinks; find wants the link's file system,
628 * not where it points.
630 if (entry
->fts_info
== FTS_SL
||
631 entry
->fts_info
== FTS_SLNONE
) {
632 if ((p
= strrchr(entry
->fts_accpath
, '/')) != NULL
)
635 p
= entry
->fts_accpath
;
643 if (statfs(entry
->fts_accpath
, &sb
))
644 err(1, "%s", entry
->fts_accpath
);
654 * Further tests may need both of these values, so
655 * always copy both of them.
657 val_flags
= sb
.f_flags
;
658 val_type
= sb
.f_type
;
660 switch (plan
->flags
& F_MTMASK
) {
662 return (val_flags
& plan
->mt_data
) != 0;
664 return (val_type
== plan
->mt_data
);
670 #if !defined(__NetBSD__)
672 c_fstype(option
, argvp
)
680 fsname
= nextarg(option
, argvp
);
681 ftsoptions
&= ~FTS_NOSTAT
;
683 new = palloc(option
);
686 * Check first for a filesystem name.
688 if (getvfsbyname(fsname
, &vfc
) == 0) {
689 new->flags
|= F_MTTYPE
;
690 new->mt_data
= vfc
.vfc_typenum
;
696 if (!strcmp(fsname
, "local")) {
697 new->flags
|= F_MTFLAG
;
698 new->mt_data
= MNT_LOCAL
;
703 if (!strcmp(fsname
, "rdonly")) {
704 new->flags
|= F_MTFLAG
;
705 new->mt_data
= MNT_RDONLY
;
711 errx(1, "%s: unknown file type", fsname
);
714 #endif /* __NetBSD__ */
717 * -group gname functions --
719 * True if the file belongs to the group gname. If gname is numeric and
720 * an equivalent of the getgrnam() function does not return a valid group
721 * name, gname is taken as a group ID.
728 return entry
->fts_statp
->st_gid
== plan
->g_data
;
732 c_group(option
, argvp
)
741 gname
= nextarg(option
, argvp
);
742 ftsoptions
&= ~FTS_NOSTAT
;
747 if (gid
== 0 && gname
[0] != '0')
748 errx(1, "%s: %s: no such group", option
->name
, gname
);
752 new = palloc(option
);
758 * -inum n functions --
760 * True if the file has inode # n.
767 COMPARE(entry
->fts_statp
->st_ino
, plan
->i_data
);
771 c_inum(option
, argvp
)
778 inum_str
= nextarg(option
, argvp
);
779 ftsoptions
&= ~FTS_NOSTAT
;
781 new = palloc(option
);
782 new->i_data
= find_parsenum(new, option
->name
, inum_str
, NULL
);
787 * -links n functions --
789 * True if the file has n links.
796 COMPARE(entry
->fts_statp
->st_nlink
, plan
->l_data
);
800 c_links(option
, argvp
)
807 nlinks
= nextarg(option
, argvp
);
808 ftsoptions
&= ~FTS_NOSTAT
;
810 new = palloc(option
);
811 new->l_data
= (nlink_t
)find_parsenum(new, option
->name
, nlinks
, NULL
);
818 * Always true - prints the current entry to stdout in "ls" format.
825 printlong(entry
->fts_path
, entry
->fts_accpath
, entry
->fts_statp
);
834 ftsoptions
&= ~FTS_NOSTAT
;
837 return palloc(option
);
843 * True if the basename of the filename being examined
844 * matches pattern using Pattern Matching Notation S3.14
851 return !fnmatch(plan
->c_data
, entry
->fts_name
,
852 plan
->flags
& F_IGNCASE
? FNM_CASEFOLD
: 0);
856 c_name(option
, argvp
)
863 pattern
= nextarg(option
, argvp
);
864 new = palloc(option
);
865 new->c_data
= pattern
;
870 * -newer file functions --
872 * True if the current file has been modified more recently
873 * then the modification time of the file named by the pathname
881 if (plan
->flags
& F_TIME_C
)
882 return entry
->fts_statp
->st_ctime
> plan
->t_data
;
883 else if (plan
->flags
& F_TIME_A
)
884 return entry
->fts_statp
->st_atime
> plan
->t_data
;
886 return entry
->fts_statp
->st_mtime
> plan
->t_data
;
890 c_newer(option
, argvp
)
898 fn_or_tspec
= nextarg(option
, argvp
);
899 ftsoptions
&= ~FTS_NOSTAT
;
901 new = palloc(option
);
902 /* compare against what */
903 if (option
->flags
& F_TIME2_T
) {
904 new->t_data
= get_date(fn_or_tspec
, (struct timeb
*) 0);
905 if (new->t_data
== (time_t) -1)
906 errx(1, "Can't parse date/time: %s", fn_or_tspec
);
908 if (stat(fn_or_tspec
, &sb
))
909 err(1, "%s", fn_or_tspec
);
910 if (option
->flags
& F_TIME2_C
)
911 new->t_data
= sb
.st_ctime
;
912 else if (option
->flags
& F_TIME2_A
)
913 new->t_data
= sb
.st_atime
;
915 new->t_data
= sb
.st_mtime
;
921 * -nogroup functions --
923 * True if file belongs to a user ID for which the equivalent
924 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
927 f_nogroup(plan
, entry
)
931 return group_from_gid(entry
->fts_statp
->st_gid
, 1) == NULL
;
935 c_nogroup(option
, argvp
)
939 ftsoptions
&= ~FTS_NOSTAT
;
941 return palloc(option
);
945 * -nouser functions --
947 * True if file belongs to a user ID for which the equivalent
948 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
951 f_nouser(plan
, entry
)
955 return user_from_uid(entry
->fts_statp
->st_uid
, 1) == NULL
;
959 c_nouser(option
, argvp
)
963 ftsoptions
&= ~FTS_NOSTAT
;
965 return palloc(option
);
971 * True if the path of the filename being examined
972 * matches pattern using Pattern Matching Notation S3.14
979 return !fnmatch(plan
->c_data
, entry
->fts_path
,
980 plan
->flags
& F_IGNCASE
? FNM_CASEFOLD
: 0);
983 /* c_path is the same as c_name */
988 * The mode argument is used to represent file mode bits. If it starts
989 * with a leading digit, it's treated as an octal mode, otherwise as a
999 mode
= entry
->fts_statp
->st_mode
&
1000 (S_ISUID
|S_ISGID
|S_ISTXT
|S_IRWXU
|S_IRWXG
|S_IRWXO
);
1001 if (plan
->flags
& F_ATLEAST
)
1002 return (plan
->m_data
| mode
) == mode
;
1003 else if (plan
->flags
& F_ANY
)
1004 return (mode
& plan
->m_data
);
1006 return mode
== plan
->m_data
;
1011 c_perm(option
, argvp
)
1019 perm
= nextarg(option
, argvp
);
1020 ftsoptions
&= ~FTS_NOSTAT
;
1022 new = palloc(option
);
1025 new->flags
|= F_ATLEAST
;
1027 } else if (*perm
== '+') {
1028 new->flags
|= F_ANY
;
1032 if ((set
= setmode(perm
)) == NULL
)
1033 errx(1, "%s: %s: illegal mode string", option
->name
, perm
);
1035 new->m_data
= getmode(set
, 0);
1041 * -print functions --
1043 * Always true, causes the current pathame to be written to
1047 f_print(plan
, entry
)
1051 (void)puts(entry
->fts_path
);
1056 c_print(option
, argvp
)
1062 return palloc(option
);
1066 * -print0 functions --
1068 * Always true, causes the current pathame to be written to
1069 * standard output followed by a NUL character
1072 f_print0(plan
, entry
)
1076 fputs(entry
->fts_path
, stdout
);
1077 fputc('\0', stdout
);
1081 /* c_print0 is the same as c_print */
1084 * -prune functions --
1086 * Prune a portion of the hierarchy.
1089 f_prune(plan
, entry
)
1095 if (fts_set(tree
, entry
, FTS_SKIP
))
1096 err(1, "%s", entry
->fts_path
);
1100 /* c_prune == c_simple */
1103 * -regex functions --
1105 * True if the whole path of the file matches pattern using
1106 * regular expression.
1109 f_regex(plan
, entry
)
1118 char errbuf
[LINE_MAX
];
1121 pre
= plan
->re_data
;
1122 str
= entry
->fts_path
;
1129 errcode
= regexec(pre
, str
, 1, &pmatch
, REG_STARTEND
);
1131 if (errcode
!= 0 && errcode
!= REG_NOMATCH
) {
1132 regerror(errcode
, pre
, errbuf
, sizeof errbuf
);
1134 plan
->flags
& F_IGNCASE
? "-iregex" : "-regex", errbuf
);
1137 if (errcode
== 0 && pmatch
.rm_so
== 0 && pmatch
.rm_eo
== len
)
1144 c_regex(option
, argvp
)
1152 char errbuf
[LINE_MAX
];
1154 if ((pre
= malloc(sizeof(regex_t
))) == NULL
)
1157 pattern
= nextarg(option
, argvp
);
1159 if ((errcode
= regcomp(pre
, pattern
,
1160 regexp_flags
| (option
->flags
& F_IGNCASE
? REG_ICASE
: 0))) != 0) {
1161 regerror(errcode
, pre
, errbuf
, sizeof errbuf
);
1162 errx(1, "%s: %s: %s",
1163 option
->flags
& F_IGNCASE
? "-iregex" : "-regex",
1167 new = palloc(option
);
1173 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or */
1176 c_simple(option
, argvp
)
1180 return palloc(option
);
1184 * -size n[c] functions --
1186 * True if the file size in bytes, divided by an implementation defined
1187 * value and rounded up to the next integer, is n. If n is followed by
1188 * a c, the size is in bytes.
1190 #define FIND_SIZE 512
1191 static int divsize
= 1;
1200 size
= divsize
? (entry
->fts_statp
->st_size
+ FIND_SIZE
- 1) /
1201 FIND_SIZE
: entry
->fts_statp
->st_size
;
1202 COMPARE(size
, plan
->o_data
);
1206 c_size(option
, argvp
)
1214 size_str
= nextarg(option
, argvp
);
1215 ftsoptions
&= ~FTS_NOSTAT
;
1217 new = palloc(option
);
1219 new->o_data
= find_parsenum(new, option
->name
, size_str
, &endch
);
1226 * -type c functions --
1228 * True if the type of the file is c, where c is b, c, d, p, f or w
1229 * for block special file, character special file, directory, FIFO,
1230 * regular file or whiteout respectively.
1237 return (entry
->fts_statp
->st_mode
& S_IFMT
) == plan
->m_data
;
1241 c_type(option
, argvp
)
1249 typestring
= nextarg(option
, argvp
);
1250 ftsoptions
&= ~FTS_NOSTAT
;
1252 switch (typestring
[0]) {
1277 ftsoptions
|= FTS_WHITEOUT
;
1279 #endif /* FTS_WHITEOUT */
1281 errx(1, "%s: %s: unknown type", option
->name
, typestring
);
1284 new = palloc(option
);
1290 * -user uname functions --
1292 * True if the file belongs to the user uname. If uname is numeric and
1293 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1294 * return a valid user name, uname is taken as a user ID.
1301 return entry
->fts_statp
->st_uid
== plan
->u_data
;
1305 c_user(option
, argvp
)
1314 username
= nextarg(option
, argvp
);
1315 ftsoptions
&= ~FTS_NOSTAT
;
1317 p
= getpwnam(username
);
1319 uid
= atoi(username
);
1320 if (uid
== 0 && username
[0] != '0')
1321 errx(1, "%s: %s: no such user", option
->name
, username
);
1325 new = palloc(option
);
1331 * -xdev functions --
1333 * Always true, causes find not to decend past directories that have a
1334 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1337 c_xdev(option
, argvp
)
1341 ftsoptions
|= FTS_XDEV
;
1343 return palloc(option
);
1347 * ( expression ) functions --
1349 * True if expression is true.
1357 register int state
= 0;
1359 for (p
= plan
->p_data
[0];
1360 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1365 * f_openparen and f_closeparen nodes are temporary place markers. They are
1366 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1367 * to a f_expr node containing the expression and the ')' node is discarded.
1368 * The functions themselves are only used as constants.
1372 f_openparen(plan
, entry
)
1380 f_closeparen(plan
, entry
)
1387 /* c_openparen == c_simple */
1388 /* c_closeparen == c_simple */
1391 * AND operator. Since AND is implicit, no node is allocated.
1394 c_and(option
, argvp
)
1402 * ! expression functions --
1404 * Negation of a primary; the unary NOT operator.
1412 register int state
= 0;
1414 for (p
= plan
->p_data
[0];
1415 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1419 /* c_not == c_simple */
1422 * expression -o expression functions --
1424 * Alternation of primaries; the OR operator. The second expression is
1425 * not evaluated if the first expression is true.
1433 register int state
= 0;
1435 for (p
= plan
->p_data
[0];
1436 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1441 for (p
= plan
->p_data
[1];
1442 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1446 /* c_or == c_simple */