From owner-svn-src-head@FreeBSD.ORG Wed Jul 10 22:45:08 2013 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 3EE4E177; Wed, 10 Jul 2013 22:45:08 +0000 (UTC) (envelope-from dteske@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id 307241FBE; Wed, 10 Jul 2013 22:45:08 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id r6AMj8xB014630; Wed, 10 Jul 2013 22:45:08 GMT (envelope-from dteske@svn.freebsd.org) Received: (from dteske@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id r6AMj8dk014629; Wed, 10 Jul 2013 22:45:08 GMT (envelope-from dteske@svn.freebsd.org) Message-Id: <201307102245.r6AMj8dk014629@svn.freebsd.org> From: Devin Teske Date: Wed, 10 Jul 2013 22:45:08 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r253175 - head/usr.sbin/bsdconfig/share X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Jul 2013 22:45:08 -0000 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.