0
|
1 |
/*
|
|
2 |
* CDDL HEADER START
|
|
3 |
*
|
|
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
|
|
7 |
* with the License.
|
|
8 |
*
|
|
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.
|
|
13 |
*
|
|
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]
|
|
19 |
*
|
|
20 |
* CDDL HEADER END
|
|
21 |
*/
|
|
22 |
/*
|
|
23 |
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
|
|
24 |
* Use is subject to license terms.
|
|
25 |
*/
|
|
26 |
|
|
27 |
#pragma ident "%Z%%M% %I% %E% SMI"
|
|
28 |
|
|
29 |
/* Copyright (c) 1988 AT&T */
|
|
30 |
/* All Rights Reserved */
|
|
31 |
|
|
32 |
|
|
33 |
#pragma weak strtoll = _strtoll
|
|
34 |
|
|
35 |
#include "synonyms.h"
|
|
36 |
#include <errno.h>
|
|
37 |
#include <ctype.h>
|
|
38 |
#include <limits.h>
|
|
39 |
#include <sys/types.h>
|
|
40 |
#include <stdlib.h>
|
|
41 |
|
|
42 |
#define DIGIT(x) \
|
|
43 |
(isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
|
|
44 |
|
|
45 |
#define MBASE ('z' - 'a' + 1 + 10)
|
|
46 |
|
|
47 |
/*
|
|
48 |
* The following macro is a local version of isalnum() which limits
|
|
49 |
* alphabetic characters to the ranges a-z and A-Z; locale dependent
|
|
50 |
* characters will not return 1. The members of a-z and A-Z are
|
|
51 |
* assumed to be in ascending order and contiguous
|
|
52 |
*/
|
|
53 |
#define lisalnum(x) \
|
|
54 |
(isdigit(x) || ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
|
|
55 |
|
|
56 |
longlong_t
|
|
57 |
strtoll(const char *str, char **nptr, int base)
|
|
58 |
{
|
|
59 |
longlong_t val;
|
|
60 |
int c;
|
|
61 |
int xx, neg = 0;
|
|
62 |
longlong_t multmin;
|
|
63 |
longlong_t limit;
|
|
64 |
longlong_t llong_min, llong_max;
|
|
65 |
const char **ptr = (const char **)nptr;
|
|
66 |
const unsigned char *ustr = (const unsigned char *)str;
|
|
67 |
|
|
68 |
llong_min = LLONG_MIN; /* from a local version of limits.h */
|
|
69 |
llong_max = LLONG_MAX;
|
|
70 |
|
|
71 |
if (ptr != (const char **)0)
|
|
72 |
*ptr = (char *)ustr; /* in case no number is formed */
|
|
73 |
if (base < 0 || base > MBASE || base == 1) {
|
|
74 |
errno = EINVAL;
|
|
75 |
return (0); /* base is invalid -- should be a fatal error */
|
|
76 |
}
|
|
77 |
if (!isalnum(c = *ustr)) {
|
|
78 |
while (isspace(c))
|
|
79 |
c = *++ustr;
|
|
80 |
switch (c) {
|
|
81 |
case '-':
|
|
82 |
neg++;
|
|
83 |
/* FALLTHROUGH */
|
|
84 |
case '+':
|
|
85 |
c = *++ustr;
|
|
86 |
}
|
|
87 |
}
|
|
88 |
if (base == 0)
|
|
89 |
if (c != '0')
|
|
90 |
base = 10;
|
|
91 |
else if (ustr[1] == 'x' || ustr[1] == 'X')
|
|
92 |
base = 16;
|
|
93 |
else
|
|
94 |
base = 8;
|
|
95 |
/*
|
|
96 |
* for any base > 10, the digits incrementally following
|
|
97 |
* 9 are assumed to be "abc...z" or "ABC...Z"
|
|
98 |
*/
|
|
99 |
if (!lisalnum(c) || (xx = DIGIT(c)) >= base)
|
|
100 |
return (0); /* no number formed */
|
|
101 |
if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') &&
|
|
102 |
isxdigit(ustr[2]))
|
|
103 |
c = *(ustr += 2); /* skip over leading "0x" or "0X" */
|
|
104 |
|
|
105 |
/* this code assumes that abs(llong_min) >= abs(llong_max) */
|
|
106 |
if (neg)
|
|
107 |
limit = llong_min;
|
|
108 |
else
|
|
109 |
limit = -llong_max;
|
|
110 |
|
|
111 |
multmin = limit / base;
|
|
112 |
|
|
113 |
val = -DIGIT(c);
|
|
114 |
for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
|
|
115 |
/* accumulate neg avoids surprises near llong_max */
|
|
116 |
if (val < multmin)
|
|
117 |
goto overflow;
|
|
118 |
val *= base;
|
|
119 |
if (val < limit + xx)
|
|
120 |
goto overflow;
|
|
121 |
val -= xx;
|
|
122 |
c = *++ustr;
|
|
123 |
}
|
|
124 |
if (ptr != (const char **)0)
|
|
125 |
*ptr = (char *)ustr;
|
|
126 |
return (neg ? val : -val);
|
|
127 |
|
|
128 |
overflow:
|
|
129 |
for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
|
|
130 |
;
|
|
131 |
if (ptr != (const char **)0)
|
|
132 |
*ptr = (char *)ustr;
|
|
133 |
errno = ERANGE;
|
|
134 |
return (neg ? llong_min : llong_max);
|
|
135 |
}
|