]>
git.saurik.com Git - apple/shell_cmds.git/blob - find/find.c
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Cimarron D. Taylor of the University of California, Berkeley.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
35 static char sccsid
[] = "@(#)find.c 8.5 (Berkeley) 8/5/94";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/usr.bin/find/find.c,v 1.23 2010/12/11 08:32:16 joel Exp $");
43 #include <sys/types.h>
55 #include <get_compat.h>
58 #define COMPAT_MODE(func, mode) 1
64 static int find_compare(const FTSENT
**s1
, const FTSENT
**s2
);
65 #else /* !__APPLE__ */
66 static int find_compare(const FTSENT
* const *s1
, const FTSENT
* const *s2
);
67 #endif /* __APPLE__ */
71 * tell fts_open() how to order the traversal of the hierarchy.
72 * This variant gives lexicographical order, i.e., alphabetical
73 * order within each directory.
77 find_compare(const FTSENT
**s1
, const FTSENT
**s2
)
78 #else /* !__APPLE__ */
79 find_compare(const FTSENT
* const *s1
, const FTSENT
* const *s2
)
80 #endif /* __APPLE__ */
83 return (strcoll((*s1
)->fts_name
, (*s2
)->fts_name
));
88 * process the command line and create a "plan" corresponding to the
92 find_formplan(char *argv
[])
94 PLAN
*plan
, *tail
, *new;
97 * for each argument in the command line, determine what kind of node
98 * it is, create the appropriate node type and add the new plan node
99 * to the end of the existing plan. The resulting plan is a linked
100 * list of plan nodes. For example, the string:
102 * % find . -name foo -newer bar -print
104 * results in the plan:
106 * [-name foo]--> [-newer bar]--> [-print]
108 * in this diagram, `[-name foo]' represents the plan node generated
109 * by c_name() with an argument of foo and `-->' represents the
110 * plan->next pointer.
112 for (plan
= tail
= NULL
; *argv
;) {
113 if (!(new = find_create(&argv
)))
124 * if the user didn't specify one of -print, -ok or -exec, then -print
125 * is assumed so we bracket the current expression with parens, if
126 * necessary, and add a -print node on the end.
133 p
= lookup_option("-print");
134 new = (p
->create
)(p
, &argv1
);
137 p
= lookup_option("(");
138 new = (p
->create
)(p
, &argv1
);
141 p
= lookup_option(")");
142 new = (p
->create
)(p
, &argv1
);
145 p
= lookup_option("-print");
146 new = (p
->create
)(p
, &argv1
);
153 * the command line has been completely processed into a search plan
154 * except for the (, ), !, and -o operators. Rearrange the plan so
155 * that the portions of the plan which are affected by the operators
156 * are moved into operator nodes themselves. For example:
158 * [!]--> [-name foo]--> [-print]
162 * [! [-name foo] ]--> [-print]
166 * [(]--> [-depth]--> [-name foo]--> [)]--> [-print]
170 * [expr [-depth]-->[-name foo] ]--> [-print]
172 * operators are handled in order of precedence.
175 plan
= paren_squish(plan
); /* ()'s */
176 plan
= not_squish(plan
); /* !'s */
177 plan
= or_squish(plan
); /* -o's */
181 /* addPath - helper function used to build a list of paths that were
182 * specified on the command line that we are allowed to search.
184 static char **addPath(char **array
, char *newPath
)
186 static int pathCounter
= 0;
188 if (newPath
== NULL
) { /* initialize array */
189 if ((array
= malloc(sizeof(char *))) == NULL
)
190 err(2, "out of memory");
194 array
= realloc(array
, (++pathCounter
+ 1) * sizeof(char *));
196 err(2, "out of memory");
198 array
[pathCounter
- 1] = newPath
;
199 array
[pathCounter
] = NULL
; /* ensure array is null terminated */
205 FTS
*tree
; /* pointer to top of FTS hierarchy */
209 * take a search plan and an array of search paths and executes the plan
210 * over all FTSENT's returned for the given search paths.
213 find_execute(PLAN
*plan
, char *paths
[])
219 int nonSearchableDirFound
= 0;
221 struct stat statInfo
;
223 /* special-case directories specified on command line - explicitly examine
224 * mode bits, to ensure failure if the directory cannot be searched
225 * (whether or not it's empty). UNIX conformance... <sigh>
228 int strict_symlinks
= (ftsoptions
& (FTS_COMFOLLOW
|FTS_LOGICAL
))
229 && COMPAT_MODE("bin/find", "unix2003");
231 myPaths
= addPath(NULL
, NULL
);
232 for (pathIndex
= 0; paths
[pathIndex
] != NULL
; ++pathIndex
) {
233 int stat_ret
= stat(paths
[pathIndex
], &statInfo
);
234 int stat_errno
= errno
;
235 if (strict_symlinks
&& stat_ret
< 0) {
236 if (stat_errno
== ELOOP
) {
237 errx(1, "Symlink loop resolving %s", paths
[pathIndex
]);
241 /* retrieve mode bits, and examine "searchable" bit of
242 directories, exempt root from POSIX conformance */
243 if (COMPAT_MODE("bin/find", "unix2003") && getuid()
245 && ((statInfo
.st_mode
& S_IFMT
) == S_IFDIR
)) {
246 if (access(paths
[pathIndex
], X_OK
) == 0) {
247 myPaths
= addPath(myPaths
, paths
[pathIndex
]);
249 if (stat_errno
!= ENAMETOOLONG
) { /* if name is too long, just let existing logic handle it */
250 warnx("%s: Permission denied", paths
[pathIndex
]);
251 nonSearchableDirFound
= 1;
255 /* not a directory, so add path to array */
256 myPaths
= addPath(myPaths
, paths
[pathIndex
]);
259 if (myPaths
[0] == NULL
) { /* were any directories searchable? */
261 return(nonSearchableDirFound
); /* no... */
264 tree
= fts_open(myPaths
, ftsoptions
, (issort
? find_compare
: NULL
));
268 for (rval
= nonSearchableDirFound
; (entry
= fts_read(tree
)) != NULL
;) {
269 if (maxdepth
!= -1 && entry
->fts_level
>= maxdepth
) {
270 if (fts_set(tree
, entry
, FTS_SKIP
))
271 err(1, "%s", entry
->fts_path
);
274 switch (entry
->fts_info
) {
286 (void)fflush(stdout
);
288 entry
->fts_path
, strerror(entry
->fts_errno
));
296 #define BADCH " \t\n\\'\""
297 if (isxargs
&& strpbrk(entry
->fts_path
, BADCH
)) {
298 (void)fflush(stdout
);
299 warnx("%s: illegal path", entry
->fts_path
);
304 if (mindepth
!= -1 && entry
->fts_level
< mindepth
)
308 * Call all the functions in the execution plan until one is
309 * false or all have been executed. This is where we do all
310 * the work specified by the user on the command line.
312 for (p
= plan
; p
&& (p
->execute
)(p
, entry
); p
= p
->next
);
316 if (execplus_error
) {
317 exit(execplus_error
);