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 #ifndef __HAVE_ARCH_STRCPY
27 * strcpy - Copy a %NUL terminated string
28 * @dest: Where to copy the string to
29 * @src: Where to copy the string from
31 char * strcpy(char * dest
,const char *src
)
35 while ((*dest
++ = *src
++) != '\0')
40 EXPORT_SYMBOL(strcpy
);
42 #ifndef __HAVE_ARCH_STRNCPY
44 * strncpy - Copy a length-limited, %NUL-terminated string
45 * @dest: Where to copy the string to
46 * @src: Where to copy the string from
47 * @count: The maximum number of bytes to copy
49 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
50 * However, the result is not %NUL-terminated if the source exceeds
53 char * strncpy(char * dest
,const char *src
,size_t count
)
57 while (count
-- && (*dest
++ = *src
++) != '\0')
63 EXPORT_SYMBOL(strncpy
);
65 #ifndef __HAVE_ARCH_STRLCPY
67 * strlcpy - Copy a %NUL terminated string into a sized buffer
68 * @dest: Where to copy the string to
69 * @src: Where to copy the string from
70 * @size: size of destination buffer
72 * Compatible with *BSD: the result is always a valid
73 * NUL-terminated string that fits in the buffer (unless,
74 * of course, the buffer size is zero). It does not pad
75 * out the result like strncpy() does.
77 size_t strlcpy(char *dest
, const char *src
, size_t size
)
79 size_t ret
= strlen(src
);
82 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
83 memcpy(dest
, src
, len
);
88 EXPORT_SYMBOL(strlcpy
);
91 #ifndef __HAVE_ARCH_STRCAT
93 * strcat - Append one %NUL-terminated string to another
94 * @dest: The string to be appended to
95 * @src: The string to append to it
97 char * strcat(char * dest
, const char * src
)
103 while ((*dest
++ = *src
++) != '\0')
109 EXPORT_SYMBOL(strcat
);
111 #ifndef __HAVE_ARCH_STRNCAT
113 * strncat - Append a length-limited, %NUL-terminated string to another
114 * @dest: The string to be appended to
115 * @src: The string to append to it
116 * @count: The maximum numbers of bytes to copy
118 * Note that in contrast to strncpy, strncat ensures the result is
121 char * strncat(char *dest
, const char *src
, size_t count
)
128 while ((*dest
++ = *src
++)) {
139 EXPORT_SYMBOL(strncat
);
141 #ifndef __HAVE_ARCH_STRCMP
143 * strcmp - Compare two strings
145 * @ct: Another string
147 int strcmp(const char * cs
,const char * ct
)
149 register signed char __res
;
152 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
159 EXPORT_SYMBOL(strcmp
);
161 #ifndef __HAVE_ARCH_STRNCMP
163 * strncmp - Compare two length-limited strings
165 * @ct: Another string
166 * @count: The maximum number of bytes to compare
168 int strncmp(const char * cs
, const char * ct
, size_t count
)
170 register signed char __res
= 0;
173 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
181 EXPORT_SYMBOL(strncmp
);
183 #ifndef __HAVE_ARCH_STRCHR
185 * strchr - Find the first occurrence of a character in a string
186 * @s: The string to be searched
187 * @c: The character to search for
189 char * _strchr(const char * s
, int c
)
191 for(; *s
!= (char) c
; ++s
)
197 EXPORT_SYMBOL(_strchr
);
199 #ifndef __HAVE_ARCH_STRRCHR
201 * strrchr - Find the last occurrence of a character in a string
202 * @s: The string to be searched
203 * @c: The character to search for
205 char * _strrchr(const char * s
, int c
)
207 const char *p
= s
+ strlen(s
);
215 EXPORT_SYMBOL(_strrchr
);
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
)
231 EXPORT_SYMBOL(strlen
);
233 #ifndef __HAVE_ARCH_STRNLEN
235 * strnlen - Find the length of a length-limited string
236 * @s: The string to be sized
237 * @count: The maximum number of bytes to search
239 size_t strnlen(const char * s
, size_t count
)
243 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
248 EXPORT_SYMBOL(strnlen
);
250 #ifndef __HAVE_ARCH_STRDUP
251 char * strdup(const char *s
)
256 ((new = malloc (strlen(s
) + 1)) == NULL
) ) {
264 EXPORT_SYMBOL(strdup
);
266 #ifndef __HAVE_ARCH_STRSPN
268 * strspn - Calculate the length of the initial substring of @s which only
269 * contain letters in @accept
270 * @s: The string to be searched
271 * @accept: The string to search for
273 size_t strspn(const char *s
, const char *accept
)
279 for (p
= s
; *p
!= '\0'; ++p
) {
280 for (a
= accept
; *a
!= '\0'; ++a
) {
292 EXPORT_SYMBOL(strspn
);
294 #ifndef __HAVE_ARCH_STRPBRK
296 * strpbrk - Find the first occurrence of a set of characters
297 * @cs: The string to be searched
298 * @ct: The characters to search for
300 char * strpbrk(const char * cs
,const char * ct
)
302 const char *sc1
, *sc2
;
304 for( sc1
= cs
; *sc1
!= '\0'; ++sc1
) {
305 for( sc2
= ct
; *sc2
!= '\0'; ++sc2
) {
313 EXPORT_SYMBOL(strpbrk
);
315 #ifndef __HAVE_ARCH_STRTOK
317 * strtok - Split a string into tokens
318 * @s: The string to be searched
319 * @ct: The characters to search for
321 * WARNING: strtok is deprecated, use strsep instead.
323 char * strtok(char * s
, const char * ct
)
327 sbegin
= s
? s
: ___strtok
;
331 sbegin
+= strspn(sbegin
,ct
);
332 if (*sbegin
== '\0') {
336 send
= strpbrk( sbegin
, ct
);
337 if (send
&& *send
!= '\0')
343 EXPORT_SYMBOL(strtok
);
345 #ifndef __HAVE_ARCH_STRSEP
347 * strsep - Split a string into tokens
348 * @s: The string to be searched
349 * @ct: The characters to search for
351 * strsep() updates @s to point after the token, ready for the next call.
353 * It returns empty tokens, too, behaving exactly like the libc function
354 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
355 * Same semantics, slimmer shape. ;)
357 char * strsep(char **s
, const char *ct
)
359 char *sbegin
= *s
, *end
;
364 end
= strpbrk(sbegin
, ct
);
372 EXPORT_SYMBOL(strsep
);
374 #ifndef __HAVE_ARCH_STRSWAB
376 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
377 * s: address of the string
379 * returns the address of the swapped string or NULL on error. If
380 * string length is odd, last byte is untouched.
382 char *strswab(const char *s
)
386 if ((NULL
== s
) || ('\0' == *s
)) {
390 for (p
=(char *)s
, q
=p
+1; (*p
!= '\0') && (*q
!= '\0'); p
+=2, q
+=2) {
402 #ifndef __HAVE_ARCH_MEMSET
404 * memset - Fill a region of memory with the given value
405 * @s: Pointer to the start of the area.
406 * @c: The byte to fill the area with
407 * @count: The size of the area.
409 * Do not use memset() to access IO space, use memset_io() instead.
411 void * memset(void * s
,int c
,size_t count
)
413 char *xs
= (char *) s
;
421 EXPORT_SYMBOL(memset
);
423 #ifndef __HAVE_ARCH_BCOPY
425 * bcopy - Copy one area of memory to another
426 * @src: Where to copy from
427 * @dest: Where to copy to
428 * @count: The size of the area.
430 * Note that this is the same as memcpy(), with the arguments reversed.
431 * memcpy() is the standard, bcopy() is a legacy BSD function.
433 * You should not use this function to access IO space, use memcpy_toio()
434 * or memcpy_fromio() instead.
436 char * bcopy(const char * src
, char * dest
, int count
)
447 #ifndef __HAVE_ARCH_MEMCPY
449 * memcpy - Copy one area of memory to another
450 * @dest: Where to copy to
451 * @src: Where to copy from
452 * @count: The size of the area.
454 * You should not use this function to access IO space, use memcpy_toio()
455 * or memcpy_fromio() instead.
457 void * memcpy(void * dest
,const void *src
,size_t count
)
459 char *tmp
= (char *) dest
, *s
= (char *) src
;
467 EXPORT_SYMBOL(memcpy
);
469 #ifndef __HAVE_ARCH_MEMMOVE
471 * memmove - Copy one area of memory to another
472 * @dest: Where to copy to
473 * @src: Where to copy from
474 * @count: The size of the area.
476 * Unlike memcpy(), memmove() copes with overlapping areas.
478 void * memmove(void * dest
,const void *src
,size_t count
)
489 tmp
= (char *) dest
+ count
;
490 s
= (char *) src
+ count
;
498 EXPORT_SYMBOL(memmove
);
500 #ifndef __HAVE_ARCH_MEMCMP
502 * memcmp - Compare two areas of memory
503 * @cs: One area of memory
504 * @ct: Another area of memory
505 * @count: The size of the area.
507 int memcmp(const void * cs
,const void * ct
,size_t count
)
509 const unsigned char *su1
, *su2
;
512 for( su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--)
513 if ((res
= *su1
- *su2
) != 0)
518 EXPORT_SYMBOL(memcmp
);
520 #ifndef __HAVE_ARCH_MEMSCAN
522 * memscan - Find a character in an area of memory.
523 * @addr: The memory area
524 * @c: The byte to search for
525 * @size: The size of the area.
527 * returns the address of the first occurrence of @c, or 1 byte past
528 * the area if @c is not found
530 void * memscan(void * addr
, int c
, size_t size
)
532 unsigned char * p
= (unsigned char *) addr
;
543 EXPORT_SYMBOL(memscan
);
545 #ifndef __HAVE_ARCH_STRSTR
547 * strstr - Find the first substring in a %NUL terminated string
548 * @s1: The string to be searched
549 * @s2: The string to search for
551 char * _strstr(const char * s1
,const char * s2
)
561 if (!memcmp(s1
,s2
,l2
))
568 EXPORT_SYMBOL(_strstr
);
570 #ifndef __HAVE_ARCH_MEMCHR
572 * memchr - Find a character in an area of memory.
573 * @s: The memory area
574 * @c: The byte to search for
575 * @n: The size of the area.
577 * returns the address of the first occurrence of @c, or %NULL
580 void *memchr(const void *s
, int c
, size_t n
)
582 const unsigned char *p
= s
;
584 if ((unsigned char)c
== *p
++) {
585 return (void *)(p
-1);
592 EXPORT_SYMBOL(memchr
);