2 * Copyright (c) 1991, 1993, 1994
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
36 * @(#)find.c 8.5 (Berkeley) 8/5/94
37 * $FreeBSD: src/usr.bin/find/find.c,v 1.7.6.3 2001/05/06 09:53:22 phk Exp $
38 * $DragonFly: src/usr.bin/find/find.c,v 1.2 2003/06/17 04:29:26 dillon Exp $
41 #include <sys/types.h>
54 static int find_compare __P((const FTSENT **s1, const FTSENT **s2));
58 * tell fts_open() how to order the traversal of the hierarchy.
59 * This variant gives lexicographical order, i.e., alphabetical
60 * order within each directory.
64 const FTSENT **s1, **s2;
67 return (strcoll((*s1)->fts_name, (*s2)->fts_name));
72 * process the command line and create a "plan" corresponding to the
79 PLAN *plan, *tail, *new;
82 * for each argument in the command line, determine what kind of node
83 * it is, create the appropriate node type and add the new plan node
84 * to the end of the existing plan. The resulting plan is a linked
85 * list of plan nodes. For example, the string:
87 * % find . -name foo -newer bar -print
89 * results in the plan:
91 * [-name foo]--> [-newer bar]--> [-print]
93 * in this diagram, `[-name foo]' represents the plan node generated
94 * by c_name() with an argument of foo and `-->' represents the
97 for (plan = tail = NULL; *argv;) {
98 if (!(new = find_create(&argv)))
109 * if the user didn't specify one of -print, -ok or -exec, then -print
110 * is assumed so we bracket the current expression with parens, if
111 * necessary, and add a -print node on the end.
118 p = option("-print");
119 new = (p->create)(p, &argv);
123 new = (p->create)(p, &argv);
127 new = (p->create)(p, &argv);
130 p = option("-print");
131 new = (p->create)(p, &argv);
138 * the command line has been completely processed into a search plan
139 * except for the (, ), !, and -o operators. Rearrange the plan so
140 * that the portions of the plan which are affected by the operators
141 * are moved into operator nodes themselves. For example:
143 * [!]--> [-name foo]--> [-print]
147 * [! [-name foo] ]--> [-print]
151 * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
155 * [expr [-depth]-->[-name foo] ]--> [-print]
157 * operators are handled in order of precedence.
160 plan = paren_squish(plan); /* ()'s */
161 plan = not_squish(plan); /* !'s */
162 plan = or_squish(plan); /* -o's */
166 FTS *tree; /* pointer to top of FTS hierarchy */
170 * take a search plan and an array of search paths and executes the plan
171 * over all FTSENT's returned for the given search paths.
174 find_execute(plan, paths)
175 PLAN *plan; /* search plan */
176 char **paths; /* array of pathnames to traverse */
178 register FTSENT *entry;
182 tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL));
186 for (rval = 0; (entry = fts_read(tree)) != NULL;) {
187 switch (entry->fts_info) {
199 (void)fflush(stdout);
201 entry->fts_path, strerror(entry->fts_errno));
209 #define BADCH " \t\n\\'\""
210 if (isxargs && strpbrk(entry->fts_path, BADCH)) {
211 (void)fflush(stdout);
212 warnx("%s: illegal path", entry->fts_path);
217 if (mindepth != -1 && entry->fts_level < mindepth)
221 * Call all the functions in the execution plan until one is
222 * false or all have been executed. This is where we do all
223 * the work specified by the user on the command line.
225 for (p = plan; p && (p->execute)(p, entry); p = p->next);
227 if (maxdepth != -1 && entry->fts_level >= maxdepth) {
228 if (fts_set(tree, entry, FTS_SKIP))
229 err(1, "%s", entry->fts_path);