1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-20 11:11:24 +00:00

Introduce a new [yet unused] function for [efficiently] getting the path to

an executable by-name without forking or using externals.

In a performance benchmark of 10,000 runs on circa 2006 hardware, f_which
out-performed `which' with an average completion time of ~2.5 seconds versus
~56 seconds.

This should be handy for future use (not that I make it a habit to call
`which' in a loop 10,000 times).
This commit is contained in:
Devin Teske 2013-07-10 22:45:07 +00:00
parent de11bcac39
commit c0adcdb9c1
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=253175

View File

@ -212,6 +212,35 @@ f_have()
f_quietly type "$@" f_quietly type "$@"
} }
# f_which $anything [$var_to_set]
#
# A fast built-in replacement for syntaxes such as foo=$( which bar ). In a
# comparison of 10,000 runs of this function versus which, this function
# completed in under 3 seconds, while `which' took almost a full minute.
#
# If $var_to_set is missing or NULL, output is (like which) to standard out.
# Returns success if a match was found, failure otherwise.
#
f_which()
{
local __name="$1" __var_to_set="$2"
case "$__name" in */*|'') return $FAILURE; esac
local __p IFS=":" __found=
for __p in $PATH; do
local __exec="$__p/$__name"
[ -f "$__exec" -a -x "$__exec" ] && __found=1 && break
done
if [ "$__found" ]; then
if [ "$__var_to_set" ]; then
setvar "$__var_to_set" "$__exec"
else
echo "$__exec"
fi
return $SUCCESS
fi
return $FAILURE
}
# f_getvar $var_to_get [$var_to_set] # f_getvar $var_to_get [$var_to_set]
# #
# Utility function designed to go along with the already-builtin setvar. # Utility function designed to go along with the already-builtin setvar.