mkfs: symlink support
[minix.git] / commands / find / find.c
blob29126847ccd59cd094f788f25780ef9a5a38627c
1 /* $NetBSD: find.c,v 1.25 2007/09/25 04:10:12 lukem Exp $ */
3 /*-
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
12 * are met:
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
32 * SUCH DAMAGE.
35 #include <sys/cdefs.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <fts.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
48 #include "find.h"
50 static int ftscompare(const FTSENT * const *, const FTSENT * const *);
52 static void sig_lock(sigset_t *);
53 static void sig_unlock(const sigset_t *);
56 * find_formplan --
57 * process the command line and create a "plan" corresponding to the
58 * command arguments.
60 PLAN *
61 find_formplan(char **argv)
63 PLAN *plan, *tail, *new;
66 * for each argument in the command line, determine what kind of node
67 * it is, create the appropriate node type and add the new plan node
68 * to the end of the existing plan. The resulting plan is a linked
69 * list of plan nodes. For example, the string:
71 * % find . -name foo -newer bar -print
73 * results in the plan:
75 * [-name foo]--> [-newer bar]--> [-print]
77 * in this diagram, `[-name foo]' represents the plan node generated
78 * by c_name() with an argument of foo and `-->' represents the
79 * plan->next pointer.
81 for (plan = tail = NULL; *argv;) {
82 if (!(new = find_create(&argv)))
83 continue;
84 if (plan == NULL)
85 tail = plan = new;
86 else {
87 tail->next = new;
88 tail = new;
93 * if the user didn't specify one of -print, -ok, -fprint, -exec, or
94 * -exit, then -print is assumed so we bracket the current expression
95 * with parens, if necessary, and add a -print node on the end.
97 if (!isoutput) {
98 if (plan == NULL) {
99 new = c_print(NULL, 0);
100 tail = plan = new;
101 } else {
102 new = c_openparen(NULL, 0);
103 new->next = plan;
104 plan = new;
105 new = c_closeparen(NULL, 0);
106 tail->next = new;
107 tail = new;
108 new = c_print(NULL, 0);
109 tail->next = new;
110 tail = new;
115 * the command line has been completely processed into a search plan
116 * except for the (, ), !, and -o operators. Rearrange the plan so
117 * that the portions of the plan which are affected by the operators
118 * are moved into operator nodes themselves. For example:
120 * [!]--> [-name foo]--> [-print]
122 * becomes
124 * [! [-name foo] ]--> [-print]
126 * and
128 * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
130 * becomes
132 * [expr [-depth]-->[-name foo] ]--> [-print]
134 * operators are handled in order of precedence.
137 plan = paren_squish(plan); /* ()'s */
138 plan = not_squish(plan); /* !'s */
139 plan = or_squish(plan); /* -o's */
140 return (plan);
143 static int
144 ftscompare(const FTSENT * const *e1, const FTSENT * const *e2)
147 return (strcoll((*e1)->fts_name, (*e2)->fts_name));
150 static void
151 sig_lock(sigset_t *s)
153 sigset_t new;
155 sigemptyset(&new);
156 #ifdef SIGINFO
157 sigaddset(&new, SIGINFO); /* block SIGINFO */
158 #endif
159 sigprocmask(SIG_BLOCK, &new, s);
162 static void
163 sig_unlock(const sigset_t *s)
166 sigprocmask(SIG_SETMASK, s, NULL);
169 FTS *tree; /* pointer to top of FTS hierarchy */
170 FTSENT *g_entry; /* shared with SIGINFO handler */
173 * find_execute --
174 * take a search plan and an array of search paths and executes the plan
175 * over all FTSENT's returned for the given search paths.
178 find_execute(PLAN *plan, char **paths)
180 PLAN *p;
181 int r, rval, cval;
182 sigset_t s;
184 cval = 1;
186 if (!(tree = fts_open(paths, ftsoptions, issort ? ftscompare : NULL)))
187 err(1, "ftsopen");
189 sig_lock(&s);
190 for (rval = 0; cval && (g_entry = fts_read(tree)) != NULL; sig_lock(&s)) {
191 sig_unlock(&s);
192 switch (g_entry->fts_info) {
193 case FTS_D:
194 if (isdepth)
195 continue;
196 break;
197 case FTS_DP:
198 if (!isdepth)
199 continue;
200 break;
201 case FTS_DNR:
202 case FTS_ERR:
203 case FTS_NS:
204 (void)fflush(stdout);
205 warnx("%s: %s",
206 g_entry->fts_path, strerror(g_entry->fts_errno));
207 rval = 1;
208 continue;
210 #define BADCH " \t\n\\'\""
211 if (isxargs && strpbrk(g_entry->fts_path, BADCH)) {
212 (void)fflush(stdout);
213 warnx("%s: illegal path", g_entry->fts_path);
214 rval = 1;
215 continue;
219 * Call all the functions in the execution plan until one is
220 * false or all have been executed. This is where we do all
221 * the work specified by the user on the command line.
223 for (p = plan; p && (p->eval)(p, g_entry); p = p->next)
224 if (p->type == N_EXIT) {
225 rval = p->exit_val;
226 cval = 0;
230 sig_unlock(&s);
231 if (errno)
232 err(1, "fts_read");
233 (void)fts_close(tree);
236 * Cleanup any plans with leftover state.
237 * Keep the last non-zero return value.
239 if ((r = find_traverse(plan, plan_cleanup, NULL)) != 0)
240 rval = r;
242 return (rval);
246 * find_traverse --
247 * traverse the plan tree and execute func() on all plans. This
248 * does not evaluate each plan's eval() function; it is intended
249 * for operations that must run on all plans, such as state
250 * cleanup.
252 * If any func() returns non-zero, then so will find_traverse().
255 find_traverse(plan, func, arg)
256 PLAN *plan;
257 int (*func)(PLAN *, void *);
258 void *arg;
260 PLAN *p;
261 int r, rval;
263 rval = 0;
264 for (p = plan; p; p = p->next) {
265 if ((r = func(p, arg)) != 0)
266 rval = r;
267 if (p->type == N_EXPR || p->type == N_OR) {
268 if (p->p_data[0])
269 if ((r = find_traverse(p->p_data[0],
270 func, arg)) != 0)
271 rval = r;
272 if (p->p_data[1])
273 if ((r = find_traverse(p->p_data[1],
274 func, arg)) != 0)
275 rval = r;
278 return rval;