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