2009-05-21 19:01:41 +00:00
|
|
|
#include "rocklibc.h"
|
|
|
|
|
|
|
|
extern unsigned long int strtoul(const char *ptr, char **endptr, int base);
|
|
|
|
|
2019-08-28 05:56:08 +00:00
|
|
|
#define ABS_LONG_MIN LONG_MAX
|
2009-05-21 19:01:41 +00:00
|
|
|
long int strtol(const char *nptr, char **endptr, int base)
|
|
|
|
{
|
|
|
|
int neg=0;
|
|
|
|
unsigned long int v;
|
|
|
|
const char*orig=nptr;
|
|
|
|
|
2020-10-04 02:45:27 +00:00
|
|
|
while(__unlikely(isspace(*nptr))) nptr++;
|
2009-05-21 19:01:41 +00:00
|
|
|
|
|
|
|
if (*nptr == '-' && isalnum(nptr[1])) { neg=-1; ++nptr; }
|
|
|
|
v=strtoul(nptr,endptr,base);
|
|
|
|
if (endptr && *endptr==nptr) *endptr=(char *)orig;
|
2020-10-04 02:45:27 +00:00
|
|
|
if (__unlikely(v>=ABS_LONG_MIN)) {
|
2009-05-21 19:01:41 +00:00
|
|
|
if (v==ABS_LONG_MIN && neg) {
|
|
|
|
errno=0;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
errno=ERANGE;
|
|
|
|
return (neg?LONG_MIN:LONG_MAX);
|
|
|
|
}
|
|
|
|
return (neg?-v:v);
|
|
|
|
}
|
|
|
|
|