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.
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/ctype.h>
25 * strncasecmp - Case insensitive, length-limited string comparison
27 * @s2: The other string
28 * @len: the maximum number of characters to compare
30 int strncasecmp(const char *s1, const char *s2, size_t len)
32 /* Yes, Virginia, it had better be unsigned */
52 return (int)c1 - (int)c2;
56 * strcasecmp - Case insensitive string comparison
58 * @s2: The other string
60 int strcasecmp(const char *s1, const char *s2)
62 return strncasecmp(s1, s2, -1U);
67 #ifndef __HAVE_ARCH_STRCPY
69 * strcpy - Copy a %NUL terminated string
70 * @dest: Where to copy the string to
71 * @src: Where to copy the string from
73 char * strcpy(char * dest,const char *src)
77 while ((*dest++ = *src++) != '\0')
83 #ifndef __HAVE_ARCH_STRNCPY
85 * strncpy - Copy a length-limited, %NUL-terminated string
86 * @dest: Where to copy the string to
87 * @src: Where to copy the string from
88 * @count: The maximum number of bytes to copy
90 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
91 * However, the result is not %NUL-terminated if the source exceeds
94 char * strncpy(char * dest,const char *src,size_t count)
98 while (count-- && (*dest++ = *src++) != '\0')
105 #ifndef __HAVE_ARCH_STRLCPY
107 * strlcpy - Copy a C-string into a sized buffer
108 * @dest: Where to copy the string to
109 * @src: Where to copy the string from
110 * @size: size of destination buffer
112 * Compatible with *BSD: the result is always a valid
113 * NUL-terminated string that fits in the buffer (unless,
114 * of course, the buffer size is zero). It does not pad
115 * out the result like strncpy() does.
117 size_t strlcpy(char *dest, const char *src, size_t size)
119 size_t ret = strlen(src);
122 size_t len = (ret >= size) ? size - 1 : ret;
123 memcpy(dest, src, len);
130 #ifndef __HAVE_ARCH_STRCAT
132 * strcat - Append one %NUL-terminated string to another
133 * @dest: The string to be appended to
134 * @src: The string to append to it
136 char * strcat(char * dest, const char * src)
142 while ((*dest++ = *src++) != '\0')
149 #ifndef __HAVE_ARCH_STRNCAT
151 * strncat - Append a length-limited, %NUL-terminated string to another
152 * @dest: The string to be appended to
153 * @src: The string to append to it
154 * @count: The maximum numbers of bytes to copy
156 * Note that in contrast to strncpy, strncat ensures the result is
159 char * strncat(char *dest, const char *src, size_t count)
166 while ((*dest++ = *src++)) {
178 #ifndef __HAVE_ARCH_STRCMP
180 * strcmp - Compare two strings
182 * @ct: Another string
184 int strcmp(const char * cs,const char * ct)
186 register signed char __res;
189 if ((__res = *cs - *ct++) != 0 || !*cs++)
197 #ifndef __HAVE_ARCH_STRNCMP
199 * strncmp - Compare two length-limited strings
201 * @ct: Another string
202 * @count: The maximum number of bytes to compare
204 int strncmp(const char * cs,const char * ct,size_t count)
206 register signed char __res = 0;
209 if ((__res = *cs - *ct++) != 0 || !*cs++)
218 #ifndef __HAVE_ARCH_STRCHR
220 * strchr - Find the first occurrence of a character in a string
221 * @s: The string to be searched
222 * @c: The character to search for
224 char * strchr(const char * s, int c)
226 for(; *s != (char) c; ++s)
233 #ifndef __HAVE_ARCH_STRRCHR
235 * strrchr - Find the last occurrence of a character in a string
236 * @s: The string to be searched
237 * @c: The character to search for
239 char * strrchr(const char * s, int c)
241 const char *p = s + strlen(s);
250 #ifndef __HAVE_ARCH_STRLEN
252 * strlen - Find the length of a string
253 * @s: The string to be sized
255 size_t strlen(const char * s)
259 for (sc = s; *sc != '\0'; ++sc)
265 #ifndef __HAVE_ARCH_STRNLEN
267 * strnlen - Find the length of a length-limited string
268 * @s: The string to be sized
269 * @count: The maximum number of bytes to search
271 size_t strnlen(const char * s, size_t count)
275 for (sc = s; count-- && *sc != '\0'; ++sc)
281 #ifndef __HAVE_ARCH_STRDUP
282 char * strdup(const char *s)
287 ((new = malloc (strlen(s) + 1)) == NULL) ) {
296 #ifndef __HAVE_ARCH_STRSPN
298 * strspn - Calculate the length of the initial substring of @s which only
299 * contain letters in @accept
300 * @s: The string to be searched
301 * @accept: The string to search for
303 size_t strspn(const char *s, const char *accept)
309 for (p = s; *p != '\0'; ++p) {
310 for (a = accept; *a != '\0'; ++a) {
323 #ifndef __HAVE_ARCH_STRPBRK
325 * strpbrk - Find the first occurrence of a set of characters
326 * @cs: The string to be searched
327 * @ct: The characters to search for
329 char * strpbrk(const char * cs,const char * ct)
331 const char *sc1,*sc2;
333 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
334 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
343 #ifndef __HAVE_ARCH_STRTOK
345 * strtok - Split a string into tokens
346 * @s: The string to be searched
347 * @ct: The characters to search for
349 * WARNING: strtok is deprecated, use strsep instead.
351 char * strtok(char * s,const char * ct)
355 sbegin = s ? s : ___strtok;
359 sbegin += strspn(sbegin,ct);
360 if (*sbegin == '\0') {
364 send = strpbrk( sbegin, ct);
365 if (send && *send != '\0')
372 #ifndef __HAVE_ARCH_STRSEP
374 * strsep - Split a string into tokens
375 * @s: The string to be searched
376 * @ct: The characters to search for
378 * strsep() updates @s to point after the token, ready for the next call.
380 * It returns empty tokens, too, behaving exactly like the libc function
381 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
382 * Same semantics, slimmer shape. ;)
384 char * strsep(char **s, const char *ct)
386 char *sbegin = *s, *end;
391 end = strpbrk(sbegin, ct);
400 #ifndef __HAVE_ARCH_STRSWAB
402 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
403 * s: address of the string
405 * returns the address of the swapped string or NULL on error. If
406 * string length is odd, last byte is untouched.
408 char *strswab(const char *s)
412 if ((NULL == s) || ('\0' == *s)) {
416 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
428 #ifndef __HAVE_ARCH_MEMSET
430 * memset - Fill a region of memory with the given value
431 * @s: Pointer to the start of the area.
432 * @c: The byte to fill the area with
433 * @count: The size of the area.
435 * Do not use memset() to access IO space, use memset_io() instead.
437 void * memset(void * s,int c,size_t count)
439 unsigned long *sl = (unsigned long *) s;
440 unsigned long cl = 0;
444 /* do it one word at a time (32 bits or 64 bits) while possible */
445 if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
446 for (i = 0; i < sizeof(*sl); i++) {
450 while (count >= sizeof(*sl)) {
452 count -= sizeof(*sl);
455 /* fill 8 bits at a time */
464 #ifndef __HAVE_ARCH_MEMCPY
466 * memcpy - Copy one area of memory to another
467 * @dest: Where to copy to
468 * @src: Where to copy from
469 * @count: The size of the area.
471 * You should not use this function to access IO space, use memcpy_toio()
472 * or memcpy_fromio() instead.
474 void * memcpy(void *dest, const void *src, size_t count)
476 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
482 /* while all data is aligned (common case), copy a word at a time */
483 if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
484 while (count >= sizeof(*dl)) {
486 count -= sizeof(*dl);
489 /* copy the reset one byte at a time */
499 #ifndef __HAVE_ARCH_MEMMOVE
501 * memmove - Copy one area of memory to another
502 * @dest: Where to copy to
503 * @src: Where to copy from
504 * @count: The size of the area.
506 * Unlike memcpy(), memmove() copes with overlapping areas.
508 void * memmove(void * dest,const void *src,size_t count)
522 tmp = (char *) dest + count;
523 s = (char *) src + count;
532 #ifndef __HAVE_ARCH_MEMCMP
534 * memcmp - Compare two areas of memory
535 * @cs: One area of memory
536 * @ct: Another area of memory
537 * @count: The size of the area.
539 int memcmp(const void * cs,const void * ct,size_t count)
541 const unsigned char *su1, *su2;
544 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
545 if ((res = *su1 - *su2) != 0)
551 #ifndef __HAVE_ARCH_MEMSCAN
553 * memscan - Find a character in an area of memory.
554 * @addr: The memory area
555 * @c: The byte to search for
556 * @size: The size of the area.
558 * returns the address of the first occurrence of @c, or 1 byte past
559 * the area if @c is not found
561 void * memscan(void * addr, int c, size_t size)
563 unsigned char * p = (unsigned char *) addr;
575 #ifndef __HAVE_ARCH_STRSTR
577 * strstr - Find the first substring in a %NUL terminated string
578 * @s1: The string to be searched
579 * @s2: The string to search for
581 char * strstr(const char * s1,const char * s2)
591 if (!memcmp(s1,s2,l2))
599 #ifndef __HAVE_ARCH_MEMCHR
601 * memchr - Find a character in an area of memory.
602 * @s: The memory area
603 * @c: The byte to search for
604 * @n: The size of the area.
606 * returns the address of the first occurrence of @c, or %NULL
609 void *memchr(const void *s, int c, size_t n)
611 const unsigned char *p = s;
613 if ((unsigned char)c == *p++) {
614 return (void *)(p-1);
621 #ifndef __HAVE_ARCH_MEMCHR_INV
622 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
626 return (void *)start;
633 * memchr_inv - Find an unmatching character in an area of memory.
634 * @start: The memory area
635 * @c: Find a character other than c
636 * @bytes: The size of the area.
638 * returns the address of the first character other than @c, or %NULL
639 * if the whole buffer contains just @c.
641 void *memchr_inv(const void *start, int c, size_t bytes)
645 unsigned int words, prefix;
648 return check_bytes8(start, value, bytes);
651 value64 |= value64 << 8;
652 value64 |= value64 << 16;
653 value64 |= value64 << 32;
655 prefix = (unsigned long)start % 8;
660 r = check_bytes8(start, value, prefix);
670 if (*(u64 *)start != value64)
671 return check_bytes8(start, value, 8);
676 return check_bytes8(start, value, bytes % 8);