summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--userland/libc/stdlib/strtol.c14
-rw-r--r--userland/libc/stdlib/strtoll.c14
-rw-r--r--userland/libc/stdlib/strtoul.c14
-rw-r--r--userland/test/test.c40
4 files changed, 71 insertions, 11 deletions
diff --git a/userland/libc/stdlib/strtol.c b/userland/libc/stdlib/strtol.c
index 30d04ec..4a1e189 100644
--- a/userland/libc/stdlib/strtol.c
+++ b/userland/libc/stdlib/strtol.c
@@ -28,8 +28,18 @@ long strtol(const char *str, char **restrict endptr, int base) {
}
if (0 == base) {
- // FIXME
- assert(0);
+ char prefix = *str;
+ if ('0' == prefix) {
+ str++;
+ if ('x' == tolower(*str)) {
+ str++;
+ base = 16;
+ } else {
+ base = 8;
+ }
+ } else {
+ base = 10;
+ }
}
if (2 <= base && 36 >= base) {
diff --git a/userland/libc/stdlib/strtoll.c b/userland/libc/stdlib/strtoll.c
index 486f91d..d1eacde 100644
--- a/userland/libc/stdlib/strtoll.c
+++ b/userland/libc/stdlib/strtoll.c
@@ -28,8 +28,18 @@ long long strtoll(const char *str, char **restrict endptr, int base) {
}
if (0 == base) {
- // FIXME
- assert(0);
+ char prefix = *str;
+ if ('0' == prefix) {
+ str++;
+ if ('x' == tolower(*str)) {
+ str++;
+ base = 16;
+ } else {
+ base = 8;
+ }
+ } else {
+ base = 10;
+ }
}
if (2 <= base && 36 >= base) {
diff --git a/userland/libc/stdlib/strtoul.c b/userland/libc/stdlib/strtoul.c
index c490aeb..f286a77 100644
--- a/userland/libc/stdlib/strtoul.c
+++ b/userland/libc/stdlib/strtoul.c
@@ -43,8 +43,18 @@ unsigned long strtoul(const char *restrict str, char **restrict endptr,
}
if (0 == base) {
- // FIXME
- assert(0);
+ char prefix = *str;
+ if ('0' == prefix) {
+ str++;
+ if ('x' == tolower(*str)) {
+ str++;
+ base = 16;
+ } else {
+ base = 8;
+ }
+ } else {
+ base = 10;
+ }
}
if (2 <= base && 36 >= base) {
diff --git a/userland/test/test.c b/userland/test/test.c
index 6df5a5a..8b3dd67 100644
--- a/userland/test/test.c
+++ b/userland/test/test.c
@@ -379,11 +379,41 @@ void strspn_test(void) {
void strtol_test(void) {
dbgln("strtol TEST");
{
- char *s = "1234";
- char *e;
- long r;
- assert(1234 == strtol(s, &e, 10));
- assert(e == (s + 4));
+ {
+ char *s = "1234";
+ char *e;
+ long r;
+ assert(1234 == strtol(s, &e, 10));
+ assert(e == (s + 4));
+ }
+ {
+ char *s = "1234";
+ char *e;
+ long r;
+ assert(1234 == strtol(s, &e, 0));
+ assert(e == (s + 4));
+ }
+ {
+ char *s = "0234";
+ char *e;
+ long r;
+ assert(156 == strtol(s, &e, 0));
+ assert(e == (s + 4));
+ }
+ {
+ char *s = "0x234";
+ char *e;
+ long r;
+ assert(564 == strtol(s, &e, 0));
+ assert(e == (s + 5));
+ }
+ {
+ char *s = "0X234";
+ char *e;
+ long r;
+ assert(564 == strtol(s, &e, 0));
+ assert(e == (s + 5));
+ }
}
dbgln("strtol TEST PASSED");
}