Set errno in strtol family of functions (#110)

This commit is contained in:
Alison Winters 2021-03-07 14:18:08 -08:00 committed by GitHub
parent b16b332539
commit f5da4efcaf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 13 deletions

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/errno.h"
#include "libc/limits.h"
/**
@ -25,5 +26,15 @@
* @param optional_base is recommended as 0 for flexidecimal
*/
long strtol(const char *s, char **opt_out_end, int optional_base) {
return strtoimax(s, opt_out_end, optional_base);
intmax_t res;
res = strtoimax(s, opt_out_end, optional_base);
if (res < LONG_MIN) {
errno = ERANGE;
return LONG_MIN;
}
if (res > LONG_MAX) {
errno = ERANGE;
return LONG_MAX;
}
return res;
}