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