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/module.h>
27 #ifndef __HAVE_ARCH_STRNICMP
29 * strnicmp - Case insensitive, length-limited string comparison
31 * @s2: The other string
32 * @len: the maximum number of characters to compare
34 int strnicmp(const char *s1
, const char *s2
, size_t len
)
36 /* Yes, Virginia, it had better be unsigned */
58 return (int)c1
- (int)c2
;
60 EXPORT_SYMBOL(strnicmp
);
63 #ifndef __HAVE_ARCH_STRCASECMP
64 int strcasecmp(const char *s1
, const char *s2
)
71 } while (c1
== c2
&& c1
!= 0);
74 EXPORT_SYMBOL(strcasecmp
);
77 #ifndef __HAVE_ARCH_STRNCASECMP
78 int strncasecmp(const char *s1
, const char *s2
, size_t n
)
85 } while ((--n
> 0) && c1
== c2
&& c1
!= 0);
88 EXPORT_SYMBOL(strncasecmp
);
91 #ifndef __HAVE_ARCH_STRCPY
93 * strcpy - Copy a %NUL terminated string
94 * @dest: Where to copy the string to
95 * @src: Where to copy the string from
98 char *strcpy(char *dest
, const char *src
)
102 while ((*dest
++ = *src
++) != '\0')
106 EXPORT_SYMBOL(strcpy
);
109 #ifndef __HAVE_ARCH_STRNCPY
111 * strncpy - Copy a length-limited, %NUL-terminated string
112 * @dest: Where to copy the string to
113 * @src: Where to copy the string from
114 * @count: The maximum number of bytes to copy
116 * The result is not %NUL-terminated if the source exceeds
119 * In the case where the length of @src is less than that of
120 * count, the remainder of @dest will be padded with %NUL.
123 char *strncpy(char *dest
, const char *src
, size_t count
)
128 if ((*tmp
= *src
) != 0)
135 EXPORT_SYMBOL(strncpy
);
138 #ifndef __HAVE_ARCH_STRLCPY
140 * strlcpy - Copy a %NUL terminated string into a sized buffer
141 * @dest: Where to copy the string to
142 * @src: Where to copy the string from
143 * @size: size of destination buffer
145 * Compatible with *BSD: the result is always a valid
146 * NUL-terminated string that fits in the buffer (unless,
147 * of course, the buffer size is zero). It does not pad
148 * out the result like strncpy() does.
150 size_t strlcpy(char *dest
, const char *src
, size_t size
)
152 size_t ret
= strlen(src
);
155 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
156 memcpy(dest
, src
, len
);
161 EXPORT_SYMBOL(strlcpy
);
164 #ifndef __HAVE_ARCH_STRCAT
166 * strcat - Append one %NUL-terminated string to another
167 * @dest: The string to be appended to
168 * @src: The string to append to it
171 char *strcat(char *dest
, const char *src
)
177 while ((*dest
++ = *src
++) != '\0')
181 EXPORT_SYMBOL(strcat
);
184 #ifndef __HAVE_ARCH_STRNCAT
186 * strncat - Append a length-limited, %NUL-terminated string to another
187 * @dest: The string to be appended to
188 * @src: The string to append to it
189 * @count: The maximum numbers of bytes to copy
191 * Note that in contrast to strncpy(), strncat() ensures the result is
194 char *strncat(char *dest
, const char *src
, size_t count
)
201 while ((*dest
++ = *src
++) != 0) {
210 EXPORT_SYMBOL(strncat
);
213 #ifndef __HAVE_ARCH_STRLCAT
215 * strlcat - Append a length-limited, %NUL-terminated string to another
216 * @dest: The string to be appended to
217 * @src: The string to append to it
218 * @count: The size of the destination buffer.
220 size_t strlcat(char *dest
, const char *src
, size_t count
)
222 size_t dsize
= strlen(dest
);
223 size_t len
= strlen(src
);
224 size_t res
= dsize
+ len
;
226 /* This would be a bug */
227 BUG_ON(dsize
>= count
);
233 memcpy(dest
, src
, len
);
237 EXPORT_SYMBOL(strlcat
);
240 #ifndef __HAVE_ARCH_STRCMP
242 * strcmp - Compare two strings
244 * @ct: Another string
247 int strcmp(const char *cs
, const char *ct
)
249 unsigned char c1
, c2
;
255 return c1
< c2
? -1 : 1;
261 EXPORT_SYMBOL(strcmp
);
264 #ifndef __HAVE_ARCH_STRNCMP
266 * strncmp - Compare two length-limited strings
268 * @ct: Another string
269 * @count: The maximum number of bytes to compare
271 int strncmp(const char *cs
, const char *ct
, size_t count
)
273 unsigned char c1
, c2
;
279 return c1
< c2
? -1 : 1;
286 EXPORT_SYMBOL(strncmp
);
289 #ifndef __HAVE_ARCH_STRCHR
291 * strchr - Find the first occurrence of a character in a string
292 * @s: The string to be searched
293 * @c: The character to search for
295 char *strchr(const char *s
, int c
)
297 for (; *s
!= (char)c
; ++s
)
302 EXPORT_SYMBOL(strchr
);
305 #ifndef __HAVE_ARCH_STRRCHR
307 * strrchr - Find the last occurrence of a character in a string
308 * @s: The string to be searched
309 * @c: The character to search for
311 char *strrchr(const char *s
, int c
)
313 const char *p
= s
+ strlen(s
);
320 EXPORT_SYMBOL(strrchr
);
323 #ifndef __HAVE_ARCH_STRNCHR
325 * strnchr - Find a character in a length limited string
326 * @s: The string to be searched
327 * @count: The number of characters to be searched
328 * @c: The character to search for
330 char *strnchr(const char *s
, size_t count
, int c
)
332 for (; count
-- && *s
!= '\0'; ++s
)
337 EXPORT_SYMBOL(strnchr
);
341 * skip_spaces - Removes leading whitespace from @str.
342 * @str: The string to be stripped.
344 * Returns a pointer to the first non-whitespace character in @str.
346 char *skip_spaces(const char *str
)
348 while (isspace(*str
))
352 EXPORT_SYMBOL(skip_spaces
);
355 * strim - Removes leading and trailing whitespace from @s.
356 * @s: The string to be stripped.
358 * Note that the first trailing whitespace is replaced with a %NUL-terminator
359 * in the given string @s. Returns a pointer to the first non-whitespace
373 while (end
>= s
&& isspace(*end
))
379 EXPORT_SYMBOL(strim
);
381 #ifndef __HAVE_ARCH_STRLEN
383 * strlen - Find the length of a string
384 * @s: The string to be sized
386 size_t strlen(const char *s
)
390 for (sc
= s
; *sc
!= '\0'; ++sc
)
394 EXPORT_SYMBOL(strlen
);
397 #ifndef __HAVE_ARCH_STRNLEN
399 * strnlen - Find the length of a length-limited string
400 * @s: The string to be sized
401 * @count: The maximum number of bytes to search
403 size_t strnlen(const char *s
, size_t count
)
407 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
411 EXPORT_SYMBOL(strnlen
);
414 #ifndef __HAVE_ARCH_STRSPN
416 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
417 * @s: The string to be searched
418 * @accept: The string to search for
420 size_t strspn(const char *s
, const char *accept
)
426 for (p
= s
; *p
!= '\0'; ++p
) {
427 for (a
= accept
; *a
!= '\0'; ++a
) {
438 EXPORT_SYMBOL(strspn
);
441 #ifndef __HAVE_ARCH_STRCSPN
443 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
444 * @s: The string to be searched
445 * @reject: The string to avoid
447 size_t strcspn(const char *s
, const char *reject
)
453 for (p
= s
; *p
!= '\0'; ++p
) {
454 for (r
= reject
; *r
!= '\0'; ++r
) {
462 EXPORT_SYMBOL(strcspn
);
465 #ifndef __HAVE_ARCH_STRPBRK
467 * strpbrk - Find the first occurrence of a set of characters
468 * @cs: The string to be searched
469 * @ct: The characters to search for
471 char *strpbrk(const char *cs
, const char *ct
)
473 const char *sc1
, *sc2
;
475 for (sc1
= cs
; *sc1
!= '\0'; ++sc1
) {
476 for (sc2
= ct
; *sc2
!= '\0'; ++sc2
) {
483 EXPORT_SYMBOL(strpbrk
);
486 #ifndef __HAVE_ARCH_STRSEP
488 * strsep - Split a string into tokens
489 * @s: The string to be searched
490 * @ct: The characters to search for
492 * strsep() updates @s to point after the token, ready for the next call.
494 * It returns empty tokens, too, behaving exactly like the libc function
495 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
496 * Same semantics, slimmer shape. ;)
498 char *strsep(char **s
, const char *ct
)
506 end
= strpbrk(sbegin
, ct
);
512 EXPORT_SYMBOL(strsep
);
516 * sysfs_streq - return true if strings are equal, modulo trailing newline
518 * @s2: another string
520 * This routine returns true iff two strings are equal, treating both
521 * NUL and newline-then-NUL as equivalent string terminations. It's
522 * geared for use with sysfs input strings, which generally terminate
523 * with newlines but are compared against values without newlines.
525 bool sysfs_streq(const char *s1
, const char *s2
)
527 while (*s1
&& *s1
== *s2
) {
534 if (!*s1
&& *s2
== '\n' && !s2
[1])
536 if (*s1
== '\n' && !s1
[1] && !*s2
)
540 EXPORT_SYMBOL(sysfs_streq
);
542 #ifndef __HAVE_ARCH_MEMSET
544 * memset - Fill a region of memory with the given value
545 * @s: Pointer to the start of the area.
546 * @c: The byte to fill the area with
547 * @count: The size of the area.
549 * Do not use memset() to access IO space, use memset_io() instead.
551 void *memset(void *s
, int c
, size_t count
)
559 EXPORT_SYMBOL(memset
);
562 #ifndef __HAVE_ARCH_MEMCPY
564 * memcpy - Copy one area of memory to another
565 * @dest: Where to copy to
566 * @src: Where to copy from
567 * @count: The size of the area.
569 * You should not use this function to access IO space, use memcpy_toio()
570 * or memcpy_fromio() instead.
572 void *memcpy(void *dest
, const void *src
, size_t count
)
581 EXPORT_SYMBOL(memcpy
);
584 #ifndef __HAVE_ARCH_MEMMOVE
586 * memmove - Copy one area of memory to another
587 * @dest: Where to copy to
588 * @src: Where to copy from
589 * @count: The size of the area.
591 * Unlike memcpy(), memmove() copes with overlapping areas.
593 void *memmove(void *dest
, const void *src
, size_t count
)
613 EXPORT_SYMBOL(memmove
);
616 #ifndef __HAVE_ARCH_MEMCMP
618 * memcmp - Compare two areas of memory
619 * @cs: One area of memory
620 * @ct: Another area of memory
621 * @count: The size of the area.
624 int memcmp(const void *cs
, const void *ct
, size_t count
)
626 const unsigned char *su1
, *su2
;
629 for (su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--)
630 if ((res
= *su1
- *su2
) != 0)
634 EXPORT_SYMBOL(memcmp
);
637 #ifndef __HAVE_ARCH_MEMSCAN
639 * memscan - Find a character in an area of memory.
640 * @addr: The memory area
641 * @c: The byte to search for
642 * @size: The size of the area.
644 * returns the address of the first occurrence of @c, or 1 byte past
645 * the area if @c is not found
647 void *memscan(void *addr
, int c
, size_t size
)
649 unsigned char *p
= addr
;
659 EXPORT_SYMBOL(memscan
);
662 #ifndef __HAVE_ARCH_STRSTR
664 * strstr - Find the first substring in a %NUL terminated string
665 * @s1: The string to be searched
666 * @s2: The string to search for
668 char *strstr(const char *s1
, const char *s2
)
678 if (!memcmp(s1
, s2
, l2
))
684 EXPORT_SYMBOL(strstr
);
687 #ifndef __HAVE_ARCH_STRNSTR
689 * strnstr - Find the first substring in a length-limited string
690 * @s1: The string to be searched
691 * @s2: The string to search for
692 * @len: the maximum number of characters to search
694 char *strnstr(const char *s1
, const char *s2
, size_t len
)
703 if (!memcmp(s1
, s2
, l2
))
709 EXPORT_SYMBOL(strnstr
);
712 #ifndef __HAVE_ARCH_MEMCHR
714 * memchr - Find a character in an area of memory.
715 * @s: The memory area
716 * @c: The byte to search for
717 * @n: The size of the area.
719 * returns the address of the first occurrence of @c, or %NULL
722 void *memchr(const void *s
, int c
, size_t n
)
724 const unsigned char *p
= s
;
726 if ((unsigned char)c
== *p
++) {
727 return (void *)(p
- 1);
732 EXPORT_SYMBOL(memchr
);