2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2004 Tobias Lorenz
5 * string handling functions
6 * based on linux/lib/string.c
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 * stupid library routines.. The optimized versions should generally be found
15 * as inline code in <asm-xx/string.h>
17 * These are buggy as well..
19 * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
20 * - Added strsep() which will replace strtok() soon (because strsep() is
21 * reentrant and should be faster). Use only strsep() in new code, please.
29 /* *** FROM string.c *** */
31 #ifndef __HAVE_ARCH_STRNICMP
33 * strnicmp - Case insensitive, length-limited string comparison
35 * @s2: The other string
36 * @len: the maximum number of characters to compare
38 int strnicmp(const char *s1
, const char *s2
, size_t len
)
40 /* Yes, Virginia, it had better be unsigned */
60 return (int)c1
- (int)c2
;
66 #ifndef __HAVE_ARCH_STRCPY
68 * strcpy - Copy a %NUL terminated string
69 * @dest: Where to copy the string to
70 * @src: Where to copy the string from
72 char * strcpy(char * dest
,const char *src
)
76 while ((*dest
++ = *src
++) != '\0')
82 #ifndef __HAVE_ARCH_STRNCPY
84 * strncpy - Copy a length-limited, %NUL-terminated string
85 * @dest: Where to copy the string to
86 * @src: Where to copy the string from
87 * @count: The maximum number of bytes to copy
89 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
90 * However, the result is not %NUL-terminated if the source exceeds
93 char * strncpy(char * dest
,const char *src
,size_t count
)
97 while (count
-- && (*dest
++ = *src
++) != '\0')
104 #ifndef __HAVE_ARCH_STRCAT
106 * strcat - Append one %NUL-terminated string to another
107 * @dest: The string to be appended to
108 * @src: The string to append to it
110 char * strcat(char * dest
, const char * src
)
116 while ((*dest
++ = *src
++) != '\0')
123 #ifndef __HAVE_ARCH_STRNCAT
125 * strncat - Append a length-limited, %NUL-terminated string to another
126 * @dest: The string to be appended to
127 * @src: The string to append to it
128 * @count: The maximum numbers of bytes to copy
130 * Note that in contrast to strncpy, strncat ensures the result is
133 char * strncat(char *dest
, const char *src
, size_t count
)
140 while ((*dest
++ = *src
++)) {
152 #ifndef __HAVE_ARCH_STRCMP
154 * strcmp - Compare two strings
156 * @ct: Another string
158 int strcmp(const char * cs
,const char * ct
)
160 register signed char __res
;
163 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
171 #ifndef __HAVE_ARCH_STRNCMP
173 * strncmp - Compare two length-limited strings
175 * @ct: Another string
176 * @count: The maximum number of bytes to compare
178 int strncmp(const char * cs
,const char * ct
,size_t count
)
180 register signed char __res
= 0;
183 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
192 #ifndef __HAVE_ARCH_STRCHR
194 * strchr - Find the first occurrence of a character in a string
195 * @s: The string to be searched
196 * @c: The character to search for
198 char * strchr(const char * s
, int c
)
200 for(; *s
!= (char) c
; ++s
)
207 #ifndef __HAVE_ARCH_STRRCHR
209 * strrchr - Find the last occurrence of a character in a string
210 * @s: The string to be searched
211 * @c: The character to search for
213 char * strrchr(const char * s
, int c
)
215 const char *p
= s
+ strlen(s
);
224 #ifndef __HAVE_ARCH_STRLEN
226 * strlen - Find the length of a string
227 * @s: The string to be sized
229 size_t strlen(const char * s
)
233 for (sc
= s
; *sc
!= '\0'; ++sc
)
239 #ifndef __HAVE_ARCH_STRNLEN
241 * strnlen - Find the length of a length-limited string
242 * @s: The string to be sized
243 * @count: The maximum number of bytes to search
245 size_t strnlen(const char * s
, size_t count
)
249 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
255 #ifndef __HAVE_ARCH_STRSPN
257 * strspn - Calculate the length of the initial substring of @s which only
258 * contain letters in @accept
259 * @s: The string to be searched
260 * @accept: The string to search for
262 size_t strspn(const char *s
, const char *accept
)
268 for (p
= s
; *p
!= '\0'; ++p
) {
269 for (a
= accept
; *a
!= '\0'; ++a
) {
282 #ifndef __HAVE_ARCH_STRPBRK
284 * strpbrk - Find the first occurrence of a set of characters
285 * @cs: The string to be searched
286 * @ct: The characters to search for
288 char * strpbrk(const char * cs
,const char * ct
)
290 const char *sc1
,*sc2
;
292 for( sc1
= cs
; *sc1
!= '\0'; ++sc1
) {
293 for( sc2
= ct
; *sc2
!= '\0'; ++sc2
) {
302 #ifndef __HAVE_ARCH_STRTOK
304 * strtok - Split a string into tokens
305 * @s: The string to be searched
306 * @ct: The characters to search for
308 * WARNING: strtok is deprecated, use strsep instead.
310 char * strtok(char * s
,const char * ct
)
314 sbegin
= s
? s
: ___strtok
;
318 sbegin
+= strspn(sbegin
,ct
);
319 if (*sbegin
== '\0') {
323 send
= strpbrk( sbegin
, ct
);
324 if (send
&& *send
!= '\0')
331 #ifndef __HAVE_ARCH_STRSEP
333 * strsep - Split a string into tokens
334 * @s: The string to be searched
335 * @ct: The characters to search for
337 * strsep() updates @s to point after the token, ready for the next call.
339 * It returns empty tokens, too, behaving exactly like the libc function
340 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
341 * Same semantics, slimmer shape. ;)
343 char * strsep(char **s
, const char *ct
)
345 char *sbegin
= *s
, *end
;
350 end
= strpbrk(sbegin
, ct
);
359 #ifndef __HAVE_ARCH_MEMSET
361 * memset - Fill a region of memory with the given value
362 * @s: Pointer to the start of the area.
363 * @c: The byte to fill the area with
364 * @count: The size of the area.
366 * Do not use memset() to access IO space, use memset_io() instead.
368 void * memset(void * s
,int c
,size_t count
)
370 char *xs
= (char *) s
;
379 #ifndef __HAVE_ARCH_BCOPY
381 * bcopy - Copy one area of memory to another
382 * @src: Where to copy from
383 * @dest: Where to copy to
384 * @count: The size of the area.
386 * Note that this is the same as memcpy(), with the arguments reversed.
387 * memcpy() is the standard, bcopy() is a legacy BSD function.
389 * You should not use this function to access IO space, use memcpy_toio()
390 * or memcpy_fromio() instead.
392 char * bcopy(const char * src
, char * dest
, int count
)
403 #ifndef __HAVE_ARCH_MEMCPY
405 * memcpy - Copy one area of memory to another
406 * @dest: Where to copy to
407 * @src: Where to copy from
408 * @count: The size of the area.
410 * You should not use this function to access IO space, use memcpy_toio()
411 * or memcpy_fromio() instead.
413 void * memcpy(void * dest
,const void *src
,size_t count
)
415 char *tmp
= (char *) dest
, *s
= (char *) src
;
424 #ifndef __HAVE_ARCH_MEMMOVE
426 * memmove - Copy one area of memory to another
427 * @dest: Where to copy to
428 * @src: Where to copy from
429 * @count: The size of the area.
431 * Unlike memcpy(), memmove() copes with overlapping areas.
433 void * memmove(void * dest
,const void *src
,size_t count
)
444 tmp
= (char *) dest
+ count
;
445 s
= (char *) src
+ count
;
454 #ifndef __HAVE_ARCH_MEMCMP
456 * memcmp - Compare two areas of memory
457 * @cs: One area of memory
458 * @ct: Another area of memory
459 * @count: The size of the area.
461 int memcmp(const void * cs
,const void * ct
,size_t count
)
463 const unsigned char *su1
, *su2
;
466 for( su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--)
467 if ((res
= *su1
- *su2
) != 0)
473 #ifndef __HAVE_ARCH_MEMSCAN
475 * memscan - Find a character in an area of memory.
476 * @addr: The memory area
477 * @c: The byte to search for
478 * @size: The size of the area.
480 * returns the address of the first occurrence of @c, or 1 byte past
481 * the area if @c is not found
483 void * memscan(void * addr
, int c
, size_t size
)
485 unsigned char * p
= (unsigned char *) addr
;
497 #ifndef __HAVE_ARCH_STRSTR
499 * strstr - Find the first substring in a %NUL terminated string
500 * @s1: The string to be searched
501 * @s2: The string to search for
503 char * strstr(const char * s1
,const char * s2
)
513 if (!memcmp(s1
,s2
,l2
))
521 #ifndef __HAVE_ARCH_MEMCHR
523 * memchr - Find a character in an area of memory.
524 * @s: The memory area
525 * @c: The byte to search for
526 * @n: The size of the area.
528 * returns the address of the first occurrence of @c, or %NULL
531 void * memchr(const void *s
, int c
, size_t n
)
533 const unsigned char *p
= s
;
535 if ((unsigned char)c
== *p
++) {
536 return (void *)(p
-1);
544 char * strdup(const char *s
) {
545 char *new = malloc(strlen(s
)+1);