Skip to content

Commit b99e485

Browse files
committed
tools lib: Update copy of strtobool from the kernel sources
Getting support for "on", "off" introduced in a81a5a1 ("lib: add "on"/"off" support to kstrtobool") and making it check for NULL, introduced in ef95159 ("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>
1 parent 8e99b6d commit b99e485

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

‎tools/lib/string.c‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,45 @@ void *memdup(const void *src, size_t len)
3939
* @s: input string
4040
* @res: result
4141
*
42-
* This routine returns 0 iff the first character is one of 'Yy1Nn0'.
43-
* Otherwise it will return -EINVAL. Value pointed to by res is
44-
* updated upon finding a match.
42+
* This routine returns 0 iff the first character is one of 'Yy1Nn0', or
43+
* [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value
44+
* pointed to by res is updated upon finding a match.
4545
*/
4646
int strtobool(const char *s, bool *res)
4747
{
48+
if (!s)
49+
return -EINVAL;
50+
4851
switch (s[0]) {
4952
case 'y':
5053
case 'Y':
5154
case '1':
5255
*res = true;
53-
break;
56+
return 0;
5457
case 'n':
5558
case 'N':
5659
case '0':
5760
*res = false;
58-
break;
61+
return 0;
62+
case 'o':
63+
case 'O':
64+
switch (s[1]) {
65+
case 'n':
66+
case 'N':
67+
*res = true;
68+
return 0;
69+
case 'f':
70+
case 'F':
71+
*res = false;
72+
return 0;
73+
default:
74+
break;
75+
}
5976
default:
60-
return -EINVAL;
77+
break;
6178
}
62-
return 0;
79+
80+
return -EINVAL;
6381
}
6482

6583
/**

0 commit comments

Comments
 (0)