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.
19 #include "libc/string.h"
20 #include "libc/stdlib.h"
23 * strnicmp - Case insensitive, length-limited string comparison
25 * @s2: The other string
26 * @len: the maximum number of characters to compare
28 int strnicmp(const char *s1, const char *s2, size_t len)
30 /* Yes, Virginia, it had better be unsigned */
50 return (int)c1 - (int)c2;
54 * strcpy - Copy a %NUL terminated string
55 * @dest: Where to copy the string to
56 * @src: Where to copy the string from
58 char * strcpy(char * dest,const char *src)
62 while ((*dest++ = *src++) != '\0')
68 * strncpy - Copy a length-limited, %NUL-terminated string
69 * @dest: Where to copy the string to
70 * @src: Where to copy the string from
71 * @count: The maximum number of bytes to copy
73 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
74 * However, the result is not %NUL-terminated if the source exceeds
77 char * strncpy(char * dest,const char *src,size_t count)
81 while (count-- && (*dest++ = *src++) != '\0')
88 * strcat - Append one %NUL-terminated string to another
89 * @dest: The string to be appended to
90 * @src: The string to append to it
92 char * strcat(char * dest, const char * src)
98 while ((*dest++ = *src++) != '\0')
105 * strncat - Append a length-limited, %NUL-terminated string to another
106 * @dest: The string to be appended to
107 * @src: The string to append to it
108 * @count: The maximum numbers of bytes to copy
110 * Note that in contrast to strncpy, strncat ensures the result is
113 char * strncat(char *dest, const char *src, size_t count)
120 while ((*dest++ = *src++)) {
132 * strcmp - Compare two strings
134 * @ct: Another string
136 int strcmp(const char * cs,const char * ct)
138 register signed char __res;
141 if ((__res = *cs - *ct++) != 0 || !*cs++)
149 * strncmp - Compare two length-limited strings
151 * @ct: Another string
152 * @count: The maximum number of bytes to compare
154 int strncmp(const char * cs,const char * ct,size_t count)
156 register signed char __res = 0;
159 if ((__res = *cs - *ct++) != 0 || !*cs++)
169 * strchr - Find the first occurrence of a character in a string
170 * @s: The string to be searched
171 * @c: The character to search for
173 char * strchr(const char * s, int c)
175 for(; *s != (char) c; ++s)
182 * strrchr - Find the last occurrence of a character in a string
183 * @s: The string to be searched
184 * @c: The character to search for
186 char * strrchr(const char * s, int c)
188 const char *p = s + strlen(s);
197 * strlen - Find the length of a string
198 * @s: The string to be sized
200 size_t strlen(const char * s)
204 for (sc = s; *sc != '\0'; ++sc)
210 * strnlen - Find the length of a length-limited string
211 * @s: The string to be sized
212 * @count: The maximum number of bytes to search
214 size_t strnlen(const char * s, size_t count)
218 for (sc = s; count-- && *sc != '\0'; ++sc)
224 * strpbrk - Find the first occurrence of a set of characters
225 * @cs: The string to be searched
226 * @ct: The characters to search for
228 char * strpbrk(const char * cs,const char * ct)
230 const char *sc1,*sc2;
232 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
233 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
242 * strsep - Split a string into tokens
243 * @s: The string to be searched
244 * @ct: The characters to search for
246 * strsep() updates @s to point after the token, ready for the next call.
248 * It returns empty tokens, too, behaving exactly like the libc function
249 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
250 * Same semantics, slimmer shape. ;)
252 char * strsep(char **s, const char *ct)
254 char *sbegin = *s, *end;
259 end = strpbrk(sbegin, ct);
268 * memset - Fill a region of memory with the given value
269 * @s: Pointer to the start of the area.
270 * @c: The byte to fill the area with
271 * @count: The size of the area.
273 * Do not use memset() to access IO space, use memset_io() instead.
275 void * memset(void * s,int c,size_t count)
277 char *xs = (char *) s;
286 * memcpy - Copy one area of memory to another
287 * @dest: Where to copy to
288 * @src: Where to copy from
289 * @count: The size of the area.
291 * You should not use this function to access IO space, use memcpy_toio()
292 * or memcpy_fromio() instead.
294 void * memcpy(void * dest,const void *src,size_t count)
296 char *tmp = (char *) dest, *s = (char *) src;
305 * memmove - Copy one area of memory to another
306 * @dest: Where to copy to
307 * @src: Where to copy from
308 * @count: The size of the area.
310 * Unlike memcpy(), memmove() copes with overlapping areas.
312 void * memmove(void * dest,const void *src,size_t count)
323 tmp = (char *) dest + count;
324 s = (char *) src + count;
333 * memcmp - Compare two areas of memory
334 * @cs: One area of memory
335 * @ct: Another area of memory
336 * @count: The size of the area.
338 int memcmp(const void * cs,const void * ct,size_t count)
340 const unsigned char *su1, *su2;
343 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
344 if ((res = *su1 - *su2) != 0)
350 strdup( const char *str )
355 p = malloc( strlen(str) + 1 );
361 strcasecmp( const char *cs, const char *ct )
363 register signed char __res;
366 char ch1 = toupper(*cs), ch2 = toupper(*ct);
368 if ((__res = ch1 - ch2) != 0 || !*cs++)
375 strncasecmp( const char *cs, const char *ct, size_t count )
377 register signed char __res = 0;
380 char ch1 = toupper(*cs), ch2 = toupper(*ct);
382 if ((__res = ch1 - ch2) != 0 || !*cs++)