atol and atoi get old-fashioned behaviour
This commit is contained in:
parent
24c9be9777
commit
ef69de1614
2 changed files with 38 additions and 15 deletions
|
@ -4,15 +4,27 @@
|
||||||
*/
|
*/
|
||||||
/* $Header$ */
|
/* $Header$ */
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
|
/* We do not use strtol here for backwards compatibility in behaviour on
|
||||||
|
overflow.
|
||||||
|
*/
|
||||||
int
|
int
|
||||||
atoi(const char *nptr)
|
atol(register const char *nptr)
|
||||||
{
|
{
|
||||||
int i, e = errno;
|
int total = 0;
|
||||||
|
register unsigned int digit;
|
||||||
|
int minus = 0;
|
||||||
|
|
||||||
i = (int)strtol(nptr, (char **)NULL, 10);
|
while (isspace(*nptr)) nptr++;
|
||||||
errno = e;
|
if (*nptr == '+') nptr++;
|
||||||
return i;
|
else if (*nptr == '-') {
|
||||||
|
minus = 1;
|
||||||
|
nptr++;
|
||||||
|
}
|
||||||
|
while (isdigit(*nptr)) {
|
||||||
|
total *= 10;
|
||||||
|
total += (*nptr++ - '0');
|
||||||
|
}
|
||||||
|
return minus ? -total : total;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,16 +4,27 @@
|
||||||
*/
|
*/
|
||||||
/* $Header$ */
|
/* $Header$ */
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
|
/* We do not use strtol here for backwards compatibility in behaviour on
|
||||||
|
overflow.
|
||||||
|
*/
|
||||||
long
|
long
|
||||||
atol(const char *nptr)
|
atol(register const char *nptr)
|
||||||
{
|
{
|
||||||
long l;
|
long total = 0;
|
||||||
int e = errno;
|
register unsigned int digit;
|
||||||
|
int minus = 0;
|
||||||
|
|
||||||
l = strtol(nptr, (char **)NULL, 10);
|
while (isspace(*nptr)) nptr++;
|
||||||
errno = e;
|
if (*nptr == '+') nptr++;
|
||||||
return l;
|
else if (*nptr == '-') {
|
||||||
|
minus = 1;
|
||||||
|
nptr++;
|
||||||
|
}
|
||||||
|
while (isdigit(*nptr)) {
|
||||||
|
total *= 10;
|
||||||
|
total += (*nptr++ - '0');
|
||||||
|
}
|
||||||
|
return minus ? -total : total;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue