bb_strtod.c 2.0 KB

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