linuxkpi: Add kstrtou16

This function convert a char * to a u16.
Simply use strtoul and cast to compare for ERANGE

Sponsored-by: The FreeBSD Foundation
Reviewed by:	hselasky
Differential Revision:	https://reviews.freebsd.org/D24996
This commit is contained in:
Emmanuel Vadot 2020-05-27 11:42:09 +00:00
parent 9b703f3082
commit 8e52f22b25

View File

@ -373,6 +373,24 @@ kstrtouint(const char *cp, unsigned int base, unsigned int *res)
return (0);
}
static inline int
kstrtou16(const char *cp, unsigned int base, u16 *res)
{
char *end;
unsigned long temp;
*res = temp = strtoul(cp, &end, base);
/* skip newline character, if any */
if (*end == '\n')
end++;
if (*cp == 0 || *end != 0)
return (-EINVAL);
if (temp != (u16)temp)
return (-ERANGE);
return (0);
}
static inline int
kstrtou32(const char *cp, unsigned int base, u32 *res)
{