1 /* $NetBSD: find.c,v 1.29 2012/03/20 20:34:57 matt Exp $ */
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Cimarron D. Taylor of the University of California, Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "from: @(#)find.c 8.5 (Berkeley) 8/5/94";
40 __RCSID("$NetBSD: find.c,v 1.29 2012/03/20 20:34:57 matt Exp $");
44 #include <sys/types.h>
59 static int ftscompare(const FTSENT
**, const FTSENT
**);
63 * process the command line and create a "plan" corresponding to the
67 find_formplan(char **argv
)
69 PLAN
*plan
, *tail
, *new;
72 * for each argument in the command line, determine what kind of node
73 * it is, create the appropriate node type and add the new plan node
74 * to the end of the existing plan. The resulting plan is a linked
75 * list of plan nodes. For example, the string:
77 * % find . -name foo -newer bar -print
79 * results in the plan:
81 * [-name foo]--> [-newer bar]--> [-print]
83 * in this diagram, `[-name foo]' represents the plan node generated
84 * by c_name() with an argument of foo and `-->' represents the
87 for (plan
= tail
= NULL
; *argv
;) {
88 if (!(new = find_create(&argv
)))
99 * if the user didn't specify one of -print, -ok, -fprint, -exec, or
100 * -exit, then -print is assumed so we bracket the current expression
101 * with parens, if necessary, and add a -print node on the end.
105 new = c_print(NULL
, 0);
108 new = c_openparen(NULL
, 0);
111 new = c_closeparen(NULL
, 0);
114 new = c_print(NULL
, 0);
121 * the command line has been completely processed into a search plan
122 * except for the (, ), !, and -o operators. Rearrange the plan so
123 * that the portions of the plan which are affected by the operators
124 * are moved into operator nodes themselves. For example:
126 * [!]--> [-name foo]--> [-print]
130 * [! [-name foo] ]--> [-print]
134 * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
138 * [expr [-depth]-->[-name foo] ]--> [-print]
140 * operators are handled in order of precedence.
143 plan
= paren_squish(plan
); /* ()'s */
144 plan
= not_squish(plan
); /* !'s */
145 plan
= or_squish(plan
); /* -o's */
150 ftscompare(const FTSENT
**e1
, const FTSENT
**e2
)
153 return (strcoll((*e1
)->fts_name
, (*e2
)->fts_name
));
163 notty
= !(isatty(STDIN_FILENO
) || isatty(STDOUT_FILENO
) ||
164 isatty(STDERR_FILENO
));
168 sigaddset(&ss
, SIGINFO
); /* block SIGINFO */
170 memset(&sa
, 0, sizeof(sa
));
171 sa
.sa_flags
= SA_RESTART
;
172 sa
.sa_handler
= show_path
;
173 (void)sigaction(SIGINFO
, &sa
, NULL
);
178 sig_lock(sigset_t
*s
)
182 sigprocmask(SIG_BLOCK
, &ss
, s
);
186 sig_unlock(const sigset_t
*s
)
190 sigprocmask(SIG_SETMASK
, s
, NULL
);
193 FTS
*tree
; /* pointer to top of FTS hierarchy */
194 FTSENT
*g_entry
; /* shared with SIGINFO handler */
198 * take a search plan and an array of search paths and executes the plan
199 * over all FTSENT's returned for the given search paths.
202 find_execute(PLAN
*plan
, char **paths
)
210 if (!(tree
= fts_open(paths
, ftsoptions
, issort
? ftscompare
: NULL
)))
215 for (rval
= 0; cval
&& (g_entry
= fts_read(tree
)) != NULL
;) {
216 switch (g_entry
->fts_info
) {
229 (void)fflush(stdout
);
231 g_entry
->fts_path
, strerror(g_entry
->fts_errno
));
236 #define BADCH " \t\n\\'\""
237 if (isxargs
&& strpbrk(g_entry
->fts_path
, BADCH
)) {
239 (void)fflush(stdout
);
240 warnx("%s: illegal path", g_entry
->fts_path
);
247 * Call all the functions in the execution plan until one is
248 * false or all have been executed. This is where we do all
249 * the work specified by the user on the command line.
252 for (p
= plan
; p
&& (p
->eval
)(p
, g_entry
); p
= p
->next
)
253 if (p
->type
== N_EXIT
) {
261 if (g_entry
== NULL
&& errno
)
263 (void)fts_close(tree
);
266 * Cleanup any plans with leftover state.
267 * Keep the last non-zero return value.
269 if ((r
= find_traverse(plan
, plan_cleanup
, NULL
)) != 0)
277 * traverse the plan tree and execute func() on all plans. This
278 * does not evaluate each plan's eval() function; it is intended
279 * for operations that must run on all plans, such as state
282 * If any func() returns non-zero, then so will find_traverse().
285 find_traverse(PLAN
*plan
, int (*func
)(PLAN
*, void *), void *arg
)
291 for (p
= plan
; p
; p
= p
->next
) {
292 if ((r
= func(p
, arg
)) != 0)
294 if (p
->type
== N_EXPR
|| p
->type
== N_OR
) {
296 if ((r
= find_traverse(p
->p_data
[0],
300 if ((r
= find_traverse(p
->p_data
[1],