2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
5 * The Regents of the University of California. All rights reserved.
7 * By using this file, you agree to the terms and conditions set
8 * forth in the LICENSE file which can be found at the top level of
9 * the sendmail distribution.
12 #pragma ident "%Z%%M% %I% %E% SMI"
15 SM_IDSTR(id
, "@(#)$Id: strto.c,v 1.18 2001/12/30 04:59:37 gshapiro Exp $")
17 #include <sys/param.h>
18 #include <sys/types.h>
22 #include <sm/limits.h>
24 #include <sm/string.h>
27 ** SM_STRTOLL -- Convert a string to a (signed) long long integer.
29 ** Ignores `locale' stuff. Assumes that the upper and lower case
30 ** alphabets and digits are each contiguous.
33 ** nptr -- string containing number
34 ** endptr -- location of first invalid character
35 ** base -- numeric base that 'nptr' number is based in
38 ** Failure: on underflow LLONG_MIN is returned; on overflow
39 ** LLONG_MAX is returned and errno is set.
40 ** When 'endptr' == '\0' then the entire string 'nptr'
42 ** Success: returns the converted number
46 sm_strtoll(nptr
, endptr
, base
)
52 register const char *s
;
53 register LONGLONG_T acc
, cutoff
;
55 register int any
, cutlim
;
58 ** Skip white space and pick up leading +/- sign if any.
59 ** If base is 0, allow 0x for hex and 0 for octal, else
60 ** assume decimal; if base is already 16, allow 0x.
66 c
= (unsigned char) *s
++;
67 } while (isascii(c
) && isspace(c
));
79 if ((base
== 0 || base
== 16) &&
80 c
== '0' && (*s
== 'x' || *s
== 'X'))
87 base
= c
== '0' ? 8 : 10;
90 ** Compute the cutoff value between legal numbers and illegal
91 ** numbers. That is the largest legal value, divided by the
92 ** base. An input number that is greater than this value, if
93 ** followed by a legal input character, is too big. One that
94 ** is equal to this value may be valid or not; the limit
95 ** between valid and invalid numbers is then based on the last
96 ** digit. For instance, if the range for long-long's is
97 ** [-9223372036854775808..9223372036854775807] and the input base
98 ** is 10, cutoff will be set to 922337203685477580 and cutlim to
99 ** either 7 (!neg) or 8 (neg), meaning that if we have
100 ** accumulated a value > 922337203685477580, or equal but the
101 ** next digit is > 7 (or 8), the number is too big, and we will
102 ** return a range error.
104 ** Set any if any `digits' consumed; make it negative to indicate
108 cutoff
= neg
? LLONG_MIN
: LLONG_MAX
;
109 cutlim
= cutoff
% base
;
120 for (acc
= 0, any
= 0;; c
= (unsigned char) *s
++)
122 if (isascii(c
) && isdigit(c
))
124 else if (isascii(c
) && isalpha(c
))
125 c
-= isupper(c
) ? 'A' - 10 : 'a' - 10;
134 if (acc
< cutoff
|| (acc
== cutoff
&& c
> cutlim
))
149 if (acc
> cutoff
|| (acc
== cutoff
&& c
> cutlim
))
164 *endptr
= (char *) (any
? s
- 1 : nptr
);
169 ** SM_STRTOULL -- Convert a string to an unsigned long long integer.
171 ** Ignores `locale' stuff. Assumes that the upper and lower case
172 ** alphabets and digits are each contiguous.
175 ** nptr -- string containing (unsigned) number
176 ** endptr -- location of first invalid character
177 ** base -- numeric base that 'nptr' number is based in
180 ** Failure: on overflow ULLONG_MAX is returned and errno is set.
181 ** When 'endptr' == '\0' then the entire string 'nptr'
183 ** Success: returns the converted number
187 sm_strtoull(nptr
, endptr
, base
)
192 register const char *s
;
193 register ULONGLONG_T acc
, cutoff
;
196 register int any
, cutlim
;
198 /* See sm_strtoll for comments as to the logic used. */
202 c
= (unsigned char) *s
++;
203 } while (isascii(c
) && isspace(c
));
214 if ((base
== 0 || base
== 16) &&
215 c
== '0' && (*s
== 'x' || *s
== 'X'))
222 base
= c
== '0' ? 8 : 10;
224 cutoff
= ULLONG_MAX
/ (ULONGLONG_T
)base
;
225 cutlim
= ULLONG_MAX
% (ULONGLONG_T
)base
;
226 for (acc
= 0, any
= 0;; c
= (unsigned char) *s
++)
228 if (isascii(c
) && isdigit(c
))
230 else if (isascii(c
) && isalpha(c
))
231 c
-= isupper(c
) ? 'A' - 10 : 'a' - 10;
238 if (acc
> cutoff
|| (acc
== cutoff
&& c
> cutlim
))
247 acc
*= (ULONGLONG_T
)base
;
252 acc
= -((LONGLONG_T
) acc
);
254 *endptr
= (char *) (any
? s
- 1 : nptr
);