]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm tools: don't use custom strtoul for hex numbers
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Fri, 23 Sep 2011 14:53:44 +0000 (17:53 +0300)
committerPekka Enberg <penberg@kernel.org>
Sun, 25 Sep 2011 19:06:47 +0000 (22:06 +0300)
Cc: kvm@vger.kernel.org
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/util/parse-options.c

index c2803796ac27e9702867ba736d3bd10de93cf8ca..2665be5b64d3bf193dba0c2b20555b8a58f68252 100644 (file)
@@ -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,