find.c revision 1.8 1 /* $NetBSD: find.c,v 1.8 1997/01/09 20:19:10 tls Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
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 #ifndef lint
40 /*static char sccsid[] = "from: @(#)find.c 8.1 (Berkeley) 6/6/93";*/
41 static char rcsid[] = "$NetBSD: find.c,v 1.8 1997/01/09 20:19:10 tls Exp $";
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 <stdio.h>
51 #include <string.h>
52 #include <stdlib.h>
53
54 #include "find.h"
55
56 /*
57 * find_formplan --
58 * process the command line and create a "plan" corresponding to the
59 * command arguments.
60 */
61 PLAN *
62 find_formplan(argv)
63 char **argv;
64 {
65 PLAN *plan, *tail, *new;
66
67 /*
68 * for each argument in the command line, determine what kind of node
69 * it is, create the appropriate node type and add the new plan node
70 * to the end of the existing plan. The resulting plan is a linked
71 * list of plan nodes. For example, the string:
72 *
73 * % find . -name foo -newer bar -print
74 *
75 * results in the plan:
76 *
77 * [-name foo]--> [-newer bar]--> [-print]
78 *
79 * in this diagram, `[-name foo]' represents the plan node generated
80 * by c_name() with an argument of foo and `-->' represents the
81 * plan->next pointer.
82 */
83 for (plan = tail = NULL; *argv;) {
84 if (!(new = find_create(&argv)))
85 continue;
86 if (plan == NULL)
87 tail = plan = new;
88 else {
89 tail->next = new;
90 tail = new;
91 }
92 }
93
94 /*
95 * if the user didn't specify one of -print, -ok or -exec, then -print
96 * is assumed so we bracket the current expression with parens, if
97 * necessary, and add a -print node on the end.
98 */
99 if (!isoutput) {
100 if (plan == NULL) {
101 new = c_print();
102 tail = plan = new;
103 } else {
104 new = c_openparen();
105 new->next = plan;
106 plan = new;
107 new = c_closeparen();
108 tail->next = new;
109 tail = new;
110 new = c_print();
111 tail->next = new;
112 tail = new;
113 }
114 }
115
116 /*
117 * the command line has been completely processed into a search plan
118 * except for the (, ), !, and -o operators. Rearrange the plan so
119 * that the portions of the plan which are affected by the operators
120 * are moved into operator nodes themselves. For example:
121 *
122 * [!]--> [-name foo]--> [-print]
123 *
124 * becomes
125 *
126 * [! [-name foo] ]--> [-print]
127 *
128 * and
129 *
130 * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
131 *
132 * becomes
133 *
134 * [expr [-depth]-->[-name foo] ]--> [-print]
135 *
136 * operators are handled in order of precedence.
137 */
138
139 plan = paren_squish(plan); /* ()'s */
140 plan = not_squish(plan); /* !'s */
141 plan = or_squish(plan); /* -o's */
142 return (plan);
143 }
144
145 FTS *tree; /* pointer to top of FTS hierarchy */
146
147 /*
148 * find_execute --
149 * take a search plan and an array of search paths and executes the plan
150 * over all FTSENT's returned for the given search paths.
151 */
152 void
153 find_execute(plan, paths)
154 PLAN *plan; /* search plan */
155 char **paths; /* array of pathnames to traverse */
156 {
157 register FTSENT *entry;
158 PLAN *p;
159
160 if (!(tree = fts_open(paths, ftsoptions, (int (*)())NULL)))
161 err(1, "ftsopen");
162
163 while (entry = fts_read(tree)) {
164 switch(entry->fts_info) {
165 case FTS_D:
166 if (isdepth)
167 continue;
168 break;
169 case FTS_DP:
170 if (!isdepth)
171 continue;
172 break;
173 case FTS_DNR:
174 case FTS_ERR:
175 case FTS_NS:
176 (void)fflush(stdout);
177 warn("%s", entry->fts_path);
178 continue;
179 }
180 #define BADCH " \t\n\\'\""
181 if (isxargs && strpbrk(entry->fts_path, BADCH)) {
182 (void)fflush(stdout);
183 warnx("%s: illegal path", entry->fts_path);
184 continue;
185 }
186
187 /*
188 * call all the functions in the execution plan until one is
189 * false or all have been executed. This is where we do all
190 * the work specified by the user on the command line.
191 */
192 for (p = plan; p && (p->eval)(p, entry); p = p->next)
193 ;
194 }
195 (void)fts_close(tree);
196 }
197