]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
Add get_float_min_max() in lib/utils.c
authorChia-Yu Chang <chia-yu.chang@nokia-bell-labs.com>
Mon, 4 Aug 2025 12:20:18 +0000 (14:20 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Fri, 8 Aug 2025 18:15:29 +0000 (11:15 -0700)
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 <chia-yu.chang@nokia-bell-labs.com>
include/utils.h
lib/utils.c

index 9a8b80262cffd59aa2c4bb12983470551fa59a41..91e6e31f941118ed7cf480712a894af6cade62ec 100644 (file)
@@ -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
index 103e4875cef8f0a9c7cdf04cfb655cdf71e0c70c..dd242d4d672e47a42c7060e258860ceb1c264af8 100644 (file)
@@ -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