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_STRCPY
33 * strcpy - Copy a %NUL terminated string
34 * @dest: Where to copy the string to
35 * @src: Where to copy the string from
37 char * strcpy(char * dest
,const char *src
)
41 while ((*dest
++ = *src
++) != '\0')
47 #ifndef __HAVE_ARCH_STRNCPY
49 * strncpy - Copy a length-limited, %NUL-terminated string
50 * @dest: Where to copy the string to
51 * @src: Where to copy the string from
52 * @count: The maximum number of bytes to copy
54 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
55 * However, the result is not %NUL-terminated if the source exceeds
58 char * strncpy(char * dest
,const char *src
,size_t count
)
62 while (count
-- && (*dest
++ = *src
++) != '\0')
69 #ifndef __HAVE_ARCH_STRCAT
71 * strcat - Append one %NUL-terminated string to another
72 * @dest: The string to be appended to
73 * @src: The string to append to it
75 char * strcat(char * dest
, const char * src
)
81 while ((*dest
++ = *src
++) != '\0')
88 #ifndef __HAVE_ARCH_STRCMP
90 * strcmp - Compare two strings
94 int strcmp(const char * cs
,const char * ct
)
96 register signed char __res
;
99 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
107 #ifndef __HAVE_ARCH_STRNCMP
109 * strncmp - Compare two length-limited strings
111 * @ct: Another string
112 * @count: The maximum number of bytes to compare
114 int strncmp(const char * cs
,const char * ct
,size_t count
)
116 register signed char __res
= 0;
119 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
128 #ifndef __HAVE_ARCH_STRCASECMP
129 int strcasecmp(const char *a
, const char *b
)
131 while (*a
&& *b
&& (*a
& ~0x20) == (*b
& ~0x20)) {a
++; b
++; }
132 return((*a
& ~0x20) - (*b
& ~0x20));
136 #ifndef __HAVE_ARCH_STRCHR
138 * strchr - Find the first occurrence of a character in a string
139 * @s: The string to be searched
140 * @c: The character to search for
142 char * strchr(const char * s
, int c
)
144 for(; *s
!= (char) c
; ++s
)
151 #ifndef __HAVE_ARCH_STRRCHR
153 * strrchr - Find the last occurrence of a character in a string
154 * @s: The string to be searched
155 * @c: The character to search for
157 char * strrchr(const char * s
, int c
)
159 const char *p
= s
+ strlen(s
);
168 #ifndef __HAVE_ARCH_STRLEN
170 * strlen - Find the length of a string
171 * @s: The string to be sized
173 size_t strlen(const char * s
)
177 for (sc
= s
; *sc
!= '\0'; ++sc
)
183 #ifndef __HAVE_ARCH_STRNLEN
185 * strnlen - Find the length of a length-limited string
186 * @s: The string to be sized
187 * @count: The maximum number of bytes to search
189 size_t strnlen(const char * s
, size_t count
)
193 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
199 #ifndef __HAVE_ARCH_MEMSET
201 * memset - Fill a region of memory with the given value
202 * @s: Pointer to the start of the area.
203 * @c: The byte to fill the area with
204 * @count: The size of the area.
206 * Do not use memset() to access IO space, use memset_io() instead.
208 void * memset(void * s
,int c
,size_t count
)
210 char *xs
= (char *) s
;
219 #ifndef __HAVE_ARCH_MEMCPY
221 * memcpy - Copy one area of memory to another
222 * @dest: Where to copy to
223 * @src: Where to copy from
224 * @count: The size of the area.
226 * You should not use this function to access IO space, use memcpy_toio()
227 * or memcpy_fromio() instead.
229 void * memcpy(void * dest
,const void *src
,size_t count
)
231 char *tmp
= (char *) dest
, *s
= (char *) src
;
240 #ifndef __HAVE_ARCH_MEMMOVE
242 * memmove - Copy one area of memory to another
243 * @dest: Where to copy to
244 * @src: Where to copy from
245 * @count: The size of the area.
247 * Unlike memcpy(), memmove() copes with overlapping areas.
249 void * memmove(void * dest
,const void *src
,size_t count
)
260 tmp
= (char *) dest
+ count
;
261 s
= (char *) src
+ count
;
270 #ifndef __HAVE_ARCH_MEMCMP
272 * memcmp - Compare two areas of memory
273 * @cs: One area of memory
274 * @ct: Another area of memory
275 * @count: The size of the area.
277 int memcmp(const void * cs
,const void * ct
,size_t count
)
279 const unsigned char *su1
, *su2
;
282 for( su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--)
283 if ((res
= *su1
- *su2
) != 0)
289 #ifndef __HAVE_ARCH_STRSTR
291 * strstr - Find the first substring in a %NUL terminated string
292 * @s1: The string to be searched
293 * @s2: The string to search for
295 char * strstr(const char * s1
,const char * s2
)
305 if (!memcmp(s1
,s2
,l2
))
313 #ifndef __HAVE_ARCH_MEMCHR
315 * memchr - Find a character in an area of memory.
316 * @s: The memory area
317 * @c: The byte to search for
318 * @n: The size of the area.
320 * returns the address of the first occurrence of @c, or %NULL
323 void * memchr(const void *s
, int c
, size_t n
)
325 const unsigned char *p
= s
;
327 if ((unsigned char)c
== *p
++) {
328 return (void *)(p
-1);
336 char * strndup(const char *s
, size_t n
)
338 size_t len
= strlen(s
);
351 char * strdup(const char *s
) {
352 return strndup(s
, ~((size_t)0));