Implement utils_parse_unsigned_long_long
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Thu, 27 Aug 2020 19:39:26 +0000 (15:39 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 12 Feb 2021 20:50:09 +0000 (15:50 -0500)
To be used for trigger creation.

Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: Id7d9f76f3b9a29c9657eaa0daf3eb112fee54b09

src/common/utils.c
src/common/utils.h

index bd61fff3d52ad8c6c16d68b0ca9e195009225018..134d2054fbde1ea74de3a8562c93c13067bfcf53 100644 (file)
@@ -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;
+}
index dec4cd8aa2268490eae52adb55795d76ab62a07b..570216d53c9709dddcfaf480f68a465bc0a8f294 100644 (file)
@@ -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 */
This page took 0.036508 seconds and 4 git commands to generate.