[SeaBIOS] [PATCH 1/6] string: add strtol() implementation

Gerd Hoffmann posted 6 patches 9 weeks ago

[SeaBIOS] [PATCH 1/6] string: add strtol() implementation

Posted by Gerd Hoffmann 9 weeks ago
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 src/string.h |  1 +
 src/string.c | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/src/string.h b/src/string.h
index d069989db4b4..17ef3c6a1e89 100644
--- a/src/string.h
+++ b/src/string.h
@@ -27,5 +27,6 @@ void *memmove(void *d, const void *s, size_t len);
 char *strtcpy(char *dest, const char *src, size_t len);
 char *strchr(const char *s, int c);
 char *nullTrailingSpace(char *buf);
+u64 strtol(const char *ptr, int base);
 
 #endif // string.h
diff --git a/src/string.c b/src/string.c
index adb8198f84f4..ec8cf6108561 100644
--- a/src/string.c
+++ b/src/string.c
@@ -249,3 +249,28 @@ nullTrailingSpace(char *buf)
         buf++;
     return buf;
 }
+
+u64 strtol(const char *ptr, int base)
+{
+    u64 digit, val = 0;
+
+    for (;;) {
+        switch (*ptr) {
+        case '0' ... '9':
+            digit = *ptr - '0';
+            break;
+        case 'a' ... 'f':
+            digit = *ptr - 'a' + 10;
+            break;
+        case 'A' ... 'F':
+            digit = *ptr - 'A' + 10;
+            break;
+        default:
+            return val;
+        }
+        if (digit >= base)
+            return val;
+        val = val * base + digit;
+        ptr++;
+    }
+}
-- 
2.18.2
_______________________________________________
SeaBIOS mailing list -- seabios@seabios.org
To unsubscribe send an email to seabios-leave@seabios.org