Date: Wed, 10 Jul 2013 22:45:08 +0000 (UTC) From: Devin Teske <dteske@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r253175 - head/usr.sbin/bsdconfig/share Message-ID: <201307102245.r6AMj8dk014629@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: dteske Date: Wed Jul 10 22:45:07 2013 New Revision: 253175 URL: http://svnweb.freebsd.org/changeset/base/253175 Log: 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). Modified: head/usr.sbin/bsdconfig/share/common.subr Modified: head/usr.sbin/bsdconfig/share/common.subr ============================================================================== --- head/usr.sbin/bsdconfig/share/common.subr Wed Jul 10 22:12:48 2013 (r253174) +++ head/usr.sbin/bsdconfig/share/common.subr Wed Jul 10 22:45:07 2013 (r253175) @@ -212,6 +212,35 @@ f_have() 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] # # Utility function designed to go along with the already-builtin setvar.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201307102245.r6AMj8dk014629>