From patchwork Wed Oct 13 08:48:47 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 67681 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 43831B70AA for ; Wed, 13 Oct 2010 23:52:23 +1100 (EST) Received: from localhost ([127.0.0.1]:56007 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P60oe-0003Gm-Jd for incoming@patchwork.ozlabs.org; Wed, 13 Oct 2010 08:52:20 -0400 Received: from [140.186.70.92] (port=35340 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P602B-0006xZ-WC for qemu-devel@nongnu.org; Wed, 13 Oct 2010 08:02:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P5x18-0004vx-EG for qemu-devel@nongnu.org; Wed, 13 Oct 2010 04:48:59 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38156) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P5x18-0004vq-6P for qemu-devel@nongnu.org; Wed, 13 Oct 2010 04:48:58 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o9D8mtuS018295 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 13 Oct 2010 04:48:55 -0400 Received: from localhost6.localdomain6 (ovpn-113-72.phx2.redhat.com [10.3.113.72]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id o9D8mqsN023950; Wed, 13 Oct 2010 04:48:54 -0400 From: Jes.Sorensen@redhat.com To: qemu-devel@nongnu.org Date: Wed, 13 Oct 2010 10:48:47 +0200 Message-Id: <1286959730-924-2-git-send-email-Jes.Sorensen@redhat.com> In-Reply-To: <1286959730-924-1-git-send-email-Jes.Sorensen@redhat.com> References: <1286959730-924-1-git-send-email-Jes.Sorensen@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 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 1/4] Introduce strtosz() library function to convert a string to a byte count. 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 strtosz() returns -1 on error. It now supports human unit formats in eg. 1.0G, with better error handling. The following suffixes are supported: B/b = bytes K/k = KB M/m = MB G/g = GB T/t = TB This patch changes -numa and -m input to use strtosz(). Signed-off-by: Jes Sorensen --- cutils.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-common.h | 1 + vl.c | 31 +++++++--------------- 3 files changed, 90 insertions(+), 21 deletions(-) diff --git a/cutils.c b/cutils.c index 5883737..4bb459d 100644 --- a/cutils.c +++ b/cutils.c @@ -23,6 +23,7 @@ */ #include "qemu-common.h" #include "host-utils.h" +#include void pstrcpy(char *buf, int buf_size, const char *str) { @@ -283,3 +284,81 @@ int fcntl_setfl(int fd, int flag) } #endif +/* + * Convert string to bytes, allowing either B/b for bytes, K/k for KB, + * M/m for MB, G/g for GB or T/t for TB. Default without any postfix + * is MB. End pointer will be returned in *end, if not NULL. + * Return -1 on error. + */ +ssize_t strtosz(const char *nptr, char **end) +{ + ssize_t retval = -1; + char *endptr, c; + int mul_required = 0; + double val, mul, integral, fraction; + + errno = 0; + val = strtod(nptr, &endptr); + if (isnan(val) || endptr == nptr || errno != 0 || val < 0 || + val >= HUGE_VAL) { + goto fail; + } + integral = modf(val, &fraction); + if (integral != 0) { + mul_required = 1; + } + /* + * Any whitespace character is fine for terminating the number, + * in addition we accept ',' to handle strings where the size is + * part of a multi token argument. Otherwise check that the suffix + * is just one character. + */ + c = *endptr++; + if (isspace(c) || c == '\0' || c == ',') { + c = 0; + } else if (!isspace(*endptr) && *endptr != 0) { + goto fail; + } + switch (c) { + case 'B': + case 'b': + mul = 1; + if (mul_required) { + goto fail; + } + break; + case 'K': + case 'k': + mul = 1 << 10; + break; + case 0: + if (mul_required) { + goto fail; + } + case 'M': + case 'm': + mul = 1ULL << 20; + break; + case 'G': + case 'g': + mul = 1ULL << 30; + break; + case 'T': + case 't': + mul = 1ULL << 40; + break; + default: + goto fail; + } + if (val * mul >= ~(size_t)0) { + goto fail; + } + retval = (val * mul); + +fail: + if (end) { + *end = endptr; + } + + return retval; +} diff --git a/qemu-common.h b/qemu-common.h index 81aafa0..0a062d4 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -153,6 +153,7 @@ time_t mktimegm(struct tm *tm); int qemu_fls(int i); int qemu_fdatasync(int fd); int fcntl_setfl(int fd, int flag); +ssize_t strtosz(const char *nptr, char **end); /* path.c */ void init_paths(const char *prefix); diff --git a/vl.c b/vl.c index df414ef..6043fa2 100644 --- a/vl.c +++ b/vl.c @@ -734,16 +734,13 @@ static void numa_add(const char *optarg) if (get_param_value(option, 128, "mem", optarg) == 0) { node_mem[nodenr] = 0; } else { - value = strtoull(option, &endptr, 0); - switch (*endptr) { - case 0: case 'M': case 'm': - value <<= 20; - break; - case 'G': case 'g': - value <<= 30; - break; + ssize_t sval; + sval = strtosz(option, NULL); + if (sval < 0) { + fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg); + exit(1); } - node_mem[nodenr] = value; + node_mem[nodenr] = sval; } if (get_param_value(option, 128, "cpus", optarg) == 0) { node_cpumask[nodenr] = 0; @@ -2163,18 +2160,10 @@ int main(int argc, char **argv, char **envp) exit(0); break; case QEMU_OPTION_m: { - uint64_t value; - char *ptr; + ssize_t value; - value = strtoul(optarg, &ptr, 10); - switch (*ptr) { - case 0: case 'M': case 'm': - value <<= 20; - break; - case 'G': case 'g': - value <<= 30; - break; - default: + value = strtosz(optarg, NULL); + if (value < 0) { fprintf(stderr, "qemu: invalid ram size: %s\n", optarg); exit(1); }