Skip to content

Commit

Permalink
Add a strtobool function matching semantics of existing in kernel equ…
Browse files Browse the repository at this point in the history
…ivalents

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 <[email protected]>
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
Jonathan Cameron authored and rustyrussell committed May 19, 2011
1 parent 6845756 commit d0f1fed
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/linux/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
extern void argv_free(char **argv);

extern bool sysfs_streq(const char *s1, const char *s2);
extern int strtobool(const char *s, bool *res);

#ifdef CONFIG_BINARY_PRINTF
int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
Expand Down
29 changes: 29 additions & 0 deletions lib/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,35 @@ bool sysfs_streq(const char *s1, const char *s2)
}
EXPORT_SYMBOL(sysfs_streq);

/**
* 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(strtobool);

#ifndef __HAVE_ARCH_MEMSET
/**
* memset - Fill a region of memory with the given value
Expand Down

0 comments on commit d0f1fed

Please sign in to comment.