]>
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char sccsid
[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/usr.bin/find/function.c,v 1.71 2011/06/13 05:22:07 avatar Exp $");
42 #include <sys/param.h>
43 #include <sys/ucred.h>
45 #include <sys/types.h>
48 #include <sys/mount.h>
66 #include <sys/sysctl.h>
67 #include <sys/xattr.h>
69 #include <get_compat.h>
71 #define COMPAT_MODE(func, mode) 1
76 static PLAN
*palloc(OPTION
*);
77 static long long find_parsenum(PLAN
*, const char *, char *, char *);
78 static long long find_parsetime(PLAN
*, const char *, char *);
79 static char *nextarg(OPTION
*, char ***);
81 extern char **environ
;
83 static PLAN
*lastexecplus
= NULL
;
86 #define COMPARE(a, b) do { \
87 switch (plan->flags & F_ELG_MASK) { \
100 palloc(OPTION
*option
)
104 if ((new = malloc(sizeof(PLAN
))) == NULL
)
106 new->execute
= option
->execute
;
107 new->flags
= option
->flags
;
114 * Parse a string of the form [+-]# and return the value.
117 find_parsenum(PLAN
*plan
, const char *option
, char *vp
, char *endch
)
120 char *endchar
, *str
; /* Pointer to character ending conversion. */
122 /* Determine comparison from leading + or -. */
127 plan
->flags
|= F_GREATER
;
131 plan
->flags
|= F_LESSTHAN
;
134 plan
->flags
|= F_EQUAL
;
139 * Convert the string with strtoq(). Note, if strtoq() returns zero
140 * and endchar points to the beginning of the string we know we have
143 value
= strtoq(str
, &endchar
, 10);
144 if (value
== 0 && endchar
== str
)
145 errx(1, "%s: %s: illegal numeric value", option
, vp
);
146 if (endchar
[0] && endch
== NULL
)
147 errx(1, "%s: %s: illegal trailing character", option
, vp
);
155 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
158 find_parsetime(PLAN
*plan
, const char *option
, char *vp
)
160 long long secs
, value
;
161 char *str
, *unit
; /* Pointer to character ending conversion. */
163 /* Determine comparison from leading + or -. */
168 plan
->flags
|= F_GREATER
;
172 plan
->flags
|= F_LESSTHAN
;
175 plan
->flags
|= F_EQUAL
;
179 value
= strtoq(str
, &unit
, 10);
180 if (value
== 0 && unit
== str
) {
181 errx(1, "%s: %s: illegal time value", option
, vp
);
191 case 's': /* seconds */
194 case 'm': /* minutes */
197 case 'h': /* hours */
198 secs
+= value
* 3600;
201 secs
+= value
* 86400;
203 case 'w': /* weeks */
204 secs
+= value
* 604800;
207 errx(1, "%s: %s: bad unit '%c'", option
, vp
, *unit
);
211 if (*str
== '\0') /* EOS */
213 value
= strtoq(str
, &unit
, 10);
214 if (value
== 0 && unit
== str
) {
215 errx(1, "%s: %s: illegal time value", option
, vp
);
219 errx(1, "%s: %s: missing trailing unit", option
, vp
);
223 plan
->flags
|= F_EXACTTIME
;
229 * Check that another argument still exists, return a pointer to it,
230 * and increment the argument vector pointer.
233 nextarg(OPTION
*option
, char ***argvp
)
237 if ((arg
= **argvp
) == 0)
238 errx(1, "%s: requires additional arguments", option
->name
);
244 * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
245 * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts
246 * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
247 * the user wanted. Correct so that -1 is "less than 1".
249 #define TIME_CORRECT(p) \
250 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
254 * -[acm]min n functions --
256 * True if the difference between the
257 * file access time (-amin)
258 * file birth time (-Bmin)
259 * last change of file status information (-cmin)
260 * file modification time (-mmin)
261 * and the current time is n min periods.
264 f_Xmin(PLAN
*plan
, FTSENT
*entry
)
266 if (plan
->flags
& F_TIME_C
) {
267 COMPARE((now
- entry
->fts_statp
->st_ctime
+
268 60 - 1) / 60, plan
->t_data
);
269 } else if (plan
->flags
& F_TIME_A
) {
270 COMPARE((now
- entry
->fts_statp
->st_atime
+
271 60 - 1) / 60, plan
->t_data
);
272 } else if (plan
->flags
& F_TIME_B
) {
273 COMPARE((now
- entry
->fts_statp
->st_birthtime
+
274 60 - 1) / 60, plan
->t_data
);
276 COMPARE((now
- entry
->fts_statp
->st_mtime
+
277 60 - 1) / 60, plan
->t_data
);
282 c_Xmin(OPTION
*option
, char ***argvp
)
287 nmins
= nextarg(option
, argvp
);
288 ftsoptions
&= ~FTS_NOSTAT
;
290 new = palloc(option
);
291 new->t_data
= find_parsenum(new, option
->name
, nmins
, NULL
);
297 * -[acm]time n functions --
299 * True if the difference between the
300 * file access time (-atime)
301 * file birth time (-Btime)
302 * last change of file status information (-ctime)
303 * file modification time (-mtime)
304 * and the current time is n 24 hour periods.
308 f_Xtime(PLAN
*plan
, FTSENT
*entry
)
312 if (plan
->flags
& F_TIME_A
)
313 xtime
= entry
->fts_statp
->st_atime
;
314 else if (plan
->flags
& F_TIME_B
)
315 xtime
= entry
->fts_statp
->st_birthtime
;
316 else if (plan
->flags
& F_TIME_C
)
317 xtime
= entry
->fts_statp
->st_ctime
;
319 xtime
= entry
->fts_statp
->st_mtime
;
321 if (plan
->flags
& F_EXACTTIME
)
322 COMPARE(now
- xtime
, plan
->t_data
);
324 COMPARE((now
- xtime
+ (COMPAT_MODE("bin/find", "unix2003") ? 0 : 86400 - 1)) / 86400, plan
->t_data
);
328 c_Xtime(OPTION
*option
, char ***argvp
)
333 value
= nextarg(option
, argvp
);
334 ftsoptions
&= ~FTS_NOSTAT
;
336 new = palloc(option
);
337 new->t_data
= find_parsetime(new, option
->name
, value
);
338 if (!(new->flags
& F_EXACTTIME
) && !COMPAT_MODE("bin/find", "unix2003"))
344 * -maxdepth/-mindepth n functions --
346 * Does the same as -prune if the level of the current file is
347 * greater/less than the specified maximum/minimum depth.
349 * Note that -maxdepth and -mindepth are handled specially in
350 * find_execute() so their f_* functions are set to f_always_true().
353 c_mXXdepth(OPTION
*option
, char ***argvp
)
358 dstr
= nextarg(option
, argvp
);
360 /* all other errors handled by find_parsenum() */
361 errx(1, "%s: %s: value must be positive", option
->name
, dstr
);
363 new = palloc(option
);
364 if (option
->flags
& F_MAXDEPTH
)
365 maxdepth
= find_parsenum(new, option
->name
, dstr
, NULL
);
367 mindepth
= find_parsenum(new, option
->name
, dstr
, NULL
);
374 * Show files with EXTENDED ACL attributes.
378 f_acl(PLAN
*plan __unused
, FTSENT
*entry
)
385 if ((facl
= acl_get_link_np(entry
->fts_accpath
, ACL_TYPE_EXTENDED
)) != NULL
) {
386 if (acl_get_entry(facl
, ACL_FIRST_ENTRY
, &ae
) == 0) {
393 #else /* !__APPLE__ */
395 f_acl(PLAN
*plan __unused
, FTSENT
*entry
)
399 int acl_supported
= 0, ret
, trivial
;
401 if (S_ISLNK(entry
->fts_statp
->st_mode
))
403 ret
= pathconf(entry
->fts_accpath
, _PC_ACL_NFS4
);
406 acl_type
= ACL_TYPE_NFS4
;
407 } else if (ret
< 0 && errno
!= EINVAL
) {
408 warn("%s", entry
->fts_accpath
);
411 if (acl_supported
== 0) {
412 ret
= pathconf(entry
->fts_accpath
, _PC_ACL_EXTENDED
);
415 acl_type
= ACL_TYPE_ACCESS
;
416 } else if (ret
< 0 && errno
!= EINVAL
) {
417 warn("%s", entry
->fts_accpath
);
421 if (acl_supported
== 0)
424 facl
= acl_get_file(entry
->fts_accpath
, acl_type
);
426 warn("%s", entry
->fts_accpath
);
429 ret
= acl_is_trivial_np(facl
, &trivial
);
432 warn("%s", entry
->fts_accpath
);
440 #endif /* __APPLE__ */
443 c_acl(OPTION
*option
, char ***argvp __unused
)
446 ftsoptions
&= ~FTS_NOSTAT
;
447 #endif /* !__APPLE__ */
448 return (palloc(option
));
453 f_xattr(PLAN
*plan __unused
, FTSENT
*entry
)
459 xattr
= listxattr(entry
->fts_accpath
, NULL
, 0, XATTR_NOFOLLOW
);
467 f_xattrname(PLAN
*plan
, FTSENT
*entry
)
473 xattr
= getxattr(entry
->fts_accpath
, plan
->c_data
, NULL
, 0, 0, XATTR_NOFOLLOW
);
479 #endif /* __APPLE__ */
482 * -delete functions --
484 * True always. Makes its best shot and continues on regardless.
487 f_delete(PLAN
*plan __unused
, FTSENT
*entry
)
489 /* ignore these from fts */
490 if (strcmp(entry
->fts_accpath
, ".") == 0 ||
491 strcmp(entry
->fts_accpath
, "..") == 0)
495 if (isdepth
== 0 || /* depth off */
496 (ftsoptions
& FTS_NOSTAT
)) /* not stat()ing */
497 errx(1, "-delete: insecure options got turned on");
499 if (!(ftsoptions
& FTS_PHYSICAL
) || /* physical off */
500 (ftsoptions
& FTS_LOGICAL
)) /* or finally, logical on */
501 errx(1, "-delete: forbidden when symlinks are followed");
503 /* Potentially unsafe - do not accept relative paths whatsoever */
504 if (strchr(entry
->fts_accpath
, '/') != NULL
)
505 errx(1, "-delete: %s: relative path potentially not safe",
508 /* Turn off user immutable bits if running as root */
509 if ((entry
->fts_statp
->st_flags
& (UF_APPEND
|UF_IMMUTABLE
)) &&
510 !(entry
->fts_statp
->st_flags
& (SF_APPEND
|SF_IMMUTABLE
)) &&
512 lchflags(entry
->fts_accpath
,
513 entry
->fts_statp
->st_flags
&= ~(UF_APPEND
|UF_IMMUTABLE
));
515 /* rmdir directories, unlink everything else */
516 if (S_ISDIR(entry
->fts_statp
->st_mode
)) {
517 if (rmdir(entry
->fts_accpath
) < 0 && errno
!= ENOTEMPTY
)
518 warn("-delete: rmdir(%s)", entry
->fts_path
);
520 if (unlink(entry
->fts_accpath
) < 0)
521 warn("-delete: unlink(%s)", entry
->fts_path
);
529 c_delete(OPTION
*option
, char ***argvp __unused
)
532 ftsoptions
&= ~FTS_NOSTAT
; /* no optimise */
533 isoutput
= 1; /* possible output */
534 isdepth
= 1; /* -depth implied */
536 return palloc(option
);
543 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
546 f_always_true(PLAN
*plan __unused
, FTSENT
*entry __unused
)
552 * -depth functions --
554 * With argument: True if the file is at level n.
555 * Without argument: Always true, causes descent of the directory hierarchy
556 * to be done so that all entries in a directory are acted on before the
560 f_depth(PLAN
*plan
, FTSENT
*entry
)
562 if (plan
->flags
& F_DEPTH
)
563 COMPARE(entry
->fts_level
, plan
->d_data
);
569 c_depth(OPTION
*option
, char ***argvp
)
574 new = palloc(option
);
577 if (str
&& !(new->flags
& F_DEPTH
)) {
578 /* skip leading + or - */
579 if (*str
== '+' || *str
== '-')
582 if (*str
== '+' || *str
== '-')
585 new->flags
|= F_DEPTH
;
588 if (new->flags
& F_DEPTH
) { /* -depth n */
591 ndepth
= nextarg(option
, argvp
);
592 new->d_data
= find_parsenum(new, option
->name
, ndepth
, NULL
);
601 * -empty functions --
603 * True if the file or directory is empty
606 f_empty(PLAN
*plan __unused
, FTSENT
*entry
)
608 if (S_ISREG(entry
->fts_statp
->st_mode
) &&
609 entry
->fts_statp
->st_size
== 0)
611 if (S_ISDIR(entry
->fts_statp
->st_mode
)) {
617 dir
= opendir(entry
->fts_accpath
);
620 for (dp
= readdir(dir
); dp
; dp
= readdir(dir
))
621 if (dp
->d_name
[0] != '.' ||
622 (dp
->d_name
[1] != '\0' &&
623 (dp
->d_name
[1] != '.' || dp
->d_name
[2] != '\0'))) {
634 c_empty(OPTION
*option
, char ***argvp __unused
)
636 ftsoptions
&= ~FTS_NOSTAT
;
638 return palloc(option
);
642 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
644 * True if the executed utility returns a zero value as exit status.
645 * The end of the primary expression is delimited by a semicolon. If
646 * "{}" occurs anywhere, it gets replaced by the current pathname,
647 * or, in the case of -execdir, the current basename (filename
648 * without leading directory prefix). For -exec and -ok,
649 * the current directory for the execution of utility is the same as
650 * the current directory when the find utility was started, whereas
651 * for -execdir, it is the directory the file resides in.
653 * The primary -ok differs from -exec in that it requests affirmation
654 * of the user before executing the utility.
657 f_exec(PLAN
*plan
, FTSENT
*entry
)
664 if (entry
== NULL
&& plan
->flags
& F_EXECPLUS
) {
665 if (plan
->e_ppos
== plan
->e_pbnum
)
667 plan
->e_argv
[plan
->e_ppos
] = NULL
;
671 /* XXX - if file/dir ends in '/' this will not work -- can it? */
672 if ((plan
->flags
& F_EXECDIR
) && \
673 (file
= strrchr(entry
->fts_path
, '/')))
676 file
= entry
->fts_path
;
678 if (plan
->flags
& F_EXECPLUS
) {
679 if ((plan
->e_argv
[plan
->e_ppos
] = strdup(file
)) == NULL
)
681 plan
->e_len
[plan
->e_ppos
] = strlen(file
);
682 plan
->e_psize
+= plan
->e_len
[plan
->e_ppos
];
683 if (++plan
->e_ppos
< plan
->e_pnummax
&&
684 plan
->e_psize
< plan
->e_psizemax
)
686 plan
->e_argv
[plan
->e_ppos
] = NULL
;
688 for (cnt
= 0; plan
->e_argv
[cnt
]; ++cnt
)
689 if (plan
->e_len
[cnt
])
690 brace_subst(plan
->e_orig
[cnt
],
691 &plan
->e_argv
[cnt
], file
,
695 doexec
: if ((plan
->flags
& F_NEEDOK
) && !queryuser(plan
->e_argv
))
698 /* make sure find output is interspersed correctly with subprocesses */
702 switch (pid
= fork()) {
707 /* change dir back from where we started */
708 if (!(plan
->flags
& F_EXECDIR
) && fchdir(dotfd
)) {
712 execvp(plan
->e_argv
[0], plan
->e_argv
);
713 warn("%s", plan
->e_argv
[0]);
716 if (plan
->flags
& F_EXECPLUS
) {
717 while (--plan
->e_ppos
>= plan
->e_pbnum
)
718 free(plan
->e_argv
[plan
->e_ppos
]);
719 plan
->e_ppos
= plan
->e_pbnum
;
720 plan
->e_psize
= plan
->e_pbsize
;
722 pid
= waitpid(pid
, &status
, 0);
723 if (plan
->flags
& F_EXECPLUS
&& WIFEXITED(status
) && WEXITSTATUS(status
) && !execplus_error
) {
724 /* Test 140 (8907531, 10656525) */
725 execplus_error
= WEXITSTATUS(status
);
727 return (pid
!= -1 && WIFEXITED(status
) && !WEXITSTATUS(status
));
731 * c_exec, c_execdir, c_ok --
732 * build three parallel arrays, one with pointers to the strings passed
733 * on the command line, one with (possibly duplicated) pointers to the
734 * argv array, and one with integer values that are lengths of the
735 * strings, but also flags meaning that the string has to be massaged.
738 c_exec(OPTION
*option
, char ***argvp
)
740 PLAN
*new; /* node returned */
743 char **argv
, **ap
, **ep
, *p
;
745 /* XXX - was in c_execdir, but seems unnecessary!?
746 ftsoptions &= ~FTS_NOSTAT;
750 /* XXX - this is a change from the previous coding */
751 new = palloc(option
);
753 for (ap
= argv
= *argvp
;; ++ap
) {
756 "%s: no terminating \";\" or \"+\"", option
->name
);
759 if (**ap
== '+' && ap
!= argv
&& strcmp(*(ap
- 1), "{}") == 0) {
760 new->flags
|= F_EXECPLUS
;
766 errx(1, "%s: no command specified", option
->name
);
768 cnt
= ap
- *argvp
+ 1;
769 if (new->flags
& F_EXECPLUS
) {
770 new->e_ppos
= new->e_pbnum
= cnt
- 2;
771 if ((argmax
= sysconf(_SC_ARG_MAX
)) == -1) {
772 warn("sysconf(_SC_ARG_MAX)");
773 argmax
= _POSIX_ARG_MAX
;
776 for (ep
= environ
; *ep
!= NULL
; ep
++)
777 argmax
-= strlen(*ep
) + 1 + sizeof(*ep
);
778 argmax
-= 1 + sizeof(*ep
);
779 new->e_pnummax
= argmax
/ 16;
780 argmax
-= sizeof(char *) * new->e_pnummax
;
782 errx(1, "no space for arguments");
783 new->e_psizemax
= argmax
;
785 cnt
+= new->e_pnummax
+ 1;
786 new->e_next
= lastexecplus
;
789 if ((new->e_argv
= malloc(cnt
* sizeof(char *))) == NULL
)
791 if ((new->e_orig
= malloc(cnt
* sizeof(char *))) == NULL
)
793 if ((new->e_len
= malloc(cnt
* sizeof(int))) == NULL
)
796 for (argv
= *argvp
, cnt
= 0; argv
< ap
; ++argv
, ++cnt
) {
797 new->e_orig
[cnt
] = *argv
;
798 if (new->flags
& F_EXECPLUS
)
799 new->e_pbsize
+= strlen(*argv
) + 1;
800 for (p
= *argv
; *p
; ++p
)
801 if (!(new->flags
& F_EXECPLUS
) && p
[0] == '{' &&
803 if ((new->e_argv
[cnt
] =
804 malloc(MAXPATHLEN
)) == NULL
)
806 new->e_len
[cnt
] = MAXPATHLEN
;
810 new->e_argv
[cnt
] = *argv
;
814 if (new->flags
& F_EXECPLUS
) {
815 new->e_psize
= new->e_pbsize
;
817 for (i
= 0; i
< new->e_pnummax
; i
++) {
818 new->e_argv
[cnt
] = NULL
;
825 new->e_argv
[cnt
] = new->e_orig
[cnt
] = NULL
;
827 done
: *argvp
= argv
+ 1;
831 /* Finish any pending -exec ... {} + functions. */
833 finish_execplus(void)
839 (p
->execute
)(p
, NULL
);
845 f_flags(PLAN
*plan
, FTSENT
*entry
)
849 flags
= entry
->fts_statp
->st_flags
;
850 if (plan
->flags
& F_ATLEAST
)
851 return (flags
| plan
->fl_flags
) == flags
&&
852 !(flags
& plan
->fl_notflags
);
853 else if (plan
->flags
& F_ANY
)
854 return (flags
& plan
->fl_flags
) ||
855 (flags
| plan
->fl_notflags
) != flags
;
857 return flags
== plan
->fl_flags
&&
858 !(plan
->fl_flags
& plan
->fl_notflags
);
862 c_flags(OPTION
*option
, char ***argvp
)
866 u_long flags
, notflags
;
868 flags_str
= nextarg(option
, argvp
);
869 ftsoptions
&= ~FTS_NOSTAT
;
871 new = palloc(option
);
873 if (*flags_str
== '-') {
874 new->flags
|= F_ATLEAST
;
876 } else if (*flags_str
== '+') {
880 if (strtofflags(&flags_str
, &flags
, ¬flags
) == 1)
881 errx(1, "%s: %s: illegal flags string", option
->name
, flags_str
);
883 new->fl_flags
= flags
;
884 new->fl_notflags
= notflags
;
889 * -follow functions --
891 * Always true, causes symbolic links to be followed on a global
895 c_follow(OPTION
*option
, char ***argvp __unused
)
897 ftsoptions
&= ~FTS_PHYSICAL
;
898 ftsoptions
|= FTS_LOGICAL
;
900 return palloc(option
);
904 * -fstype functions --
906 * True if the file is of a certain type.
909 f_fstype(PLAN
*plan
, FTSENT
*entry
)
911 static dev_t curdev
; /* need a guaranteed illegal dev value */
912 static int first
= 1;
914 static int val_flags
;
915 static char fstype
[sizeof(sb
.f_fstypename
)];
916 char *p
, save
[2] = {0,0};
918 if ((plan
->flags
& F_MTMASK
) == F_MTUNKNOWN
)
921 /* Only check when we cross mount point. */
922 if (first
|| curdev
!= entry
->fts_statp
->st_dev
) {
923 curdev
= entry
->fts_statp
->st_dev
;
926 * Statfs follows symlinks; find wants the link's filesystem,
927 * not where it points.
929 if (entry
->fts_info
== FTS_SL
||
930 entry
->fts_info
== FTS_SLNONE
) {
931 if ((p
= strrchr(entry
->fts_accpath
, '/')) != NULL
)
934 p
= entry
->fts_accpath
;
942 if (statfs(entry
->fts_accpath
, &sb
))
943 err(1, "%s", entry
->fts_accpath
);
953 * Further tests may need both of these values, so
954 * always copy both of them.
956 val_flags
= sb
.f_flags
;
957 strlcpy(fstype
, sb
.f_fstypename
, sizeof(fstype
));
959 switch (plan
->flags
& F_MTMASK
) {
961 return val_flags
& plan
->mt_data
;
963 return (strncmp(fstype
, plan
->c_data
, sizeof(fstype
)) == 0);
970 c_fstype(OPTION
*option
, char ***argvp
)
975 fsname
= nextarg(option
, argvp
);
976 ftsoptions
&= ~FTS_NOSTAT
;
978 new = palloc(option
);
981 if (!strcmp(fsname
, "local")) {
982 new->flags
|= F_MTFLAG
;
983 new->mt_data
= MNT_LOCAL
;
988 if (!strcmp(fsname
, "rdonly")) {
989 new->flags
|= F_MTFLAG
;
990 new->mt_data
= MNT_RDONLY
;
996 new->flags
|= F_MTTYPE
;
997 new->c_data
= fsname
;
1002 * -group gname functions --
1004 * True if the file belongs to the group gname. If gname is numeric and
1005 * an equivalent of the getgrnam() function does not return a valid group
1006 * name, gname is taken as a group ID.
1009 f_group(PLAN
*plan
, FTSENT
*entry
)
1011 COMPARE(entry
->fts_statp
->st_gid
, plan
->g_data
);
1015 c_group(OPTION
*option
, char ***argvp
)
1022 gname
= nextarg(option
, argvp
);
1023 ftsoptions
&= ~FTS_NOSTAT
;
1025 new = palloc(option
);
1026 g
= getgrnam(gname
);
1029 if (gname
[0] == '-' || gname
[0] == '+')
1032 if (gid
== 0 && gname
[0] != '0')
1033 errx(1, "%s: %s: no such group", option
->name
, gname
);
1034 gid
= find_parsenum(new, option
->name
, cp
, NULL
);
1043 * -inum n functions --
1045 * True if the file has inode # n.
1048 f_inum(PLAN
*plan
, FTSENT
*entry
)
1050 COMPARE(entry
->fts_statp
->st_ino
, plan
->i_data
);
1054 c_inum(OPTION
*option
, char ***argvp
)
1059 inum_str
= nextarg(option
, argvp
);
1060 ftsoptions
&= ~FTS_NOSTAT
;
1062 new = palloc(option
);
1063 new->i_data
= find_parsenum(new, option
->name
, inum_str
, NULL
);
1070 * True if the file has the same inode (eg hard link) FN
1073 /* f_samefile is just f_inum */
1075 c_samefile(OPTION
*option
, char ***argvp
)
1081 fn
= nextarg(option
, argvp
);
1082 ftsoptions
&= ~FTS_NOSTAT
;
1084 new = palloc(option
);
1087 new->i_data
= sb
.st_ino
;
1092 * -links n functions --
1094 * True if the file has n links.
1097 f_links(PLAN
*plan
, FTSENT
*entry
)
1099 COMPARE(entry
->fts_statp
->st_nlink
, plan
->l_data
);
1103 c_links(OPTION
*option
, char ***argvp
)
1108 nlinks
= nextarg(option
, argvp
);
1109 ftsoptions
&= ~FTS_NOSTAT
;
1111 new = palloc(option
);
1112 new->l_data
= (nlink_t
)find_parsenum(new, option
->name
, nlinks
, NULL
);
1119 * Always true - prints the current entry to stdout in "ls" format.
1122 f_ls(PLAN
*plan __unused
, FTSENT
*entry
)
1124 printlong(entry
->fts_path
, entry
->fts_accpath
, entry
->fts_statp
);
1129 c_ls(OPTION
*option
, char ***argvp __unused
)
1131 ftsoptions
&= ~FTS_NOSTAT
;
1134 return palloc(option
);
1138 * -name functions --
1140 * True if the basename of the filename being examined
1141 * matches pattern using Pattern Matching Notation S3.14
1144 f_name(PLAN
*plan
, FTSENT
*entry
)
1149 if (plan
->flags
& F_LINK
) {
1151 if (readlink(entry
->fts_accpath
, fn
, sizeof(fn
)) == -1)
1153 } else if (entry
->fts_namelen
== 0) {
1154 name
= basename(entry
->fts_path
);
1156 name
= entry
->fts_name
;
1157 return !fnmatch(plan
->c_data
, name
,
1158 plan
->flags
& F_IGNCASE
? FNM_CASEFOLD
: 0);
1162 c_name(OPTION
*option
, char ***argvp
)
1167 pattern
= nextarg(option
, argvp
);
1168 new = palloc(option
);
1169 new->c_data
= pattern
;
1174 * -newer file functions --
1176 * True if the current file has been modified more recently
1177 * then the modification time of the file named by the pathname
1181 f_newer(PLAN
*plan
, FTSENT
*entry
)
1183 if (plan
->flags
& F_TIME_C
)
1184 return entry
->fts_statp
->st_ctime
> plan
->t_data
;
1185 else if (plan
->flags
& F_TIME_A
)
1186 return entry
->fts_statp
->st_atime
> plan
->t_data
;
1187 else if (plan
->flags
& F_TIME_B
)
1188 return entry
->fts_statp
->st_birthtime
> plan
->t_data
;
1190 return entry
->fts_statp
->st_mtime
> plan
->t_data
;
1194 c_newer(OPTION
*option
, char ***argvp
)
1200 fn_or_tspec
= nextarg(option
, argvp
);
1201 ftsoptions
&= ~FTS_NOSTAT
;
1203 new = palloc(option
);
1204 /* compare against what */
1205 if (option
->flags
& F_TIME2_T
) {
1206 new->t_data
= get_date(fn_or_tspec
);
1207 if (new->t_data
== (time_t) -1)
1208 errx(1, "Can't parse date/time: %s", fn_or_tspec
);
1210 if (stat(fn_or_tspec
, &sb
))
1211 err(1, "%s", fn_or_tspec
);
1212 if (option
->flags
& F_TIME2_C
)
1213 new->t_data
= sb
.st_ctime
;
1214 else if (option
->flags
& F_TIME2_A
)
1215 new->t_data
= sb
.st_atime
;
1216 else if (option
->flags
& F_TIME2_B
)
1217 new->t_data
= sb
.st_birthtime
;
1219 new->t_data
= sb
.st_mtime
;
1225 * -nogroup functions --
1227 * True if file belongs to a user ID for which the equivalent
1228 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1231 f_nogroup(PLAN
*plan __unused
, FTSENT
*entry
)
1233 return group_from_gid(entry
->fts_statp
->st_gid
, 1) == NULL
;
1237 c_nogroup(OPTION
*option
, char ***argvp __unused
)
1239 ftsoptions
&= ~FTS_NOSTAT
;
1241 return palloc(option
);
1245 * -nouser functions --
1247 * True if file belongs to a user ID for which the equivalent
1248 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1251 f_nouser(PLAN
*plan __unused
, FTSENT
*entry
)
1253 return user_from_uid(entry
->fts_statp
->st_uid
, 1) == NULL
;
1257 c_nouser(OPTION
*option
, char ***argvp __unused
)
1259 ftsoptions
&= ~FTS_NOSTAT
;
1261 return palloc(option
);
1265 * -path functions --
1267 * True if the path of the filename being examined
1268 * matches pattern using Pattern Matching Notation S3.14
1271 f_path(PLAN
*plan
, FTSENT
*entry
)
1273 return !fnmatch(plan
->c_data
, entry
->fts_path
,
1274 plan
->flags
& F_IGNCASE
? FNM_CASEFOLD
: 0);
1277 /* c_path is the same as c_name */
1280 * -perm functions --
1282 * The mode argument is used to represent file mode bits. If it starts
1283 * with a leading digit, it's treated as an octal mode, otherwise as a
1287 f_perm(PLAN
*plan
, FTSENT
*entry
)
1291 mode
= entry
->fts_statp
->st_mode
&
1292 (S_ISUID
|S_ISGID
|S_ISTXT
|S_IRWXU
|S_IRWXG
|S_IRWXO
);
1293 if (plan
->flags
& F_ATLEAST
)
1294 return (plan
->m_data
| mode
) == mode
;
1295 else if (plan
->flags
& F_ANY
)
1296 return (mode
& plan
->m_data
);
1298 return mode
== plan
->m_data
;
1303 c_perm(OPTION
*option
, char ***argvp
)
1309 perm
= nextarg(option
, argvp
);
1310 ftsoptions
&= ~FTS_NOSTAT
;
1312 new = palloc(option
);
1315 new->flags
|= F_ATLEAST
;
1317 } else if (*perm
== '+') {
1318 if ((set
= setmode(perm
+ 1)) != NULL
) {
1319 new->flags
|= F_ANY
;
1325 if ((set
= setmode(perm
)) == NULL
)
1326 errx(1, "%s: %s: illegal mode string", option
->name
, perm
);
1328 new->m_data
= getmode(set
, 0);
1334 * -print functions --
1336 * Always true, causes the current pathname to be written to
1340 f_print(PLAN
*plan __unused
, FTSENT
*entry
)
1342 (void)puts(entry
->fts_path
);
1347 c_print(OPTION
*option
, char ***argvp __unused
)
1351 return palloc(option
);
1355 * -print0 functions --
1357 * Always true, causes the current pathname to be written to
1358 * standard output followed by a NUL character
1361 f_print0(PLAN
*plan __unused
, FTSENT
*entry
)
1363 fputs(entry
->fts_path
, stdout
);
1364 fputc('\0', stdout
);
1368 /* c_print0 is the same as c_print */
1371 * -prune functions --
1373 * Prune a portion of the hierarchy.
1376 f_prune(PLAN
*plan __unused
, FTSENT
*entry
)
1378 if (fts_set(tree
, entry
, FTS_SKIP
))
1379 err(1, "%s", entry
->fts_path
);
1383 /* c_prune == c_simple */
1386 * -regex functions --
1388 * True if the whole path of the file matches pattern using
1389 * regular expression.
1392 f_regex(PLAN
*plan
, FTSENT
*entry
)
1399 char errbuf
[LINE_MAX
];
1402 pre
= plan
->re_data
;
1403 str
= entry
->fts_path
;
1410 errcode
= regexec(pre
, str
, 1, &pmatch
, REG_STARTEND
);
1412 if (errcode
!= 0 && errcode
!= REG_NOMATCH
) {
1413 regerror(errcode
, pre
, errbuf
, sizeof errbuf
);
1415 plan
->flags
& F_IGNCASE
? "-iregex" : "-regex", errbuf
);
1418 if (errcode
== 0 && pmatch
.rm_so
== 0 && pmatch
.rm_eo
== len
)
1425 c_regex(OPTION
*option
, char ***argvp
)
1431 char errbuf
[LINE_MAX
];
1433 if ((pre
= malloc(sizeof(regex_t
))) == NULL
)
1436 pattern
= nextarg(option
, argvp
);
1438 if ((errcode
= regcomp(pre
, pattern
,
1439 regexp_flags
| (option
->flags
& F_IGNCASE
? REG_ICASE
: 0))) != 0) {
1440 regerror(errcode
, pre
, errbuf
, sizeof errbuf
);
1441 errx(1, "%s: %s: %s",
1442 option
->flags
& F_IGNCASE
? "-iregex" : "-regex",
1446 new = palloc(option
);
1452 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1455 c_simple(OPTION
*option
, char ***argvp __unused
)
1457 return palloc(option
);
1461 * -size n[c] functions --
1463 * True if the file size in bytes, divided by an implementation defined
1464 * value and rounded up to the next integer, is n. If n is followed by
1465 * one of c k M G T P, the size is in bytes, kilobytes,
1466 * megabytes, gigabytes, terabytes or petabytes respectively.
1468 #define FIND_SIZE 512
1469 static int divsize
= 1;
1472 f_size(PLAN
*plan
, FTSENT
*entry
)
1476 size
= divsize
? (entry
->fts_statp
->st_size
+ FIND_SIZE
- 1) /
1477 FIND_SIZE
: entry
->fts_statp
->st_size
;
1478 COMPARE(size
, plan
->o_data
);
1482 c_size(OPTION
*option
, char ***argvp
)
1489 size_str
= nextarg(option
, argvp
);
1490 ftsoptions
&= ~FTS_NOSTAT
;
1492 new = palloc(option
);
1494 new->o_data
= find_parsenum(new, option
->name
, size_str
, &endch
);
1495 if (endch
!= '\0') {
1499 case 'c': /* characters */
1502 case 'k': /* kilobytes 1<<10 */
1505 case 'M': /* megabytes 1<<20 */
1508 case 'G': /* gigabytes 1<<30 */
1509 scale
= 0x40000000LL
;
1511 case 'T': /* terabytes 1<<40 */
1512 scale
= 0x1000000000LL
;
1514 case 'P': /* petabytes 1<<50 */
1515 scale
= 0x4000000000000LL
;
1518 errx(1, "%s: %s: illegal trailing character",
1519 option
->name
, size_str
);
1522 if (new->o_data
> QUAD_MAX
/ scale
)
1523 errx(1, "%s: %s: value too large",
1524 option
->name
, size_str
);
1525 new->o_data
*= scale
;
1531 * -type c functions --
1533 * True if the type of the file is c, where c is b, c, d, p, f or w
1534 * for block special file, character special file, directory, FIFO,
1535 * regular file or whiteout respectively.
1538 f_type(PLAN
*plan
, FTSENT
*entry
)
1540 return (entry
->fts_statp
->st_mode
& S_IFMT
) == plan
->m_data
;
1544 c_type(OPTION
*option
, char ***argvp
)
1550 typestring
= nextarg(option
, argvp
);
1551 ftsoptions
&= ~FTS_NOSTAT
;
1553 switch (typestring
[0]) {
1578 ftsoptions
|= FTS_WHITEOUT
;
1580 #endif /* FTS_WHITEOUT */
1582 errx(1, "%s: %s: unknown type", option
->name
, typestring
);
1585 new = palloc(option
);
1591 * -user uname functions --
1593 * True if the file belongs to the user uname. If uname is numeric and
1594 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1595 * return a valid user name, uname is taken as a user ID.
1598 f_user(PLAN
*plan
, FTSENT
*entry
)
1600 COMPARE(entry
->fts_statp
->st_uid
, plan
->u_data
);
1604 c_user(OPTION
*option
, char ***argvp
)
1611 username
= nextarg(option
, argvp
);
1612 ftsoptions
&= ~FTS_NOSTAT
;
1614 new = palloc(option
);
1615 p
= getpwnam(username
);
1617 char* cp
= username
;
1618 if( username
[0] == '-' || username
[0] == '+' )
1620 uid
= atoi(username
);
1621 if (uid
== 0 && username
[0] != '0')
1622 errx(1, "%s: %s: no such user", option
->name
, username
);
1623 uid
= find_parsenum(new, option
->name
, cp
, NULL
);
1632 * -xdev functions --
1634 * Always true, causes find not to descend past directories that have a
1635 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1638 c_xdev(OPTION
*option
, char ***argvp __unused
)
1640 ftsoptions
|= FTS_XDEV
;
1642 return palloc(option
);
1646 * ( expression ) functions --
1648 * True if expression is true.
1651 f_expr(PLAN
*plan
, FTSENT
*entry
)
1656 for (p
= plan
->p_data
[0];
1657 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1662 * f_openparen and f_closeparen nodes are temporary place markers. They are
1663 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1664 * to a f_expr node containing the expression and the ')' node is discarded.
1665 * The functions themselves are only used as constants.
1669 f_openparen(PLAN
*plan __unused
, FTSENT
*entry __unused
)
1675 f_closeparen(PLAN
*plan __unused
, FTSENT
*entry __unused
)
1680 /* c_openparen == c_simple */
1681 /* c_closeparen == c_simple */
1684 * AND operator. Since AND is implicit, no node is allocated.
1687 c_and(OPTION
*option __unused
, char ***argvp __unused
)
1693 * ! expression functions --
1695 * Negation of a primary; the unary NOT operator.
1698 f_not(PLAN
*plan
, FTSENT
*entry
)
1703 for (p
= plan
->p_data
[0];
1704 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1708 /* c_not == c_simple */
1711 * expression -o expression functions --
1713 * Alternation of primaries; the OR operator. The second expression is
1714 * not evaluated if the first expression is true.
1717 f_or(PLAN
*plan
, FTSENT
*entry
)
1722 for (p
= plan
->p_data
[0];
1723 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1728 for (p
= plan
->p_data
[1];
1729 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1733 /* c_or == c_simple */
1741 f_false(PLAN
*plan __unused
, FTSENT
*entry __unused
)
1746 /* c_false == c_simple */
1754 f_quit(PLAN
*plan __unused
, FTSENT
*entry __unused
)
1759 /* c_quit == c_simple */