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 #include <asm/byteorder.h>
31 #include <asm/word-at-a-time.h>
34 #ifndef __HAVE_ARCH_STRNCASECMP
36 * strncasecmp - Case insensitive, length-limited string comparison
38 * @s2: The other string
39 * @len: the maximum number of characters to compare
41 int strncasecmp(const char *s1
, const char *s2
, size_t len
)
43 /* Yes, Virginia, it had better be unsigned */
61 return (int)c1
- (int)c2
;
63 EXPORT_SYMBOL(strncasecmp
);
65 #ifndef __HAVE_ARCH_STRNICMP
67 int strnicmp(const char *s1
, const char *s2
, size_t len
)
69 return strncasecmp(s1
, s2
, len
);
71 EXPORT_SYMBOL(strnicmp
);
74 #ifndef __HAVE_ARCH_STRCASECMP
75 int strcasecmp(const char *s1
, const char *s2
)
82 } while (c1
== c2
&& c1
!= 0);
85 EXPORT_SYMBOL(strcasecmp
);
88 #ifndef __HAVE_ARCH_STRCPY
90 * strcpy - Copy a %NUL terminated string
91 * @dest: Where to copy the string to
92 * @src: Where to copy the string from
95 char *strcpy(char *dest
, const char *src
)
99 while ((*dest
++ = *src
++) != '\0')
103 EXPORT_SYMBOL(strcpy
);
106 #ifndef __HAVE_ARCH_STRNCPY
108 * strncpy - Copy a length-limited, C-string
109 * @dest: Where to copy the string to
110 * @src: Where to copy the string from
111 * @count: The maximum number of bytes to copy
113 * The result is not %NUL-terminated if the source exceeds
116 * In the case where the length of @src is less than that of
117 * count, the remainder of @dest will be padded with %NUL.
120 char *strncpy(char *dest
, const char *src
, size_t count
)
125 if ((*tmp
= *src
) != 0)
132 EXPORT_SYMBOL(strncpy
);
135 #ifndef __HAVE_ARCH_STRLCPY
137 * strlcpy - Copy a C-string into a sized buffer
138 * @dest: Where to copy the string to
139 * @src: Where to copy the string from
140 * @size: size of destination buffer
142 * Compatible with *BSD: the result is always a valid
143 * NUL-terminated string that fits in the buffer (unless,
144 * of course, the buffer size is zero). It does not pad
145 * out the result like strncpy() does.
147 size_t strlcpy(char *dest
, const char *src
, size_t size
)
149 size_t ret
= strlen(src
);
152 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
153 memcpy(dest
, src
, len
);
158 EXPORT_SYMBOL(strlcpy
);
161 #ifndef __HAVE_ARCH_STRSCPY
163 * strscpy - Copy a C-string into a sized buffer
164 * @dest: Where to copy the string to
165 * @src: Where to copy the string from
166 * @count: Size of destination buffer
168 * Copy the string, or as much of it as fits, into the dest buffer.
169 * The routine returns the number of characters copied (not including
170 * the trailing NUL) or -E2BIG if the destination buffer wasn't big enough.
171 * The behavior is undefined if the string buffers overlap.
172 * The destination buffer is always NUL terminated, unless it's zero-sized.
174 * Preferred to strlcpy() since the API doesn't require reading memory
175 * from the src string beyond the specified "count" bytes, and since
176 * the return value is easier to error-check than strlcpy()'s.
177 * In addition, the implementation is robust to the string changing out
178 * from underneath it, unlike the current strlcpy() implementation.
180 * Preferred to strncpy() since it always returns a valid string, and
181 * doesn't unnecessarily force the tail of the destination buffer to be
182 * zeroed. If the zeroing is desired, it's likely cleaner to use strscpy()
183 * with an overflow test, then just memset() the tail of the dest buffer.
185 ssize_t
strscpy(char *dest
, const char *src
, size_t count
)
187 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
194 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
196 * If src is unaligned, don't cross a page boundary,
197 * since we don't know if the next page is mapped.
199 if ((long)src
& (sizeof(long) - 1)) {
200 size_t limit
= PAGE_SIZE
- ((long)src
& (PAGE_SIZE
- 1));
205 /* If src or dest is unaligned, don't do word-at-a-time. */
206 if (((long) dest
| (long) src
) & (sizeof(long) - 1))
210 while (max
>= sizeof(unsigned long)) {
211 unsigned long c
, data
;
213 c
= *(unsigned long *)(src
+res
);
214 if (has_zero(c
, &data
, &constants
)) {
215 data
= prep_zero_mask(c
, data
, &constants
);
216 data
= create_zero_mask(data
);
217 *(unsigned long *)(dest
+res
) = c
& zero_bytemask(data
);
218 return res
+ find_zero(data
);
220 *(unsigned long *)(dest
+res
) = c
;
221 res
+= sizeof(unsigned long);
222 count
-= sizeof(unsigned long);
223 max
-= sizeof(unsigned long);
237 /* Hit buffer length without finding a NUL; force NUL-termination. */
243 EXPORT_SYMBOL(strscpy
);
246 #ifndef __HAVE_ARCH_STRCAT
248 * strcat - Append one %NUL-terminated string to another
249 * @dest: The string to be appended to
250 * @src: The string to append to it
253 char *strcat(char *dest
, const char *src
)
259 while ((*dest
++ = *src
++) != '\0')
263 EXPORT_SYMBOL(strcat
);
266 #ifndef __HAVE_ARCH_STRNCAT
268 * strncat - Append a length-limited, C-string to another
269 * @dest: The string to be appended to
270 * @src: The string to append to it
271 * @count: The maximum numbers of bytes to copy
273 * Note that in contrast to strncpy(), strncat() ensures the result is
276 char *strncat(char *dest
, const char *src
, size_t count
)
283 while ((*dest
++ = *src
++) != 0) {
292 EXPORT_SYMBOL(strncat
);
295 #ifndef __HAVE_ARCH_STRLCAT
297 * strlcat - Append a length-limited, C-string to another
298 * @dest: The string to be appended to
299 * @src: The string to append to it
300 * @count: The size of the destination buffer.
302 size_t strlcat(char *dest
, const char *src
, size_t count
)
304 size_t dsize
= strlen(dest
);
305 size_t len
= strlen(src
);
306 size_t res
= dsize
+ len
;
308 /* This would be a bug */
309 BUG_ON(dsize
>= count
);
315 memcpy(dest
, src
, len
);
319 EXPORT_SYMBOL(strlcat
);
322 #ifndef __HAVE_ARCH_STRCMP
324 * strcmp - Compare two strings
326 * @ct: Another string
329 int strcmp(const char *cs
, const char *ct
)
331 unsigned char c1
, c2
;
337 return c1
< c2
? -1 : 1;
343 EXPORT_SYMBOL(strcmp
);
346 #ifndef __HAVE_ARCH_STRNCMP
348 * strncmp - Compare two length-limited strings
350 * @ct: Another string
351 * @count: The maximum number of bytes to compare
353 int strncmp(const char *cs
, const char *ct
, size_t count
)
355 unsigned char c1
, c2
;
361 return c1
< c2
? -1 : 1;
368 EXPORT_SYMBOL(strncmp
);
371 #ifndef __HAVE_ARCH_STRCHR
373 * strchr - Find the first occurrence of a character in a string
374 * @s: The string to be searched
375 * @c: The character to search for
377 char *strchr(const char *s
, int c
)
379 for (; *s
!= (char)c
; ++s
)
384 EXPORT_SYMBOL(strchr
);
387 #ifndef __HAVE_ARCH_STRCHRNUL
389 * strchrnul - Find and return a character in a string, or end of string
390 * @s: The string to be searched
391 * @c: The character to search for
393 * Returns pointer to first occurrence of 'c' in s. If c is not found, then
394 * return a pointer to the null byte at the end of s.
396 char *strchrnul(const char *s
, int c
)
398 while (*s
&& *s
!= (char)c
)
402 EXPORT_SYMBOL(strchrnul
);
405 #ifndef __HAVE_ARCH_STRRCHR
407 * strrchr - Find the last occurrence of a character in a string
408 * @s: The string to be searched
409 * @c: The character to search for
411 char *strrchr(const char *s
, int c
)
413 const char *p
= s
+ strlen(s
);
420 EXPORT_SYMBOL(strrchr
);
423 #ifndef __HAVE_ARCH_STRNCHR
425 * strnchr - Find a character in a length limited string
426 * @s: The string to be searched
427 * @count: The number of characters to be searched
428 * @c: The character to search for
430 char *strnchr(const char *s
, size_t count
, int c
)
432 for (; count
-- && *s
!= '\0'; ++s
)
437 EXPORT_SYMBOL(strnchr
);
441 * skip_spaces - Removes leading whitespace from @str.
442 * @str: The string to be stripped.
444 * Returns a pointer to the first non-whitespace character in @str.
446 char *skip_spaces(const char *str
)
448 while (isspace(*str
))
452 EXPORT_SYMBOL(skip_spaces
);
455 * strim - Removes leading and trailing whitespace from @s.
456 * @s: The string to be stripped.
458 * Note that the first trailing whitespace is replaced with a %NUL-terminator
459 * in the given string @s. Returns a pointer to the first non-whitespace
472 while (end
>= s
&& isspace(*end
))
476 return skip_spaces(s
);
478 EXPORT_SYMBOL(strim
);
480 #ifndef __HAVE_ARCH_STRLEN
482 * strlen - Find the length of a string
483 * @s: The string to be sized
485 size_t strlen(const char *s
)
489 for (sc
= s
; *sc
!= '\0'; ++sc
)
493 EXPORT_SYMBOL(strlen
);
496 #ifndef __HAVE_ARCH_STRNLEN
498 * strnlen - Find the length of a length-limited string
499 * @s: The string to be sized
500 * @count: The maximum number of bytes to search
502 size_t strnlen(const char *s
, size_t count
)
506 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
510 EXPORT_SYMBOL(strnlen
);
513 #ifndef __HAVE_ARCH_STRSPN
515 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
516 * @s: The string to be searched
517 * @accept: The string to search for
519 size_t strspn(const char *s
, const char *accept
)
525 for (p
= s
; *p
!= '\0'; ++p
) {
526 for (a
= accept
; *a
!= '\0'; ++a
) {
537 EXPORT_SYMBOL(strspn
);
540 #ifndef __HAVE_ARCH_STRCSPN
542 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
543 * @s: The string to be searched
544 * @reject: The string to avoid
546 size_t strcspn(const char *s
, const char *reject
)
552 for (p
= s
; *p
!= '\0'; ++p
) {
553 for (r
= reject
; *r
!= '\0'; ++r
) {
561 EXPORT_SYMBOL(strcspn
);
564 #ifndef __HAVE_ARCH_STRPBRK
566 * strpbrk - Find the first occurrence of a set of characters
567 * @cs: The string to be searched
568 * @ct: The characters to search for
570 char *strpbrk(const char *cs
, const char *ct
)
572 const char *sc1
, *sc2
;
574 for (sc1
= cs
; *sc1
!= '\0'; ++sc1
) {
575 for (sc2
= ct
; *sc2
!= '\0'; ++sc2
) {
582 EXPORT_SYMBOL(strpbrk
);
585 #ifndef __HAVE_ARCH_STRSEP
587 * strsep - Split a string into tokens
588 * @s: The string to be searched
589 * @ct: The characters to search for
591 * strsep() updates @s to point after the token, ready for the next call.
593 * It returns empty tokens, too, behaving exactly like the libc function
594 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
595 * Same semantics, slimmer shape. ;)
597 char *strsep(char **s
, const char *ct
)
605 end
= strpbrk(sbegin
, ct
);
611 EXPORT_SYMBOL(strsep
);
615 * sysfs_streq - return true if strings are equal, modulo trailing newline
617 * @s2: another string
619 * This routine returns true iff two strings are equal, treating both
620 * NUL and newline-then-NUL as equivalent string terminations. It's
621 * geared for use with sysfs input strings, which generally terminate
622 * with newlines but are compared against values without newlines.
624 bool sysfs_streq(const char *s1
, const char *s2
)
626 while (*s1
&& *s1
== *s2
) {
633 if (!*s1
&& *s2
== '\n' && !s2
[1])
635 if (*s1
== '\n' && !s1
[1] && !*s2
)
639 EXPORT_SYMBOL(sysfs_streq
);
642 * strtobool - convert common user inputs into boolean values
646 * This routine returns 0 iff the first character is one of 'Yy1Nn0'.
647 * Otherwise it will return -EINVAL. Value pointed to by res is
648 * updated upon finding a match.
650 int strtobool(const char *s
, bool *res
)
668 EXPORT_SYMBOL(strtobool
);
670 #ifndef __HAVE_ARCH_MEMSET
672 * memset - Fill a region of memory with the given value
673 * @s: Pointer to the start of the area.
674 * @c: The byte to fill the area with
675 * @count: The size of the area.
677 * Do not use memset() to access IO space, use memset_io() instead.
679 void *memset(void *s
, int c
, size_t count
)
687 EXPORT_SYMBOL(memset
);
691 * memzero_explicit - Fill a region of memory (e.g. sensitive
692 * keying data) with 0s.
693 * @s: Pointer to the start of the area.
694 * @count: The size of the area.
696 * memzero_explicit() doesn't need an arch-specific version as
697 * it just invokes the one of memset() implicitly.
699 void memzero_explicit(void *s
, size_t count
)
704 EXPORT_SYMBOL(memzero_explicit
);
706 #ifndef __HAVE_ARCH_MEMCPY
708 * memcpy - Copy one area of memory to another
709 * @dest: Where to copy to
710 * @src: Where to copy from
711 * @count: The size of the area.
713 * You should not use this function to access IO space, use memcpy_toio()
714 * or memcpy_fromio() instead.
716 void *memcpy(void *dest
, const void *src
, size_t count
)
725 EXPORT_SYMBOL(memcpy
);
728 #ifndef __HAVE_ARCH_MEMMOVE
730 * memmove - Copy one area of memory to another
731 * @dest: Where to copy to
732 * @src: Where to copy from
733 * @count: The size of the area.
735 * Unlike memcpy(), memmove() copes with overlapping areas.
737 void *memmove(void *dest
, const void *src
, size_t count
)
757 EXPORT_SYMBOL(memmove
);
760 #ifndef __HAVE_ARCH_MEMCMP
762 * memcmp - Compare two areas of memory
763 * @cs: One area of memory
764 * @ct: Another area of memory
765 * @count: The size of the area.
768 __visible
int memcmp(const void *cs
, const void *ct
, size_t count
)
770 const unsigned char *su1
, *su2
;
773 for (su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--)
774 if ((res
= *su1
- *su2
) != 0)
778 EXPORT_SYMBOL(memcmp
);
781 #ifndef __HAVE_ARCH_MEMSCAN
783 * memscan - Find a character in an area of memory.
784 * @addr: The memory area
785 * @c: The byte to search for
786 * @size: The size of the area.
788 * returns the address of the first occurrence of @c, or 1 byte past
789 * the area if @c is not found
791 void *memscan(void *addr
, int c
, size_t size
)
793 unsigned char *p
= addr
;
803 EXPORT_SYMBOL(memscan
);
806 #ifndef __HAVE_ARCH_STRSTR
808 * strstr - Find the first substring in a %NUL terminated string
809 * @s1: The string to be searched
810 * @s2: The string to search for
812 char *strstr(const char *s1
, const char *s2
)
822 if (!memcmp(s1
, s2
, l2
))
828 EXPORT_SYMBOL(strstr
);
831 #ifndef __HAVE_ARCH_STRNSTR
833 * strnstr - Find the first substring in a length-limited string
834 * @s1: The string to be searched
835 * @s2: The string to search for
836 * @len: the maximum number of characters to search
838 char *strnstr(const char *s1
, const char *s2
, size_t len
)
847 if (!memcmp(s1
, s2
, l2
))
853 EXPORT_SYMBOL(strnstr
);
856 #ifndef __HAVE_ARCH_MEMCHR
858 * memchr - Find a character in an area of memory.
859 * @s: The memory area
860 * @c: The byte to search for
861 * @n: The size of the area.
863 * returns the address of the first occurrence of @c, or %NULL
866 void *memchr(const void *s
, int c
, size_t n
)
868 const unsigned char *p
= s
;
870 if ((unsigned char)c
== *p
++) {
871 return (void *)(p
- 1);
876 EXPORT_SYMBOL(memchr
);
879 static void *check_bytes8(const u8
*start
, u8 value
, unsigned int bytes
)
883 return (void *)start
;
891 * memchr_inv - Find an unmatching character in an area of memory.
892 * @start: The memory area
893 * @c: Find a character other than c
894 * @bytes: The size of the area.
896 * returns the address of the first character other than @c, or %NULL
897 * if the whole buffer contains just @c.
899 void *memchr_inv(const void *start
, int c
, size_t bytes
)
903 unsigned int words
, prefix
;
906 return check_bytes8(start
, value
, bytes
);
909 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
910 value64
*= 0x0101010101010101;
911 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
912 value64
*= 0x01010101;
913 value64
|= value64
<< 32;
915 value64
|= value64
<< 8;
916 value64
|= value64
<< 16;
917 value64
|= value64
<< 32;
920 prefix
= (unsigned long)start
% 8;
925 r
= check_bytes8(start
, value
, prefix
);
935 if (*(u64
*)start
!= value64
)
936 return check_bytes8(start
, value
, 8);
941 return check_bytes8(start
, value
, bytes
% 8);
943 EXPORT_SYMBOL(memchr_inv
);