diff options
author | millert <> | 2002-06-29 00:20:11 +0000 |
---|---|---|
committer | millert <> | 2002-06-29 00:20:11 +0000 |
commit | 6adefe10f90940abb77fbd207bd42c11497679b9 (patch) | |
tree | b09c3b0aa815c3c77e8a7c20492b1d18817cd32b /src/lib/libc/stdlib/strtoll.c | |
parent | e08f871ca434f7bd4986a8b654059f32fda1e02a (diff) | |
download | openbsd-6adefe10f90940abb77fbd207bd42c11497679b9.tar.gz openbsd-6adefe10f90940abb77fbd207bd42c11497679b9.tar.bz2 openbsd-6adefe10f90940abb77fbd207bd42c11497679b9.zip |
Replace strtou?q() with the more standard strtou?ll(), using weak
aliases to fake up strtou?q(). espie@ OK.
Diffstat (limited to 'src/lib/libc/stdlib/strtoll.c')
-rw-r--r-- | src/lib/libc/stdlib/strtoll.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/strtoll.c b/src/lib/libc/stdlib/strtoll.c new file mode 100644 index 0000000000..b0eb6d198c --- /dev/null +++ b/src/lib/libc/stdlib/strtoll.c | |||
@@ -0,0 +1,166 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1992 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. All advertising materials mentioning features or use of this software | ||
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 | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | */ | ||
33 | |||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | static const char rcsid[] = "$OpenBSD: strtoll.c,v 1.1 2002/06/29 00:20:11 millert Exp $"; | ||
36 | #endif /* LIBC_SCCS and not lint */ | ||
37 | |||
38 | #include <sys/types.h> | ||
39 | |||
40 | #include <ctype.h> | ||
41 | #include <errno.h> | ||
42 | #include <limits.h> | ||
43 | #include <stdlib.h> | ||
44 | |||
45 | /* | ||
46 | * Convert a string to a long long. | ||
47 | * | ||
48 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
49 | * alphabets and digits are each contiguous. | ||
50 | */ | ||
51 | long long | ||
52 | strtoll(nptr, endptr, base) | ||
53 | const char *nptr; | ||
54 | char **endptr; | ||
55 | int base; | ||
56 | { | ||
57 | const char *s; | ||
58 | long long acc, cutoff; | ||
59 | int c; | ||
60 | int neg, any, cutlim; | ||
61 | |||
62 | /* | ||
63 | * Skip white space and pick up leading +/- sign if any. | ||
64 | * If base is 0, allow 0x for hex and 0 for octal, else | ||
65 | * assume decimal; if base is already 16, allow 0x. | ||
66 | */ | ||
67 | s = nptr; | ||
68 | do { | ||
69 | c = (unsigned char) *s++; | ||
70 | } while (isspace(c)); | ||
71 | if (c == '-') { | ||
72 | neg = 1; | ||
73 | c = *s++; | ||
74 | } else { | ||
75 | neg = 0; | ||
76 | if (c == '+') | ||
77 | c = *s++; | ||
78 | } | ||
79 | if ((base == 0 || base == 16) && | ||
80 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
81 | c = s[1]; | ||
82 | s += 2; | ||
83 | base = 16; | ||
84 | } | ||
85 | if (base == 0) | ||
86 | base = c == '0' ? 8 : 10; | ||
87 | |||
88 | /* | ||
89 | * Compute the cutoff value between legal numbers and illegal | ||
90 | * numbers. That is the largest legal value, divided by the | ||
91 | * base. An input number that is greater than this value, if | ||
92 | * followed by a legal input character, is too big. One that | ||
93 | * is equal to this value may be valid or not; the limit | ||
94 | * between valid and invalid numbers is then based on the last | ||
95 | * digit. For instance, if the range for long longs is | ||
96 | * [-9223372036854775808..9223372036854775807] and the input base | ||
97 | * is 10, cutoff will be set to 922337203685477580 and cutlim to | ||
98 | * either 7 (neg==0) or 8 (neg==1), meaning that if we have | ||
99 | * accumulated a value > 922337203685477580, or equal but the | ||
100 | * next digit is > 7 (or 8), the number is too big, and we will | ||
101 | * return a range error. | ||
102 | * | ||
103 | * Set any if any `digits' consumed; make it negative to indicate | ||
104 | * overflow. | ||
105 | */ | ||
106 | cutoff = neg ? LLONG_MIN : LLONG_MAX; | ||
107 | cutlim = cutoff % base; | ||
108 | cutoff /= base; | ||
109 | if (neg) { | ||
110 | if (cutlim > 0) { | ||
111 | cutlim -= base; | ||
112 | cutoff += 1; | ||
113 | } | ||
114 | cutlim = -cutlim; | ||
115 | } | ||
116 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
117 | if (isdigit(c)) | ||
118 | c -= '0'; | ||
119 | else if (isalpha(c)) | ||
120 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
121 | else | ||
122 | break; | ||
123 | if (c >= base) | ||
124 | break; | ||
125 | if (any < 0) | ||
126 | continue; | ||
127 | if (neg) { | ||
128 | if (acc < cutoff || (acc == cutoff && c > cutlim)) { | ||
129 | any = -1; | ||
130 | acc = LLONG_MIN; | ||
131 | errno = ERANGE; | ||
132 | } else { | ||
133 | any = 1; | ||
134 | acc *= base; | ||
135 | acc -= c; | ||
136 | } | ||
137 | } else { | ||
138 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { | ||
139 | any = -1; | ||
140 | acc = LLONG_MAX; | ||
141 | errno = ERANGE; | ||
142 | } else { | ||
143 | any = 1; | ||
144 | acc *= base; | ||
145 | acc += c; | ||
146 | } | ||
147 | } | ||
148 | } | ||
149 | if (endptr != 0) | ||
150 | *endptr = (char *) (any ? s - 1 : nptr); | ||
151 | return (acc); | ||
152 | } | ||
153 | |||
154 | #ifdef __weak_alias | ||
155 | __weak_alias(strtoq, strtoll); | ||
156 | #else | ||
157 | quad_t | ||
158 | strtoq(nptr, endptr, base) | ||
159 | const char *nptr; | ||
160 | char **endptr; | ||
161 | int base; | ||
162 | { | ||
163 | |||
164 | return ((quad_t)strtoll(nptr, endptr, base); | ||
165 | } | ||
166 | #endif | ||