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_STRCAT
67 * strcat - Append one %NUL-terminated string to another
68 * @dest: The string to be appended to
69 * @src: The string to append to it
71 char * strcat(char * dest
, const char * src
)
77 while ((*dest
++ = *src
++) != '\0')
83 EXPORT_SYMBOL(strcat
);
85 #ifndef __HAVE_ARCH_STRNCAT
87 * strncat - Append a length-limited, %NUL-terminated string to another
88 * @dest: The string to be appended to
89 * @src: The string to append to it
90 * @count: The maximum numbers of bytes to copy
92 * Note that in contrast to strncpy, strncat ensures the result is
95 char * strncat(char *dest
, const char *src
, size_t count
)
102 while ((*dest
++ = *src
++)) {
113 EXPORT_SYMBOL(strncat
);
115 #ifndef __HAVE_ARCH_STRCMP
117 * strcmp - Compare two strings
119 * @ct: Another string
121 int strcmp(const char * cs
,const char * ct
)
123 register signed char __res
;
126 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
133 EXPORT_SYMBOL(strcmp
);
135 #ifndef __HAVE_ARCH_STRNCMP
137 * strncmp - Compare two length-limited strings
139 * @ct: Another string
140 * @count: The maximum number of bytes to compare
142 int strncmp(const char * cs
, const char * ct
, size_t count
)
144 register signed char __res
= 0;
147 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
155 EXPORT_SYMBOL(strncmp
);
157 #ifndef __HAVE_ARCH_STRCHR
159 * strchr - Find the first occurrence of a character in a string
160 * @s: The string to be searched
161 * @c: The character to search for
163 char * _strchr(const char * s
, int c
)
165 for(; *s
!= (char) c
; ++s
)
171 EXPORT_SYMBOL(_strchr
);
173 #ifndef __HAVE_ARCH_STRRCHR
175 * strrchr - Find the last occurrence of a character in a string
176 * @s: The string to be searched
177 * @c: The character to search for
179 char * _strrchr(const char * s
, int c
)
181 const char *p
= s
+ strlen(s
);
189 EXPORT_SYMBOL(strrchr
);
191 #ifndef __HAVE_ARCH_STRLEN
193 * strlen - Find the length of a string
194 * @s: The string to be sized
196 size_t strlen(const char * s
)
200 for (sc
= s
; *sc
!= '\0'; ++sc
)
205 EXPORT_SYMBOL(strlen
);
207 #ifndef __HAVE_ARCH_STRNLEN
209 * strnlen - Find the length of a length-limited string
210 * @s: The string to be sized
211 * @count: The maximum number of bytes to search
213 size_t strnlen(const char * s
, size_t count
)
217 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
222 EXPORT_SYMBOL(strnlen
);
224 #ifndef __HAVE_ARCH_STRDUP
225 char * strdup(const char *s
)
230 ((new = malloc (strlen(s
) + 1)) == NULL
) ) {
238 EXPORT_SYMBOL(strdup
);
240 #ifndef __HAVE_ARCH_STRSPN
242 * strspn - Calculate the length of the initial substring of @s which only
243 * contain letters in @accept
244 * @s: The string to be searched
245 * @accept: The string to search for
247 size_t strspn(const char *s
, const char *accept
)
253 for (p
= s
; *p
!= '\0'; ++p
) {
254 for (a
= accept
; *a
!= '\0'; ++a
) {
266 EXPORT_SYMBOL(strspn
);
268 #ifndef __HAVE_ARCH_STRPBRK
270 * strpbrk - Find the first occurrence of a set of characters
271 * @cs: The string to be searched
272 * @ct: The characters to search for
274 char * strpbrk(const char * cs
,const char * ct
)
276 const char *sc1
, *sc2
;
278 for( sc1
= cs
; *sc1
!= '\0'; ++sc1
) {
279 for( sc2
= ct
; *sc2
!= '\0'; ++sc2
) {
287 EXPORT_SYMBOL(strpbrk
);
289 #ifndef __HAVE_ARCH_STRTOK
291 * strtok - Split a string into tokens
292 * @s: The string to be searched
293 * @ct: The characters to search for
295 * WARNING: strtok is deprecated, use strsep instead.
297 char * strtok(char * s
, const char * ct
)
301 sbegin
= s
? s
: ___strtok
;
305 sbegin
+= strspn(sbegin
,ct
);
306 if (*sbegin
== '\0') {
310 send
= strpbrk( sbegin
, ct
);
311 if (send
&& *send
!= '\0')
317 EXPORT_SYMBOL(strtok
);
319 #ifndef __HAVE_ARCH_STRSEP
321 * strsep - Split a string into tokens
322 * @s: The string to be searched
323 * @ct: The characters to search for
325 * strsep() updates @s to point after the token, ready for the next call.
327 * It returns empty tokens, too, behaving exactly like the libc function
328 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
329 * Same semantics, slimmer shape. ;)
331 char * strsep(char **s
, const char *ct
)
333 char *sbegin
= *s
, *end
;
338 end
= strpbrk(sbegin
, ct
);
346 EXPORT_SYMBOL(strsep
);
348 #ifndef __HAVE_ARCH_STRSWAB
350 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
351 * s: address of the string
353 * returns the address of the swapped string or NULL on error. If
354 * string length is odd, last byte is untouched.
356 char *strswab(const char *s
)
360 if ((NULL
== s
) || ('\0' == *s
)) {
364 for (p
=(char *)s
, q
=p
+1; (*p
!= '\0') && (*q
!= '\0'); p
+=2, q
+=2) {
376 #ifndef __HAVE_ARCH_MEMSET
378 * memset - Fill a region of memory with the given value
379 * @s: Pointer to the start of the area.
380 * @c: The byte to fill the area with
381 * @count: The size of the area.
383 * Do not use memset() to access IO space, use memset_io() instead.
385 void * memset(void * s
,int c
,size_t count
)
387 char *xs
= (char *) s
;
395 EXPORT_SYMBOL(memset
);
397 #ifndef __HAVE_ARCH_BCOPY
399 * bcopy - Copy one area of memory to another
400 * @src: Where to copy from
401 * @dest: Where to copy to
402 * @count: The size of the area.
404 * Note that this is the same as memcpy(), with the arguments reversed.
405 * memcpy() is the standard, bcopy() is a legacy BSD function.
407 * You should not use this function to access IO space, use memcpy_toio()
408 * or memcpy_fromio() instead.
410 char * bcopy(const char * src
, char * dest
, int count
)
421 #ifndef __HAVE_ARCH_MEMCPY
423 * memcpy - Copy one area of memory to another
424 * @dest: Where to copy to
425 * @src: Where to copy from
426 * @count: The size of the area.
428 * You should not use this function to access IO space, use memcpy_toio()
429 * or memcpy_fromio() instead.
431 void * memcpy(void * dest
,const void *src
,size_t count
)
433 char *tmp
= (char *) dest
, *s
= (char *) src
;
441 EXPORT_SYMBOL(memcpy
);
443 #ifndef __HAVE_ARCH_MEMMOVE
445 * memmove - Copy one area of memory to another
446 * @dest: Where to copy to
447 * @src: Where to copy from
448 * @count: The size of the area.
450 * Unlike memcpy(), memmove() copes with overlapping areas.
452 void * memmove(void * dest
,const void *src
,size_t count
)
463 tmp
= (char *) dest
+ count
;
464 s
= (char *) src
+ count
;
472 EXPORT_SYMBOL(memmove
);
474 #ifndef __HAVE_ARCH_MEMCMP
476 * memcmp - Compare two areas of memory
477 * @cs: One area of memory
478 * @ct: Another area of memory
479 * @count: The size of the area.
481 int memcmp(const void * cs
,const void * ct
,size_t count
)
483 const unsigned char *su1
, *su2
;
486 for( su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--)
487 if ((res
= *su1
- *su2
) != 0)
492 EXPORT_SYMBOL(memcmp
);
494 #ifndef __HAVE_ARCH_MEMSCAN
496 * memscan - Find a character in an area of memory.
497 * @addr: The memory area
498 * @c: The byte to search for
499 * @size: The size of the area.
501 * returns the address of the first occurrence of @c, or 1 byte past
502 * the area if @c is not found
504 void * memscan(void * addr
, int c
, size_t size
)
506 unsigned char * p
= (unsigned char *) addr
;
517 EXPORT_SYMBOL(memscan
);
519 #ifndef __HAVE_ARCH_STRSTR
521 * strstr - Find the first substring in a %NUL terminated string
522 * @s1: The string to be searched
523 * @s2: The string to search for
525 char * _strstr(const char * s1
,const char * s2
)
535 if (!memcmp(s1
,s2
,l2
))
542 EXPORT_SYMBOL(strstr
);
544 #ifndef __HAVE_ARCH_MEMCHR
546 * memchr - Find a character in an area of memory.
547 * @s: The memory area
548 * @c: The byte to search for
549 * @n: The size of the area.
551 * returns the address of the first occurrence of @c, or %NULL
554 void *memchr(const void *s
, int c
, size_t n
)
556 const unsigned char *p
= s
;
558 if ((unsigned char)c
== *p
++) {
559 return (void *)(p
-1);
566 EXPORT_SYMBOL(memchr
);