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.
17 * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
18 * Matthew Hawkins <matt@mh.dropbear.id.au>
19 * - Kissed strtok() goodbye
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/ctype.h>
25 #include <linux/kernel.h>
26 #include <linux/export.h>
27 #include <linux/bug.h>
28 #include <linux/errno.h>
30 #ifndef __HAVE_ARCH_STRNCASECMP
32 * strncasecmp - Case insensitive, length-limited string comparison
34 * @s2: The other string
35 * @len: the maximum number of characters to compare
37 int strncasecmp(const char *s1
, const char *s2
, size_t len
)
39 /* Yes, Virginia, it had better be unsigned */
57 return (int)c1
- (int)c2
;
59 EXPORT_SYMBOL(strncasecmp
);
62 #ifndef __HAVE_ARCH_STRCASECMP
63 int strcasecmp(const char *s1
, const char *s2
)
70 } while (c1
== c2
&& c1
!= 0);
73 EXPORT_SYMBOL(strcasecmp
);
76 #ifndef __HAVE_ARCH_STRCPY
78 * strcpy - Copy a %NUL terminated string
79 * @dest: Where to copy the string to
80 * @src: Where to copy the string from
83 char *strcpy(char *dest
, const char *src
)
87 while ((*dest
++ = *src
++) != '\0')
91 EXPORT_SYMBOL(strcpy
);
94 #ifndef __HAVE_ARCH_STRNCPY
96 * strncpy - Copy a length-limited, C-string
97 * @dest: Where to copy the string to
98 * @src: Where to copy the string from
99 * @count: The maximum number of bytes to copy
101 * The result is not %NUL-terminated if the source exceeds
104 * In the case where the length of @src is less than that of
105 * count, the remainder of @dest will be padded with %NUL.
108 char *strncpy(char *dest
, const char *src
, size_t count
)
113 if ((*tmp
= *src
) != 0)
120 EXPORT_SYMBOL(strncpy
);
123 #ifndef __HAVE_ARCH_STRLCPY
125 * strlcpy - Copy a C-string into a sized buffer
126 * @dest: Where to copy the string to
127 * @src: Where to copy the string from
128 * @size: size of destination buffer
130 * Compatible with *BSD: the result is always a valid
131 * NUL-terminated string that fits in the buffer (unless,
132 * of course, the buffer size is zero). It does not pad
133 * out the result like strncpy() does.
135 size_t strlcpy(char *dest
, const char *src
, size_t size
)
137 size_t ret
= strlen(src
);
140 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
141 memcpy(dest
, src
, len
);
146 EXPORT_SYMBOL(strlcpy
);
149 #ifndef __HAVE_ARCH_STRCAT
151 * strcat - Append one %NUL-terminated string to another
152 * @dest: The string to be appended to
153 * @src: The string to append to it
156 char *strcat(char *dest
, const char *src
)
162 while ((*dest
++ = *src
++) != '\0')
166 EXPORT_SYMBOL(strcat
);
169 #ifndef __HAVE_ARCH_STRNCAT
171 * strncat - Append a length-limited, C-string to another
172 * @dest: The string to be appended to
173 * @src: The string to append to it
174 * @count: The maximum numbers of bytes to copy
176 * Note that in contrast to strncpy(), strncat() ensures the result is
179 char *strncat(char *dest
, const char *src
, size_t count
)
186 while ((*dest
++ = *src
++) != 0) {
195 EXPORT_SYMBOL(strncat
);
198 #ifndef __HAVE_ARCH_STRLCAT
200 * strlcat - Append a length-limited, C-string to another
201 * @dest: The string to be appended to
202 * @src: The string to append to it
203 * @count: The size of the destination buffer.
205 size_t strlcat(char *dest
, const char *src
, size_t count
)
207 size_t dsize
= strlen(dest
);
208 size_t len
= strlen(src
);
209 size_t res
= dsize
+ len
;
211 /* This would be a bug */
212 BUG_ON(dsize
>= count
);
218 memcpy(dest
, src
, len
);
222 EXPORT_SYMBOL(strlcat
);
225 #ifndef __HAVE_ARCH_STRCMP
227 * strcmp - Compare two strings
229 * @ct: Another string
232 int strcmp(const char *cs
, const char *ct
)
234 unsigned char c1
, c2
;
240 return c1
< c2
? -1 : 1;
246 EXPORT_SYMBOL(strcmp
);
249 #ifndef __HAVE_ARCH_STRNCMP
251 * strncmp - Compare two length-limited strings
253 * @ct: Another string
254 * @count: The maximum number of bytes to compare
256 int strncmp(const char *cs
, const char *ct
, size_t count
)
258 unsigned char c1
, c2
;
264 return c1
< c2
? -1 : 1;
271 EXPORT_SYMBOL(strncmp
);
274 #ifndef __HAVE_ARCH_STRCHR
276 * strchr - Find the first occurrence of a character in a string
277 * @s: The string to be searched
278 * @c: The character to search for
280 char *strchr(const char *s
, int c
)
282 for (; *s
!= (char)c
; ++s
)
287 EXPORT_SYMBOL(strchr
);
290 #ifndef __HAVE_ARCH_STRCHRNUL
292 * strchrnul - Find and return a character in a string, or end of string
293 * @s: The string to be searched
294 * @c: The character to search for
296 * Returns pointer to first occurrence of 'c' in s. If c is not found, then
297 * return a pointer to the null byte at the end of s.
299 char *strchrnul(const char *s
, int c
)
301 while (*s
&& *s
!= (char)c
)
305 EXPORT_SYMBOL(strchrnul
);
308 #ifndef __HAVE_ARCH_STRRCHR
310 * strrchr - Find the last occurrence of a character in a string
311 * @s: The string to be searched
312 * @c: The character to search for
314 char *strrchr(const char *s
, int c
)
316 const char *last
= NULL
;
323 EXPORT_SYMBOL(strrchr
);
326 #ifndef __HAVE_ARCH_STRNCHR
328 * strnchr - Find a character in a length limited string
329 * @s: The string to be searched
330 * @count: The number of characters to be searched
331 * @c: The character to search for
333 char *strnchr(const char *s
, size_t count
, int c
)
335 for (; count
-- && *s
!= '\0'; ++s
)
340 EXPORT_SYMBOL(strnchr
);
344 * skip_spaces - Removes leading whitespace from @str.
345 * @str: The string to be stripped.
347 * Returns a pointer to the first non-whitespace character in @str.
349 char *skip_spaces(const char *str
)
351 while (isspace(*str
))
355 EXPORT_SYMBOL(skip_spaces
);
358 * strim - Removes leading and trailing whitespace from @s.
359 * @s: The string to be stripped.
361 * Note that the first trailing whitespace is replaced with a %NUL-terminator
362 * in the given string @s. Returns a pointer to the first non-whitespace
375 while (end
>= s
&& isspace(*end
))
379 return skip_spaces(s
);
381 EXPORT_SYMBOL(strim
);
383 #ifndef __HAVE_ARCH_STRLEN
385 * strlen - Find the length of a string
386 * @s: The string to be sized
388 size_t strlen(const char *s
)
392 for (sc
= s
; *sc
!= '\0'; ++sc
)
396 EXPORT_SYMBOL(strlen
);
399 #ifndef __HAVE_ARCH_STRNLEN
401 * strnlen - Find the length of a length-limited string
402 * @s: The string to be sized
403 * @count: The maximum number of bytes to search
405 size_t strnlen(const char *s
, size_t count
)
409 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
413 EXPORT_SYMBOL(strnlen
);
416 #ifndef __HAVE_ARCH_STRSPN
418 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
419 * @s: The string to be searched
420 * @accept: The string to search for
422 size_t strspn(const char *s
, const char *accept
)
428 for (p
= s
; *p
!= '\0'; ++p
) {
429 for (a
= accept
; *a
!= '\0'; ++a
) {
440 EXPORT_SYMBOL(strspn
);
443 #ifndef __HAVE_ARCH_STRCSPN
445 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
446 * @s: The string to be searched
447 * @reject: The string to avoid
449 size_t strcspn(const char *s
, const char *reject
)
455 for (p
= s
; *p
!= '\0'; ++p
) {
456 for (r
= reject
; *r
!= '\0'; ++r
) {
464 EXPORT_SYMBOL(strcspn
);
467 #ifndef __HAVE_ARCH_STRPBRK
469 * strpbrk - Find the first occurrence of a set of characters
470 * @cs: The string to be searched
471 * @ct: The characters to search for
473 char *strpbrk(const char *cs
, const char *ct
)
475 const char *sc1
, *sc2
;
477 for (sc1
= cs
; *sc1
!= '\0'; ++sc1
) {
478 for (sc2
= ct
; *sc2
!= '\0'; ++sc2
) {
485 EXPORT_SYMBOL(strpbrk
);
488 #ifndef __HAVE_ARCH_STRSEP
490 * strsep - Split a string into tokens
491 * @s: The string to be searched
492 * @ct: The characters to search for
494 * strsep() updates @s to point after the token, ready for the next call.
496 * It returns empty tokens, too, behaving exactly like the libc function
497 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
498 * Same semantics, slimmer shape. ;)
500 char *strsep(char **s
, const char *ct
)
508 end
= strpbrk(sbegin
, ct
);
514 EXPORT_SYMBOL(strsep
);
518 * sysfs_streq - return true if strings are equal, modulo trailing newline
520 * @s2: another string
522 * This routine returns true iff two strings are equal, treating both
523 * NUL and newline-then-NUL as equivalent string terminations. It's
524 * geared for use with sysfs input strings, which generally terminate
525 * with newlines but are compared against values without newlines.
527 bool sysfs_streq(const char *s1
, const char *s2
)
529 while (*s1
&& *s1
== *s2
) {
536 if (!*s1
&& *s2
== '\n' && !s2
[1])
538 if (*s1
== '\n' && !s1
[1] && !*s2
)
542 EXPORT_SYMBOL(sysfs_streq
);
545 * strtobool - convert common user inputs into boolean values
549 * This routine returns 0 iff the first character is one of 'Yy1Nn0'.
550 * Otherwise it will return -EINVAL. Value pointed to by res is
551 * updated upon finding a match.
553 int strtobool(const char *s
, bool *res
)
571 EXPORT_SYMBOL(strtobool
);
573 #ifndef __HAVE_ARCH_MEMSET
575 * memset - Fill a region of memory with the given value
576 * @s: Pointer to the start of the area.
577 * @c: The byte to fill the area with
578 * @count: The size of the area.
580 * Do not use memset() to access IO space, use memset_io() instead.
582 void *memset(void *s
, int c
, size_t count
)
590 EXPORT_SYMBOL(memset
);
594 * memzero_explicit - Fill a region of memory (e.g. sensitive
595 * keying data) with 0s.
596 * @s: Pointer to the start of the area.
597 * @count: The size of the area.
599 * Note: usually using memset() is just fine (!), but in cases
600 * where clearing out _local_ data at the end of a scope is
601 * necessary, memzero_explicit() should be used instead in
602 * order to prevent the compiler from optimising away zeroing.
604 * memzero_explicit() doesn't need an arch-specific version as
605 * it just invokes the one of memset() implicitly.
607 void memzero_explicit(void *s
, size_t count
)
612 EXPORT_SYMBOL(memzero_explicit
);
614 #ifndef __HAVE_ARCH_MEMCPY
616 * memcpy - Copy one area of memory to another
617 * @dest: Where to copy to
618 * @src: Where to copy from
619 * @count: The size of the area.
621 * You should not use this function to access IO space, use memcpy_toio()
622 * or memcpy_fromio() instead.
624 void *memcpy(void *dest
, const void *src
, size_t count
)
633 EXPORT_SYMBOL(memcpy
);
636 #ifndef __HAVE_ARCH_MEMMOVE
638 * memmove - Copy one area of memory to another
639 * @dest: Where to copy to
640 * @src: Where to copy from
641 * @count: The size of the area.
643 * Unlike memcpy(), memmove() copes with overlapping areas.
645 void *memmove(void *dest
, const void *src
, size_t count
)
665 EXPORT_SYMBOL(memmove
);
668 #ifndef __HAVE_ARCH_MEMCMP
670 * memcmp - Compare two areas of memory
671 * @cs: One area of memory
672 * @ct: Another area of memory
673 * @count: The size of the area.
676 __visible
int memcmp(const void *cs
, const void *ct
, size_t count
)
678 const unsigned char *su1
, *su2
;
681 for (su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--)
682 if ((res
= *su1
- *su2
) != 0)
686 EXPORT_SYMBOL(memcmp
);
689 #ifndef __HAVE_ARCH_MEMSCAN
691 * memscan - Find a character in an area of memory.
692 * @addr: The memory area
693 * @c: The byte to search for
694 * @size: The size of the area.
696 * returns the address of the first occurrence of @c, or 1 byte past
697 * the area if @c is not found
699 void *memscan(void *addr
, int c
, size_t size
)
701 unsigned char *p
= addr
;
711 EXPORT_SYMBOL(memscan
);
714 #ifndef __HAVE_ARCH_STRSTR
716 * strstr - Find the first substring in a %NUL terminated string
717 * @s1: The string to be searched
718 * @s2: The string to search for
720 char *strstr(const char *s1
, const char *s2
)
730 if (!memcmp(s1
, s2
, l2
))
736 EXPORT_SYMBOL(strstr
);
739 #ifndef __HAVE_ARCH_STRNSTR
741 * strnstr - Find the first substring in a length-limited string
742 * @s1: The string to be searched
743 * @s2: The string to search for
744 * @len: the maximum number of characters to search
746 char *strnstr(const char *s1
, const char *s2
, size_t len
)
755 if (!memcmp(s1
, s2
, l2
))
761 EXPORT_SYMBOL(strnstr
);
764 #ifndef __HAVE_ARCH_MEMCHR
766 * memchr - Find a character in an area of memory.
767 * @s: The memory area
768 * @c: The byte to search for
769 * @n: The size of the area.
771 * returns the address of the first occurrence of @c, or %NULL
774 void *memchr(const void *s
, int c
, size_t n
)
776 const unsigned char *p
= s
;
778 if ((unsigned char)c
== *p
++) {
779 return (void *)(p
- 1);
784 EXPORT_SYMBOL(memchr
);
787 static void *check_bytes8(const u8
*start
, u8 value
, unsigned int bytes
)
791 return (void *)start
;
799 * memchr_inv - Find an unmatching character in an area of memory.
800 * @start: The memory area
801 * @c: Find a character other than c
802 * @bytes: The size of the area.
804 * returns the address of the first character other than @c, or %NULL
805 * if the whole buffer contains just @c.
807 void *memchr_inv(const void *start
, int c
, size_t bytes
)
811 unsigned int words
, prefix
;
814 return check_bytes8(start
, value
, bytes
);
817 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
818 value64
*= 0x0101010101010101;
819 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
820 value64
*= 0x01010101;
821 value64
|= value64
<< 32;
823 value64
|= value64
<< 8;
824 value64
|= value64
<< 16;
825 value64
|= value64
<< 32;
828 prefix
= (unsigned long)start
% 8;
833 r
= check_bytes8(start
, value
, prefix
);
843 if (*(u64
*)start
!= value64
)
844 return check_bytes8(start
, value
, 8);
849 return check_bytes8(start
, value
, bytes
% 8);
851 EXPORT_SYMBOL(memchr_inv
);
854 * strreplace - Replace all occurrences of character in string.
855 * @s: The string to operate on.
856 * @old: The character being replaced.
857 * @new: The character @old is replaced with.
859 * Returns pointer to the nul byte at the end of @s.
861 char *strreplace(char *s
, char old
, char new)
868 EXPORT_SYMBOL(strreplace
);