diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libc/stdlib/strtoll.c (renamed from src/lib/libc/stdlib/strtoq.c) | 97 |
1 files changed, 57 insertions, 40 deletions
diff --git a/src/lib/libc/stdlib/strtoq.c b/src/lib/libc/stdlib/strtoll.c index fc559e9d7f..37379252e3 100644 --- a/src/lib/libc/stdlib/strtoq.c +++ b/src/lib/libc/stdlib/strtoll.c | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | /* $OpenBSD: strtoll.c,v 1.6 2005/11/10 10:00:17 espie Exp $ */ | ||
| 1 | /*- | 2 | /*- |
| 2 | * Copyright (c) 1992 The Regents of the University of California. | 3 | * Copyright (c) 1992 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,34 +28,26 @@ | |||
| 31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
| 32 | */ | 29 | */ |
| 33 | 30 | ||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | static char sccsid[] = "@(#)strtoq.c 5.1 (Berkeley) 6/26/92"; | ||
| 36 | #endif /* LIBC_SCCS and not lint */ | ||
| 37 | |||
| 38 | #include <sys/types.h> | 31 | #include <sys/types.h> |
| 39 | 32 | ||
| 40 | #include <limits.h> | ||
| 41 | #include <errno.h> | ||
| 42 | #include <ctype.h> | 33 | #include <ctype.h> |
| 34 | #include <errno.h> | ||
| 35 | #include <limits.h> | ||
| 43 | #include <stdlib.h> | 36 | #include <stdlib.h> |
| 44 | 37 | ||
| 45 | /* | 38 | /* |
| 46 | * Convert a string to a quad integer. | 39 | * Convert a string to a long long. |
| 47 | * | 40 | * |
| 48 | * Ignores `locale' stuff. Assumes that the upper and lower case | 41 | * Ignores `locale' stuff. Assumes that the upper and lower case |
| 49 | * alphabets and digits are each contiguous. | 42 | * alphabets and digits are each contiguous. |
| 50 | */ | 43 | */ |
| 51 | quad_t | 44 | long long |
| 52 | strtoq(nptr, endptr, base) | 45 | strtoll(const char *nptr, char **endptr, int base) |
| 53 | const char *nptr; | ||
| 54 | char **endptr; | ||
| 55 | register int base; | ||
| 56 | { | 46 | { |
| 57 | register const char *s; | 47 | const char *s; |
| 58 | register u_quad_t acc; | 48 | long long acc, cutoff; |
| 59 | register int c; | 49 | int c; |
| 60 | register u_quad_t qbase, cutoff; | 50 | int neg, any, cutlim; |
| 61 | register int neg, any, cutlim; | ||
| 62 | 51 | ||
| 63 | /* | 52 | /* |
| 64 | * Skip white space and pick up leading +/- sign if any. | 53 | * Skip white space and pick up leading +/- sign if any. |
| @@ -67,7 +56,7 @@ strtoq(nptr, endptr, base) | |||
| 67 | */ | 56 | */ |
| 68 | s = nptr; | 57 | s = nptr; |
| 69 | do { | 58 | do { |
| 70 | c = *s++; | 59 | c = (unsigned char) *s++; |
| 71 | } while (isspace(c)); | 60 | } while (isspace(c)); |
| 72 | if (c == '-') { | 61 | if (c == '-') { |
| 73 | neg = 1; | 62 | neg = 1; |
| @@ -93,7 +82,7 @@ strtoq(nptr, endptr, base) | |||
| 93 | * followed by a legal input character, is too big. One that | 82 | * followed by a legal input character, is too big. One that |
| 94 | * is equal to this value may be valid or not; the limit | 83 | * is equal to this value may be valid or not; the limit |
| 95 | * between valid and invalid numbers is then based on the last | 84 | * between valid and invalid numbers is then based on the last |
| 96 | * digit. For instance, if the range for quads is | 85 | * digit. For instance, if the range for long longs is |
| 97 | * [-9223372036854775808..9223372036854775807] and the input base | 86 | * [-9223372036854775808..9223372036854775807] and the input base |
| 98 | * is 10, cutoff will be set to 922337203685477580 and cutlim to | 87 | * is 10, cutoff will be set to 922337203685477580 and cutlim to |
| 99 | * either 7 (neg==0) or 8 (neg==1), meaning that if we have | 88 | * either 7 (neg==0) or 8 (neg==1), meaning that if we have |
| @@ -104,11 +93,17 @@ strtoq(nptr, endptr, base) | |||
| 104 | * Set any if any `digits' consumed; make it negative to indicate | 93 | * Set any if any `digits' consumed; make it negative to indicate |
| 105 | * overflow. | 94 | * overflow. |
| 106 | */ | 95 | */ |
| 107 | qbase = (unsigned)base; | 96 | cutoff = neg ? LLONG_MIN : LLONG_MAX; |
| 108 | cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX; | 97 | cutlim = cutoff % base; |
| 109 | cutlim = cutoff % qbase; | 98 | cutoff /= base; |
| 110 | cutoff /= qbase; | 99 | if (neg) { |
| 111 | for (acc = 0, any = 0;; c = *s++) { | 100 | if (cutlim > 0) { |
| 101 | cutlim -= base; | ||
| 102 | cutoff += 1; | ||
| 103 | } | ||
| 104 | cutlim = -cutlim; | ||
| 105 | } | ||
| 106 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
| 112 | if (isdigit(c)) | 107 | if (isdigit(c)) |
| 113 | c -= '0'; | 108 | c -= '0'; |
| 114 | else if (isalpha(c)) | 109 | else if (isalpha(c)) |
| @@ -117,20 +112,42 @@ strtoq(nptr, endptr, base) | |||
| 117 | break; | 112 | break; |
| 118 | if (c >= base) | 113 | if (c >= base) |
| 119 | break; | 114 | break; |
| 120 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | 115 | if (any < 0) |
| 121 | any = -1; | 116 | continue; |
| 122 | else { | 117 | if (neg) { |
| 123 | any = 1; | 118 | if (acc < cutoff || (acc == cutoff && c > cutlim)) { |
| 124 | acc *= qbase; | 119 | any = -1; |
| 125 | acc += c; | 120 | acc = LLONG_MIN; |
| 121 | errno = ERANGE; | ||
| 122 | } else { | ||
| 123 | any = 1; | ||
| 124 | acc *= base; | ||
| 125 | acc -= c; | ||
| 126 | } | ||
| 127 | } else { | ||
| 128 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { | ||
| 129 | any = -1; | ||
| 130 | acc = LLONG_MAX; | ||
| 131 | errno = ERANGE; | ||
| 132 | } else { | ||
| 133 | any = 1; | ||
| 134 | acc *= base; | ||
| 135 | acc += c; | ||
| 136 | } | ||
| 126 | } | 137 | } |
| 127 | } | 138 | } |
| 128 | if (any < 0) { | ||
| 129 | acc = neg ? QUAD_MIN : QUAD_MAX; | ||
| 130 | errno = ERANGE; | ||
| 131 | } else if (neg) | ||
| 132 | acc = -acc; | ||
| 133 | if (endptr != 0) | 139 | if (endptr != 0) |
| 134 | *endptr = (char *) (any ? s - 1 : nptr); | 140 | *endptr = (char *) (any ? s - 1 : nptr); |
| 135 | return (acc); | 141 | return (acc); |
| 136 | } | 142 | } |
| 143 | |||
| 144 | #ifdef __weak_alias | ||
| 145 | __weak_alias(strtoq, strtoll); | ||
| 146 | #else | ||
| 147 | quad_t | ||
| 148 | strtoq(const char *nptr, char **endptr, int base) | ||
| 149 | { | ||
| 150 | |||
| 151 | return ((quad_t)strtoll(nptr, endptr, base)); | ||
| 152 | } | ||
| 153 | #endif | ||
