string: Introduce strtobool
We have at least two places which convert a string to a boolean type, so create a common function for this. strtobool treats - any positive (nonzero) number as true - "0" as false - "true" (case insensitive) as true - "false" (case insensitive) as false Every other value results in an error and the input *val is not modified. The caller is expected to initialize *val with the correct default before calling strtobool. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
parent
941056dad1
commit
22b8e9415d
|
@ -4,5 +4,6 @@
|
|||
#include <linux/string.h>
|
||||
|
||||
void *memdup(const void *, size_t);
|
||||
int strtobool(const char *str, int *val);
|
||||
|
||||
#endif /* __STRING_H */
|
||||
|
|
43
lib/string.c
43
lib/string.c
|
@ -739,3 +739,46 @@ void *memdup(const void *orig, size_t size)
|
|||
return buf;
|
||||
}
|
||||
EXPORT_SYMBOL(memdup);
|
||||
|
||||
/**
|
||||
* strtobool - convert a string to a boolean value
|
||||
* @str - The string
|
||||
* @val - The boolean value returned.
|
||||
*
|
||||
* This function treats
|
||||
* - any positive (nonzero) number as true
|
||||
* - "0" as false
|
||||
* - "true" (case insensitive) as true
|
||||
* - "false" (case insensitive) as false
|
||||
*
|
||||
* Every other value results in an error and the @val is not
|
||||
* modified. The caller is expected to initialize @val with the
|
||||
* correct default before calling strtobool.
|
||||
*
|
||||
* Returns 0 for success or negative error code if the variable does
|
||||
* not exist or contains something this function does not recognize
|
||||
* as true or false.
|
||||
*/
|
||||
int strtobool(const char *str, int *val)
|
||||
{
|
||||
if (!str || !*str)
|
||||
return -EINVAL;
|
||||
|
||||
if (simple_strtoul(str, NULL, 0) > 0) {
|
||||
*val = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!strcmp(str, "0") || !strcasecmp(str, "false")) {
|
||||
*val = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!strcasecmp(str, "true")) {
|
||||
*val = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -EINVAL;
|
||||
}
|
||||
EXPORT_SYMBOL(strtobool);
|
||||
|
|
Loading…
Reference in New Issue