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