1 // SPDX-License-Identifier: GPL-2.0 << 2 /* 1 /* 3 * linux/lib/string.c 2 * linux/lib/string.c 4 * 3 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 4 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 5 */ 7 6 8 /* 7 /* 9 * This file should be used only for "library" !! 8 * stupid library routines.. The optimized versions should generally be found 10 * alternative implementations on specific arc !! 9 * as inline code in <asm-xx/string.h> 11 * found in <asm-xx/string.h>), or get overloa << 12 * (Specifically, this file is built with __NO << 13 * 10 * 14 * Other helper functions should live in strin !! 11 * These are buggy as well.. >> 12 * >> 13 * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de> >> 14 * - Added strsep() which will replace strtok() soon (because strsep() is >> 15 * reentrant and should be faster). Use only strsep() in new code, please. >> 16 * >> 17 * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>, >> 18 * Matthew Hawkins <matt@mh.dropbear.id.au> >> 19 * - Kissed strtok() goodbye 15 */ 20 */ 16 21 17 #define __NO_FORTIFY << 18 #include <linux/bits.h> << 19 #include <linux/bug.h> << 20 #include <linux/ctype.h> << 21 #include <linux/errno.h> << 22 #include <linux/limits.h> << 23 #include <linux/linkage.h> << 24 #include <linux/stddef.h> << 25 #include <linux/string.h> << 26 #include <linux/types.h> 22 #include <linux/types.h> >> 23 #include <linux/string.h> >> 24 #include <linux/ctype.h> >> 25 #include <linux/module.h> 27 26 28 #include <asm/page.h> !! 27 #ifndef __HAVE_ARCH_STRNICMP 29 #include <asm/rwonce.h> << 30 #include <linux/unaligned.h> << 31 #include <asm/word-at-a-time.h> << 32 << 33 #ifndef __HAVE_ARCH_STRNCASECMP << 34 /** 28 /** 35 * strncasecmp - Case insensitive, length-limi !! 29 * strnicmp - Case insensitive, length-limited string comparison 36 * @s1: One string 30 * @s1: One string 37 * @s2: The other string 31 * @s2: The other string 38 * @len: the maximum number of characters to c 32 * @len: the maximum number of characters to compare 39 */ 33 */ 40 int strncasecmp(const char *s1, const char *s2 !! 34 int strnicmp(const char *s1, const char *s2, size_t len) 41 { 35 { 42 /* Yes, Virginia, it had better be uns 36 /* Yes, Virginia, it had better be unsigned */ 43 unsigned char c1, c2; 37 unsigned char c1, c2; 44 38 45 if (!len) !! 39 c1 = c2 = 0; 46 return 0; !! 40 if (len) { 47 !! 41 do { 48 do { !! 42 c1 = *s1; 49 c1 = *s1++; !! 43 c2 = *s2; 50 c2 = *s2++; !! 44 s1++; 51 if (!c1 || !c2) !! 45 s2++; 52 break; !! 46 if (!c1) 53 if (c1 == c2) !! 47 break; 54 continue; !! 48 if (!c2) 55 c1 = tolower(c1); !! 49 break; 56 c2 = tolower(c2); !! 50 if (c1 == c2) 57 if (c1 != c2) !! 51 continue; 58 break; !! 52 c1 = tolower(c1); 59 } while (--len); !! 53 c2 = tolower(c2); >> 54 if (c1 != c2) >> 55 break; >> 56 } while (--len); >> 57 } 60 return (int)c1 - (int)c2; 58 return (int)c1 - (int)c2; 61 } 59 } 62 EXPORT_SYMBOL(strncasecmp); !! 60 EXPORT_SYMBOL(strnicmp); 63 #endif 61 #endif 64 62 65 #ifndef __HAVE_ARCH_STRCASECMP 63 #ifndef __HAVE_ARCH_STRCASECMP 66 int strcasecmp(const char *s1, const char *s2) 64 int strcasecmp(const char *s1, const char *s2) 67 { 65 { 68 int c1, c2; 66 int c1, c2; 69 67 70 do { 68 do { 71 c1 = tolower(*s1++); 69 c1 = tolower(*s1++); 72 c2 = tolower(*s2++); 70 c2 = tolower(*s2++); 73 } while (c1 == c2 && c1 != 0); 71 } while (c1 == c2 && c1 != 0); 74 return c1 - c2; 72 return c1 - c2; 75 } 73 } 76 EXPORT_SYMBOL(strcasecmp); 74 EXPORT_SYMBOL(strcasecmp); 77 #endif 75 #endif 78 76 >> 77 #ifndef __HAVE_ARCH_STRNCASECMP >> 78 int strncasecmp(const char *s1, const char *s2, size_t n) >> 79 { >> 80 int c1, c2; >> 81 >> 82 do { >> 83 c1 = tolower(*s1++); >> 84 c2 = tolower(*s2++); >> 85 } while ((--n > 0) && c1 == c2 && c1 != 0); >> 86 return c1 - c2; >> 87 } >> 88 EXPORT_SYMBOL(strncasecmp); >> 89 #endif >> 90 79 #ifndef __HAVE_ARCH_STRCPY 91 #ifndef __HAVE_ARCH_STRCPY >> 92 /** >> 93 * strcpy - Copy a %NUL terminated string >> 94 * @dest: Where to copy the string to >> 95 * @src: Where to copy the string from >> 96 */ >> 97 #undef strcpy 80 char *strcpy(char *dest, const char *src) 98 char *strcpy(char *dest, const char *src) 81 { 99 { 82 char *tmp = dest; 100 char *tmp = dest; 83 101 84 while ((*dest++ = *src++) != '\0') 102 while ((*dest++ = *src++) != '\0') 85 /* nothing */; 103 /* nothing */; 86 return tmp; 104 return tmp; 87 } 105 } 88 EXPORT_SYMBOL(strcpy); 106 EXPORT_SYMBOL(strcpy); 89 #endif 107 #endif 90 108 91 #ifndef __HAVE_ARCH_STRNCPY 109 #ifndef __HAVE_ARCH_STRNCPY >> 110 /** >> 111 * strncpy - Copy a length-limited, %NUL-terminated string >> 112 * @dest: Where to copy the string to >> 113 * @src: Where to copy the string from >> 114 * @count: The maximum number of bytes to copy >> 115 * >> 116 * The result is not %NUL-terminated if the source exceeds >> 117 * @count bytes. >> 118 * >> 119 * In the case where the length of @src is less than that of >> 120 * count, the remainder of @dest will be padded with %NUL. >> 121 * >> 122 */ 92 char *strncpy(char *dest, const char *src, siz 123 char *strncpy(char *dest, const char *src, size_t count) 93 { 124 { 94 char *tmp = dest; 125 char *tmp = dest; 95 126 96 while (count) { 127 while (count) { 97 if ((*tmp = *src) != 0) 128 if ((*tmp = *src) != 0) 98 src++; 129 src++; 99 tmp++; 130 tmp++; 100 count--; 131 count--; 101 } 132 } 102 return dest; 133 return dest; 103 } 134 } 104 EXPORT_SYMBOL(strncpy); 135 EXPORT_SYMBOL(strncpy); 105 #endif 136 #endif 106 137 107 ssize_t sized_strscpy(char *dest, const char * !! 138 #ifndef __HAVE_ARCH_STRLCPY >> 139 /** >> 140 * strlcpy - Copy a %NUL terminated string into a sized buffer >> 141 * @dest: Where to copy the string to >> 142 * @src: Where to copy the string from >> 143 * @size: size of destination buffer >> 144 * >> 145 * Compatible with *BSD: the result is always a valid >> 146 * NUL-terminated string that fits in the buffer (unless, >> 147 * of course, the buffer size is zero). It does not pad >> 148 * out the result like strncpy() does. >> 149 */ >> 150 size_t strlcpy(char *dest, const char *src, size_t size) 108 { 151 { 109 const struct word_at_a_time constants !! 152 size_t ret = strlen(src); 110 size_t max = count; << 111 long res = 0; << 112 << 113 if (count == 0 || WARN_ON_ONCE(count > << 114 return -E2BIG; << 115 << 116 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS << 117 /* << 118 * If src is unaligned, don't cross a << 119 * since we don't know if the next pag << 120 */ << 121 if ((long)src & (sizeof(long) - 1)) { << 122 size_t limit = PAGE_SIZE - ((l << 123 if (limit < max) << 124 max = limit; << 125 } << 126 #else << 127 /* If src or dest is unaligned, don't << 128 if (((long) dest | (long) src) & (size << 129 max = 0; << 130 #endif << 131 << 132 /* << 133 * read_word_at_a_time() below may rea << 134 * trailing zero and use them in compa << 135 * under KMSAN to prevent false positi << 136 */ << 137 if (IS_ENABLED(CONFIG_KMSAN)) << 138 max = 0; << 139 << 140 while (max >= sizeof(unsigned long)) { << 141 unsigned long c, data; << 142 << 143 c = read_word_at_a_time(src+re << 144 if (has_zero(c, &data, &consta << 145 data = prep_zero_mask( << 146 data = create_zero_mas << 147 *(unsigned long *)(des << 148 return res + find_zero << 149 } << 150 *(unsigned long *)(dest+res) = << 151 res += sizeof(unsigned long); << 152 count -= sizeof(unsigned long) << 153 max -= sizeof(unsigned long); << 154 } << 155 153 156 while (count) { !! 154 if (size) { 157 char c; !! 155 size_t len = (ret >= size) ? size - 1 : ret; 158 !! 156 memcpy(dest, src, len); 159 c = src[res]; !! 157 dest[len] = '\0'; 160 dest[res] = c; << 161 if (!c) << 162 return res; << 163 res++; << 164 count--; << 165 } 158 } 166 !! 159 return ret; 167 /* Hit buffer length without finding a << 168 if (res) << 169 dest[res-1] = '\0'; << 170 << 171 return -E2BIG; << 172 } << 173 EXPORT_SYMBOL(sized_strscpy); << 174 << 175 /** << 176 * stpcpy - copy a string from src to dest ret << 177 * of dest, including src's %NUL-term << 178 * @dest: pointer to end of string being copie << 179 * to receive copy. << 180 * @src: pointer to the beginning of string be << 181 * dest. << 182 * << 183 * stpcpy differs from strcpy in a key way: th << 184 * to the new %NUL-terminating character in @d << 185 * value is a pointer to the start of @dest). << 186 * unsafe as it doesn't perform bounds checkin << 187 * not recommended for usage. Instead, its def << 188 * the compiler lowers other libcalls to stpcp << 189 */ << 190 char *stpcpy(char *__restrict__ dest, const ch << 191 char *stpcpy(char *__restrict__ dest, const ch << 192 { << 193 while ((*dest++ = *src++) != '\0') << 194 /* nothing */; << 195 return --dest; << 196 } 160 } 197 EXPORT_SYMBOL(stpcpy); !! 161 EXPORT_SYMBOL(strlcpy); >> 162 #endif 198 163 199 #ifndef __HAVE_ARCH_STRCAT 164 #ifndef __HAVE_ARCH_STRCAT >> 165 /** >> 166 * strcat - Append one %NUL-terminated string to another >> 167 * @dest: The string to be appended to >> 168 * @src: The string to append to it >> 169 */ >> 170 #undef strcat 200 char *strcat(char *dest, const char *src) 171 char *strcat(char *dest, const char *src) 201 { 172 { 202 char *tmp = dest; 173 char *tmp = dest; 203 174 204 while (*dest) 175 while (*dest) 205 dest++; 176 dest++; 206 while ((*dest++ = *src++) != '\0') 177 while ((*dest++ = *src++) != '\0') 207 ; 178 ; 208 return tmp; 179 return tmp; 209 } 180 } 210 EXPORT_SYMBOL(strcat); 181 EXPORT_SYMBOL(strcat); 211 #endif 182 #endif 212 183 213 #ifndef __HAVE_ARCH_STRNCAT 184 #ifndef __HAVE_ARCH_STRNCAT >> 185 /** >> 186 * strncat - Append a length-limited, %NUL-terminated string to another >> 187 * @dest: The string to be appended to >> 188 * @src: The string to append to it >> 189 * @count: The maximum numbers of bytes to copy >> 190 * >> 191 * Note that in contrast to strncpy(), strncat() ensures the result is >> 192 * terminated. >> 193 */ 214 char *strncat(char *dest, const char *src, siz 194 char *strncat(char *dest, const char *src, size_t count) 215 { 195 { 216 char *tmp = dest; 196 char *tmp = dest; 217 197 218 if (count) { 198 if (count) { 219 while (*dest) 199 while (*dest) 220 dest++; 200 dest++; 221 while ((*dest++ = *src++) != 0 201 while ((*dest++ = *src++) != 0) { 222 if (--count == 0) { 202 if (--count == 0) { 223 *dest = '\0'; 203 *dest = '\0'; 224 break; 204 break; 225 } 205 } 226 } 206 } 227 } 207 } 228 return tmp; 208 return tmp; 229 } 209 } 230 EXPORT_SYMBOL(strncat); 210 EXPORT_SYMBOL(strncat); 231 #endif 211 #endif 232 212 233 #ifndef __HAVE_ARCH_STRLCAT 213 #ifndef __HAVE_ARCH_STRLCAT >> 214 /** >> 215 * strlcat - Append a length-limited, %NUL-terminated string to another >> 216 * @dest: The string to be appended to >> 217 * @src: The string to append to it >> 218 * @count: The size of the destination buffer. >> 219 */ 234 size_t strlcat(char *dest, const char *src, si 220 size_t strlcat(char *dest, const char *src, size_t count) 235 { 221 { 236 size_t dsize = strlen(dest); 222 size_t dsize = strlen(dest); 237 size_t len = strlen(src); 223 size_t len = strlen(src); 238 size_t res = dsize + len; 224 size_t res = dsize + len; 239 225 240 /* This would be a bug */ 226 /* This would be a bug */ 241 BUG_ON(dsize >= count); 227 BUG_ON(dsize >= count); 242 228 243 dest += dsize; 229 dest += dsize; 244 count -= dsize; 230 count -= dsize; 245 if (len >= count) 231 if (len >= count) 246 len = count-1; 232 len = count-1; 247 __builtin_memcpy(dest, src, len); !! 233 memcpy(dest, src, len); 248 dest[len] = 0; 234 dest[len] = 0; 249 return res; 235 return res; 250 } 236 } 251 EXPORT_SYMBOL(strlcat); 237 EXPORT_SYMBOL(strlcat); 252 #endif 238 #endif 253 239 254 #ifndef __HAVE_ARCH_STRCMP 240 #ifndef __HAVE_ARCH_STRCMP 255 /** 241 /** 256 * strcmp - Compare two strings 242 * strcmp - Compare two strings 257 * @cs: One string 243 * @cs: One string 258 * @ct: Another string 244 * @ct: Another string 259 */ 245 */ >> 246 #undef strcmp 260 int strcmp(const char *cs, const char *ct) 247 int strcmp(const char *cs, const char *ct) 261 { 248 { 262 unsigned char c1, c2; 249 unsigned char c1, c2; 263 250 264 while (1) { 251 while (1) { 265 c1 = *cs++; 252 c1 = *cs++; 266 c2 = *ct++; 253 c2 = *ct++; 267 if (c1 != c2) 254 if (c1 != c2) 268 return c1 < c2 ? -1 : 255 return c1 < c2 ? -1 : 1; 269 if (!c1) 256 if (!c1) 270 break; 257 break; 271 } 258 } 272 return 0; 259 return 0; 273 } 260 } 274 EXPORT_SYMBOL(strcmp); 261 EXPORT_SYMBOL(strcmp); 275 #endif 262 #endif 276 263 277 #ifndef __HAVE_ARCH_STRNCMP 264 #ifndef __HAVE_ARCH_STRNCMP 278 /** 265 /** 279 * strncmp - Compare two length-limited string 266 * strncmp - Compare two length-limited strings 280 * @cs: One string 267 * @cs: One string 281 * @ct: Another string 268 * @ct: Another string 282 * @count: The maximum number of bytes to comp 269 * @count: The maximum number of bytes to compare 283 */ 270 */ 284 int strncmp(const char *cs, const char *ct, si 271 int strncmp(const char *cs, const char *ct, size_t count) 285 { 272 { 286 unsigned char c1, c2; 273 unsigned char c1, c2; 287 274 288 while (count) { 275 while (count) { 289 c1 = *cs++; 276 c1 = *cs++; 290 c2 = *ct++; 277 c2 = *ct++; 291 if (c1 != c2) 278 if (c1 != c2) 292 return c1 < c2 ? -1 : 279 return c1 < c2 ? -1 : 1; 293 if (!c1) 280 if (!c1) 294 break; 281 break; 295 count--; 282 count--; 296 } 283 } 297 return 0; 284 return 0; 298 } 285 } 299 EXPORT_SYMBOL(strncmp); 286 EXPORT_SYMBOL(strncmp); 300 #endif 287 #endif 301 288 302 #ifndef __HAVE_ARCH_STRCHR 289 #ifndef __HAVE_ARCH_STRCHR 303 /** 290 /** 304 * strchr - Find the first occurrence of a cha 291 * strchr - Find the first occurrence of a character in a string 305 * @s: The string to be searched 292 * @s: The string to be searched 306 * @c: The character to search for 293 * @c: The character to search for 307 * << 308 * Note that the %NUL-terminator is considered << 309 * be searched for. << 310 */ 294 */ 311 char *strchr(const char *s, int c) 295 char *strchr(const char *s, int c) 312 { 296 { 313 for (; *s != (char)c; ++s) 297 for (; *s != (char)c; ++s) 314 if (*s == '\0') 298 if (*s == '\0') 315 return NULL; 299 return NULL; 316 return (char *)s; 300 return (char *)s; 317 } 301 } 318 EXPORT_SYMBOL(strchr); 302 EXPORT_SYMBOL(strchr); 319 #endif 303 #endif 320 304 321 #ifndef __HAVE_ARCH_STRCHRNUL << 322 /** << 323 * strchrnul - Find and return a character in << 324 * @s: The string to be searched << 325 * @c: The character to search for << 326 * << 327 * Returns pointer to first occurrence of 'c' << 328 * return a pointer to the null byte at the en << 329 */ << 330 char *strchrnul(const char *s, int c) << 331 { << 332 while (*s && *s != (char)c) << 333 s++; << 334 return (char *)s; << 335 } << 336 EXPORT_SYMBOL(strchrnul); << 337 #endif << 338 << 339 /** << 340 * strnchrnul - Find and return a character in << 341 * or end of string << 342 * @s: The string to be searched << 343 * @count: The number of characters to be sear << 344 * @c: The character to search for << 345 * << 346 * Returns pointer to the first occurrence of << 347 * then return a pointer to the last character << 348 */ << 349 char *strnchrnul(const char *s, size_t count, << 350 { << 351 while (count-- && *s && *s != (char)c) << 352 s++; << 353 return (char *)s; << 354 } << 355 << 356 #ifndef __HAVE_ARCH_STRRCHR 305 #ifndef __HAVE_ARCH_STRRCHR 357 /** 306 /** 358 * strrchr - Find the last occurrence of a cha 307 * strrchr - Find the last occurrence of a character in a string 359 * @s: The string to be searched 308 * @s: The string to be searched 360 * @c: The character to search for 309 * @c: The character to search for 361 */ 310 */ 362 char *strrchr(const char *s, int c) 311 char *strrchr(const char *s, int c) 363 { 312 { 364 const char *last = NULL; !! 313 const char *p = s + strlen(s); 365 do { !! 314 do { 366 if (*s == (char)c) !! 315 if (*p == (char)c) 367 last = s; !! 316 return (char *)p; 368 } while (*s++); !! 317 } while (--p >= s); 369 return (char *)last; !! 318 return NULL; 370 } 319 } 371 EXPORT_SYMBOL(strrchr); 320 EXPORT_SYMBOL(strrchr); 372 #endif 321 #endif 373 322 374 #ifndef __HAVE_ARCH_STRNCHR 323 #ifndef __HAVE_ARCH_STRNCHR 375 /** 324 /** 376 * strnchr - Find a character in a length limi 325 * strnchr - Find a character in a length limited string 377 * @s: The string to be searched 326 * @s: The string to be searched 378 * @count: The number of characters to be sear 327 * @count: The number of characters to be searched 379 * @c: The character to search for 328 * @c: The character to search for 380 * << 381 * Note that the %NUL-terminator is considered << 382 * be searched for. << 383 */ 329 */ 384 char *strnchr(const char *s, size_t count, int 330 char *strnchr(const char *s, size_t count, int c) 385 { 331 { 386 while (count--) { !! 332 for (; count-- && *s != '\0'; ++s) 387 if (*s == (char)c) 333 if (*s == (char)c) 388 return (char *)s; 334 return (char *)s; 389 if (*s++ == '\0') << 390 break; << 391 } << 392 return NULL; 335 return NULL; 393 } 336 } 394 EXPORT_SYMBOL(strnchr); 337 EXPORT_SYMBOL(strnchr); 395 #endif 338 #endif 396 339 >> 340 /** >> 341 * strstrip - Removes leading and trailing whitespace from @s. >> 342 * @s: The string to be stripped. >> 343 * >> 344 * Note that the first trailing whitespace is replaced with a %NUL-terminator >> 345 * in the given string @s. Returns a pointer to the first non-whitespace >> 346 * character in @s. >> 347 */ >> 348 char *strstrip(char *s) >> 349 { >> 350 size_t size; >> 351 char *end; >> 352 >> 353 size = strlen(s); >> 354 >> 355 if (!size) >> 356 return s; >> 357 >> 358 end = s + size - 1; >> 359 while (end >= s && isspace(*end)) >> 360 end--; >> 361 *(end + 1) = '\0'; >> 362 >> 363 while (*s && isspace(*s)) >> 364 s++; >> 365 >> 366 return s; >> 367 } >> 368 EXPORT_SYMBOL(strstrip); >> 369 397 #ifndef __HAVE_ARCH_STRLEN 370 #ifndef __HAVE_ARCH_STRLEN >> 371 /** >> 372 * strlen - Find the length of a string >> 373 * @s: The string to be sized >> 374 */ 398 size_t strlen(const char *s) 375 size_t strlen(const char *s) 399 { 376 { 400 const char *sc; 377 const char *sc; 401 378 402 for (sc = s; *sc != '\0'; ++sc) 379 for (sc = s; *sc != '\0'; ++sc) 403 /* nothing */; 380 /* nothing */; 404 return sc - s; 381 return sc - s; 405 } 382 } 406 EXPORT_SYMBOL(strlen); 383 EXPORT_SYMBOL(strlen); 407 #endif 384 #endif 408 385 409 #ifndef __HAVE_ARCH_STRNLEN 386 #ifndef __HAVE_ARCH_STRNLEN >> 387 /** >> 388 * strnlen - Find the length of a length-limited string >> 389 * @s: The string to be sized >> 390 * @count: The maximum number of bytes to search >> 391 */ 410 size_t strnlen(const char *s, size_t count) 392 size_t strnlen(const char *s, size_t count) 411 { 393 { 412 const char *sc; 394 const char *sc; 413 395 414 for (sc = s; count-- && *sc != '\0'; + 396 for (sc = s; count-- && *sc != '\0'; ++sc) 415 /* nothing */; 397 /* nothing */; 416 return sc - s; 398 return sc - s; 417 } 399 } 418 EXPORT_SYMBOL(strnlen); 400 EXPORT_SYMBOL(strnlen); 419 #endif 401 #endif 420 402 421 #ifndef __HAVE_ARCH_STRSPN 403 #ifndef __HAVE_ARCH_STRSPN 422 /** 404 /** 423 * strspn - Calculate the length of the initia 405 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept 424 * @s: The string to be searched 406 * @s: The string to be searched 425 * @accept: The string to search for 407 * @accept: The string to search for 426 */ 408 */ 427 size_t strspn(const char *s, const char *accep 409 size_t strspn(const char *s, const char *accept) 428 { 410 { 429 const char *p; 411 const char *p; >> 412 const char *a; >> 413 size_t count = 0; 430 414 431 for (p = s; *p != '\0'; ++p) { 415 for (p = s; *p != '\0'; ++p) { 432 if (!strchr(accept, *p)) !! 416 for (a = accept; *a != '\0'; ++a) { 433 break; !! 417 if (*p == *a) >> 418 break; >> 419 } >> 420 if (*a == '\0') >> 421 return count; >> 422 ++count; 434 } 423 } 435 return p - s; !! 424 return count; 436 } 425 } >> 426 437 EXPORT_SYMBOL(strspn); 427 EXPORT_SYMBOL(strspn); 438 #endif 428 #endif 439 429 440 #ifndef __HAVE_ARCH_STRCSPN 430 #ifndef __HAVE_ARCH_STRCSPN 441 /** 431 /** 442 * strcspn - Calculate the length of the initi 432 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject 443 * @s: The string to be searched 433 * @s: The string to be searched 444 * @reject: The string to avoid 434 * @reject: The string to avoid 445 */ 435 */ 446 size_t strcspn(const char *s, const char *reje 436 size_t strcspn(const char *s, const char *reject) 447 { 437 { 448 const char *p; 438 const char *p; >> 439 const char *r; >> 440 size_t count = 0; 449 441 450 for (p = s; *p != '\0'; ++p) { 442 for (p = s; *p != '\0'; ++p) { 451 if (strchr(reject, *p)) !! 443 for (r = reject; *r != '\0'; ++r) { 452 break; !! 444 if (*p == *r) >> 445 return count; >> 446 } >> 447 ++count; 453 } 448 } 454 return p - s; !! 449 return count; 455 } 450 } 456 EXPORT_SYMBOL(strcspn); 451 EXPORT_SYMBOL(strcspn); 457 #endif 452 #endif 458 453 459 #ifndef __HAVE_ARCH_STRPBRK 454 #ifndef __HAVE_ARCH_STRPBRK 460 /** 455 /** 461 * strpbrk - Find the first occurrence of a se 456 * strpbrk - Find the first occurrence of a set of characters 462 * @cs: The string to be searched 457 * @cs: The string to be searched 463 * @ct: The characters to search for 458 * @ct: The characters to search for 464 */ 459 */ 465 char *strpbrk(const char *cs, const char *ct) 460 char *strpbrk(const char *cs, const char *ct) 466 { 461 { 467 const char *sc; !! 462 const char *sc1, *sc2; 468 463 469 for (sc = cs; *sc != '\0'; ++sc) { !! 464 for (sc1 = cs; *sc1 != '\0'; ++sc1) { 470 if (strchr(ct, *sc)) !! 465 for (sc2 = ct; *sc2 != '\0'; ++sc2) { 471 return (char *)sc; !! 466 if (*sc1 == *sc2) >> 467 return (char *)sc1; >> 468 } 472 } 469 } 473 return NULL; 470 return NULL; 474 } 471 } 475 EXPORT_SYMBOL(strpbrk); 472 EXPORT_SYMBOL(strpbrk); 476 #endif 473 #endif 477 474 478 #ifndef __HAVE_ARCH_STRSEP 475 #ifndef __HAVE_ARCH_STRSEP 479 /** 476 /** 480 * strsep - Split a string into tokens 477 * strsep - Split a string into tokens 481 * @s: The string to be searched 478 * @s: The string to be searched 482 * @ct: The characters to search for 479 * @ct: The characters to search for 483 * 480 * 484 * strsep() updates @s to point after the toke 481 * strsep() updates @s to point after the token, ready for the next call. 485 * 482 * 486 * It returns empty tokens, too, behaving exac 483 * It returns empty tokens, too, behaving exactly like the libc function 487 * of that name. In fact, it was stolen from g 484 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. 488 * Same semantics, slimmer shape. ;) 485 * Same semantics, slimmer shape. ;) 489 */ 486 */ 490 char *strsep(char **s, const char *ct) 487 char *strsep(char **s, const char *ct) 491 { 488 { 492 char *sbegin = *s; 489 char *sbegin = *s; 493 char *end; 490 char *end; 494 491 495 if (sbegin == NULL) 492 if (sbegin == NULL) 496 return NULL; 493 return NULL; 497 494 498 end = strpbrk(sbegin, ct); 495 end = strpbrk(sbegin, ct); 499 if (end) 496 if (end) 500 *end++ = '\0'; 497 *end++ = '\0'; 501 *s = end; 498 *s = end; 502 return sbegin; 499 return sbegin; 503 } 500 } 504 EXPORT_SYMBOL(strsep); 501 EXPORT_SYMBOL(strsep); 505 #endif 502 #endif 506 503 >> 504 /** >> 505 * sysfs_streq - return true if strings are equal, modulo trailing newline >> 506 * @s1: one string >> 507 * @s2: another string >> 508 * >> 509 * This routine returns true iff two strings are equal, treating both >> 510 * NUL and newline-then-NUL as equivalent string terminations. It's >> 511 * geared for use with sysfs input strings, which generally terminate >> 512 * with newlines but are compared against values without newlines. >> 513 */ >> 514 bool sysfs_streq(const char *s1, const char *s2) >> 515 { >> 516 while (*s1 && *s1 == *s2) { >> 517 s1++; >> 518 s2++; >> 519 } >> 520 >> 521 if (*s1 == *s2) >> 522 return true; >> 523 if (!*s1 && *s2 == '\n' && !s2[1]) >> 524 return true; >> 525 if (*s1 == '\n' && !s1[1] && !*s2) >> 526 return true; >> 527 return false; >> 528 } >> 529 EXPORT_SYMBOL(sysfs_streq); >> 530 507 #ifndef __HAVE_ARCH_MEMSET 531 #ifndef __HAVE_ARCH_MEMSET 508 /** 532 /** 509 * memset - Fill a region of memory with the g 533 * memset - Fill a region of memory with the given value 510 * @s: Pointer to the start of the area. 534 * @s: Pointer to the start of the area. 511 * @c: The byte to fill the area with 535 * @c: The byte to fill the area with 512 * @count: The size of the area. 536 * @count: The size of the area. 513 * 537 * 514 * Do not use memset() to access IO space, use 538 * Do not use memset() to access IO space, use memset_io() instead. 515 */ 539 */ 516 void *memset(void *s, int c, size_t count) 540 void *memset(void *s, int c, size_t count) 517 { 541 { 518 char *xs = s; 542 char *xs = s; 519 543 520 while (count--) 544 while (count--) 521 *xs++ = c; 545 *xs++ = c; 522 return s; 546 return s; 523 } 547 } 524 EXPORT_SYMBOL(memset); 548 EXPORT_SYMBOL(memset); 525 #endif 549 #endif 526 550 527 #ifndef __HAVE_ARCH_MEMSET16 << 528 /** << 529 * memset16() - Fill a memory area with a uint << 530 * @s: Pointer to the start of the area. << 531 * @v: The value to fill the area with << 532 * @count: The number of values to store << 533 * << 534 * Differs from memset() in that it fills with << 535 * of a byte. Remember that @count is the num << 536 * store, not the number of bytes. << 537 */ << 538 void *memset16(uint16_t *s, uint16_t v, size_t << 539 { << 540 uint16_t *xs = s; << 541 << 542 while (count--) << 543 *xs++ = v; << 544 return s; << 545 } << 546 EXPORT_SYMBOL(memset16); << 547 #endif << 548 << 549 #ifndef __HAVE_ARCH_MEMSET32 << 550 /** << 551 * memset32() - Fill a memory area with a uint << 552 * @s: Pointer to the start of the area. << 553 * @v: The value to fill the area with << 554 * @count: The number of values to store << 555 * << 556 * Differs from memset() in that it fills with << 557 * of a byte. Remember that @count is the num << 558 * store, not the number of bytes. << 559 */ << 560 void *memset32(uint32_t *s, uint32_t v, size_t << 561 { << 562 uint32_t *xs = s; << 563 << 564 while (count--) << 565 *xs++ = v; << 566 return s; << 567 } << 568 EXPORT_SYMBOL(memset32); << 569 #endif << 570 << 571 #ifndef __HAVE_ARCH_MEMSET64 << 572 /** << 573 * memset64() - Fill a memory area with a uint << 574 * @s: Pointer to the start of the area. << 575 * @v: The value to fill the area with << 576 * @count: The number of values to store << 577 * << 578 * Differs from memset() in that it fills with << 579 * of a byte. Remember that @count is the num << 580 * store, not the number of bytes. << 581 */ << 582 void *memset64(uint64_t *s, uint64_t v, size_t << 583 { << 584 uint64_t *xs = s; << 585 << 586 while (count--) << 587 *xs++ = v; << 588 return s; << 589 } << 590 EXPORT_SYMBOL(memset64); << 591 #endif << 592 << 593 #ifndef __HAVE_ARCH_MEMCPY 551 #ifndef __HAVE_ARCH_MEMCPY 594 /** 552 /** 595 * memcpy - Copy one area of memory to another 553 * memcpy - Copy one area of memory to another 596 * @dest: Where to copy to 554 * @dest: Where to copy to 597 * @src: Where to copy from 555 * @src: Where to copy from 598 * @count: The size of the area. 556 * @count: The size of the area. 599 * 557 * 600 * You should not use this function to access 558 * You should not use this function to access IO space, use memcpy_toio() 601 * or memcpy_fromio() instead. 559 * or memcpy_fromio() instead. 602 */ 560 */ 603 void *memcpy(void *dest, const void *src, size 561 void *memcpy(void *dest, const void *src, size_t count) 604 { 562 { 605 char *tmp = dest; 563 char *tmp = dest; 606 const char *s = src; 564 const char *s = src; 607 565 608 while (count--) 566 while (count--) 609 *tmp++ = *s++; 567 *tmp++ = *s++; 610 return dest; 568 return dest; 611 } 569 } 612 EXPORT_SYMBOL(memcpy); 570 EXPORT_SYMBOL(memcpy); 613 #endif 571 #endif 614 572 615 #ifndef __HAVE_ARCH_MEMMOVE 573 #ifndef __HAVE_ARCH_MEMMOVE 616 /** 574 /** 617 * memmove - Copy one area of memory to anothe 575 * memmove - Copy one area of memory to another 618 * @dest: Where to copy to 576 * @dest: Where to copy to 619 * @src: Where to copy from 577 * @src: Where to copy from 620 * @count: The size of the area. 578 * @count: The size of the area. 621 * 579 * 622 * Unlike memcpy(), memmove() copes with overl 580 * Unlike memcpy(), memmove() copes with overlapping areas. 623 */ 581 */ 624 void *memmove(void *dest, const void *src, siz 582 void *memmove(void *dest, const void *src, size_t count) 625 { 583 { 626 char *tmp; 584 char *tmp; 627 const char *s; 585 const char *s; 628 586 629 if (dest <= src) { 587 if (dest <= src) { 630 tmp = dest; 588 tmp = dest; 631 s = src; 589 s = src; 632 while (count--) 590 while (count--) 633 *tmp++ = *s++; 591 *tmp++ = *s++; 634 } else { 592 } else { 635 tmp = dest; 593 tmp = dest; 636 tmp += count; 594 tmp += count; 637 s = src; 595 s = src; 638 s += count; 596 s += count; 639 while (count--) 597 while (count--) 640 *--tmp = *--s; 598 *--tmp = *--s; 641 } 599 } 642 return dest; 600 return dest; 643 } 601 } 644 EXPORT_SYMBOL(memmove); 602 EXPORT_SYMBOL(memmove); 645 #endif 603 #endif 646 604 647 #ifndef __HAVE_ARCH_MEMCMP 605 #ifndef __HAVE_ARCH_MEMCMP 648 /** 606 /** 649 * memcmp - Compare two areas of memory 607 * memcmp - Compare two areas of memory 650 * @cs: One area of memory 608 * @cs: One area of memory 651 * @ct: Another area of memory 609 * @ct: Another area of memory 652 * @count: The size of the area. 610 * @count: The size of the area. 653 */ 611 */ 654 #undef memcmp 612 #undef memcmp 655 __visible int memcmp(const void *cs, const voi !! 613 int memcmp(const void *cs, const void *ct, size_t count) 656 { 614 { 657 const unsigned char *su1, *su2; 615 const unsigned char *su1, *su2; 658 int res = 0; 616 int res = 0; 659 617 660 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS << 661 if (count >= sizeof(unsigned long)) { << 662 const unsigned long *u1 = cs; << 663 const unsigned long *u2 = ct; << 664 do { << 665 if (get_unaligned(u1) << 666 break; << 667 u1++; << 668 u2++; << 669 count -= sizeof(unsign << 670 } while (count >= sizeof(unsig << 671 cs = u1; << 672 ct = u2; << 673 } << 674 #endif << 675 for (su1 = cs, su2 = ct; 0 < count; ++ 618 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) 676 if ((res = *su1 - *su2) != 0) 619 if ((res = *su1 - *su2) != 0) 677 break; 620 break; 678 return res; 621 return res; 679 } 622 } 680 EXPORT_SYMBOL(memcmp); 623 EXPORT_SYMBOL(memcmp); 681 #endif 624 #endif 682 625 683 #ifndef __HAVE_ARCH_BCMP << 684 /** << 685 * bcmp - returns 0 if and only if the buffers << 686 * @a: pointer to first buffer. << 687 * @b: pointer to second buffer. << 688 * @len: size of buffers. << 689 * << 690 * The sign or magnitude of a non-zero return << 691 * meaning, and architectures may implement th << 692 * while this particular implementation is a s << 693 * not rely on anything but whether the return << 694 */ << 695 int bcmp(const void *a, const void *b, size_t << 696 { << 697 return memcmp(a, b, len); << 698 } << 699 EXPORT_SYMBOL(bcmp); << 700 #endif << 701 << 702 #ifndef __HAVE_ARCH_MEMSCAN 626 #ifndef __HAVE_ARCH_MEMSCAN 703 /** 627 /** 704 * memscan - Find a character in an area of me 628 * memscan - Find a character in an area of memory. 705 * @addr: The memory area 629 * @addr: The memory area 706 * @c: The byte to search for 630 * @c: The byte to search for 707 * @size: The size of the area. 631 * @size: The size of the area. 708 * 632 * 709 * returns the address of the first occurrence 633 * returns the address of the first occurrence of @c, or 1 byte past 710 * the area if @c is not found 634 * the area if @c is not found 711 */ 635 */ 712 void *memscan(void *addr, int c, size_t size) 636 void *memscan(void *addr, int c, size_t size) 713 { 637 { 714 unsigned char *p = addr; 638 unsigned char *p = addr; 715 639 716 while (size) { 640 while (size) { 717 if (*p == (unsigned char)c) !! 641 if (*p == c) 718 return (void *)p; 642 return (void *)p; 719 p++; 643 p++; 720 size--; 644 size--; 721 } 645 } 722 return (void *)p; 646 return (void *)p; 723 } 647 } 724 EXPORT_SYMBOL(memscan); 648 EXPORT_SYMBOL(memscan); 725 #endif 649 #endif 726 650 727 #ifndef __HAVE_ARCH_STRSTR 651 #ifndef __HAVE_ARCH_STRSTR 728 /** 652 /** 729 * strstr - Find the first substring in a %NUL 653 * strstr - Find the first substring in a %NUL terminated string 730 * @s1: The string to be searched 654 * @s1: The string to be searched 731 * @s2: The string to search for 655 * @s2: The string to search for 732 */ 656 */ 733 char *strstr(const char *s1, const char *s2) 657 char *strstr(const char *s1, const char *s2) 734 { 658 { 735 size_t l1, l2; !! 659 int l1, l2; 736 660 737 l2 = strlen(s2); 661 l2 = strlen(s2); 738 if (!l2) 662 if (!l2) 739 return (char *)s1; 663 return (char *)s1; 740 l1 = strlen(s1); 664 l1 = strlen(s1); 741 while (l1 >= l2) { 665 while (l1 >= l2) { 742 l1--; 666 l1--; 743 if (!memcmp(s1, s2, l2)) 667 if (!memcmp(s1, s2, l2)) 744 return (char *)s1; 668 return (char *)s1; 745 s1++; 669 s1++; 746 } 670 } 747 return NULL; 671 return NULL; 748 } 672 } 749 EXPORT_SYMBOL(strstr); 673 EXPORT_SYMBOL(strstr); 750 #endif 674 #endif 751 675 752 #ifndef __HAVE_ARCH_STRNSTR << 753 /** << 754 * strnstr - Find the first substring in a len << 755 * @s1: The string to be searched << 756 * @s2: The string to search for << 757 * @len: the maximum number of characters to s << 758 */ << 759 char *strnstr(const char *s1, const char *s2, << 760 { << 761 size_t l2; << 762 << 763 l2 = strlen(s2); << 764 if (!l2) << 765 return (char *)s1; << 766 while (len >= l2) { << 767 len--; << 768 if (!memcmp(s1, s2, l2)) << 769 return (char *)s1; << 770 s1++; << 771 } << 772 return NULL; << 773 } << 774 EXPORT_SYMBOL(strnstr); << 775 #endif << 776 << 777 #ifndef __HAVE_ARCH_MEMCHR 676 #ifndef __HAVE_ARCH_MEMCHR 778 /** 677 /** 779 * memchr - Find a character in an area of mem 678 * memchr - Find a character in an area of memory. 780 * @s: The memory area 679 * @s: The memory area 781 * @c: The byte to search for 680 * @c: The byte to search for 782 * @n: The size of the area. 681 * @n: The size of the area. 783 * 682 * 784 * returns the address of the first occurrence 683 * returns the address of the first occurrence of @c, or %NULL 785 * if @c is not found 684 * if @c is not found 786 */ 685 */ 787 void *memchr(const void *s, int c, size_t n) 686 void *memchr(const void *s, int c, size_t n) 788 { 687 { 789 const unsigned char *p = s; 688 const unsigned char *p = s; 790 while (n-- != 0) { 689 while (n-- != 0) { 791 if ((unsigned char)c == *p++) 690 if ((unsigned char)c == *p++) { 792 return (void *)(p - 1) 691 return (void *)(p - 1); 793 } 692 } 794 } 693 } 795 return NULL; 694 return NULL; 796 } 695 } 797 EXPORT_SYMBOL(memchr); 696 EXPORT_SYMBOL(memchr); 798 #endif 697 #endif 799 << 800 static void *check_bytes8(const u8 *start, u8 << 801 { << 802 while (bytes) { << 803 if (*start != value) << 804 return (void *)start; << 805 start++; << 806 bytes--; << 807 } << 808 return NULL; << 809 } << 810 << 811 /** << 812 * memchr_inv - Find an unmatching character i << 813 * @start: The memory area << 814 * @c: Find a character other than c << 815 * @bytes: The size of the area. << 816 * << 817 * returns the address of the first character << 818 * if the whole buffer contains just @c. << 819 */ << 820 void *memchr_inv(const void *start, int c, siz << 821 { << 822 u8 value = c; << 823 u64 value64; << 824 unsigned int words, prefix; << 825 << 826 if (bytes <= 16) << 827 return check_bytes8(start, val << 828 << 829 value64 = value; << 830 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) & << 831 value64 *= 0x0101010101010101ULL; << 832 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) << 833 value64 *= 0x01010101; << 834 value64 |= value64 << 32; << 835 #else << 836 value64 |= value64 << 8; << 837 value64 |= value64 << 16; << 838 value64 |= value64 << 32; << 839 #endif << 840 << 841 prefix = (unsigned long)start % 8; << 842 if (prefix) { << 843 u8 *r; << 844 << 845 prefix = 8 - prefix; << 846 r = check_bytes8(start, value, << 847 if (r) << 848 return r; << 849 start += prefix; << 850 bytes -= prefix; << 851 } << 852 << 853 words = bytes / 8; << 854 << 855 while (words) { << 856 if (*(u64 *)start != value64) << 857 return check_bytes8(st << 858 start += 8; << 859 words--; << 860 } << 861 << 862 return check_bytes8(start, value, byte << 863 } << 864 EXPORT_SYMBOL(memchr_inv); << 865 698
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.