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