utils: Implement get_s64()

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>
This commit is contained in:
Vinicius Costa Gomes 2018-10-05 16:25:17 -07:00 committed by David Ahern
parent 720a44a751
commit a066bac8a2
2 changed files with 22 additions and 0 deletions

View 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);

View 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;