]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include/strutils: add ul_strtou16()
authorKarel Zak <kzak@redhat.com>
Tue, 24 Jun 2025 09:29:22 +0000 (11:29 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 24 Jun 2025 09:29:22 +0000 (11:29 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/strutils.h
lib/strutils.c

index 7cb3652540353a73288edbfc5d484e5e3f120914..e462040c80cc0386eb84c3b5e72cb5a965130e1b 100644 (file)
@@ -29,6 +29,8 @@ 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_strtou16(const char *str, uint16_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);
index de0be759651beead0b90ff56f96e32b011e13bbf..2316ada11fd7eee6f7f6957aa6d06302b396872b 100644 (file)
@@ -393,6 +393,19 @@ int ul_strtou32(const char *str, uint32_t *num, int base)
        return rc;
 }
 
+int ul_strtou16(const char *str, uint16_t *num, int base)
+{
+       uint64_t tmp;
+       int rc;
+
+       rc = ul_strtou64(str, &tmp, base);
+       if (rc == 0 && tmp > UINT16_MAX)
+               rc = -(errno = ERANGE);
+       if (rc == 0)
+               *num = (uint16_t) tmp;
+       return rc;
+}
+
 /*
  * Convert strings to numbers in defined range and print message on error.
  *