2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
5 ANSI C function atol().
11 /*****************************************************************************
22 Convert a string of digits into an long integer.
25 str - The string which should be converted. Leading
26 whitespace are ignored. The number may be prefixed
30 The value of string str.
47 atof(), atoi(), strtod(), strtol(), strtoul()
51 ******************************************************************************/
53 unsigned long val
= 0;
60 while (isspace (*str
))
65 if (*str
== '+' || *str
== '-')
69 Conversion loop, from FreeBSD's src/lib/libc/stdlib/strtoul.c
71 The previous AROS loop was
72 a) inefficient - it did a division each time around.
73 b) buggy - it returned the wrong value in endptr on overflow.
75 cutoff
= (unsigned long)ULONG_MAX
/ (unsigned long)10;
76 cutlim
= (unsigned long)ULONG_MAX
% (unsigned long)10;
90 any < 0 when we have overflowed. We still need to find the
91 end of the subject sequence
93 if (any
< 0 || val
> cutoff
|| (val
== cutoff
&& digit
> cutlim
))
100 val
= (val
* 10) + digit
;