diff options
author | deraadt <> | 1995-12-21 14:58:39 +0000 |
---|---|---|
committer | deraadt <> | 1995-12-21 14:58:39 +0000 |
commit | 0ec9503a81eeeb5a23e8654cc0e76fca89ab163b (patch) | |
tree | 5139e3085b6bfe3f9b0ebfb69133325f13456ead /src/lib/libc/stdlib/strtol.c | |
parent | 0fd8d706013540776674a583d2456224f1631724 (diff) | |
download | openbsd-0ec9503a81eeeb5a23e8654cc0e76fca89ab163b.tar.gz openbsd-0ec9503a81eeeb5a23e8654cc0e76fca89ab163b.tar.bz2 openbsd-0ec9503a81eeeb5a23e8654cc0e76fca89ab163b.zip |
from netbsd; Rearrange to avoid sign problems with GCC.
Diffstat (limited to 'src/lib/libc/stdlib/strtol.c')
-rw-r--r-- | src/lib/libc/stdlib/strtol.c | 65 |
1 files changed, 43 insertions, 22 deletions
diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c index 6f374abd5f..021fdc0d2a 100644 --- a/src/lib/libc/stdlib/strtol.c +++ b/src/lib/libc/stdlib/strtol.c | |||
@@ -33,12 +33,12 @@ | |||
33 | 33 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | 34 | #if defined(LIBC_SCCS) && !defined(lint) |
35 | /*static char *sccsid = "from: @(#)strtol.c 5.4 (Berkeley) 2/23/91";*/ | 35 | /*static char *sccsid = "from: @(#)strtol.c 5.4 (Berkeley) 2/23/91";*/ |
36 | static char *rcsid = "$Id: strtol.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | 36 | static char *rcsid = "$Id: strtol.c,v 1.2 1995/12/21 14:58:36 deraadt Exp $"; |
37 | #endif /* LIBC_SCCS and not lint */ | 37 | #endif /* LIBC_SCCS and not lint */ |
38 | 38 | ||
39 | #include <limits.h> | ||
40 | #include <ctype.h> | 39 | #include <ctype.h> |
41 | #include <errno.h> | 40 | #include <errno.h> |
41 | #include <limits.h> | ||
42 | #include <stdlib.h> | 42 | #include <stdlib.h> |
43 | 43 | ||
44 | 44 | ||
@@ -54,25 +54,28 @@ strtol(nptr, endptr, base) | |||
54 | char **endptr; | 54 | char **endptr; |
55 | register int base; | 55 | register int base; |
56 | { | 56 | { |
57 | register const char *s = nptr; | 57 | register const char *s; |
58 | register unsigned long acc; | 58 | register long acc, cutoff; |
59 | register int c; | 59 | register int c; |
60 | register unsigned long cutoff; | 60 | register int neg, any, cutlim; |
61 | register int neg = 0, any, cutlim; | ||
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 = *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,9 +102,16 @@ 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; |
108 | if (neg) { | ||
109 | if (cutlim > 0) { | ||
110 | cutlim -= base; | ||
111 | cutoff += 1; | ||
112 | } | ||
113 | cutlim = -cutlim; | ||
114 | } | ||
105 | for (acc = 0, any = 0;; c = *s++) { | 115 | for (acc = 0, any = 0;; c = *s++) { |
106 | if (isdigit(c)) | 116 | if (isdigit(c)) |
107 | c -= '0'; | 117 | c -= '0'; |
@@ -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); |