find.c revision 1.12 1 /* $NetBSD: find.c,v 1.12 2000/03/10 11:46:04 itohy 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "from: @(#)find.c 8.5 (Berkeley) 8/5/94";
43 #else
44 __RCSID("$NetBSD: find.c,v 1.12 2000/03/10 11:46:04 itohy Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <sys/types.h>
49 #include <sys/stat.h>
50
51 #include <err.h>
52 #include <errno.h>
53 #include <fts.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <stdlib.h>
57
58 #include "find.h"
59
60 static int ftscompare __P((const FTSENT **, const FTSENT **));
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 return strcmp((*e1)->fts_name, (*e2)->fts_name);
156 }
157
158 FTS *tree; /* pointer to top of FTS hierarchy */
159
160 /*
161 * find_execute --
162 * take a search plan and an array of search paths and executes the plan
163 * over all FTSENT's returned for the given search paths.
164 */
165 int
166 find_execute(plan, paths)
167 PLAN *plan; /* search plan */
168 char **paths; /* array of pathnames to traverse */
169 {
170 register FTSENT *entry;
171 PLAN *p;
172 int rval;
173
174 if (!(tree = fts_open(paths, ftsoptions, issort ? ftscompare : NULL)))
175 err(1, "ftsopen");
176
177 for (rval = 0; (entry = fts_read(tree)) != NULL; ) {
178 switch (entry->fts_info) {
179 case FTS_D:
180 if (isdepth)
181 continue;
182 break;
183 case FTS_DP:
184 if (!isdepth)
185 continue;
186 break;
187 case FTS_DNR:
188 case FTS_ERR:
189 case FTS_NS:
190 (void)fflush(stdout);
191 warnx("%s: %s",
192 entry->fts_path, strerror(entry->fts_errno));
193 rval = 1;
194 continue;
195 #ifdef FTS_W
196 case FTS_W:
197 continue;
198 #endif /* FTS_W */
199 }
200 #define BADCH " \t\n\\'\""
201 if (isxargs && strpbrk(entry->fts_path, BADCH)) {
202 (void)fflush(stdout);
203 warnx("%s: illegal path", entry->fts_path);
204 rval = 1;
205 continue;
206 }
207
208 /*
209 * Call all the functions in the execution plan until one is
210 * false or all have been executed. This is where we do all
211 * the work specified by the user on the command line.
212 */
213 for (p = plan; p && (p->eval)(p, entry); p = p->next)
214 ;
215 }
216 if (errno)
217 err(1, "fts_read");
218 (void)fts_close(tree);
219 return (rval);
220 }
221