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.
17 * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
18 * Matthew Hawkins <matt@mh.dropbear.id.au>
19 * - Kissed strtok() goodbye
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/ctype.h>
25 #include <linux/module.h>
27 #ifndef __HAVE_ARCH_STRNICMP
29 * strnicmp - Case insensitive, length-limited string comparison
31 * @s2: The other string
32 * @len: the maximum number of characters to compare
34 int strnicmp(const char *s1, const char *s2, size_t len)
36 /* Yes, Virginia, it had better be unsigned */
56 return (int)c1 - (int)c2;
60 #ifndef __HAVE_ARCH_STRCPY
62 * strcpy - Copy a %NUL terminated string
63 * @dest: Where to copy the string to
64 * @src: Where to copy the string from
66 char * strcpy(char * dest,const char *src)
70 while ((*dest++ = *src++) != '\0')
76 #ifndef __HAVE_ARCH_STRNCPY
78 * strncpy - Copy a length-limited, %NUL-terminated string
79 * @dest: Where to copy the string to
80 * @src: Where to copy the string from
81 * @count: The maximum number of bytes to copy
83 * The result is not %NUL-terminated if the source exceeds
86 char * strncpy(char * dest,const char *src,size_t count)
90 while (count && (*dest++ = *src++) != '\0')
100 #ifndef __HAVE_ARCH_STRLCPY
102 * strlcpy - Copy a %NUL terminated string into a sized buffer
103 * @dest: Where to copy the string to
104 * @src: Where to copy the string from
105 * @size: size of destination buffer
107 * Compatible with *BSD: the result is always a valid
108 * NUL-terminated string that fits in the buffer (unless,
109 * of course, the buffer size is zero). It does not pad
110 * out the result like strncpy() does.
112 size_t strlcpy(char *dest, const char *src, size_t size)
114 size_t ret = strlen(src);
117 size_t len = (ret >= size) ? size-1 : ret;
118 memcpy(dest, src, len);
123 EXPORT_SYMBOL(strlcpy);
126 #ifndef __HAVE_ARCH_STRCAT
128 * strcat - Append one %NUL-terminated string to another
129 * @dest: The string to be appended to
130 * @src: The string to append to it
132 char * strcat(char * dest, const char * src)
138 while ((*dest++ = *src++) != '\0')
145 #ifndef __HAVE_ARCH_STRNCAT
147 * strncat - Append a length-limited, %NUL-terminated string to another
148 * @dest: The string to be appended to
149 * @src: The string to append to it
150 * @count: The maximum numbers of bytes to copy
152 * Note that in contrast to strncpy, strncat ensures the result is
155 char * strncat(char *dest, const char *src, size_t count)
162 while ((*dest++ = *src++)) {
174 #ifndef __HAVE_ARCH_STRLCAT
176 * strlcat - Append a length-limited, %NUL-terminated string to another
177 * @dest: The string to be appended to
178 * @src: The string to append to it
179 * @count: The size of the destination buffer.
181 size_t strlcat(char *dest, const char *src, size_t count)
183 size_t dsize = strlen(dest);
184 size_t len = strlen(src);
185 size_t res = dsize + len;
187 /* This would be a bug */
188 BUG_ON(dsize >= count);
194 memcpy(dest, src, len);
198 EXPORT_SYMBOL(strlcat);
201 #ifndef __HAVE_ARCH_STRCMP
203 * strcmp - Compare two strings
205 * @ct: Another string
207 int strcmp(const char * cs,const char * ct)
209 register signed char __res;
212 if ((__res = *cs - *ct++) != 0 || !*cs++)
220 #ifndef __HAVE_ARCH_STRNCMP
222 * strncmp - Compare two length-limited strings
224 * @ct: Another string
225 * @count: The maximum number of bytes to compare
227 int strncmp(const char * cs,const char * ct,size_t count)
229 register signed char __res = 0;
232 if ((__res = *cs - *ct++) != 0 || !*cs++)
241 #ifndef __HAVE_ARCH_STRCHR
243 * strchr - Find the first occurrence of a character in a string
244 * @s: The string to be searched
245 * @c: The character to search for
247 char * strchr(const char * s, int c)
249 for(; *s != (char) c; ++s)
256 #ifndef __HAVE_ARCH_STRRCHR
258 * strrchr - Find the last occurrence of a character in a string
259 * @s: The string to be searched
260 * @c: The character to search for
262 char * strrchr(const char * s, int c)
264 const char *p = s + strlen(s);
273 #ifndef __HAVE_ARCH_STRLEN
275 * strlen - Find the length of a string
276 * @s: The string to be sized
278 size_t strlen(const char * s)
282 for (sc = s; *sc != '\0'; ++sc)
288 #ifndef __HAVE_ARCH_STRNLEN
290 * strnlen - Find the length of a length-limited string
291 * @s: The string to be sized
292 * @count: The maximum number of bytes to search
294 size_t strnlen(const char * s, size_t count)
298 for (sc = s; count-- && *sc != '\0'; ++sc)
304 #ifndef __HAVE_ARCH_STRSPN
306 * strspn - Calculate the length of the initial substring of @s which only
307 * contain letters in @accept
308 * @s: The string to be searched
309 * @accept: The string to search for
311 size_t strspn(const char *s, const char *accept)
317 for (p = s; *p != '\0'; ++p) {
318 for (a = accept; *a != '\0'; ++a) {
332 * strcspn - Calculate the length of the initial substring of @s which does
333 * not contain letters in @reject
334 * @s: The string to be searched
335 * @reject: The string to avoid
337 size_t strcspn(const char *s, const char *reject)
343 for (p = s; *p != '\0'; ++p) {
344 for (r = reject; *r != '\0'; ++r) {
354 #ifndef __HAVE_ARCH_STRPBRK
356 * strpbrk - Find the first occurrence of a set of characters
357 * @cs: The string to be searched
358 * @ct: The characters to search for
360 char * strpbrk(const char * cs,const char * ct)
362 const char *sc1,*sc2;
364 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
365 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
374 #ifndef __HAVE_ARCH_STRSEP
376 * strsep - Split a string into tokens
377 * @s: The string to be searched
378 * @ct: The characters to search for
380 * strsep() updates @s to point after the token, ready for the next call.
382 * It returns empty tokens, too, behaving exactly like the libc function
383 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
384 * Same semantics, slimmer shape. ;)
386 char * strsep(char **s, const char *ct)
388 char *sbegin = *s, *end;
393 end = strpbrk(sbegin, ct);
402 #ifndef __HAVE_ARCH_MEMSET
404 * memset - Fill a region of memory with the given value
405 * @s: Pointer to the start of the area.
406 * @c: The byte to fill the area with
407 * @count: The size of the area.
409 * Do not use memset() to access IO space, use memset_io() instead.
411 void * memset(void * s,int c,size_t count)
413 char *xs = (char *) s;
422 #ifndef __HAVE_ARCH_BCOPY
424 * bcopy - Copy one area of memory to another
425 * @src: Where to copy from
426 * @dest: Where to copy to
427 * @count: The size of the area.
429 * Note that this is the same as memcpy(), with the arguments reversed.
430 * memcpy() is the standard, bcopy() is a legacy BSD function.
432 * You should not use this function to access IO space, use memcpy_toio()
433 * or memcpy_fromio() instead.
435 char * bcopy(const char * src, char * dest, int count)
446 #ifndef __HAVE_ARCH_MEMCPY
448 * memcpy - Copy one area of memory to another
449 * @dest: Where to copy to
450 * @src: Where to copy from
451 * @count: The size of the area.
453 * You should not use this function to access IO space, use memcpy_toio()
454 * or memcpy_fromio() instead.
456 void * memcpy(void * dest,const void *src,size_t count)
458 char *tmp = (char *) dest, *s = (char *) src;
467 #ifndef __HAVE_ARCH_MEMMOVE
469 * memmove - Copy one area of memory to another
470 * @dest: Where to copy to
471 * @src: Where to copy from
472 * @count: The size of the area.
474 * Unlike memcpy(), memmove() copes with overlapping areas.
476 void * memmove(void * dest,const void *src,size_t count)
487 tmp = (char *) dest + count;
488 s = (char *) src + count;
497 #ifndef __HAVE_ARCH_MEMCMP
499 * memcmp - Compare two areas of memory
500 * @cs: One area of memory
501 * @ct: Another area of memory
502 * @count: The size of the area.
504 int memcmp(const void * cs,const void * ct,size_t count)
506 const unsigned char *su1, *su2;
509 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
510 if ((res = *su1 - *su2) != 0)
516 #ifndef __HAVE_ARCH_MEMSCAN
518 * memscan - Find a character in an area of memory.
519 * @addr: The memory area
520 * @c: The byte to search for
521 * @size: The size of the area.
523 * returns the address of the first occurrence of @c, or 1 byte past
524 * the area if @c is not found
526 void * memscan(void * addr, int c, size_t size)
528 unsigned char * p = (unsigned char *) addr;
540 #ifndef __HAVE_ARCH_STRSTR
542 * strstr - Find the first substring in a %NUL terminated string
543 * @s1: The string to be searched
544 * @s2: The string to search for
546 char * strstr(const char * s1,const char * s2)
556 if (!memcmp(s1,s2,l2))
564 #ifndef __HAVE_ARCH_MEMCHR
566 * memchr - Find a character in an area of memory.
567 * @s: The memory area
568 * @c: The byte to search for
569 * @n: The size of the area.
571 * returns the address of the first occurrence of @c, or %NULL
574 void *memchr(const void *s, int c, size_t n)
576 const unsigned char *p = s;
578 if ((unsigned char)c == *p++) {
579 return (void *)(p-1);