From patchwork Thu Sep 16 14:52:26 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 64980 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]) by ozlabs.org (Postfix) with ESMTP id F0948B70D6 for ; Fri, 17 Sep 2010 01:32:58 +1000 (EST) Received: from localhost ([127.0.0.1]:57590 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OwGLI-00028e-B2 for incoming@patchwork.ozlabs.org; Thu, 16 Sep 2010 11:25:44 -0400 Received: from [140.186.70.92] (port=52764 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OwGGB-000579-AR for qemu-devel@nongnu.org; Thu, 16 Sep 2010 11:20:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OwFpF-0006qz-Jk for qemu-devel@nongnu.org; Thu, 16 Sep 2010 10:52:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:8397) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OwFpF-0006qs-CU for qemu-devel@nongnu.org; Thu, 16 Sep 2010 10:52:37 -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 o8GEqZlY002858 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 16 Sep 2010 10:52:35 -0400 Received: from localhost6.localdomain6 (ovpn-113-72.phx2.redhat.com [10.3.113.72]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o8GEqVFc027556; Thu, 16 Sep 2010 10:52:34 -0400 From: Jes.Sorensen@redhat.com To: qemu-devel@nongnu.org Date: Thu, 16 Sep 2010 16:52:26 +0200 Message-Id: <1284648749-18479-3-git-send-email-Jes.Sorensen@redhat.com> In-Reply-To: <1284648749-18479-1-git-send-email-Jes.Sorensen@redhat.com> References: <1284648749-18479-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 Subject: [Qemu-devel] [PATCH 2/5] Support human unit formats in strtobytes, 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 dabbed4..67767a9 100644 --- a/cutils.c +++ b/cutils.c @@ -259,34 +259,52 @@ int fcntl_setfl(int fd, int flag) */ uint64_t strtobytes(const char *nptr, char **end) { - uint64_t value; + uint64_t retval = 0; 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 = 0; + goto fail; } + retval = (uint64_t)(val * mul); + if (end) *end = endptr; - return value; +fail: + return retval; }