Wang Nan | 7d85c43 | 2015-11-16 11:42:05 -0300 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 4ddd327 | 2015-11-16 11:36:29 -0300 | [diff] [blame] | 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
Wang Nan | 7d85c43 | 2015-11-16 11:42:05 -0300 | [diff] [blame] | 17 | #include <errno.h> |
Arnaldo Carvalho de Melo | 4ddd327 | 2015-11-16 11:36:29 -0300 | [diff] [blame] | 18 | #include <linux/string.h> |
Josh Poimboeuf | ce99091 | 2015-12-15 09:39:33 -0600 | [diff] [blame] | 19 | #include <linux/compiler.h> |
Arnaldo Carvalho de Melo | 4ddd327 | 2015-11-16 11:36:29 -0300 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * memdup - duplicate region of memory |
| 23 | * |
| 24 | * @src: memory region to duplicate |
| 25 | * @len: memory region length |
| 26 | */ |
| 27 | void *memdup(const void *src, size_t len) |
| 28 | { |
| 29 | void *p = malloc(len); |
| 30 | |
| 31 | if (p) |
| 32 | memcpy(p, src, len); |
| 33 | |
| 34 | return p; |
| 35 | } |
Wang Nan | 7d85c43 | 2015-11-16 11:42:05 -0300 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * strtobool - convert common user inputs into boolean values |
| 39 | * @s: input string |
| 40 | * @res: result |
| 41 | * |
Arnaldo Carvalho de Melo | b99e485 | 2017-07-20 15:35:33 -0300 | [diff] [blame] | 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. |
Wang Nan | 7d85c43 | 2015-11-16 11:42:05 -0300 | [diff] [blame] | 45 | */ |
| 46 | int strtobool(const char *s, bool *res) |
| 47 | { |
Arnaldo Carvalho de Melo | b99e485 | 2017-07-20 15:35:33 -0300 | [diff] [blame] | 48 | if (!s) |
| 49 | return -EINVAL; |
| 50 | |
Wang Nan | 7d85c43 | 2015-11-16 11:42:05 -0300 | [diff] [blame] | 51 | switch (s[0]) { |
| 52 | case 'y': |
| 53 | case 'Y': |
| 54 | case '1': |
| 55 | *res = true; |
Arnaldo Carvalho de Melo | b99e485 | 2017-07-20 15:35:33 -0300 | [diff] [blame] | 56 | return 0; |
Wang Nan | 7d85c43 | 2015-11-16 11:42:05 -0300 | [diff] [blame] | 57 | case 'n': |
| 58 | case 'N': |
| 59 | case '0': |
| 60 | *res = false; |
Arnaldo Carvalho de Melo | b99e485 | 2017-07-20 15:35:33 -0300 | [diff] [blame] | 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 | } |
Wang Nan | 7d85c43 | 2015-11-16 11:42:05 -0300 | [diff] [blame] | 76 | default: |
Arnaldo Carvalho de Melo | b99e485 | 2017-07-20 15:35:33 -0300 | [diff] [blame] | 77 | break; |
Wang Nan | 7d85c43 | 2015-11-16 11:42:05 -0300 | [diff] [blame] | 78 | } |
Arnaldo Carvalho de Melo | b99e485 | 2017-07-20 15:35:33 -0300 | [diff] [blame] | 79 | |
| 80 | return -EINVAL; |
Wang Nan | 7d85c43 | 2015-11-16 11:42:05 -0300 | [diff] [blame] | 81 | } |
Josh Poimboeuf | ce99091 | 2015-12-15 09:39:33 -0600 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * strlcpy - Copy a C-string into a sized buffer |
| 85 | * @dest: Where to copy the string to |
| 86 | * @src: Where to copy the string from |
| 87 | * @size: size of destination buffer |
| 88 | * |
| 89 | * Compatible with *BSD: the result is always a valid |
| 90 | * NUL-terminated string that fits in the buffer (unless, |
| 91 | * of course, the buffer size is zero). It does not pad |
| 92 | * out the result like strncpy() does. |
| 93 | * |
| 94 | * If libc has strlcpy() then that version will override this |
| 95 | * implementation: |
| 96 | */ |
| 97 | size_t __weak strlcpy(char *dest, const char *src, size_t size) |
| 98 | { |
| 99 | size_t ret = strlen(src); |
| 100 | |
| 101 | if (size) { |
| 102 | size_t len = (ret >= size) ? size - 1 : ret; |
| 103 | memcpy(dest, src, len); |
| 104 | dest[len] = '\0'; |
| 105 | } |
| 106 | return ret; |
| 107 | } |