1989-05-16 13:13:53 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1989-05-16 13:13:53 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
1989-12-18 15:14:14 +00:00
|
|
|
|
|
|
|
static unsigned long
|
2018-06-21 20:33:47 +00:00
|
|
|
string2long(register const char* nptr, char** endptr,
|
|
|
|
int base, int is_signed);
|
1989-05-16 13:13:53 +00:00
|
|
|
|
|
|
|
long int
|
2018-06-21 20:33:47 +00:00
|
|
|
strtol(register const char* nptr, char** endptr, int base)
|
1989-12-18 15:14:14 +00:00
|
|
|
{
|
|
|
|
return (signed long)string2long(nptr, endptr, base, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long int
|
2018-06-21 20:33:47 +00:00
|
|
|
strtoul(register const char* nptr, char** endptr, int base)
|
1989-12-18 15:14:14 +00:00
|
|
|
{
|
|
|
|
return (unsigned long)string2long(nptr, endptr, base, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long
|
2018-06-21 20:33:47 +00:00
|
|
|
string2long(register const char* nptr, char** const endptr,
|
|
|
|
int base, int is_signed)
|
1989-05-16 13:13:53 +00:00
|
|
|
{
|
1995-08-17 10:03:43 +00:00
|
|
|
register unsigned int v;
|
1989-12-18 15:14:14 +00:00
|
|
|
register unsigned long val = 0;
|
1989-05-16 13:13:53 +00:00
|
|
|
register int c;
|
1989-12-18 15:14:14 +00:00
|
|
|
int ovfl = 0, sign = 1;
|
|
|
|
const char *startnptr = nptr, *nrstart;
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (endptr)
|
|
|
|
*endptr = (char*)nptr;
|
|
|
|
while (isspace(*nptr))
|
|
|
|
nptr++;
|
1989-12-18 15:14:14 +00:00
|
|
|
c = *nptr;
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (c == '-' || c == '+')
|
|
|
|
{
|
|
|
|
if (c == '-')
|
|
|
|
sign = -1;
|
1989-12-18 15:14:14 +00:00
|
|
|
nptr++;
|
1989-05-16 13:13:53 +00:00
|
|
|
}
|
2018-06-21 20:33:47 +00:00
|
|
|
nrstart = nptr; /* start of the number */
|
1989-12-18 15:14:14 +00:00
|
|
|
|
|
|
|
/* When base is 0, the syntax determines the actual base */
|
|
|
|
if (base == 0)
|
|
|
|
if (*nptr == '0')
|
2018-06-21 20:33:47 +00:00
|
|
|
if (*++nptr == 'x' || *nptr == 'X')
|
|
|
|
{
|
1989-12-18 15:14:14 +00:00
|
|
|
base = 16;
|
|
|
|
nptr++;
|
|
|
|
}
|
2018-06-21 20:33:47 +00:00
|
|
|
else
|
|
|
|
base = 8;
|
|
|
|
else
|
|
|
|
base = 10;
|
|
|
|
else if (base == 16 && *nptr == '0' && (*++nptr == 'x' || *nptr == 'X'))
|
1989-12-18 15:14:14 +00:00
|
|
|
nptr++;
|
1989-05-16 13:13:53 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
while (isdigit(c = *nptr) || isalpha(c))
|
|
|
|
{
|
|
|
|
if (!ovfl)
|
|
|
|
{
|
1989-12-18 15:14:14 +00:00
|
|
|
if (isalpha(c))
|
|
|
|
v = 10 + (isupper(c) ? c - 'A' : c - 'a');
|
|
|
|
else
|
|
|
|
v = c - '0';
|
2018-06-21 20:33:47 +00:00
|
|
|
if (v >= base)
|
|
|
|
break;
|
|
|
|
if (val > (ULONG_MAX - v) / base)
|
|
|
|
++ovfl;
|
|
|
|
else
|
|
|
|
val = (val * base) + v;
|
1989-05-16 13:13:53 +00:00
|
|
|
}
|
1989-12-18 15:14:14 +00:00
|
|
|
nptr++;
|
|
|
|
}
|
2018-06-21 20:33:47 +00:00
|
|
|
if (endptr)
|
|
|
|
{
|
|
|
|
if (nrstart == nptr)
|
|
|
|
*endptr = (char*)startnptr;
|
|
|
|
else
|
|
|
|
*endptr = (char*)nptr;
|
1989-05-16 13:13:53 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (!ovfl)
|
|
|
|
{
|
1990-03-28 16:37:18 +00:00
|
|
|
/* Overflow is only possible when converting a signed long.
|
1995-08-17 10:03:43 +00:00
|
|
|
* The "-(LONG_MIN+1)+(unsigned long) 1" construction is there
|
|
|
|
* to prevent overflow warnings on -LONG_MIN.
|
1990-03-28 16:37:18 +00:00
|
|
|
*/
|
|
|
|
if (is_signed
|
2018-06-21 20:33:47 +00:00
|
|
|
&& ((sign < 0 && val > -(LONG_MIN + 1) + (unsigned long)1)
|
|
|
|
|| (sign > 0 && val > LONG_MAX)))
|
|
|
|
ovfl++;
|
1989-12-18 15:14:14 +00:00
|
|
|
}
|
1989-05-16 13:13:53 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (ovfl)
|
|
|
|
{
|
1989-12-18 15:14:14 +00:00
|
|
|
errno = ERANGE;
|
|
|
|
if (is_signed)
|
2018-06-21 20:33:47 +00:00
|
|
|
if (sign < 0)
|
|
|
|
return LONG_MIN;
|
|
|
|
else
|
|
|
|
return LONG_MAX;
|
|
|
|
else
|
|
|
|
return ULONG_MAX;
|
1989-12-18 15:14:14 +00:00
|
|
|
}
|
1995-08-17 10:03:43 +00:00
|
|
|
return (sign * val);
|
1989-05-16 13:13:53 +00:00
|
|
|
}
|