* Copyright (c) 1990 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Cimarron D. Taylor of the University of California, Berkeley.
* %sccs.include.redist.c%
"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)find.c 4.26 (Berkeley) %G%";
FTS
*tree
; /* pointer to top of FTS hierarchy */
time_t now
; /* time find was run */
dev_t curdev
= (dev_t
)-1; /* device number of current tree */
int ftsoptions
; /* options passed to ftsopen() */
int deprecated
; /* old or new syntax */
int depth
; /* set by -depth option */
int output_specified
; /* one of -print, -ok or -exec was specified */
int xdev
; /* set by -xdev option */
(void)time(&now
); /* initialize the time-of-day */
ftsoptions
= FTS_MULTIPLE
|FTS_NOSTAT
|FTS_PHYSICAL
;
* if arguments start with an option, it's new syntax; otherwise,
* if has a "-option" anywhere it must be old syntax.
for (p
= argv
+ 1; *p
; ++p
)
plan
= find_formplan(argv
); /* execution plan */
find_execute(plan
, paths
);
* process the command line and create a "plan" corresponding to the
PLAN
*c_print(), *find_create(), *find_squish_not(), *find_squish_or();
PLAN
*find_squish_paren();
* for each argument in the command line, determine what kind of node
* it is, create the appropriate node type and add the new plan node
* to the end of the existing plan. The resulting plan is a linked
* list of plan nodes. For example, the string:
* % find . -name foo -newer bar -print
* [-name foo]--> [-newer bar]--> [-print]
* in this diagram, `[-name foo]' represents the plan node generated
* by c_name() with an argument of foo and `-->' represents the
for (plan
= NULL
; *argv
;) {
new = find_create(&argv
);
* if the user didn't specify one of -print, -ok or -exec, then -print
* is assumed so we add a -print node on the end. It is possible that
* the user might want the -print someplace else on the command line,
* but there's no way to know that.
* the command line has been completely processed into a search plan
* except for the (, ), !, and -o operators. Rearrange the plan so
* that the portions of the plan which are affected by the operators
* are moved into operator nodes themselves. For example:
* [!]--> [-name foo]--> [-print]
* [! [-name foo] ]--> [-print]
* [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
* [expr [-depth]-->[-name foo] ]--> [-print]
* operators are handled in order of precedence.
plan
= find_squish_paren(plan
); /* ()'s */
plan
= find_squish_not(plan
); /* !'s */
plan
= find_squish_or(plan
); /* -o's */
* take a search plan and an array of search paths and executes the plan
* over all FTSENT's returned for the given search paths.
find_execute(plan
, paths
)
PLAN
*plan
; /* search plan */
char **paths
; /* array of pathnames to traverse */
FTSENT
*entry
; /* current fts entry */
if (!(tree
= ftsopen(paths
, ftsoptions
, NULL
))) {
(void)fprintf(stderr
, "find: ftsopen: %s.\n", strerror(errno
));
while (entry
= ftsread(tree
)) {
"find: %s: unable to read.\n", entry
->path
);
"find: %s: unable to search.\n", entry
->path
);
"find: %s: %s.\n", entry
->path
, strerror(errno
));
"find: directory cycle: %s.\n", entry
->path
);
if (!(ftsoptions
& FTS_NOSTAT
)) {
"find: can't stat: %s.\n", entry
->path
);
/* always keep curdev up to date, -fstype uses it. */
if (xdev
&& curdev
!= entry
->statb
.st_dev
&&
curdev
!= -1 && ftsset(tree
, entry
, FTS_SKIP
)) {
(void)fprintf(stderr
, "find: %s: %s.\n",
entry
->path
, strerror(errno
));
* call all the functions in the execution plan until one is
* false or all have been executed. This is where we do all
* the work specified by the user on the command line.
for (p
= plan
; p
&& (p
->eval
)(p
, entry
); p
= p
->next
);
curdev
= entry
->statb
.st_dev
;