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