This commit is contained in:
Julian Barathieu 2020-01-20 20:01:35 +01:00
parent 08d08539d8
commit 3fc80a9a28
1 changed files with 17 additions and 8 deletions

View File

@ -23,17 +23,21 @@
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include <libc.h> #include <libc.h>
#include <lib/buf.h>
long strtol(const char *str, char **endp, int base) { long strtol(const char *str, char **endp, int base) {
char c;
ulong n; ulong n;
bool neg = 0; bool neg = 0;
const char *save, *start = str; const char *save, *start = str;
assert(str != NULL);
char c = *str;
// Ignore leading spaces // Ignore leading spaces
do { while (isspace(c)) {
c = *str++; c = *str++;
} while (isspace(c)); }
// Accept any +/-'s, whatever the base // Accept any +/-'s, whatever the base
// In particular we accept things like "-0xF" // In particular we accept things like "-0xF"
@ -100,13 +104,19 @@ long strtol(const char *str, char **endp, int base) {
// Made from the code above by removing "neg" // Made from the code above by removing "neg"
// //
ulong strtoul(const char *str, char **endp, int base) { ulong strtoul(const char *str, char **endp, int base) {
char c;
ulong n; ulong n;
const char *save, *start = str; const char *save, *start = str;
do { assert(str != NULL);
char c = *str;
while (isspace(c)) {
c = *str++;
}
if (c == '+')
c = *str++; c = *str++;
} while (isspace(c));
if (c == '0') { if (c == '0') {
c = *str++; c = *str++;
@ -142,10 +152,9 @@ ulong strtoul(const char *str, char **endp, int base) {
n = (n * base) + c; n = (n * base) + c;
} }
} }
if (endp != NULL) if (endp != NULL)
*endp = (char *)(str == save ? start : str); *endp = (char *)(str == save ? start : str);
return n; return (long)n;
} }