Deleted Added
full compact
find.c (207677) find.c (207705)
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.
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 * @(#)find.c 8.5 (Berkeley) 8/5/94
37 */
38
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
39#include <sys/cdefs.h>
44#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/usr.bin/find/find.c 207677 2010-05-05 21:24:18Z delphij $");
45__FBSDID("$FreeBSD: head/usr.bin/find/find.c 207705 2010-05-06 17:06:36Z delphij $");
41
42#include <sys/types.h>
43#include <sys/stat.h>
44
45#include <err.h>
46#include <errno.h>
47#include <fts.h>
48#include <regex.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52
53#include "find.h"
54
55static int find_compare(const FTSENT * const *s1, const FTSENT * const *s2);
56
57/*
58 * find_compare --
59 * tell fts_open() how to order the traversal of the hierarchy.
60 * This variant gives lexicographical order, i.e., alphabetical
61 * order within each directory.
62 */
63static int
64find_compare(const FTSENT * const *s1, const FTSENT * const *s2)
65{
66
67 return (strcoll((*s1)->fts_name, (*s2)->fts_name));
68}
69
70/*
71 * find_formplan --
72 * process the command line and create a "plan" corresponding to the
73 * command arguments.
74 */
75PLAN *
76find_formplan(char *argv[])
77{
78 PLAN *plan, *tail, *new;
79
80 /*
81 * for each argument in the command line, determine what kind of node
82 * it is, create the appropriate node type and add the new plan node
83 * to the end of the existing plan. The resulting plan is a linked
84 * list of plan nodes. For example, the string:
85 *
86 * % find . -name foo -newer bar -print
87 *
88 * results in the plan:
89 *
90 * [-name foo]--> [-newer bar]--> [-print]
91 *
92 * in this diagram, `[-name foo]' represents the plan node generated
93 * by c_name() with an argument of foo and `-->' represents the
94 * plan->next pointer.
95 */
96 for (plan = tail = NULL; *argv;) {
97 if (!(new = find_create(&argv)))
98 continue;
99 if (plan == NULL)
100 tail = plan = new;
101 else {
102 tail->next = new;
103 tail = new;
104 }
105 }
106
107 /*
108 * if the user didn't specify one of -print, -ok or -exec, then -print
109 * is assumed so we bracket the current expression with parens, if
110 * necessary, and add a -print node on the end.
111 */
112 if (!isoutput) {
113 OPTION *p;
114 char **argv1 = 0;
115
116 if (plan == NULL) {
117 p = lookup_option("-print");
118 new = (p->create)(p, &argv1);
119 tail = plan = new;
120 } else {
121 p = lookup_option("(");
122 new = (p->create)(p, &argv1);
123 new->next = plan;
124 plan = new;
125 p = lookup_option(")");
126 new = (p->create)(p, &argv1);
127 tail->next = new;
128 tail = new;
129 p = lookup_option("-print");
130 new = (p->create)(p, &argv1);
131 tail->next = new;
132 tail = new;
133 }
134 }
135
136 /*
137 * the command line has been completely processed into a search plan
138 * except for the (, ), !, and -o operators. Rearrange the plan so
139 * that the portions of the plan which are affected by the operators
140 * are moved into operator nodes themselves. For example:
141 *
142 * [!]--> [-name foo]--> [-print]
143 *
144 * becomes
145 *
146 * [! [-name foo] ]--> [-print]
147 *
148 * and
149 *
150 * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
151 *
152 * becomes
153 *
154 * [expr [-depth]-->[-name foo] ]--> [-print]
155 *
156 * operators are handled in order of precedence.
157 */
158
159 plan = paren_squish(plan); /* ()'s */
160 plan = not_squish(plan); /* !'s */
161 plan = or_squish(plan); /* -o's */
162 return (plan);
163}
164
165FTS *tree; /* pointer to top of FTS hierarchy */
166
167/*
168 * find_execute --
169 * take a search plan and an array of search paths and executes the plan
170 * over all FTSENT's returned for the given search paths.
171 */
172int
173find_execute(PLAN *plan, char *paths[])
174{
175 FTSENT *entry;
176 PLAN *p;
177 int rval;
178
179 tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL));
180 if (tree == NULL)
181 err(1, "ftsopen");
182
183 for (rval = 0; (entry = fts_read(tree)) != NULL;) {
184 if (maxdepth != -1 && entry->fts_level >= maxdepth) {
185 if (fts_set(tree, entry, FTS_SKIP))
186 err(1, "%s", entry->fts_path);
187 }
188
189 switch (entry->fts_info) {
190 case FTS_D:
191 if (isdepth)
192 continue;
193 break;
194 case FTS_DP:
195 if (!isdepth)
196 continue;
197 break;
198 case FTS_DNR:
199 case FTS_ERR:
200 case FTS_NS:
201 (void)fflush(stdout);
202 warnx("%s: %s",
203 entry->fts_path, strerror(entry->fts_errno));
204 rval = 1;
205 continue;
206#ifdef FTS_W
207 case FTS_W:
208 continue;
209#endif /* FTS_W */
210 }
211#define BADCH " \t\n\\'\""
212 if (isxargs && strpbrk(entry->fts_path, BADCH)) {
213 (void)fflush(stdout);
214 warnx("%s: illegal path", entry->fts_path);
215 rval = 1;
216 continue;
217 }
218
219 if (mindepth != -1 && entry->fts_level < mindepth)
220 continue;
221
222 /*
223 * Call all the functions in the execution plan until one is
224 * false or all have been executed. This is where we do all
225 * the work specified by the user on the command line.
226 */
227 for (p = plan; p && (p->execute)(p, entry); p = p->next);
228 }
229 finish_execplus();
230 if (errno)
231 err(1, "fts_read");
232 return (rval);
233}
46
47#include <sys/types.h>
48#include <sys/stat.h>
49
50#include <err.h>
51#include <errno.h>
52#include <fts.h>
53#include <regex.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57
58#include "find.h"
59
60static int find_compare(const FTSENT * const *s1, const FTSENT * const *s2);
61
62/*
63 * find_compare --
64 * tell fts_open() how to order the traversal of the hierarchy.
65 * This variant gives lexicographical order, i.e., alphabetical
66 * order within each directory.
67 */
68static int
69find_compare(const FTSENT * const *s1, const FTSENT * const *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(char *argv[])
82{
83 PLAN *plan, *tail, *new;
84
85 /*
86 * for each argument in the command line, determine what kind of node
87 * it is, create the appropriate node type and add the new plan node
88 * to the end of the existing plan. The resulting plan is a linked
89 * list of plan nodes. For example, the string:
90 *
91 * % find . -name foo -newer bar -print
92 *
93 * results in the plan:
94 *
95 * [-name foo]--> [-newer bar]--> [-print]
96 *
97 * in this diagram, `[-name foo]' represents the plan node generated
98 * by c_name() with an argument of foo and `-->' represents the
99 * plan->next pointer.
100 */
101 for (plan = tail = NULL; *argv;) {
102 if (!(new = find_create(&argv)))
103 continue;
104 if (plan == NULL)
105 tail = plan = new;
106 else {
107 tail->next = new;
108 tail = new;
109 }
110 }
111
112 /*
113 * if the user didn't specify one of -print, -ok or -exec, then -print
114 * is assumed so we bracket the current expression with parens, if
115 * necessary, and add a -print node on the end.
116 */
117 if (!isoutput) {
118 OPTION *p;
119 char **argv1 = 0;
120
121 if (plan == NULL) {
122 p = lookup_option("-print");
123 new = (p->create)(p, &argv1);
124 tail = plan = new;
125 } else {
126 p = lookup_option("(");
127 new = (p->create)(p, &argv1);
128 new->next = plan;
129 plan = new;
130 p = lookup_option(")");
131 new = (p->create)(p, &argv1);
132 tail->next = new;
133 tail = new;
134 p = lookup_option("-print");
135 new = (p->create)(p, &argv1);
136 tail->next = new;
137 tail = new;
138 }
139 }
140
141 /*
142 * the command line has been completely processed into a search plan
143 * except for the (, ), !, and -o operators. Rearrange the plan so
144 * that the portions of the plan which are affected by the operators
145 * are moved into operator nodes themselves. For example:
146 *
147 * [!]--> [-name foo]--> [-print]
148 *
149 * becomes
150 *
151 * [! [-name foo] ]--> [-print]
152 *
153 * and
154 *
155 * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
156 *
157 * becomes
158 *
159 * [expr [-depth]-->[-name foo] ]--> [-print]
160 *
161 * operators are handled in order of precedence.
162 */
163
164 plan = paren_squish(plan); /* ()'s */
165 plan = not_squish(plan); /* !'s */
166 plan = or_squish(plan); /* -o's */
167 return (plan);
168}
169
170FTS *tree; /* pointer to top of FTS hierarchy */
171
172/*
173 * find_execute --
174 * take a search plan and an array of search paths and executes the plan
175 * over all FTSENT's returned for the given search paths.
176 */
177int
178find_execute(PLAN *plan, char *paths[])
179{
180 FTSENT *entry;
181 PLAN *p;
182 int rval;
183
184 tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL));
185 if (tree == NULL)
186 err(1, "ftsopen");
187
188 for (rval = 0; (entry = fts_read(tree)) != NULL;) {
189 if (maxdepth != -1 && entry->fts_level >= maxdepth) {
190 if (fts_set(tree, entry, FTS_SKIP))
191 err(1, "%s", entry->fts_path);
192 }
193
194 switch (entry->fts_info) {
195 case FTS_D:
196 if (isdepth)
197 continue;
198 break;
199 case FTS_DP:
200 if (!isdepth)
201 continue;
202 break;
203 case FTS_DNR:
204 case FTS_ERR:
205 case FTS_NS:
206 (void)fflush(stdout);
207 warnx("%s: %s",
208 entry->fts_path, strerror(entry->fts_errno));
209 rval = 1;
210 continue;
211#ifdef FTS_W
212 case FTS_W:
213 continue;
214#endif /* FTS_W */
215 }
216#define BADCH " \t\n\\'\""
217 if (isxargs && strpbrk(entry->fts_path, BADCH)) {
218 (void)fflush(stdout);
219 warnx("%s: illegal path", entry->fts_path);
220 rval = 1;
221 continue;
222 }
223
224 if (mindepth != -1 && entry->fts_level < mindepth)
225 continue;
226
227 /*
228 * Call all the functions in the execution plan until one is
229 * false or all have been executed. This is where we do all
230 * the work specified by the user on the command line.
231 */
232 for (p = plan; p && (p->execute)(p, entry); p = p->next);
233 }
234 finish_execplus();
235 if (errno)
236 err(1, "fts_read");
237 return (rval);
238}