3 # Copyright (C) 2008 The Android Open Source Project
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 # Finds files with the specified name under a particular directory, stopping
20 # the search in a given subdirectory when the file is found.
23 set -o nounset # fail when dereferencing unset variables
24 set -o errexit # fail if any subcommand fails
26 progName=`basename $0`
29 echo "$progName: $@" >&2
42 Usage: $progName [<options>] <dirlist> <filename>
46 Both behave in the same way as their find(1) equivalents.
48 Avoids returning results from any path matching the given glob-style
49 pattern (e.g., "*/out/*"). May be used multiple times.
65 while [[ "${1:0:2}" == "--" ]]
70 if [[ "$name" == "mindepth" || "$name" == "maxdepth" ]]
72 # Add to beginning of findargs; these must come before the expression.
73 findargs="-$name $value $findargs"
74 elif [[ "$name" == "prune" ]]
76 # Add to end of findargs; these are part of the expression.
77 findargs="$findargs -path $value -prune -or"
83 # The filename is the last argument
86 # Print out all files that match, as long as the path isn't explicitly
87 # pruned. This will print out extraneous results from directories whose
88 # parents have a match. These are filtered out by the awk script below.
89 find "${@:1:$nargs-1}" $findargs -type f -name "$filename" -print |
91 # Only pass along the directory of each match.
92 sed -e 's/\/[^\/]*$/\//' |
94 # Sort the output, so directories appear immediately before their contents.
95 # If there are any duplicates, the awk script will implicitly ignore them.
96 # The LC_ALL=C forces sort(1) to use bytewise ordering instead of listening
97 # to the locale, which may do case-insensitive and/or alphanumeric-only
101 # Always print the first line, which can't possibly be covered by a
102 # parent directory match. After that, only print lines where the last
103 # line printed isn't a prefix.
104 awk -v "filename=$filename" '
105 (NR == 1) || (index($0, last) != 1) {
107 printf("%s%s\n", $0, filename);