207
207
#endif /* !defined(HAVE_ASPRINTF) && !defined(__AST_DEBUG_MALLOC) */
211
#define LONG_MIN (-9223372036854775807L-1L)
212
/* min value of a "long int" */
215
#define LONG_MAX 9223372036854775807L
216
/* max value of a "long int" */
220
* Convert a string to a quad integer.
222
* \note Ignores `locale' stuff. Assumes that the upper and lower case
223
* alphabets and digits are each contiguous.
225
uint64_t strtoq(const char *nptr, char **endptr, int base)
230
uint64_t qbase, cutoff;
231
int neg, any, cutlim;
234
* Skip white space and pick up leading +/- sign if any.
235
* If base is 0, allow 0x for hex and 0 for octal, else
236
* assume decimal; if base is already 16, allow 0x.
241
} while (isspace(c));
250
if ((base == 0 || base == 16) &&
251
c == '\0' && (*s == 'x' || *s == 'X')) {
257
base = c == '\0' ? 8 : 10;
260
* Compute the cutoff value between legal numbers and illegal
261
* numbers. That is the largest legal value, divided by the
262
* base. An input number that is greater than this value, if
263
* followed by a legal input character, is too big. One that
264
* is equal to this value may be valid or not; the limit
265
* between valid and invalid numbers is then based on the last
266
* digit. For instance, if the range for quads is
267
* [-9223372036854775808..9223372036854775807] and the input base
268
* is 10, cutoff will be set to 922337203685477580 and cutlim to
269
* either 7 (neg==0) or 8 (neg==1), meaning that if we have
270
* accumulated a value > 922337203685477580, or equal but the
271
* next digit is > 7 (or 8), the number is too big, and we will
272
* return a range error.
274
* Set any if any `digits' consumed; make it negative to indicate
277
qbase = (unsigned)base;
278
cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
279
cutlim = cutoff % qbase;
281
for (acc = 0, any = 0;; c = *s++) {
287
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
292
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
301
acc = neg ? LONG_MIN : LONG_MAX;
305
*((const char **)endptr) = any ? s - 1 : nptr;
308
#endif /* !HAVE_STRTOQ */
310
209
#ifndef HAVE_GETLOADAVG
312
211
/*! \brief Alternative method of getting load avg on Linux only */