From owner-svn-src-all@FreeBSD.ORG Sun Aug 15 18:32:07 2010 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 22B2B106564A; Sun, 15 Aug 2010 18:32:07 +0000 (UTC) (envelope-from des@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id EBCC48FC17; Sun, 15 Aug 2010 18:32:06 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o7FIW6UL068106; Sun, 15 Aug 2010 18:32:06 GMT (envelope-from des@svn.freebsd.org) Received: (from des@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o7FIW6nA068103; Sun, 15 Aug 2010 18:32:06 GMT (envelope-from des@svn.freebsd.org) Message-Id: <201008151832.o7FIW6nA068103@svn.freebsd.org> From: Dag-Erling Smorgrav Date: Sun, 15 Aug 2010 18:32:06 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r211343 - head/lib/libutil X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Aug 2010 18:32:07 -0000 Author: des Date: Sun Aug 15 18:32:06 2010 New Revision: 211343 URL: http://svn.freebsd.org/changeset/base/211343 Log: Further simplify the code, and update the manpage. Submitted by: Christoph Mallon Modified: head/lib/libutil/expand_number.3 head/lib/libutil/expand_number.c Modified: head/lib/libutil/expand_number.3 ============================================================================== --- head/lib/libutil/expand_number.3 Sun Aug 15 17:49:41 2010 (r211342) +++ head/lib/libutil/expand_number.3 Sun Aug 15 18:32:06 2010 (r211343) @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd April 16, 2007 +.Dd August 15, 2010 .Dt EXPAND_NUMBER 3 .Os .Sh NAME @@ -37,14 +37,14 @@ .In libutil.h .Ft int .Fo expand_number -.Fa "const char *buf" "int64_t *num" +.Fa "const char *buf" "uint64_t *num" .Fc .Sh DESCRIPTION The .Fn expand_number function unformats the .Fa buf -string and stores a signed 64-bit quantity at address pointed out by the +string and stores a unsigned 64-bit quantity at address pointed out by the .Fa num argument. .Pp Modified: head/lib/libutil/expand_number.c ============================================================================== --- head/lib/libutil/expand_number.c Sun Aug 15 17:49:41 2010 (r211342) +++ head/lib/libutil/expand_number.c Sun Aug 15 18:32:06 2010 (r211343) @@ -36,7 +36,7 @@ __FBSDID("$FreeBSD$"); #include /* - * Convert an expression of the following forms to a int64_t. + * Convert an expression of the following forms to a uint64_t. * 1) A positive decimal number. * 2) A positive decimal number followed by a 'b' or 'B' (mult by 1). * 3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10). @@ -50,6 +50,7 @@ int expand_number(const char *buf, uint64_t *num) { uint64_t number; + unsigned shift; char *endptr; number = strtoumax(buf, &endptr, 0); @@ -60,41 +61,41 @@ expand_number(const char *buf, uint64_t return (-1); } - if (*endptr == '\0') { - /* No unit. */ - *num = number; - return (0); - } - -#define SHIFT(n, b) \ - do { if (((n << b) >> b) != n) goto overflow; n <<= b; } while (0) - switch (tolower((unsigned char)*endptr)) { case 'e': - SHIFT(number, 10); + shift = 60; + break; case 'p': - SHIFT(number, 10); + shift = 50; + break; case 't': - SHIFT(number, 10); + shift = 40; + break; case 'g': - SHIFT(number, 10); + shift = 30; + break; case 'm': - SHIFT(number, 10); + shift = 20; + break; case 'k': - SHIFT(number, 10); - case 'b': + shift = 10; break; + case 'b': + case '\0': /* No unit. */ + *num = number; + return (0); default: /* Unrecognized unit. */ errno = EINVAL; return (-1); } - *num = number; - return (0); + if ((number << shift) >> shift != number) { + /* Overflow */ + errno = ERANGE; + return (-1); + } -overflow: - /* Overflow */ - errno = ERANGE; - return (-1); + *num = number << shift; + return (0); }