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.
13 FILE_LICENCE ( GPL2_ONLY
);
16 * stupid library routines.. The optimized versions should generally be found
17 * as inline code in <asm-xx/string.h>
19 * These are buggy as well..
21 * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
22 * - Added strsep() which will replace strtok() soon (because strsep() is
23 * reentrant and should be faster). Use only strsep() in new code, please.
31 /* *** FROM string.c *** */
33 #ifndef __HAVE_ARCH_STRCPY
35 * strcpy - Copy a %NUL terminated string
36 * @dest: Where to copy the string to
37 * @src: Where to copy the string from
39 char * strcpy(char * dest
,const char *src
)
43 while ((*dest
++ = *src
++) != '\0')
49 #ifndef __HAVE_ARCH_STRNCPY
51 * strncpy - Copy a length-limited, %NUL-terminated string
52 * @dest: Where to copy the string to
53 * @src: Where to copy the string from
54 * @count: The maximum number of bytes to copy
56 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
57 * However, the result is not %NUL-terminated if the source exceeds
60 char * strncpy(char * dest
,const char *src
,size_t count
)
64 while (count
-- && (*dest
++ = *src
++) != '\0')
71 #ifndef __HAVE_ARCH_STRCAT
73 * strcat - Append one %NUL-terminated string to another
74 * @dest: The string to be appended to
75 * @src: The string to append to it
77 char * strcat(char * dest
, const char * src
)
83 while ((*dest
++ = *src
++) != '\0')
90 #ifndef __HAVE_ARCH_STRCMP
92 * strcmp - Compare two strings
96 int strcmp(const char * cs
,const char * ct
)
98 register signed char __res
;
101 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
109 #ifndef __HAVE_ARCH_STRNCMP
111 * strncmp - Compare two length-limited strings
113 * @ct: Another string
114 * @count: The maximum number of bytes to compare
116 int strncmp(const char * cs
,const char * ct
,size_t count
)
118 register signed char __res
= 0;
121 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
130 #ifndef __HAVE_ARCH_STRCASECMP
131 int strcasecmp(const char *a
, const char *b
)
133 while (*a
&& *b
&& (*a
& ~0x20) == (*b
& ~0x20)) {a
++; b
++; }
134 return((*a
& ~0x20) - (*b
& ~0x20));
138 #ifndef __HAVE_ARCH_STRCHR
140 * strchr - Find the first occurrence of a character in a string
141 * @s: The string to be searched
142 * @c: The character to search for
144 char * strchr(const char * s
, int c
)
146 for(; *s
!= (char) c
; ++s
)
153 #ifndef __HAVE_ARCH_STRRCHR
155 * strrchr - Find the last occurrence of a character in a string
156 * @s: The string to be searched
157 * @c: The character to search for
159 char * strrchr(const char * s
, int c
)
161 const char *p
= s
+ strlen(s
);
170 #ifndef __HAVE_ARCH_STRLEN
172 * strlen - Find the length of a string
173 * @s: The string to be sized
175 size_t strlen(const char * s
)
179 for (sc
= s
; *sc
!= '\0'; ++sc
)
185 #ifndef __HAVE_ARCH_STRNLEN
187 * strnlen - Find the length of a length-limited string
188 * @s: The string to be sized
189 * @count: The maximum number of bytes to search
191 size_t strnlen(const char * s
, size_t count
)
195 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
201 #ifndef __HAVE_ARCH_MEMSET
203 * memset - Fill a region of memory with the given value
204 * @s: Pointer to the start of the area.
205 * @c: The byte to fill the area with
206 * @count: The size of the area.
208 * Do not use memset() to access IO space, use memset_io() instead.
210 void * memset(void * s
,int c
,size_t count
)
212 char *xs
= (char *) s
;
221 #ifndef __HAVE_ARCH_MEMCPY
223 * memcpy - Copy one area of memory to another
224 * @dest: Where to copy to
225 * @src: Where to copy from
226 * @count: The size of the area.
228 * You should not use this function to access IO space, use memcpy_toio()
229 * or memcpy_fromio() instead.
231 void * memcpy(void * dest
,const void *src
,size_t count
)
233 char *tmp
= (char *) dest
, *s
= (char *) src
;
242 #ifndef __HAVE_ARCH_MEMMOVE
244 * memmove - Copy one area of memory to another
245 * @dest: Where to copy to
246 * @src: Where to copy from
247 * @count: The size of the area.
249 * Unlike memcpy(), memmove() copes with overlapping areas.
251 void * memmove(void * dest
,const void *src
,size_t count
)
262 tmp
= (char *) dest
+ count
;
263 s
= (char *) src
+ count
;
272 #ifndef __HAVE_ARCH_MEMCMP
274 * memcmp - Compare two areas of memory
275 * @cs: One area of memory
276 * @ct: Another area of memory
277 * @count: The size of the area.
279 int memcmp(const void * cs
,const void * ct
,size_t count
)
281 const unsigned char *su1
, *su2
;
284 for( su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--)
285 if ((res
= *su1
- *su2
) != 0)
291 #ifndef __HAVE_ARCH_STRSTR
293 * strstr - Find the first substring in a %NUL terminated string
294 * @s1: The string to be searched
295 * @s2: The string to search for
297 char * strstr(const char * s1
,const char * s2
)
307 if (!memcmp(s1
,s2
,l2
))
315 #ifndef __HAVE_ARCH_MEMCHR
317 * memchr - Find a character in an area of memory.
318 * @s: The memory area
319 * @c: The byte to search for
320 * @n: The size of the area.
322 * returns the address of the first occurrence of @c, or %NULL
325 void * memchr(const void *s
, int c
, size_t n
)
327 const unsigned char *p
= s
;
329 if ((unsigned char)c
== *p
++) {
330 return (void *)(p
-1);
338 char * strndup(const char *s
, size_t n
)
340 size_t len
= strlen(s
);
353 char * strdup(const char *s
) {
354 return strndup(s
, ~((size_t)0));