find.c revision 1.2 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1991 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Cimarron D. Taylor of the University of California, Berkeley.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.2 mycroft /*static char sccsid[] = "from: @(#)find.c 5.3 (Berkeley) 5/25/91";*/
39 1.2 mycroft static char rcsid[] = "$Id: find.c,v 1.2 1993/08/01 18:16:16 mycroft Exp $";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #include <sys/types.h>
43 1.1 cgd #include <sys/stat.h>
44 1.1 cgd #include <sys/errno.h>
45 1.1 cgd #include <fts.h>
46 1.1 cgd #include <stdio.h>
47 1.1 cgd #include <string.h>
48 1.1 cgd #include <stdlib.h>
49 1.1 cgd #include "find.h"
50 1.1 cgd
51 1.1 cgd /*
52 1.1 cgd * find_formplan --
53 1.1 cgd * process the command line and create a "plan" corresponding to the
54 1.1 cgd * command arguments.
55 1.1 cgd */
56 1.1 cgd PLAN *
57 1.1 cgd find_formplan(argv)
58 1.1 cgd char **argv;
59 1.1 cgd {
60 1.1 cgd PLAN *plan, *tail, *new;
61 1.1 cgd PLAN *c_print(), *find_create(), *not_squish(), *or_squish();
62 1.1 cgd PLAN *paren_squish();
63 1.1 cgd
64 1.1 cgd /*
65 1.1 cgd * for each argument in the command line, determine what kind of node
66 1.1 cgd * it is, create the appropriate node type and add the new plan node
67 1.1 cgd * to the end of the existing plan. The resulting plan is a linked
68 1.1 cgd * list of plan nodes. For example, the string:
69 1.1 cgd *
70 1.1 cgd * % find . -name foo -newer bar -print
71 1.1 cgd *
72 1.1 cgd * results in the plan:
73 1.1 cgd *
74 1.1 cgd * [-name foo]--> [-newer bar]--> [-print]
75 1.1 cgd *
76 1.1 cgd * in this diagram, `[-name foo]' represents the plan node generated
77 1.1 cgd * by c_name() with an argument of foo and `-->' represents the
78 1.1 cgd * plan->next pointer.
79 1.1 cgd */
80 1.1 cgd for (plan = NULL; *argv;) {
81 1.1 cgd if (!(new = find_create(&argv)))
82 1.1 cgd continue;
83 1.1 cgd if (plan == NULL)
84 1.1 cgd tail = plan = new;
85 1.1 cgd else {
86 1.1 cgd tail->next = new;
87 1.1 cgd tail = new;
88 1.1 cgd }
89 1.1 cgd }
90 1.1 cgd
91 1.1 cgd /*
92 1.1 cgd * if the user didn't specify one of -print, -ok or -exec, then -print
93 1.1 cgd * is assumed so we add a -print node on the end. It is possible that
94 1.1 cgd * the user might want the -print someplace else on the command line,
95 1.1 cgd * but there's no way to know that.
96 1.1 cgd */
97 1.1 cgd if (!isoutput) {
98 1.1 cgd new = c_print();
99 1.1 cgd if (plan == NULL)
100 1.1 cgd tail = plan = new;
101 1.1 cgd else {
102 1.1 cgd tail->next = new;
103 1.1 cgd tail = new;
104 1.1 cgd }
105 1.1 cgd }
106 1.1 cgd
107 1.1 cgd /*
108 1.1 cgd * the command line has been completely processed into a search plan
109 1.1 cgd * except for the (, ), !, and -o operators. Rearrange the plan so
110 1.1 cgd * that the portions of the plan which are affected by the operators
111 1.1 cgd * are moved into operator nodes themselves. For example:
112 1.1 cgd *
113 1.1 cgd * [!]--> [-name foo]--> [-print]
114 1.1 cgd *
115 1.1 cgd * becomes
116 1.1 cgd *
117 1.1 cgd * [! [-name foo] ]--> [-print]
118 1.1 cgd *
119 1.1 cgd * and
120 1.1 cgd *
121 1.1 cgd * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
122 1.1 cgd *
123 1.1 cgd * becomes
124 1.1 cgd *
125 1.1 cgd * [expr [-depth]-->[-name foo] ]--> [-print]
126 1.1 cgd *
127 1.1 cgd * operators are handled in order of precedence.
128 1.1 cgd */
129 1.1 cgd
130 1.1 cgd plan = paren_squish(plan); /* ()'s */
131 1.1 cgd plan = not_squish(plan); /* !'s */
132 1.1 cgd plan = or_squish(plan); /* -o's */
133 1.1 cgd return(plan);
134 1.1 cgd }
135 1.1 cgd
136 1.1 cgd FTS *tree; /* pointer to top of FTS hierarchy */
137 1.1 cgd
138 1.1 cgd /*
139 1.1 cgd * find_execute --
140 1.1 cgd * take a search plan and an array of search paths and executes the plan
141 1.1 cgd * over all FTSENT's returned for the given search paths.
142 1.1 cgd */
143 1.1 cgd void
144 1.1 cgd find_execute(plan, paths)
145 1.1 cgd PLAN *plan; /* search plan */
146 1.1 cgd char **paths; /* array of pathnames to traverse */
147 1.1 cgd {
148 1.1 cgd register FTSENT *entry;
149 1.1 cgd PLAN *p;
150 1.1 cgd
151 1.1 cgd if (!(tree = fts_open(paths, ftsoptions, (int (*)())NULL)))
152 1.1 cgd err("ftsopen: %s", strerror(errno));
153 1.1 cgd
154 1.1 cgd while (entry = fts_read(tree)) {
155 1.1 cgd switch(entry->fts_info) {
156 1.1 cgd case FTS_D:
157 1.1 cgd if (isdepth)
158 1.1 cgd continue;
159 1.1 cgd break;
160 1.1 cgd case FTS_DP:
161 1.1 cgd if (!isdepth)
162 1.1 cgd continue;
163 1.1 cgd break;
164 1.1 cgd case FTS_DNR:
165 1.1 cgd case FTS_ERR:
166 1.1 cgd case FTS_NS:
167 1.1 cgd (void)fprintf(stderr, "find: %s: %s\n",
168 1.1 cgd entry->fts_path, strerror(errno));
169 1.1 cgd continue;
170 1.1 cgd case FTS_SL:
171 1.1 cgd if (entry->fts_level == FTS_ROOTLEVEL) {
172 1.1 cgd (void)fts_set(tree, entry, FTS_FOLLOW);
173 1.1 cgd continue;
174 1.1 cgd }
175 1.1 cgd break;
176 1.1 cgd }
177 1.1 cgd
178 1.1 cgd #define BADCH " \t\n\\'\""
179 1.1 cgd if (isxargs && strpbrk(entry->fts_path, BADCH)) {
180 1.1 cgd (void)fprintf(stderr,
181 1.1 cgd "find: illegal path: %s\n", entry->fts_path);
182 1.1 cgd continue;
183 1.1 cgd }
184 1.1 cgd
185 1.1 cgd /*
186 1.1 cgd * call all the functions in the execution plan until one is
187 1.1 cgd * false or all have been executed. This is where we do all
188 1.1 cgd * the work specified by the user on the command line.
189 1.1 cgd */
190 1.1 cgd for (p = plan; p && (p->eval)(p, entry); p = p->next);
191 1.1 cgd }
192 1.1 cgd (void)fts_close(tree);
193 1.1 cgd }
194