summaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenis Vlasenko2008-07-12 17:05:14 +0000
committerDenis Vlasenko2008-07-12 17:05:14 +0000
commitadbb73bda7c0ff75caceaf6ad29187293f0afd3f (patch)
treeacfa5f28ecd65dc585e83200816a904febf01e25 /libbb
parent34e8f6a7ac6a88304e89725d7286f1ff4405a70c (diff)
downloadbusybox-adbb73bda7c0ff75caceaf6ad29187293f0afd3f.zip
busybox-adbb73bda7c0ff75caceaf6ad29187293f0afd3f.tar.gz
sleep: if FANCY && DESKTOP, support fractional seconds, minutes,
hours and so on. It's coreutils compat. bloatcheck is atrocious :( function old new delta sleep_main 71 362 +291 bb_strtod - 127 +127 make_device 1269 1294 +25 getoptscmd 708 713 +5 switch_root_main 402 401 -1 display_speed 90 85 -5 show_entry 295 289 -6 parse_expr 841 833 -8 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 3/4 up/down: 448/-20) Total: 428 bytes
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Kbuild1
-rw-r--r--libbb/bb_strtod.c85
-rw-r--r--libbb/bb_strtonum.c30
3 files changed, 86 insertions, 30 deletions
diff --git a/libbb/Kbuild b/libbb/Kbuild
index c49297b..ab85ffc 100644
--- a/libbb/Kbuild
+++ b/libbb/Kbuild
@@ -13,6 +13,7 @@ lib-y += bb_basename.o
lib-y += bb_do_delay.o
lib-y += bb_pwd.o
lib-y += bb_qsort.o
+lib-y += bb_strtod.o
lib-y += bb_strtonum.o
lib-y += change_identity.o
lib-y += chomp.o
diff --git a/libbb/bb_strtod.c b/libbb/bb_strtod.c
new file mode 100644
index 0000000..0515ff8
--- /dev/null
+++ b/libbb/bb_strtod.c
@@ -0,0 +1,85 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Utility routines.
+ *
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+#include "libbb.h"
+#include <math.h> /* just for HUGE_VAL */
+
+#define NOT_DIGIT(a) (((unsigned char)(a-'0')) > 9)
+
+double FAST_FUNC bb_strtod(const char *arg, char **endp)
+{
+ double v;
+ char *endptr;
+
+ if (arg[0] != '-' && NOT_DIGIT(arg[0]))
+ goto err;
+ errno = 0;
+ v = strtod(arg, &endptr);
+ if (endp)
+ *endp = endptr;
+ if (endptr[0]) {
+ /* "1234abcg" or out-of-range? */
+ if (isalnum(endptr[0]) || errno) {
+ err:
+ errno = ERANGE;
+ return HUGE_VAL;
+ }
+ /* good number, just suspicious terminator */
+ errno = EINVAL;
+ }
+ return v;
+}
+
+#if 0
+/* String to timespec: "NNNN[.NNNNN]" -> struct timespec.
+ * Can be used for other fixed-point needs.
+ * Returns pointer past last converted char,
+ * and returns errno similar to bb_strtoXX functions.
+ */
+char* FAST_FUNC bb_str_to_ts(struct timespec *ts, const char *arg)
+{
+ if (sizeof(ts->tv_sec) <= sizeof(int))
+ ts->tv_sec = bb_strtou(arg, &arg, 10);
+ else if (sizeof(ts->tv_sec) <= sizeof(long))
+ ts->tv_sec = bb_strtoul(arg, &arg, 10);
+ else
+ ts->tv_sec = bb_strtoull(arg, &arg, 10);
+ ts->tv_nsec = 0;
+
+ if (*arg != '.')
+ return arg;
+
+ /* !EINVAL: number is not ok (alphanumeric ending, overflow etc) */
+ if (errno != EINVAL)
+ return arg;
+
+ if (!*++arg) /* "NNN." */
+ return arg;
+
+ { /* "NNN.xxx" - parse xxx */
+ int ndigits;
+ char *p;
+ char buf[10]; /* we never use more than 9 digits */
+
+ /* Need to make a copy to avoid false overflow */
+ safe_strncpy(buf, arg, 10);
+ ts->tv_nsec = bb_strtou(buf, &p, 10);
+ ndigits = p - buf;
+ arg += ndigits;
+ /* normalize to nsec */
+ while (ndigits < 9) {
+ ndigits++;
+ ts->tv_nsec *= 10;
+ }
+ while (isdigit(*arg)) /* skip possible 10th plus digits */
+ arg++;
+ }
+ return arg;
+}
+#endif
diff --git a/libbb/bb_strtonum.c b/libbb/bb_strtonum.c
index 525c830..50ed99b 100644
--- a/libbb/bb_strtonum.c
+++ b/libbb/bb_strtonum.c
@@ -124,33 +124,3 @@ int FAST_FUNC bb_strtoi(const char *arg, char **endp, int base)
return handle_errors(v, endp, endptr);
}
#endif
-
-/* Floating point */
-
-#if 0
-
-#include <math.h> /* just for HUGE_VAL */
-#define NOT_DIGIT(a) (((unsigned char)(a-'0')) > 9)
-double FAST_FUNC bb_strtod(const char *arg, char **endp)
-{
- double v;
- char *endptr;
-
- if (arg[0] != '-' && NOT_DIGIT(arg[0])) goto err;
- errno = 0;
- v = strtod(arg, &endptr);
- if (endp) *endp = endptr;
- if (endptr[0]) {
- /* "1234abcg" or out-of-range? */
- if (isalnum(endptr[0]) || errno) {
- err:
- errno = ERANGE;
- return HUGE_VAL;
- }
- /* good number, just suspicious terminator */
- errno = EINVAL;
- }
- return v;
-}
-
-#endif