]>
git.saurik.com Git - apple/shell_cmds.git/blob - find/function.c
a7cbd263dbab9358f13026627bd4ba1b26651546
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";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD: src/usr.bin/find/function.c,v 1.60 2008/02/24 00:01:06 imp Exp $");
46 #include <sys/param.h>
47 #include <sys/ucred.h>
49 #include <sys/types.h>
52 #include <sys/mount.h>
53 #include <sys/timeb.h>
73 #include <sys/sysctl.h>
75 #include <get_compat.h>
77 #define COMPAT_MODE(func, mode) 1
80 static PLAN
*palloc(OPTION
*);
81 static long long find_parsenum(PLAN
*, const char *, char *, char *);
82 static long long find_parsetime(PLAN
*, const char *, char *);
83 static char *nextarg(OPTION
*, char ***);
85 extern char **environ
;
87 static PLAN
*lastexecplus
= NULL
;
89 #define COMPARE(a, b) do { \
90 switch (plan->flags & F_ELG_MASK) { \
103 palloc(OPTION
*option
)
107 if ((new = malloc(sizeof(PLAN
))) == NULL
)
109 new->execute
= option
->execute
;
110 new->flags
= option
->flags
;
117 * Parse a string of the form [+-]# and return the value.
120 find_parsenum(PLAN
*plan
, const char *option
, char *vp
, char *endch
)
123 char *endchar
, *str
; /* Pointer to character ending conversion. */
125 /* Determine comparison from leading + or -. */
130 plan
->flags
|= F_GREATER
;
134 plan
->flags
|= F_LESSTHAN
;
137 plan
->flags
|= F_EQUAL
;
142 * Convert the string with strtoq(). Note, if strtoq() returns zero
143 * and endchar points to the beginning of the string we know we have
146 value
= strtoq(str
, &endchar
, 10);
147 if (value
== 0 && endchar
== str
)
148 errx(1, "%s: %s: illegal numeric value", option
, vp
);
149 if (endchar
[0] && endch
== NULL
)
150 errx(1, "%s: %s: illegal trailing character", option
, vp
);
158 * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value.
161 find_parsetime(PLAN
*plan
, const char *option
, char *vp
)
163 long long secs
, value
;
164 char *str
, *unit
; /* Pointer to character ending conversion. */
166 /* Determine comparison from leading + or -. */
171 plan
->flags
|= F_GREATER
;
175 plan
->flags
|= F_LESSTHAN
;
178 plan
->flags
|= F_EQUAL
;
182 value
= strtoq(str
, &unit
, 10);
183 if (value
== 0 && unit
== str
) {
184 errx(1, "%s: %s: illegal time value", option
, vp
);
194 case 's': /* seconds */
197 case 'm': /* minutes */
200 case 'h': /* hours */
201 secs
+= value
* 3600;
204 secs
+= value
* 86400;
206 case 'w': /* weeks */
207 secs
+= value
* 604800;
210 errx(1, "%s: %s: bad unit '%c'", option
, vp
, *unit
);
214 if (*str
== '\0') /* EOS */
216 value
= strtoq(str
, &unit
, 10);
217 if (value
== 0 && unit
== str
) {
218 errx(1, "%s: %s: illegal time value", option
, vp
);
222 errx(1, "%s: %s: missing trailing unit", option
, vp
);
226 plan
->flags
|= F_EXACTTIME
;
232 * Check that another argument still exists, return a pointer to it,
233 * and increment the argument vector pointer.
236 nextarg(OPTION
*option
, char ***argvp
)
240 if ((arg
= **argvp
) == 0)
241 errx(1, "%s: requires additional arguments", option
->name
);
247 * The value of n for the inode times (atime, birthtime, ctime, mtime) is a
248 * range, i.e. n matches from (n - 1) to n 24 hour periods. This interacts
249 * with -n, such that "-mtime -1" would be less than 0 days, which isn't what
250 * the user wanted. Correct so that -1 is "less than 1".
252 #define TIME_CORRECT(p) \
253 if (((p)->flags & F_ELG_MASK) == F_LESSTHAN) \
257 * -[acm]min n functions --
259 * True if the difference between the
260 * file access time (-amin)
261 * file birth time (-Bmin)
262 * last change of file status information (-cmin)
263 * file modification time (-mmin)
264 * and the current time is n min periods.
267 f_Xmin(PLAN
*plan
, FTSENT
*entry
)
269 if (plan
->flags
& F_TIME_C
) {
270 COMPARE((now
- entry
->fts_statp
->st_ctime
+
271 60 - 1) / 60, plan
->t_data
);
272 } else if (plan
->flags
& F_TIME_A
) {
273 COMPARE((now
- entry
->fts_statp
->st_atime
+
274 60 - 1) / 60, plan
->t_data
);
275 } else if (plan
->flags
& F_TIME_B
) {
276 COMPARE((now
- entry
->fts_statp
->st_birthtime
+
277 60 - 1) / 60, plan
->t_data
);
279 COMPARE((now
- entry
->fts_statp
->st_mtime
+
280 60 - 1) / 60, plan
->t_data
);
285 c_Xmin(OPTION
*option
, char ***argvp
)
290 nmins
= nextarg(option
, argvp
);
291 ftsoptions
&= ~FTS_NOSTAT
;
293 new = palloc(option
);
294 new->t_data
= find_parsenum(new, option
->name
, nmins
, NULL
);
300 * -[acm]time n functions --
302 * True if the difference between the
303 * file access time (-atime)
304 * file birth time (-Btime)
305 * last change of file status information (-ctime)
306 * file modification time (-mtime)
307 * and the current time is n 24 hour periods.
311 f_Xtime(PLAN
*plan
, FTSENT
*entry
)
315 if (plan
->flags
& F_TIME_A
)
316 xtime
= entry
->fts_statp
->st_atime
;
317 else if (plan
->flags
& F_TIME_B
)
318 xtime
= entry
->fts_statp
->st_birthtime
;
319 else if (plan
->flags
& F_TIME_C
)
320 xtime
= entry
->fts_statp
->st_ctime
;
322 xtime
= entry
->fts_statp
->st_mtime
;
324 if (plan
->flags
& F_EXACTTIME
)
325 COMPARE(now
- xtime
, plan
->t_data
);
327 COMPARE((now
- xtime
+ (COMPAT_MODE("bin/find", "unix2003") ? 0 : 86400 - 1)) / 86400, plan
->t_data
);
331 c_Xtime(OPTION
*option
, char ***argvp
)
336 value
= nextarg(option
, argvp
);
337 ftsoptions
&= ~FTS_NOSTAT
;
339 new = palloc(option
);
340 new->t_data
= find_parsetime(new, option
->name
, value
);
341 if (!(new->flags
& F_EXACTTIME
) && !COMPAT_MODE("bin/find", "unix2003"))
347 * -maxdepth/-mindepth n functions --
349 * Does the same as -prune if the level of the current file is
350 * greater/less than the specified maximum/minimum depth.
352 * Note that -maxdepth and -mindepth are handled specially in
353 * find_execute() so their f_* functions are set to f_always_true().
356 c_mXXdepth(OPTION
*option
, char ***argvp
)
361 dstr
= nextarg(option
, argvp
);
363 /* all other errors handled by find_parsenum() */
364 errx(1, "%s: %s: value must be positive", option
->name
, dstr
);
366 new = palloc(option
);
367 if (option
->flags
& F_MAXDEPTH
)
368 maxdepth
= find_parsenum(new, option
->name
, dstr
, NULL
);
370 mindepth
= find_parsenum(new, option
->name
, dstr
, NULL
);
378 * Show files with EXTENDED ACL attributes.
381 f_acl(PLAN
*plan __unused
, FTSENT
*entry
)
387 if (S_ISLNK(entry
->fts_statp
->st_mode
))
389 if ((match
= pathconf(entry
->fts_accpath
, _PC_ACL_EXTENDED
)) <= 0) {
390 if (match
< 0 && errno
!= EINVAL
)
391 warn("%s", entry
->fts_accpath
);
396 if ((facl
= acl_get_file(entry
->fts_accpath
,ACL_TYPE_ACCESS
)) != NULL
) {
397 if (acl_get_entry(facl
, ACL_FIRST_ENTRY
, &ae
) == 1) {
399 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS
400 * must have at least three entries (owner, group,
404 while (acl_get_entry(facl
, ACL_NEXT_ENTRY
, &ae
) == 1) {
413 warn("%s", entry
->fts_accpath
);
418 c_acl(OPTION
*option
, char ***argvp __unused
)
420 ftsoptions
&= ~FTS_NOSTAT
;
421 return (palloc(option
));
423 #endif /* !__APPLE__ */
426 * -delete functions --
428 * True always. Makes its best shot and continues on regardless.
431 f_delete(PLAN
*plan __unused
, FTSENT
*entry
)
433 /* ignore these from fts */
434 if (strcmp(entry
->fts_accpath
, ".") == 0 ||
435 strcmp(entry
->fts_accpath
, "..") == 0)
439 if (isdepth
== 0 || /* depth off */
440 (ftsoptions
& FTS_NOSTAT
) || /* not stat()ing */
441 !(ftsoptions
& FTS_PHYSICAL
) || /* physical off */
442 (ftsoptions
& FTS_LOGICAL
)) /* or finally, logical on */
443 errx(1, "-delete: insecure options got turned on");
445 /* Potentially unsafe - do not accept relative paths whatsoever */
446 if (strchr(entry
->fts_accpath
, '/') != NULL
)
447 errx(1, "-delete: %s: relative path potentially not safe",
450 /* Turn off user immutable bits if running as root */
451 if ((entry
->fts_statp
->st_flags
& (UF_APPEND
|UF_IMMUTABLE
)) &&
452 !(entry
->fts_statp
->st_flags
& (SF_APPEND
|SF_IMMUTABLE
)) &&
454 chflags(entry
->fts_accpath
,
455 entry
->fts_statp
->st_flags
&= ~(UF_APPEND
|UF_IMMUTABLE
));
457 /* rmdir directories, unlink everything else */
458 if (S_ISDIR(entry
->fts_statp
->st_mode
)) {
459 if (rmdir(entry
->fts_accpath
) < 0 && errno
!= ENOTEMPTY
)
460 warn("-delete: rmdir(%s)", entry
->fts_path
);
462 if (unlink(entry
->fts_accpath
) < 0)
463 warn("-delete: unlink(%s)", entry
->fts_path
);
471 c_delete(OPTION
*option
, char ***argvp __unused
)
474 ftsoptions
&= ~FTS_NOSTAT
; /* no optimise */
475 ftsoptions
|= FTS_PHYSICAL
; /* disable -follow */
476 ftsoptions
&= ~FTS_LOGICAL
; /* disable -follow */
477 isoutput
= 1; /* possible output */
478 isdepth
= 1; /* -depth implied */
480 return palloc(option
);
487 * Always true, used for -maxdepth, -mindepth, -xdev, -follow, and -true
490 f_always_true(PLAN
*plan __unused
, FTSENT
*entry __unused
)
496 * -depth functions --
498 * With argument: True if the file is at level n.
499 * Without argument: Always true, causes descent of the directory hierarchy
500 * to be done so that all entries in a directory are acted on before the
504 f_depth(PLAN
*plan
, FTSENT
*entry
)
506 if (plan
->flags
& F_DEPTH
)
507 COMPARE(entry
->fts_level
, plan
->d_data
);
513 c_depth(OPTION
*option
, char ***argvp
)
518 new = palloc(option
);
521 if (str
&& !(new->flags
& F_DEPTH
)) {
522 /* skip leading + or - */
523 if (*str
== '+' || *str
== '-')
526 if (*str
== '+' || *str
== '-')
529 new->flags
|= F_DEPTH
;
532 if (new->flags
& F_DEPTH
) { /* -depth n */
535 ndepth
= nextarg(option
, argvp
);
536 new->d_data
= find_parsenum(new, option
->name
, ndepth
, NULL
);
545 * -empty functions --
547 * True if the file or directory is empty
550 f_empty(PLAN
*plan __unused
, FTSENT
*entry
)
552 if (S_ISREG(entry
->fts_statp
->st_mode
) &&
553 entry
->fts_statp
->st_size
== 0)
555 if (S_ISDIR(entry
->fts_statp
->st_mode
)) {
561 dir
= opendir(entry
->fts_accpath
);
563 err(1, "%s", entry
->fts_accpath
);
564 for (dp
= readdir(dir
); dp
; dp
= readdir(dir
))
565 if (dp
->d_name
[0] != '.' ||
566 (dp
->d_name
[1] != '\0' &&
567 (dp
->d_name
[1] != '.' || dp
->d_name
[2] != '\0'))) {
578 c_empty(OPTION
*option
, char ***argvp __unused
)
580 ftsoptions
&= ~FTS_NOSTAT
;
582 return palloc(option
);
586 * [-exec | -execdir | -ok] utility [arg ... ] ; functions --
588 * True if the executed utility returns a zero value as exit status.
589 * The end of the primary expression is delimited by a semicolon. If
590 * "{}" occurs anywhere, it gets replaced by the current pathname,
591 * or, in the case of -execdir, the current basename (filename
592 * without leading directory prefix). For -exec and -ok,
593 * the current directory for the execution of utility is the same as
594 * the current directory when the find utility was started, whereas
595 * for -execdir, it is the directory the file resides in.
597 * The primary -ok differs from -exec in that it requests affirmation
598 * of the user before executing the utility.
601 f_exec(PLAN
*plan
, FTSENT
*entry
)
608 if (entry
== NULL
&& plan
->flags
& F_EXECPLUS
) {
609 if (plan
->e_ppos
== plan
->e_pbnum
)
611 plan
->e_argv
[plan
->e_ppos
] = NULL
;
615 /* XXX - if file/dir ends in '/' this will not work -- can it? */
616 if ((plan
->flags
& F_EXECDIR
) && \
617 (file
= strrchr(entry
->fts_path
, '/')))
620 file
= entry
->fts_path
;
622 if (plan
->flags
& F_EXECPLUS
) {
623 if ((plan
->e_argv
[plan
->e_ppos
] = strdup(file
)) == NULL
)
625 plan
->e_len
[plan
->e_ppos
] = strlen(file
);
626 plan
->e_psize
+= plan
->e_len
[plan
->e_ppos
];
627 if (++plan
->e_ppos
< plan
->e_pnummax
&&
628 plan
->e_psize
< plan
->e_psizemax
)
630 plan
->e_argv
[plan
->e_ppos
] = NULL
;
632 for (cnt
= 0; plan
->e_argv
[cnt
]; ++cnt
)
633 if (plan
->e_len
[cnt
])
634 brace_subst(plan
->e_orig
[cnt
],
635 &plan
->e_argv
[cnt
], file
,
639 doexec
: if ((plan
->flags
& F_NEEDOK
) && !queryuser(plan
->e_argv
))
642 /* make sure find output is interspersed correctly with subprocesses */
646 switch (pid
= fork()) {
651 /* change dir back from where we started */
652 if (!(plan
->flags
& F_EXECDIR
) && fchdir(dotfd
)) {
656 execvp(plan
->e_argv
[0], plan
->e_argv
);
657 warn("%s", plan
->e_argv
[0]);
660 if (plan
->flags
& F_EXECPLUS
) {
661 while (--plan
->e_ppos
>= plan
->e_pbnum
)
662 free(plan
->e_argv
[plan
->e_ppos
]);
663 plan
->e_ppos
= plan
->e_pbnum
;
664 plan
->e_psize
= plan
->e_pbsize
;
666 pid
= waitpid(pid
, &status
, 0);
667 if (plan
->flags
& F_EXECPLUS
&& WIFEXITED(status
) && WEXITSTATUS(status
))
668 _exit(WEXITSTATUS(status
));
669 return (pid
!= -1 && WIFEXITED(status
) && !WEXITSTATUS(status
));
673 * c_exec, c_execdir, c_ok --
674 * build three parallel arrays, one with pointers to the strings passed
675 * on the command line, one with (possibly duplicated) pointers to the
676 * argv array, and one with integer values that are lengths of the
677 * strings, but also flags meaning that the string has to be massaged.
680 c_exec(OPTION
*option
, char ***argvp
)
682 PLAN
*new; /* node returned */
685 char **argv
, **ap
, **ep
, *p
;
687 /* XXX - was in c_execdir, but seems unnecessary!?
688 ftsoptions &= ~FTS_NOSTAT;
692 /* XXX - this is a change from the previous coding */
693 new = palloc(option
);
695 for (ap
= argv
= *argvp
;; ++ap
) {
698 "%s: no terminating \";\" or \"+\"", option
->name
);
701 if (**ap
== '+' && ap
!= argv
&& strcmp(*(ap
- 1), "{}") == 0) {
702 new->flags
|= F_EXECPLUS
;
708 errx(1, "%s: no command specified", option
->name
);
710 cnt
= ap
- *argvp
+ 1;
711 if (new->flags
& F_EXECPLUS
) {
712 new->e_ppos
= new->e_pbnum
= cnt
- 2;
713 if ((argmax
= sysconf(_SC_ARG_MAX
)) == -1) {
714 warn("sysconf(_SC_ARG_MAX)");
715 argmax
= _POSIX_ARG_MAX
;
718 for (ep
= environ
; *ep
!= NULL
; ep
++)
719 argmax
-= strlen(*ep
) + 1 + sizeof(*ep
);
720 argmax
-= 1 + sizeof(*ep
);
721 new->e_pnummax
= argmax
/ 16;
722 argmax
-= sizeof(char *) * new->e_pnummax
;
724 errx(1, "no space for arguments");
725 new->e_psizemax
= argmax
;
727 cnt
+= new->e_pnummax
+ 1;
728 new->e_next
= lastexecplus
;
731 if ((new->e_argv
= malloc(cnt
* sizeof(char *))) == NULL
)
733 if ((new->e_orig
= malloc(cnt
* sizeof(char *))) == NULL
)
735 if ((new->e_len
= malloc(cnt
* sizeof(int))) == NULL
)
738 for (argv
= *argvp
, cnt
= 0; argv
< ap
; ++argv
, ++cnt
) {
739 new->e_orig
[cnt
] = *argv
;
740 if (new->flags
& F_EXECPLUS
)
741 new->e_pbsize
+= strlen(*argv
) + 1;
742 for (p
= *argv
; *p
; ++p
)
743 if (!(new->flags
& F_EXECPLUS
) && p
[0] == '{' &&
745 if ((new->e_argv
[cnt
] =
746 malloc(MAXPATHLEN
)) == NULL
)
748 new->e_len
[cnt
] = MAXPATHLEN
;
752 new->e_argv
[cnt
] = *argv
;
756 if (new->flags
& F_EXECPLUS
) {
757 new->e_psize
= new->e_pbsize
;
759 for (i
= 0; i
< new->e_pnummax
; i
++) {
760 new->e_argv
[cnt
] = NULL
;
767 new->e_argv
[cnt
] = new->e_orig
[cnt
] = NULL
;
769 done
: *argvp
= argv
+ 1;
773 /* Finish any pending -exec ... {} + functions. */
781 (p
->execute
)(p
, NULL
);
787 f_flags(PLAN
*plan
, FTSENT
*entry
)
791 flags
= entry
->fts_statp
->st_flags
;
792 if (plan
->flags
& F_ATLEAST
)
793 return (flags
| plan
->fl_flags
) == flags
&&
794 !(flags
& plan
->fl_notflags
);
795 else if (plan
->flags
& F_ANY
)
796 return (flags
& plan
->fl_flags
) ||
797 (flags
| plan
->fl_notflags
) != flags
;
799 return flags
== plan
->fl_flags
&&
800 !(plan
->fl_flags
& plan
->fl_notflags
);
804 c_flags(OPTION
*option
, char ***argvp
)
808 u_long flags
, notflags
;
810 flags_str
= nextarg(option
, argvp
);
811 ftsoptions
&= ~FTS_NOSTAT
;
813 new = palloc(option
);
815 if (*flags_str
== '-') {
816 new->flags
|= F_ATLEAST
;
818 } else if (*flags_str
== '+') {
822 if (strtofflags(&flags_str
, &flags
, ¬flags
) == 1)
823 errx(1, "%s: %s: illegal flags string", option
->name
, flags_str
);
825 new->fl_flags
= flags
;
826 new->fl_notflags
= notflags
;
831 * -follow functions --
833 * Always true, causes symbolic links to be followed on a global
837 c_follow(OPTION
*option
, char ***argvp __unused
)
839 ftsoptions
&= ~FTS_PHYSICAL
;
840 ftsoptions
|= FTS_LOGICAL
;
842 return palloc(option
);
846 * -fstype functions --
848 * True if the file is of a certain type.
851 f_fstype(PLAN
*plan
, FTSENT
*entry
)
853 static dev_t curdev
; /* need a guaranteed illegal dev value */
854 static int first
= 1;
856 static int val_type
, val_flags
;
857 char *p
, save
[2] = {0,0};
859 if ((plan
->flags
& F_MTMASK
) == F_MTUNKNOWN
)
862 /* Only check when we cross mount point. */
863 if (first
|| curdev
!= entry
->fts_statp
->st_dev
) {
864 curdev
= entry
->fts_statp
->st_dev
;
867 * Statfs follows symlinks; find wants the link's filesystem,
868 * not where it points.
870 if (entry
->fts_info
== FTS_SL
||
871 entry
->fts_info
== FTS_SLNONE
) {
872 if ((p
= strrchr(entry
->fts_accpath
, '/')) != NULL
)
875 p
= entry
->fts_accpath
;
883 if (statfs(entry
->fts_accpath
, &sb
))
884 err(1, "%s", entry
->fts_accpath
);
894 * Further tests may need both of these values, so
895 * always copy both of them.
897 val_flags
= sb
.f_flags
;
898 val_type
= sb
.f_type
;
900 switch (plan
->flags
& F_MTMASK
) {
902 return val_flags
& plan
->mt_data
;
904 return val_type
== plan
->mt_data
;
911 c_fstype(OPTION
*option
, char ***argvp
)
917 fsname
= nextarg(option
, argvp
);
918 ftsoptions
&= ~FTS_NOSTAT
;
920 new = palloc(option
);
923 * Check first for a filesystem name.
925 if (getvfsbyname(fsname
, &vfc
) == 0) {
926 new->flags
|= F_MTTYPE
;
927 new->mt_data
= vfc
.vfc_typenum
;
933 if (!strcmp(fsname
, "local")) {
934 new->flags
|= F_MTFLAG
;
935 new->mt_data
= MNT_LOCAL
;
940 if (!strcmp(fsname
, "rdonly")) {
941 new->flags
|= F_MTFLAG
;
942 new->mt_data
= MNT_RDONLY
;
949 * We need to make filesystem checks for filesystems
950 * that exists but aren't in the kernel work.
952 fprintf(stderr
, "Warning: Unknown filesystem type %s\n", fsname
);
953 new->flags
|= F_MTUNKNOWN
;
958 * -group gname functions --
960 * True if the file belongs to the group gname. If gname is numeric and
961 * an equivalent of the getgrnam() function does not return a valid group
962 * name, gname is taken as a group ID.
965 f_group(PLAN
*plan
, FTSENT
*entry
)
967 COMPARE(entry
->fts_statp
->st_gid
, plan
->g_data
);
971 c_group(OPTION
*option
, char ***argvp
)
978 gname
= nextarg(option
, argvp
);
979 ftsoptions
&= ~FTS_NOSTAT
;
981 new = palloc(option
);
985 if (gname
[0] == '-' || gname
[0] == '+')
988 if (gid
== 0 && gname
[0] != '0')
989 errx(1, "%s: %s: no such group", option
->name
, gname
);
990 gid
= find_parsenum(new, option
->name
, cp
, NULL
);
999 * -inum n functions --
1001 * True if the file has inode # n.
1004 f_inum(PLAN
*plan
, FTSENT
*entry
)
1006 COMPARE(entry
->fts_statp
->st_ino
, plan
->i_data
);
1010 c_inum(OPTION
*option
, char ***argvp
)
1015 inum_str
= nextarg(option
, argvp
);
1016 ftsoptions
&= ~FTS_NOSTAT
;
1018 new = palloc(option
);
1019 new->i_data
= find_parsenum(new, option
->name
, inum_str
, NULL
);
1026 * True if the file has the same inode (eg hard link) FN
1029 /* f_samefile is just f_inum */
1031 c_samefile(OPTION
*option
, char ***argvp
)
1037 fn
= nextarg(option
, argvp
);
1038 ftsoptions
&= ~FTS_NOSTAT
;
1040 new = palloc(option
);
1043 new->i_data
= sb
.st_ino
;
1048 * -links n functions --
1050 * True if the file has n links.
1053 f_links(PLAN
*plan
, FTSENT
*entry
)
1055 COMPARE(entry
->fts_statp
->st_nlink
, plan
->l_data
);
1059 c_links(OPTION
*option
, char ***argvp
)
1064 nlinks
= nextarg(option
, argvp
);
1065 ftsoptions
&= ~FTS_NOSTAT
;
1067 new = palloc(option
);
1068 new->l_data
= (nlink_t
)find_parsenum(new, option
->name
, nlinks
, NULL
);
1075 * Always true - prints the current entry to stdout in "ls" format.
1078 f_ls(PLAN
*plan __unused
, FTSENT
*entry
)
1080 printlong(entry
->fts_path
, entry
->fts_accpath
, entry
->fts_statp
);
1085 c_ls(OPTION
*option
, char ***argvp __unused
)
1087 ftsoptions
&= ~FTS_NOSTAT
;
1090 return palloc(option
);
1094 * -name functions --
1096 * True if the basename of the filename being examined
1097 * matches pattern using Pattern Matching Notation S3.14
1100 f_name(PLAN
*plan
, FTSENT
*entry
)
1105 if (plan
->flags
& F_LINK
) {
1107 if (readlink(entry
->fts_path
, fn
, sizeof(fn
)) == -1)
1109 } else if (entry
->fts_namelen
== 0) {
1110 name
= basename(entry
->fts_path
);
1112 name
= entry
->fts_name
;
1113 return !fnmatch(plan
->c_data
, name
,
1114 plan
->flags
& F_IGNCASE
? FNM_CASEFOLD
: 0);
1118 c_name(OPTION
*option
, char ***argvp
)
1123 pattern
= nextarg(option
, argvp
);
1124 new = palloc(option
);
1125 new->c_data
= pattern
;
1130 * -newer file functions --
1132 * True if the current file has been modified more recently
1133 * then the modification time of the file named by the pathname
1137 f_newer(PLAN
*plan
, FTSENT
*entry
)
1139 if (plan
->flags
& F_TIME_C
)
1140 return entry
->fts_statp
->st_ctime
> plan
->t_data
;
1141 else if (plan
->flags
& F_TIME_A
)
1142 return entry
->fts_statp
->st_atime
> plan
->t_data
;
1143 else if (plan
->flags
& F_TIME_B
)
1144 return entry
->fts_statp
->st_birthtime
> plan
->t_data
;
1146 return entry
->fts_statp
->st_mtime
> plan
->t_data
;
1150 c_newer(OPTION
*option
, char ***argvp
)
1156 fn_or_tspec
= nextarg(option
, argvp
);
1157 ftsoptions
&= ~FTS_NOSTAT
;
1159 new = palloc(option
);
1160 /* compare against what */
1161 if (option
->flags
& F_TIME2_T
) {
1162 new->t_data
= get_date(fn_or_tspec
, (struct timeb
*) 0);
1163 if (new->t_data
== (time_t) -1)
1164 errx(1, "Can't parse date/time: %s", fn_or_tspec
);
1166 if (stat(fn_or_tspec
, &sb
))
1167 err(1, "%s", fn_or_tspec
);
1168 if (option
->flags
& F_TIME2_C
)
1169 new->t_data
= sb
.st_ctime
;
1170 else if (option
->flags
& F_TIME2_A
)
1171 new->t_data
= sb
.st_atime
;
1173 new->t_data
= sb
.st_mtime
;
1179 * -nogroup functions --
1181 * True if file belongs to a user ID for which the equivalent
1182 * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL.
1185 f_nogroup(PLAN
*plan __unused
, FTSENT
*entry
)
1187 return group_from_gid(entry
->fts_statp
->st_gid
, 1) == NULL
;
1191 c_nogroup(OPTION
*option
, char ***argvp __unused
)
1193 ftsoptions
&= ~FTS_NOSTAT
;
1195 return palloc(option
);
1199 * -nouser functions --
1201 * True if file belongs to a user ID for which the equivalent
1202 * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL.
1205 f_nouser(PLAN
*plan __unused
, FTSENT
*entry
)
1207 return user_from_uid(entry
->fts_statp
->st_uid
, 1) == NULL
;
1211 c_nouser(OPTION
*option
, char ***argvp __unused
)
1213 ftsoptions
&= ~FTS_NOSTAT
;
1215 return palloc(option
);
1219 * -path functions --
1221 * True if the path of the filename being examined
1222 * matches pattern using Pattern Matching Notation S3.14
1225 f_path(PLAN
*plan
, FTSENT
*entry
)
1227 return !fnmatch(plan
->c_data
, entry
->fts_path
,
1228 plan
->flags
& F_IGNCASE
? FNM_CASEFOLD
: 0);
1231 /* c_path is the same as c_name */
1234 * -perm functions --
1236 * The mode argument is used to represent file mode bits. If it starts
1237 * with a leading digit, it's treated as an octal mode, otherwise as a
1241 f_perm(PLAN
*plan
, FTSENT
*entry
)
1245 mode
= entry
->fts_statp
->st_mode
&
1246 (S_ISUID
|S_ISGID
|S_ISTXT
|S_IRWXU
|S_IRWXG
|S_IRWXO
);
1247 if (plan
->flags
& F_ATLEAST
)
1248 return (plan
->m_data
| mode
) == mode
;
1249 else if (plan
->flags
& F_ANY
)
1250 return (mode
& plan
->m_data
);
1252 return mode
== plan
->m_data
;
1257 c_perm(OPTION
*option
, char ***argvp
)
1263 perm
= nextarg(option
, argvp
);
1264 ftsoptions
&= ~FTS_NOSTAT
;
1266 new = palloc(option
);
1269 new->flags
|= F_ATLEAST
;
1271 } else if (*perm
== '+') {
1272 if ((set
= setmode(perm
+ 1)) != NULL
) {
1273 new->flags
|= F_ANY
;
1279 if ((set
= setmode(perm
)) == NULL
)
1280 errx(1, "%s: %s: illegal mode string", option
->name
, perm
);
1282 new->m_data
= getmode(set
, 0);
1288 * -print functions --
1290 * Always true, causes the current pathname to be written to
1294 f_print(PLAN
*plan __unused
, FTSENT
*entry
)
1296 (void)puts(entry
->fts_path
);
1301 c_print(OPTION
*option
, char ***argvp __unused
)
1305 return palloc(option
);
1309 * -print0 functions --
1311 * Always true, causes the current pathname to be written to
1312 * standard output followed by a NUL character
1315 f_print0(PLAN
*plan __unused
, FTSENT
*entry
)
1317 fputs(entry
->fts_path
, stdout
);
1318 fputc('\0', stdout
);
1322 /* c_print0 is the same as c_print */
1325 * -prune functions --
1327 * Prune a portion of the hierarchy.
1330 f_prune(PLAN
*plan __unused
, FTSENT
*entry
)
1332 if (fts_set(tree
, entry
, FTS_SKIP
))
1333 err(1, "%s", entry
->fts_path
);
1337 /* c_prune == c_simple */
1340 * -regex functions --
1342 * True if the whole path of the file matches pattern using
1343 * regular expression.
1346 f_regex(PLAN
*plan
, FTSENT
*entry
)
1353 char errbuf
[LINE_MAX
];
1356 pre
= plan
->re_data
;
1357 str
= entry
->fts_path
;
1364 errcode
= regexec(pre
, str
, 1, &pmatch
, REG_STARTEND
);
1366 if (errcode
!= 0 && errcode
!= REG_NOMATCH
) {
1367 regerror(errcode
, pre
, errbuf
, sizeof errbuf
);
1369 plan
->flags
& F_IGNCASE
? "-iregex" : "-regex", errbuf
);
1372 if (errcode
== 0 && pmatch
.rm_so
== 0 && pmatch
.rm_eo
== len
)
1379 c_regex(OPTION
*option
, char ***argvp
)
1385 char errbuf
[LINE_MAX
];
1387 if ((pre
= malloc(sizeof(regex_t
))) == NULL
)
1390 pattern
= nextarg(option
, argvp
);
1392 if ((errcode
= regcomp(pre
, pattern
,
1393 regexp_flags
| (option
->flags
& F_IGNCASE
? REG_ICASE
: 0))) != 0) {
1394 regerror(errcode
, pre
, errbuf
, sizeof errbuf
);
1395 errx(1, "%s: %s: %s",
1396 option
->flags
& F_IGNCASE
? "-iregex" : "-regex",
1400 new = palloc(option
);
1406 /* c_simple covers c_prune, c_openparen, c_closeparen, c_not, c_or, c_true, c_false */
1409 c_simple(OPTION
*option
, char ***argvp __unused
)
1411 return palloc(option
);
1415 * -size n[c] functions --
1417 * True if the file size in bytes, divided by an implementation defined
1418 * value and rounded up to the next integer, is n. If n is followed by
1419 * one of c k M G T P, the size is in bytes, kilobytes,
1420 * megabytes, gigabytes, terabytes or petabytes respectively.
1422 #define FIND_SIZE 512
1423 static int divsize
= 1;
1426 f_size(PLAN
*plan
, FTSENT
*entry
)
1430 size
= divsize
? (entry
->fts_statp
->st_size
+ FIND_SIZE
- 1) /
1431 FIND_SIZE
: entry
->fts_statp
->st_size
;
1432 COMPARE(size
, plan
->o_data
);
1436 c_size(OPTION
*option
, char ***argvp
)
1443 size_str
= nextarg(option
, argvp
);
1444 ftsoptions
&= ~FTS_NOSTAT
;
1446 new = palloc(option
);
1448 new->o_data
= find_parsenum(new, option
->name
, size_str
, &endch
);
1449 if (endch
!= '\0') {
1453 case 'c': /* characters */
1456 case 'k': /* kilobytes 1<<10 */
1459 case 'M': /* megabytes 1<<20 */
1462 case 'G': /* gigabytes 1<<30 */
1463 scale
= 0x40000000LL
;
1465 case 'T': /* terabytes 1<<40 */
1466 scale
= 0x1000000000LL
;
1468 case 'P': /* petabytes 1<<50 */
1469 scale
= 0x4000000000000LL
;
1472 errx(1, "%s: %s: illegal trailing character",
1473 option
->name
, size_str
);
1476 if (new->o_data
> QUAD_MAX
/ scale
)
1477 errx(1, "%s: %s: value too large",
1478 option
->name
, size_str
);
1479 new->o_data
*= scale
;
1485 * -type c functions --
1487 * True if the type of the file is c, where c is b, c, d, p, f or w
1488 * for block special file, character special file, directory, FIFO,
1489 * regular file or whiteout respectively.
1492 f_type(PLAN
*plan
, FTSENT
*entry
)
1494 return (entry
->fts_statp
->st_mode
& S_IFMT
) == plan
->m_data
;
1498 c_type(OPTION
*option
, char ***argvp
)
1504 typestring
= nextarg(option
, argvp
);
1505 ftsoptions
&= ~FTS_NOSTAT
;
1507 switch (typestring
[0]) {
1532 ftsoptions
|= FTS_WHITEOUT
;
1534 #endif /* FTS_WHITEOUT */
1536 errx(1, "%s: %s: unknown type", option
->name
, typestring
);
1539 new = palloc(option
);
1545 * -user uname functions --
1547 * True if the file belongs to the user uname. If uname is numeric and
1548 * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not
1549 * return a valid user name, uname is taken as a user ID.
1552 f_user(PLAN
*plan
, FTSENT
*entry
)
1554 COMPARE(entry
->fts_statp
->st_uid
, plan
->u_data
);
1558 c_user(OPTION
*option
, char ***argvp
)
1565 username
= nextarg(option
, argvp
);
1566 ftsoptions
&= ~FTS_NOSTAT
;
1568 new = palloc(option
);
1569 p
= getpwnam(username
);
1571 char* cp
= username
;
1572 if( username
[0] == '-' || username
[0] == '+' )
1574 uid
= atoi(username
);
1575 if (uid
== 0 && username
[0] != '0')
1576 errx(1, "%s: %s: no such user", option
->name
, username
);
1577 uid
= find_parsenum(new, option
->name
, cp
, NULL
);
1586 * -xdev functions --
1588 * Always true, causes find not to descend past directories that have a
1589 * different device ID (st_dev, see stat() S5.6.2 [POSIX.1])
1592 c_xdev(OPTION
*option
, char ***argvp __unused
)
1594 ftsoptions
|= FTS_XDEV
;
1596 return palloc(option
);
1600 * ( expression ) functions --
1602 * True if expression is true.
1605 f_expr(PLAN
*plan
, FTSENT
*entry
)
1610 for (p
= plan
->p_data
[0];
1611 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1616 * f_openparen and f_closeparen nodes are temporary place markers. They are
1617 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
1618 * to a f_expr node containing the expression and the ')' node is discarded.
1619 * The functions themselves are only used as constants.
1623 f_openparen(PLAN
*plan __unused
, FTSENT
*entry __unused
)
1629 f_closeparen(PLAN
*plan __unused
, FTSENT
*entry __unused
)
1634 /* c_openparen == c_simple */
1635 /* c_closeparen == c_simple */
1638 * AND operator. Since AND is implicit, no node is allocated.
1641 c_and(OPTION
*option __unused
, char ***argvp __unused
)
1647 * ! expression functions --
1649 * Negation of a primary; the unary NOT operator.
1652 f_not(PLAN
*plan
, FTSENT
*entry
)
1657 for (p
= plan
->p_data
[0];
1658 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1662 /* c_not == c_simple */
1665 * expression -o expression functions --
1667 * Alternation of primaries; the OR operator. The second expression is
1668 * not evaluated if the first expression is true.
1671 f_or(PLAN
*plan
, FTSENT
*entry
)
1676 for (p
= plan
->p_data
[0];
1677 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1682 for (p
= plan
->p_data
[1];
1683 p
&& (state
= (p
->execute
)(p
, entry
)); p
= p
->next
);
1687 /* c_or == c_simple */
1695 f_false(PLAN
*plan __unused
, FTSENT
*entry __unused
)
1700 /* c_false == c_simple */
1708 f_quit(PLAN
*plan __unused
, FTSENT
*entry __unused
)
1713 /* c_quit == c_simple */