Deleted Added
full compact
find.c (93604) find.c (97736)
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Cimarron D. Taylor of the University of California, Berkeley.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38#if 0
39static char sccsid[] = "@(#)find.c 8.5 (Berkeley) 8/5/94";
40#else
41#endif
42#endif /* not lint */
43#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Cimarron D. Taylor of the University of California, Berkeley.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38#if 0
39static char sccsid[] = "@(#)find.c 8.5 (Berkeley) 8/5/94";
40#else
41#endif
42#endif /* not lint */
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/usr.bin/find/find.c 93604 2002-04-01 22:56:56Z obrien $");
44__FBSDID("$FreeBSD: head/usr.bin/find/find.c 97736 2002-06-02 12:57:41Z tjr $");
45
46#include <sys/types.h>
47#include <sys/stat.h>
48
49#include <err.h>
50#include <errno.h>
51#include <fts.h>
52#include <regex.h>
53#include <stdio.h>
54#include <string.h>
55#include <stdlib.h>
56
57#include "find.h"
58
59static int find_compare(const FTSENT **s1, const FTSENT **s2);
60
61/*
62 * find_compare --
63 * tell fts_open() how to order the traversal of the hierarchy.
64 * This variant gives lexicographical order, i.e., alphabetical
65 * order within each directory.
66 */
67static int
68find_compare(s1, s2)
69 const FTSENT **s1, **s2;
70{
71
72 return (strcoll((*s1)->fts_name, (*s2)->fts_name));
73}
74
75/*
76 * find_formplan --
77 * process the command line and create a "plan" corresponding to the
78 * command arguments.
79 */
80PLAN *
81find_formplan(argv)
82 char **argv;
83{
84 PLAN *plan, *tail, *new;
85
86 /*
87 * for each argument in the command line, determine what kind of node
88 * it is, create the appropriate node type and add the new plan node
89 * to the end of the existing plan. The resulting plan is a linked
90 * list of plan nodes. For example, the string:
91 *
92 * % find . -name foo -newer bar -print
93 *
94 * results in the plan:
95 *
96 * [-name foo]--> [-newer bar]--> [-print]
97 *
98 * in this diagram, `[-name foo]' represents the plan node generated
99 * by c_name() with an argument of foo and `-->' represents the
100 * plan->next pointer.
101 */
102 for (plan = tail = NULL; *argv;) {
103 if (!(new = find_create(&argv)))
104 continue;
105 if (plan == NULL)
106 tail = plan = new;
107 else {
108 tail->next = new;
109 tail = new;
110 }
111 }
112
113 /*
114 * if the user didn't specify one of -print, -ok or -exec, then -print
115 * is assumed so we bracket the current expression with parens, if
116 * necessary, and add a -print node on the end.
117 */
118 if (!isoutput) {
119 OPTION *p;
120 char **argv1 = 0;
121
122 if (plan == NULL) {
123 p = lookup_option("-print");
124 new = (p->create)(p, &argv1);
125 tail = plan = new;
126 } else {
127 p = lookup_option("(");
128 new = (p->create)(p, &argv1);
129 new->next = plan;
130 plan = new;
131 p = lookup_option(")");
132 new = (p->create)(p, &argv1);
133 tail->next = new;
134 tail = new;
135 p = lookup_option("-print");
136 new = (p->create)(p, &argv1);
137 tail->next = new;
138 tail = new;
139 }
140 }
141
142 /*
143 * the command line has been completely processed into a search plan
144 * except for the (, ), !, and -o operators. Rearrange the plan so
145 * that the portions of the plan which are affected by the operators
146 * are moved into operator nodes themselves. For example:
147 *
148 * [!]--> [-name foo]--> [-print]
149 *
150 * becomes
151 *
152 * [! [-name foo] ]--> [-print]
153 *
154 * and
155 *
156 * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
157 *
158 * becomes
159 *
160 * [expr [-depth]-->[-name foo] ]--> [-print]
161 *
162 * operators are handled in order of precedence.
163 */
164
165 plan = paren_squish(plan); /* ()'s */
166 plan = not_squish(plan); /* !'s */
167 plan = or_squish(plan); /* -o's */
168 return (plan);
169}
170
171FTS *tree; /* pointer to top of FTS hierarchy */
172
173/*
174 * find_execute --
175 * take a search plan and an array of search paths and executes the plan
176 * over all FTSENT's returned for the given search paths.
177 */
178int
179find_execute(plan, paths)
180 PLAN *plan; /* search plan */
181 char **paths; /* array of pathnames to traverse */
182{
183 FTSENT *entry;
184 PLAN *p;
185 int rval;
186
187 tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL));
188 if (tree == NULL)
189 err(1, "ftsopen");
190
191 for (rval = 0; (entry = fts_read(tree)) != NULL;) {
192 switch (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 entry->fts_path, strerror(entry->fts_errno));
207 rval = 1;
208 continue;
209#ifdef FTS_W
210 case FTS_W:
211 continue;
212#endif /* FTS_W */
213 }
214#define BADCH " \t\n\\'\""
215 if (isxargs && strpbrk(entry->fts_path, BADCH)) {
216 (void)fflush(stdout);
217 warnx("%s: illegal path", entry->fts_path);
218 rval = 1;
219 continue;
220 }
221
222 if (mindepth != -1 && entry->fts_level < mindepth)
223 continue;
224
225 /*
226 * Call all the functions in the execution plan until one is
227 * false or all have been executed. This is where we do all
228 * the work specified by the user on the command line.
229 */
230 for (p = plan; p && (p->execute)(p, entry); p = p->next);
231
232 if (maxdepth != -1 && entry->fts_level >= maxdepth) {
233 if (fts_set(tree, entry, FTS_SKIP))
234 err(1, "%s", entry->fts_path);
235 continue;
236 }
237 }
45
46#include <sys/types.h>
47#include <sys/stat.h>
48
49#include <err.h>
50#include <errno.h>
51#include <fts.h>
52#include <regex.h>
53#include <stdio.h>
54#include <string.h>
55#include <stdlib.h>
56
57#include "find.h"
58
59static int find_compare(const FTSENT **s1, const FTSENT **s2);
60
61/*
62 * find_compare --
63 * tell fts_open() how to order the traversal of the hierarchy.
64 * This variant gives lexicographical order, i.e., alphabetical
65 * order within each directory.
66 */
67static int
68find_compare(s1, s2)
69 const FTSENT **s1, **s2;
70{
71
72 return (strcoll((*s1)->fts_name, (*s2)->fts_name));
73}
74
75/*
76 * find_formplan --
77 * process the command line and create a "plan" corresponding to the
78 * command arguments.
79 */
80PLAN *
81find_formplan(argv)
82 char **argv;
83{
84 PLAN *plan, *tail, *new;
85
86 /*
87 * for each argument in the command line, determine what kind of node
88 * it is, create the appropriate node type and add the new plan node
89 * to the end of the existing plan. The resulting plan is a linked
90 * list of plan nodes. For example, the string:
91 *
92 * % find . -name foo -newer bar -print
93 *
94 * results in the plan:
95 *
96 * [-name foo]--> [-newer bar]--> [-print]
97 *
98 * in this diagram, `[-name foo]' represents the plan node generated
99 * by c_name() with an argument of foo and `-->' represents the
100 * plan->next pointer.
101 */
102 for (plan = tail = NULL; *argv;) {
103 if (!(new = find_create(&argv)))
104 continue;
105 if (plan == NULL)
106 tail = plan = new;
107 else {
108 tail->next = new;
109 tail = new;
110 }
111 }
112
113 /*
114 * if the user didn't specify one of -print, -ok or -exec, then -print
115 * is assumed so we bracket the current expression with parens, if
116 * necessary, and add a -print node on the end.
117 */
118 if (!isoutput) {
119 OPTION *p;
120 char **argv1 = 0;
121
122 if (plan == NULL) {
123 p = lookup_option("-print");
124 new = (p->create)(p, &argv1);
125 tail = plan = new;
126 } else {
127 p = lookup_option("(");
128 new = (p->create)(p, &argv1);
129 new->next = plan;
130 plan = new;
131 p = lookup_option(")");
132 new = (p->create)(p, &argv1);
133 tail->next = new;
134 tail = new;
135 p = lookup_option("-print");
136 new = (p->create)(p, &argv1);
137 tail->next = new;
138 tail = new;
139 }
140 }
141
142 /*
143 * the command line has been completely processed into a search plan
144 * except for the (, ), !, and -o operators. Rearrange the plan so
145 * that the portions of the plan which are affected by the operators
146 * are moved into operator nodes themselves. For example:
147 *
148 * [!]--> [-name foo]--> [-print]
149 *
150 * becomes
151 *
152 * [! [-name foo] ]--> [-print]
153 *
154 * and
155 *
156 * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
157 *
158 * becomes
159 *
160 * [expr [-depth]-->[-name foo] ]--> [-print]
161 *
162 * operators are handled in order of precedence.
163 */
164
165 plan = paren_squish(plan); /* ()'s */
166 plan = not_squish(plan); /* !'s */
167 plan = or_squish(plan); /* -o's */
168 return (plan);
169}
170
171FTS *tree; /* pointer to top of FTS hierarchy */
172
173/*
174 * find_execute --
175 * take a search plan and an array of search paths and executes the plan
176 * over all FTSENT's returned for the given search paths.
177 */
178int
179find_execute(plan, paths)
180 PLAN *plan; /* search plan */
181 char **paths; /* array of pathnames to traverse */
182{
183 FTSENT *entry;
184 PLAN *p;
185 int rval;
186
187 tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL));
188 if (tree == NULL)
189 err(1, "ftsopen");
190
191 for (rval = 0; (entry = fts_read(tree)) != NULL;) {
192 switch (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 entry->fts_path, strerror(entry->fts_errno));
207 rval = 1;
208 continue;
209#ifdef FTS_W
210 case FTS_W:
211 continue;
212#endif /* FTS_W */
213 }
214#define BADCH " \t\n\\'\""
215 if (isxargs && strpbrk(entry->fts_path, BADCH)) {
216 (void)fflush(stdout);
217 warnx("%s: illegal path", entry->fts_path);
218 rval = 1;
219 continue;
220 }
221
222 if (mindepth != -1 && entry->fts_level < mindepth)
223 continue;
224
225 /*
226 * Call all the functions in the execution plan until one is
227 * false or all have been executed. This is where we do all
228 * the work specified by the user on the command line.
229 */
230 for (p = plan; p && (p->execute)(p, entry); p = p->next);
231
232 if (maxdepth != -1 && entry->fts_level >= maxdepth) {
233 if (fts_set(tree, entry, FTS_SKIP))
234 err(1, "%s", entry->fts_path);
235 continue;
236 }
237 }
238 /* Finish any pending -exec ... {} + functions. */
239 for (p = plan; p != NULL; p = p->next)
240 if (p->execute == f_exec && p->flags & F_EXECPLUS)
241 (p->execute)(p, NULL);
238 if (errno)
239 err(1, "fts_read");
240 return (rval);
241}
242 if (errno)
243 err(1, "fts_read");
244 return (rval);
245}