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);
66 #ifndef __HAVE_ARCH_STRCASECMP
67 int strcasecmp(const char *s1, const char *s2)
74 } while (c1 == c2 && c1 != 0);
77 EXPORT_SYMBOL(strcasecmp);
80 #ifndef __HAVE_ARCH_STRCPY
82 * strcpy - Copy a %NUL terminated string
83 * @dest: Where to copy the string to
84 * @src: Where to copy the string from
87 char *strcpy(char *dest, const char *src)
91 while ((*dest++ = *src++) != '\0')
95 EXPORT_SYMBOL(strcpy);
98 #ifndef __HAVE_ARCH_STRNCPY
100 * strncpy - Copy a length-limited, C-string
101 * @dest: Where to copy the string to
102 * @src: Where to copy the string from
103 * @count: The maximum number of bytes to copy
105 * The result is not %NUL-terminated if the source exceeds
108 * In the case where the length of @src is less than that of
109 * count, the remainder of @dest will be padded with %NUL.
112 char *strncpy(char *dest, const char *src, size_t count)
117 if ((*tmp = *src) != 0)
124 EXPORT_SYMBOL(strncpy);
127 #ifndef __HAVE_ARCH_STRLCPY
129 * strlcpy - Copy a C-string into a sized buffer
130 * @dest: Where to copy the string to
131 * @src: Where to copy the string from
132 * @size: size of destination buffer
134 * Compatible with *BSD: the result is always a valid
135 * NUL-terminated string that fits in the buffer (unless,
136 * of course, the buffer size is zero). It does not pad
137 * out the result like strncpy() does.
139 size_t strlcpy(char *dest, const char *src, size_t size)
141 size_t ret = strlen(src);
144 size_t len = (ret >= size) ? size - 1 : ret;
145 memcpy(dest, src, len);
150 EXPORT_SYMBOL(strlcpy);
153 #ifndef __HAVE_ARCH_STRSCPY
155 * strscpy - Copy a C-string into a sized buffer
156 * @dest: Where to copy the string to
157 * @src: Where to copy the string from
158 * @count: Size of destination buffer
160 * Copy the string, or as much of it as fits, into the dest buffer. The
161 * behavior is undefined if the string buffers overlap. The destination
162 * buffer is always NUL terminated, unless it's zero-sized.
164 * Preferred to strlcpy() since the API doesn't require reading memory
165 * from the src string beyond the specified "count" bytes, and since
166 * the return value is easier to error-check than strlcpy()'s.
167 * In addition, the implementation is robust to the string changing out
168 * from underneath it, unlike the current strlcpy() implementation.
170 * Preferred to strncpy() since it always returns a valid string, and
171 * doesn't unnecessarily force the tail of the destination buffer to be
172 * zeroed. If zeroing is desired please use strscpy_pad().
174 * Return: The number of characters copied (not including the trailing
175 * %NUL) or -E2BIG if the destination buffer wasn't big enough.
177 ssize_t strscpy(char *dest, const char *src, size_t count)
179 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
186 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
188 * If src is unaligned, don't cross a page boundary,
189 * since we don't know if the next page is mapped.
191 if ((long)src & (sizeof(long) - 1)) {
192 size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
197 /* If src or dest is unaligned, don't do word-at-a-time. */
198 if (((long) dest | (long) src) & (sizeof(long) - 1))
202 while (max >= sizeof(unsigned long)) {
203 unsigned long c, data;
205 c = read_word_at_a_time(src+res);
206 if (has_zero(c, &data, &constants)) {
207 data = prep_zero_mask(c, data, &constants);
208 data = create_zero_mask(data);
209 *(unsigned long *)(dest+res) = c & zero_bytemask(data);
210 return res + find_zero(data);
212 *(unsigned long *)(dest+res) = c;
213 res += sizeof(unsigned long);
214 count -= sizeof(unsigned long);
215 max -= sizeof(unsigned long);
229 /* Hit buffer length without finding a NUL; force NUL-termination. */
235 EXPORT_SYMBOL(strscpy);
239 * stpcpy - copy a string from src to dest returning a pointer to the new end
240 * of dest, including src's %NUL-terminator. May overrun dest.
241 * @dest: pointer to end of string being copied into. Must be large enough
243 * @src: pointer to the beginning of string being copied from. Must not overlap
246 * stpcpy differs from strcpy in a key way: the return value is a pointer
247 * to the new %NUL-terminating character in @dest. (For strcpy, the return
248 * value is a pointer to the start of @dest). This interface is considered
249 * unsafe as it doesn't perform bounds checking of the inputs. As such it's
250 * not recommended for usage. Instead, its definition is provided in case
251 * the compiler lowers other libcalls to stpcpy.
253 char *stpcpy(char *__restrict__ dest, const char *__restrict__ src);
254 char *stpcpy(char *__restrict__ dest, const char *__restrict__ src)
256 while ((*dest++ = *src++) != '\0')
260 EXPORT_SYMBOL(stpcpy);
263 * strscpy_pad() - Copy a C-string into a sized buffer
264 * @dest: Where to copy the string to
265 * @src: Where to copy the string from
266 * @count: Size of destination buffer
268 * Copy the string, or as much of it as fits, into the dest buffer. The
269 * behavior is undefined if the string buffers overlap. The destination
270 * buffer is always %NUL terminated, unless it's zero-sized.
272 * If the source string is shorter than the destination buffer, zeros
273 * the tail of the destination buffer.
275 * For full explanation of why you may want to consider using the
276 * 'strscpy' functions please see the function docstring for strscpy().
278 * Return: The number of characters copied (not including the trailing
279 * %NUL) or -E2BIG if the destination buffer wasn't big enough.
281 ssize_t strscpy_pad(char *dest, const char *src, size_t count)
285 written = strscpy(dest, src, count);
286 if (written < 0 || written == count - 1)
289 memset(dest + written + 1, 0, count - written - 1);
293 EXPORT_SYMBOL(strscpy_pad);
295 #ifndef __HAVE_ARCH_STRCAT
297 * strcat - Append one %NUL-terminated string to another
298 * @dest: The string to be appended to
299 * @src: The string to append to it
302 char *strcat(char *dest, const char *src)
308 while ((*dest++ = *src++) != '\0')
312 EXPORT_SYMBOL(strcat);
315 #ifndef __HAVE_ARCH_STRNCAT
317 * strncat - Append a length-limited, C-string to another
318 * @dest: The string to be appended to
319 * @src: The string to append to it
320 * @count: The maximum numbers of bytes to copy
322 * Note that in contrast to strncpy(), strncat() ensures the result is
325 char *strncat(char *dest, const char *src, size_t count)
332 while ((*dest++ = *src++) != 0) {
341 EXPORT_SYMBOL(strncat);
344 #ifndef __HAVE_ARCH_STRLCAT
346 * strlcat - Append a length-limited, C-string to another
347 * @dest: The string to be appended to
348 * @src: The string to append to it
349 * @count: The size of the destination buffer.
351 size_t strlcat(char *dest, const char *src, size_t count)
353 size_t dsize = strlen(dest);
354 size_t len = strlen(src);
355 size_t res = dsize + len;
357 /* This would be a bug */
358 BUG_ON(dsize >= count);
364 memcpy(dest, src, len);
368 EXPORT_SYMBOL(strlcat);
371 #ifndef __HAVE_ARCH_STRCMP
373 * strcmp - Compare two strings
375 * @ct: Another string
378 int strcmp(const char *cs, const char *ct)
380 unsigned char c1, c2;
386 return c1 < c2 ? -1 : 1;
392 EXPORT_SYMBOL(strcmp);
395 #ifndef __HAVE_ARCH_STRNCMP
397 * strncmp - Compare two length-limited strings
399 * @ct: Another string
400 * @count: The maximum number of bytes to compare
402 int strncmp(const char *cs, const char *ct, size_t count)
404 unsigned char c1, c2;
410 return c1 < c2 ? -1 : 1;
417 EXPORT_SYMBOL(strncmp);
420 #ifndef __HAVE_ARCH_STRCHR
422 * strchr - Find the first occurrence of a character in a string
423 * @s: The string to be searched
424 * @c: The character to search for
426 char *strchr(const char *s, int c)
428 for (; *s != (char)c; ++s)
433 EXPORT_SYMBOL(strchr);
436 #ifndef __HAVE_ARCH_STRCHRNUL
438 * strchrnul - Find and return a character in a string, or end of string
439 * @s: The string to be searched
440 * @c: The character to search for
442 * Returns pointer to first occurrence of 'c' in s. If c is not found, then
443 * return a pointer to the null byte at the end of s.
445 char *strchrnul(const char *s, int c)
447 while (*s && *s != (char)c)
451 EXPORT_SYMBOL(strchrnul);
454 #ifndef __HAVE_ARCH_STRRCHR
456 * strrchr - Find the last occurrence of a character in a string
457 * @s: The string to be searched
458 * @c: The character to search for
460 char *strrchr(const char *s, int c)
462 const char *last = NULL;
469 EXPORT_SYMBOL(strrchr);
472 #ifndef __HAVE_ARCH_STRNCHR
474 * strnchr - Find a character in a length limited string
475 * @s: The string to be searched
476 * @count: The number of characters to be searched
477 * @c: The character to search for
479 char *strnchr(const char *s, size_t count, int c)
481 for (; count-- && *s != '\0'; ++s)
486 EXPORT_SYMBOL(strnchr);
490 * skip_spaces - Removes leading whitespace from @str.
491 * @str: The string to be stripped.
493 * Returns a pointer to the first non-whitespace character in @str.
495 char *skip_spaces(const char *str)
497 while (isspace(*str))
501 EXPORT_SYMBOL(skip_spaces);
504 * strim - Removes leading and trailing whitespace from @s.
505 * @s: The string to be stripped.
507 * Note that the first trailing whitespace is replaced with a %NUL-terminator
508 * in the given string @s. Returns a pointer to the first non-whitespace
521 while (end >= s && isspace(*end))
525 return skip_spaces(s);
527 EXPORT_SYMBOL(strim);
529 #ifndef __HAVE_ARCH_STRLEN
531 * strlen - Find the length of a string
532 * @s: The string to be sized
534 size_t strlen(const char *s)
538 for (sc = s; *sc != '\0'; ++sc)
542 EXPORT_SYMBOL(strlen);
545 #ifndef __HAVE_ARCH_STRNLEN
547 * strnlen - Find the length of a length-limited string
548 * @s: The string to be sized
549 * @count: The maximum number of bytes to search
551 size_t strnlen(const char *s, size_t count)
555 for (sc = s; count-- && *sc != '\0'; ++sc)
559 EXPORT_SYMBOL(strnlen);
562 #ifndef __HAVE_ARCH_STRSPN
564 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
565 * @s: The string to be searched
566 * @accept: The string to search for
568 size_t strspn(const char *s, const char *accept)
574 for (p = s; *p != '\0'; ++p) {
575 for (a = accept; *a != '\0'; ++a) {
586 EXPORT_SYMBOL(strspn);
589 #ifndef __HAVE_ARCH_STRCSPN
591 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
592 * @s: The string to be searched
593 * @reject: The string to avoid
595 size_t strcspn(const char *s, const char *reject)
601 for (p = s; *p != '\0'; ++p) {
602 for (r = reject; *r != '\0'; ++r) {
610 EXPORT_SYMBOL(strcspn);
613 #ifndef __HAVE_ARCH_STRPBRK
615 * strpbrk - Find the first occurrence of a set of characters
616 * @cs: The string to be searched
617 * @ct: The characters to search for
619 char *strpbrk(const char *cs, const char *ct)
621 const char *sc1, *sc2;
623 for (sc1 = cs; *sc1 != '\0'; ++sc1) {
624 for (sc2 = ct; *sc2 != '\0'; ++sc2) {
631 EXPORT_SYMBOL(strpbrk);
634 #ifndef __HAVE_ARCH_STRSEP
636 * strsep - Split a string into tokens
637 * @s: The string to be searched
638 * @ct: The characters to search for
640 * strsep() updates @s to point after the token, ready for the next call.
642 * It returns empty tokens, too, behaving exactly like the libc function
643 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
644 * Same semantics, slimmer shape. ;)
646 char *strsep(char **s, const char *ct)
654 end = strpbrk(sbegin, ct);
660 EXPORT_SYMBOL(strsep);
664 * sysfs_streq - return true if strings are equal, modulo trailing newline
666 * @s2: another string
668 * This routine returns true iff two strings are equal, treating both
669 * NUL and newline-then-NUL as equivalent string terminations. It's
670 * geared for use with sysfs input strings, which generally terminate
671 * with newlines but are compared against values without newlines.
673 bool sysfs_streq(const char *s1, const char *s2)
675 while (*s1 && *s1 == *s2) {
682 if (!*s1 && *s2 == '\n' && !s2[1])
684 if (*s1 == '\n' && !s1[1] && !*s2)
688 EXPORT_SYMBOL(sysfs_streq);
691 * match_string - matches given string in an array
692 * @array: array of strings
693 * @n: number of strings in the array or -1 for NULL terminated arrays
694 * @string: string to match with
697 * index of a @string in the @array if matches, or %-EINVAL otherwise.
699 int match_string(const char * const *array, size_t n, const char *string)
704 for (index = 0; index < n; index++) {
708 if (!strcmp(item, string))
714 EXPORT_SYMBOL(match_string);
716 #ifndef __HAVE_ARCH_MEMSET
718 * memset - Fill a region of memory with the given value
719 * @s: Pointer to the start of the area.
720 * @c: The byte to fill the area with
721 * @count: The size of the area.
723 * Do not use memset() to access IO space, use memset_io() instead.
725 void *memset(void *s, int c, size_t count)
733 EXPORT_SYMBOL(memset);
737 * memzero_explicit - Fill a region of memory (e.g. sensitive
738 * keying data) with 0s.
739 * @s: Pointer to the start of the area.
740 * @count: The size of the area.
742 * Note: usually using memset() is just fine (!), but in cases
743 * where clearing out _local_ data at the end of a scope is
744 * necessary, memzero_explicit() should be used instead in
745 * order to prevent the compiler from optimising away zeroing.
747 * memzero_explicit() doesn't need an arch-specific version as
748 * it just invokes the one of memset() implicitly.
750 void memzero_explicit(void *s, size_t count)
755 EXPORT_SYMBOL(memzero_explicit);
757 #ifndef __HAVE_ARCH_MEMSET16
759 * memset16() - Fill a memory area with a uint16_t
760 * @s: Pointer to the start of the area.
761 * @v: The value to fill the area with
762 * @count: The number of values to store
764 * Differs from memset() in that it fills with a uint16_t instead
765 * of a byte. Remember that @count is the number of uint16_ts to
766 * store, not the number of bytes.
768 void *memset16(uint16_t *s, uint16_t v, size_t count)
776 EXPORT_SYMBOL(memset16);
779 #ifndef __HAVE_ARCH_MEMSET32
781 * memset32() - Fill a memory area with a uint32_t
782 * @s: Pointer to the start of the area.
783 * @v: The value to fill the area with
784 * @count: The number of values to store
786 * Differs from memset() in that it fills with a uint32_t instead
787 * of a byte. Remember that @count is the number of uint32_ts to
788 * store, not the number of bytes.
790 void *memset32(uint32_t *s, uint32_t v, size_t count)
798 EXPORT_SYMBOL(memset32);
801 #ifndef __HAVE_ARCH_MEMSET64
803 * memset64() - Fill a memory area with a uint64_t
804 * @s: Pointer to the start of the area.
805 * @v: The value to fill the area with
806 * @count: The number of values to store
808 * Differs from memset() in that it fills with a uint64_t instead
809 * of a byte. Remember that @count is the number of uint64_ts to
810 * store, not the number of bytes.
812 void *memset64(uint64_t *s, uint64_t v, size_t count)
820 EXPORT_SYMBOL(memset64);
823 #ifndef __HAVE_ARCH_MEMCPY
825 * memcpy - Copy one area of memory to another
826 * @dest: Where to copy to
827 * @src: Where to copy from
828 * @count: The size of the area.
830 * You should not use this function to access IO space, use memcpy_toio()
831 * or memcpy_fromio() instead.
833 void *memcpy(void *dest, const void *src, size_t count)
842 EXPORT_SYMBOL(memcpy);
845 #ifndef __HAVE_ARCH_MEMMOVE
847 * memmove - Copy one area of memory to another
848 * @dest: Where to copy to
849 * @src: Where to copy from
850 * @count: The size of the area.
852 * Unlike memcpy(), memmove() copes with overlapping areas.
854 void *memmove(void *dest, const void *src, size_t count)
874 EXPORT_SYMBOL(memmove);
877 #ifndef __HAVE_ARCH_MEMCMP
879 * memcmp - Compare two areas of memory
880 * @cs: One area of memory
881 * @ct: Another area of memory
882 * @count: The size of the area.
885 __visible int memcmp(const void *cs, const void *ct, size_t count)
887 const unsigned char *su1, *su2;
890 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
891 if ((res = *su1 - *su2) != 0)
895 EXPORT_SYMBOL(memcmp);
898 #ifndef __HAVE_ARCH_BCMP
900 * bcmp - returns 0 if and only if the buffers have identical contents.
901 * @a: pointer to first buffer.
902 * @b: pointer to second buffer.
903 * @len: size of buffers.
905 * The sign or magnitude of a non-zero return value has no particular
906 * meaning, and architectures may implement their own more efficient bcmp(). So
907 * while this particular implementation is a simple (tail) call to memcmp, do
908 * not rely on anything but whether the return value is zero or non-zero.
911 int bcmp(const void *a, const void *b, size_t len)
913 return memcmp(a, b, len);
918 #ifndef __HAVE_ARCH_MEMSCAN
920 * memscan - Find a character in an area of memory.
921 * @addr: The memory area
922 * @c: The byte to search for
923 * @size: The size of the area.
925 * returns the address of the first occurrence of @c, or 1 byte past
926 * the area if @c is not found
928 void *memscan(void *addr, int c, size_t size)
930 unsigned char *p = addr;
940 EXPORT_SYMBOL(memscan);
943 #ifndef __HAVE_ARCH_STRSTR
945 * strstr - Find the first substring in a %NUL terminated string
946 * @s1: The string to be searched
947 * @s2: The string to search for
949 char *strstr(const char *s1, const char *s2)
959 if (!memcmp(s1, s2, l2))
965 EXPORT_SYMBOL(strstr);
968 #ifndef __HAVE_ARCH_STRNSTR
970 * strnstr - Find the first substring in a length-limited string
971 * @s1: The string to be searched
972 * @s2: The string to search for
973 * @len: the maximum number of characters to search
975 char *strnstr(const char *s1, const char *s2, size_t len)
984 if (!memcmp(s1, s2, l2))
990 EXPORT_SYMBOL(strnstr);
993 #ifndef __HAVE_ARCH_MEMCHR
995 * memchr - Find a character in an area of memory.
996 * @s: The memory area
997 * @c: The byte to search for
998 * @n: The size of the area.
1000 * returns the address of the first occurrence of @c, or %NULL
1001 * if @c is not found
1003 void *memchr(const void *s, int c, size_t n)
1005 const unsigned char *p = s;
1007 if ((unsigned char)c == *p++) {
1008 return (void *)(p - 1);
1013 EXPORT_SYMBOL(memchr);
1016 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
1019 if (*start != value)
1020 return (void *)start;
1028 * memchr_inv - Find an unmatching character in an area of memory.
1029 * @start: The memory area
1030 * @c: Find a character other than c
1031 * @bytes: The size of the area.
1033 * returns the address of the first character other than @c, or %NULL
1034 * if the whole buffer contains just @c.
1036 void *memchr_inv(const void *start, int c, size_t bytes)
1040 unsigned int words, prefix;
1043 return check_bytes8(start, value, bytes);
1046 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
1047 value64 *= 0x0101010101010101ULL;
1048 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
1049 value64 *= 0x01010101;
1050 value64 |= value64 << 32;
1052 value64 |= value64 << 8;
1053 value64 |= value64 << 16;
1054 value64 |= value64 << 32;
1057 prefix = (unsigned long)start % 8;
1061 prefix = 8 - prefix;
1062 r = check_bytes8(start, value, prefix);
1072 if (*(u64 *)start != value64)
1073 return check_bytes8(start, value, 8);
1078 return check_bytes8(start, value, bytes % 8);
1080 EXPORT_SYMBOL(memchr_inv);
1083 * strreplace - Replace all occurrences of character in string.
1084 * @s: The string to operate on.
1085 * @old: The character being replaced.
1086 * @new: The character @old is replaced with.
1088 * Returns pointer to the nul byte at the end of @s.
1090 char *strreplace(char *s, char old, char new)
1097 EXPORT_SYMBOL(strreplace);