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.c83
1 files changed, 48 insertions, 35 deletions
diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c
index 6f374abd5f..9692bb6b07 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.6 2005/03/30 18:51:49 pat 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
@@ -49,30 +44,30 @@ 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. 44 * alphabets and digits are each contiguous.
50 */ 45 */
51long 46long
52strtol(nptr, endptr, base) 47strtol(const char *nptr, char **endptr, int base)
53 const char *nptr;
54 char **endptr;
55 register int base;
56{ 48{
57 register const char *s = nptr; 49 const char *s;
58 register unsigned long acc; 50 long acc, cutoff;
59 register int c; 51 int c;
60 register unsigned long cutoff; 52 int neg, any, cutlim;
61 register int neg = 0, any, cutlim;
62 53
63 /* 54 /*
64 * Skip white space and pick up leading +/- sign if any. 55 * Skip white space and pick up leading +/- sign if any.
65 * If base is 0, allow 0x for hex and 0 for octal, else 56 * If base is 0, allow 0x for hex and 0 for octal, else
66 * assume decimal; if base is already 16, allow 0x. 57 * assume decimal; if base is already 16, allow 0x.
67 */ 58 */
59 s = nptr;
68 do { 60 do {
69 c = *s++; 61 c = (unsigned char) *s++;
70 } while (isspace(c)); 62 } while (isspace(c));
71 if (c == '-') { 63 if (c == '-') {
72 neg = 1; 64 neg = 1;
73 c = *s++; 65 c = *s++;
74 } else if (c == '+') 66 } else {
75 c = *s++; 67 neg = 0;
68 if (c == '+')
69 c = *s++;
70 }
76 if ((base == 0 || base == 16) && 71 if ((base == 0 || base == 16) &&
77 c == '0' && (*s == 'x' || *s == 'X')) { 72 c == '0' && (*s == 'x' || *s == 'X')) {
78 c = s[1]; 73 c = s[1];
@@ -99,10 +94,17 @@ strtol(nptr, endptr, base)
99 * Set any if any `digits' consumed; make it negative to indicate 94 * Set any if any `digits' consumed; make it negative to indicate
100 * overflow. 95 * overflow.
101 */ 96 */
102 cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 97 cutoff = neg ? LONG_MIN : LONG_MAX;
103 cutlim = cutoff % (unsigned long)base; 98 cutlim = cutoff % base;
104 cutoff /= (unsigned long)base; 99 cutoff /= base;
105 for (acc = 0, any = 0;; c = *s++) { 100 if (neg) {
101 if (cutlim > 0) {
102 cutlim -= base;
103 cutoff += 1;
104 }
105 cutlim = -cutlim;
106 }
107 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
106 if (isdigit(c)) 108 if (isdigit(c))
107 c -= '0'; 109 c -= '0';
108 else if (isalpha(c)) 110 else if (isalpha(c))
@@ -111,19 +113,30 @@ strtol(nptr, endptr, base)
111 break; 113 break;
112 if (c >= base) 114 if (c >= base)
113 break; 115 break;
114 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 116 if (any < 0)
115 any = -1; 117 continue;
116 else { 118 if (neg) {
117 any = 1; 119 if (acc < cutoff || acc == cutoff && c > cutlim) {
118 acc *= base; 120 any = -1;
119 acc += c; 121 acc = LONG_MIN;
122 errno = ERANGE;
123 } else {
124 any = 1;
125 acc *= base;
126 acc -= c;
127 }
128 } else {
129 if (acc > cutoff || acc == cutoff && c > cutlim) {
130 any = -1;
131 acc = LONG_MAX;
132 errno = ERANGE;
133 } else {
134 any = 1;
135 acc *= base;
136 acc += c;
137 }
120 } 138 }
121 } 139 }
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) 140 if (endptr != 0)
128 *endptr = (char *) (any ? s - 1 : nptr); 141 *endptr = (char *) (any ? s - 1 : nptr);
129 return (acc); 142 return (acc);