3
0

bb_strtod.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* 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. #include "libbb.h"
  10. #include <math.h> /* just for HUGE_VAL */
  11. #define NOT_DIGIT(a) (((unsigned char)(a-'0')) > 9)
  12. double FAST_FUNC bb_strtod(const char *arg, char **endp)
  13. {
  14. double v;
  15. char *endptr;
  16. /* Allow .NN form. People want to use "sleep .15" etc */
  17. if (arg[0] != '-' && arg[0] != '.' && NOT_DIGIT(arg[0]))
  18. goto err;
  19. errno = 0;
  20. v = strtod(arg, &endptr);
  21. if (endp)
  22. *endp = endptr;
  23. if (endptr[0]) {
  24. /* "1234abcg" or out-of-range? */
  25. if (isalnum(endptr[0]) || errno) {
  26. err:
  27. errno = ERANGE;
  28. return HUGE_VAL;
  29. }
  30. /* good number, just suspicious terminator */
  31. errno = EINVAL;
  32. }
  33. return v;
  34. }
  35. #if 0
  36. /* String to timespec: "NNNN[.NNNNN]" -> struct timespec.
  37. * Can be used for other fixed-point needs.
  38. * Returns pointer past last converted char,
  39. * and returns errno similar to bb_strtoXX functions.
  40. */
  41. char* FAST_FUNC bb_str_to_ts(struct timespec *ts, const char *arg)
  42. {
  43. if (sizeof(ts->tv_sec) <= sizeof(int))
  44. ts->tv_sec = bb_strtou(arg, &arg, 10);
  45. else if (sizeof(ts->tv_sec) <= sizeof(long))
  46. ts->tv_sec = bb_strtoul(arg, &arg, 10);
  47. else
  48. ts->tv_sec = bb_strtoull(arg, &arg, 10);
  49. ts->tv_nsec = 0;
  50. if (*arg != '.')
  51. return arg;
  52. /* !EINVAL: number is not ok (alphanumeric ending, overflow etc) */
  53. if (errno != EINVAL)
  54. return arg;
  55. if (!*++arg) /* "NNN." */
  56. return arg;
  57. { /* "NNN.xxx" - parse xxx */
  58. int ndigits;
  59. char *p;
  60. char buf[10]; /* we never use more than 9 digits */
  61. /* Need to make a copy to avoid false overflow */
  62. safe_strncpy(buf, arg, 10);
  63. ts->tv_nsec = bb_strtou(buf, &p, 10);
  64. ndigits = p - buf;
  65. arg += ndigits;
  66. /* normalize to nsec */
  67. while (ndigits < 9) {
  68. ndigits++;
  69. ts->tv_nsec *= 10;
  70. }
  71. while (isdigit(*arg)) /* skip possible 10th plus digits */
  72. arg++;
  73. }
  74. return arg;
  75. }
  76. #endif