All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC][PATCH] tools: kvm: don't use custom strtoul for hex numbers
@ 2011-09-23 14:25 Andy Shevchenko
  2011-09-23 14:53 ` [RFC][PATCHv2] " Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2011-09-23 14:25 UTC (permalink / raw)
  To: linux-kernel; +Cc: Andy Shevchenko, Pekka Enberg, kvm

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [RFC][PATCHv2] tools: kvm: don't use custom strtoul for hex numbers
  2011-09-23 14:25 [RFC][PATCH] tools: kvm: don't use custom strtoul for hex numbers Andy Shevchenko
@ 2011-09-23 14:53 ` Andy Shevchenko
  2011-09-25 19:08   ` Pekka Enberg
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2011-09-23 14:53 UTC (permalink / raw)
  To: linux-kernel; +Cc: Andy Shevchenko, Pekka Enberg, kvm

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

diff --git a/tools/kvm/util/parse-options.c b/tools/kvm/util/parse-options.c
index c280379..2665be5 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,82 +40,27 @@ 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:
+		return opterror(opt, "invalid numeric conversion", flags);
 	}
 
 	return 0;
-
-enotnum:
-	return opterror(opt, "expects a numerical value", flags);
-invcall:
-	return opterror(opt, "invalid numeric conversion", flags);
 }
 
 static int get_value(struct parse_opt_ctx_t *p,
-- 
1.7.6.3


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [RFC][PATCHv2] tools: kvm: don't use custom strtoul for hex numbers
  2011-09-23 14:53 ` [RFC][PATCHv2] " Andy Shevchenko
@ 2011-09-25 19:08   ` Pekka Enberg
  0 siblings, 0 replies; 3+ messages in thread
From: Pekka Enberg @ 2011-09-25 19:08 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-kernel, KVM General, Cyrill Gorcunov, Sasha Levin

On Fri, Sep 23, 2011 at 5:53 PM, Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Cc: Pekka Enberg <penberg@kernel.org>
> Cc: kvm@vger.kernel.org
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Applied, thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-09-25 19:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-23 14:25 [RFC][PATCH] tools: kvm: don't use custom strtoul for hex numbers Andy Shevchenko
2011-09-23 14:53 ` [RFC][PATCHv2] " Andy Shevchenko
2011-09-25 19:08   ` Pekka Enberg

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.