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