diff options
author | Kees Cook <keescook@chromium.org> | 2016-03-17 17:22:50 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2016-03-17 18:09:34 -0400 |
commit | ef951599074ba4fad2d0efa0a977129b41e6d203 (patch) | |
tree | dcbd4d37ac7bb455efe788b9df90d24632bbfa25 /lib/kstrtox.c | |
parent | e3bde9568d992c5f985e6e30731a5f9f9bef7b13 (diff) |
lib: move strtobool() to kstrtobool()
Create the kstrtobool_from_user() helper and move strtobool() logic into
the new kstrtobool() (matching all the other kstrto* functions).
Provides an inline wrapper for existing strtobool() callers.
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Joe Perches <joe@perches.com>
Cc: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Amitkumar Karwar <akarwar@marvell.com>
Cc: Nishant Sarmukadam <nishants@marvell.com>
Cc: Kalle Valo <kvalo@codeaurora.org>
Cc: Steve French <sfrench@samba.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib/kstrtox.c')
-rw-r--r-- | lib/kstrtox.c | 50 |
1 files changed, 50 insertions, 0 deletions
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 | { \ |