diff options
author | Hauke Mehrtens <hauke@hauke-m.de> | 2013-10-27 15:12:08 +0100 |
---|---|---|
committer | Hauke Mehrtens <hauke@hauke-m.de> | 2013-10-27 18:39:18 +0100 |
commit | d255d0490455dfb184963ab49d74366170a50407 (patch) | |
tree | 9f3609997230e4ca67febfeef1b1b957e5fddf18 | |
parent | 3db1becf5420fa80e27386390ec36896dd50015d (diff) |
backports: add strtobool()
This was added in the following upstream commit:
commit d0f1fed29e6e73d9d17f4c91a5896a4ce3938d45
Author: Jonathan Cameron <jic23@cam.ac.uk>
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 <hauke@hauke-m.de>
-rw-r--r-- | backport/backport-include/linux/string.h | 5 | ||||
-rw-r--r-- | backport/compat/compat-3.0.c | 29 |
2 files changed, 34 insertions, 0 deletions
diff --git a/backport/backport-include/linux/string.h b/backport/backport-include/linux/string.h index 819d1414..f9f17505 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 9841019f..1bed6a6a 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); |