xref: /freebsd/usr.bin/find/find.c (revision 3f0164abf32b9b761e0a2cb4bdca3a8b84f156d4)
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  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)find.c	8.5 (Berkeley) 8/5/94";
36 #else
37 #endif
38 #endif /* not lint */
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 
46 #include <err.h>
47 #include <errno.h>
48 #include <fts.h>
49 #include <regex.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 
54 #include "find.h"
55 
56 static int find_compare(const FTSENT * const *s1, const FTSENT * const *s2);
57 
58 /*
59  * find_compare --
60  *	tell fts_open() how to order the traversal of the hierarchy.
61  *	This variant gives lexicographical order, i.e., alphabetical
62  *	order within each directory.
63  */
64 static int
65 find_compare(const FTSENT * const *s1, const FTSENT * const *s2)
66 {
67 
68 	return (strcoll((*s1)->fts_name, (*s2)->fts_name));
69 }
70 
71 /*
72  * find_formplan --
73  *	process the command line and create a "plan" corresponding to the
74  *	command arguments.
75  */
76 PLAN *
77 find_formplan(char *argv[])
78 {
79 	PLAN *plan, *tail, *new;
80 
81 	/*
82 	 * for each argument in the command line, determine what kind of node
83 	 * it is, create the appropriate node type and add the new plan node
84 	 * to the end of the existing plan.  The resulting plan is a linked
85 	 * list of plan nodes.  For example, the string:
86 	 *
87 	 *	% find . -name foo -newer bar -print
88 	 *
89 	 * results in the plan:
90 	 *
91 	 *	[-name foo]--> [-newer bar]--> [-print]
92 	 *
93 	 * in this diagram, `[-name foo]' represents the plan node generated
94 	 * by c_name() with an argument of foo and `-->' represents the
95 	 * plan->next pointer.
96 	 */
97 	for (plan = tail = NULL; *argv;) {
98 		if (!(new = find_create(&argv)))
99 			continue;
100 		if (plan == NULL)
101 			tail = plan = new;
102 		else {
103 			tail->next = new;
104 			tail = new;
105 		}
106 	}
107 
108 	/*
109 	 * if the user didn't specify one of -print, -ok or -exec, then -print
110 	 * is assumed so we bracket the current expression with parens, if
111 	 * necessary, and add a -print node on the end.
112 	 */
113 	if (!isoutput) {
114 		OPTION *p;
115 		char **argv1 = 0;
116 
117 		if (plan == NULL) {
118 			p = lookup_option("-print");
119 			new = (p->create)(p, &argv1);
120 			tail = plan = new;
121 		} else {
122 			p = lookup_option("(");
123 			new = (p->create)(p, &argv1);
124 			new->next = plan;
125 			plan = new;
126 			p = lookup_option(")");
127 			new = (p->create)(p, &argv1);
128 			tail->next = new;
129 			tail = new;
130 			p = lookup_option("-print");
131 			new = (p->create)(p, &argv1);
132 			tail->next = new;
133 			tail = new;
134 		}
135 	}
136 
137 	/*
138 	 * the command line has been completely processed into a search plan
139 	 * except for the (, ), !, and -o operators.  Rearrange the plan so
140 	 * that the portions of the plan which are affected by the operators
141 	 * are moved into operator nodes themselves.  For example:
142 	 *
143 	 *	[!]--> [-name foo]--> [-print]
144 	 *
145 	 * becomes
146 	 *
147 	 *	[! [-name foo] ]--> [-print]
148 	 *
149 	 * and
150 	 *
151 	 *	[(]--> [-depth]--> [-name foo]--> [)]--> [-print]
152 	 *
153 	 * becomes
154 	 *
155 	 *	[expr [-depth]-->[-name foo] ]--> [-print]
156 	 *
157 	 * operators are handled in order of precedence.
158 	 */
159 
160 	plan = paren_squish(plan);		/* ()'s */
161 	plan = not_squish(plan);		/* !'s */
162 	plan = or_squish(plan);			/* -o's */
163 	return (plan);
164 }
165 
166 FTS *tree;			/* pointer to top of FTS hierarchy */
167 
168 /*
169  * find_execute --
170  *	take a search plan and an array of search paths and executes the plan
171  *	over all FTSENT's returned for the given search paths.
172  */
173 int
174 find_execute(PLAN *plan, char *paths[])
175 {
176 	FTSENT *entry;
177 	PLAN *p;
178 	int rval;
179 
180 	tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL));
181 	if (tree == NULL)
182 		err(1, "ftsopen");
183 
184 	for (rval = 0; (entry = fts_read(tree)) != NULL;) {
185 		if (maxdepth != -1 && entry->fts_level >= maxdepth) {
186 			if (fts_set(tree, entry, FTS_SKIP))
187 				err(1, "%s", entry->fts_path);
188 		}
189 
190 		switch (entry->fts_info) {
191 		case FTS_D:
192 			if (isdepth)
193 				continue;
194 			break;
195 		case FTS_DP:
196 			if (!isdepth)
197 				continue;
198 			break;
199 		case FTS_DNR:
200 		case FTS_NS:
201 			if (ignore_readdir_race &&
202 			    entry->fts_errno == ENOENT && entry->fts_level > 0)
203 				continue;
204 			/* FALLTHROUGH */
205 		case FTS_ERR:
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 && (!ignore_readdir_race || errno != ENOENT))
236 		err(1, "fts_read");
237 	return (rval);
238 }
239