rockbox/firmware/libc/strtol.c
William Wilgus 8a773fb29f lua -- remove strtol and strtoul replace with rb->strtol and rb->strtoul
Change-Id: Ib7ba358b6488b946404c0c4cd8773e948810a7f7
2025-12-06 18:11:13 -05:00

32 lines
775 B
C

/* The following file is (with slight modifications for Rockbox) from dietlibc
* version 0.31 which is licensed under the GPL version 2: */
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <ctype.h>
#include <gcc_extensions.h>
#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(UNLIKELY(isspace(*nptr))) nptr++;
if (*nptr == '-' && isalnum(nptr[1])) { neg=-1; ++nptr; }
v=strtoul(nptr,endptr,base);
if (endptr && *endptr==nptr) *endptr=(char *)orig;
if (UNLIKELY(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);
}