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