aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/linux/kernel.h2
-rw-r--r--include/linux/string.h6
-rw-r--r--lib/kstrtox.c50
-rw-r--r--lib/string.c29
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);
357int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); 357int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
358int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); 358int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
359int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); 359int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
360int __must_check kstrtobool(const char *s, bool *res);
360 361
361int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); 362int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
362int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); 363int __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
368int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); 369int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
369int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); 370int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
370int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); 371int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
372int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res);
371 373
372static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) 374static 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);
128extern void argv_free(char **argv); 128extern void argv_free(char **argv);
129 129
130extern bool sysfs_streq(const char *s1, const char *s2); 130extern bool sysfs_streq(const char *s1, const char *s2);
131extern int strtobool(const char *s, bool *res); 131extern int kstrtobool(const char *s, bool *res);
132static inline int strtobool(const char *s, bool *res)
133{
134 return kstrtobool(s, res);
135}
132 136
133int match_string(const char * const *array, size_t n, const char *string); 137int 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}
322EXPORT_SYMBOL(kstrtos8); 322EXPORT_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 */
333int 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}
355EXPORT_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 */
361int 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}
372EXPORT_SYMBOL(kstrtobool_from_user);
373
324#define kstrto_from_user(f, g, type) \ 374#define kstrto_from_user(f, g, type) \
325int f(const char __user *s, size_t count, unsigned int base, type *res) \ 375int 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}
657EXPORT_SYMBOL(match_string); 657EXPORT_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 */
668int 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}
686EXPORT_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