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