rockbox/apps/plugins/lua/strtol.c
William Wilgus c251d1879f lua fix mem_read_write, strtol
back when I wrote this I was running the sim on a 32 bit machine
I didn't catch the hardcoded LONG_MAX reference or the fact that
lua_tointeger maxes ot at 32 bits

on 64 bit machines strtol caused all kinds of issues especially since
it returned the real LONG_MIN/MAX values

Change-Id: I3571ebbd9df333f7cbf4077562412c27429bfadc
2019-08-28 00:56:08 -05:00

27 lines
607 B
C

#include "rocklibc.h"
extern unsigned long int strtoul(const char *ptr, char **endptr, int base);
#define ABS_LONG_MIN LONG_MAX
long int strtol(const char *nptr, char **endptr, int base)
{
int neg=0;
unsigned long int v;
const char*orig=nptr;
while(isspace(*nptr)) nptr++;
if (*nptr == '-' && isalnum(nptr[1])) { neg=-1; ++nptr; }
v=strtoul(nptr,endptr,base);
if (endptr && *endptr==nptr) *endptr=(char *)orig;
if (v>=ABS_LONG_MIN) {
if (v==ABS_LONG_MIN && neg) {
errno=0;
return v;
}
errno=ERANGE;
return (neg?LONG_MIN:LONG_MAX);
}
return (neg?-v:v);
}