]>
git.cameronkatri.com Git - apple_cmds.git/blob - shell_cmds/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
74 #include <libiosexec.h>
78 static PLAN
*palloc(OPTION
*);
79 static long long find_parsenum(PLAN
*, const char *, char *, char *);
80 static long long find_parsetime(PLAN
*, const char *, char *);
81 static char *nextarg(OPTION
*, char ***);
83 extern char **environ
;
85 static PLAN
*lastexecplus
= NULL
;
88 #define COMPARE(a, b) do { \
89 switch (plan->flags & F_ELG_MASK) { \
102 palloc(OPTION
*option
)
106 if ((new = malloc(sizeof(PLAN
))) == NULL
)
108 new->execute
= option
->execute
;
109 new->flags
= option
->flags
;
116 * Parse a string of the form [+-]# and return the value.
119 find_parsenum(PLAN
*plan
, const char *option
, char *vp
, char *endch
)
122 char *endchar
, *str
; /* Pointer to character ending conversion. */
124 /* Determine comparison from leading + or -. */
129 plan
->flags
|= F_GREATER
;
133 plan
->flags
|= F_LESSTHAN
;
136 plan
->flags
|= F_EQUAL
;
141 * Convert the string with strtoq(). Note, if strtoq() returns zero
142 * and endchar points to the beginning of the string we know we have
145 value
= strtoq(str
, &endchar
, 10);
146 if (value
== 0 && endchar
== str
)
147 errx(1, "%s: %s: illegal numeric value", option
, vp
);
148 if (endchar
[0] && endch
== NULL
)
149 errx(1, "%s: %s: illegal trailing character", option
, vp
);
157 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
160 find_parsetime(PLAN
*plan
, const char *option
, char *vp
)
162 long long secs
, value
;
163 char *str
, *unit
; /* Pointer to character ending conversion. */
165 /* Determine comparison from leading + or -. */
170 plan
->flags
|= F_GREATER
;
174 plan
->flags
|= F_LESSTHAN
;
177 plan
->flags
|= F_EQUAL
;
181 value
= strtoq(str
, &unit
, 10);
182 if (value
== 0 && unit
== str
) {
183 errx(1, "%s: %s: illegal time value", option
, vp
);
193 case 's': /* seconds */
196 case 'm': /* minutes */
199 case 'h': /* hours */
200 secs
+= value
* 3600;
203 secs
+= value
* 86400;
205 case 'w': /* weeks */
206 secs
+= value
* 604800;
209 errx(1, "%s: %s: bad unit '%c'", option
, vp
, *unit
);
213 if (*str
== '\0') /* EOS */
215 value
= strtoq(str
, &unit
, 10);
216 if (value
== 0 && unit
== str
) {
217 errx(1, "%s: %s: illegal time value", option
, vp
);
221 errx(1, "%s: %s: missing trailing unit", option
, vp
);
225 plan
->flags
|= F_EXACTTIME
;
231 * Check that another argument still exists, return a pointer to it,
232 * and increment the argument vector pointer.
235 nextarg(OPTION
*option
, char ***argvp
)
239 if ((arg
= **argvp
) == 0)
240 errx(1, "%s: requires additional arguments", option
->name
);
246 * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
247 * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts
248 * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
249 * the user wanted. Correct so that -1 is "less than 1".
251 #define TIME_CORRECT(p) \
252 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
256 * -[acm]min n functions --
258 * True if the difference between the
259 * file access time (-amin)
260 * file birth time (-Bmin)
261 * last change of file status information (-cmin)
262 * file modification time (-mmin)
263 * and the current time is n min periods.
266 f_Xmin(PLAN
*plan
, FTSENT
*entry
)
268 if (plan
->flags
& F_TIME_C
) {
269 COMPARE((now
- entry
->fts_statp
->st_ctime
+
270 60 - 1) / 60, plan
->t_data
);
271 } else if (plan
->flags
& F_TIME_A
) {
272 COMPARE((now
- entry
->fts_statp
->st_atime
+
273 60 - 1) / 60, plan
->t_data
);
274 } else if (plan
->flags
& F_TIME_B
) {
275 COMPARE((now
- entry
->fts_statp
->st_birthtime
+
276 60 - 1) / 60, plan
->t_data
);
278 COMPARE((now
- entry
->fts_statp
->st_mtime
+
279 60 - 1) / 60, plan
->t_data
);
284 c_Xmin(OPTION
*option
, char ***argvp
)
289 nmins
= nextarg(option
, argvp
);
290 ftsoptions
&= ~FTS_NOSTAT
;
292 new = palloc(option
);
293 new->t_data
= find_parsenum(new, option
->name
, nmins
, NULL
);
299 * -[acm]time n functions --
301 * True if the difference between the
302 * file access time (-atime)
303 * file birth time (-Btime)
304 * last change of file status information (-ctime)
305 * file modification time (-mtime)
306 * and the current time is n 24 hour periods.
310 f_Xtime(PLAN
*plan
, FTSENT
*entry
)
314 if (plan
->flags
& F_TIME_A
)
315 xtime
= entry
->fts_statp
->st_atime
;
316 else if (plan
->flags
& F_TIME_B
)
317 xtime
= entry
->fts_statp
->st_birthtime
;
318 else if (plan
->flags
& F_TIME_C
)
319 xtime
= entry
->fts_statp
->st_ctime
;
321 xtime
= entry
->fts_statp
->st_mtime
;
323 if (plan
->flags
& F_EXACTTIME
)
324 COMPARE(now
- xtime
, plan
->t_data
);
326 COMPARE((now
- xtime
+ (COMPAT_MODE("bin/find", "unix2003") ? 0 : 86400 - 1)) / 86400, plan
->t_data
);
330 c_Xtime(OPTION
*option
, char ***argvp
)
335 value
= nextarg(option
, argvp
);
336 ftsoptions
&= ~FTS_NOSTAT
;
338 new = palloc(option
);
339 new->t_data
= find_parsetime(new, option
->name
, value
);
340 if (!(new->flags
& F_EXACTTIME
) && !COMPAT_MODE("bin/find", "unix2003"))
346 * -maxdepth/-mindepth n functions --
348 * Does the same as -prune if the level of the current file is
349 * greater/less than the specified maximum/minimum depth.
351 * Note that -maxdepth and -mindepth are handled specially in
352 * find_execute() so their f_* functions are set to f_always_true().
355 c_mXXdepth(OPTION
*option
, char ***argvp
)
360 dstr
= nextarg(option
, argvp
);
362 /* all other errors handled by find_parsenum() */
363 errx(1, "%s: %s: value must be positive", option
->name
, dstr
);
365 new = palloc(option
);
366 if (option
->flags
& F_MAXDEPTH
)
367 maxdepth
= find_parsenum(new, option
->name
, dstr
, NULL
);
369 mindepth
= find_parsenum(new, option
->name
, dstr
, NULL
);
376 * Show files with EXTENDED ACL attributes.
380 f_acl(PLAN
*plan __unused
, FTSENT
*entry
)
387 if ((facl
= acl_get_link_np(entry
->fts_accpath
, ACL_TYPE_EXTENDED
)) != NULL
) {
388 if (acl_get_entry(facl
, ACL_FIRST_ENTRY
, &ae
) == 0) {
395 #else /* !__APPLE__ */
397 f_acl(PLAN
*plan __unused
, FTSENT
*entry
)
401 int acl_supported
= 0, ret
, trivial
;
403 if (S_ISLNK(entry
->fts_statp
->st_mode
))
405 ret
= pathconf(entry
->fts_accpath
, _PC_ACL_NFS4
);
408 acl_type
= ACL_TYPE_NFS4
;
409 } else if (ret
< 0 && errno
!= EINVAL
) {
410 warn("%s", entry
->fts_accpath
);
413 if (acl_supported
== 0) {
414 ret
= pathconf(entry
->fts_accpath
, _PC_ACL_EXTENDED
);
417 acl_type
= ACL_TYPE_ACCESS
;
418 } else if (ret
< 0 && errno
!= EINVAL
) {
419 warn("%s", entry
->fts_accpath
);
423 if (acl_supported
== 0)
426 facl
= acl_get_file(entry
->fts_accpath
, acl_type
);
428 warn("%s", entry
->fts_accpath
);
431 ret
= acl_is_trivial_np(facl
, &trivial
);
434 warn("%s", entry
->fts_accpath
);
442 #endif /* __APPLE__ */
445 c_acl(OPTION
*option
, char ***argvp __unused
)
448 ftsoptions
&= ~FTS_NOSTAT
;
449 #endif /* !__APPLE__ */
450 return (palloc(option
));
455 f_xattr(PLAN
*plan __unused
, FTSENT
*entry
)
461 xattr
= listxattr(entry
->fts_accpath
, NULL
, 0, XATTR_NOFOLLOW
);
469 f_xattrname(PLAN
*plan
, FTSENT
*entry
)
475 xattr
= getxattr(entry
->fts_accpath
, plan
->c_data
, NULL
, 0, 0, XATTR_NOFOLLOW
);
481 #endif /* __APPLE__ */
484 * -delete functions --
486 * True always. Makes its best shot and continues on regardless.
489 f_delete(PLAN
*plan __unused
, FTSENT
*entry
)
491 /* ignore these from fts */
492 if (strcmp(entry
->fts_accpath
, ".") == 0 ||
493 strcmp(entry
->fts_accpath
, "..") == 0)
497 if (isdepth
== 0 || /* depth off */
498 (ftsoptions
& FTS_NOSTAT
)) /* not stat()ing */
499 errx(1, "-delete: insecure options got turned on");
501 if (!(ftsoptions
& FTS_PHYSICAL
) || /* physical off */
502 (ftsoptions
& FTS_LOGICAL
)) /* or finally, logical on */
503 errx(1, "-delete: forbidden when symlinks are followed");
505 /* Potentially unsafe - do not accept relative paths whatsoever */
506 if (strchr(entry
->fts_accpath
, '/') != NULL
)
507 errx(1, "-delete: %s: relative path potentially not safe",
510 /* Turn off user immutable bits if running as root */
511 if ((entry
->fts_statp
->st_flags
& (UF_APPEND
|UF_IMMUTABLE
)) &&
512 !(entry
->fts_statp
->st_flags
& (SF_APPEND
|SF_IMMUTABLE
)) &&
514 lchflags(entry
->fts_accpath
,
515 entry
->fts_statp
->st_flags
&= ~(UF_APPEND
|UF_IMMUTABLE
));
517 /* rmdir directories, unlink everything else */
518 if (S_ISDIR(entry
->fts_statp
->st_mode
)) {
519 if (rmdir(entry
->fts_accpath
) < 0 && errno
!= ENOTEMPTY
)
520 warn("-delete: rmdir(%s)", entry
->fts_path
);
522 if (unlink(entry
->fts_accpath
) < 0)
523 warn("-delete: unlink(%s)", entry
->fts_path
);
531 c_delete(OPTION
*option
, char ***argvp __unused
)
534 ftsoptions
&= ~FTS_NOSTAT
; /* no optimise */
535 isoutput
= 1; /* possible output */
536 isdepth
= 1; /* -depth implied */
538 return palloc(option
);
545 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
548 f_always_true(PLAN
*plan __unused
, FTSENT
*entry __unused
)
554 * -depth functions --
556 * With argument: True if the file is at level n.
557 * Without argument: Always true, causes descent of the directory hierarchy
558 * to be done so that all entries in a directory are acted on before the
562 f_depth(PLAN
*plan
, FTSENT
*entry
)
564 if (plan
->flags
& F_DEPTH
)
565 COMPARE(entry
->fts_level
, plan
->d_data
);
571 c_depth(OPTION
*option
, char ***argvp
)
576 new = palloc(option
);
579 if (str
&& !(new->flags
& F_DEPTH
)) {
580 /* skip leading + or - */
581 if (*str
== '+' || *str
== '-')
584 if (*str
== '+' || *str
== '-')
587 new->flags
|= F_DEPTH
;
590 if (new->flags
& F_DEPTH
) { /* -depth n */
593 ndepth
= nextarg(option
, argvp
);
594 new->d_data
= find_parsenum(new, option
->name
, ndepth
, NULL
);
603 * -empty functions --
605 * True if the file or directory is empty
608 f_empty(PLAN
*plan __unused
, FTSENT
*entry
)
610 if (S_ISREG(entry
->fts_statp
->st_mode
) &&
611 entry
->fts_statp
->st_size
== 0)
613 if (S_ISDIR(entry
->fts_statp
->st_mode
)) {
619 dir
= opendir(entry
->fts_accpath
);
622 for (dp
= readdir(dir
); dp
; dp
= readdir(dir
))
623 if (dp
->d_name
[0] != '.' ||
624 (dp
->d_name
[1] != '\0' &&
625 (dp
->d_name
[1] != '.' || dp
->d_name
[2] != '\0'))) {
636 c_empty(OPTION
*option
, char ***argvp __unused
)
638 ftsoptions
&= ~FTS_NOSTAT
;
640 return palloc(option
);
644 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
646 * True if the executed utility returns a zero value as exit status.
647 * The end of the primary expression is delimited by a semicolon. If
648 * "{}" occurs anywhere, it gets replaced by the current pathname,
649 * or, in the case of -execdir, the current basename (filename
650 * without leading directory prefix). For -exec and -ok,
651 * the current directory for the execution of utility is the same as
652 * the current directory when the find utility was started, whereas
653 * for -execdir, it is the directory the file resides in.
655 * The primary -ok differs from -exec in that it requests affirmation
656 * of the user before executing the utility.
659 f_exec(PLAN
*plan
, FTSENT
*entry
)
666 if (entry
== NULL
&& plan
->flags
& F_EXECPLUS
) {
667 if (plan
->e_ppos
== plan
->e_pbnum
)
669 plan
->e_argv
[plan
->e_ppos
] = NULL
;
673 /* XXX - if file/dir ends in '/' this will not work -- can it? */
674 if ((plan
->flags
& F_EXECDIR
) && \
675 (file
= strrchr(entry
->fts_path
, '/')))
678 file
= entry
->fts_path
;
680 if (plan
->flags
& F_EXECPLUS
) {
681 if ((plan
->e_argv
[plan
->e_ppos
] = strdup(file
)) == NULL
)
683 plan
->e_len
[plan
->e_ppos
] = strlen(file
);
684 plan
->e_psize
+= plan
->e_len
[plan
->e_ppos
];
685 if (++plan
->e_ppos
< plan
->e_pnummax
&&
686 plan
->e_psize
< plan
->e_psizemax
)
688 plan
->e_argv
[plan
->e_ppos
] = NULL
;
690 for (cnt
= 0; plan
->e_argv
[cnt
]; ++cnt
)
691 if (plan
->e_len
[cnt
])
692 brace_subst(plan
->e_orig
[cnt
],
693 &plan
->e_argv
[cnt
], file
,
697 doexec
: if ((plan
->flags
& F_NEEDOK
) && !queryuser(plan
->e_argv
))
700 /* make sure find output is interspersed correctly with subprocesses */
704 switch (pid
= fork()) {
709 /* change dir back from where we started */
710 if (!(plan
->flags
& F_EXECDIR
) && fchdir(dotfd
)) {
714 execvp(plan
->e_argv
[0], plan
->e_argv
);
715 warn("%s", plan
->e_argv
[0]);
718 if (plan
->flags
& F_EXECPLUS
) {
719 while (--plan
->e_ppos
>= plan
->e_pbnum
)
720 free(plan
->e_argv
[plan
->e_ppos
]);
721 plan
->e_ppos
= plan
->e_pbnum
;
722 plan
->e_psize
= plan
->e_pbsize
;
724 pid
= waitpid(pid
, &status
, 0);
725 if (plan
->flags
& F_EXECPLUS
&& WIFEXITED(status
) && WEXITSTATUS(status
) && !execplus_error
) {
726 /* Test 140 (8907531, 10656525) */
727 execplus_error
= WEXITSTATUS(status
);
729 return (pid
!= -1 && WIFEXITED(status
) && !WEXITSTATUS(status
));
733 * c_exec, c_execdir, c_ok --
734 * build three parallel arrays, one with pointers to the strings passed
735 * on the command line, one with (possibly duplicated) pointers to the
736 * argv array, and one with integer values that are lengths of the
737 * strings, but also flags meaning that the string has to be massaged.
740 c_exec(OPTION
*option
, char ***argvp
)
742 PLAN
*new; /* node returned */
745 char **argv
, **ap
, **ep
, *p
;
747 /* XXX - was in c_execdir, but seems unnecessary!?
748 ftsoptions &= ~FTS_NOSTAT;
752 /* XXX - this is a change from the previous coding */
753 new = palloc(option
);
755 for (ap
= argv
= *argvp
;; ++ap
) {
758 "%s: no terminating \";\" or \"+\"", option
->name
);
761 if (**ap
== '+' && ap
!= argv
&& strcmp(*(ap
- 1), "{}") == 0) {
762 new->flags
|= F_EXECPLUS
;
768 errx(1, "%s: no command specified", option
->name
);
770 cnt
= ap
- *argvp
+ 1;
771 if (new->flags
& F_EXECPLUS
) {
772 new->e_ppos
= new->e_pbnum
= cnt
- 2;
773 if ((argmax
= sysconf(_SC_ARG_MAX
)) == -1) {
774 warn("sysconf(_SC_ARG_MAX)");
775 argmax
= _POSIX_ARG_MAX
;
778 for (ep
= environ
; *ep
!= NULL
; ep
++)
779 argmax
-= strlen(*ep
) + 1 + sizeof(*ep
);
780 argmax
-= 1 + sizeof(*ep
);
781 new->e_pnummax
= argmax
/ 16;
782 argmax
-= sizeof(char *) * new->e_pnummax
;
784 errx(1, "no space for arguments");
785 new->e_psizemax
= argmax
;
787 cnt
+= new->e_pnummax
+ 1;
788 new->e_next
= lastexecplus
;
791 if ((new->e_argv
= malloc(cnt
* sizeof(char *))) == NULL
)
793 if ((new->e_orig
= malloc(cnt
* sizeof(char *))) == NULL
)
795 if ((new->e_len
= malloc(cnt
* sizeof(int))) == NULL
)
798 for (argv
= *argvp
, cnt
= 0; argv
< ap
; ++argv
, ++cnt
) {
799 new->e_orig
[cnt
] = *argv
;
800 if (new->flags
& F_EXECPLUS
)
801 new->e_pbsize
+= strlen(*argv
) + 1;
802 for (p
= *argv
; *p
; ++p
)
803 if (!(new->flags
& F_EXECPLUS
) && p
[0] == '{' &&
805 if ((new->e_argv
[cnt
] =
806 malloc(MAXPATHLEN
)) == NULL
)
808 new->e_len
[cnt
] = MAXPATHLEN
;
812 new->e_argv
[cnt
] = *argv
;
816 if (new->flags
& F_EXECPLUS
) {
817 new->e_psize
= new->e_pbsize
;
819 for (i
= 0; i
< new->e_pnummax
; i
++) {
820 new->e_argv
[cnt
] = NULL
;
827 new->e_argv
[cnt
] = new->e_orig
[cnt
] = NULL
;
829 done
: *argvp
= argv
+ 1;
833 /* Finish any pending -exec ... {} + functions. */
835 finish_execplus(void)
841 (p
->execute
)(p
, NULL
);
847 f_flags(PLAN
*plan
, FTSENT
*entry
)
851 flags
= entry
->fts_statp
->st_flags
;
852 if (plan
->flags
& F_ATLEAST
)
853 return (flags
| plan
->fl_flags
) == flags
&&
854 !(flags
& plan
->fl_notflags
);
855 else if (plan
->flags
& F_ANY
)
856 return (flags
& plan
->fl_flags
) ||
857 (flags
| plan
->fl_notflags
) != flags
;
859 return flags
== plan
->fl_flags
&&
860 !(plan
->fl_flags
& plan
->fl_notflags
);
864 c_flags(OPTION
*option
, char ***argvp
)
868 u_long flags
, notflags
;
870 flags_str
= nextarg(option
, argvp
);
871 ftsoptions
&= ~FTS_NOSTAT
;
873 new = palloc(option
);
875 if (*flags_str
== '-') {
876 new->flags
|= F_ATLEAST
;
878 } else if (*flags_str
== '+') {
882 if (strtofflags(&flags_str
, &flags
, ¬flags
) == 1)
883 errx(1, "%s: %s: illegal flags string", option
->name
, flags_str
);
885 new->fl_flags
= flags
;
886 new->fl_notflags
= notflags
;
891 * -follow functions --
893 * Always true, causes symbolic links to be followed on a global
897 c_follow(OPTION
*option
, char ***argvp __unused
)
899 ftsoptions
&= ~FTS_PHYSICAL
;
900 ftsoptions
|= FTS_LOGICAL
;
902 return palloc(option
);
906 * -fstype functions --
908 * True if the file is of a certain type.
911 f_fstype(PLAN
*plan
, FTSENT
*entry
)
913 static dev_t curdev
; /* need a guaranteed illegal dev value */
914 static int first
= 1;
916 static int val_flags
;
917 static char fstype
[sizeof(sb
.f_fstypename
)];
918 char *p
, save
[2] = {0,0};
920 if ((plan
->flags
& F_MTMASK
) == F_MTUNKNOWN
)
923 /* Only check when we cross mount point. */
924 if (first
|| curdev
!= entry
->fts_statp
->st_dev
) {
925 curdev
= entry
->fts_statp
->st_dev
;
928 * Statfs follows symlinks; find wants the link's filesystem,
929 * not where it points.
931 if (entry
->fts_info
== FTS_SL
||
932 entry
->fts_info
== FTS_SLNONE
) {
933 if ((p
= strrchr(entry
->fts_accpath
, '/')) != NULL
)
936 p
= entry
->fts_accpath
;
944 if (statfs(entry
->fts_accpath
, &sb
))
945 err(1, "%s", entry
->fts_accpath
);
955 * Further tests may need both of these values, so
956 * always copy both of them.
958 val_flags
= sb
.f_flags
;
959 strlcpy(fstype
, sb
.f_fstypename
, sizeof(fstype
));
961 switch (plan
->flags
& F_MTMASK
) {
963 return val_flags
& plan
->mt_data
;
965 return (strncmp(fstype
, plan
->c_data
, sizeof(fstype
)) == 0);
972 c_fstype(OPTION
*option
, char ***argvp
)
977 fsname
= nextarg(option
, argvp
);
978 ftsoptions
&= ~FTS_NOSTAT
;
980 new = palloc(option
);
983 if (!strcmp(fsname
, "local")) {
984 new->flags
|= F_MTFLAG
;
985 new->mt_data
= MNT_LOCAL
;
990 if (!strcmp(fsname
, "rdonly")) {
991 new->flags
|= F_MTFLAG
;
992 new->mt_data
= MNT_RDONLY
;
998 new->flags
|= F_MTTYPE
;
999 new->c_data
= fsname
;
1004 * -group gname functions --
1006 * True if the file belongs to the group gname. If gname is numeric and
1007 * an equivalent of the getgrnam() function does not return a valid group
1008 * name, gname is taken as a group ID.
1011 f_group(PLAN
*plan
, FTSENT
*entry
)
1013 COMPARE(entry
->fts_statp
->st_gid
, plan
->g_data
);
1017 c_group(OPTION
*option
, char ***argvp
)
1024 gname
= nextarg(option
, argvp
);
1025 ftsoptions
&= ~FTS_NOSTAT
;
1027 new = palloc(option
);
1028 g
= getgrnam(gname
);
1031 if (gname
[0] == '-' || gname
[0] == '+')
1034 if (gid
== 0 && gname
[0] != '0')
1035 errx(1, "%s: %s: no such group", option
->name
, gname
);
1036 gid
= find_parsenum(new, option
->name
, cp
, NULL
);
1045 * -inum n functions --
1047 * True if the file has inode # n.
1050 f_inum(PLAN
*plan
, FTSENT
*entry
)
1052 COMPARE(entry
->fts_statp
->st_ino
, plan
->i_data
);
1056 c_inum(OPTION
*option
, char ***argvp
)
1061 inum_str
= nextarg(option
, argvp
);
1062 ftsoptions
&= ~FTS_NOSTAT
;
1064 new = palloc(option
);
1065 new->i_data
= find_parsenum(new, option
->name
, inum_str
, NULL
);
1072 * True if the file has the same inode (eg hard link) FN
1075 /* f_samefile is just f_inum */
1077 c_samefile(OPTION
*option
, char ***argvp
)
1083 fn
= nextarg(option
, argvp
);
1084 ftsoptions
&= ~FTS_NOSTAT
;
1086 new = palloc(option
);
1089 new->i_data
= sb
.st_ino
;
1094 * -links n functions --
1096 * True if the file has n links.
1099 f_links(PLAN
*plan
, FTSENT
*entry
)
1101 COMPARE(entry
->fts_statp
->st_nlink
, plan
->l_data
);
1105 c_links(OPTION
*option
, char ***argvp
)
1110 nlinks
= nextarg(option
, argvp
);
1111 ftsoptions
&= ~FTS_NOSTAT
;
1113 new = palloc(option
);
1114 new->l_data
= (nlink_t
)find_parsenum(new, option
->name
, nlinks
, NULL
);
1121 * Always true - prints the current entry to stdout in "ls" format.
1124 f_ls(PLAN
*plan __unused
, FTSENT
*entry
)
1126 printlong(entry
->fts_path
, entry
->fts_accpath
, entry
->fts_statp
);
1131 c_ls(OPTION
*option
, char ***argvp __unused
)
1133 ftsoptions
&= ~FTS_NOSTAT
;
1136 return palloc(option
);
1140 * -name functions --
1142 * True if the basename of the filename being examined
1143 * matches pattern using Pattern Matching Notation S3.14
1146 f_name(PLAN
*plan
, FTSENT
*entry
)
1152 if (plan
->flags
& F_LINK
) {
1154 * The below test both avoids obviously useless readlink()
1155 * calls and ensures that symlinks with existent target do
1156 * not match if symlinks are being followed.
1157 * Assumption: fts will stat all symlinks that are to be
1158 * followed and will return the stat information.
1160 if (entry
->fts_info
!= FTS_NSOK
&& entry
->fts_info
!= FTS_SL
&&
1161 entry
->fts_info
!= FTS_SLNONE
)
1163 len
= readlink(entry
->fts_accpath
, fn
, sizeof(fn
) - 1);
1168 } else if (entry
->fts_namelen
== 0) {
1169 name
= basename(entry
->fts_path
);
1171 name
= entry
->fts_name
;
1172 return !fnmatch(plan
->c_data
, name
,
1173 plan
->flags
& F_IGNCASE
? FNM_CASEFOLD
: 0);
1177 c_name(OPTION
*option
, char ***argvp
)
1182 pattern
= nextarg(option
, argvp
);
1183 new = palloc(option
);
1184 new->c_data
= pattern
;
1189 * -newer file functions --
1191 * True if the current file has been modified more recently
1192 * then the modification time of the file named by the pathname
1196 f_newer(PLAN
*plan
, FTSENT
*entry
)
1198 if (plan
->flags
& F_TIME_C
)
1199 return entry
->fts_statp
->st_ctime
> plan
->t_data
;
1200 else if (plan
->flags
& F_TIME_A
)
1201 return entry
->fts_statp
->st_atime
> plan
->t_data
;
1202 else if (plan
->flags
& F_TIME_B
)
1203 return entry
->fts_statp
->st_birthtime
> plan
->t_data
;
1205 return entry
->fts_statp
->st_mtime
> plan
->t_data
;
1209 c_newer(OPTION
*option
, char ***argvp
)
1215 fn_or_tspec
= nextarg(option
, argvp
);
1216 ftsoptions
&= ~FTS_NOSTAT
;
1218 new = palloc(option
);
1219 /* compare against what */
1220 if (option
->flags
& F_TIME2_T
) {
1221 new->t_data
= get_date(fn_or_tspec
);
1222 if (new->t_data
== (time_t) -1)
1223 errx(1, "Can't parse date/time: %s", fn_or_tspec
);
1225 if (stat(fn_or_tspec
, &sb
))
1226 err(1, "%s", fn_or_tspec
);
1227 if (option
->flags
& F_TIME2_C
)
1228 new->t_data
= sb
.st_ctime
;
1229 else if (option
->flags
& F_TIME2_A
)
1230 new->t_data
= sb
.st_atime
;
1231 else if (option
->flags
& F_TIME2_B
)
1232 new->t_data
= sb
.st_birthtime
;
1234 new->t_data
= sb
.st_mtime
;
1240 * -nogroup functions --
1242 * True if file belongs to a user ID for which the equivalent
1243 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1246 f_nogroup(PLAN
*plan __unused
, FTSENT
*entry
)
1248 return group_from_gid(entry
->fts_statp
->st_gid
, 1) == NULL
;
1252 c_nogroup(OPTION
*option
, char ***argvp __unused
)
1254 ftsoptions
&= ~FTS_NOSTAT
;
1256 return palloc(option
);
1260 * -nouser functions --
1262 * True if file belongs to a user ID for which the equivalent
1263 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1266 f_nouser(PLAN
*plan __unused
, FTSENT
*entry
)
1268 return user_from_uid(entry
->fts_statp
->st_uid
, 1) == NULL
;
1272 c_nouser(OPTION
*option
, char ***argvp __unused
)
1274 ftsoptions
&= ~FTS_NOSTAT
;
1276 return palloc(option
);
1280 * -path functions --
1282 * True if the path of the filename being examined
1283 * matches pattern using Pattern Matching Notation S3.14
1286 f_path(PLAN
*plan
, FTSENT
*entry
)
1288 return !fnmatch(plan
->c_data
, entry
->fts_path
,
1289 plan
->flags
& F_IGNCASE
? FNM_CASEFOLD
: 0);
1292 /* c_path is the same as c_name */
1295 * -perm functions --
1297 * The mode argument is used to represent file mode bits. If it starts
1298 * with a leading digit, it's treated as an octal mode, otherwise as a
1302 f_perm(PLAN
*plan
, FTSENT
*entry
)
1306 mode
= entry
->fts_statp
->st_mode
&
1307 (S_ISUID
|S_ISGID
|S_ISTXT
|S_IRWXU
|S_IRWXG
|S_IRWXO
);
1308 if (plan
->flags
& F_ATLEAST
)
1309 return (plan
->m_data
| mode
) == mode
;
1310 else if (plan
->flags
& F_ANY
)
1311 return (mode
& plan
->m_data
);
1313 return mode
== plan
->m_data
;
1318 c_perm(OPTION
*option
, char ***argvp
)
1324 perm
= nextarg(option
, argvp
);
1325 ftsoptions
&= ~FTS_NOSTAT
;
1327 new = palloc(option
);
1330 new->flags
|= F_ATLEAST
;
1332 } else if (*perm
== '+') {
1333 if ((set
= setmode(perm
+ 1)) != NULL
) {
1334 new->flags
|= F_ANY
;
1340 if ((set
= setmode(perm
)) == NULL
)
1341 errx(1, "%s: %s: illegal mode string", option
->name
, perm
);
1343 new->m_data
= getmode(set
, 0);
1349 * -print functions --
1351 * Always true, causes the current pathname to be written to
1355 f_print(PLAN
*plan __unused
, FTSENT
*entry
)
1357 (void)puts(entry
->fts_path
);
1362 c_print(OPTION
*option
, char ***argvp __unused
)
1366 return palloc(option
);
1370 * -print0 functions --
1372 * Always true, causes the current pathname to be written to
1373 * standard output followed by a NUL character
1376 f_print0(PLAN
*plan __unused
, FTSENT
*entry
)
1378 fputs(entry
->fts_path
, stdout
);
1379 fputc('\0', stdout
);
1383 /* c_print0 is the same as c_print */
1386 * -prune functions --
1388 * Prune a portion of the hierarchy.
1391 f_prune(PLAN
*plan __unused
, FTSENT
*entry
)
1393 if (fts_set(tree
, entry
, FTS_SKIP
))
1394 err(1, "%s", entry
->fts_path
);
1398 /* c_prune == c_simple */
1401 * -regex functions --
1403 * True if the whole path of the file matches pattern using
1404 * regular expression.
1407 f_regex(PLAN
*plan
, FTSENT
*entry
)
1414 char errbuf
[LINE_MAX
];
1417 pre
= plan
->re_data
;
1418 str
= entry
->fts_path
;
1425 errcode
= regexec(pre
, str
, 1, &pmatch
, REG_STARTEND
);
1427 if (errcode
!= 0 && errcode
!= REG_NOMATCH
) {
1428 regerror(errcode
, pre
, errbuf
, sizeof errbuf
);
1430 plan
->flags
& F_IGNCASE
? "-iregex" : "-regex", errbuf
);
1433 if (errcode
== 0 && pmatch
.rm_so
== 0 && pmatch
.rm_eo
== len
)
1440 c_regex(OPTION
*option
, char ***argvp
)
1446 char errbuf
[LINE_MAX
];
1448 if ((pre
= malloc(sizeof(regex_t
))) == NULL
)
1451 pattern
= nextarg(option
, argvp
);
1453 if ((errcode
= regcomp(pre
, pattern
,
1454 regexp_flags
| (option
->flags
& F_IGNCASE
? REG_ICASE
: 0))) != 0) {
1455 regerror(errcode
, pre
, errbuf
, sizeof errbuf
);
1456 errx(1, "%s: %s: %s",
1457 option
->flags
& F_IGNCASE
? "-iregex" : "-regex",
1461 new = palloc(option
);
1467 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1470 c_simple(OPTION
*option
, char ***argvp __unused
)
1472 return palloc(option
);
1476 * -size n[c] functions --
1478 * True if the file size in bytes, divided by an implementation defined
1479 * value and rounded up to the next integer, is n. If n is followed by
1480 * one of c k M G T P, the size is in bytes, kilobytes,
1481 * megabytes, gigabytes, terabytes or petabytes respectively.
1483 #define FIND_SIZE 512
1484 static int divsize
= 1;
1487 f_size(PLAN
*plan
, FTSENT
*entry
)
1491 size
= divsize
? (entry
->fts_statp
->st_size
+ FIND_SIZE
- 1) /
1492 FIND_SIZE
: entry
->fts_statp
->st_size
;
1493 COMPARE(size
, plan
->o_data
);
1497 c_size(OPTION
*option
, char ***argvp
)
1504 size_str
= nextarg(option
, argvp
);
1505 ftsoptions
&= ~FTS_NOSTAT
;
1507 new = palloc(option
);
1509 new->o_data
= find_parsenum(new, option
->name
, size_str
, &endch
);
1510 if (endch
!= '\0') {
1514 case 'c': /* characters */
1517 case 'k': /* kilobytes 1<<10 */
1520 case 'M': /* megabytes 1<<20 */
1523 case 'G': /* gigabytes 1<<30 */
1524 scale
= 0x40000000LL
;
1526 case 'T': /* terabytes 1<<40 */
1527 scale
= 0x1000000000LL
;
1529 case 'P': /* petabytes 1<<50 */
1530 scale
= 0x4000000000000LL
;
1533 errx(1, "%s: %s: illegal trailing character",
1534 option
->name
, size_str
);
1537 if (new->o_data
> QUAD_MAX
/ scale
)
1538 errx(1, "%s: %s: value too large",
1539 option
->name
, size_str
);
1540 new->o_data
*= scale
;
1546 * -type c functions --
1548 * True if the type of the file is c, where c is b, c, d, p, f or w
1549 * for block special file, character special file, directory, FIFO,
1550 * regular file or whiteout respectively.
1553 f_type(PLAN
*plan
, FTSENT
*entry
)
1555 return (entry
->fts_statp
->st_mode
& S_IFMT
) == plan
->m_data
;
1559 c_type(OPTION
*option
, char ***argvp
)
1565 typestring
= nextarg(option
, argvp
);
1566 ftsoptions
&= ~FTS_NOSTAT
;
1568 switch (typestring
[0]) {
1593 ftsoptions
|= FTS_WHITEOUT
;
1595 #endif /* FTS_WHITEOUT */
1597 errx(1, "%s: %s: unknown type", option
->name
, typestring
);
1600 new = palloc(option
);
1606 * -user uname functions --
1608 * True if the file belongs to the user uname. If uname is numeric and
1609 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1610 * return a valid user name, uname is taken as a user ID.
1613 f_user(PLAN
*plan
, FTSENT
*entry
)
1615 COMPARE(entry
->fts_statp
->st_uid
, plan
->u_data
);
1619 c_user(OPTION
*option
, char ***argvp
)
1626 username
= nextarg(option
, argvp
);
1627 ftsoptions
&= ~FTS_NOSTAT
;
1629 new = palloc(option
);
1630 p
= getpwnam(username
);
1632 char* cp
= username
;
1633 if( username
[0] == '-' || username
[0] == '+' )
1635 uid
= atoi(username
);
1636 if (uid
== 0 && username
[0] != '0')
1637 errx(1, "%s: %s: no such user", option
->name
, username
);
1638 uid
= find_parsenum(new, option
->name
, cp
, NULL
);
1647 * -xdev functions --
1649 * Always true, causes find not to descend past directories that have a
1650 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1653 c_xdev(OPTION
*option
, char ***argvp __unused
)
1655 ftsoptions
|= FTS_XDEV
;
1657 return palloc(option
);
1661 * ( expression ) functions --
1663 * True if expression is true.
1666 f_expr(PLAN
*plan
, FTSENT
*entry
)
1671 for (p
= plan
->p_data
[0];
1672 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1677 * f_openparen and f_closeparen nodes are temporary place markers. They are
1678 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1679 * to a f_expr node containing the expression and the ')' node is discarded.
1680 * The functions themselves are only used as constants.
1684 f_openparen(PLAN
*plan __unused
, FTSENT
*entry __unused
)
1690 f_closeparen(PLAN
*plan __unused
, FTSENT
*entry __unused
)
1695 /* c_openparen == c_simple */
1696 /* c_closeparen == c_simple */
1699 * AND operator. Since AND is implicit, no node is allocated.
1702 c_and(OPTION
*option __unused
, char ***argvp __unused
)
1708 * ! expression functions --
1710 * Negation of a primary; the unary NOT operator.
1713 f_not(PLAN
*plan
, FTSENT
*entry
)
1718 for (p
= plan
->p_data
[0];
1719 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1723 /* c_not == c_simple */
1726 * expression -o expression functions --
1728 * Alternation of primaries; the OR operator. The second expression is
1729 * not evaluated if the first expression is true.
1732 f_or(PLAN
*plan
, FTSENT
*entry
)
1737 for (p
= plan
->p_data
[0];
1738 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1743 for (p
= plan
->p_data
[1];
1744 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1748 /* c_or == c_simple */
1756 f_false(PLAN
*plan __unused
, FTSENT
*entry __unused
)
1761 /* c_false == c_simple */
1769 f_quit(PLAN
*plan __unused
, FTSENT
*entry __unused
)
1774 /* c_quit == c_simple */