Let's provide a wrapper for strtod(). Signed-off-by: David Hildenbrand <da...@redhat.com> --- include/qemu/cutils.h | 1 + util/cutils.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+)
diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h index 7071bfe2d4..84fb9e53c6 100644 --- a/include/qemu/cutils.h +++ b/include/qemu/cutils.h @@ -146,6 +146,7 @@ int qemu_strtoi64(const char *nptr, const char **endptr, int base, int64_t *result); int qemu_strtou64(const char *nptr, const char **endptr, int base, uint64_t *result); +int qemu_strtod(const char *nptr, const char **endptr, double *result); int parse_uint(const char *s, unsigned long long *value, char **endptr, int base); diff --git a/util/cutils.c b/util/cutils.c index 698bd315bd..850e3af9ea 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -544,6 +544,28 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base, return check_strtox_error(nptr, ep, endptr, errno); } +/** + * Convert string @nptr to a double. + * + * Works like qemu_strtoul(), except it stores +/-HUGE_VAL on + * overflow/underflow. + */ +int qemu_strtod(const char *nptr, const char **endptr, double *result) +{ + char *ep; + + if (!nptr) { + if (endptr) { + *endptr = nptr; + } + return -EINVAL; + } + + errno = 0; + *result = strtod(nptr, &ep); + return check_strtox_error(nptr, ep, endptr, errno); +} + /** * Searches for the first occurrence of 'c' in 's', and returns a pointer * to the trailing null byte if none was found. -- 2.17.2