]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
utils: Implement get_s64()
authorVinicius Costa Gomes <vinicius.gomes@intel.com>
Fri, 5 Oct 2018 23:25:17 +0000 (16:25 -0700)
committerDavid Ahern <dsahern@gmail.com>
Sun, 7 Oct 2018 17:30:28 +0000 (10:30 -0700)
Add this helper to read signed 64-bit integers from a string.

Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
include/utils.h
lib/utils.c

index eba67b6ecf44907d25a5b54db515f9305ba212aa..258d630ec9ce69959bd86d154414387c7019da74 100644 (file)
@@ -144,6 +144,7 @@ int get_time_rtt(unsigned *val, const char *arg, int *raw);
 #define get_byte get_u8
 #define get_ushort get_u16
 #define get_short get_s16
+int get_s64(__s64 *val, const char *arg, int base);
 int get_u64(__u64 *val, const char *arg, int base);
 int get_u32(__u32 *val, const char *arg, int base);
 int get_s32(__s32 *val, const char *arg, int base);
index 406ab8bd98275e3e9d1c6340a5aa9b84313f925c..cd1e0a987178d08b243c4f65d79587bd2d0c7f46 100644 (file)
@@ -383,6 +383,27 @@ int get_u8(__u8 *val, const char *arg, int base)
        return 0;
 }
 
+int get_s64(__s64 *val, const char *arg, int base)
+{
+       long res;
+       char *ptr;
+
+       errno = 0;
+
+       if (!arg || !*arg)
+               return -1;
+       res = strtoll(arg, &ptr, base);
+       if (!ptr || ptr == arg || *ptr)
+               return -1;
+       if ((res == LLONG_MIN || res == LLONG_MAX) && errno == ERANGE)
+               return -1;
+       if (res > INT64_MAX || res < INT64_MIN)
+               return -1;
+
+       *val = res;
+       return 0;
+}
+
 int get_s32(__s32 *val, const char *arg, int base)
 {
        long res;