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_STRCAT
107 * strcat - Append one %NUL-terminated string to another
108 * @dest: The string to be appended to
109 * @src: The string to append to it
111 char * strcat(char * dest, const char * src)
117 while ((*dest++ = *src++) != '\0')
124 #ifndef __HAVE_ARCH_STRNCAT
126 * strncat - Append a length-limited, %NUL-terminated string to another
127 * @dest: The string to be appended to
128 * @src: The string to append to it
129 * @count: The maximum numbers of bytes to copy
131 * Note that in contrast to strncpy, strncat ensures the result is
134 char * strncat(char *dest, const char *src, size_t count)
141 while ((*dest++ = *src++)) {
153 #ifndef __HAVE_ARCH_STRCMP
155 * strcmp - Compare two strings
157 * @ct: Another string
159 int strcmp(const char * cs,const char * ct)
161 register signed char __res;
164 if ((__res = *cs - *ct++) != 0 || !*cs++)
172 #ifndef __HAVE_ARCH_STRNCMP
174 * strncmp - Compare two length-limited strings
176 * @ct: Another string
177 * @count: The maximum number of bytes to compare
179 int strncmp(const char * cs,const char * ct,size_t count)
181 register signed char __res = 0;
184 if ((__res = *cs - *ct++) != 0 || !*cs++)
193 #ifndef __HAVE_ARCH_STRCHR
195 * strchr - Find the first occurrence of a character in a string
196 * @s: The string to be searched
197 * @c: The character to search for
199 char * strchr(const char * s, int c)
201 for(; *s != (char) c; ++s)
208 #ifndef __HAVE_ARCH_STRRCHR
210 * strrchr - Find the last occurrence of a character in a string
211 * @s: The string to be searched
212 * @c: The character to search for
214 char * strrchr(const char * s, int c)
216 const char *p = s + strlen(s);
225 #ifndef __HAVE_ARCH_STRLEN
227 * strlen - Find the length of a string
228 * @s: The string to be sized
230 size_t strlen(const char * s)
234 for (sc = s; *sc != '\0'; ++sc)
240 #ifndef __HAVE_ARCH_STRNLEN
242 * strnlen - Find the length of a length-limited string
243 * @s: The string to be sized
244 * @count: The maximum number of bytes to search
246 size_t strnlen(const char * s, size_t count)
250 for (sc = s; count-- && *sc != '\0'; ++sc)
256 #ifndef __HAVE_ARCH_STRDUP
257 char * strdup(const char *s)
262 ((new = malloc (strlen(s) + 1)) == NULL) ) {
271 #ifndef __HAVE_ARCH_STRSPN
273 * strspn - Calculate the length of the initial substring of @s which only
274 * contain letters in @accept
275 * @s: The string to be searched
276 * @accept: The string to search for
278 size_t strspn(const char *s, const char *accept)
284 for (p = s; *p != '\0'; ++p) {
285 for (a = accept; *a != '\0'; ++a) {
298 #ifndef __HAVE_ARCH_STRPBRK
300 * strpbrk - Find the first occurrence of a set of characters
301 * @cs: The string to be searched
302 * @ct: The characters to search for
304 char * strpbrk(const char * cs,const char * ct)
306 const char *sc1,*sc2;
308 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
309 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
318 #ifndef __HAVE_ARCH_STRTOK
320 * strtok - Split a string into tokens
321 * @s: The string to be searched
322 * @ct: The characters to search for
324 * WARNING: strtok is deprecated, use strsep instead.
326 char * strtok(char * s,const char * ct)
330 sbegin = s ? s : ___strtok;
334 sbegin += strspn(sbegin,ct);
335 if (*sbegin == '\0') {
339 send = strpbrk( sbegin, ct);
340 if (send && *send != '\0')
347 #ifndef __HAVE_ARCH_STRSEP
349 * strsep - Split a string into tokens
350 * @s: The string to be searched
351 * @ct: The characters to search for
353 * strsep() updates @s to point after the token, ready for the next call.
355 * It returns empty tokens, too, behaving exactly like the libc function
356 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
357 * Same semantics, slimmer shape. ;)
359 char * strsep(char **s, const char *ct)
361 char *sbegin = *s, *end;
366 end = strpbrk(sbegin, ct);
375 #ifndef __HAVE_ARCH_STRSWAB
377 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
378 * s: address of the string
380 * returns the address of the swapped string or NULL on error. If
381 * string length is odd, last byte is untouched.
383 char *strswab(const char *s)
387 if ((NULL == s) || ('\0' == *s)) {
391 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
403 #ifndef __HAVE_ARCH_MEMSET
405 * memset - Fill a region of memory with the given value
406 * @s: Pointer to the start of the area.
407 * @c: The byte to fill the area with
408 * @count: The size of the area.
410 * Do not use memset() to access IO space, use memset_io() instead.
412 void * memset(void * s,int c,size_t count)
414 unsigned long *sl = (unsigned long *) s;
415 unsigned long cl = 0;
419 /* do it one word at a time (32 bits or 64 bits) while possible */
420 if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
421 for (i = 0; i < sizeof(*sl); i++) {
425 while (count >= sizeof(*sl)) {
427 count -= sizeof(*sl);
430 /* fill 8 bits at a time */
439 #ifndef __HAVE_ARCH_BCOPY
441 * bcopy - Copy one area of memory to another
442 * @src: Where to copy from
443 * @dest: Where to copy to
444 * @count: The size of the area.
446 * Note that this is the same as memcpy(), with the arguments reversed.
447 * memcpy() is the standard, bcopy() is a legacy BSD function.
449 * You should not use this function to access IO space, use memcpy_toio()
450 * or memcpy_fromio() instead.
452 char * bcopy(const char * src, char * dest, int count)
463 #ifndef __HAVE_ARCH_MEMCPY
465 * memcpy - Copy one area of memory to another
466 * @dest: Where to copy to
467 * @src: Where to copy from
468 * @count: The size of the area.
470 * You should not use this function to access IO space, use memcpy_toio()
471 * or memcpy_fromio() instead.
473 void * memcpy(void *dest, const void *src, size_t count)
475 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
481 /* while all data is aligned (common case), copy a word at a time */
482 if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
483 while (count >= sizeof(*dl)) {
485 count -= sizeof(*dl);
488 /* copy the reset one byte at a time */
498 #ifndef __HAVE_ARCH_MEMMOVE
500 * memmove - Copy one area of memory to another
501 * @dest: Where to copy to
502 * @src: Where to copy from
503 * @count: The size of the area.
505 * Unlike memcpy(), memmove() copes with overlapping areas.
507 void * memmove(void * dest,const void *src,size_t count)
521 tmp = (char *) dest + count;
522 s = (char *) src + count;
531 #ifndef __HAVE_ARCH_MEMCMP
533 * memcmp - Compare two areas of memory
534 * @cs: One area of memory
535 * @ct: Another area of memory
536 * @count: The size of the area.
538 int memcmp(const void * cs,const void * ct,size_t count)
540 const unsigned char *su1, *su2;
543 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
544 if ((res = *su1 - *su2) != 0)
550 #ifndef __HAVE_ARCH_MEMSCAN
552 * memscan - Find a character in an area of memory.
553 * @addr: The memory area
554 * @c: The byte to search for
555 * @size: The size of the area.
557 * returns the address of the first occurrence of @c, or 1 byte past
558 * the area if @c is not found
560 void * memscan(void * addr, int c, size_t size)
562 unsigned char * p = (unsigned char *) addr;
574 #ifndef __HAVE_ARCH_STRSTR
576 * strstr - Find the first substring in a %NUL terminated string
577 * @s1: The string to be searched
578 * @s2: The string to search for
580 char * strstr(const char * s1,const char * s2)
590 if (!memcmp(s1,s2,l2))
598 #ifndef __HAVE_ARCH_MEMCHR
600 * memchr - Find a character in an area of memory.
601 * @s: The memory area
602 * @c: The byte to search for
603 * @n: The size of the area.
605 * returns the address of the first occurrence of @c, or %NULL
608 void *memchr(const void *s, int c, size_t n)
610 const unsigned char *p = s;
612 if ((unsigned char)c == *p++) {
613 return (void *)(p-1);