4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * stupid library routines.. The optimized versions should generally be found
9 * as inline code in <asm-xx/string.h>
11 * These are buggy as well..
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.
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/ctype.h>
24 #if 0 /* not used - was: #ifndef __HAVE_ARCH_STRNICMP */
26 * strnicmp - Case insensitive, length-limited string comparison
28 * @s2: The other string
29 * @len: the maximum number of characters to compare
31 int strnicmp(const char *s1, const char *s2, size_t len)
33 /* Yes, Virginia, it had better be unsigned */
53 return (int)c1 - (int)c2;
59 #ifndef CONFIG_NAND_SPL
60 #ifndef __HAVE_ARCH_STRCPY
62 * strcpy - Copy a %NUL terminated string
63 * @dest: Where to copy the string to
64 * @src: Where to copy the string from
66 char * strcpy(char * dest,const char *src)
70 while ((*dest++ = *src++) != '\0')
76 #ifndef __HAVE_ARCH_STRNCPY
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
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
87 char * strncpy(char * dest,const char *src,size_t count)
91 while (count-- && (*dest++ = *src++) != '\0')
98 #ifndef __HAVE_ARCH_STRCAT
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
104 char * strcat(char * dest, const char * src)
110 while ((*dest++ = *src++) != '\0')
117 #ifndef __HAVE_ARCH_STRNCAT
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
124 * Note that in contrast to strncpy, strncat ensures the result is
127 char * strncat(char *dest, const char *src, size_t count)
134 while ((*dest++ = *src++)) {
146 #ifndef __HAVE_ARCH_STRCMP
148 * strcmp - Compare two strings
150 * @ct: Another string
152 int strcmp(const char * cs,const char * ct)
154 register signed char __res;
157 if ((__res = *cs - *ct++) != 0 || !*cs++)
165 #ifndef __HAVE_ARCH_STRNCMP
167 * strncmp - Compare two length-limited strings
169 * @ct: Another string
170 * @count: The maximum number of bytes to compare
172 int strncmp(const char * cs,const char * ct,size_t count)
174 register signed char __res = 0;
177 if ((__res = *cs - *ct++) != 0 || !*cs++)
186 #ifndef __HAVE_ARCH_STRCHR
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
192 char * strchr(const char * s, int c)
194 for(; *s != (char) c; ++s)
201 #ifndef __HAVE_ARCH_STRRCHR
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
207 char * strrchr(const char * s, int c)
209 const char *p = s + strlen(s);
218 #ifndef __HAVE_ARCH_STRLEN
220 * strlen - Find the length of a string
221 * @s: The string to be sized
223 size_t strlen(const char * s)
227 for (sc = s; *sc != '\0'; ++sc)
233 #ifndef __HAVE_ARCH_STRNLEN
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
239 size_t strnlen(const char * s, size_t count)
243 for (sc = s; count-- && *sc != '\0'; ++sc)
249 #ifndef __HAVE_ARCH_STRDUP
250 char * strdup(const char *s)
255 ((new = malloc (strlen(s) + 1)) == NULL) ) {
264 #ifndef __HAVE_ARCH_STRSPN
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
271 size_t strspn(const char *s, const char *accept)
277 for (p = s; *p != '\0'; ++p) {
278 for (a = accept; *a != '\0'; ++a) {
291 #ifndef __HAVE_ARCH_STRPBRK
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
297 char * strpbrk(const char * cs,const char * ct)
299 const char *sc1,*sc2;
301 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
302 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
311 #ifndef __HAVE_ARCH_STRTOK
313 * strtok - Split a string into tokens
314 * @s: The string to be searched
315 * @ct: The characters to search for
317 * WARNING: strtok is deprecated, use strsep instead.
319 char * strtok(char * s,const char * ct)
323 sbegin = s ? s : ___strtok;
327 sbegin += strspn(sbegin,ct);
328 if (*sbegin == '\0') {
332 send = strpbrk( sbegin, ct);
333 if (send && *send != '\0')
340 #ifndef __HAVE_ARCH_STRSEP
342 * strsep - Split a string into tokens
343 * @s: The string to be searched
344 * @ct: The characters to search for
346 * strsep() updates @s to point after the token, ready for the next call.
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. ;)
352 char * strsep(char **s, const char *ct)
354 char *sbegin = *s, *end;
359 end = strpbrk(sbegin, ct);
368 #ifndef __HAVE_ARCH_STRSWAB
370 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
371 * s: address of the string
373 * returns the address of the swapped string or NULL on error. If
374 * string length is odd, last byte is untouched.
376 char *strswab(const char *s)
380 if ((NULL == s) || ('\0' == *s)) {
384 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
397 #ifndef __HAVE_ARCH_MEMSET
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.
404 * Do not use memset() to access IO space, use memset_io() instead.
406 void * memset(void * s,int c,size_t count)
408 unsigned long *sl = (unsigned long *) s;
409 unsigned long cl = 0;
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++) {
419 while (count >= sizeof(*sl)) {
421 count -= sizeof(*sl);
424 /* fill 8 bits at a time */
433 #ifndef CONFIG_NAND_SPL
434 #ifndef __HAVE_ARCH_BCOPY
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.
441 * Note that this is the same as memcpy(), with the arguments reversed.
442 * memcpy() is the standard, bcopy() is a legacy BSD function.
444 * You should not use this function to access IO space, use memcpy_toio()
445 * or memcpy_fromio() instead.
447 char * bcopy(const char * src, char * dest, int count)
459 #ifndef __HAVE_ARCH_MEMCPY
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.
466 * You should not use this function to access IO space, use memcpy_toio()
467 * or memcpy_fromio() instead.
469 void * memcpy(void *dest, const void *src, size_t count)
471 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
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)) {
478 count -= sizeof(*dl);
481 /* copy the reset one byte at a time */
491 #ifndef CONFIG_NAND_SPL
492 #ifndef __HAVE_ARCH_MEMMOVE
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.
499 * Unlike memcpy(), memmove() copes with overlapping areas.
501 void * memmove(void * dest,const void *src,size_t count)
512 tmp = (char *) dest + count;
513 s = (char *) src + count;
522 #ifndef __HAVE_ARCH_MEMCMP
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.
529 int memcmp(const void * cs,const void * ct,size_t count)
531 const unsigned char *su1, *su2;
534 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
535 if ((res = *su1 - *su2) != 0)
541 #ifndef __HAVE_ARCH_MEMSCAN
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.
548 * returns the address of the first occurrence of @c, or 1 byte past
549 * the area if @c is not found
551 void * memscan(void * addr, int c, size_t size)
553 unsigned char * p = (unsigned char *) addr;
565 #ifndef __HAVE_ARCH_STRSTR
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
571 char * strstr(const char * s1,const char * s2)
581 if (!memcmp(s1,s2,l2))
589 #ifndef __HAVE_ARCH_MEMCHR
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.
596 * returns the address of the first occurrence of @c, or %NULL
599 void *memchr(const void *s, int c, size_t n)
601 const unsigned char *p = s;
603 if ((unsigned char)c == *p++) {
604 return (void *)(p-1);
613 #ifndef __HAVE_ARCH_WCSNCMP
614 int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n)
621 /* XXX assumes wchar_t = int */
622 return (*(const unsigned short *)s1 -
623 *(const unsigned short *)--s2);
632 #ifndef __HAVE_ARCH_WCSLEN
633 size_t wcslen(const wchar_t *s)
645 #ifndef __HAVE_ARCH_WCSCMP
646 int wcscmp(const wchar_t *s1, const wchar_t *s2)
651 /* XXX assumes wchar_t = int */
652 return (*(const unsigned int *)s1 - *(const unsigned int *)--s2);