find.c revision 1.18 1 /* $NetBSD: find.c,v 1.18 2003/08/07 11:13:41 agc Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Cimarron D. Taylor of the University of California, Berkeley.
9 *
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.
21 *
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.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "from: @(#)find.c 8.5 (Berkeley) 8/5/94";
39 #else
40 __RCSID("$NetBSD: find.c,v 1.18 2003/08/07 11:13:41 agc Exp $");
41 #endif
42 #endif /* not lint */
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46
47 #include <err.h>
48 #include <errno.h>
49 #include <fts.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <stdlib.h>
54
55 #include "find.h"
56
57 static int ftscompare __P((const FTSENT **, const FTSENT **));
58
59 static void sig_lock __P((sigset_t *));
60 static void sig_unlock __P((const sigset_t *));
61
62 /*
63 * find_formplan --
64 * process the command line and create a "plan" corresponding to the
65 * command arguments.
66 */
67 PLAN *
68 find_formplan(argv)
69 char **argv;
70 {
71 PLAN *plan, *tail, *new;
72
73 /*
74 * for each argument in the command line, determine what kind of node
75 * it is, create the appropriate node type and add the new plan node
76 * to the end of the existing plan. The resulting plan is a linked
77 * list of plan nodes. For example, the string:
78 *
79 * % find . -name foo -newer bar -print
80 *
81 * results in the plan:
82 *
83 * [-name foo]--> [-newer bar]--> [-print]
84 *
85 * in this diagram, `[-name foo]' represents the plan node generated
86 * by c_name() with an argument of foo and `-->' represents the
87 * plan->next pointer.
88 */
89 for (plan = tail = NULL; *argv;) {
90 if (!(new = find_create(&argv)))
91 continue;
92 if (plan == NULL)
93 tail = plan = new;
94 else {
95 tail->next = new;
96 tail = new;
97 }
98 }
99
100 /*
101 * if the user didn't specify one of -print, -ok or -exec, then -print
102 * is assumed so we bracket the current expression with parens, if
103 * necessary, and add a -print node on the end.
104 */
105 if (!isoutput) {
106 if (plan == NULL) {
107 new = c_print(NULL, 0);
108 tail = plan = new;
109 } else {
110 new = c_openparen(NULL, 0);
111 new->next = plan;
112 plan = new;
113 new = c_closeparen(NULL, 0);
114 tail->next = new;
115 tail = new;
116 new = c_print(NULL, 0);
117 tail->next = new;
118 tail = new;
119 }
120 }
121
122 /*
123 * the command line has been completely processed into a search plan
124 * except for the (, ), !, and -o operators. Rearrange the plan so
125 * that the portions of the plan which are affected by the operators
126 * are moved into operator nodes themselves. For example:
127 *
128 * [!]--> [-name foo]--> [-print]
129 *
130 * becomes
131 *
132 * [! [-name foo] ]--> [-print]
133 *
134 * and
135 *
136 * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
137 *
138 * becomes
139 *
140 * [expr [-depth]-->[-name foo] ]--> [-print]
141 *
142 * operators are handled in order of precedence.
143 */
144
145 plan = paren_squish(plan); /* ()'s */
146 plan = not_squish(plan); /* !'s */
147 plan = or_squish(plan); /* -o's */
148 return (plan);
149 }
150
151 static int
152 ftscompare(e1, e2)
153 const FTSENT **e1, **e2;
154 {
155
156 return (strcoll((*e1)->fts_name, (*e2)->fts_name));
157 }
158
159 static void
160 sig_lock(s)
161 sigset_t *s;
162 {
163 sigset_t new;
164
165 sigemptyset(&new);
166 sigaddset(&new, SIGINFO); /* block SIGINFO */
167 sigprocmask(SIG_BLOCK, &new, s);
168 }
169
170 static void
171 sig_unlock(s)
172 const sigset_t *s;
173 {
174
175 sigprocmask(SIG_SETMASK, s, NULL);
176 }
177
178 FTS *tree; /* pointer to top of FTS hierarchy */
179 FTSENT *g_entry; /* shared with SIGINFO handler */
180
181 /*
182 * find_execute --
183 * take a search plan and an array of search paths and executes the plan
184 * over all FTSENT's returned for the given search paths.
185 */
186 int
187 find_execute(plan, paths)
188 PLAN *plan; /* search plan */
189 char **paths; /* array of pathnames to traverse */
190 {
191 PLAN *p;
192 int rval;
193 sigset_t s;
194
195 if (!(tree = fts_open(paths, ftsoptions, issort ? ftscompare : NULL)))
196 err(1, "ftsopen");
197
198 sig_lock(&s);
199 for (rval = 0; (g_entry = fts_read(tree)) != NULL; sig_lock(&s)) {
200 sig_unlock(&s);
201 switch (g_entry->fts_info) {
202 case FTS_D:
203 if (isdepth)
204 continue;
205 break;
206 case FTS_DP:
207 if (!isdepth)
208 continue;
209 break;
210 case FTS_DNR:
211 case FTS_ERR:
212 case FTS_NS:
213 (void)fflush(stdout);
214 warnx("%s: %s",
215 g_entry->fts_path, strerror(g_entry->fts_errno));
216 rval = 1;
217 continue;
218 #ifdef FTS_W
219 case FTS_W:
220 continue;
221 #endif /* FTS_W */
222 }
223 #define BADCH " \t\n\\'\""
224 if (isxargs && strpbrk(g_entry->fts_path, BADCH)) {
225 (void)fflush(stdout);
226 warnx("%s: illegal path", g_entry->fts_path);
227 rval = 1;
228 continue;
229 }
230
231 /*
232 * Call all the functions in the execution plan until one is
233 * false or all have been executed. This is where we do all
234 * the work specified by the user on the command line.
235 */
236 for (p = plan; p && (p->eval)(p, g_entry); p = p->next)
237 ;
238 }
239 sig_unlock(&s);
240 if (errno)
241 err(1, "fts_read");
242 (void)fts_close(tree);
243 return (rval);
244 }
245