tizen 2.4 release
[kernel/u-boot-tm1.git] / lib / string.c
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
23
24 #if 0 /* not used - was: #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 CONFIG_NAND_SPL
60 #ifndef __HAVE_ARCH_STRCPY
61 /**
62  * strcpy - Copy a %NUL terminated string
63  * @dest: Where to copy the string to
64  * @src: Where to copy the string from
65  */
66 char * strcpy(char * dest,const char *src)
67 {
68         char *tmp = dest;
69
70         while ((*dest++ = *src++) != '\0')
71                 /* nothing */;
72         return tmp;
73 }
74 #endif
75
76 #ifndef __HAVE_ARCH_STRNCPY
77 /**
78  * strncpy - Copy a length-limited, %NUL-terminated string
79  * @dest: Where to copy the string to
80  * @src: Where to copy the string from
81  * @count: The maximum number of bytes to copy
82  *
83  * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
84  * However, the result is not %NUL-terminated if the source exceeds
85  * @count bytes.
86  */
87 char * strncpy(char * dest,const char *src,size_t count)
88 {
89         char *tmp = dest;
90
91         while (count-- && (*dest++ = *src++) != '\0')
92                 /* nothing */;
93
94         return tmp;
95 }
96 #endif
97
98 #ifndef __HAVE_ARCH_STRCAT
99 /**
100  * strcat - Append one %NUL-terminated string to another
101  * @dest: The string to be appended to
102  * @src: The string to append to it
103  */
104 char * strcat(char * dest, const char * src)
105 {
106         char *tmp = dest;
107
108         while (*dest)
109                 dest++;
110         while ((*dest++ = *src++) != '\0')
111                 ;
112
113         return tmp;
114 }
115 #endif
116
117 #ifndef __HAVE_ARCH_STRNCAT
118 /**
119  * strncat - Append a length-limited, %NUL-terminated string to another
120  * @dest: The string to be appended to
121  * @src: The string to append to it
122  * @count: The maximum numbers of bytes to copy
123  *
124  * Note that in contrast to strncpy, strncat ensures the result is
125  * terminated.
126  */
127 char * strncat(char *dest, const char *src, size_t count)
128 {
129         char *tmp = dest;
130
131         if (count) {
132                 while (*dest)
133                         dest++;
134                 while ((*dest++ = *src++)) {
135                         if (--count == 0) {
136                                 *dest = '\0';
137                                 break;
138                         }
139                 }
140         }
141
142         return tmp;
143 }
144 #endif
145
146 #ifndef __HAVE_ARCH_STRCMP
147 /**
148  * strcmp - Compare two strings
149  * @cs: One string
150  * @ct: Another string
151  */
152 int strcmp(const char * cs,const char * ct)
153 {
154         register signed char __res;
155
156         while (1) {
157                 if ((__res = *cs - *ct++) != 0 || !*cs++)
158                         break;
159         }
160
161         return __res;
162 }
163 #endif
164
165 #ifndef __HAVE_ARCH_STRNCMP
166 /**
167  * strncmp - Compare two length-limited strings
168  * @cs: One string
169  * @ct: Another string
170  * @count: The maximum number of bytes to compare
171  */
172 int strncmp(const char * cs,const char * ct,size_t count)
173 {
174         register signed char __res = 0;
175
176         while (count) {
177                 if ((__res = *cs - *ct++) != 0 || !*cs++)
178                         break;
179                 count--;
180         }
181
182         return __res;
183 }
184 #endif
185
186 #ifndef __HAVE_ARCH_STRCHR
187 /**
188  * strchr - Find the first occurrence of a character in a string
189  * @s: The string to be searched
190  * @c: The character to search for
191  */
192 char * strchr(const char * s, int c)
193 {
194         for(; *s != (char) c; ++s)
195                 if (*s == '\0')
196                         return NULL;
197         return (char *) s;
198 }
199 #endif
200
201 #ifndef __HAVE_ARCH_STRRCHR
202 /**
203  * strrchr - Find the last occurrence of a character in a string
204  * @s: The string to be searched
205  * @c: The character to search for
206  */
207 char * strrchr(const char * s, int c)
208 {
209        const char *p = s + strlen(s);
210        do {
211            if (*p == (char)c)
212                return (char *)p;
213        } while (--p >= s);
214        return NULL;
215 }
216 #endif
217
218 #ifndef __HAVE_ARCH_STRLEN
219 /**
220  * strlen - Find the length of a string
221  * @s: The string to be sized
222  */
223 size_t strlen(const char * s)
224 {
225         const char *sc;
226
227         for (sc = s; *sc != '\0'; ++sc)
228                 /* nothing */;
229         return sc - s;
230 }
231 #endif
232
233 #ifndef __HAVE_ARCH_STRNLEN
234 /**
235  * strnlen - Find the length of a length-limited string
236  * @s: The string to be sized
237  * @count: The maximum number of bytes to search
238  */
239 size_t strnlen(const char * s, size_t count)
240 {
241         const char *sc;
242
243         for (sc = s; count-- && *sc != '\0'; ++sc)
244                 /* nothing */;
245         return sc - s;
246 }
247 #endif
248
249 #ifndef __HAVE_ARCH_STRDUP
250 char * strdup(const char *s)
251 {
252         char *new;
253
254         if ((s == NULL) ||
255             ((new = malloc (strlen(s) + 1)) == NULL) ) {
256                 return NULL;
257         }
258
259         strcpy (new, s);
260         return new;
261 }
262 #endif
263
264 #ifndef __HAVE_ARCH_STRSPN
265 /**
266  * strspn - Calculate the length of the initial substring of @s which only
267  *      contain letters in @accept
268  * @s: The string to be searched
269  * @accept: The string to search for
270  */
271 size_t strspn(const char *s, const char *accept)
272 {
273         const char *p;
274         const char *a;
275         size_t count = 0;
276
277         for (p = s; *p != '\0'; ++p) {
278                 for (a = accept; *a != '\0'; ++a) {
279                         if (*p == *a)
280                                 break;
281                 }
282                 if (*a == '\0')
283                         return count;
284                 ++count;
285         }
286
287         return count;
288 }
289 #endif
290
291 #ifndef __HAVE_ARCH_STRPBRK
292 /**
293  * strpbrk - Find the first occurrence of a set of characters
294  * @cs: The string to be searched
295  * @ct: The characters to search for
296  */
297 char * strpbrk(const char * cs,const char * ct)
298 {
299         const char *sc1,*sc2;
300
301         for( sc1 = cs; *sc1 != '\0'; ++sc1) {
302                 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
303                         if (*sc1 == *sc2)
304                                 return (char *) sc1;
305                 }
306         }
307         return NULL;
308 }
309 #endif
310
311 #ifndef __HAVE_ARCH_STRTOK
312 /**
313  * strtok - Split a string into tokens
314  * @s: The string to be searched
315  * @ct: The characters to search for
316  *
317  * WARNING: strtok is deprecated, use strsep instead.
318  */
319 char * strtok(char * s,const char * ct)
320 {
321         char *sbegin, *send;
322
323         sbegin  = s ? s : ___strtok;
324         if (!sbegin) {
325                 return NULL;
326         }
327         sbegin += strspn(sbegin,ct);
328         if (*sbegin == '\0') {
329                 ___strtok = NULL;
330                 return( NULL );
331         }
332         send = strpbrk( sbegin, ct);
333         if (send && *send != '\0')
334                 *send++ = '\0';
335         ___strtok = send;
336         return (sbegin);
337 }
338 #endif
339
340 #ifndef __HAVE_ARCH_STRSEP
341 /**
342  * strsep - Split a string into tokens
343  * @s: The string to be searched
344  * @ct: The characters to search for
345  *
346  * strsep() updates @s to point after the token, ready for the next call.
347  *
348  * It returns empty tokens, too, behaving exactly like the libc function
349  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
350  * Same semantics, slimmer shape. ;)
351  */
352 char * strsep(char **s, const char *ct)
353 {
354         char *sbegin = *s, *end;
355
356         if (sbegin == NULL)
357                 return NULL;
358
359         end = strpbrk(sbegin, ct);
360         if (end)
361                 *end++ = '\0';
362         *s = end;
363
364         return sbegin;
365 }
366 #endif
367
368 #ifndef __HAVE_ARCH_STRSWAB
369 /**
370  * strswab - swap adjacent even and odd bytes in %NUL-terminated string
371  * s: address of the string
372  *
373  * returns the address of the swapped string or NULL on error. If
374  * string length is odd, last byte is untouched.
375  */
376 char *strswab(const char *s)
377 {
378         char *p, *q;
379
380         if ((NULL == s) || ('\0' == *s)) {
381                 return (NULL);
382         }
383
384         for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
385                 char  tmp;
386
387                 tmp = *p;
388                 *p  = *q;
389                 *q  = tmp;
390         }
391
392         return (char *) s;
393 }
394 #endif
395 #endif
396
397 #ifndef __HAVE_ARCH_MEMSET
398 /**
399  * memset - Fill a region of memory with the given value
400  * @s: Pointer to the start of the area.
401  * @c: The byte to fill the area with
402  * @count: The size of the area.
403  *
404  * Do not use memset() to access IO space, use memset_io() instead.
405  */
406 void * memset(void * s,int c,size_t count)
407 {
408         unsigned long *sl = (unsigned long *) s;
409         unsigned long cl = 0;
410         char *s8;
411         int i;
412
413         /* do it one word at a time (32 bits or 64 bits) while possible */
414         if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
415                 for (i = 0; i < sizeof(*sl); i++) {
416                         cl <<= 8;
417                         cl |= c & 0xff;
418                 }
419                 while (count >= sizeof(*sl)) {
420                         *sl++ = cl;
421                         count -= sizeof(*sl);
422                 }
423         }
424         /* fill 8 bits at a time */
425         s8 = (char *)sl;
426         while (count--)
427                 *s8++ = c;
428
429         return s;
430 }
431 #endif
432
433 #ifndef CONFIG_NAND_SPL
434 #ifndef __HAVE_ARCH_BCOPY
435 /**
436  * bcopy - Copy one area of memory to another
437  * @src: Where to copy from
438  * @dest: Where to copy to
439  * @count: The size of the area.
440  *
441  * Note that this is the same as memcpy(), with the arguments reversed.
442  * memcpy() is the standard, bcopy() is a legacy BSD function.
443  *
444  * You should not use this function to access IO space, use memcpy_toio()
445  * or memcpy_fromio() instead.
446  */
447 char * bcopy(const char * src, char * dest, int count)
448 {
449         char *tmp = dest;
450
451         while (count--)
452                 *tmp++ = *src++;
453
454         return dest;
455 }
456 #endif
457 #endif
458
459 #ifndef __HAVE_ARCH_MEMCPY
460 /**
461  * memcpy - 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  * You should not use this function to access IO space, use memcpy_toio()
467  * or memcpy_fromio() instead.
468  */
469 void * memcpy(void *dest, const void *src, size_t count)
470 {
471         unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
472         char *d8, *s8;
473
474         /* while all data is aligned (common case), copy a word at a time */
475         if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
476                 while (count >= sizeof(*dl)) {
477                         *dl++ = *sl++;
478                         count -= sizeof(*dl);
479                 }
480         }
481         /* copy the reset one byte at a time */
482         d8 = (char *)dl;
483         s8 = (char *)sl;
484         while (count--)
485                 *d8++ = *s8++;
486
487         return dest;
488 }
489 #endif
490
491 #ifndef CONFIG_NAND_SPL
492 #ifndef __HAVE_ARCH_MEMMOVE
493 /**
494  * memmove - Copy one area of memory to another
495  * @dest: Where to copy to
496  * @src: Where to copy from
497  * @count: The size of the area.
498  *
499  * Unlike memcpy(), memmove() copes with overlapping areas.
500  */
501 void * memmove(void * dest,const void *src,size_t count)
502 {
503         char *tmp, *s;
504
505         if (dest <= src) {
506                 tmp = (char *) dest;
507                 s = (char *) src;
508                 while (count--)
509                         *tmp++ = *s++;
510                 }
511         else {
512                 tmp = (char *) dest + count;
513                 s = (char *) src + count;
514                 while (count--)
515                         *--tmp = *--s;
516                 }
517
518         return dest;
519 }
520 #endif
521
522 #ifndef __HAVE_ARCH_MEMCMP
523 /**
524  * memcmp - Compare two areas of memory
525  * @cs: One area of memory
526  * @ct: Another area of memory
527  * @count: The size of the area.
528  */
529 int memcmp(const void * cs,const void * ct,size_t count)
530 {
531         const unsigned char *su1, *su2;
532         int res = 0;
533
534         for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
535                 if ((res = *su1 - *su2) != 0)
536                         break;
537         return res;
538 }
539 #endif
540
541 #ifndef __HAVE_ARCH_MEMSCAN
542 /**
543  * memscan - Find a character in an area of memory.
544  * @addr: The memory area
545  * @c: The byte to search for
546  * @size: The size of the area.
547  *
548  * returns the address of the first occurrence of @c, or 1 byte past
549  * the area if @c is not found
550  */
551 void * memscan(void * addr, int c, size_t size)
552 {
553         unsigned char * p = (unsigned char *) addr;
554
555         while (size) {
556                 if (*p == c)
557                         return (void *) p;
558                 p++;
559                 size--;
560         }
561         return (void *) p;
562 }
563 #endif
564
565 #ifndef __HAVE_ARCH_STRSTR
566 /**
567  * strstr - Find the first substring in a %NUL terminated string
568  * @s1: The string to be searched
569  * @s2: The string to search for
570  */
571 char * strstr(const char * s1,const char * s2)
572 {
573         int l1, l2;
574
575         l2 = strlen(s2);
576         if (!l2)
577                 return (char *) s1;
578         l1 = strlen(s1);
579         while (l1 >= l2) {
580                 l1--;
581                 if (!memcmp(s1,s2,l2))
582                         return (char *) s1;
583                 s1++;
584         }
585         return NULL;
586 }
587 #endif
588
589 #ifndef __HAVE_ARCH_MEMCHR
590 /**
591  * memchr - Find a character in an area of memory.
592  * @s: The memory area
593  * @c: The byte to search for
594  * @n: The size of the area.
595  *
596  * returns the address of the first occurrence of @c, or %NULL
597  * if @c is not found
598  */
599 void *memchr(const void *s, int c, size_t n)
600 {
601         const unsigned char *p = s;
602         while (n-- != 0) {
603                 if ((unsigned char)c == *p++) {
604                         return (void *)(p-1);
605                 }
606         }
607         return NULL;
608 }
609
610 #endif
611 #endif
612
613 #ifndef __HAVE_ARCH_WCSNCMP
614 int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n)
615 {
616
617         if (n == 0)
618                 return (0);
619         do {
620                 if (*s1 != *s2++) {
621                         /* XXX assumes wchar_t = int */
622                         return (*(const unsigned short *)s1 -
623                             *(const unsigned short *)--s2);
624                 }
625                 if (*s1++ == 0)
626                         break;
627         } while (--n != 0);
628         return (0);
629 }
630 #endif
631
632 #ifndef __HAVE_ARCH_WCSLEN
633 size_t wcslen(const wchar_t *s)
634 {
635         const wchar_t *p;
636
637         p = s;
638         while (*p)
639                 p++;
640
641         return p - s;
642 }
643 #endif
644
645 #ifndef __HAVE_ARCH_WCSCMP
646 int wcscmp(const wchar_t *s1, const wchar_t *s2)
647 {
648         while (*s1 == *s2++)
649                 if (*s1++ == '\0')
650                         return (0);
651         /* XXX assumes wchar_t = int */
652         return (*(const unsigned int *)s1 - *(const unsigned int *)--s2);
653 }
654 #endif
655