xref: /netbsd-src/usr.bin/find/find.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*-
2  * Copyright (c) 1991, 1993
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 /*static char sccsid[] = "from: @(#)find.c	8.1 (Berkeley) 6/6/93";*/
39 static char rcsid[] = "$Id: find.c,v 1.6 1994/07/18 09:55:36 cgd Exp $";
40 #endif /* not lint */
41 
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <fts.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <stdlib.h>
51 
52 #include "find.h"
53 
54 /*
55  * find_formplan --
56  *	process the command line and create a "plan" corresponding to the
57  *	command arguments.
58  */
59 PLAN *
60 find_formplan(argv)
61 	char **argv;
62 {
63 	PLAN *plan, *tail, *new;
64 
65 	/*
66 	 * for each argument in the command line, determine what kind of node
67 	 * it is, create the appropriate node type and add the new plan node
68 	 * to the end of the existing plan.  The resulting plan is a linked
69 	 * list of plan nodes.  For example, the string:
70 	 *
71 	 *	% find . -name foo -newer bar -print
72 	 *
73 	 * results in the plan:
74 	 *
75 	 *	[-name foo]--> [-newer bar]--> [-print]
76 	 *
77 	 * in this diagram, `[-name foo]' represents the plan node generated
78 	 * by c_name() with an argument of foo and `-->' represents the
79 	 * plan->next pointer.
80 	 */
81 	for (plan = tail = NULL; *argv;) {
82 		if (!(new = find_create(&argv)))
83 			continue;
84 		if (plan == NULL)
85 			tail = plan = new;
86 		else {
87 			tail->next = new;
88 			tail = new;
89 		}
90 	}
91 
92 	/*
93 	 * if the user didn't specify one of -print, -ok or -exec, then -print
94 	 * is assumed so we bracket the current expression with parens, if
95 	 * necessary, and add a -print node on the end.
96 	 */
97 	if (!isoutput) {
98 		if (plan == NULL) {
99 			new = c_print();
100 			tail = plan = new;
101 		} else {
102 			new = c_openparen();
103 			new->next = plan;
104 			plan = new;
105 			new = c_closeparen();
106 			tail->next = new;
107 			tail = new;
108 			new = c_print();
109 			tail->next = new;
110 			tail = new;
111 		}
112 	}
113 
114 	/*
115 	 * the command line has been completely processed into a search plan
116 	 * except for the (, ), !, and -o operators.  Rearrange the plan so
117 	 * that the portions of the plan which are affected by the operators
118 	 * are moved into operator nodes themselves.  For example:
119 	 *
120 	 *	[!]--> [-name foo]--> [-print]
121 	 *
122 	 * becomes
123 	 *
124 	 *	[! [-name foo] ]--> [-print]
125 	 *
126 	 * and
127 	 *
128 	 *	[(]--> [-depth]--> [-name foo]--> [)]--> [-print]
129 	 *
130 	 * becomes
131 	 *
132 	 *	[expr [-depth]-->[-name foo] ]--> [-print]
133 	 *
134 	 * operators are handled in order of precedence.
135 	 */
136 
137 	plan = paren_squish(plan);		/* ()'s */
138 	plan = not_squish(plan);		/* !'s */
139 	plan = or_squish(plan);			/* -o's */
140 	return (plan);
141 }
142 
143 FTS *tree;			/* pointer to top of FTS hierarchy */
144 
145 /*
146  * find_execute --
147  *	take a search plan and an array of search paths and executes the plan
148  *	over all FTSENT's returned for the given search paths.
149  */
150 void
151 find_execute(plan, paths)
152 	PLAN *plan;		/* search plan */
153 	char **paths;		/* array of pathnames to traverse */
154 {
155 	register FTSENT *entry;
156 	PLAN *p;
157 
158 	if (!(tree = fts_open(paths, ftsoptions, (int (*)())NULL)))
159 		err(1, "ftsopen");
160 
161 	while (entry = fts_read(tree)) {
162 		switch(entry->fts_info) {
163 		case FTS_D:
164 			if (isdepth)
165 				continue;
166 			break;
167 		case FTS_DP:
168 			if (!isdepth)
169 				continue;
170 			break;
171 		case FTS_DNR:
172 		case FTS_ERR:
173 		case FTS_NS:
174 			(void)fflush(stdout);
175 			warn("%s", entry->fts_path);
176 			continue;
177 		}
178 #define	BADCH	" \t\n\\'\""
179 		if (isxargs && strpbrk(entry->fts_path, BADCH)) {
180 			(void)fflush(stdout);
181 			warnx("%s: illegal path", entry->fts_path);
182 			continue;
183 		}
184 
185 		/*
186 		 * call all the functions in the execution plan until one is
187 		 * false or all have been executed.  This is where we do all
188 		 * the work specified by the user on the command line.
189 		 */
190 		for (p = plan; p && (p->eval)(p, entry); p = p->next);
191 	}
192 	(void)fts_close(tree);
193 }
194