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>
22 #ifndef __HAVE_ARCH_STRNICMP
24 * strnicmp - Case insensitive, length-limited string comparison
26 * @s2: The other string
27 * @len: the maximum number of characters to compare
29 int strnicmp(const char *s1, const char *s2, size_t len)
31 /* Yes, Virginia, it had better be unsigned */
51 return (int)c1 - (int)c2;
57 #ifndef __HAVE_ARCH_STRCPY
59 * strcpy - Copy a %NUL terminated string
60 * @dest: Where to copy the string to
61 * @src: Where to copy the string from
63 char * strcpy(char * dest,const char *src)
67 while ((*dest++ = *src++) != '\0')
73 #ifndef __HAVE_ARCH_STRNCPY
75 * strncpy - Copy a length-limited, %NUL-terminated string
76 * @dest: Where to copy the string to
77 * @src: Where to copy the string from
78 * @count: The maximum number of bytes to copy
80 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
81 * However, the result is not %NUL-terminated if the source exceeds
84 char * strncpy(char * dest,const char *src,size_t count)
88 while (count-- && (*dest++ = *src++) != '\0')
95 #ifndef __HAVE_ARCH_STRCAT
97 * strcat - Append one %NUL-terminated string to another
98 * @dest: The string to be appended to
99 * @src: The string to append to it
101 char * strcat(char * dest, const char * src)
107 while ((*dest++ = *src++) != '\0')
114 #ifndef __HAVE_ARCH_STRNCAT
116 * strncat - Append a length-limited, %NUL-terminated string to another
117 * @dest: The string to be appended to
118 * @src: The string to append to it
119 * @count: The maximum numbers of bytes to copy
121 * Note that in contrast to strncpy, strncat ensures the result is
124 char * strncat(char *dest, const char *src, size_t count)
131 while ((*dest++ = *src++)) {
143 #ifndef __HAVE_ARCH_STRCMP
145 * strcmp - Compare two strings
147 * @ct: Another string
149 int strcmp(const char * cs,const char * ct)
151 register signed char __res;
154 if ((__res = *cs - *ct++) != 0 || !*cs++)
162 #ifndef __HAVE_ARCH_STRNCMP
164 * strncmp - Compare two length-limited strings
166 * @ct: Another string
167 * @count: The maximum number of bytes to compare
169 int strncmp(const char * cs,const char * ct,size_t count)
171 register signed char __res = 0;
174 if ((__res = *cs - *ct++) != 0 || !*cs++)
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)
198 #ifndef __HAVE_ARCH_STRRCHR
200 * strrchr - Find the last occurrence of a character in a string
201 * @s: The string to be searched
202 * @c: The character to search for
204 char * strrchr(const char * s, int c)
206 const char *p = s + strlen(s);
215 #ifndef __HAVE_ARCH_STRLEN
217 * strlen - Find the length of a string
218 * @s: The string to be sized
220 size_t strlen(const char * s)
224 for (sc = s; *sc != '\0'; ++sc)
230 #ifndef __HAVE_ARCH_STRNLEN
232 * strnlen - Find the length of a length-limited string
233 * @s: The string to be sized
234 * @count: The maximum number of bytes to search
236 size_t strnlen(const char * s, size_t count)
240 for (sc = s; count-- && *sc != '\0'; ++sc)
246 #ifndef __HAVE_ARCH_STRSPN
248 * strspn - Calculate the length of the initial substring of @s which only
249 * contain letters in @accept
250 * @s: The string to be searched
251 * @accept: The string to search for
253 size_t strspn(const char *s, const char *accept)
259 for (p = s; *p != '\0'; ++p) {
260 for (a = accept; *a != '\0'; ++a) {
273 #ifndef __HAVE_ARCH_STRPBRK
275 * strpbrk - Find the first occurrence of a set of characters
276 * @cs: The string to be searched
277 * @ct: The characters to search for
279 char * strpbrk(const char * cs,const char * ct)
281 const char *sc1,*sc2;
283 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
284 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
293 #ifndef __HAVE_ARCH_STRTOK
295 * strtok - Split a string into tokens
296 * @s: The string to be searched
297 * @ct: The characters to search for
299 * WARNING: strtok is deprecated, use strsep instead.
301 char * strtok(char * s,const char * ct)
305 sbegin = s ? s : ___strtok;
309 sbegin += strspn(sbegin,ct);
310 if (*sbegin == '\0') {
314 send = strpbrk( sbegin, ct);
315 if (send && *send != '\0')
322 #ifndef __HAVE_ARCH_STRSEP
324 * strsep - Split a string into tokens
325 * @s: The string to be searched
326 * @ct: The characters to search for
328 * strsep() updates @s to point after the token, ready for the next call.
330 * It returns empty tokens, too, behaving exactly like the libc function
331 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
332 * Same semantics, slimmer shape. ;)
334 char * strsep(char **s, const char *ct)
336 char *sbegin = *s, *end;
341 end = strpbrk(sbegin, ct);
350 #ifndef __HAVE_ARCH_MEMSET
352 * memset - Fill a region of memory with the given value
353 * @s: Pointer to the start of the area.
354 * @c: The byte to fill the area with
355 * @count: The size of the area.
357 * Do not use memset() to access IO space, use memset_io() instead.
359 void * memset(void * s,int c,size_t count)
361 char *xs = (char *) s;
370 #ifndef __HAVE_ARCH_BCOPY
372 * bcopy - Copy one area of memory to another
373 * @src: Where to copy from
374 * @dest: Where to copy to
375 * @count: The size of the area.
377 * Note that this is the same as memcpy(), with the arguments reversed.
378 * memcpy() is the standard, bcopy() is a legacy BSD function.
380 * You should not use this function to access IO space, use memcpy_toio()
381 * or memcpy_fromio() instead.
383 char * bcopy(const char * src, char * dest, int count)
394 #ifndef __HAVE_ARCH_MEMCPY
396 * memcpy - Copy one area of memory to another
397 * @dest: Where to copy to
398 * @src: Where to copy from
399 * @count: The size of the area.
401 * You should not use this function to access IO space, use memcpy_toio()
402 * or memcpy_fromio() instead.
404 void * memcpy(void * dest,const void *src,size_t count)
406 char *tmp = (char *) dest, *s = (char *) src;
415 #ifndef __HAVE_ARCH_MEMMOVE
417 * memmove - Copy one area of memory to another
418 * @dest: Where to copy to
419 * @src: Where to copy from
420 * @count: The size of the area.
422 * Unlike memcpy(), memmove() copes with overlapping areas.
424 void * memmove(void * dest,const void *src,size_t count)
435 tmp = (char *) dest + count;
436 s = (char *) src + count;
445 #ifndef __HAVE_ARCH_MEMCMP
447 * memcmp - Compare two areas of memory
448 * @cs: One area of memory
449 * @ct: Another area of memory
450 * @count: The size of the area.
452 int memcmp(const void * cs,const void * ct,size_t count)
454 const unsigned char *su1, *su2;
457 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
458 if ((res = *su1 - *su2) != 0)
464 #ifndef __HAVE_ARCH_MEMSCAN
466 * memscan - Find a character in an area of memory.
467 * @addr: The memory area
468 * @c: The byte to search for
469 * @size: The size of the area.
471 * returns the address of the first occurrence of @c, or 1 byte past
472 * the area if @c is not found
474 void * memscan(void * addr, int c, size_t size)
476 unsigned char * p = (unsigned char *) addr;
488 #ifndef __HAVE_ARCH_STRSTR
490 * strstr - Find the first substring in a %NUL terminated string
491 * @s1: The string to be searched
492 * @s2: The string to search for
494 char * strstr(const char * s1,const char * s2)
504 if (!memcmp(s1,s2,l2))
512 #ifndef __HAVE_ARCH_MEMCHR
514 * memchr - Find a character in an area of memory.
515 * @s: The memory area
516 * @c: The byte to search for
517 * @n: The size of the area.
519 * returns the address of the first occurrence of @c, or %NULL
522 void *memchr(const void *s, int c, size_t n)
524 const unsigned char *p = s;
526 if ((unsigned char)c == *p++) {
527 return (void *)(p-1);