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 */
34 #if defined(_KERNEL) && !defined(_BOOT)
35 #include <sys/errno.h>
36 #else /* _KERNEL && !_BOOT */
37 #if !defined(_BOOT) && !defined(_KMDB) && !defined(_STANDALONE)
39 #endif /* !_BOOT && !_KMDB && !_STANDALONE */
40 #if defined(_STANDALONE)
41 #include <sys/cdefs.h>
49 #endif /* _STANDALONE */
50 #endif /* _KERNEL && !_BOOT */
51 #include "strtolctype.h"
52 #include <sys/types.h>
54 #if defined(_KERNEL) && !defined(_BOOT)
56 ddi_strtoul(const char *str
, char **nptr
, int base
, unsigned long *result
)
57 #else /* _KERNEL && !_BOOT */
59 strtoul(const char *str
, char **nptr
, int base
)
60 #endif /* _KERNEL && !_BOOT */
66 unsigned long multmax
;
67 const char **ptr
= (const char **)nptr
;
68 const unsigned char *ustr
= (const unsigned char *)str
;
71 *ptr
= (char *)ustr
; /* in case no number is formed */
72 if (base
< 0 || base
> MBASE
|| base
== 1) {
73 /* base is invalid -- should be a fatal error */
74 #if defined(_KERNEL) && !defined(_BOOT)
76 #else /* _KERNEL && !_BOOT */
79 #endif /* _KERNEL && !_BOOT */
81 if (!isalnum(c
= *ustr
)) {
95 else if (ustr
[1] == 'x' || ustr
[1] == 'X')
101 * for any base > 10, the digits incrementally following
102 * 9 are assumed to be "abc...z" or "ABC...Z"
104 if (!lisalnum(c
) || (xx
= DIGIT(c
)) >= base
) {
105 /* no number formed */
106 #if defined(_KERNEL) && !defined(_BOOT)
108 #else /* _KERNEL && !_BOOT */
110 #endif /* _KERNEL && !_BOOT */
112 if (base
== 16 && c
== '0' && (ustr
[1] == 'x' || ustr
[1] == 'X') &&
114 c
= *(ustr
+= 2); /* skip over leading "0x" or "0X" */
116 multmax
= ULONG_MAX
/ (unsigned long)base
;
118 for (c
= *++ustr
; lisalnum(c
) && (xx
= DIGIT(c
)) < base
; ) {
122 if (ULONG_MAX
- val
< (unsigned long)xx
)
129 #if defined(_KERNEL) && !defined(_BOOT)
130 *result
= neg
? -val
: val
;
132 #else /* _KERNEL && !_BOOT */
133 return (neg
? -val
: val
);
134 #endif /* _KERNEL && !_BOOT */
137 for (c
= *++ustr
; lisalnum(c
) && (xx
= DIGIT(c
)) < base
; (c
= *++ustr
))
141 #if defined(_KERNEL) && !defined(_BOOT)
143 #else /* _KERNEL && !_BOOT */
146 #endif /* _KERNEL && !_BOOT */