From owner-freebsd-hackers@FreeBSD.ORG Fri Feb 11 18:07:59 2011 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 228C8106566B for ; Fri, 11 Feb 2011 18:07:59 +0000 (UTC) (envelope-from dteske@vicor.com) Received: from postoffice.vicor.com (postoffice.vicor.com [69.26.56.53]) by mx1.freebsd.org (Postfix) with ESMTP id 0937A8FC08 for ; Fri, 11 Feb 2011 18:07:58 +0000 (UTC) Received: from [192.82.228.105] (port=61137) by postoffice.vicor.com with esmtpsa (SSLv3:AES256-SHA:256) (Exim 4.71) (envelope-from ) id 1PnxPQ-0005Gg-Gk; Fri, 11 Feb 2011 10:07:58 -0800 From: Devin Teske To: Ian Smith In-Reply-To: <20110212015155.I96449@sola.nimnet.asn.au> References: <20110211120031.9D37510656E5@hub.freebsd.org> <20110212015155.I96449@sola.nimnet.asn.au> Organization: VICOR, Inc. Date: Fri, 11 Feb 2011 10:08:07 -0800 Message-ID: <1297447687.9144.6.camel@dt.vicor.com> Mime-Version: 1.0 X-Mailer: Evolution 2.32.1 X-Scan-Signature: 417bba55d4dc493adb4d94730825773c X-Scan-Host: postoffice.vicor.com Content-Type: text/plain; charset="cp1252" Content-Transfer-Encoding: 7bit X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: freebsd-hackers@freebsd.org Subject: Re: [RELEASE] host-setup(1): a dialog(1)-based utility for configuring FreeBSD X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Feb 2011 18:07:59 -0000 On Sat, 2011-02-12 at 02:30 +1100, Ian Smith wrote: > In freebsd-questions Digest, Vol 349, Issue 8, Message: 15 > On Thu, 10 Feb 2011 19:53:53 -0800 Devin Teske wrote: > > Hi All, > > > > I'd like to announce the release of a new script. A script that I've > > developed for our field engineers that I'd like to share with the rest > > of the world. > > > > http://druidbsd.sourceforge.net/download/host-setup.txt > > > > host-setup(1) is a dialog(1)-based utility (written in sh(1)) designed > > to make configuring FreeBSD more efficient. > > Nice, if only as great bedtime reading so far; I've already learned some > new techniques. I'm particularly proud of this little diddy (see lines 1600-1607 of host-setup; rewritten to be a functional example program that takes a number like "26" and produces "255.255.255.192"): ============================================================ #!/bin/sh blen2netmask() { local nbits="$1" netmask="" n=0 while [ $n -lt 4 ]; do netmask="$netmask${netmask:+.}$(( (65280 >> ($nbits - 8 * $n) & 255) * ((8*$n) < $nbits & $nbits <= (8*($n+1))) + 255 * ($nbits > (8*($n+1))) ))" n=$(( $n + 1 )) done echo "$netmask" } blen2netmask "$@" ============================================================ I wrote _several_ other implementations and above is the one that I finally settled on as being the most elegant. I was after a solution that (a) didn't use anything but built-in internals of sh(1), and (b) got the job done as fast as computationally possible. $ time blen2netmask 26 255.255.255.192 real 0m0.004s user 0m0.001s sys 0m0.004s That's pretty fast, I'd say ^_^ (faster than the other implementations -- especially considering that it doesn't have to fork anything). -- Devin P.S. Maybe I ought to expand it to IPv6 considering that the IPv4 address space has [reportedly] finally ran out (is that true?). > I expect to steal lots of it wholesale (acknowledged :) > > cheers, Ian > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"