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