1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * linux/tools/lib/string.c 3 * linux/tools/lib/string.c 4 * 4 * 5 * Copied from linux/lib/string.c, where it i 5 * Copied from linux/lib/string.c, where it is: 6 * 6 * 7 * Copyright (C) 1991, 1992 Linus Torvalds 7 * Copyright (C) 1991, 1992 Linus Torvalds 8 * 8 * 9 * More specifically, the first copied functi 9 * More specifically, the first copied function was strtobool, which 10 * was introduced by: 10 * was introduced by: 11 * 11 * 12 * d0f1fed29e6e ("Add a strtobool function ma 12 * d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents") 13 * Author: Jonathan Cameron <jic23@cam.ac.uk> 13 * Author: Jonathan Cameron <jic23@cam.ac.uk> 14 */ 14 */ 15 15 16 #include <stdlib.h> 16 #include <stdlib.h> 17 #include <string.h> 17 #include <string.h> 18 #include <errno.h> 18 #include <errno.h> 19 #include <linux/string.h> 19 #include <linux/string.h> 20 #include <linux/ctype.h> 20 #include <linux/ctype.h> 21 #include <linux/compiler.h> 21 #include <linux/compiler.h> 22 22 23 /** 23 /** 24 * memdup - duplicate region of memory 24 * memdup - duplicate region of memory 25 * 25 * 26 * @src: memory region to duplicate 26 * @src: memory region to duplicate 27 * @len: memory region length 27 * @len: memory region length 28 */ 28 */ 29 void *memdup(const void *src, size_t len) 29 void *memdup(const void *src, size_t len) 30 { 30 { 31 void *p = malloc(len); 31 void *p = malloc(len); 32 32 33 if (p) 33 if (p) 34 memcpy(p, src, len); 34 memcpy(p, src, len); 35 35 36 return p; 36 return p; 37 } 37 } 38 38 39 /** 39 /** 40 * strtobool - convert common user inputs into 40 * strtobool - convert common user inputs into boolean values 41 * @s: input string 41 * @s: input string 42 * @res: result 42 * @res: result 43 * 43 * 44 * This routine returns 0 iff the first charac 44 * This routine returns 0 iff the first character is one of 'Yy1Nn0', or 45 * [oO][NnFf] for "on" and "off". Otherwise it 45 * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value 46 * pointed to by res is updated upon finding a 46 * pointed to by res is updated upon finding a match. 47 */ 47 */ 48 int strtobool(const char *s, bool *res) 48 int strtobool(const char *s, bool *res) 49 { 49 { 50 if (!s) 50 if (!s) 51 return -EINVAL; 51 return -EINVAL; 52 52 53 switch (s[0]) { 53 switch (s[0]) { 54 case 'y': 54 case 'y': 55 case 'Y': 55 case 'Y': 56 case '1': 56 case '1': 57 *res = true; 57 *res = true; 58 return 0; 58 return 0; 59 case 'n': 59 case 'n': 60 case 'N': 60 case 'N': 61 case '': 61 case '': 62 *res = false; 62 *res = false; 63 return 0; 63 return 0; 64 case 'o': 64 case 'o': 65 case 'O': 65 case 'O': 66 switch (s[1]) { 66 switch (s[1]) { 67 case 'n': 67 case 'n': 68 case 'N': 68 case 'N': 69 *res = true; 69 *res = true; 70 return 0; 70 return 0; 71 case 'f': 71 case 'f': 72 case 'F': 72 case 'F': 73 *res = false; 73 *res = false; 74 return 0; 74 return 0; 75 default: 75 default: 76 break; 76 break; 77 } 77 } 78 default: 78 default: 79 break; 79 break; 80 } 80 } 81 81 82 return -EINVAL; 82 return -EINVAL; 83 } 83 } 84 84 85 /** 85 /** 86 * strlcpy - Copy a C-string into a sized buff 86 * strlcpy - Copy a C-string into a sized buffer 87 * @dest: Where to copy the string to 87 * @dest: Where to copy the string to 88 * @src: Where to copy the string from 88 * @src: Where to copy the string from 89 * @size: size of destination buffer 89 * @size: size of destination buffer 90 * 90 * 91 * Compatible with *BSD: the result is always 91 * Compatible with *BSD: the result is always a valid 92 * NUL-terminated string that fits in the buff 92 * NUL-terminated string that fits in the buffer (unless, 93 * of course, the buffer size is zero). It doe 93 * of course, the buffer size is zero). It does not pad 94 * out the result like strncpy() does. 94 * out the result like strncpy() does. 95 * 95 * 96 * If libc has strlcpy() then that version wil 96 * If libc has strlcpy() then that version will override this 97 * implementation: 97 * implementation: 98 */ 98 */ 99 #ifdef __clang__ 99 #ifdef __clang__ 100 #pragma clang diagnostic push 100 #pragma clang diagnostic push 101 #pragma clang diagnostic ignored "-Wignored-at 101 #pragma clang diagnostic ignored "-Wignored-attributes" 102 #endif 102 #endif 103 size_t __weak strlcpy(char *dest, const char * 103 size_t __weak strlcpy(char *dest, const char *src, size_t size) 104 { 104 { 105 size_t ret = strlen(src); 105 size_t ret = strlen(src); 106 106 107 if (size) { 107 if (size) { 108 size_t len = (ret >= size) ? s 108 size_t len = (ret >= size) ? size - 1 : ret; 109 memcpy(dest, src, len); 109 memcpy(dest, src, len); 110 dest[len] = '\0'; 110 dest[len] = '\0'; 111 } 111 } 112 return ret; 112 return ret; 113 } 113 } 114 #ifdef __clang__ 114 #ifdef __clang__ 115 #pragma clang diagnostic pop 115 #pragma clang diagnostic pop 116 #endif 116 #endif 117 117 118 /** 118 /** 119 * skip_spaces - Removes leading whitespace fr 119 * skip_spaces - Removes leading whitespace from @str. 120 * @str: The string to be stripped. 120 * @str: The string to be stripped. 121 * 121 * 122 * Returns a pointer to the first non-whitespa 122 * Returns a pointer to the first non-whitespace character in @str. 123 */ 123 */ 124 char *skip_spaces(const char *str) 124 char *skip_spaces(const char *str) 125 { 125 { 126 while (isspace(*str)) 126 while (isspace(*str)) 127 ++str; 127 ++str; 128 return (char *)str; 128 return (char *)str; 129 } 129 } 130 130 131 /** 131 /** 132 * strim - Removes leading and trailing whites 132 * strim - Removes leading and trailing whitespace from @s. 133 * @s: The string to be stripped. 133 * @s: The string to be stripped. 134 * 134 * 135 * Note that the first trailing whitespace is 135 * Note that the first trailing whitespace is replaced with a %NUL-terminator 136 * in the given string @s. Returns a pointer t 136 * in the given string @s. Returns a pointer to the first non-whitespace 137 * character in @s. 137 * character in @s. 138 */ 138 */ 139 char *strim(char *s) 139 char *strim(char *s) 140 { 140 { 141 size_t size; 141 size_t size; 142 char *end; 142 char *end; 143 143 144 size = strlen(s); 144 size = strlen(s); 145 if (!size) 145 if (!size) 146 return s; 146 return s; 147 147 148 end = s + size - 1; 148 end = s + size - 1; 149 while (end >= s && isspace(*end)) 149 while (end >= s && isspace(*end)) 150 end--; 150 end--; 151 *(end + 1) = '\0'; 151 *(end + 1) = '\0'; 152 152 153 return skip_spaces(s); 153 return skip_spaces(s); 154 } 154 } 155 155 156 /* << 157 * remove_spaces - Removes whitespaces from @s << 158 */ << 159 void remove_spaces(char *s) << 160 { << 161 char *d = s; << 162 << 163 do { << 164 while (*d == ' ') << 165 ++d; << 166 } while ((*s++ = *d++)); << 167 } << 168 << 169 /** 156 /** 170 * strreplace - Replace all occurrences of cha 157 * strreplace - Replace all occurrences of character in string. 171 * @s: The string to operate on. 158 * @s: The string to operate on. 172 * @old: The character being replaced. 159 * @old: The character being replaced. 173 * @new: The character @old is replaced with. 160 * @new: The character @old is replaced with. 174 * 161 * 175 * Returns pointer to the nul byte at the end 162 * Returns pointer to the nul byte at the end of @s. 176 */ 163 */ 177 char *strreplace(char *s, char old, char new) 164 char *strreplace(char *s, char old, char new) 178 { 165 { 179 for (; *s; ++s) 166 for (; *s; ++s) 180 if (*s == old) 167 if (*s == old) 181 *s = new; 168 *s = new; 182 return s; 169 return s; 183 } 170 } 184 171 185 static void *check_bytes8(const u8 *start, u8 172 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes) 186 { 173 { 187 while (bytes) { 174 while (bytes) { 188 if (*start != value) 175 if (*start != value) 189 return (void *)start; 176 return (void *)start; 190 start++; 177 start++; 191 bytes--; 178 bytes--; 192 } 179 } 193 return NULL; 180 return NULL; 194 } 181 } 195 182 196 /** 183 /** 197 * memchr_inv - Find an unmatching character i 184 * memchr_inv - Find an unmatching character in an area of memory. 198 * @start: The memory area 185 * @start: The memory area 199 * @c: Find a character other than c 186 * @c: Find a character other than c 200 * @bytes: The size of the area. 187 * @bytes: The size of the area. 201 * 188 * 202 * returns the address of the first character 189 * returns the address of the first character other than @c, or %NULL 203 * if the whole buffer contains just @c. 190 * if the whole buffer contains just @c. 204 */ 191 */ 205 void *memchr_inv(const void *start, int c, siz 192 void *memchr_inv(const void *start, int c, size_t bytes) 206 { 193 { 207 u8 value = c; 194 u8 value = c; 208 u64 value64; 195 u64 value64; 209 unsigned int words, prefix; 196 unsigned int words, prefix; 210 197 211 if (bytes <= 16) 198 if (bytes <= 16) 212 return check_bytes8(start, val 199 return check_bytes8(start, value, bytes); 213 200 214 value64 = value; 201 value64 = value; 215 value64 |= value64 << 8; 202 value64 |= value64 << 8; 216 value64 |= value64 << 16; 203 value64 |= value64 << 16; 217 value64 |= value64 << 32; 204 value64 |= value64 << 32; 218 205 219 prefix = (unsigned long)start % 8; 206 prefix = (unsigned long)start % 8; 220 if (prefix) { 207 if (prefix) { 221 u8 *r; 208 u8 *r; 222 209 223 prefix = 8 - prefix; 210 prefix = 8 - prefix; 224 r = check_bytes8(start, value, 211 r = check_bytes8(start, value, prefix); 225 if (r) 212 if (r) 226 return r; 213 return r; 227 start += prefix; 214 start += prefix; 228 bytes -= prefix; 215 bytes -= prefix; 229 } 216 } 230 217 231 words = bytes / 8; 218 words = bytes / 8; 232 219 233 while (words) { 220 while (words) { 234 if (*(u64 *)start != value64) 221 if (*(u64 *)start != value64) 235 return check_bytes8(st 222 return check_bytes8(start, value, 8); 236 start += 8; 223 start += 8; 237 words--; 224 words--; 238 } 225 } 239 226 240 return check_bytes8(start, value, byte 227 return check_bytes8(start, value, bytes % 8); 241 } 228 } 242 229
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.