From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=50720 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P605B-0006RN-Ut for qemu-devel@nongnu.org; Wed, 13 Oct 2010 08:05:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P5wYP-0000AI-C5 for qemu-devel@nongnu.org; Wed, 13 Oct 2010 04:19:18 -0400 Received: from mx1.redhat.com ([209.132.183.28]:32201) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P5wYP-0000A7-4k for qemu-devel@nongnu.org; Wed, 13 Oct 2010 04:19:17 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o9D8JFCP004039 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 13 Oct 2010 04:19:15 -0400 From: Markus Armbruster Subject: Re: [Qemu-devel] [PATCH 1/4] Introduce strtosz() library function to convert a string to a byte count. References: <1286954450-30860-1-git-send-email-Jes.Sorensen@redhat.com> <1286954450-30860-2-git-send-email-Jes.Sorensen@redhat.com> Date: Wed, 13 Oct 2010 10:19:12 +0200 In-Reply-To: <1286954450-30860-2-git-send-email-Jes.Sorensen@redhat.com> (Jes Sorensen's message of "Wed, 13 Oct 2010 09:20:47 +0200") Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Jes.Sorensen@redhat.com Cc: pbonzini@redhat.com, qemu-devel@nongnu.org Jes.Sorensen@redhat.com writes: > 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..44a862a 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); Redundant parenthesis. Not worth a respin. > + > + if (end) { > + *end = endptr; > + } Unlike strtol() & friends, we assign to *end only on success. > + > +fail: > + return retval; > +} [...] *Not* worth a respin.