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 */
56 return (int)c1
- (int)c2
;
60 #ifndef __HAVE_ARCH_STRCPY
62 * strcpy - Copy a %NUL terminated string
63 * @dest: Where to copy the string to
64 * @src: Where to copy the string from
66 char * strcpy(char * dest
,const char *src
)
70 while ((*dest
++ = *src
++) != '\0')
76 #ifndef __HAVE_ARCH_STRNCPY
78 * strncpy - Copy a length-limited, %NUL-terminated string
79 * @dest: Where to copy the string to
80 * @src: Where to copy the string from
81 * @count: The maximum number of bytes to copy
83 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
84 * However, the result is not %NUL-terminated if the source exceeds
87 char * strncpy(char * dest
,const char *src
,size_t count
)
91 while (count
-- && (*dest
++ = *src
++) != '\0')
98 #ifndef __HAVE_ARCH_STRLCPY
100 * strlcpy - Copy a %NUL terminated string into a sized buffer
101 * @dest: Where to copy the string to
102 * @src: Where to copy the string from
103 * @size: size of destination buffer
105 * Compatible with *BSD: the result is always a valid
106 * NUL-terminated string that fits in the buffer (unless,
107 * of course, the buffer size is zero). It does not pad
108 * out the result like strncpy() does.
110 size_t strlcpy(char *dest
, const char *src
, size_t size
)
112 size_t ret
= strlen(src
);
115 size_t len
= (ret
>= size
) ? size
-1 : ret
;
116 memcpy(dest
, src
, len
);
121 EXPORT_SYMBOL(strlcpy
);
124 #ifndef __HAVE_ARCH_STRCAT
126 * strcat - Append one %NUL-terminated string to another
127 * @dest: The string to be appended to
128 * @src: The string to append to it
130 char * strcat(char * dest
, const char * src
)
136 while ((*dest
++ = *src
++) != '\0')
143 #ifndef __HAVE_ARCH_STRNCAT
145 * strncat - Append a length-limited, %NUL-terminated string to another
146 * @dest: The string to be appended to
147 * @src: The string to append to it
148 * @count: The maximum numbers of bytes to copy
150 * Note that in contrast to strncpy, strncat ensures the result is
153 char * strncat(char *dest
, const char *src
, size_t count
)
160 while ((*dest
++ = *src
++)) {
172 #ifndef __HAVE_ARCH_STRLCAT
174 * strlcat - Append a length-limited, %NUL-terminated string to another
175 * @dest: The string to be appended to
176 * @src: The string to append to it
177 * @count: The size of the destination buffer.
179 size_t strlcat(char *dest
, const char *src
, size_t count
)
181 size_t dsize
= strlen(dest
);
182 size_t len
= strlen(src
);
183 size_t res
= dsize
+ len
;
185 /* This would be a bug */
186 BUG_ON(dsize
>= count
);
192 memcpy(dest
, src
, len
);
196 EXPORT_SYMBOL(strlcat
);
199 #ifndef __HAVE_ARCH_STRCMP
201 * strcmp - Compare two strings
203 * @ct: Another string
205 int strcmp(const char * cs
,const char * ct
)
207 register signed char __res
;
210 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
218 #ifndef __HAVE_ARCH_STRNCMP
220 * strncmp - Compare two length-limited strings
222 * @ct: Another string
223 * @count: The maximum number of bytes to compare
225 int strncmp(const char * cs
,const char * ct
,size_t count
)
227 register signed char __res
= 0;
230 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
239 #ifndef __HAVE_ARCH_STRCHR
241 * strchr - Find the first occurrence of a character in a string
242 * @s: The string to be searched
243 * @c: The character to search for
245 char * strchr(const char * s
, int c
)
247 for(; *s
!= (char) c
; ++s
)
254 #ifndef __HAVE_ARCH_STRRCHR
256 * strrchr - Find the last occurrence of a character in a string
257 * @s: The string to be searched
258 * @c: The character to search for
260 char * strrchr(const char * s
, int c
)
262 const char *p
= s
+ strlen(s
);
271 #ifndef __HAVE_ARCH_STRLEN
273 * strlen - Find the length of a string
274 * @s: The string to be sized
276 size_t strlen(const char * s
)
280 for (sc
= s
; *sc
!= '\0'; ++sc
)
286 #ifndef __HAVE_ARCH_STRNLEN
288 * strnlen - Find the length of a length-limited string
289 * @s: The string to be sized
290 * @count: The maximum number of bytes to search
292 size_t strnlen(const char * s
, size_t count
)
296 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
302 #ifndef __HAVE_ARCH_STRSPN
304 * strspn - Calculate the length of the initial substring of @s which only
305 * contain letters in @accept
306 * @s: The string to be searched
307 * @accept: The string to search for
309 size_t strspn(const char *s
, const char *accept
)
315 for (p
= s
; *p
!= '\0'; ++p
) {
316 for (a
= accept
; *a
!= '\0'; ++a
) {
330 * strcspn - Calculate the length of the initial substring of @s which does
331 * not contain letters in @reject
332 * @s: The string to be searched
333 * @reject: The string to avoid
335 size_t strcspn(const char *s
, const char *reject
)
341 for (p
= s
; *p
!= '\0'; ++p
) {
342 for (r
= reject
; *r
!= '\0'; ++r
) {
352 #ifndef __HAVE_ARCH_STRPBRK
354 * strpbrk - Find the first occurrence of a set of characters
355 * @cs: The string to be searched
356 * @ct: The characters to search for
358 char * strpbrk(const char * cs
,const char * ct
)
360 const char *sc1
,*sc2
;
362 for( sc1
= cs
; *sc1
!= '\0'; ++sc1
) {
363 for( sc2
= ct
; *sc2
!= '\0'; ++sc2
) {
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_MEMSET
402 * memset - Fill a region of memory with the given value
403 * @s: Pointer to the start of the area.
404 * @c: The byte to fill the area with
405 * @count: The size of the area.
407 * Do not use memset() to access IO space, use memset_io() instead.
409 void * memset(void * s
,int c
, size_t count
)
411 char *xs
= (char *) s
;
420 #ifndef __HAVE_ARCH_BCOPY
422 * bcopy - Copy one area of memory to another
423 * @src: Where to copy from
424 * @dest: Where to copy to
425 * @count: The size of the area.
427 * Note that this is the same as memcpy(), with the arguments reversed.
428 * memcpy() is the standard, bcopy() is a legacy BSD function.
430 * You should not use this function to access IO space, use memcpy_toio()
431 * or memcpy_fromio() instead.
433 char * bcopy(const char * src
, char * dest
, int count
)
444 #ifndef __HAVE_ARCH_MEMCPY
446 * memcpy - Copy one area of memory to another
447 * @dest: Where to copy to
448 * @src: Where to copy from
449 * @count: The size of the area.
451 * You should not use this function to access IO space, use memcpy_toio()
452 * or memcpy_fromio() instead.
454 void * memcpy(void * dest
,const void *src
,size_t count
)
456 char *tmp
= (char *) dest
, *s
= (char *) src
;
465 #ifndef __HAVE_ARCH_MEMMOVE
467 * memmove - Copy one area of memory to another
468 * @dest: Where to copy to
469 * @src: Where to copy from
470 * @count: The size of the area.
472 * Unlike memcpy(), memmove() copes with overlapping areas.
474 void * memmove(void * dest
,const void *src
,size_t count
)
485 tmp
= (char *) dest
+ count
;
486 s
= (char *) src
+ count
;
495 #ifndef __HAVE_ARCH_MEMCMP
497 * memcmp - Compare two areas of memory
498 * @cs: One area of memory
499 * @ct: Another area of memory
500 * @count: The size of the area.
502 int memcmp(const void * cs
,const void * ct
,size_t count
)
504 const unsigned char *su1
, *su2
;
507 for( su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--)
508 if ((res
= *su1
- *su2
) != 0)
514 #ifndef __HAVE_ARCH_MEMSCAN
516 * memscan - Find a character in an area of memory.
517 * @addr: The memory area
518 * @c: The byte to search for
519 * @size: The size of the area.
521 * returns the address of the first occurrence of @c, or 1 byte past
522 * the area if @c is not found
524 void * memscan(void * addr
, int c
, size_t size
)
526 unsigned char * p
= (unsigned char *) addr
;
527 unsigned char * e
= p
+ size
;
530 if (*p
== (unsigned char)c
)
539 #ifndef __HAVE_ARCH_STRSTR
541 * strstr - Find the first substring in a %NUL terminated string
542 * @s1: The string to be searched
543 * @s2: The string to search for
545 char * strstr(const char * s1
,const char * s2
)
555 if (!memcmp(s1
,s2
,l2
))
563 #ifndef __HAVE_ARCH_MEMCHR
565 * memchr - Find a character in an area of memory.
566 * @s: The memory area
567 * @c: The byte to search for
568 * @n: The size of the area.
570 * returns the address of the first occurrence of @c, or %NULL
573 void *memchr(const void *s
, int c
, size_t n
)
575 const unsigned char *p
= s
;
577 if ((unsigned char)c
== *p
++) {
578 return (void *)(p
-1);