4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
30 #if defined(_KERNEL) && !defined(_BOOT)
31 #include <sys/errno.h>
32 #else /* _KERNEL && !_BOOT */
33 #if !defined(_BOOT) && !defined(_KMDB)
35 #endif /* !_BOOT && !_KMDB */
40 #endif /* _KERNEL && !_BOOT */
41 #include "strtolctype.h"
42 #include <sys/types.h>
44 #if defined(_KERNEL) && !defined(_BOOT)
46 ddi_strtoll(const char *str
, char **nptr
, int base
, longlong_t
*result
)
47 #else /* _KERNEL && !_BOOT */
49 strtoll(const char *str
, char **nptr
, int base
)
50 #endif /* _KERNEL && !_BOOT */
58 const char **ptr
= (const char **)nptr
;
59 const unsigned char *ustr
= (const unsigned char *)str
;
61 if (ptr
!= (const char **)0)
62 *ptr
= (char *)ustr
; /* in case no number is formed */
63 if (base
< 0 || base
> MBASE
|| base
== 1) {
64 /* base is invalid -- should be a fatal error */
65 #if defined(_KERNEL) && !defined(_BOOT)
67 #else /* _KERNEL && !_BOOT */
70 #endif /* _KERNEL && !_BOOT */
72 if (!isalnum(c
= *ustr
)) {
86 else if (ustr
[1] == 'x' || ustr
[1] == 'X')
91 * for any base > 10, the digits incrementally following
92 * 9 are assumed to be "abc...z" or "ABC...Z"
94 if (!lisalnum(c
) || (xx
= DIGIT(c
)) >= base
) {
95 /* no number formed */
96 #if defined(_KERNEL) && !defined(_BOOT)
98 #else /* _KERNEL && !_BOOT */
100 #endif /* _KERNEL && !_BOOT */
102 if (base
== 16 && c
== '0' && (ustr
[1] == 'x' || ustr
[1] == 'X') &&
104 c
= *(ustr
+= 2); /* skip over leading "0x" or "0X" */
106 /* this code assumes that abs(LLONG_MIN) >= abs(LLONG_MAX) */
111 multmin
= limit
/ (longlong_t
)base
;
113 for (c
= *++ustr
; lisalnum(c
) && (xx
= DIGIT(c
)) < base
; ) {
114 /* accumulate neg avoids surprises near LLONG_MAX */
118 if (val
< limit
+ xx
)
123 if (ptr
!= (const char **)0)
125 #if defined(_KERNEL) && !defined(_BOOT)
126 *result
= neg
? val
: -val
;
128 #else /* _KERNEL && !_BOOT */
129 return (neg
? val
: -val
);
130 #endif /* _KERNEL && !_BOOT */
133 for (c
= *++ustr
; lisalnum(c
) && (xx
= DIGIT(c
)) < base
; (c
= *++ustr
))
135 if (ptr
!= (const char **)0)
137 #if defined(_KERNEL) && !defined(_BOOT)
139 #else /* _KERNEL && !_BOOT */
141 return (neg
? LLONG_MIN
: LLONG_MAX
);
142 #endif /* _KERNEL && !_BOOT */