From: Chia-Yu Chang <[email protected]>

get_float_min_max() is based on get_float() and does an additional
check within the range strictly between the minimum and maximum values.

Signed-off-by: Chia-Yu Chang <[email protected]>
---
 include/utils.h |  1 +
 lib/utils.c     | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/include/utils.h b/include/utils.h
index 9a8b8026..91e6e31f 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -147,6 +147,7 @@ int get_long(long *val, const char *arg, int base);
 int get_integer(int *val, const char *arg, int base);
 int get_unsigned(unsigned *val, const char *arg, int base);
 int get_float(float *val, const char *arg);
+int get_float_min_max(float *val, const char *arg, float min, float max);
 int get_time_rtt(unsigned *val, const char *arg, int *raw);
 #define get_byte get_u8
 #define get_ushort get_u16
diff --git a/lib/utils.c b/lib/utils.c
index 103e4875..dd242d4d 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -234,6 +234,22 @@ int get_float(float *val, const char *arg)
        return 0;
 }
 
+int get_float_min_max(float *val, const char *arg, float min, float max)
+{
+       float res;
+       char *ptr;
+
+       if (!arg || !*arg)
+               return -1;
+       res = strtof(arg, &ptr);
+       if (!ptr || ptr == arg || *ptr)
+               return -1;
+       if (res < min || res > max)
+               return -1;
+       *val = res;
+       return 0;
+}
+
 /*
  * get_time_rtt is "translated" from a similar routine "get_time" in
  * tc_util.c.  We don't use the exact same routine because tc passes
-- 
2.34.1


Reply via email to