tools lib: Update copy of strtobool from the kernel sources

Getting support for "on", "off" introduced in a81a5a17d4 ("lib: add
"on"/"off" support to kstrtobool") and making it check for NULL,
introduced in ef95159907 ("lib: move strtobool() to kstrtobool()").

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: Kees Cook <keescook@chromium.org>
Link: http://lkml.kernel.org/n/tip-mu8ghin4rklacmmubzwv8td7@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Arnaldo Carvalho de Melo 2017-07-20 15:35:33 -03:00
parent 8e99b6d453
commit b99e4850df
1 changed files with 25 additions and 7 deletions

View File

@ -39,27 +39,45 @@ void *memdup(const void *src, size_t len)
* @s: input string * @s: input string
* @res: result * @res: result
* *
* This routine returns 0 iff the first character is one of 'Yy1Nn0'. * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
* Otherwise it will return -EINVAL. Value pointed to by res is * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value
* updated upon finding a match. * pointed to by res is updated upon finding a match.
*/ */
int strtobool(const char *s, bool *res) int strtobool(const char *s, bool *res)
{ {
if (!s)
return -EINVAL;
switch (s[0]) { switch (s[0]) {
case 'y': case 'y':
case 'Y': case 'Y':
case '1': case '1':
*res = true; *res = true;
break; return 0;
case 'n': case 'n':
case 'N': case 'N':
case '0': case '0':
*res = false; *res = false;
break; return 0;
case 'o':
case 'O':
switch (s[1]) {
case 'n':
case 'N':
*res = true;
return 0;
case 'f':
case 'F':
*res = false;
return 0;
default:
break;
}
default: default:
return -EINVAL; break;
} }
return 0;
return -EINVAL;
} }
/** /**