summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/strtol.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/strtol.c')
-rw-r--r--src/lib/libc/stdlib/strtol.c76
1 files changed, 46 insertions, 30 deletions
diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c
index 6f374abd5f..1c39720794 100644
--- a/src/lib/libc/stdlib/strtol.c
+++ b/src/lib/libc/stdlib/strtol.c
@@ -10,11 +10,7 @@
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software 13 * 3. Neither the name of the University nor the names of its contributors
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software 14 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission. 15 * without specific prior written permission.
20 * 16 *
@@ -32,13 +28,12 @@
32 */ 28 */
33 29
34#if defined(LIBC_SCCS) && !defined(lint) 30#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strtol.c 5.4 (Berkeley) 2/23/91";*/ 31static char *rcsid = "$OpenBSD: strtol.c,v 1.5 2003/06/02 20:18:38 millert Exp $";
36static char *rcsid = "$Id: strtol.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 32#endif /* LIBC_SCCS and not lint */
38 33
39#include <limits.h>
40#include <ctype.h> 34#include <ctype.h>
41#include <errno.h> 35#include <errno.h>
36#include <limits.h>
42#include <stdlib.h> 37#include <stdlib.h>
43 38
44 39
@@ -54,25 +49,28 @@ strtol(nptr, endptr, base)
54 char **endptr; 49 char **endptr;
55 register int base; 50 register int base;
56{ 51{
57 register const char *s = nptr; 52 register const char *s;
58 register unsigned long acc; 53 register long acc, cutoff;
59 register int c; 54 register int c;
60 register unsigned long cutoff; 55 register int neg, any, cutlim;
61 register int neg = 0, any, cutlim;
62 56
63 /* 57 /*
64 * Skip white space and pick up leading +/- sign if any. 58 * Skip white space and pick up leading +/- sign if any.
65 * If base is 0, allow 0x for hex and 0 for octal, else 59 * If base is 0, allow 0x for hex and 0 for octal, else
66 * assume decimal; if base is already 16, allow 0x. 60 * assume decimal; if base is already 16, allow 0x.
67 */ 61 */
62 s = nptr;
68 do { 63 do {
69 c = *s++; 64 c = (unsigned char) *s++;
70 } while (isspace(c)); 65 } while (isspace(c));
71 if (c == '-') { 66 if (c == '-') {
72 neg = 1; 67 neg = 1;
73 c = *s++; 68 c = *s++;
74 } else if (c == '+') 69 } else {
75 c = *s++; 70 neg = 0;
71 if (c == '+')
72 c = *s++;
73 }
76 if ((base == 0 || base == 16) && 74 if ((base == 0 || base == 16) &&
77 c == '0' && (*s == 'x' || *s == 'X')) { 75 c == '0' && (*s == 'x' || *s == 'X')) {
78 c = s[1]; 76 c = s[1];
@@ -99,10 +97,17 @@ strtol(nptr, endptr, base)
99 * Set any if any `digits' consumed; make it negative to indicate 97 * Set any if any `digits' consumed; make it negative to indicate
100 * overflow. 98 * overflow.
101 */ 99 */
102 cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 100 cutoff = neg ? LONG_MIN : LONG_MAX;
103 cutlim = cutoff % (unsigned long)base; 101 cutlim = cutoff % base;
104 cutoff /= (unsigned long)base; 102 cutoff /= base;
105 for (acc = 0, any = 0;; c = *s++) { 103 if (neg) {
104 if (cutlim > 0) {
105 cutlim -= base;
106 cutoff += 1;
107 }
108 cutlim = -cutlim;
109 }
110 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
106 if (isdigit(c)) 111 if (isdigit(c))
107 c -= '0'; 112 c -= '0';
108 else if (isalpha(c)) 113 else if (isalpha(c))
@@ -111,19 +116,30 @@ strtol(nptr, endptr, base)
111 break; 116 break;
112 if (c >= base) 117 if (c >= base)
113 break; 118 break;
114 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 119 if (any < 0)
115 any = -1; 120 continue;
116 else { 121 if (neg) {
117 any = 1; 122 if (acc < cutoff || acc == cutoff && c > cutlim) {
118 acc *= base; 123 any = -1;
119 acc += c; 124 acc = LONG_MIN;
125 errno = ERANGE;
126 } else {
127 any = 1;
128 acc *= base;
129 acc -= c;
130 }
131 } else {
132 if (acc > cutoff || acc == cutoff && c > cutlim) {
133 any = -1;
134 acc = LONG_MAX;
135 errno = ERANGE;
136 } else {
137 any = 1;
138 acc *= base;
139 acc += c;
140 }
120 } 141 }
121 } 142 }
122 if (any < 0) {
123 acc = neg ? LONG_MIN : LONG_MAX;
124 errno = ERANGE;
125 } else if (neg)
126 acc = -acc;
127 if (endptr != 0) 143 if (endptr != 0)
128 *endptr = (char *) (any ? s - 1 : nptr); 144 *endptr = (char *) (any ? s - 1 : nptr);
129 return (acc); 145 return (acc);