find.c revision 200462
1130636Srwatson/*-
2101242Srwatson * Copyright (c) 1991, 1993, 1994
3107603Sru *	The Regents of the University of California.  All rights reserved.
4101242Srwatson *
5101242Srwatson * This code is derived from software contributed to Berkeley by
6101242Srwatson * Cimarron D. Taylor of the University of California, Berkeley.
7101242Srwatson *
8101242Srwatson * Redistribution and use in source and binary forms, with or without
9107603Sru * modification, are permitted provided that the following conditions
10101242Srwatson * are met:
11101242Srwatson * 1. Redistributions of source code must retain the above copyright
12101242Srwatson *    notice, this list of conditions and the following disclaimer.
13101242Srwatson * 2. Redistributions in binary form must reproduce the above copyright
14101242Srwatson *    notice, this list of conditions and the following disclaimer in the
15101242Srwatson *    documentation and/or other materials provided with the distribution.
16101242Srwatson * 3. All advertising materials mentioning features or use of this software
17101242Srwatson *    must display the following acknowledgement:
18107603Sru *	This product includes software developed by the University of
19101242Srwatson *	California, Berkeley and its contributors.
20101242Srwatson * 4. Neither the name of the University nor the names of its contributors
21101242Srwatson *    may be used to endorse or promote products derived from this software
22101242Srwatson *    without specific prior written permission.
23101242Srwatson *
24101242Srwatson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25101242Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26101242Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27101242Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28101242Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29101242Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30107603Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31101242Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32107603Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33101242Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34101242Srwatson * SUCH DAMAGE.
35107744Sru */
36101242Srwatson
37101242Srwatson#ifndef lint
38130636Srwatson#if 0
39101242Srwatsonstatic char sccsid[] = "@(#)find.c	8.5 (Berkeley) 8/5/94";
40131365Sru#else
41131365Sru#endif
42101242Srwatson#endif /* not lint */
43101242Srwatson
44107603Sru#include <sys/cdefs.h>
45107603Sru__FBSDID("$FreeBSD: head/usr.bin/find/find.c 200462 2009-12-13 03:14:06Z delphij $");
46101242Srwatson
47101242Srwatson#include <sys/types.h>
48109263Schris#include <sys/stat.h>
49109263Schris
50109263Schris#include <err.h>
51130636Srwatson#include <errno.h>
52130636Srwatson#include <fts.h>
53109263Schris#include <regex.h>
54109263Schris#include <stdio.h>
55122810Srwatson#include <stdlib.h>
56122810Srwatson#include <string.h>
57109263Schris
58109263Schris#include "find.h"
59109263Schris
60101242Srwatsonstatic int find_compare(const FTSENT * const *s1, const FTSENT * const *s2);
61101242Srwatson
62101242Srwatson/*
63122810Srwatson * find_compare --
64122810Srwatson *	tell fts_open() how to order the traversal of the hierarchy.
65130636Srwatson *	This variant gives lexicographical order, i.e., alphabetical
66131365Sru *	order within each directory.
67130636Srwatson */
68130636Srwatsonstatic int
69130636Srwatsonfind_compare(const FTSENT * const *s1, const FTSENT * const *s2)
70122810Srwatson{
71122810Srwatson
72101242Srwatson	return (strcoll((*s1)->fts_name, (*s2)->fts_name));
73122810Srwatson}
74122810Srwatson
75122810Srwatson/*
76122810Srwatson * find_formplan --
77122810Srwatson *	process the command line and create a "plan" corresponding to the
78122810Srwatson *	command arguments.
79122810Srwatson */
80122810SrwatsonPLAN *
81122810Srwatsonfind_formplan(char *argv[])
82122810Srwatson{
83122810Srwatson	PLAN *plan, *tail, *new;
84122810Srwatson
85109263Schris	/*
86101242Srwatson	 * for each argument in the command line, determine what kind of node
87122810Srwatson	 * it is, create the appropriate node type and add the new plan node
88122810Srwatson	 * to the end of the existing plan.  The resulting plan is a linked
89101242Srwatson	 * list of plan nodes.  For example, the string:
90122810Srwatson	 *
91131365Sru	 *	% find . -name foo -newer bar -print
92122810Srwatson	 *
93122810Srwatson	 * results in the plan:
94122810Srwatson	 *
95122810Srwatson	 *	[-name foo]--> [-newer bar]--> [-print]
96122810Srwatson	 *
97122810Srwatson	 * in this diagram, `[-name foo]' represents the plan node generated
98122810Srwatson	 * by c_name() with an argument of foo and `-->' represents the
99122810Srwatson	 * plan->next pointer.
100101242Srwatson	 */
101101242Srwatson	for (plan = tail = NULL; *argv;) {
102101242Srwatson		if (!(new = find_create(&argv)))
103101242Srwatson			continue;
104109263Schris		if (plan == NULL)
105101242Srwatson			tail = plan = new;
106101242Srwatson		else {
107101242Srwatson			tail->next = new;
108122810Srwatson			tail = new;
109122810Srwatson		}
110122810Srwatson	}
111101242Srwatson
112101242Srwatson	/*
113109263Schris	 * if the user didn't specify one of -print, -ok or -exec, then -print
114101242Srwatson	 * is assumed so we bracket the current expression with parens, if
115101242Srwatson	 * necessary, and add a -print node on the end.
116101242Srwatson	 */
117101242Srwatson	if (!isoutput) {
118101242Srwatson		OPTION *p;
119101242Srwatson		char **argv1 = 0;
120109263Schris
121101242Srwatson		if (plan == NULL) {
122101242Srwatson			p = lookup_option("-print");
123101242Srwatson			new = (p->create)(p, &argv1);
124101242Srwatson			tail = plan = new;
125101242Srwatson		} else {
126101242Srwatson			p = lookup_option("(");
127109263Schris			new = (p->create)(p, &argv1);
128101242Srwatson			new->next = plan;
129101242Srwatson			plan = new;
130101242Srwatson			p = lookup_option(")");
131101242Srwatson			new = (p->create)(p, &argv1);
132101242Srwatson			tail->next = new;
133109263Schris			tail = new;
134107603Sru			p = lookup_option("-print");
135120010Sru			new = (p->create)(p, &argv1);
136196123Srwatson			tail->next = new;
137109273Schris			tail = new;
138109263Schris		}
139101242Srwatson	}
140101242Srwatson
141101242Srwatson	/*
142101242Srwatson	 * the command line has been completely processed into a search plan
143101242Srwatson	 * except for the (, ), !, and -o operators.  Rearrange the plan so
144101242Srwatson	 * that the portions of the plan which are affected by the operators
145101242Srwatson	 * are moved into operator nodes themselves.  For example:
146101242Srwatson	 *
147119321Srwatson	 *	[!]--> [-name foo]--> [-print]
148119321Srwatson	 *
149119321Srwatson	 * becomes
150119321Srwatson	 *
151119321Srwatson	 *	[! [-name foo] ]--> [-print]
152119321Srwatson	 *
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}
239