diff options
Diffstat (limited to 'src/lib/libc/stdlib/strtoull.c')
-rw-r--r-- | src/lib/libc/stdlib/strtoull.c | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/strtoull.c b/src/lib/libc/stdlib/strtoull.c new file mode 100644 index 0000000000..a0ac9b381f --- /dev/null +++ b/src/lib/libc/stdlib/strtoull.c | |||
@@ -0,0 +1,124 @@ | |||
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. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static const char rcsid[] = "$OpenBSD: strtoull.c,v 1.2 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | |||
36 | #include <ctype.h> | ||
37 | #include <errno.h> | ||
38 | #include <limits.h> | ||
39 | #include <stdlib.h> | ||
40 | |||
41 | /* | ||
42 | * Convert a string to an unsigned long long. | ||
43 | * | ||
44 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
45 | * alphabets and digits are each contiguous. | ||
46 | */ | ||
47 | unsigned long long | ||
48 | strtoull(nptr, endptr, base) | ||
49 | const char *nptr; | ||
50 | char **endptr; | ||
51 | int base; | ||
52 | { | ||
53 | const char *s; | ||
54 | unsigned long long acc, cutoff; | ||
55 | int c; | ||
56 | int neg, any, cutlim; | ||
57 | |||
58 | /* | ||
59 | * See strtoq for comments as to the logic used. | ||
60 | */ | ||
61 | s = nptr; | ||
62 | do { | ||
63 | c = (unsigned char) *s++; | ||
64 | } while (isspace(c)); | ||
65 | if (c == '-') { | ||
66 | neg = 1; | ||
67 | c = *s++; | ||
68 | } else { | ||
69 | neg = 0; | ||
70 | if (c == '+') | ||
71 | c = *s++; | ||
72 | } | ||
73 | if ((base == 0 || base == 16) && | ||
74 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
75 | c = s[1]; | ||
76 | s += 2; | ||
77 | base = 16; | ||
78 | } | ||
79 | if (base == 0) | ||
80 | base = c == '0' ? 8 : 10; | ||
81 | |||
82 | cutoff = ULLONG_MAX / (unsigned long long)base; | ||
83 | cutlim = ULLONG_MAX % (unsigned long long)base; | ||
84 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
85 | if (isdigit(c)) | ||
86 | c -= '0'; | ||
87 | else if (isalpha(c)) | ||
88 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
89 | else | ||
90 | break; | ||
91 | if (c >= base) | ||
92 | break; | ||
93 | if (any < 0) | ||
94 | continue; | ||
95 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { | ||
96 | any = -1; | ||
97 | acc = ULLONG_MAX; | ||
98 | errno = ERANGE; | ||
99 | } else { | ||
100 | any = 1; | ||
101 | acc *= (unsigned long long)base; | ||
102 | acc += c; | ||
103 | } | ||
104 | } | ||
105 | if (neg && any > 0) | ||
106 | acc = -acc; | ||
107 | if (endptr != 0) | ||
108 | *endptr = (char *) (any ? s - 1 : nptr); | ||
109 | return (acc); | ||
110 | } | ||
111 | |||
112 | #ifdef __weak_alias | ||
113 | __weak_alias(strtouq, strtoull); | ||
114 | #else | ||
115 | u_quad_t | ||
116 | strtouq(nptr, endptr, base) | ||
117 | const char *nptr; | ||
118 | char **endptr; | ||
119 | int base; | ||
120 | { | ||
121 | |||
122 | return ((u_quad_t)strtoull(nptr, endptr, base); | ||
123 | } | ||
124 | #endif | ||