wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * linux/lib/string.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * stupid library routines.. The optimized versions should generally be found |
| 9 | * as inline code in <asm-xx/string.h> |
| 10 | * |
| 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 | |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/ctype.h> |
| 21 | #include <malloc.h> |
| 22 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 23 | |
| 24 | #ifndef __HAVE_ARCH_STRNICMP |
| 25 | /** |
| 26 | * strnicmp - Case insensitive, length-limited string comparison |
| 27 | * @s1: One string |
| 28 | * @s2: The other string |
| 29 | * @len: the maximum number of characters to compare |
| 30 | */ |
| 31 | int strnicmp(const char *s1, const char *s2, size_t len) |
| 32 | { |
| 33 | /* Yes, Virginia, it had better be unsigned */ |
| 34 | unsigned char c1, c2; |
| 35 | |
| 36 | c1 = 0; c2 = 0; |
| 37 | if (len) { |
| 38 | do { |
| 39 | c1 = *s1; c2 = *s2; |
| 40 | s1++; s2++; |
| 41 | if (!c1) |
| 42 | break; |
| 43 | if (!c2) |
| 44 | break; |
| 45 | if (c1 == c2) |
| 46 | continue; |
| 47 | c1 = tolower(c1); |
| 48 | c2 = tolower(c2); |
| 49 | if (c1 != c2) |
| 50 | break; |
| 51 | } while (--len); |
| 52 | } |
| 53 | return (int)c1 - (int)c2; |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | char * ___strtok; |
| 58 | |
| 59 | #ifndef __HAVE_ARCH_STRCPY |
| 60 | /** |
| 61 | * strcpy - Copy a %NUL terminated string |
| 62 | * @dest: Where to copy the string to |
| 63 | * @src: Where to copy the string from |
| 64 | */ |
| 65 | char * strcpy(char * dest,const char *src) |
| 66 | { |
| 67 | char *tmp = dest; |
| 68 | |
| 69 | while ((*dest++ = *src++) != '\0') |
| 70 | /* nothing */; |
| 71 | return tmp; |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | #ifndef __HAVE_ARCH_STRNCPY |
| 76 | /** |
| 77 | * strncpy - Copy a length-limited, %NUL-terminated string |
| 78 | * @dest: Where to copy the string to |
| 79 | * @src: Where to copy the string from |
| 80 | * @count: The maximum number of bytes to copy |
| 81 | * |
| 82 | * Note that unlike userspace strncpy, this does not %NUL-pad the buffer. |
| 83 | * However, the result is not %NUL-terminated if the source exceeds |
| 84 | * @count bytes. |
| 85 | */ |
| 86 | char * strncpy(char * dest,const char *src,size_t count) |
| 87 | { |
| 88 | char *tmp = dest; |
| 89 | |
| 90 | while (count-- && (*dest++ = *src++) != '\0') |
| 91 | /* nothing */; |
| 92 | |
| 93 | return tmp; |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | #ifndef __HAVE_ARCH_STRCAT |
| 98 | /** |
| 99 | * strcat - Append one %NUL-terminated string to another |
| 100 | * @dest: The string to be appended to |
| 101 | * @src: The string to append to it |
| 102 | */ |
| 103 | char * strcat(char * dest, const char * src) |
| 104 | { |
| 105 | char *tmp = dest; |
| 106 | |
| 107 | while (*dest) |
| 108 | dest++; |
| 109 | while ((*dest++ = *src++) != '\0') |
| 110 | ; |
| 111 | |
| 112 | return tmp; |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | #ifndef __HAVE_ARCH_STRNCAT |
| 117 | /** |
| 118 | * strncat - Append a length-limited, %NUL-terminated string to another |
| 119 | * @dest: The string to be appended to |
| 120 | * @src: The string to append to it |
| 121 | * @count: The maximum numbers of bytes to copy |
| 122 | * |
| 123 | * Note that in contrast to strncpy, strncat ensures the result is |
| 124 | * terminated. |
| 125 | */ |
| 126 | char * strncat(char *dest, const char *src, size_t count) |
| 127 | { |
| 128 | char *tmp = dest; |
| 129 | |
| 130 | if (count) { |
| 131 | while (*dest) |
| 132 | dest++; |
| 133 | while ((*dest++ = *src++)) { |
| 134 | if (--count == 0) { |
| 135 | *dest = '\0'; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return tmp; |
| 142 | } |
| 143 | #endif |
| 144 | |
| 145 | #ifndef __HAVE_ARCH_STRCMP |
| 146 | /** |
| 147 | * strcmp - Compare two strings |
| 148 | * @cs: One string |
| 149 | * @ct: Another string |
| 150 | */ |
| 151 | int strcmp(const char * cs,const char * ct) |
| 152 | { |
| 153 | register signed char __res; |
| 154 | |
| 155 | while (1) { |
| 156 | if ((__res = *cs - *ct++) != 0 || !*cs++) |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | return __res; |
| 161 | } |
| 162 | #endif |
| 163 | |
| 164 | #ifndef __HAVE_ARCH_STRNCMP |
| 165 | /** |
| 166 | * strncmp - Compare two length-limited strings |
| 167 | * @cs: One string |
| 168 | * @ct: Another string |
| 169 | * @count: The maximum number of bytes to compare |
| 170 | */ |
| 171 | int strncmp(const char * cs,const char * ct,size_t count) |
| 172 | { |
| 173 | register signed char __res = 0; |
| 174 | |
| 175 | while (count) { |
| 176 | if ((__res = *cs - *ct++) != 0 || !*cs++) |
| 177 | break; |
| 178 | count--; |
| 179 | } |
| 180 | |
| 181 | return __res; |
| 182 | } |
| 183 | #endif |
| 184 | |
| 185 | #ifndef __HAVE_ARCH_STRCHR |
| 186 | /** |
| 187 | * strchr - Find the first occurrence of a character in a string |
| 188 | * @s: The string to be searched |
| 189 | * @c: The character to search for |
| 190 | */ |
| 191 | char * strchr(const char * s, int c) |
| 192 | { |
| 193 | for(; *s != (char) c; ++s) |
| 194 | if (*s == '\0') |
| 195 | return NULL; |
| 196 | return (char *) s; |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | #ifndef __HAVE_ARCH_STRRCHR |
| 201 | /** |
| 202 | * strrchr - Find the last occurrence of a character in a string |
| 203 | * @s: The string to be searched |
| 204 | * @c: The character to search for |
| 205 | */ |
| 206 | char * strrchr(const char * s, int c) |
| 207 | { |
| 208 | const char *p = s + strlen(s); |
| 209 | do { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 210 | if (*p == (char)c) |
| 211 | return (char *)p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 212 | } while (--p >= s); |
| 213 | return NULL; |
| 214 | } |
| 215 | #endif |
| 216 | |
| 217 | #ifndef __HAVE_ARCH_STRLEN |
| 218 | /** |
| 219 | * strlen - Find the length of a string |
| 220 | * @s: The string to be sized |
| 221 | */ |
| 222 | size_t strlen(const char * s) |
| 223 | { |
| 224 | const char *sc; |
| 225 | |
| 226 | for (sc = s; *sc != '\0'; ++sc) |
| 227 | /* nothing */; |
| 228 | return sc - s; |
| 229 | } |
| 230 | #endif |
| 231 | |
| 232 | #ifndef __HAVE_ARCH_STRNLEN |
| 233 | /** |
| 234 | * strnlen - Find the length of a length-limited string |
| 235 | * @s: The string to be sized |
| 236 | * @count: The maximum number of bytes to search |
| 237 | */ |
| 238 | size_t strnlen(const char * s, size_t count) |
| 239 | { |
| 240 | const char *sc; |
| 241 | |
| 242 | for (sc = s; count-- && *sc != '\0'; ++sc) |
| 243 | /* nothing */; |
| 244 | return sc - s; |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | #ifndef __HAVE_ARCH_STRDUP |
| 249 | char * strdup(const char *s) |
| 250 | { |
| 251 | char *new; |
| 252 | |
| 253 | if ((s == NULL) || |
| 254 | ((new = malloc (strlen(s) + 1)) == NULL) ) { |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | strcpy (new, s); |
| 259 | return new; |
| 260 | } |
| 261 | #endif |
| 262 | |
| 263 | #ifndef __HAVE_ARCH_STRSPN |
| 264 | /** |
| 265 | * strspn - Calculate the length of the initial substring of @s which only |
| 266 | * contain letters in @accept |
| 267 | * @s: The string to be searched |
| 268 | * @accept: The string to search for |
| 269 | */ |
| 270 | size_t strspn(const char *s, const char *accept) |
| 271 | { |
| 272 | const char *p; |
| 273 | const char *a; |
| 274 | size_t count = 0; |
| 275 | |
| 276 | for (p = s; *p != '\0'; ++p) { |
| 277 | for (a = accept; *a != '\0'; ++a) { |
| 278 | if (*p == *a) |
| 279 | break; |
| 280 | } |
| 281 | if (*a == '\0') |
| 282 | return count; |
| 283 | ++count; |
| 284 | } |
| 285 | |
| 286 | return count; |
| 287 | } |
| 288 | #endif |
| 289 | |
| 290 | #ifndef __HAVE_ARCH_STRPBRK |
| 291 | /** |
| 292 | * strpbrk - Find the first occurrence of a set of characters |
| 293 | * @cs: The string to be searched |
| 294 | * @ct: The characters to search for |
| 295 | */ |
| 296 | char * strpbrk(const char * cs,const char * ct) |
| 297 | { |
| 298 | const char *sc1,*sc2; |
| 299 | |
| 300 | for( sc1 = cs; *sc1 != '\0'; ++sc1) { |
| 301 | for( sc2 = ct; *sc2 != '\0'; ++sc2) { |
| 302 | if (*sc1 == *sc2) |
| 303 | return (char *) sc1; |
| 304 | } |
| 305 | } |
| 306 | return NULL; |
| 307 | } |
| 308 | #endif |
| 309 | |
| 310 | #ifndef __HAVE_ARCH_STRTOK |
| 311 | /** |
| 312 | * strtok - Split a string into tokens |
| 313 | * @s: The string to be searched |
| 314 | * @ct: The characters to search for |
| 315 | * |
| 316 | * WARNING: strtok is deprecated, use strsep instead. |
| 317 | */ |
| 318 | char * strtok(char * s,const char * ct) |
| 319 | { |
| 320 | char *sbegin, *send; |
| 321 | |
| 322 | sbegin = s ? s : ___strtok; |
| 323 | if (!sbegin) { |
| 324 | return NULL; |
| 325 | } |
| 326 | sbegin += strspn(sbegin,ct); |
| 327 | if (*sbegin == '\0') { |
| 328 | ___strtok = NULL; |
| 329 | return( NULL ); |
| 330 | } |
| 331 | send = strpbrk( sbegin, ct); |
| 332 | if (send && *send != '\0') |
| 333 | *send++ = '\0'; |
| 334 | ___strtok = send; |
| 335 | return (sbegin); |
| 336 | } |
| 337 | #endif |
| 338 | |
| 339 | #ifndef __HAVE_ARCH_STRSEP |
| 340 | /** |
| 341 | * strsep - Split a string into tokens |
| 342 | * @s: The string to be searched |
| 343 | * @ct: The characters to search for |
| 344 | * |
| 345 | * strsep() updates @s to point after the token, ready for the next call. |
| 346 | * |
| 347 | * It returns empty tokens, too, behaving exactly like the libc function |
| 348 | * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. |
| 349 | * Same semantics, slimmer shape. ;) |
| 350 | */ |
| 351 | char * strsep(char **s, const char *ct) |
| 352 | { |
| 353 | char *sbegin = *s, *end; |
| 354 | |
| 355 | if (sbegin == NULL) |
| 356 | return NULL; |
| 357 | |
| 358 | end = strpbrk(sbegin, ct); |
| 359 | if (end) |
| 360 | *end++ = '\0'; |
| 361 | *s = end; |
| 362 | |
| 363 | return sbegin; |
| 364 | } |
| 365 | #endif |
| 366 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 367 | #ifndef __HAVE_ARCH_STRSWAB |
| 368 | /** |
| 369 | * strswab - swap adjacent even and odd bytes in %NUL-terminated string |
| 370 | * s: address of the string |
| 371 | * |
| 372 | * returns the address of the swapped string or NULL on error. If |
| 373 | * string length is odd, last byte is untouched. |
| 374 | */ |
| 375 | char *strswab(const char *s) |
| 376 | { |
| 377 | char *p; |
| 378 | |
| 379 | if ((NULL == s) || ('\0' == *s)) { |
| 380 | return (NULL); |
| 381 | } |
| 382 | |
| 383 | for (p = ((char *)s + 1); '\0' != *p; p += 2) { |
| 384 | char tmp; |
| 385 | tmp = *(p-1); |
| 386 | *(p-1) = *p; |
| 387 | *p = tmp; |
| 388 | } |
| 389 | |
| 390 | return (char *) s; |
| 391 | } |
| 392 | #endif |
| 393 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 394 | #ifndef __HAVE_ARCH_MEMSET |
| 395 | /** |
| 396 | * memset - Fill a region of memory with the given value |
| 397 | * @s: Pointer to the start of the area. |
| 398 | * @c: The byte to fill the area with |
| 399 | * @count: The size of the area. |
| 400 | * |
| 401 | * Do not use memset() to access IO space, use memset_io() instead. |
| 402 | */ |
| 403 | void * memset(void * s,int c,size_t count) |
| 404 | { |
| 405 | char *xs = (char *) s; |
| 406 | |
| 407 | while (count--) |
| 408 | *xs++ = c; |
| 409 | |
| 410 | return s; |
| 411 | } |
| 412 | #endif |
| 413 | |
| 414 | #ifndef __HAVE_ARCH_BCOPY |
| 415 | /** |
| 416 | * bcopy - Copy one area of memory to another |
| 417 | * @src: Where to copy from |
| 418 | * @dest: Where to copy to |
| 419 | * @count: The size of the area. |
| 420 | * |
| 421 | * Note that this is the same as memcpy(), with the arguments reversed. |
| 422 | * memcpy() is the standard, bcopy() is a legacy BSD function. |
| 423 | * |
| 424 | * You should not use this function to access IO space, use memcpy_toio() |
| 425 | * or memcpy_fromio() instead. |
| 426 | */ |
| 427 | char * bcopy(const char * src, char * dest, int count) |
| 428 | { |
| 429 | char *tmp = dest; |
| 430 | |
| 431 | while (count--) |
| 432 | *tmp++ = *src++; |
| 433 | |
| 434 | return dest; |
| 435 | } |
| 436 | #endif |
| 437 | |
| 438 | #ifndef __HAVE_ARCH_MEMCPY |
| 439 | /** |
| 440 | * memcpy - Copy one area of memory to another |
| 441 | * @dest: Where to copy to |
| 442 | * @src: Where to copy from |
| 443 | * @count: The size of the area. |
| 444 | * |
| 445 | * You should not use this function to access IO space, use memcpy_toio() |
| 446 | * or memcpy_fromio() instead. |
| 447 | */ |
| 448 | void * memcpy(void * dest,const void *src,size_t count) |
| 449 | { |
| 450 | char *tmp = (char *) dest, *s = (char *) src; |
| 451 | |
| 452 | while (count--) |
| 453 | *tmp++ = *s++; |
| 454 | |
| 455 | return dest; |
| 456 | } |
| 457 | #endif |
| 458 | |
| 459 | #ifndef __HAVE_ARCH_MEMMOVE |
| 460 | /** |
| 461 | * memmove - Copy one area of memory to another |
| 462 | * @dest: Where to copy to |
| 463 | * @src: Where to copy from |
| 464 | * @count: The size of the area. |
| 465 | * |
| 466 | * Unlike memcpy(), memmove() copes with overlapping areas. |
| 467 | */ |
| 468 | void * memmove(void * dest,const void *src,size_t count) |
| 469 | { |
| 470 | char *tmp, *s; |
| 471 | |
| 472 | if (dest <= src) { |
| 473 | tmp = (char *) dest; |
| 474 | s = (char *) src; |
| 475 | while (count--) |
| 476 | *tmp++ = *s++; |
| 477 | } |
| 478 | else { |
| 479 | tmp = (char *) dest + count; |
| 480 | s = (char *) src + count; |
| 481 | while (count--) |
| 482 | *--tmp = *--s; |
| 483 | } |
| 484 | |
| 485 | return dest; |
| 486 | } |
| 487 | #endif |
| 488 | |
| 489 | #ifndef __HAVE_ARCH_MEMCMP |
| 490 | /** |
| 491 | * memcmp - Compare two areas of memory |
| 492 | * @cs: One area of memory |
| 493 | * @ct: Another area of memory |
| 494 | * @count: The size of the area. |
| 495 | */ |
| 496 | int memcmp(const void * cs,const void * ct,size_t count) |
| 497 | { |
| 498 | const unsigned char *su1, *su2; |
| 499 | int res = 0; |
| 500 | |
| 501 | for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) |
| 502 | if ((res = *su1 - *su2) != 0) |
| 503 | break; |
| 504 | return res; |
| 505 | } |
| 506 | #endif |
| 507 | |
| 508 | #ifndef __HAVE_ARCH_MEMSCAN |
| 509 | /** |
| 510 | * memscan - Find a character in an area of memory. |
| 511 | * @addr: The memory area |
| 512 | * @c: The byte to search for |
| 513 | * @size: The size of the area. |
| 514 | * |
| 515 | * returns the address of the first occurrence of @c, or 1 byte past |
| 516 | * the area if @c is not found |
| 517 | */ |
| 518 | void * memscan(void * addr, int c, size_t size) |
| 519 | { |
| 520 | unsigned char * p = (unsigned char *) addr; |
| 521 | |
| 522 | while (size) { |
| 523 | if (*p == c) |
| 524 | return (void *) p; |
| 525 | p++; |
| 526 | size--; |
| 527 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 528 | return (void *) p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 529 | } |
| 530 | #endif |
| 531 | |
| 532 | #ifndef __HAVE_ARCH_STRSTR |
| 533 | /** |
| 534 | * strstr - Find the first substring in a %NUL terminated string |
| 535 | * @s1: The string to be searched |
| 536 | * @s2: The string to search for |
| 537 | */ |
| 538 | char * strstr(const char * s1,const char * s2) |
| 539 | { |
| 540 | int l1, l2; |
| 541 | |
| 542 | l2 = strlen(s2); |
| 543 | if (!l2) |
| 544 | return (char *) s1; |
| 545 | l1 = strlen(s1); |
| 546 | while (l1 >= l2) { |
| 547 | l1--; |
| 548 | if (!memcmp(s1,s2,l2)) |
| 549 | return (char *) s1; |
| 550 | s1++; |
| 551 | } |
| 552 | return NULL; |
| 553 | } |
| 554 | #endif |
| 555 | |
| 556 | #ifndef __HAVE_ARCH_MEMCHR |
| 557 | /** |
| 558 | * memchr - Find a character in an area of memory. |
| 559 | * @s: The memory area |
| 560 | * @c: The byte to search for |
| 561 | * @n: The size of the area. |
| 562 | * |
| 563 | * returns the address of the first occurrence of @c, or %NULL |
| 564 | * if @c is not found |
| 565 | */ |
| 566 | void *memchr(const void *s, int c, size_t n) |
| 567 | { |
| 568 | const unsigned char *p = s; |
| 569 | while (n-- != 0) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 570 | if ((unsigned char)c == *p++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 571 | return (void *)(p-1); |
| 572 | } |
| 573 | } |
| 574 | return NULL; |
| 575 | } |
| 576 | |
| 577 | #endif |