Commit d0f1fed29e6e73d9d17f4c91a5896a4ce3938d45
Committed by
Rusty Russell
1 parent
6845756b29
Exists in
master
and in
20 other branches
Add a strtobool function matching semantics of existing in kernel equivalents
This is a rename of the usr_strtobool proposal, which was a renamed, relocated and fixed version of previous kstrtobool RFC Signed-off-by: Jonathan Cameron <jic23@cam.ac.uk> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Showing 2 changed files with 30 additions and 0 deletions Side-by-side Diff
include/linux/string.h
... | ... | @@ -123,6 +123,7 @@ |
123 | 123 | extern void argv_free(char **argv); |
124 | 124 | |
125 | 125 | extern bool sysfs_streq(const char *s1, const char *s2); |
126 | +extern int strtobool(const char *s, bool *res); | |
126 | 127 | |
127 | 128 | #ifdef CONFIG_BINARY_PRINTF |
128 | 129 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args); |
lib/string.c
... | ... | @@ -535,6 +535,35 @@ |
535 | 535 | } |
536 | 536 | EXPORT_SYMBOL(sysfs_streq); |
537 | 537 | |
538 | +/** | |
539 | + * strtobool - convert common user inputs into boolean values | |
540 | + * @s: input string | |
541 | + * @res: result | |
542 | + * | |
543 | + * This routine returns 0 iff the first character is one of 'Yy1Nn0'. | |
544 | + * Otherwise it will return -EINVAL. Value pointed to by res is | |
545 | + * updated upon finding a match. | |
546 | + */ | |
547 | +int strtobool(const char *s, bool *res) | |
548 | +{ | |
549 | + switch (s[0]) { | |
550 | + case 'y': | |
551 | + case 'Y': | |
552 | + case '1': | |
553 | + *res = true; | |
554 | + break; | |
555 | + case 'n': | |
556 | + case 'N': | |
557 | + case '0': | |
558 | + *res = false; | |
559 | + break; | |
560 | + default: | |
561 | + return -EINVAL; | |
562 | + } | |
563 | + return 0; | |
564 | +} | |
565 | +EXPORT_SYMBOL(strtobool); | |
566 | + | |
538 | 567 | #ifndef __HAVE_ARCH_MEMSET |
539 | 568 | /** |
540 | 569 | * memset - Fill a region of memory with the given value |