strtol.c revision 1.1.1.1.8.1 1 1.1 mrg /*-
2 1.1 mrg * Copyright (c) 1990 The Regents of the University of California.
3 1.1 mrg * All rights reserved.
4 1.1 mrg *
5 1.1 mrg * Redistribution and use in source and binary forms, with or without
6 1.1 mrg * modification, are permitted provided that the following conditions
7 1.1 mrg * are met:
8 1.1 mrg * 1. Redistributions of source code must retain the above copyright
9 1.1 mrg * notice, this list of conditions and the following disclaimer.
10 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer in the
12 1.1 mrg * documentation and/or other materials provided with the distribution.
13 1.1 mrg * 3. [rescinded 22 July 1999]
14 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
15 1.1 mrg * may be used to endorse or promote products derived from this software
16 1.1 mrg * without specific prior written permission.
17 1.1 mrg *
18 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 mrg * SUCH DAMAGE.
29 1.1 mrg */
30 1.1 mrg
31 1.1 mrg /*
32 1.1 mrg
33 1.1.1.1.8.1 tls @deftypefn Supplemental {long int} strtol (const char *@var{string}, @
34 1.1.1.1.8.1 tls char **@var{endptr}, int @var{base})
35 1.1.1.1.8.1 tls @deftypefnx Supplemental {unsigned long int} strtoul (const char *@var{string}, @
36 1.1.1.1.8.1 tls char **@var{endptr}, int @var{base})
37 1.1 mrg
38 1.1 mrg The @code{strtol} function converts the string in @var{string} to a
39 1.1 mrg long integer value according to the given @var{base}, which must be
40 1.1 mrg between 2 and 36 inclusive, or be the special value 0. If @var{base}
41 1.1 mrg is 0, @code{strtol} will look for the prefixes @code{0} and @code{0x}
42 1.1 mrg to indicate bases 8 and 16, respectively, else default to base 10.
43 1.1 mrg When the base is 16 (either explicitly or implicitly), a prefix of
44 1.1 mrg @code{0x} is allowed. The handling of @var{endptr} is as that of
45 1.1 mrg @code{strtod} above. The @code{strtoul} function is the same, except
46 1.1 mrg that the converted value is unsigned.
47 1.1 mrg
48 1.1 mrg @end deftypefn
49 1.1 mrg
50 1.1 mrg */
51 1.1 mrg
52 1.1 mrg #ifdef HAVE_CONFIG_H
53 1.1 mrg #include "config.h"
54 1.1 mrg #endif
55 1.1 mrg #ifdef HAVE_LIMITS_H
56 1.1 mrg #include <limits.h>
57 1.1 mrg #endif
58 1.1 mrg #ifdef HAVE_SYS_PARAM_H
59 1.1 mrg #include <sys/param.h>
60 1.1 mrg #endif
61 1.1 mrg #include <errno.h>
62 1.1 mrg #ifdef NEED_DECLARATION_ERRNO
63 1.1 mrg extern int errno;
64 1.1 mrg #endif
65 1.1 mrg #include "safe-ctype.h"
66 1.1 mrg
67 1.1 mrg /* FIXME: It'd be nice to configure around these, but the include files are too
68 1.1 mrg painful. These macros should at least be more portable than hardwired hex
69 1.1 mrg constants. */
70 1.1 mrg
71 1.1 mrg #ifndef ULONG_MAX
72 1.1 mrg #define ULONG_MAX ((unsigned long)(~0L)) /* 0xFFFFFFFF */
73 1.1 mrg #endif
74 1.1 mrg
75 1.1 mrg #ifndef LONG_MAX
76 1.1 mrg #define LONG_MAX ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF */
77 1.1 mrg #endif
78 1.1 mrg
79 1.1 mrg #ifndef LONG_MIN
80 1.1 mrg #define LONG_MIN ((long)(~LONG_MAX)) /* 0x80000000 */
81 1.1 mrg #endif
82 1.1 mrg
83 1.1 mrg /*
84 1.1 mrg * Convert a string to a long integer.
85 1.1 mrg *
86 1.1 mrg * Ignores `locale' stuff. Assumes that the upper and lower case
87 1.1 mrg * alphabets and digits are each contiguous.
88 1.1 mrg */
89 1.1 mrg long
90 1.1 mrg strtol(const char *nptr, char **endptr, register int base)
91 1.1 mrg {
92 1.1 mrg register const char *s = nptr;
93 1.1 mrg register unsigned long acc;
94 1.1 mrg register int c;
95 1.1 mrg register unsigned long cutoff;
96 1.1 mrg register int neg = 0, any, cutlim;
97 1.1 mrg
98 1.1 mrg /*
99 1.1 mrg * Skip white space and pick up leading +/- sign if any.
100 1.1 mrg * If base is 0, allow 0x for hex and 0 for octal, else
101 1.1 mrg * assume decimal; if base is already 16, allow 0x.
102 1.1 mrg */
103 1.1 mrg do {
104 1.1 mrg c = *s++;
105 1.1 mrg } while (ISSPACE(c));
106 1.1 mrg if (c == '-') {
107 1.1 mrg neg = 1;
108 1.1 mrg c = *s++;
109 1.1 mrg } else if (c == '+')
110 1.1 mrg c = *s++;
111 1.1 mrg if ((base == 0 || base == 16) &&
112 1.1 mrg c == '0' && (*s == 'x' || *s == 'X')) {
113 1.1 mrg c = s[1];
114 1.1 mrg s += 2;
115 1.1 mrg base = 16;
116 1.1 mrg }
117 1.1 mrg if (base == 0)
118 1.1 mrg base = c == '0' ? 8 : 10;
119 1.1 mrg
120 1.1 mrg /*
121 1.1 mrg * Compute the cutoff value between legal numbers and illegal
122 1.1 mrg * numbers. That is the largest legal value, divided by the
123 1.1 mrg * base. An input number that is greater than this value, if
124 1.1 mrg * followed by a legal input character, is too big. One that
125 1.1 mrg * is equal to this value may be valid or not; the limit
126 1.1 mrg * between valid and invalid numbers is then based on the last
127 1.1 mrg * digit. For instance, if the range for longs is
128 1.1 mrg * [-2147483648..2147483647] and the input base is 10,
129 1.1 mrg * cutoff will be set to 214748364 and cutlim to either
130 1.1 mrg * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
131 1.1 mrg * a value > 214748364, or equal but the next digit is > 7 (or 8),
132 1.1 mrg * the number is too big, and we will return a range error.
133 1.1 mrg *
134 1.1 mrg * Set any if any `digits' consumed; make it negative to indicate
135 1.1 mrg * overflow.
136 1.1 mrg */
137 1.1 mrg cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
138 1.1 mrg cutlim = cutoff % (unsigned long)base;
139 1.1 mrg cutoff /= (unsigned long)base;
140 1.1 mrg for (acc = 0, any = 0;; c = *s++) {
141 1.1 mrg if (ISDIGIT(c))
142 1.1 mrg c -= '0';
143 1.1 mrg else if (ISALPHA(c))
144 1.1 mrg c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
145 1.1 mrg else
146 1.1 mrg break;
147 1.1 mrg if (c >= base)
148 1.1 mrg break;
149 1.1 mrg if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
150 1.1 mrg any = -1;
151 1.1 mrg else {
152 1.1 mrg any = 1;
153 1.1 mrg acc *= base;
154 1.1 mrg acc += c;
155 1.1 mrg }
156 1.1 mrg }
157 1.1 mrg if (any < 0) {
158 1.1 mrg acc = neg ? LONG_MIN : LONG_MAX;
159 1.1 mrg errno = ERANGE;
160 1.1 mrg } else if (neg)
161 1.1 mrg acc = -acc;
162 1.1 mrg if (endptr != 0)
163 1.1 mrg *endptr = (char *) (any ? s - 1 : nptr);
164 1.1 mrg return (acc);
165 1.1 mrg }
166