1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 #include <math.h> /* just for HUGE_VAL */
13 #define NOT_DIGIT(a) (((unsigned char)(a-'0')) > 9)
15 double FAST_FUNC bb_strtod(const char *arg, char **endp)
20 if (arg[0] != '-' && NOT_DIGIT(arg[0]))
23 v = strtod(arg, &endptr);
27 /* "1234abcg" or out-of-range? */
28 if (isalnum(endptr[0]) || errno) {
33 /* good number, just suspicious terminator */
40 /* String to timespec: "NNNN[.NNNNN]" -> struct timespec.
41 * Can be used for other fixed-point needs.
42 * Returns pointer past last converted char,
43 * and returns errno similar to bb_strtoXX functions.
45 char* FAST_FUNC bb_str_to_ts(struct timespec *ts, const char *arg)
47 if (sizeof(ts->tv_sec) <= sizeof(int))
48 ts->tv_sec = bb_strtou(arg, &arg, 10);
49 else if (sizeof(ts->tv_sec) <= sizeof(long))
50 ts->tv_sec = bb_strtoul(arg, &arg, 10);
52 ts->tv_sec = bb_strtoull(arg, &arg, 10);
58 /* !EINVAL: number is not ok (alphanumeric ending, overflow etc) */
62 if (!*++arg) /* "NNN." */
65 { /* "NNN.xxx" - parse xxx */
68 char buf[10]; /* we never use more than 9 digits */
70 /* Need to make a copy to avoid false overflow */
71 safe_strncpy(buf, arg, 10);
72 ts->tv_nsec = bb_strtou(buf, &p, 10);
75 /* normalize to nsec */
80 while (isdigit(*arg)) /* skip possible 10th plus digits */