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>
23 #if 0 /* not used - was: #ifndef __HAVE_ARCH_STRNICMP */
25 * strnicmp - Case insensitive, length-limited string comparison
27 * @s2: The other string
28 * @len: the maximum number of characters to compare
30 int strnicmp(const char *s1, const char *s2, size_t len)
32 /* Yes, Virginia, it had better be unsigned */
52 return (int)c1 - (int)c2;
58 #ifndef __HAVE_ARCH_STRCPY
60 * strcpy - Copy a %NUL terminated string
61 * @dest: Where to copy the string to
62 * @src: Where to copy the string from
64 char * strcpy(char * dest,const char *src)
68 while ((*dest++ = *src++) != '\0')
74 #ifndef __HAVE_ARCH_STRNCPY
76 * strncpy - Copy a length-limited, %NUL-terminated string
77 * @dest: Where to copy the string to
78 * @src: Where to copy the string from
79 * @count: The maximum number of bytes to copy
81 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
82 * However, the result is not %NUL-terminated if the source exceeds
85 char * strncpy(char * dest,const char *src,size_t count)
89 while (count-- && (*dest++ = *src++) != '\0')
96 #ifndef __HAVE_ARCH_STRCAT
98 * strcat - Append one %NUL-terminated string to another
99 * @dest: The string to be appended to
100 * @src: The string to append to it
102 char * strcat(char * dest, const char * src)
108 while ((*dest++ = *src++) != '\0')
115 #ifndef __HAVE_ARCH_STRNCAT
117 * strncat - Append a length-limited, %NUL-terminated string to another
118 * @dest: The string to be appended to
119 * @src: The string to append to it
120 * @count: The maximum numbers of bytes to copy
122 * Note that in contrast to strncpy, strncat ensures the result is
125 char * strncat(char *dest, const char *src, size_t count)
132 while ((*dest++ = *src++)) {
144 #ifndef __HAVE_ARCH_STRCMP
146 * strcmp - Compare two strings
148 * @ct: Another string
150 int strcmp(const char * cs,const char * ct)
152 register signed char __res;
155 if ((__res = *cs - *ct++) != 0 || !*cs++)
163 #ifndef __HAVE_ARCH_STRNCMP
165 * strncmp - Compare two length-limited strings
167 * @ct: Another string
168 * @count: The maximum number of bytes to compare
170 int strncmp(const char * cs,const char * ct,size_t count)
172 register signed char __res = 0;
175 if ((__res = *cs - *ct++) != 0 || !*cs++)
184 #ifndef __HAVE_ARCH_STRCHR
186 * strchr - Find the first occurrence of a character in a string
187 * @s: The string to be searched
188 * @c: The character to search for
190 char * strchr(const char * s, int c)
192 for(; *s != (char) c; ++s)
199 #ifndef __HAVE_ARCH_STRRCHR
201 * strrchr - Find the last occurrence of a character in a string
202 * @s: The string to be searched
203 * @c: The character to search for
205 char * strrchr(const char * s, int c)
207 const char *p = s + strlen(s);
216 #ifndef __HAVE_ARCH_STRLEN
218 * strlen - Find the length of a string
219 * @s: The string to be sized
221 size_t strlen(const char * s)
225 for (sc = s; *sc != '\0'; ++sc)
231 #ifndef __HAVE_ARCH_STRNLEN
233 * strnlen - Find the length of a length-limited string
234 * @s: The string to be sized
235 * @count: The maximum number of bytes to search
237 size_t strnlen(const char * s, size_t count)
241 for (sc = s; count-- && *sc != '\0'; ++sc)
248 #ifndef __HAVE_ARCH_STRSPN
250 * strspn - Calculate the length of the initial substring of @s which only
251 * contain letters in @accept
252 * @s: The string to be searched
253 * @accept: The string to search for
255 size_t strspn(const char *s, const char *accept)
261 for (p = s; *p != '\0'; ++p) {
262 for (a = accept; *a != '\0'; ++a) {
275 #ifndef __HAVE_ARCH_STRPBRK
277 * strpbrk - Find the first occurrence of a set of characters
278 * @cs: The string to be searched
279 * @ct: The characters to search for
281 char * strpbrk(const char * cs,const char * ct)
283 const char *sc1,*sc2;
285 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
286 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
295 #ifndef __HAVE_ARCH_STRTOK
297 * strtok - Split a string into tokens
298 * @s: The string to be searched
299 * @ct: The characters to search for
301 * WARNING: strtok is deprecated, use strsep instead.
303 char * strtok(char * s,const char * ct)
307 sbegin = s ? s : ___strtok;
311 sbegin += strspn(sbegin,ct);
312 if (*sbegin == '\0') {
316 send = strpbrk( sbegin, ct);
317 if (send && *send != '\0')
324 #ifndef __HAVE_ARCH_STRSEP
326 * strsep - Split a string into tokens
327 * @s: The string to be searched
328 * @ct: The characters to search for
330 * strsep() updates @s to point after the token, ready for the next call.
332 * It returns empty tokens, too, behaving exactly like the libc function
333 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
334 * Same semantics, slimmer shape. ;)
336 char * strsep(char **s, const char *ct)
338 char *sbegin = *s, *end;
343 end = strpbrk(sbegin, ct);
352 #ifndef __HAVE_ARCH_STRSWAB
354 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
355 * s: address of the string
357 * returns the address of the swapped string or NULL on error. If
358 * string length is odd, last byte is untouched.
360 char *strswab(const char *s)
364 if ((NULL == s) || ('\0' == *s)) {
368 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
380 #ifndef __HAVE_ARCH_MEMSET
382 * memset - Fill a region of memory with the given value
383 * @s: Pointer to the start of the area.
384 * @c: The byte to fill the area with
385 * @count: The size of the area.
387 * Do not use memset() to access IO space, use memset_io() instead.
389 void * memset(void * s,int c,size_t count)
391 unsigned long *sl = (unsigned long *) s;
392 unsigned long cl = 0;
396 /* do it one word at a time (32 bits or 64 bits) while possible */
397 if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
398 for (i = 0; i < sizeof(*sl); i++) {
402 while (count >= sizeof(*sl)) {
404 count -= sizeof(*sl);
407 /* fill 8 bits at a time */
416 #ifndef __HAVE_ARCH_BCOPY
418 * bcopy - Copy one area of memory to another
419 * @src: Where to copy from
420 * @dest: Where to copy to
421 * @count: The size of the area.
423 * Note that this is the same as memcpy(), with the arguments reversed.
424 * memcpy() is the standard, bcopy() is a legacy BSD function.
426 * You should not use this function to access IO space, use memcpy_toio()
427 * or memcpy_fromio() instead.
429 char * bcopy(const char * src, char * dest, int count)
440 #ifndef __HAVE_ARCH_MEMCPY
442 * memcpy - Copy one area of memory to another
443 * @dest: Where to copy to
444 * @src: Where to copy from
445 * @count: The size of the area.
447 * You should not use this function to access IO space, use memcpy_toio()
448 * or memcpy_fromio() instead.
450 void * memcpy(void *dest, const void *src, size_t count)
452 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
455 /* while all data is aligned (common case), copy a word at a time */
456 if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
457 while (count >= sizeof(*dl)) {
459 count -= sizeof(*dl);
462 /* copy the reset one byte at a time */
472 #ifndef __HAVE_ARCH_MEMMOVE
474 * memmove - Copy one area of memory to another
475 * @dest: Where to copy to
476 * @src: Where to copy from
477 * @count: The size of the area.
479 * Unlike memcpy(), memmove() copes with overlapping areas.
481 void * memmove(void * dest,const void *src,size_t count)
492 tmp = (char *) dest + count;
493 s = (char *) src + count;
502 #ifndef __HAVE_ARCH_MEMCMP
504 * memcmp - Compare two areas of memory
505 * @cs: One area of memory
506 * @ct: Another area of memory
507 * @count: The size of the area.
509 int memcmp(const void * cs,const void * ct,size_t count)
511 const unsigned char *su1, *su2;
514 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
515 if ((res = *su1 - *su2) != 0)
521 #ifndef __HAVE_ARCH_MEMSCAN
523 * memscan - Find a character in an area of memory.
524 * @addr: The memory area
525 * @c: The byte to search for
526 * @size: The size of the area.
528 * returns the address of the first occurrence of @c, or 1 byte past
529 * the area if @c is not found
531 void * memscan(void * addr, int c, size_t size)
533 unsigned char * p = (unsigned char *) addr;
545 #ifndef __HAVE_ARCH_STRSTR
547 * strstr - Find the first substring in a %NUL terminated string
548 * @s1: The string to be searched
549 * @s2: The string to search for
551 char * strstr(const char * s1,const char * s2)
561 if (!memcmp(s1,s2,l2))
569 #ifndef __HAVE_ARCH_MEMCHR
571 * memchr - Find a character in an area of memory.
572 * @s: The memory area
573 * @c: The byte to search for
574 * @n: The size of the area.
576 * returns the address of the first occurrence of @c, or %NULL
579 void *memchr(const void *s, int c, size_t n)
581 const unsigned char *p = s;
583 if ((unsigned char)c == *p++) {
584 return (void *)(p-1);