diff options
| -rw-r--r-- | include/linux/kernel.h | 2 | ||||
| -rw-r--r-- | include/linux/string.h | 6 | ||||
| -rw-r--r-- | lib/kstrtox.c | 50 | ||||
| -rw-r--r-- | lib/string.c | 29 |
4 files changed, 57 insertions, 30 deletions
diff --git a/include/linux/kernel.h b/include/linux/kernel.h index f31638c6e873..f4fa2b29c38c 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h | |||
| @@ -357,6 +357,7 @@ int __must_check kstrtou16(const char *s, unsigned int base, u16 *res); | |||
| 357 | int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); | 357 | int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); |
| 358 | int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); | 358 | int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); |
| 359 | int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); | 359 | int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); |
| 360 | int __must_check kstrtobool(const char *s, bool *res); | ||
| 360 | 361 | ||
| 361 | int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); | 362 | int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); |
| 362 | int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); | 363 | int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); |
| @@ -368,6 +369,7 @@ int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigne | |||
| 368 | int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); | 369 | int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); |
| 369 | int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); | 370 | int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); |
| 370 | int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); | 371 | int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); |
| 372 | int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res); | ||
| 371 | 373 | ||
| 372 | static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) | 374 | static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) |
| 373 | { | 375 | { |
diff --git a/include/linux/string.h b/include/linux/string.h index 0f235e80d355..d3993a79a325 100644 --- a/include/linux/string.h +++ b/include/linux/string.h | |||
| @@ -128,7 +128,11 @@ extern char **argv_split(gfp_t gfp, const char *str, int *argcp); | |||
| 128 | extern void argv_free(char **argv); | 128 | extern void argv_free(char **argv); |
| 129 | 129 | ||
| 130 | extern bool sysfs_streq(const char *s1, const char *s2); | 130 | extern bool sysfs_streq(const char *s1, const char *s2); |
| 131 | extern int strtobool(const char *s, bool *res); | 131 | extern int kstrtobool(const char *s, bool *res); |
| 132 | static inline int strtobool(const char *s, bool *res) | ||
| 133 | { | ||
| 134 | return kstrtobool(s, res); | ||
| 135 | } | ||
| 132 | 136 | ||
| 133 | int match_string(const char * const *array, size_t n, const char *string); | 137 | int match_string(const char * const *array, size_t n, const char *string); |
| 134 | 138 | ||
diff --git a/lib/kstrtox.c b/lib/kstrtox.c index 94be244e8441..e8ba4a013e82 100644 --- a/lib/kstrtox.c +++ b/lib/kstrtox.c | |||
| @@ -321,6 +321,56 @@ int kstrtos8(const char *s, unsigned int base, s8 *res) | |||
| 321 | } | 321 | } |
| 322 | EXPORT_SYMBOL(kstrtos8); | 322 | EXPORT_SYMBOL(kstrtos8); |
| 323 | 323 | ||
| 324 | /** | ||
| 325 | * kstrtobool - convert common user inputs into boolean values | ||
| 326 | * @s: input string | ||
| 327 | * @res: result | ||
| 328 | * | ||
| 329 | * This routine returns 0 iff the first character is one of 'Yy1Nn0'. | ||
| 330 | * Otherwise it will return -EINVAL. Value pointed to by res is | ||
| 331 | * updated upon finding a match. | ||
| 332 | */ | ||
| 333 | int kstrtobool(const char *s, bool *res) | ||
| 334 | { | ||
| 335 | if (!s) | ||
| 336 | return -EINVAL; | ||
| 337 | |||
| 338 | switch (s[0]) { | ||
| 339 | case 'y': | ||
| 340 | case 'Y': | ||
| 341 | case '1': | ||
| 342 | *res = true; | ||
| 343 | return 0; | ||
| 344 | case 'n': | ||
| 345 | case 'N': | ||
| 346 | case '0': | ||
| 347 | *res = false; | ||
| 348 | return 0; | ||
| 349 | default: | ||
| 350 | break; | ||
| 351 | } | ||
| 352 | |||
| 353 | return -EINVAL; | ||
| 354 | } | ||
| 355 | EXPORT_SYMBOL(kstrtobool); | ||
| 356 | |||
| 357 | /* | ||
| 358 | * Since "base" would be a nonsense argument, this open-codes the | ||
| 359 | * _from_user helper instead of using the helper macro below. | ||
| 360 | */ | ||
| 361 | int kstrtobool_from_user(const char __user *s, size_t count, bool *res) | ||
| 362 | { | ||
| 363 | /* Longest string needed to differentiate, newline, terminator */ | ||
| 364 | char buf[4]; | ||
| 365 | |||
| 366 | count = min(count, sizeof(buf) - 1); | ||
| 367 | if (copy_from_user(buf, s, count)) | ||
| 368 | return -EFAULT; | ||
| 369 | buf[count] = '\0'; | ||
| 370 | return kstrtobool(buf, res); | ||
| 371 | } | ||
| 372 | EXPORT_SYMBOL(kstrtobool_from_user); | ||
| 373 | |||
| 324 | #define kstrto_from_user(f, g, type) \ | 374 | #define kstrto_from_user(f, g, type) \ |
| 325 | int f(const char __user *s, size_t count, unsigned int base, type *res) \ | 375 | int f(const char __user *s, size_t count, unsigned int base, type *res) \ |
| 326 | { \ | 376 | { \ |
diff --git a/lib/string.c b/lib/string.c index e9c9db161d2c..ed83562a53ae 100644 --- a/lib/string.c +++ b/lib/string.c | |||
| @@ -656,35 +656,6 @@ int match_string(const char * const *array, size_t n, const char *string) | |||
| 656 | } | 656 | } |
| 657 | EXPORT_SYMBOL(match_string); | 657 | EXPORT_SYMBOL(match_string); |
| 658 | 658 | ||
| 659 | /** | ||
| 660 | * strtobool - convert common user inputs into boolean values | ||
| 661 | * @s: input string | ||
| 662 | * @res: result | ||
| 663 | * | ||
| 664 | * This routine returns 0 iff the first character is one of 'Yy1Nn0'. | ||
| 665 | * Otherwise it will return -EINVAL. Value pointed to by res is | ||
| 666 | * updated upon finding a match. | ||
| 667 | */ | ||
| 668 | int strtobool(const char *s, bool *res) | ||
| 669 | { | ||
| 670 | switch (s[0]) { | ||
| 671 | case 'y': | ||
| 672 | case 'Y': | ||
| 673 | case '1': | ||
| 674 | *res = true; | ||
| 675 | break; | ||
| 676 | case 'n': | ||
| 677 | case 'N': | ||
| 678 | case '0': | ||
| 679 | *res = false; | ||
| 680 | break; | ||
| 681 | default: | ||
| 682 | return -EINVAL; | ||
| 683 | } | ||
| 684 | return 0; | ||
| 685 | } | ||
| 686 | EXPORT_SYMBOL(strtobool); | ||
| 687 | |||
| 688 | #ifndef __HAVE_ARCH_MEMSET | 659 | #ifndef __HAVE_ARCH_MEMSET |
| 689 | /** | 660 | /** |
| 690 | * memset - Fill a region of memory with the given value | 661 | * memset - Fill a region of memory with the given value |
