]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include/strutils: add ul_strtold()
authorKarel Zak <kzak@redhat.com>
Tue, 12 Sep 2023 12:14:50 +0000 (14:14 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 20 Nov 2023 21:25:46 +0000 (22:25 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/strutils.h
lib/strutils.c

index 7b44b4e00bf34876973fe39afc246d15d2053ffc..e9f8a0ce040bd33458acce840e25b98be36b4bc9 100644 (file)
@@ -27,6 +27,7 @@ extern int ul_strtos64(const char *str, int64_t *num, int base);
 extern int ul_strtou64(const char *str, uint64_t *num, int base);
 extern int ul_strtos32(const char *str, int32_t *num, int base);
 extern int ul_strtou32(const char *str, uint32_t *num, int base);
+extern int ul_strtold(const char *str, long double *num);
 
 extern int64_t str2num_or_err(const char *str, int base, const char *errmesg, int64_t low, int64_t up);
 extern uint64_t str2unum_or_err(const char *str, int base, const char *errmesg, uint64_t up);
index ccf71b987f33ae840a06e10d56a0c2d457bdaecc..422f8e58077e0523db27fd93c9c4f8c7977329ce 100644 (file)
@@ -456,21 +456,28 @@ err:
        errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
 }
 
-long double strtold_or_err(const char *str, const char *errmesg)
+int ul_strtold(const char *str, long double *num)
 {
-       double num;
        char *end = NULL;
 
        errno = 0;
        if (str == NULL || *str == '\0')
-               goto err;
-       num = strtold(str, &end);
+               return -(errno = EINVAL);
+       *num = strtold(str, &end);
 
-       if (errno || str == end || (end && *end))
-               goto err;
+       if (errno != 0)
+               return -errno;
+       if (str == end || (end && *end))
+               return -(errno = EINVAL);
+       return 0;
+}
 
-       return num;
-err:
+long double strtold_or_err(const char *str, const char *errmesg)
+{
+       long double num = 0;
+
+       if (ul_strtold(str, &num) == 0)
+               return num;
        if (errno == ERANGE)
                err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);