summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/util.c59
1 files changed, 31 insertions, 28 deletions
diff --git a/common/util.c b/common/util.c
index 8005e0bfdb..f3d3fc3993 100644
--- a/common/util.c
+++ b/common/util.c
@@ -115,6 +115,18 @@ int atoi(const char *nptr)
return neg ? -result : result;
}
+static int find_base(int base, int *c, const char **nptr) {
+ if ((base == 0 || base == 16) && *c == '0'
+ && (**nptr == 'x' || **nptr == 'X')) {
+ *c = (*nptr)[1];
+ (*nptr) += 2;
+ base = 16;
+ } else if (base == 0) {
+ base = *c == '0' ? 8 : 10;
+ }
+ return base;
+}
+
/* Like strtol(), but for integers */
int strtoi(const char *nptr, char **endptr, int base)
@@ -123,24 +135,18 @@ int strtoi(const char *nptr, char **endptr, int base)
int neg = 0;
int c = '\0';
- if (endptr)
- *endptr = (char *)nptr;
-
while ((c = *nptr++) && isspace(c))
;
- if (c == '0' && *nptr == 'x') {
- base = 16;
- c = nptr[1];
- nptr += 2;
- } else if (base == 0) {
- base = 10;
- if (c == '-') {
- neg = 1;
- c = *nptr++;
- }
+ if (c == '+') {
+ c = *nptr++;
+ } else if (c == '-') {
+ neg = 1;
+ c = *nptr++;
}
+ base = find_base(base, &c, &nptr);
+
while (c) {
if (c >= '0' && c < '0' + MIN(base, 10))
result = result * base + (c - '0');
@@ -151,11 +157,11 @@ int strtoi(const char *nptr, char **endptr, int base)
else
break;
- if (endptr)
- *endptr = (char *)nptr;
c = *nptr++;
}
+ if (endptr)
+ *endptr = (char *)nptr - 1;
return neg ? -result : result;
}
@@ -164,21 +170,18 @@ uint64_t strtoul(const char *nptr, char **endptr, int base)
uint64_t result = 0;
int c = '\0';
- if (endptr)
- *endptr = (char *)nptr;
-
while ((c = *nptr++) && isspace(c))
;
- if (c == '0' && *nptr == 'x') {
- base = 16;
- c = nptr[1];
- nptr += 2;
- } else if (base == 0) {
- base = 10;
- if (c == '-')
- return result;
+ if (c == '+') {
+ c = *nptr++;
+ } else if (c == '-') {
+ if (endptr)
+ *endptr = (char *)nptr - 1;
+ return result;
}
+
+ base = find_base(base, &c, &nptr);
while (c) {
if (c >= '0' && c < '0' + MIN(base, 10))
@@ -190,11 +193,11 @@ uint64_t strtoul(const char *nptr, char **endptr, int base)
else
break;
- if (endptr)
- *endptr = (char *)nptr;
c = *nptr++;
}
+ if (endptr)
+ *endptr = (char *)nptr - 1;
return result;
}