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 #undef __HAVE_ARCH_MEMCMP
25 #undef __HAVE_ARCH_MEMCPY
26 #undef __HAVE_ARCH_MEMMOVE
27 #undef __HAVE_ARCH_MEMSET
28 #undef __HAVE_ARCH_BCOPY
29 #undef __HAVE_ARCH_STRCAT
30 #undef __HAVE_ARCH_STRCHR
31 #undef __HAVE_ARCH_STRCMP
32 #undef __HAVE_ARCH_STRCPY
33 #undef __HAVE_ARCH_STRLEN
34 #undef __HAVE_ARCH_STRNCPY
36 #define __HAVE_ARCH_MEMCMP
37 #define __HAVE_ARCH_MEMCPY
38 #define __HAVE_ARCH_MEMMOVE
39 #define __HAVE_ARCH_MEMSET
40 #define __HAVE_ARCH_BCOPY
41 #define __HAVE_ARCH_STRCAT
42 #define __HAVE_ARCH_STRCMP
43 #define __HAVE_ARCH_STRCPY
44 #define __HAVE_ARCH_STRLEN
45 #define __HAVE_ARCH_STRNCPY
48 #ifndef __HAVE_ARCH_STRNICMP
50 * strnicmp - Case insensitive, length-limited string comparison
52 * @s2: The other string
53 * @len: the maximum number of characters to compare
55 int strnicmp(const char *s1, const char *s2, size_t len)
57 /* Yes, Virginia, it had better be unsigned */
77 return (int)c1 - (int)c2;
83 #ifndef __HAVE_ARCH_STRCPY
85 * strcpy - Copy a %NUL terminated string
86 * @dest: Where to copy the string to
87 * @src: Where to copy the string from
89 char * strcpy(char * dest,const char *src)
93 while ((*dest++ = *src++) != '\0')
99 #ifndef __HAVE_ARCH_STRNCPY
101 * strncpy - Copy a length-limited, %NUL-terminated string
102 * @dest: Where to copy the string to
103 * @src: Where to copy the string from
104 * @count: The maximum number of bytes to copy
106 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
107 * However, the result is not %NUL-terminated if the source exceeds
110 char * strncpy(char * dest,const char *src,size_t count)
114 while (count-- && (*dest++ = *src++) != '\0')
121 #ifndef __HAVE_ARCH_STRCAT
123 * strcat - Append one %NUL-terminated string to another
124 * @dest: The string to be appended to
125 * @src: The string to append to it
127 char * strcat(char * dest, const char * src)
133 while ((*dest++ = *src++) != '\0')
140 #ifndef __HAVE_ARCH_STRNCAT
142 * strncat - Append a length-limited, %NUL-terminated string to another
143 * @dest: The string to be appended to
144 * @src: The string to append to it
145 * @count: The maximum numbers of bytes to copy
147 * Note that in contrast to strncpy, strncat ensures the result is
150 char * strncat(char *dest, const char *src, size_t count)
157 while ((*dest++ = *src++)) {
169 #ifndef __HAVE_ARCH_STRCMP
171 * strcmp - Compare two strings
173 * @ct: Another string
175 int strcmp(const char * cs,const char * ct)
177 register signed char __res;
180 if ((__res = *cs - *ct++) != 0 || !*cs++)
188 #ifndef __HAVE_ARCH_STRNCMP
190 * strncmp - Compare two length-limited strings
192 * @ct: Another string
193 * @count: The maximum number of bytes to compare
195 int strncmp(const char * cs,const char * ct,size_t count)
197 register signed char __res = 0;
200 if ((__res = *cs - *ct++) != 0 || !*cs++)
209 #ifndef __HAVE_ARCH_STRCHR
211 * strchr - Find the first occurrence of a character in a string
212 * @s: The string to be searched
213 * @c: The character to search for
215 char * strchr(const char * s, int c)
217 for(; *s != (char) c; ++s)
224 #ifndef __HAVE_ARCH_STRRCHR
226 * strrchr - Find the last occurrence of a character in a string
227 * @s: The string to be searched
228 * @c: The character to search for
230 char * strrchr(const char * s, int c)
232 const char *p = s + strlen(s);
241 #ifndef __HAVE_ARCH_STRLEN
243 * strlen - Find the length of a string
244 * @s: The string to be sized
246 size_t strlen(const char * s)
250 for (sc = s; *sc != '\0'; ++sc)
256 #ifndef __HAVE_ARCH_STRNLEN
258 * strnlen - Find the length of a length-limited string
259 * @s: The string to be sized
260 * @count: The maximum number of bytes to search
262 size_t strnlen(const char * s, size_t count)
266 for (sc = s; count-- && *sc != '\0'; ++sc)
272 #ifndef __HAVE_ARCH_STRDUP
273 char * strdup(const char *s)
278 ((new = malloc (strlen(s) + 1)) == NULL) ) {
287 #ifndef __HAVE_ARCH_STRSPN
289 * strspn - Calculate the length of the initial substring of @s which only
290 * contain letters in @accept
291 * @s: The string to be searched
292 * @accept: The string to search for
294 size_t strspn(const char *s, const char *accept)
300 for (p = s; *p != '\0'; ++p) {
301 for (a = accept; *a != '\0'; ++a) {
314 #ifndef __HAVE_ARCH_STRPBRK
316 * strpbrk - Find the first occurrence of a set of characters
317 * @cs: The string to be searched
318 * @ct: The characters to search for
320 char * strpbrk(const char * cs,const char * ct)
322 const char *sc1,*sc2;
324 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
325 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
334 #ifndef __HAVE_ARCH_STRTOK
336 * strtok - Split a string into tokens
337 * @s: The string to be searched
338 * @ct: The characters to search for
340 * WARNING: strtok is deprecated, use strsep instead.
342 char * strtok(char * s,const char * ct)
346 sbegin = s ? s : ___strtok;
350 sbegin += strspn(sbegin,ct);
351 if (*sbegin == '\0') {
355 send = strpbrk( sbegin, ct);
356 if (send && *send != '\0')
363 #ifndef __HAVE_ARCH_STRSEP
365 * strsep - Split a string into tokens
366 * @s: The string to be searched
367 * @ct: The characters to search for
369 * strsep() updates @s to point after the token, ready for the next call.
371 * It returns empty tokens, too, behaving exactly like the libc function
372 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
373 * Same semantics, slimmer shape. ;)
375 char * strsep(char **s, const char *ct)
377 char *sbegin = *s, *end;
382 end = strpbrk(sbegin, ct);
391 #ifndef __HAVE_ARCH_MEMSET
393 * memset - Fill a region of memory with the given value
394 * @s: Pointer to the start of the area.
395 * @c: The byte to fill the area with
396 * @count: The size of the area.
398 * Do not use memset() to access IO space, use memset_io() instead.
400 void * memset(void * s,int c,size_t count)
402 char *xs = (char *) s;
411 #ifndef __HAVE_ARCH_BCOPY
413 * bcopy - Copy one area of memory to another
414 * @src: Where to copy from
415 * @dest: Where to copy to
416 * @count: The size of the area.
418 * Note that this is the same as memcpy(), with the arguments reversed.
419 * memcpy() is the standard, bcopy() is a legacy BSD function.
421 * You should not use this function to access IO space, use memcpy_toio()
422 * or memcpy_fromio() instead.
424 char * bcopy(const char * src, char * dest, int count)
435 #ifndef __HAVE_ARCH_MEMCPY
437 * memcpy - Copy one area of memory to another
438 * @dest: Where to copy to
439 * @src: Where to copy from
440 * @count: The size of the area.
442 * You should not use this function to access IO space, use memcpy_toio()
443 * or memcpy_fromio() instead.
445 void * memcpy(void * dest,const void *src,size_t count)
447 char *tmp = (char *) dest, *s = (char *) src;
456 #ifndef __HAVE_ARCH_MEMMOVE
458 * memmove - Copy one area of memory to another
459 * @dest: Where to copy to
460 * @src: Where to copy from
461 * @count: The size of the area.
463 * Unlike memcpy(), memmove() copes with overlapping areas.
465 void * memmove(void * dest,const void *src,size_t count)
476 tmp = (char *) dest + count;
477 s = (char *) src + count;
486 #ifndef __HAVE_ARCH_MEMCMP
488 * memcmp - Compare two areas of memory
489 * @cs: One area of memory
490 * @ct: Another area of memory
491 * @count: The size of the area.
493 int memcmp(const void * cs,const void * ct,size_t count)
495 const unsigned char *su1, *su2;
498 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
499 if ((res = *su1 - *su2) != 0)
505 #ifndef __HAVE_ARCH_MEMSCAN
507 * memscan - Find a character in an area of memory.
508 * @addr: The memory area
509 * @c: The byte to search for
510 * @size: The size of the area.
512 * returns the address of the first occurrence of @c, or 1 byte past
513 * the area if @c is not found
515 void * memscan(void * addr, int c, size_t size)
517 unsigned char * p = (unsigned char *) addr;
529 #ifndef __HAVE_ARCH_STRSTR
531 * strstr - Find the first substring in a %NUL terminated string
532 * @s1: The string to be searched
533 * @s2: The string to search for
535 char * strstr(const char * s1,const char * s2)
545 if (!memcmp(s1,s2,l2))
553 #ifndef __HAVE_ARCH_MEMCHR
555 * memchr - Find a character in an area of memory.
556 * @s: The memory area
557 * @c: The byte to search for
558 * @n: The size of the area.
560 * returns the address of the first occurrence of @c, or %NULL
563 void *memchr(const void *s, int c, size_t n)
565 const unsigned char *p = s;
567 if ((unsigned char)c == *p++) {
568 return (void *)(p-1);