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.
19 #include <linux/compiler.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/ctype.h>
27 * strncasecmp - Case insensitive, length-limited string comparison
29 * @s2: The other string
30 * @len: the maximum number of characters to compare
32 int strncasecmp(const char *s1, const char *s2, size_t len)
34 /* Yes, Virginia, it had better be unsigned */
54 return (int)c1 - (int)c2;
58 * strcasecmp - Case insensitive string comparison
60 * @s2: The other string
62 int strcasecmp(const char *s1, const char *s2)
64 return strncasecmp(s1, s2, -1U);
69 #ifndef __HAVE_ARCH_STRCPY
71 * strcpy - Copy a %NUL terminated string
72 * @dest: Where to copy the string to
73 * @src: Where to copy the string from
75 char * strcpy(char * dest,const char *src)
79 while ((*dest++ = *src++) != '\0')
85 #ifndef __HAVE_ARCH_STRNCPY
87 * strncpy - Copy a length-limited, %NUL-terminated string
88 * @dest: Where to copy the string to
89 * @src: Where to copy the string from
90 * @count: The maximum number of bytes to copy
92 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
93 * However, the result is not %NUL-terminated if the source exceeds
96 char * strncpy(char * dest,const char *src,size_t count)
100 while (count-- && (*dest++ = *src++) != '\0')
107 #ifndef __HAVE_ARCH_STRLCPY
109 * strlcpy - Copy a C-string into a sized buffer
110 * @dest: Where to copy the string to
111 * @src: Where to copy the string from
112 * @size: size of destination buffer
114 * Compatible with *BSD: the result is always a valid
115 * NUL-terminated string that fits in the buffer (unless,
116 * of course, the buffer size is zero). It does not pad
117 * out the result like strncpy() does.
119 * Return: the number of bytes copied
121 size_t strlcpy(char *dest, const char *src, size_t size)
124 size_t srclen = strlen(src);
125 size_t len = (srclen >= size) ? size - 1 : srclen;
127 memcpy(dest, src, len);
136 #ifndef __HAVE_ARCH_STRCAT
138 * strcat - Append one %NUL-terminated string to another
139 * @dest: The string to be appended to
140 * @src: The string to append to it
142 char * strcat(char * dest, const char * src)
148 while ((*dest++ = *src++) != '\0')
155 #ifndef __HAVE_ARCH_STRNCAT
157 * strncat - Append a length-limited, %NUL-terminated string to another
158 * @dest: The string to be appended to
159 * @src: The string to append to it
160 * @count: The maximum numbers of bytes to copy
162 * Note that in contrast to strncpy, strncat ensures the result is
165 char * strncat(char *dest, const char *src, size_t count)
172 while ((*dest++ = *src++)) {
184 #ifndef __HAVE_ARCH_STRLCAT
186 * strlcat - Append a length-limited, %NUL-terminated string to another
187 * @dest: The string to be appended to
188 * @src: The string to append to it
189 * @size: The size of @dest
191 * Compatible with *BSD: the result is always a valid NUL-terminated string that
192 * fits in the buffer (unless, of course, the buffer size is zero). It does not
193 * write past @size like strncat() does.
195 size_t strlcat(char *dest, const char *src, size_t size)
197 size_t len = strnlen(dest, size);
199 return len + strlcpy(dest + len, src, size - len);
203 #ifndef __HAVE_ARCH_STRCMP
205 * strcmp - Compare two strings
207 * @ct: Another string
209 int strcmp(const char * cs,const char * ct)
211 register signed char __res;
214 if ((__res = *cs - *ct++) != 0 || !*cs++)
222 #ifndef __HAVE_ARCH_STRNCMP
224 * strncmp - Compare two length-limited strings
226 * @ct: Another string
227 * @count: The maximum number of bytes to compare
229 int strncmp(const char * cs,const char * ct,size_t count)
231 register signed char __res = 0;
234 if ((__res = *cs - *ct++) != 0 || !*cs++)
243 #ifndef __HAVE_ARCH_STRCHR
245 * strchr - Find the first occurrence of a character in a string
246 * @s: The string to be searched
247 * @c: The character to search for
249 char * strchr(const char * s, int c)
251 for(; *s != (char) c; ++s)
258 const char *strchrnul(const char *s, int c)
260 for (; *s != (char)c; ++s)
266 #ifndef __HAVE_ARCH_STRRCHR
268 * strrchr - Find the last occurrence of a character in a string
269 * @s: The string to be searched
270 * @c: The character to search for
272 char * strrchr(const char * s, int c)
274 const char *p = s + strlen(s);
283 #ifndef __HAVE_ARCH_STRLEN
285 * strlen - Find the length of a string
286 * @s: The string to be sized
288 size_t strlen(const char * s)
292 for (sc = s; *sc != '\0'; ++sc)
298 #ifndef __HAVE_ARCH_STRNLEN
300 * strnlen - Find the length of a length-limited string
301 * @s: The string to be sized
302 * @count: The maximum number of bytes to search
304 size_t strnlen(const char * s, size_t count)
308 for (sc = s; count-- && *sc != '\0'; ++sc)
314 #ifndef __HAVE_ARCH_STRCSPN
316 * strcspn - Calculate the length of the initial substring of @s which does
317 * not contain letters in @reject
318 * @s: The string to be searched
319 * @reject: The string to avoid
321 size_t strcspn(const char *s, const char *reject)
327 for (p = s; *p != '\0'; ++p) {
328 for (r = reject; *r != '\0'; ++r) {
338 #ifndef __HAVE_ARCH_STRDUP
339 char * strdup(const char *s)
344 ((new = malloc (strlen(s) + 1)) == NULL) ) {
352 char * strndup(const char *s, size_t n)
365 new = malloc(len + 1);
369 strncpy(new, s, len);
376 #ifndef __HAVE_ARCH_STRSPN
378 * strspn - Calculate the length of the initial substring of @s which only
379 * contain letters in @accept
380 * @s: The string to be searched
381 * @accept: The string to search for
383 size_t strspn(const char *s, const char *accept)
389 for (p = s; *p != '\0'; ++p) {
390 for (a = accept; *a != '\0'; ++a) {
403 #ifndef __HAVE_ARCH_STRPBRK
405 * strpbrk - Find the first occurrence of a set of characters
406 * @cs: The string to be searched
407 * @ct: The characters to search for
409 char * strpbrk(const char * cs,const char * ct)
411 const char *sc1,*sc2;
413 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
414 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
423 #ifndef __HAVE_ARCH_STRTOK
425 * strtok - Split a string into tokens
426 * @s: The string to be searched
427 * @ct: The characters to search for
429 * WARNING: strtok is deprecated, use strsep instead.
431 char * strtok(char * s,const char * ct)
435 sbegin = s ? s : ___strtok;
439 sbegin += strspn(sbegin,ct);
440 if (*sbegin == '\0') {
444 send = strpbrk( sbegin, ct);
445 if (send && *send != '\0')
452 #ifndef __HAVE_ARCH_STRSEP
454 * strsep - Split a string into tokens
455 * @s: The string to be searched
456 * @ct: The characters to search for
458 * strsep() updates @s to point after the token, ready for the next call.
460 * It returns empty tokens, too, behaving exactly like the libc function
461 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
462 * Same semantics, slimmer shape. ;)
464 char * strsep(char **s, const char *ct)
466 char *sbegin = *s, *end;
471 end = strpbrk(sbegin, ct);
480 #ifndef __HAVE_ARCH_STRSWAB
482 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
483 * s: address of the string
485 * returns the address of the swapped string or NULL on error. If
486 * string length is odd, last byte is untouched.
488 char *strswab(const char *s)
492 if ((NULL == s) || ('\0' == *s)) {
496 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
508 #ifndef __HAVE_ARCH_MEMSET
510 * memset - Fill a region of memory with the given value
511 * @s: Pointer to the start of the area.
512 * @c: The byte to fill the area with
513 * @count: The size of the area.
515 * Do not use memset() to access IO space, use memset_io() instead.
517 __used void * memset(void * s,int c,size_t count)
519 unsigned long *sl = (unsigned long *) s;
522 #if !CONFIG_IS_ENABLED(TINY_MEMSET)
523 unsigned long cl = 0;
526 /* do it one word at a time (32 bits or 64 bits) while possible */
527 if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
528 for (i = 0; i < sizeof(*sl); i++) {
532 while (count >= sizeof(*sl)) {
534 count -= sizeof(*sl);
537 #endif /* fill 8 bits at a time */
546 #ifndef __HAVE_ARCH_MEMCPY
548 * memcpy - Copy one area of memory to another
549 * @dest: Where to copy to
550 * @src: Where to copy from
551 * @count: The size of the area.
553 * You should not use this function to access IO space, use memcpy_toio()
554 * or memcpy_fromio() instead.
556 __used void * memcpy(void *dest, const void *src, size_t count)
558 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
564 /* while all data is aligned (common case), copy a word at a time */
565 if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
566 while (count >= sizeof(*dl)) {
568 count -= sizeof(*dl);
571 /* copy the reset one byte at a time */
581 #ifndef __HAVE_ARCH_MEMMOVE
583 * memmove - Copy one area of memory to another
584 * @dest: Where to copy to
585 * @src: Where to copy from
586 * @count: The size of the area.
588 * Unlike memcpy(), memmove() copes with overlapping areas.
590 __used void * memmove(void * dest,const void *src,size_t count)
594 if (dest <= src || (src + count) <= dest) {
596 * Use the fast memcpy implementation (ARCH optimized or lib/string.c) when it is possible:
597 * - when dest is before src (assuming that memcpy is doing forward-copying)
598 * - when destination don't overlap the source buffer (src + count <= dest)
600 * WARNING: the first optimisation cause an issue, when __HAVE_ARCH_MEMCPY is defined,
601 * __HAVE_ARCH_MEMMOVE is not defined and if the memcpy ARCH-specific
602 * implementation is not doing a forward-copying.
604 * No issue today because memcpy is doing a forward-copying in lib/string.c and for ARM32
605 * architecture; no other arches use __HAVE_ARCH_MEMCPY without __HAVE_ARCH_MEMMOVE.
607 memcpy(dest, src, count);
609 tmp = (char *) dest + count;
610 s = (char *) src + count;
619 #ifndef __HAVE_ARCH_MEMCMP
621 * memcmp - Compare two areas of memory
622 * @cs: One area of memory
623 * @ct: Another area of memory
624 * @count: The size of the area.
626 __used int memcmp(const void * cs,const void * ct,size_t count)
628 const unsigned char *su1, *su2;
631 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
632 if ((res = *su1 - *su2) != 0)
638 #ifndef __HAVE_ARCH_MEMSCAN
640 * memscan - Find a character in an area of memory.
641 * @addr: The memory area
642 * @c: The byte to search for
643 * @size: The size of the area.
645 * returns the address of the first occurrence of @c, or 1 byte past
646 * the area if @c is not found
648 void * memscan(void * addr, int c, size_t size)
650 unsigned char * p = (unsigned char *) addr;
662 #ifndef __HAVE_ARCH_STRSTR
664 * strstr - Find the first substring in a %NUL terminated string
665 * @s1: The string to be searched
666 * @s2: The string to search for
668 char * strstr(const char * s1,const char * s2)
678 if (!memcmp(s1,s2,l2))
686 #ifndef __HAVE_ARCH_MEMCHR
688 * memchr - Find a character in an area of memory.
689 * @s: The memory area
690 * @c: The byte to search for
691 * @n: The size of the area.
693 * returns the address of the first occurrence of @c, or %NULL
696 void *memchr(const void *s, int c, size_t n)
698 const unsigned char *p = s;
700 if ((unsigned char)c == *p++) {
701 return (void *)(p-1);
708 #ifndef __HAVE_ARCH_MEMCHR_INV
709 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
713 return (void *)start;
720 * memchr_inv - Find an unmatching character in an area of memory.
721 * @start: The memory area
722 * @c: Find a character other than c
723 * @bytes: The size of the area.
725 * returns the address of the first character other than @c, or %NULL
726 * if the whole buffer contains just @c.
728 void *memchr_inv(const void *start, int c, size_t bytes)
732 unsigned int words, prefix;
735 return check_bytes8(start, value, bytes);
738 value64 |= value64 << 8;
739 value64 |= value64 << 16;
740 value64 |= value64 << 32;
742 prefix = (unsigned long)start % 8;
747 r = check_bytes8(start, value, prefix);
757 if (*(u64 *)start != value64)
758 return check_bytes8(start, value, 8);
763 return check_bytes8(start, value, bytes % 8);