x11gfx.hidd: support 32 bit modes
[AROS.git] / compiler / stdc / strtol.c
blobe07adb08e7165029606512e331680b53d6d85024
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function strtol().
6 */
8 #include <ctype.h>
9 #include <errno.h>
10 #ifndef AROS_NO_LIMITS_H
11 # include <limits.h>
12 #else
13 # define LONG_MAX 2147483647L
14 # define LONG_MIN (-LONG_MAX - 1L)
15 #endif
17 /*****************************************************************************
19 NAME */
20 #include <stdlib.h>
22 long strtol (
24 /* SYNOPSIS */
25 const char * str,
26 char ** endptr,
27 int base)
29 /* FUNCTION
30 Convert a string of digits into an integer according to the
31 given base.
33 INPUTS
34 str - The string which should be converted. Leading
35 whitespace are ignored. The number may be prefixed
36 by a '+' or '-'. If base is above 10, then the
37 alphabetic characters from 'A' are used to specify
38 digits above 9 (ie. 'A' or 'a' is 10, 'B' or 'b' is
39 11 and so on until 'Z' or 'z' is 35).
40 endptr - If this is non-NULL, then the address of the first
41 character after the number in the string is stored
42 here.
43 base - The base for the number. May be 0 or between 2 and 36,
44 including both. 0 means to autodetect the base. strtoul()
45 selects the base by inspecting the first characters
46 of the string. If they are "0x", then base 16 is
47 assumed. If they are "0", then base 8 is assumed. Any
48 other digit will assume base 10. This is like in C.
50 If you give base 16, then an optional "0x" may
51 precede the number in the string.
53 RESULT
54 The value of the string. The first character after the number
55 is returned in *endptr, if endptr is non-NULL. If no digits can
56 be converted, *endptr contains str (if non-NULL) and 0 is
57 returned.
59 NOTES
61 EXAMPLE
62 // returns 1, ptr points to the 0-Byte
63 strol (" \t +0x1", &ptr, 0);
65 // Returns 15. ptr points to the a
66 strol ("017a", &ptr, 0);
68 // Returns 215 (5*36 + 35)
69 strol ("5z", &ptr, 36);
71 BUGS
73 SEE ALSO
74 atof(), atoi(), atol(), strtod(), strtoul()
76 INTERNALS
78 ******************************************************************************/
80 long val = 0;
81 char * ptr;
82 char * copy;
84 while (isspace (*str))
85 str ++;
87 copy = (char *)str;
89 if (*str)
91 val = strtoul (str, &ptr, base);
93 if (endptr)
95 if (ptr == str)
96 str = copy;
97 else
98 str = ptr;
101 /* Remember: strtoul() has already done the sign conversion */
102 if (*copy == '-')
104 if ((signed long)val > 0)
106 #ifndef STDC_STATIC
107 errno = ERANGE;
108 #endif
109 val = LONG_MIN;
112 else
114 if ((signed long)val < 0)
116 #ifndef STDC_STATIC
117 errno = ERANGE;
118 #endif
119 val = LONG_MAX;
124 if (endptr)
125 *endptr = (char *)str;
127 return val;
128 } /* strtol */