diff options
Diffstat (limited to 'tools/lib/string.c')
| -rw-r--r-- | tools/lib/string.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/tools/lib/string.c b/tools/lib/string.c new file mode 100644 index 000000000000..065e54f42d8f --- /dev/null +++ b/tools/lib/string.c | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | /* | ||
| 2 | * linux/tools/lib/string.c | ||
| 3 | * | ||
| 4 | * Copied from linux/lib/string.c, where it is: | ||
| 5 | * | ||
| 6 | * Copyright (C) 1991, 1992 Linus Torvalds | ||
| 7 | * | ||
| 8 | * More specifically, the first copied function was strtobool, which | ||
| 9 | * was introduced by: | ||
| 10 | * | ||
| 11 | * d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents") | ||
| 12 | * Author: Jonathan Cameron <jic23@cam.ac.uk> | ||
| 13 | */ | ||
| 14 | |||
| 15 | #include <stdlib.h> | ||
| 16 | #include <string.h> | ||
| 17 | #include <errno.h> | ||
| 18 | #include <linux/string.h> | ||
| 19 | |||
| 20 | /** | ||
| 21 | * memdup - duplicate region of memory | ||
| 22 | * | ||
| 23 | * @src: memory region to duplicate | ||
| 24 | * @len: memory region length | ||
| 25 | */ | ||
| 26 | void *memdup(const void *src, size_t len) | ||
| 27 | { | ||
| 28 | void *p = malloc(len); | ||
| 29 | |||
| 30 | if (p) | ||
| 31 | memcpy(p, src, len); | ||
| 32 | |||
| 33 | return p; | ||
| 34 | } | ||
| 35 | |||
| 36 | /** | ||
| 37 | * strtobool - convert common user inputs into boolean values | ||
| 38 | * @s: input string | ||
| 39 | * @res: result | ||
| 40 | * | ||
| 41 | * This routine returns 0 iff the first character is one of 'Yy1Nn0'. | ||
| 42 | * Otherwise it will return -EINVAL. Value pointed to by res is | ||
| 43 | * updated upon finding a match. | ||
| 44 | */ | ||
| 45 | int strtobool(const char *s, bool *res) | ||
| 46 | { | ||
| 47 | switch (s[0]) { | ||
| 48 | case 'y': | ||
| 49 | case 'Y': | ||
| 50 | case '1': | ||
| 51 | *res = true; | ||
| 52 | break; | ||
| 53 | case 'n': | ||
| 54 | case 'N': | ||
| 55 | case '0': | ||
| 56 | *res = false; | ||
| 57 | break; | ||
| 58 | default: | ||
| 59 | return -EINVAL; | ||
| 60 | } | ||
| 61 | return 0; | ||
| 62 | } | ||
