mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: linux-kernel@vger.kernel.org
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Pekka Enberg <penberg@kernel.org>,
	kvm@vger.kernel.org
Subject: [RFC][PATCH] tools: kvm: don't use custom strtoul for hex numbers
Date: Fri, 23 Sep 2011 17:25:46 +0300	[thread overview]
Message-ID: <ae9ebfd216b4560d885e3b437d98b93b78e45c1c.1316787913.git.andriy.shevchenko@linux.intel.com> (raw)

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: kvm@vger.kernel.org
---
 tools/kvm/util/parse-options.c |   81 ++++++++--------------------------------
 1 files changed, 16 insertions(+), 65 deletions(-)

diff --git a/tools/kvm/util/parse-options.c b/tools/kvm/util/parse-options.c
index c280379..4f9fdff 100644
--- a/tools/kvm/util/parse-options.c
+++ b/tools/kvm/util/parse-options.c
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
@@ -39,74 +40,24 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
 	return 0;
 }
 
-#define numvalue(c)					\
-	((c) >= 'a' ? (c) - 'a' + 10 :			\
-	 (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
-
-static u64 readhex(const char *str, bool *error)
-{
-	char *pos = strchr(str, 'x') + 1;
-	u64 res = 0;
-
-	while (*pos) {
-		unsigned int v = numvalue(*pos);
-		if (v > 16) {
-			*error = true;
-			return 0;
-		}
-
-		res = (res * 16) + v;
-		pos++;
-	}
-
-	*error = false;
-	return res;
-}
-
 static int readnum(const struct option *opt, int flags,
 		   const char *str, char **end)
 {
-	if (strchr(str, 'x')) {
-		bool error;
-		u64 value;
-
-		value = readhex(str, &error);
-		if (error)
-			goto enotnum;
-
-		switch (opt->type) {
-		case OPTION_INTEGER:
-			*(int *)opt->value = value;
-			break;
-		case OPTION_UINTEGER:
-			*(unsigned int *)opt->value = value;
-			break;
-		case OPTION_LONG:
-			*(long *)opt->value = value;
-			break;
-		case OPTION_U64:
-			*(u64 *)opt->value = value;
-			break;
-		default:
-			goto invcall;
-		}
-	} else {
-		switch (opt->type) {
-		case OPTION_INTEGER:
-			*(int *)opt->value = strtol(str, end, 10);
-			break;
-		case OPTION_UINTEGER:
-			*(unsigned int *)opt->value = strtol(str, end, 10);
-			break;
-		case OPTION_LONG:
-			*(long *)opt->value = strtol(str, end, 10);
-			break;
-		case OPTION_U64:
-			*(u64 *)opt->value = strtoull(str, end, 10);
-			break;
-		default:
-			goto invcall;
-		}
+	switch (opt->type) {
+	case OPTION_INTEGER:
+		*(int *)opt->value = strtol(str, end, 0);
+		break;
+	case OPTION_UINTEGER:
+		*(unsigned int *)opt->value = strtol(str, end, 0);
+		break;
+	case OPTION_LONG:
+		*(long *)opt->value = strtol(str, end, 0);
+		break;
+	case OPTION_U64:
+		*(u64 *)opt->value = strtoull(str, end, 0);
+		break;
+	default:
+		goto invcall;
 	}
 
 	return 0;
-- 
1.7.6.3


             reply	other threads:[~2011-09-23 14:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-23 14:25 Andy Shevchenko [this message]
2011-09-23 14:53 ` [RFC][PATCHv2] " Andy Shevchenko
2011-09-25 19:08   ` Pekka Enberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ae9ebfd216b4560d885e3b437d98b93b78e45c1c.1316787913.git.andriy.shevchenko@linux.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=penberg@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®