scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 1c715ade1420db7b2aea9beaca48eaf48ef6ea1f
parent 14293d43ec4d596e274d79f3ccc62860a40237c4
Author: Quentin Rameau <quinq@fifth.space>
Date:   Sun, 23 Dec 2018 17:49:05 +0100

[libc] strtoul[l]: return input string on parsing error

Setting errno to EINVAL is not specified in c99 (it is in POSIX though).

Diffstat:
Msrc/libc/stdlib/strtoul.c | 16+++++++---------
Msrc/libc/stdlib/strtoull.c | 16+++++++---------
2 files changed, 14 insertions(+), 18 deletions(-)

diff --git a/src/libc/stdlib/strtoul.c b/src/libc/stdlib/strtoul.c @@ -14,6 +14,9 @@ strtoul(const char *s, char **end, int base) static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const char *t, *p; + if (end) + *end = s; + while (isspace(*s)) ++s; @@ -25,14 +28,11 @@ strtoul(const char *s, char **end, int base) } if (base == 0) { - if (*s == '0' && toupper(s[1]) == 'X') - base = 16; - else if (*s == '0') - base = 8; + if (*s == '0') + base = toupper(s[1]) == 'X' ? 16 : 8; else base = 10; } - if (base == 16 && *s == '0' && toupper(s[1]) == 'X') s += 2; @@ -48,11 +48,9 @@ strtoul(const char *s, char **end, int base) n += d; } - - if (end) + if (end && t != s) *end = t; - if (n == 0 && s == t) - errno = EINVAL; + return n*sign; overflow: diff --git a/src/libc/stdlib/strtoull.c b/src/libc/stdlib/strtoull.c @@ -14,6 +14,9 @@ strtoull(const char *s, char **end, int base) static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const char *t, *p; + if (end) + *end = s; + while (isspace(*s)) ++s; @@ -25,14 +28,11 @@ strtoull(const char *s, char **end, int base) } if (base == 0) { - if (*s == '0' && toupper(s[1]) == 'X') - base = 16; - else if (*s == '0') - base = 8; + if (*s == '0') + base = toupper(s[1]) == 'X' ? 16 : 8; else base = 10; } - if (base == 16 && *s == '0' && toupper(s[1]) == 'X') s += 2; @@ -48,11 +48,9 @@ strtoull(const char *s, char **end, int base) n += d; } - - if (end) + if (end && t != s) *end = t; - if (n == 0 && s == t) - errno = EINVAL; + return n*sign; overflow: