From patchwork Thu Dec 9 12:13:33 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 74931 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 9D122B70B0 for ; Fri, 10 Dec 2010 03:23:45 +1100 (EST) Received: from localhost ([127.0.0.1]:36453 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQjHR-00058R-Lj for incoming@patchwork.ozlabs.org; Thu, 09 Dec 2010 11:23:41 -0500 Received: from [140.186.70.92] (port=40807 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQfNt-0004eQ-Cj for qemu-devel@nongnu.org; Thu, 09 Dec 2010 07:14:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PQfNU-0006KV-2A for qemu-devel@nongnu.org; Thu, 09 Dec 2010 07:14:03 -0500 Received: from mx1.redhat.com ([209.132.183.28]:17439) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PQfNT-0006K8-R2 for qemu-devel@nongnu.org; Thu, 09 Dec 2010 07:13:40 -0500 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 oB9CDc7Q021878 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 9 Dec 2010 07:13:38 -0500 Received: from red-feather.redhat.com (ovpn-113-53.phx2.redhat.com [10.3.113.53]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id oB9CDZtF026611; Thu, 9 Dec 2010 07:13:37 -0500 From: Jes.Sorensen@redhat.com To: kwolf@redhat.com Date: Thu, 9 Dec 2010 13:13:33 +0100 Message-Id: <1291896814-3554-2-git-send-email-Jes.Sorensen@redhat.com> In-Reply-To: <1291896814-3554-1-git-send-email-Jes.Sorensen@redhat.com> References: <1291896814-3554-1-git-send-email-Jes.Sorensen@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: qemu-devel@nongnu.org, armbru@redhat.com, stefanha@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH 1/2] Introduce strtosz_suffix() 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 This introduces strtosz_suffix() which allows the caller to specify a default suffix in case the non default of MB is wanted. strtosz() is kept as a wrapper for strtosz_suffix() which keeps it's current default of MB. Signed-off-by: Jes Sorensen --- cutils.c | 17 ++++++++++++++--- qemu-common.h | 7 +++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/cutils.c b/cutils.c index 28089aa..1d24d9a 100644 --- a/cutils.c +++ b/cutils.c @@ -291,10 +291,10 @@ int fcntl_setfl(int fd, int flag) * value must be terminated by whitespace, ',' or '\0'. Return -1 on * error. */ -ssize_t strtosz(const char *nptr, char **end) +ssize_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) { ssize_t retval = -1; - char *endptr, c; + char *endptr, c, d; int mul_required = 0; double val, mul, integral, fraction; @@ -313,10 +313,16 @@ ssize_t strtosz(const char *nptr, char **end) * part of a multi token argument. */ c = *endptr; + d = c; if (isspace(c) || c == '\0' || c == ',') { c = 0; + if (default_suffix) { + d = default_suffix; + } else { + d = c; + } } - switch (c) { + switch (d) { case 'B': case 'b': mul = 1; @@ -371,3 +377,8 @@ fail: return retval; } + +ssize_t strtosz(const char *nptr, char **end) +{ + return strtosz_suffix(nptr, end, 0); +} diff --git a/qemu-common.h b/qemu-common.h index de82c2e..dc44cd6 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -149,7 +149,14 @@ time_t mktimegm(struct tm *tm); int qemu_fls(int i); int qemu_fdatasync(int fd); int fcntl_setfl(int fd, int flag); + +#define STRTOSZ_DEFSUFFIX_TB 'T' +#define STRTOSZ_DEFSUFFIX_GB 'G' +#define STRTOSZ_DEFSUFFIX_MB 'M' +#define STRTOSZ_DEFSUFFIX_KB 'K' +#define STRTOSZ_DEFSUFFIX_B 'B' ssize_t strtosz(const char *nptr, char **end); +ssize_t strtosz_suffix(const char *nptr, char **end, const char); /* path.c */ void init_paths(const char *prefix);