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>
24 #if 0 /* not used - was: #ifndef __HAVE_ARCH_STRNICMP */
26 * strnicmp - Case insensitive, length-limited string comparison
28 * @s2: The other string
29 * @len: the maximum number of characters to compare
31 int strnicmp(const char *s1, const char *s2, size_t len)
33 /* Yes, Virginia, it had better be unsigned */
53 return (int)c1 - (int)c2;
59 #ifndef __HAVE_ARCH_STRCPY
61 * strcpy - Copy a %NUL terminated string
62 * @dest: Where to copy the string to
63 * @src: Where to copy the string from
65 char * strcpy(char * dest,const char *src)
69 while ((*dest++ = *src++) != '\0')
75 #ifndef __HAVE_ARCH_STRNCPY
77 * strncpy - Copy a length-limited, %NUL-terminated string
78 * @dest: Where to copy the string to
79 * @src: Where to copy the string from
80 * @count: The maximum number of bytes to copy
82 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
83 * However, the result is not %NUL-terminated if the source exceeds
86 char * strncpy(char * dest,const char *src,size_t count)
90 while (count-- && (*dest++ = *src++) != '\0')
97 #ifndef __HAVE_ARCH_STRCAT
99 * strcat - Append one %NUL-terminated string to another
100 * @dest: The string to be appended to
101 * @src: The string to append to it
103 char * strcat(char * dest, const char * src)
109 while ((*dest++ = *src++) != '\0')
116 #ifndef __HAVE_ARCH_STRNCAT
118 * strncat - Append a length-limited, %NUL-terminated string to another
119 * @dest: The string to be appended to
120 * @src: The string to append to it
121 * @count: The maximum numbers of bytes to copy
123 * Note that in contrast to strncpy, strncat ensures the result is
126 char * strncat(char *dest, const char *src, size_t count)
133 while ((*dest++ = *src++)) {
145 #ifndef __HAVE_ARCH_STRCMP
147 * strcmp - Compare two strings
149 * @ct: Another string
151 int strcmp(const char * cs,const char * ct)
153 register signed char __res;
156 if ((__res = *cs - *ct++) != 0 || !*cs++)
164 #ifndef __HAVE_ARCH_STRNCMP
166 * strncmp - Compare two length-limited strings
168 * @ct: Another string
169 * @count: The maximum number of bytes to compare
171 int strncmp(const char * cs,const char * ct,size_t count)
173 register signed char __res = 0;
176 if ((__res = *cs - *ct++) != 0 || !*cs++)
185 #ifndef __HAVE_ARCH_STRCHR
187 * strchr - Find the first occurrence of a character in a string
188 * @s: The string to be searched
189 * @c: The character to search for
191 char * strchr(const char * s, int c)
193 for(; *s != (char) c; ++s)
200 #ifndef __HAVE_ARCH_STRRCHR
202 * strrchr - Find the last occurrence of a character in a string
203 * @s: The string to be searched
204 * @c: The character to search for
206 char * strrchr(const char * s, int c)
208 const char *p = s + strlen(s);
217 #ifndef __HAVE_ARCH_STRLEN
219 * strlen - Find the length of a string
220 * @s: The string to be sized
222 size_t strlen(const char * s)
226 for (sc = s; *sc != '\0'; ++sc)
232 #ifndef __HAVE_ARCH_STRNLEN
234 * strnlen - Find the length of a length-limited string
235 * @s: The string to be sized
236 * @count: The maximum number of bytes to search
238 size_t strnlen(const char * s, size_t count)
242 for (sc = s; count-- && *sc != '\0'; ++sc)
248 #ifndef __HAVE_ARCH_STRDUP
249 char * strdup(const char *s)
254 ((new = malloc (strlen(s) + 1)) == NULL) ) {
263 #ifndef __HAVE_ARCH_STRSPN
265 * strspn - Calculate the length of the initial substring of @s which only
266 * contain letters in @accept
267 * @s: The string to be searched
268 * @accept: The string to search for
270 size_t strspn(const char *s, const char *accept)
276 for (p = s; *p != '\0'; ++p) {
277 for (a = accept; *a != '\0'; ++a) {
290 #ifndef __HAVE_ARCH_STRPBRK
292 * strpbrk - Find the first occurrence of a set of characters
293 * @cs: The string to be searched
294 * @ct: The characters to search for
296 char * strpbrk(const char * cs,const char * ct)
298 const char *sc1,*sc2;
300 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
301 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
310 #ifndef __HAVE_ARCH_STRTOK
312 * strtok - Split a string into tokens
313 * @s: The string to be searched
314 * @ct: The characters to search for
316 * WARNING: strtok is deprecated, use strsep instead.
318 char * strtok(char * s,const char * ct)
322 sbegin = s ? s : ___strtok;
326 sbegin += strspn(sbegin,ct);
327 if (*sbegin == '\0') {
331 send = strpbrk( sbegin, ct);
332 if (send && *send != '\0')
339 #ifndef __HAVE_ARCH_STRSEP
341 * strsep - Split a string into tokens
342 * @s: The string to be searched
343 * @ct: The characters to search for
345 * strsep() updates @s to point after the token, ready for the next call.
347 * It returns empty tokens, too, behaving exactly like the libc function
348 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
349 * Same semantics, slimmer shape. ;)
351 char * strsep(char **s, const char *ct)
353 char *sbegin = *s, *end;
358 end = strpbrk(sbegin, ct);
367 #ifndef __HAVE_ARCH_STRSWAB
369 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
370 * s: address of the string
372 * returns the address of the swapped string or NULL on error. If
373 * string length is odd, last byte is untouched.
375 char *strswab(const char *s)
379 if ((NULL == s) || ('\0' == *s)) {
383 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
395 #ifndef __HAVE_ARCH_MEMSET
397 * memset - Fill a region of memory with the given value
398 * @s: Pointer to the start of the area.
399 * @c: The byte to fill the area with
400 * @count: The size of the area.
402 * Do not use memset() to access IO space, use memset_io() instead.
404 void * memset(void * s,int c,size_t count)
406 char *xs = (char *) s;
415 #ifndef __HAVE_ARCH_BCOPY
417 * bcopy - Copy one area of memory to another
418 * @src: Where to copy from
419 * @dest: Where to copy to
420 * @count: The size of the area.
422 * Note that this is the same as memcpy(), with the arguments reversed.
423 * memcpy() is the standard, bcopy() is a legacy BSD function.
425 * You should not use this function to access IO space, use memcpy_toio()
426 * or memcpy_fromio() instead.
428 char * bcopy(const char * src, char * dest, int count)
439 #ifndef __HAVE_ARCH_MEMCPY
441 * memcpy - Copy one area of memory to another
442 * @dest: Where to copy to
443 * @src: Where to copy from
444 * @count: The size of the area.
446 * You should not use this function to access IO space, use memcpy_toio()
447 * or memcpy_fromio() instead.
449 void * memcpy(void * dest,const void *src,size_t count)
451 char *tmp = (char *) dest, *s = (char *) src;
460 #ifndef __HAVE_ARCH_MEMMOVE
462 * memmove - Copy one area of memory to another
463 * @dest: Where to copy to
464 * @src: Where to copy from
465 * @count: The size of the area.
467 * Unlike memcpy(), memmove() copes with overlapping areas.
469 void * memmove(void * dest,const void *src,size_t count)
480 tmp = (char *) dest + count;
481 s = (char *) src + count;
490 #ifndef __HAVE_ARCH_MEMCMP
492 * memcmp - Compare two areas of memory
493 * @cs: One area of memory
494 * @ct: Another area of memory
495 * @count: The size of the area.
497 int memcmp(const void * cs,const void * ct,size_t count)
499 const unsigned char *su1, *su2;
502 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
503 if ((res = *su1 - *su2) != 0)
509 #ifndef __HAVE_ARCH_MEMSCAN
511 * memscan - Find a character in an area of memory.
512 * @addr: The memory area
513 * @c: The byte to search for
514 * @size: The size of the area.
516 * returns the address of the first occurrence of @c, or 1 byte past
517 * the area if @c is not found
519 void * memscan(void * addr, int c, size_t size)
521 unsigned char * p = (unsigned char *) addr;
533 #ifndef __HAVE_ARCH_STRSTR
535 * strstr - Find the first substring in a %NUL terminated string
536 * @s1: The string to be searched
537 * @s2: The string to search for
539 char * strstr(const char * s1,const char * s2)
549 if (!memcmp(s1,s2,l2))
557 #ifndef __HAVE_ARCH_MEMCHR
559 * memchr - Find a character in an area of memory.
560 * @s: The memory area
561 * @c: The byte to search for
562 * @n: The size of the area.
564 * returns the address of the first occurrence of @c, or %NULL
567 void *memchr(const void *s, int c, size_t n)
569 const unsigned char *p = s;
571 if ((unsigned char)c == *p++) {
572 return (void *)(p-1);