diff options
-rw-r--r-- | lib/string.c | 113 |
1 files changed, 53 insertions, 60 deletions
diff --git a/lib/string.c b/lib/string.c index d886ef157c12..54eb3f81f85e 100644 --- a/lib/string.c +++ b/lib/string.c | |||
@@ -39,8 +39,10 @@ int strnicmp(const char *s1, const char *s2, size_t len) | |||
39 | c1 = 0; c2 = 0; | 39 | c1 = 0; c2 = 0; |
40 | if (len) { | 40 | if (len) { |
41 | do { | 41 | do { |
42 | c1 = *s1; c2 = *s2; | 42 | c1 = *s1; |
43 | s1++; s2++; | 43 | c2 = *s2; |
44 | s1++; | ||
45 | s2++; | ||
44 | if (!c1) | 46 | if (!c1) |
45 | break; | 47 | break; |
46 | if (!c2) | 48 | if (!c2) |
@@ -55,7 +57,6 @@ int strnicmp(const char *s1, const char *s2, size_t len) | |||
55 | } | 57 | } |
56 | return (int)c1 - (int)c2; | 58 | return (int)c1 - (int)c2; |
57 | } | 59 | } |
58 | |||
59 | EXPORT_SYMBOL(strnicmp); | 60 | EXPORT_SYMBOL(strnicmp); |
60 | #endif | 61 | #endif |
61 | 62 | ||
@@ -66,7 +67,7 @@ EXPORT_SYMBOL(strnicmp); | |||
66 | * @src: Where to copy the string from | 67 | * @src: Where to copy the string from |
67 | */ | 68 | */ |
68 | #undef strcpy | 69 | #undef strcpy |
69 | char * strcpy(char * dest,const char *src) | 70 | char *strcpy(char *dest, const char *src) |
70 | { | 71 | { |
71 | char *tmp = dest; | 72 | char *tmp = dest; |
72 | 73 | ||
@@ -91,12 +92,13 @@ EXPORT_SYMBOL(strcpy); | |||
91 | * count, the remainder of @dest will be padded with %NUL. | 92 | * count, the remainder of @dest will be padded with %NUL. |
92 | * | 93 | * |
93 | */ | 94 | */ |
94 | char * strncpy(char * dest,const char *src,size_t count) | 95 | char *strncpy(char *dest, const char *src, size_t count) |
95 | { | 96 | { |
96 | char *tmp = dest; | 97 | char *tmp = dest; |
97 | 98 | ||
98 | while (count) { | 99 | while (count) { |
99 | if ((*tmp = *src) != 0) src++; | 100 | if ((*tmp = *src) != 0) |
101 | src++; | ||
100 | tmp++; | 102 | tmp++; |
101 | count--; | 103 | count--; |
102 | } | 104 | } |
@@ -122,7 +124,7 @@ size_t strlcpy(char *dest, const char *src, size_t size) | |||
122 | size_t ret = strlen(src); | 124 | size_t ret = strlen(src); |
123 | 125 | ||
124 | if (size) { | 126 | if (size) { |
125 | size_t len = (ret >= size) ? size-1 : ret; | 127 | size_t len = (ret >= size) ? size - 1 : ret; |
126 | memcpy(dest, src, len); | 128 | memcpy(dest, src, len); |
127 | dest[len] = '\0'; | 129 | dest[len] = '\0'; |
128 | } | 130 | } |
@@ -138,7 +140,7 @@ EXPORT_SYMBOL(strlcpy); | |||
138 | * @src: The string to append to it | 140 | * @src: The string to append to it |
139 | */ | 141 | */ |
140 | #undef strcat | 142 | #undef strcat |
141 | char * strcat(char * dest, const char * src) | 143 | char *strcat(char *dest, const char *src) |
142 | { | 144 | { |
143 | char *tmp = dest; | 145 | char *tmp = dest; |
144 | 146 | ||
@@ -162,7 +164,7 @@ EXPORT_SYMBOL(strcat); | |||
162 | * Note that in contrast to strncpy, strncat ensures the result is | 164 | * Note that in contrast to strncpy, strncat ensures the result is |
163 | * terminated. | 165 | * terminated. |
164 | */ | 166 | */ |
165 | char * strncat(char *dest, const char *src, size_t count) | 167 | char *strncat(char *dest, const char *src, size_t count) |
166 | { | 168 | { |
167 | char *tmp = dest; | 169 | char *tmp = dest; |
168 | 170 | ||
@@ -176,7 +178,6 @@ char * strncat(char *dest, const char *src, size_t count) | |||
176 | } | 178 | } |
177 | } | 179 | } |
178 | } | 180 | } |
179 | |||
180 | return tmp; | 181 | return tmp; |
181 | } | 182 | } |
182 | EXPORT_SYMBOL(strncat); | 183 | EXPORT_SYMBOL(strncat); |
@@ -216,7 +217,7 @@ EXPORT_SYMBOL(strlcat); | |||
216 | * @ct: Another string | 217 | * @ct: Another string |
217 | */ | 218 | */ |
218 | #undef strcmp | 219 | #undef strcmp |
219 | int strcmp(const char * cs,const char * ct) | 220 | int strcmp(const char *cs, const char *ct) |
220 | { | 221 | { |
221 | register signed char __res; | 222 | register signed char __res; |
222 | 223 | ||
@@ -224,7 +225,6 @@ int strcmp(const char * cs,const char * ct) | |||
224 | if ((__res = *cs - *ct++) != 0 || !*cs++) | 225 | if ((__res = *cs - *ct++) != 0 || !*cs++) |
225 | break; | 226 | break; |
226 | } | 227 | } |
227 | |||
228 | return __res; | 228 | return __res; |
229 | } | 229 | } |
230 | EXPORT_SYMBOL(strcmp); | 230 | EXPORT_SYMBOL(strcmp); |
@@ -237,7 +237,7 @@ EXPORT_SYMBOL(strcmp); | |||
237 | * @ct: Another string | 237 | * @ct: Another string |
238 | * @count: The maximum number of bytes to compare | 238 | * @count: The maximum number of bytes to compare |
239 | */ | 239 | */ |
240 | int strncmp(const char * cs,const char * ct,size_t count) | 240 | int strncmp(const char *cs, const char *ct, size_t count) |
241 | { | 241 | { |
242 | register signed char __res = 0; | 242 | register signed char __res = 0; |
243 | 243 | ||
@@ -246,7 +246,6 @@ int strncmp(const char * cs,const char * ct,size_t count) | |||
246 | break; | 246 | break; |
247 | count--; | 247 | count--; |
248 | } | 248 | } |
249 | |||
250 | return __res; | 249 | return __res; |
251 | } | 250 | } |
252 | EXPORT_SYMBOL(strncmp); | 251 | EXPORT_SYMBOL(strncmp); |
@@ -258,12 +257,12 @@ EXPORT_SYMBOL(strncmp); | |||
258 | * @s: The string to be searched | 257 | * @s: The string to be searched |
259 | * @c: The character to search for | 258 | * @c: The character to search for |
260 | */ | 259 | */ |
261 | char * strchr(const char * s, int c) | 260 | char *strchr(const char *s, int c) |
262 | { | 261 | { |
263 | for(; *s != (char) c; ++s) | 262 | for (; *s != (char)c; ++s) |
264 | if (*s == '\0') | 263 | if (*s == '\0') |
265 | return NULL; | 264 | return NULL; |
266 | return (char *) s; | 265 | return (char *)s; |
267 | } | 266 | } |
268 | EXPORT_SYMBOL(strchr); | 267 | EXPORT_SYMBOL(strchr); |
269 | #endif | 268 | #endif |
@@ -274,7 +273,7 @@ EXPORT_SYMBOL(strchr); | |||
274 | * @s: The string to be searched | 273 | * @s: The string to be searched |
275 | * @c: The character to search for | 274 | * @c: The character to search for |
276 | */ | 275 | */ |
277 | char * strrchr(const char * s, int c) | 276 | char *strrchr(const char *s, int c) |
278 | { | 277 | { |
279 | const char *p = s + strlen(s); | 278 | const char *p = s + strlen(s); |
280 | do { | 279 | do { |
@@ -296,8 +295,8 @@ EXPORT_SYMBOL(strrchr); | |||
296 | char *strnchr(const char *s, size_t count, int c) | 295 | char *strnchr(const char *s, size_t count, int c) |
297 | { | 296 | { |
298 | for (; count-- && *s != '\0'; ++s) | 297 | for (; count-- && *s != '\0'; ++s) |
299 | if (*s == (char) c) | 298 | if (*s == (char)c) |
300 | return (char *) s; | 299 | return (char *)s; |
301 | return NULL; | 300 | return NULL; |
302 | } | 301 | } |
303 | EXPORT_SYMBOL(strnchr); | 302 | EXPORT_SYMBOL(strnchr); |
@@ -308,7 +307,7 @@ EXPORT_SYMBOL(strnchr); | |||
308 | * strlen - Find the length of a string | 307 | * strlen - Find the length of a string |
309 | * @s: The string to be sized | 308 | * @s: The string to be sized |
310 | */ | 309 | */ |
311 | size_t strlen(const char * s) | 310 | size_t strlen(const char *s) |
312 | { | 311 | { |
313 | const char *sc; | 312 | const char *sc; |
314 | 313 | ||
@@ -325,7 +324,7 @@ EXPORT_SYMBOL(strlen); | |||
325 | * @s: The string to be sized | 324 | * @s: The string to be sized |
326 | * @count: The maximum number of bytes to search | 325 | * @count: The maximum number of bytes to search |
327 | */ | 326 | */ |
328 | size_t strnlen(const char * s, size_t count) | 327 | size_t strnlen(const char *s, size_t count) |
329 | { | 328 | { |
330 | const char *sc; | 329 | const char *sc; |
331 | 330 | ||
@@ -358,7 +357,6 @@ size_t strspn(const char *s, const char *accept) | |||
358 | return count; | 357 | return count; |
359 | ++count; | 358 | ++count; |
360 | } | 359 | } |
361 | |||
362 | return count; | 360 | return count; |
363 | } | 361 | } |
364 | 362 | ||
@@ -384,9 +382,8 @@ size_t strcspn(const char *s, const char *reject) | |||
384 | } | 382 | } |
385 | ++count; | 383 | ++count; |
386 | } | 384 | } |
387 | |||
388 | return count; | 385 | return count; |
389 | } | 386 | } |
390 | EXPORT_SYMBOL(strcspn); | 387 | EXPORT_SYMBOL(strcspn); |
391 | 388 | ||
392 | #ifndef __HAVE_ARCH_STRPBRK | 389 | #ifndef __HAVE_ARCH_STRPBRK |
@@ -395,14 +392,14 @@ EXPORT_SYMBOL(strcspn); | |||
395 | * @cs: The string to be searched | 392 | * @cs: The string to be searched |
396 | * @ct: The characters to search for | 393 | * @ct: The characters to search for |
397 | */ | 394 | */ |
398 | char * strpbrk(const char * cs,const char * ct) | 395 | char *strpbrk(const char *cs, const char *ct) |
399 | { | 396 | { |
400 | const char *sc1,*sc2; | 397 | const char *sc1, *sc2; |
401 | 398 | ||
402 | for( sc1 = cs; *sc1 != '\0'; ++sc1) { | 399 | for (sc1 = cs; *sc1 != '\0'; ++sc1) { |
403 | for( sc2 = ct; *sc2 != '\0'; ++sc2) { | 400 | for (sc2 = ct; *sc2 != '\0'; ++sc2) { |
404 | if (*sc1 == *sc2) | 401 | if (*sc1 == *sc2) |
405 | return (char *) sc1; | 402 | return (char *)sc1; |
406 | } | 403 | } |
407 | } | 404 | } |
408 | return NULL; | 405 | return NULL; |
@@ -422,9 +419,10 @@ EXPORT_SYMBOL(strpbrk); | |||
422 | * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. | 419 | * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. |
423 | * Same semantics, slimmer shape. ;) | 420 | * Same semantics, slimmer shape. ;) |
424 | */ | 421 | */ |
425 | char * strsep(char **s, const char *ct) | 422 | char *strsep(char **s, const char *ct) |
426 | { | 423 | { |
427 | char *sbegin = *s, *end; | 424 | char *sbegin = *s; |
425 | char *end; | ||
428 | 426 | ||
429 | if (sbegin == NULL) | 427 | if (sbegin == NULL) |
430 | return NULL; | 428 | return NULL; |
@@ -433,10 +431,8 @@ char * strsep(char **s, const char *ct) | |||
433 | if (end) | 431 | if (end) |
434 | *end++ = '\0'; | 432 | *end++ = '\0'; |
435 | *s = end; | 433 | *s = end; |
436 | |||
437 | return sbegin; | 434 | return sbegin; |
438 | } | 435 | } |
439 | |||
440 | EXPORT_SYMBOL(strsep); | 436 | EXPORT_SYMBOL(strsep); |
441 | #endif | 437 | #endif |
442 | 438 | ||
@@ -449,13 +445,12 @@ EXPORT_SYMBOL(strsep); | |||
449 | * | 445 | * |
450 | * Do not use memset() to access IO space, use memset_io() instead. | 446 | * Do not use memset() to access IO space, use memset_io() instead. |
451 | */ | 447 | */ |
452 | void * memset(void * s,int c,size_t count) | 448 | void *memset(void *s, int c, size_t count) |
453 | { | 449 | { |
454 | char *xs = (char *) s; | 450 | char *xs = (char *)s; |
455 | 451 | ||
456 | while (count--) | 452 | while (count--) |
457 | *xs++ = c; | 453 | *xs++ = c; |
458 | |||
459 | return s; | 454 | return s; |
460 | } | 455 | } |
461 | EXPORT_SYMBOL(memset); | 456 | EXPORT_SYMBOL(memset); |
@@ -471,13 +466,13 @@ EXPORT_SYMBOL(memset); | |||
471 | * You should not use this function to access IO space, use memcpy_toio() | 466 | * You should not use this function to access IO space, use memcpy_toio() |
472 | * or memcpy_fromio() instead. | 467 | * or memcpy_fromio() instead. |
473 | */ | 468 | */ |
474 | void * memcpy(void * dest,const void *src,size_t count) | 469 | void *memcpy(void *dest, const void *src, size_t count) |
475 | { | 470 | { |
476 | char *tmp = (char *) dest, *s = (char *) src; | 471 | char *tmp = (char *)dest; |
472 | char *s = (char *)src; | ||
477 | 473 | ||
478 | while (count--) | 474 | while (count--) |
479 | *tmp++ = *s++; | 475 | *tmp++ = *s++; |
480 | |||
481 | return dest; | 476 | return dest; |
482 | } | 477 | } |
483 | EXPORT_SYMBOL(memcpy); | 478 | EXPORT_SYMBOL(memcpy); |
@@ -492,23 +487,21 @@ EXPORT_SYMBOL(memcpy); | |||
492 | * | 487 | * |
493 | * Unlike memcpy(), memmove() copes with overlapping areas. | 488 | * Unlike memcpy(), memmove() copes with overlapping areas. |
494 | */ | 489 | */ |
495 | void * memmove(void * dest,const void *src,size_t count) | 490 | void *memmove(void *dest, const void *src, size_t count) |
496 | { | 491 | { |
497 | char *tmp, *s; | 492 | char *tmp, *s; |
498 | 493 | ||
499 | if (dest <= src) { | 494 | if (dest <= src) { |
500 | tmp = (char *) dest; | 495 | tmp = (char *)dest; |
501 | s = (char *) src; | 496 | s = (char *)src; |
502 | while (count--) | 497 | while (count--) |
503 | *tmp++ = *s++; | 498 | *tmp++ = *s++; |
504 | } | 499 | } else { |
505 | else { | 500 | tmp = (char *)dest + count; |
506 | tmp = (char *) dest + count; | 501 | s = (char *)src + count; |
507 | s = (char *) src + count; | ||
508 | while (count--) | 502 | while (count--) |
509 | *--tmp = *--s; | 503 | *--tmp = *--s; |
510 | } | 504 | } |
511 | |||
512 | return dest; | 505 | return dest; |
513 | } | 506 | } |
514 | EXPORT_SYMBOL(memmove); | 507 | EXPORT_SYMBOL(memmove); |
@@ -522,12 +515,12 @@ EXPORT_SYMBOL(memmove); | |||
522 | * @count: The size of the area. | 515 | * @count: The size of the area. |
523 | */ | 516 | */ |
524 | #undef memcmp | 517 | #undef memcmp |
525 | int memcmp(const void * cs,const void * ct,size_t count) | 518 | int memcmp(const void *cs, const void *ct, size_t count) |
526 | { | 519 | { |
527 | const unsigned char *su1, *su2; | 520 | const unsigned char *su1, *su2; |
528 | int res = 0; | 521 | int res = 0; |
529 | 522 | ||
530 | for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) | 523 | for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) |
531 | if ((res = *su1 - *su2) != 0) | 524 | if ((res = *su1 - *su2) != 0) |
532 | break; | 525 | break; |
533 | return res; | 526 | return res; |
@@ -545,17 +538,17 @@ EXPORT_SYMBOL(memcmp); | |||
545 | * returns the address of the first occurrence of @c, or 1 byte past | 538 | * returns the address of the first occurrence of @c, or 1 byte past |
546 | * the area if @c is not found | 539 | * the area if @c is not found |
547 | */ | 540 | */ |
548 | void * memscan(void * addr, int c, size_t size) | 541 | void *memscan(void *addr, int c, size_t size) |
549 | { | 542 | { |
550 | unsigned char * p = (unsigned char *) addr; | 543 | unsigned char *p = (unsigned char *)addr; |
551 | 544 | ||
552 | while (size) { | 545 | while (size) { |
553 | if (*p == c) | 546 | if (*p == c) |
554 | return (void *) p; | 547 | return (void *)p; |
555 | p++; | 548 | p++; |
556 | size--; | 549 | size--; |
557 | } | 550 | } |
558 | return (void *) p; | 551 | return (void *)p; |
559 | } | 552 | } |
560 | EXPORT_SYMBOL(memscan); | 553 | EXPORT_SYMBOL(memscan); |
561 | #endif | 554 | #endif |
@@ -566,18 +559,18 @@ EXPORT_SYMBOL(memscan); | |||
566 | * @s1: The string to be searched | 559 | * @s1: The string to be searched |
567 | * @s2: The string to search for | 560 | * @s2: The string to search for |
568 | */ | 561 | */ |
569 | char * strstr(const char * s1,const char * s2) | 562 | char *strstr(const char *s1, const char *s2) |
570 | { | 563 | { |
571 | int l1, l2; | 564 | int l1, l2; |
572 | 565 | ||
573 | l2 = strlen(s2); | 566 | l2 = strlen(s2); |
574 | if (!l2) | 567 | if (!l2) |
575 | return (char *) s1; | 568 | return (char *)s1; |
576 | l1 = strlen(s1); | 569 | l1 = strlen(s1); |
577 | while (l1 >= l2) { | 570 | while (l1 >= l2) { |
578 | l1--; | 571 | l1--; |
579 | if (!memcmp(s1,s2,l2)) | 572 | if (!memcmp(s1, s2, l2)) |
580 | return (char *) s1; | 573 | return (char *)s1; |
581 | s1++; | 574 | s1++; |
582 | } | 575 | } |
583 | return NULL; | 576 | return NULL; |
@@ -600,7 +593,7 @@ void *memchr(const void *s, int c, size_t n) | |||
600 | const unsigned char *p = s; | 593 | const unsigned char *p = s; |
601 | while (n-- != 0) { | 594 | while (n-- != 0) { |
602 | if ((unsigned char)c == *p++) { | 595 | if ((unsigned char)c == *p++) { |
603 | return (void *)(p-1); | 596 | return (void *)(p - 1); |
604 | } | 597 | } |
605 | } | 598 | } |
606 | return NULL; | 599 | return NULL; |