This commit is contained in:
parent
08d08539d8
commit
3fc80a9a28
|
@ -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;
|
||||||
|
|
||||||
|
assert(str != NULL);
|
||||||
|
|
||||||
do {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue