-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlibfind.bash
More file actions
executable file
·339 lines (288 loc) · 7.36 KB
/
libfind.bash
File metadata and controls
executable file
·339 lines (288 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/bin/bash
# ----------------------------------------------------------------------
# libfind
#
# Finds library files with the use of expressions.
#
# By default, it searches for files in directories specified in
# /etc/ld.so.conf, but it can be configured to use other directories
# instead.
#
# Usage: libfind[.bash] [options] [-e] expression [[--] expression ...]
#
# Author: konsolebox
# Copyright Free / Public Domain
# May 10, 2022
# ----------------------------------------------------------------------
VERSION=2022.05.10
[ -n "${BASH_VERSION}" ] && [[ BASH_VERSINFO -ge 4 ]] || {
echo "This script requires Bash version 4.0 or newer." >&2
exit 1
}
set -f +o posix && shopt -s extglob || exit 1
declare -A LD_CONF_HASH=()
declare LIB_PATHS=()
declare LIB_PATHS_COMMON=(/{usr/{x86_64-pc-linux-gnu/,i386-pc-linux-gnu/,local/,},}lib{64,32,x32,})
function show_help_info {
echo "libfind ${VERSION}
Finds library files based on expressions.
By default it searches for files in directories specified in
/etc/ld.so.conf but it can be configured to use other directories
instead.
Usage: $0 [options] [-e] expression [[--] expression ...]
Use of common paths:
-c Search in common library directories instead of
those specified in /etc/ld.so.conf.
-C Same as -c but adds the common library directories
into the list extracted from /etc/ld.so.conf.
Glob-based options:
-p, --path Treat all expressions as keywords that will match
against the whole path and not just the filename.
-x, --exact Treat all expressions as exact glob patterns. No
extra wildcard character is added before or after
the keyword.
-X, --exact-path Same as -x, but the glob pattern applies with the
whole path, and not just the filename.
Regex-based options:
--awk Treat all expressions as Awk regular expressions.
--egrep Treat all expressions as egrep expressions.
--emacs Treat all expressions as Emacs regular expressions.
-E, --extended Treat all expressions as extended regular expressions.
-r, --regex Treat all expressions as basic regular expressions.
Modifiers:
-s Treat all expressions as case sensitive. This option can co-exist
with other expression type options.
Others:
-e EXPR Treat following argument as an expression.
-h, --help Show this help info.
-V, --version Show version.
Notes:
- Only one of --awk, --egrep, --emacs, -E, -r, -p, -x or -X can
become effective.
- Regex-based expression types rely on find's -regex so they match a
whole pathname and not just a file's filename.
- When no expression type is specified, it defaults to glob patterns."
}
function get_clean_path {
local t=() i=0 IFS=/
case $1 in
/*)
set -- $1
;;
*)
set -- ${PWD} $1
;;
esac
for __; do
case $__ in
..)
(( i )) && unset 't[--i]'
continue
;;
''|.)
continue
;;
esac
t[i++]=$__
done
__="/${t[*]}"
}
function get_lib_paths {
# This function tries to comply with ldconfig's behavior.
# See parse_conf() and parse_conf_include() in ldconfig.c.
# It expects that IFS is set to default value, and that noglob is set.
local file=$1
if [[ -z ${LD_CONF_HASH[${file}]+.} && -f ${file} && -r ${file} ]]; then
LD_CONF_HASH[${file}]=.
while read -r __; do
case $__ in
/*)
get_clean_path "$__"
LIB_PATHS+=("$__")
;;
include\ *)
set -- $__
shift
for __; do
if [[ $__ == /* ]]; then
get_clean_path "$__"
else
get_clean_path "${file}/../$__"
fi
while read -r __; do
get_lib_paths "$__"
done < <(compgen -G "$__")
done
;;
esac
done < "${file}"
fi
}
function fail {
printf '%s\n' "$@" >&2
exit 1
}
function get_opt_and_optarg {
OPT=$1 OPTARG= OPTSHIFT=0
if [[ $1 == -[!-]?* ]]; then
OPT=${1:0:2} OPTARG=${1:2}
elif [[ $1 == --*=* ]]; then
OPT=${1%%=*} OPTARG=${1#*=}
elif [[ ${2+.} ]]; then
OPTARG=$2 OPTSHIFT=1
else
return 1
fi
return 0
}
function main {
local case_sensitive=false expressions=() mode=default use_or_add_common_paths=false __
while [[ $# -gt 0 ]]; do
case $1 in
-c)
[[ ${use_or_add_common_paths} == add ]] && fail "Only one of -c or -C can be specified."
use_or_add_common_paths=use
;;
-C)
[[ ${use_or_add_common_paths} == use ]] && fail "Only one of -c or -C can be specified."
use_or_add_common_paths=add
;;
--awk|--egrep|--emacs)
mode=${1#--}
;;
-E|--extended)
mode=extended
;;
-p|--path)
mode=path
;;
-r|--regex)
mode=basic
;;
-x|--exact)
mode=exact_pattern
;;
-X)
mode=exact_path_pattern
;;
-s)
case_sensitive=true
;;
-e*)
get_opt_and_optarg "${@:1:2}" || fail "No argument follows -e."
expressions+=("${OPTARG}")
shift "${OPTSHIFT}"
;;
-h|--help)
show_help_info
return 2
;;
-V|--version)
echo "${VERSION}"
return 2
;;
--)
expressions+=("${@:2}")
break
;;
-[!-][!-]*)
set -- "${1:0:2}" "-${1:2}" "${@:2}"
continue
;;
-?*)
fail "Invalid option: $1"
;;
*)
expressions+=("$1")
;;
esac
shift
done
[[ ${#expressions[@]} -eq 0 ]] && \
fail "No expression was specified. Run with --help to get usage info."
if [[ ${mode} == @(default|exact_pattern) ]]; then
for __ in "${expressions[@]}"; do
if [[ $__ == */* ]]; then
fail "Forward-slash (/) can't be included when matching against a filename." \
"Please run libfind with --help to see other search methods."
fi
done
fi
if [[ ${use_or_add_common_paths} == use ]]; then
LIB_PATHS=("${LIB_PATHS_COMMON[@]}")
else
get_lib_paths /etc/ld.so.conf
if [[ ${use_or_add_common_paths} == add ]]; then
LIB_PATHS+=("${LIB_PATHS_COMMON[@]}")
fi
fi
local lib_paths_filtered=()
local -A reg=()
for __ in "${LIB_PATHS[@]}"; do
if [[ $__ != *([[:blank:]]) && -z ${reg[$__]+.} && -d $__ && -r $__ && -x $__ ]]; then
lib_paths_filtered+=("$__")
reg[$__]=.
fi
done
[[ ${#lib_paths_filtered[@]} -eq 0 ]] && return 1
local name_opt="-iname" path_opt="-ipath" regex_opt="-iregex"
if [[ ${case_sensitive} == true ]]; then
name_opt="-name"
regex_opt="-regex"
path_opt="-path"
fi
local expr_opt=${name_opt} regex_type_args=() add_wildcards=true
case ${mode} in
awk)
expr_opt=${regex_opt}
regex_type_args=(-regextype posix-awk)
add_wildcards=false
;;
basic)
expr_opt=${regex_opt}
regex_type_args=(-regextype posix-basic)
add_wildcards=false
;;
emacs)
expr_opt=${regex_opt}
regex_type_args=(-regextype emacs)
add_wildcards=false
;;
egrep)
expr_opt=${regex_opt}
regex_type_args=(-regextype posix-egrep)
add_wildcards=false
;;
extended)
expr_opt=${regex_opt}
regex_type_args=(-regextype posix-extended)
add_wildcards=false
;;
exact_pattern)
expr_opt=${name_opt}
regex_type_args=()
add_wildcards=false
;;
exact_path_pattern)
expr_opt=${path_opt}
regex_type_args=()
add_wildcards=false
;;
path)
expr_opt=${path_opt}
regex_type_args=()
;;
esac
local expr_args=()
if [[ ${add_wildcards} == true ]]; then
for __ in "${expressions[@]}"; do
expr_args+=("${expr_opt}" "*$__*")
done
else
for __ in "${expressions[@]}"; do
expr_args+=("${expr_opt}" "$__")
done
fi
find "${lib_paths_filtered[@]}" -maxdepth 1 -xtype f "${regex_type_args[@]}" "${expr_args[@]}"
}
main "$@"