svn commit: r253175 - head/usr.sbin/bsdconfig/share

Devin Teske dteske at FreeBSD.org
Wed Jul 10 22:45:08 UTC 2013


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.


More information about the svn-src-head mailing list