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/types.h>
20 #include <linux/string.h>
21 #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: the number of bytes copied
120 size_t strlcpy(char *dest, const char *src, size_t size)
123 size_t srclen = strlen(src);
124 size_t len = (srclen >= size) ? size - 1 : srclen;
126 memcpy(dest, src, len);
135 #ifndef __HAVE_ARCH_STRCAT
137 * strcat - Append one %NUL-terminated string to another
138 * @dest: The string to be appended to
139 * @src: The string to append to it
141 char * strcat(char * dest, const char * src)
147 while ((*dest++ = *src++) != '\0')
154 #ifndef __HAVE_ARCH_STRNCAT
156 * strncat - Append a length-limited, %NUL-terminated string to another
157 * @dest: The string to be appended to
158 * @src: The string to append to it
159 * @count: The maximum numbers of bytes to copy
161 * Note that in contrast to strncpy, strncat ensures the result is
164 char * strncat(char *dest, const char *src, size_t count)
171 while ((*dest++ = *src++)) {
183 #ifndef __HAVE_ARCH_STRCMP
185 * strcmp - Compare two strings
187 * @ct: Another string
189 int strcmp(const char * cs,const char * ct)
191 register signed char __res;
194 if ((__res = *cs - *ct++) != 0 || !*cs++)
202 #ifndef __HAVE_ARCH_STRNCMP
204 * strncmp - Compare two length-limited strings
206 * @ct: Another string
207 * @count: The maximum number of bytes to compare
209 int strncmp(const char * cs,const char * ct,size_t count)
211 register signed char __res = 0;
214 if ((__res = *cs - *ct++) != 0 || !*cs++)
223 #ifndef __HAVE_ARCH_STRCHR
225 * strchr - Find the first occurrence of a character in a string
226 * @s: The string to be searched
227 * @c: The character to search for
229 char * strchr(const char * s, int c)
231 for(; *s != (char) c; ++s)
238 const char *strchrnul(const char *s, int c)
240 for (; *s != (char)c; ++s)
246 #ifndef __HAVE_ARCH_STRRCHR
248 * strrchr - Find the last occurrence of a character in a string
249 * @s: The string to be searched
250 * @c: The character to search for
252 char * strrchr(const char * s, int c)
254 const char *p = s + strlen(s);
263 #ifndef __HAVE_ARCH_STRLEN
265 * strlen - Find the length of a string
266 * @s: The string to be sized
268 size_t strlen(const char * s)
272 for (sc = s; *sc != '\0'; ++sc)
278 #ifndef __HAVE_ARCH_STRNLEN
280 * strnlen - Find the length of a length-limited string
281 * @s: The string to be sized
282 * @count: The maximum number of bytes to search
284 size_t strnlen(const char * s, size_t count)
288 for (sc = s; count-- && *sc != '\0'; ++sc)
294 #ifndef __HAVE_ARCH_STRCSPN
296 * strcspn - Calculate the length of the initial substring of @s which does
297 * not contain letters in @reject
298 * @s: The string to be searched
299 * @reject: The string to avoid
301 size_t strcspn(const char *s, const char *reject)
307 for (p = s; *p != '\0'; ++p) {
308 for (r = reject; *r != '\0'; ++r) {
318 #ifndef __HAVE_ARCH_STRDUP
319 char * strdup(const char *s)
324 ((new = malloc (strlen(s) + 1)) == NULL) ) {
332 char * strndup(const char *s, size_t n)
345 new = malloc(len + 1);
349 strncpy(new, s, len);
356 #ifndef __HAVE_ARCH_STRSPN
358 * strspn - Calculate the length of the initial substring of @s which only
359 * contain letters in @accept
360 * @s: The string to be searched
361 * @accept: The string to search for
363 size_t strspn(const char *s, const char *accept)
369 for (p = s; *p != '\0'; ++p) {
370 for (a = accept; *a != '\0'; ++a) {
383 #ifndef __HAVE_ARCH_STRPBRK
385 * strpbrk - Find the first occurrence of a set of characters
386 * @cs: The string to be searched
387 * @ct: The characters to search for
389 char * strpbrk(const char * cs,const char * ct)
391 const char *sc1,*sc2;
393 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
394 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
403 #ifndef __HAVE_ARCH_STRTOK
405 * strtok - Split a string into tokens
406 * @s: The string to be searched
407 * @ct: The characters to search for
409 * WARNING: strtok is deprecated, use strsep instead.
411 char * strtok(char * s,const char * ct)
415 sbegin = s ? s : ___strtok;
419 sbegin += strspn(sbegin,ct);
420 if (*sbegin == '\0') {
424 send = strpbrk( sbegin, ct);
425 if (send && *send != '\0')
432 #ifndef __HAVE_ARCH_STRSEP
434 * strsep - Split a string into tokens
435 * @s: The string to be searched
436 * @ct: The characters to search for
438 * strsep() updates @s to point after the token, ready for the next call.
440 * It returns empty tokens, too, behaving exactly like the libc function
441 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
442 * Same semantics, slimmer shape. ;)
444 char * strsep(char **s, const char *ct)
446 char *sbegin = *s, *end;
451 end = strpbrk(sbegin, ct);
460 #ifndef __HAVE_ARCH_STRSWAB
462 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
463 * s: address of the string
465 * returns the address of the swapped string or NULL on error. If
466 * string length is odd, last byte is untouched.
468 char *strswab(const char *s)
472 if ((NULL == s) || ('\0' == *s)) {
476 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
488 #ifndef __HAVE_ARCH_MEMSET
490 * memset - Fill a region of memory with the given value
491 * @s: Pointer to the start of the area.
492 * @c: The byte to fill the area with
493 * @count: The size of the area.
495 * Do not use memset() to access IO space, use memset_io() instead.
497 void * memset(void * s,int c,size_t count)
499 unsigned long *sl = (unsigned long *) s;
502 #if !CONFIG_IS_ENABLED(TINY_MEMSET)
503 unsigned long cl = 0;
506 /* do it one word at a time (32 bits or 64 bits) while possible */
507 if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
508 for (i = 0; i < sizeof(*sl); i++) {
512 while (count >= sizeof(*sl)) {
514 count -= sizeof(*sl);
517 #endif /* fill 8 bits at a time */
526 #ifndef __HAVE_ARCH_MEMCPY
528 * memcpy - Copy one area of memory to another
529 * @dest: Where to copy to
530 * @src: Where to copy from
531 * @count: The size of the area.
533 * You should not use this function to access IO space, use memcpy_toio()
534 * or memcpy_fromio() instead.
536 void * memcpy(void *dest, const void *src, size_t count)
538 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
544 /* while all data is aligned (common case), copy a word at a time */
545 if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
546 while (count >= sizeof(*dl)) {
548 count -= sizeof(*dl);
551 /* copy the reset one byte at a time */
561 #ifndef __HAVE_ARCH_MEMMOVE
563 * memmove - Copy one area of memory to another
564 * @dest: Where to copy to
565 * @src: Where to copy from
566 * @count: The size of the area.
568 * Unlike memcpy(), memmove() copes with overlapping areas.
570 void * memmove(void * dest,const void *src,size_t count)
574 if (dest <= src || (src + count) <= dest) {
576 * Use the fast memcpy implementation (ARCH optimized or lib/string.c) when it is possible:
577 * - when dest is before src (assuming that memcpy is doing forward-copying)
578 * - when destination don't overlap the source buffer (src + count <= dest)
580 * WARNING: the first optimisation cause an issue, when __HAVE_ARCH_MEMCPY is defined,
581 * __HAVE_ARCH_MEMMOVE is not defined and if the memcpy ARCH-specific
582 * implementation is not doing a forward-copying.
584 * No issue today because memcpy is doing a forward-copying in lib/string.c and for ARM32
585 * architecture; no other arches use __HAVE_ARCH_MEMCPY without __HAVE_ARCH_MEMMOVE.
587 memcpy(dest, src, count);
589 tmp = (char *) dest + count;
590 s = (char *) src + count;
599 #ifndef __HAVE_ARCH_MEMCMP
601 * memcmp - Compare two areas of memory
602 * @cs: One area of memory
603 * @ct: Another area of memory
604 * @count: The size of the area.
606 int memcmp(const void * cs,const void * ct,size_t count)
608 const unsigned char *su1, *su2;
611 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
612 if ((res = *su1 - *su2) != 0)
618 #ifndef __HAVE_ARCH_MEMSCAN
620 * memscan - Find a character in an area of memory.
621 * @addr: The memory area
622 * @c: The byte to search for
623 * @size: The size of the area.
625 * returns the address of the first occurrence of @c, or 1 byte past
626 * the area if @c is not found
628 void * memscan(void * addr, int c, size_t size)
630 unsigned char * p = (unsigned char *) addr;
642 #ifndef __HAVE_ARCH_STRSTR
644 * strstr - Find the first substring in a %NUL terminated string
645 * @s1: The string to be searched
646 * @s2: The string to search for
648 char * strstr(const char * s1,const char * s2)
658 if (!memcmp(s1,s2,l2))
666 #ifndef __HAVE_ARCH_MEMCHR
668 * memchr - Find a character in an area of memory.
669 * @s: The memory area
670 * @c: The byte to search for
671 * @n: The size of the area.
673 * returns the address of the first occurrence of @c, or %NULL
676 void *memchr(const void *s, int c, size_t n)
678 const unsigned char *p = s;
680 if ((unsigned char)c == *p++) {
681 return (void *)(p-1);
688 #ifndef __HAVE_ARCH_MEMCHR_INV
689 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
693 return (void *)start;
700 * memchr_inv - Find an unmatching character in an area of memory.
701 * @start: The memory area
702 * @c: Find a character other than c
703 * @bytes: The size of the area.
705 * returns the address of the first character other than @c, or %NULL
706 * if the whole buffer contains just @c.
708 void *memchr_inv(const void *start, int c, size_t bytes)
712 unsigned int words, prefix;
715 return check_bytes8(start, value, bytes);
718 value64 |= value64 << 8;
719 value64 |= value64 << 16;
720 value64 |= value64 << 32;
722 prefix = (unsigned long)start % 8;
727 r = check_bytes8(start, value, prefix);
737 if (*(u64 *)start != value64)
738 return check_bytes8(start, value, 8);
743 return check_bytes8(start, value, bytes % 8);