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.c97
1 files changed, 59 insertions, 38 deletions
diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c
index 6f374abd5f..dc2cf8871c 100644
--- a/src/lib/libc/stdlib/strtol.c
+++ b/src/lib/libc/stdlib/strtol.c
@@ -1,3 +1,4 @@
1/* $OpenBSD: strtol.c,v 1.9 2013/04/17 17:40:35 tedu Exp $ */
1/*- 2/*-
2 * Copyright (c) 1990 The Regents of the University of California. 3 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved. 4 * All rights reserved.
@@ -10,11 +11,7 @@
10 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software 14 * 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 15 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission. 16 * without specific prior written permission.
20 * 17 *
@@ -31,14 +28,9 @@
31 * SUCH DAMAGE. 28 * SUCH DAMAGE.
32 */ 29 */
33 30
34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strtol.c 5.4 (Berkeley) 2/23/91";*/
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 */
38
39#include <limits.h>
40#include <ctype.h> 31#include <ctype.h>
41#include <errno.h> 32#include <errno.h>
33#include <limits.h>
42#include <stdlib.h> 34#include <stdlib.h>
43 35
44 36
@@ -49,30 +41,41 @@ static char *rcsid = "$Id: strtol.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"
49 * alphabets and digits are each contiguous. 41 * alphabets and digits are each contiguous.
50 */ 42 */
51long 43long
52strtol(nptr, endptr, base) 44strtol(const char *nptr, char **endptr, int base)
53 const char *nptr;
54 char **endptr;
55 register int base;
56{ 45{
57 register const char *s = nptr; 46 const char *s;
58 register unsigned long acc; 47 long acc, cutoff;
59 register int c; 48 int c;
60 register unsigned long cutoff; 49 int neg, any, cutlim;
61 register int neg = 0, any, cutlim; 50
51 /*
52 * Ensure that base is between 2 and 36 inclusive, or the special
53 * value of 0.
54 */
55 if (base != 0 && (base < 2 || base > 36)) {
56 if (endptr != 0)
57 *endptr = (char *)nptr;
58 errno = EINVAL;
59 return 0;
60 }
62 61
63 /* 62 /*
64 * Skip white space and pick up leading +/- sign if any. 63 * Skip white space and pick up leading +/- sign if any.
65 * If base is 0, allow 0x for hex and 0 for octal, else 64 * If base is 0, allow 0x for hex and 0 for octal, else
66 * assume decimal; if base is already 16, allow 0x. 65 * assume decimal; if base is already 16, allow 0x.
67 */ 66 */
67 s = nptr;
68 do { 68 do {
69 c = *s++; 69 c = (unsigned char) *s++;
70 } while (isspace(c)); 70 } while (isspace(c));
71 if (c == '-') { 71 if (c == '-') {
72 neg = 1; 72 neg = 1;
73 c = *s++; 73 c = *s++;
74 } else if (c == '+') 74 } else {
75 c = *s++; 75 neg = 0;
76 if (c == '+')
77 c = *s++;
78 }
76 if ((base == 0 || base == 16) && 79 if ((base == 0 || base == 16) &&
77 c == '0' && (*s == 'x' || *s == 'X')) { 80 c == '0' && (*s == 'x' || *s == 'X')) {
78 c = s[1]; 81 c = s[1];
@@ -99,10 +102,17 @@ strtol(nptr, endptr, base)
99 * Set any if any `digits' consumed; make it negative to indicate 102 * Set any if any `digits' consumed; make it negative to indicate
100 * overflow. 103 * overflow.
101 */ 104 */
102 cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 105 cutoff = neg ? LONG_MIN : LONG_MAX;
103 cutlim = cutoff % (unsigned long)base; 106 cutlim = cutoff % base;
104 cutoff /= (unsigned long)base; 107 cutoff /= base;
105 for (acc = 0, any = 0;; c = *s++) { 108 if (neg) {
109 if (cutlim > 0) {
110 cutlim -= base;
111 cutoff += 1;
112 }
113 cutlim = -cutlim;
114 }
115 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
106 if (isdigit(c)) 116 if (isdigit(c))
107 c -= '0'; 117 c -= '0';
108 else if (isalpha(c)) 118 else if (isalpha(c))
@@ -111,19 +121,30 @@ strtol(nptr, endptr, base)
111 break; 121 break;
112 if (c >= base) 122 if (c >= base)
113 break; 123 break;
114 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 124 if (any < 0)
115 any = -1; 125 continue;
116 else { 126 if (neg) {
117 any = 1; 127 if (acc < cutoff || (acc == cutoff && c > cutlim)) {
118 acc *= base; 128 any = -1;
119 acc += c; 129 acc = LONG_MIN;
130 errno = ERANGE;
131 } else {
132 any = 1;
133 acc *= base;
134 acc -= c;
135 }
136 } else {
137 if (acc > cutoff || (acc == cutoff && c > cutlim)) {
138 any = -1;
139 acc = LONG_MAX;
140 errno = ERANGE;
141 } else {
142 any = 1;
143 acc *= base;
144 acc += c;
145 }
120 } 146 }
121 } 147 }
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) 148 if (endptr != 0)
128 *endptr = (char *) (any ? s - 1 : nptr); 149 *endptr = (char *) (any ? s - 1 : nptr);
129 return (acc); 150 return (acc);