4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 /* Copyright (c) 1984 AT&T */
23 /* All Rights Reserved */
25 #pragma ident "%Z%%M% %I% %E% SMI" /* from S5R2 2.1 */
29 #define DIGIT(x) (isdigit(x) ? (x) - '0' : \
30 islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
31 #define MBASE ('z' - 'a' + 1 + 10)
34 strtol(str
, ptr
, base
)
43 if (ptr
!= (char **)0)
44 *ptr
= str
; /* in case no number is formed */
45 if (base
< 0 || base
> MBASE
)
46 return (0); /* base is invalid -- should be a fatal error */
47 if (!isalnum(c
= *str
)) {
53 case '+': /* fall-through */
60 else if (str
[1] == 'x' || str
[1] == 'X')
65 * for any base > 10, the digits incrementally following
66 * 9 are assumed to be "abc...z" or "ABC...Z"
68 if (!isalnum(c
) || (xx
= DIGIT(c
)) >= base
)
69 return (0); /* no number formed */
70 if (base
== 16 && c
== '0' && isxdigit(str
[2]) &&
71 (str
[1] == 'x' || str
[1] == 'X'))
72 c
= *(str
+= 2); /* skip over leading "0x" or "0X" */
73 for (val
= -DIGIT(c
); isalnum(c
= *++str
) && (xx
= DIGIT(c
)) < base
; )
74 /* accumulate neg avoids surprises near MAXLONG */
75 val
= base
* val
- xx
;
76 if (ptr
!= (char **)0)
78 return (neg
? val
: -val
);