blob: 0515ff8678e6480368727f0703a10bdb017502c2 [file] [log] [blame]
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10#include "libbb.h"
11#include <math.h> /* just for HUGE_VAL */
12
13#define NOT_DIGIT(a) (((unsigned char)(a-'0')) > 9)
14
15double FAST_FUNC bb_strtod(const char *arg, char **endp)
16{
17 double v;
18 char *endptr;
19
20 if (arg[0] != '-' && NOT_DIGIT(arg[0]))
21 goto err;
22 errno = 0;
23 v = strtod(arg, &endptr);
24 if (endp)
25 *endp = endptr;
26 if (endptr[0]) {
27 /* "1234abcg" or out-of-range? */
28 if (isalnum(endptr[0]) || errno) {
29 err:
30 errno = ERANGE;
31 return HUGE_VAL;
32 }
33 /* good number, just suspicious terminator */
34 errno = EINVAL;
35 }
36 return v;
37}
38
39#if 0
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.
44 */
45char* FAST_FUNC bb_str_to_ts(struct timespec *ts, const char *arg)
46{
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);
51 else
52 ts->tv_sec = bb_strtoull(arg, &arg, 10);
53 ts->tv_nsec = 0;
54
55 if (*arg != '.')
56 return arg;
57
58 /* !EINVAL: number is not ok (alphanumeric ending, overflow etc) */
59 if (errno != EINVAL)
60 return arg;
61
62 if (!*++arg) /* "NNN." */
63 return arg;
64
65 { /* "NNN.xxx" - parse xxx */
66 int ndigits;
67 char *p;
68 char buf[10]; /* we never use more than 9 digits */
69
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);
73 ndigits = p - buf;
74 arg += ndigits;
75 /* normalize to nsec */
76 while (ndigits < 9) {
77 ndigits++;
78 ts->tv_nsec *= 10;
79 }
80 while (isdigit(*arg)) /* skip possible 10th plus digits */
81 arg++;
82 }
83 return arg;
84}
85#endif