From: Hauke Mehrtens Date: Sun, 27 Oct 2013 14:12:08 +0000 (+0100) Subject: backports: add strtobool() X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=d255d0490455dfb184963ab49d74366170a50407;p=openwrt%2Fstaging%2Fblogic.git backports: add strtobool() This was added in the following upstream commit: commit d0f1fed29e6e73d9d17f4c91a5896a4ce3938d45 Author: Jonathan Cameron Date: Tue Apr 19 12:43:45 2011 +0100 Add a strtobool function matching semantics of existing in kernel equivalents Signed-off-by: Hauke Mehrtens --- diff --git a/backport/backport-include/linux/string.h b/backport/backport-include/linux/string.h index 819d1414413e..f9f175059f85 100644 --- a/backport/backport-include/linux/string.h +++ b/backport/backport-include/linux/string.h @@ -8,4 +8,9 @@ extern size_t memweight(const void *ptr, size_t bytes); #endif +#if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0) +#define strtobool LINUX_BACKPORT(strtobool) +extern int strtobool(const char *s, bool *res); +#endif + #endif /* __BACKPORT_LINUX_STRING_H */ diff --git a/backport/compat/compat-3.0.c b/backport/compat/compat-3.0.c index 9841019feffa..1bed6a6afab6 100644 --- a/backport/compat/compat-3.0.c +++ b/backport/compat/compat-3.0.c @@ -60,3 +60,32 @@ kstrto_from_user(kstrtou16_from_user, kstrtou16, u16); kstrto_from_user(kstrtos16_from_user, kstrtos16, s16); kstrto_from_user(kstrtou8_from_user, kstrtou8, u8); kstrto_from_user(kstrtos8_from_user, kstrtos8, s8); + +/** + * strtobool - convert common user inputs into boolean values + * @s: input string + * @res: result + * + * This routine returns 0 iff the first character is one of 'Yy1Nn0'. + * Otherwise it will return -EINVAL. Value pointed to by res is + * updated upon finding a match. + */ +int strtobool(const char *s, bool *res) +{ + switch (s[0]) { + case 'y': + case 'Y': + case '1': + *res = true; + break; + case 'n': + case 'N': + case '0': + *res = false; + break; + default: + return -EINVAL; + } + return 0; +} +EXPORT_SYMBOL_GPL(strtobool);