From patchwork Fri Oct 8 09:15:55 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 67139 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id D04F5B70B8 for ; Fri, 8 Oct 2010 20:24:33 +1100 (EST) Received: from localhost ([127.0.0.1]:43283 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P49Bl-00081m-Q3 for incoming@patchwork.ozlabs.org; Fri, 08 Oct 2010 05:24:29 -0400 Received: from [140.186.70.92] (port=54004 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P493o-0003pj-Ep for qemu-devel@nongnu.org; Fri, 08 Oct 2010 05:16:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P493n-0000Ib-G6 for qemu-devel@nongnu.org; Fri, 08 Oct 2010 05:16:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49380) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P493n-0000IT-8Z for qemu-devel@nongnu.org; Fri, 08 Oct 2010 05:16:15 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o989GDJb020546 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 8 Oct 2010 05:16:13 -0400 Received: from localhost6.localdomain6 (ovpn-113-89.phx2.redhat.com [10.3.113.89]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o989G3RV015657; Fri, 8 Oct 2010 05:16:11 -0400 From: Jes.Sorensen@redhat.com To: qemu-devel@nongnu.org Date: Fri, 8 Oct 2010 11:15:55 +0200 Message-Id: <1286529360-5715-3-git-send-email-Jes.Sorensen@redhat.com> In-Reply-To: <1286529360-5715-1-git-send-email-Jes.Sorensen@redhat.com> References: <1286529360-5715-1-git-send-email-Jes.Sorensen@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: pbonzini@redhat.com, armbru@redhat.com Subject: [Qemu-devel] [PATCH 2/7] Support human unit formats in strtosz, eg. 1.0G X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Jes Sorensen Signed-off-by: Jes Sorensen --- cutils.c | 34 ++++++++++++++++++++++++++-------- 1 files changed, 26 insertions(+), 8 deletions(-) diff --git a/cutils.c b/cutils.c index ee591c5..0782032 100644 --- a/cutils.c +++ b/cutils.c @@ -291,34 +291,52 @@ int fcntl_setfl(int fd, int flag) */ ssize_t strtosz(const char *nptr, char **end) { - int64_t value; + ssize_t retval = -1; char *endptr; + int mul_required = 0; + double val, mul = 1; + + endptr = (char *)nptr + strspn(nptr, " 0123456789"); + if (*endptr == '.') { + mul_required = 1; + } + + val = strtod(nptr, &endptr); + + if (val < 0) + goto fail; - value = strtoll(nptr, &endptr, 0); switch (*endptr++) { case 'K': case 'k': - value <<= 10; + mul = 1 << 10; break; case 0: + case ' ': + if (mul_required) { + goto fail; + } case 'M': case 'm': - value <<= 20; + mul = 1ULL << 20; break; case 'G': case 'g': - value <<= 30; + mul = 1ULL << 30; break; case 'T': case 't': - value <<= 40; + mul = 1ULL << 40; break; default: - value = -1; + goto fail; } + retval = (ssize_t)(val * mul); + if (end) *end = endptr; - return value; +fail: + return retval; }