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..
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>
26 * strncasecmp - Case insensitive, length-limited string comparison
28 * @s2: The other string
29 * @len: the maximum number of characters to compare
31 int strncasecmp(const char *s1, const char *s2, size_t len)
33 /* Yes, Virginia, it had better be unsigned */
53 return (int)c1 - (int)c2;
57 * strcasecmp - Case insensitive string comparison
59 * @s2: The other string
61 int strcasecmp(const char *s1, const char *s2)
63 return strncasecmp(s1, s2, -1U);
68 #ifndef __HAVE_ARCH_STRCPY
70 * strcpy - Copy a %NUL terminated string
71 * @dest: Where to copy the string to
72 * @src: Where to copy the string from
74 char * strcpy(char * dest,const char *src)
78 while ((*dest++ = *src++) != '\0')
84 #ifndef __HAVE_ARCH_STRNCPY
86 * strncpy - Copy a length-limited, %NUL-terminated string
87 * @dest: Where to copy the string to
88 * @src: Where to copy the string from
89 * @count: The maximum number of bytes to copy
91 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
92 * However, the result is not %NUL-terminated if the source exceeds
95 char * strncpy(char * dest,const char *src,size_t count)
99 while (count-- && (*dest++ = *src++) != '\0')
106 #ifndef __HAVE_ARCH_STRLCPY
108 * strlcpy - Copy a C-string into a sized buffer
109 * @dest: Where to copy the string to
110 * @src: Where to copy the string from
111 * @size: size of destination buffer
113 * Compatible with *BSD: the result is always a valid
114 * NUL-terminated string that fits in the buffer (unless,
115 * of course, the buffer size is zero). It does not pad
116 * out the result like strncpy() does.
118 * Return: strlen(src)
120 size_t strlcpy(char *dest, const char *src, size_t size)
122 size_t ret = strlen(src);
125 size_t len = (ret >= size) ? size - 1 : ret;
126 memcpy(dest, src, len);
133 #ifndef __HAVE_ARCH_STRCAT
135 * strcat - Append one %NUL-terminated string to another
136 * @dest: The string to be appended to
137 * @src: The string to append to it
139 char * strcat(char * dest, const char * src)
145 while ((*dest++ = *src++) != '\0')
152 #ifndef __HAVE_ARCH_STRNCAT
154 * strncat - Append a length-limited, %NUL-terminated string to another
155 * @dest: The string to be appended to
156 * @src: The string to append to it
157 * @count: The maximum numbers of bytes to copy
159 * Note that in contrast to strncpy, strncat ensures the result is
162 char * strncat(char *dest, const char *src, size_t count)
169 while ((*dest++ = *src++)) {
181 #ifndef __HAVE_ARCH_STRLCAT
183 * strlcat - Append a length-limited, %NUL-terminated string to another
184 * @dest: The string to be appended to
185 * @src: The string to append to it
186 * @size: The size of @dest
188 * Compatible with *BSD: the result is always a valid NUL-terminated string that
189 * fits in the buffer (unless, of course, the buffer size is zero). It does not
190 * write past @size like strncat() does.
192 * Return: min(strlen(dest), size) + strlen(src)
194 size_t strlcat(char *dest, const char *src, size_t size)
196 size_t len = strnlen(dest, size);
198 return len + strlcpy(dest + len, src, size - len);
202 #ifndef __HAVE_ARCH_STRCMP
204 * strcmp - Compare two strings
206 * @ct: Another string
208 int strcmp(const char *cs, const char *ct)
213 unsigned char a = *cs++;
214 unsigned char b = *ct++;
225 #ifndef __HAVE_ARCH_STRNCMP
227 * strncmp - Compare two length-limited strings
229 * @ct: Another string
230 * @count: The maximum number of bytes to compare
232 int strncmp(const char *cs, const char *ct, size_t count)
237 unsigned char a = *cs++;
238 unsigned char b = *ct++;
249 #ifndef __HAVE_ARCH_STRCHR
251 * strchr - Find the first occurrence of a character in a string
252 * @s: The string to be searched
253 * @c: The character to search for
255 char * strchr(const char * s, int c)
257 for(; *s != (char) c; ++s)
264 const char *strchrnul(const char *s, int c)
266 for (; *s != (char)c; ++s)
272 #ifndef __HAVE_ARCH_STRRCHR
274 * strrchr - Find the last occurrence of a character in a string
275 * @s: The string to be searched
276 * @c: The character to search for
278 char * strrchr(const char * s, int c)
280 const char *p = s + strlen(s);
289 #ifndef __HAVE_ARCH_STRLEN
291 * strlen - Find the length of a string
292 * @s: The string to be sized
294 size_t strlen(const char * s)
298 for (sc = s; *sc != '\0'; ++sc)
304 #ifndef __HAVE_ARCH_STRNLEN
306 * strnlen - Find the length of a length-limited string
307 * @s: The string to be sized
308 * @count: The maximum number of bytes to search
310 size_t strnlen(const char * s, size_t count)
314 for (sc = s; count-- && *sc != '\0'; ++sc)
320 #ifndef __HAVE_ARCH_STRCSPN
322 * strcspn - Calculate the length of the initial substring of @s which does
323 * not contain letters in @reject
324 * @s: The string to be searched
325 * @reject: The string to avoid
327 size_t strcspn(const char *s, const char *reject)
333 for (p = s; *p != '\0'; ++p) {
334 for (r = reject; *r != '\0'; ++r) {
344 #ifndef __HAVE_ARCH_STRDUP
345 char * strdup(const char *s)
350 ((new = malloc (strlen(s) + 1)) == NULL) ) {
358 char * strndup(const char *s, size_t n)
371 new = malloc(len + 1);
375 strncpy(new, s, len);
382 #ifndef __HAVE_ARCH_STRSPN
384 * strspn - Calculate the length of the initial substring of @s which only
385 * contain letters in @accept
386 * @s: The string to be searched
387 * @accept: The string to search for
389 size_t strspn(const char *s, const char *accept)
395 for (p = s; *p != '\0'; ++p) {
396 for (a = accept; *a != '\0'; ++a) {
409 #ifndef __HAVE_ARCH_STRPBRK
411 * strpbrk - Find the first occurrence of a set of characters
412 * @cs: The string to be searched
413 * @ct: The characters to search for
415 char * strpbrk(const char * cs,const char * ct)
417 const char *sc1,*sc2;
419 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
420 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
429 #ifndef __HAVE_ARCH_STRTOK
431 * strtok - Split a string into tokens
432 * @s: The string to be searched
433 * @ct: The characters to search for
435 * WARNING: strtok is deprecated, use strsep instead.
437 char * strtok(char * s,const char * ct)
441 sbegin = s ? s : ___strtok;
445 sbegin += strspn(sbegin,ct);
446 if (*sbegin == '\0') {
450 send = strpbrk( sbegin, ct);
451 if (send && *send != '\0')
458 #ifndef __HAVE_ARCH_STRSEP
460 * strsep - Split a string into tokens
461 * @s: The string to be searched
462 * @ct: The characters to search for
464 * strsep() updates @s to point after the token, ready for the next call.
466 * It returns empty tokens, too, behaving exactly like the libc function
467 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
468 * Same semantics, slimmer shape. ;)
470 char * strsep(char **s, const char *ct)
472 char *sbegin = *s, *end;
477 end = strpbrk(sbegin, ct);
486 #ifndef __HAVE_ARCH_STRSWAB
488 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
489 * s: address of the string
491 * returns the address of the swapped string or NULL on error. If
492 * string length is odd, last byte is untouched.
494 char *strswab(const char *s)
498 if ((NULL == s) || ('\0' == *s)) {
502 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
514 #ifndef __HAVE_ARCH_MEMSET
516 * memset - Fill a region of memory with the given value
517 * @s: Pointer to the start of the area.
518 * @c: The byte to fill the area with
519 * @count: The size of the area.
521 * Do not use memset() to access IO space, use memset_io() instead.
523 __used void * memset(void * s,int c,size_t count)
525 unsigned long *sl = (unsigned long *) s;
528 #if !CONFIG_IS_ENABLED(TINY_MEMSET)
529 unsigned long cl = 0;
532 /* do it one word at a time (32 bits or 64 bits) while possible */
533 if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
534 for (i = 0; i < sizeof(*sl); i++) {
538 while (count >= sizeof(*sl)) {
540 count -= sizeof(*sl);
543 #endif /* fill 8 bits at a time */
552 #ifndef __HAVE_ARCH_MEMCPY
554 * memcpy - Copy one area of memory to another
555 * @dest: Where to copy to
556 * @src: Where to copy from
557 * @count: The size of the area.
559 * You should not use this function to access IO space, use memcpy_toio()
560 * or memcpy_fromio() instead.
562 __used void * memcpy(void *dest, const void *src, size_t count)
564 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
570 /* while all data is aligned (common case), copy a word at a time */
571 if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
572 while (count >= sizeof(*dl)) {
574 count -= sizeof(*dl);
577 /* copy the reset one byte at a time */
587 #ifndef __HAVE_ARCH_MEMMOVE
589 * memmove - Copy one area of memory to another
590 * @dest: Where to copy to
591 * @src: Where to copy from
592 * @count: The size of the area.
594 * Unlike memcpy(), memmove() copes with overlapping areas.
596 __used void * memmove(void * dest,const void *src,size_t count)
600 if (dest <= src || (src + count) <= dest) {
602 * Use the fast memcpy implementation (ARCH optimized or lib/string.c) when it is possible:
603 * - when dest is before src (assuming that memcpy is doing forward-copying)
604 * - when destination don't overlap the source buffer (src + count <= dest)
606 * WARNING: the first optimisation cause an issue, when __HAVE_ARCH_MEMCPY is defined,
607 * __HAVE_ARCH_MEMMOVE is not defined and if the memcpy ARCH-specific
608 * implementation is not doing a forward-copying.
610 * No issue today because memcpy is doing a forward-copying in lib/string.c and for ARM32
611 * architecture; no other arches use __HAVE_ARCH_MEMCPY without __HAVE_ARCH_MEMMOVE.
613 memcpy(dest, src, count);
615 tmp = (char *) dest + count;
616 s = (char *) src + count;
625 #ifndef __HAVE_ARCH_MEMCMP
627 * memcmp - Compare two areas of memory
628 * @cs: One area of memory
629 * @ct: Another area of memory
630 * @count: The size of the area.
632 __used int memcmp(const void * cs,const void * ct,size_t count)
634 const unsigned char *su1, *su2;
637 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
638 if ((res = *su1 - *su2) != 0)
644 #ifndef __HAVE_ARCH_MEMSCAN
646 * memscan - Find a character in an area of memory.
647 * @addr: The memory area
648 * @c: The byte to search for
649 * @size: The size of the area.
651 * returns the address of the first occurrence of @c, or 1 byte past
652 * the area if @c is not found
654 void * memscan(void * addr, int c, size_t size)
656 unsigned char * p = (unsigned char *) addr;
668 char *memdup(const void *src, size_t len)
681 #ifndef __HAVE_ARCH_STRSTR
683 * strstr - Find the first substring in a %NUL terminated string
684 * @s1: The string to be searched
685 * @s2: The string to search for
687 char * strstr(const char * s1,const char * s2)
697 if (!memcmp(s1,s2,l2))
705 #ifndef __HAVE_ARCH_MEMCHR
707 * memchr - Find a character in an area of memory.
708 * @s: The memory area
709 * @c: The byte to search for
710 * @n: The size of the area.
712 * returns the address of the first occurrence of @c, or %NULL
715 void *memchr(const void *s, int c, size_t n)
717 const unsigned char *p = s;
719 if ((unsigned char)c == *p++) {
720 return (void *)(p-1);
727 #ifndef __HAVE_ARCH_MEMCHR_INV
728 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
732 return (void *)start;
739 * memchr_inv - Find an unmatching character in an area of memory.
740 * @start: The memory area
741 * @c: Find a character other than c
742 * @bytes: The size of the area.
744 * returns the address of the first character other than @c, or %NULL
745 * if the whole buffer contains just @c.
747 void *memchr_inv(const void *start, int c, size_t bytes)
751 unsigned int words, prefix;
754 return check_bytes8(start, value, bytes);
757 value64 |= value64 << 8;
758 value64 |= value64 << 16;
759 value64 |= value64 << 32;
761 prefix = (unsigned long)start % 8;
766 r = check_bytes8(start, value, prefix);
776 if (*(u64 *)start != value64)
777 return check_bytes8(start, value, 8);
782 return check_bytes8(start, value, bytes % 8);