From 240baf2b88e34c1600aa2d11a1f1d9a11af4893f Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Thu, 27 Aug 2020 15:39:26 -0400 Subject: [PATCH] Implement utils_parse_unsigned_long_long MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit To be used for trigger creation. Signed-off-by: Simon Marchi Signed-off-by: Jonathan Rajotte Signed-off-by: Jérémie Galarneau Change-Id: Id7d9f76f3b9a29c9657eaa0daf3eb112fee54b09 --- src/common/utils.c | 36 ++++++++++++++++++++++++++++++++++++ src/common/utils.h | 12 ++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/common/utils.c b/src/common/utils.c index bd61fff3d..134d2054f 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -1669,3 +1669,39 @@ end: free(buf); return ret_val; } + +LTTNG_HIDDEN +int utils_parse_unsigned_long_long(const char *str, + unsigned long long *value) +{ + int ret; + char *endptr; + + assert(str); + assert(value); + + errno = 0; + *value = strtoull(str, &endptr, 10); + + /* Conversion failed. Out of range? */ + if (errno != 0) { + /* Don't print an error; allow the caller to log a better error. */ + DBG("Failed to parse string as unsigned long long number: string = '%s', errno = %d", + str, errno); + ret = -1; + goto end; + } + + /* Not the end of the string or empty string. */ + if (*endptr || endptr == str) { + DBG("Failed to parse string as unsigned long long number: string = '%s'", + str); + ret = -1; + goto end; + } + + ret = 0; + +end: + return ret; +} diff --git a/src/common/utils.h b/src/common/utils.h index dec4cd8aa..570216d53 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -59,4 +59,16 @@ enum lttng_error_code utils_user_id_from_name( enum lttng_error_code utils_group_id_from_name( const char *group_name, gid_t *group_id); +/* + * Parse `str` as an unsigned long long value. + * + * Return 0 on success. Return -1 on failure which can be because: + * + * - `str` is zero length + * - `str` contains invalid + */ +LTTNG_HIDDEN +int utils_parse_unsigned_long_long(const char *str, + unsigned long long *value); + #endif /* _COMMON_UTILS_H */ -- 2.34.1