Get driver to work again after last night's torture.
[linux-2.6/linux-mips.git] / lib / string.c
bloba9a7f5c26781b14df19d46c81a6581087eb706bd
1 /*
2 * linux/lib/string.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
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
28 /**
29 * strnicmp - Case insensitive, length-limited string comparison
30 * @s1: One string
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 */
37 unsigned char c1, c2;
39 c1 = 0; c2 = 0;
40 if (len) {
41 do {
42 c1 = *s1; c2 = *s2;
43 s1++; s2++;
44 if (!c1)
45 break;
46 if (!c2)
47 break;
48 if (c1 == c2)
49 continue;
50 c1 = tolower(c1);
51 c2 = tolower(c2);
52 if (c1 != c2)
53 break;
54 } while (--len);
56 return (int)c1 - (int)c2;
58 #endif
60 #ifndef __HAVE_ARCH_STRCPY
61 /**
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)
68 char *tmp = dest;
70 while ((*dest++ = *src++) != '\0')
71 /* nothing */;
72 return tmp;
74 #endif
76 #ifndef __HAVE_ARCH_STRNCPY
77 /**
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 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
84 * However, the result is not %NUL-terminated if the source exceeds
85 * @count bytes.
87 char * strncpy(char * dest,const char *src,size_t count)
89 char *tmp = dest;
91 while (count-- && (*dest++ = *src++) != '\0')
92 /* nothing */;
94 return tmp;
96 #endif
98 #ifndef __HAVE_ARCH_STRLCPY
99 /**
100 * strlcpy - Copy a %NUL terminated string into a sized buffer
101 * @dest: Where to copy the string to
102 * @src: Where to copy the string from
103 * @size: size of destination buffer
105 * Compatible with *BSD: the result is always a valid
106 * NUL-terminated string that fits in the buffer (unless,
107 * of course, the buffer size is zero). It does not pad
108 * out the result like strncpy() does.
110 size_t strlcpy(char *dest, const char *src, size_t size)
112 size_t ret = strlen(src);
114 if (size) {
115 size_t len = (ret >= size) ? size-1 : ret;
116 memcpy(dest, src, len);
117 dest[len] = '\0';
119 return ret;
121 EXPORT_SYMBOL(strlcpy);
122 #endif
124 #ifndef __HAVE_ARCH_STRCAT
126 * strcat - Append one %NUL-terminated string to another
127 * @dest: The string to be appended to
128 * @src: The string to append to it
130 char * strcat(char * dest, const char * src)
132 char *tmp = dest;
134 while (*dest)
135 dest++;
136 while ((*dest++ = *src++) != '\0')
139 return tmp;
141 #endif
143 #ifndef __HAVE_ARCH_STRNCAT
145 * strncat - Append a length-limited, %NUL-terminated string to another
146 * @dest: The string to be appended to
147 * @src: The string to append to it
148 * @count: The maximum numbers of bytes to copy
150 * Note that in contrast to strncpy, strncat ensures the result is
151 * terminated.
153 char * strncat(char *dest, const char *src, size_t count)
155 char *tmp = dest;
157 if (count) {
158 while (*dest)
159 dest++;
160 while ((*dest++ = *src++)) {
161 if (--count == 0) {
162 *dest = '\0';
163 break;
168 return tmp;
170 #endif
172 #ifndef __HAVE_ARCH_STRLCAT
174 * strlcat - Append a length-limited, %NUL-terminated string to another
175 * @dest: The string to be appended to
176 * @src: The string to append to it
177 * @count: The size of the destination buffer.
179 size_t strlcat(char *dest, const char *src, size_t count)
181 size_t dsize = strlen(dest);
182 size_t len = strlen(src);
183 size_t res = dsize + len;
185 /* This would be a bug */
186 BUG_ON(dsize >= count);
188 dest += dsize;
189 count -= dsize;
190 if (len >= count)
191 len = count-1;
192 memcpy(dest, src, len);
193 dest[len] = 0;
194 return res;
196 EXPORT_SYMBOL(strlcat);
197 #endif
199 #ifndef __HAVE_ARCH_STRCMP
201 * strcmp - Compare two strings
202 * @cs: One string
203 * @ct: Another string
205 int strcmp(const char * cs,const char * ct)
207 register signed char __res;
209 while (1) {
210 if ((__res = *cs - *ct++) != 0 || !*cs++)
211 break;
214 return __res;
216 #endif
218 #ifndef __HAVE_ARCH_STRNCMP
220 * strncmp - Compare two length-limited strings
221 * @cs: One string
222 * @ct: Another string
223 * @count: The maximum number of bytes to compare
225 int strncmp(const char * cs,const char * ct,size_t count)
227 register signed char __res = 0;
229 while (count) {
230 if ((__res = *cs - *ct++) != 0 || !*cs++)
231 break;
232 count--;
235 return __res;
237 #endif
239 #ifndef __HAVE_ARCH_STRCHR
241 * strchr - Find the first occurrence of a character in a string
242 * @s: The string to be searched
243 * @c: The character to search for
245 char * strchr(const char * s, int c)
247 for(; *s != (char) c; ++s)
248 if (*s == '\0')
249 return NULL;
250 return (char *) s;
252 #endif
254 #ifndef __HAVE_ARCH_STRRCHR
256 * strrchr - Find the last occurrence of a character in a string
257 * @s: The string to be searched
258 * @c: The character to search for
260 char * strrchr(const char * s, int c)
262 const char *p = s + strlen(s);
263 do {
264 if (*p == (char)c)
265 return (char *)p;
266 } while (--p >= s);
267 return NULL;
269 #endif
271 #ifndef __HAVE_ARCH_STRLEN
273 * strlen - Find the length of a string
274 * @s: The string to be sized
276 size_t strlen(const char * s)
278 const char *sc;
280 for (sc = s; *sc != '\0'; ++sc)
281 /* nothing */;
282 return sc - s;
284 #endif
286 #ifndef __HAVE_ARCH_STRNLEN
288 * strnlen - Find the length of a length-limited string
289 * @s: The string to be sized
290 * @count: The maximum number of bytes to search
292 size_t strnlen(const char * s, size_t count)
294 const char *sc;
296 for (sc = s; count-- && *sc != '\0'; ++sc)
297 /* nothing */;
298 return sc - s;
300 #endif
302 #ifndef __HAVE_ARCH_STRSPN
304 * strspn - Calculate the length of the initial substring of @s which only
305 * contain letters in @accept
306 * @s: The string to be searched
307 * @accept: The string to search for
309 size_t strspn(const char *s, const char *accept)
311 const char *p;
312 const char *a;
313 size_t count = 0;
315 for (p = s; *p != '\0'; ++p) {
316 for (a = accept; *a != '\0'; ++a) {
317 if (*p == *a)
318 break;
320 if (*a == '\0')
321 return count;
322 ++count;
325 return count;
327 #endif
330 * strcspn - Calculate the length of the initial substring of @s which does
331 * not contain letters in @reject
332 * @s: The string to be searched
333 * @reject: The string to avoid
335 size_t strcspn(const char *s, const char *reject)
337 const char *p;
338 const char *r;
339 size_t count = 0;
341 for (p = s; *p != '\0'; ++p) {
342 for (r = reject; *r != '\0'; ++r) {
343 if (*p == *r)
344 return count;
346 ++count;
349 return count;
352 #ifndef __HAVE_ARCH_STRPBRK
354 * strpbrk - Find the first occurrence of a set of characters
355 * @cs: The string to be searched
356 * @ct: The characters to search for
358 char * strpbrk(const char * cs,const char * ct)
360 const char *sc1,*sc2;
362 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
363 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
364 if (*sc1 == *sc2)
365 return (char *) sc1;
368 return NULL;
370 #endif
372 #ifndef __HAVE_ARCH_STRSEP
374 * strsep - Split a string into tokens
375 * @s: The string to be searched
376 * @ct: The characters to search for
378 * strsep() updates @s to point after the token, ready for the next call.
380 * It returns empty tokens, too, behaving exactly like the libc function
381 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
382 * Same semantics, slimmer shape. ;)
384 char * strsep(char **s, const char *ct)
386 char *sbegin = *s, *end;
388 if (sbegin == NULL)
389 return NULL;
391 end = strpbrk(sbegin, ct);
392 if (end)
393 *end++ = '\0';
394 *s = end;
396 return sbegin;
398 #endif
400 #ifndef __HAVE_ARCH_MEMSET
402 * memset - Fill a region of memory with the given value
403 * @s: Pointer to the start of the area.
404 * @c: The byte to fill the area with
405 * @count: The size of the area.
407 * Do not use memset() to access IO space, use memset_io() instead.
409 void * memset(void * s,int c, size_t count)
411 char *xs = (char *) s;
413 while (count--)
414 *xs++ = c;
416 return s;
418 #endif
420 #ifndef __HAVE_ARCH_BCOPY
422 * bcopy - Copy one area of memory to another
423 * @src: Where to copy from
424 * @dest: Where to copy to
425 * @count: The size of the area.
427 * Note that this is the same as memcpy(), with the arguments reversed.
428 * memcpy() is the standard, bcopy() is a legacy BSD function.
430 * You should not use this function to access IO space, use memcpy_toio()
431 * or memcpy_fromio() instead.
433 char * bcopy(const char * src, char * dest, int count)
435 char *tmp = dest;
437 while (count--)
438 *tmp++ = *src++;
440 return dest;
442 #endif
444 #ifndef __HAVE_ARCH_MEMCPY
446 * memcpy - Copy one area of memory to another
447 * @dest: Where to copy to
448 * @src: Where to copy from
449 * @count: The size of the area.
451 * You should not use this function to access IO space, use memcpy_toio()
452 * or memcpy_fromio() instead.
454 void * memcpy(void * dest,const void *src,size_t count)
456 char *tmp = (char *) dest, *s = (char *) src;
458 while (count--)
459 *tmp++ = *s++;
461 return dest;
463 #endif
465 #ifndef __HAVE_ARCH_MEMMOVE
467 * memmove - Copy one area of memory to another
468 * @dest: Where to copy to
469 * @src: Where to copy from
470 * @count: The size of the area.
472 * Unlike memcpy(), memmove() copes with overlapping areas.
474 void * memmove(void * dest,const void *src,size_t count)
476 char *tmp, *s;
478 if (dest <= src) {
479 tmp = (char *) dest;
480 s = (char *) src;
481 while (count--)
482 *tmp++ = *s++;
484 else {
485 tmp = (char *) dest + count;
486 s = (char *) src + count;
487 while (count--)
488 *--tmp = *--s;
491 return dest;
493 #endif
495 #ifndef __HAVE_ARCH_MEMCMP
497 * memcmp - Compare two areas of memory
498 * @cs: One area of memory
499 * @ct: Another area of memory
500 * @count: The size of the area.
502 int memcmp(const void * cs,const void * ct,size_t count)
504 const unsigned char *su1, *su2;
505 int res = 0;
507 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
508 if ((res = *su1 - *su2) != 0)
509 break;
510 return res;
512 #endif
514 #ifndef __HAVE_ARCH_MEMSCAN
516 * memscan - Find a character in an area of memory.
517 * @addr: The memory area
518 * @c: The byte to search for
519 * @size: The size of the area.
521 * returns the address of the first occurrence of @c, or 1 byte past
522 * the area if @c is not found
524 void * memscan(void * addr, int c, size_t size)
526 unsigned char * p = (unsigned char *) addr;
527 unsigned char * e = p + size;
529 while (p != e) {
530 if (*p == (unsigned char)c)
531 return (void *) p;
532 p++;
535 return (void *) p;
537 #endif
539 #ifndef __HAVE_ARCH_STRSTR
541 * strstr - Find the first substring in a %NUL terminated string
542 * @s1: The string to be searched
543 * @s2: The string to search for
545 char * strstr(const char * s1,const char * s2)
547 int l1, l2;
549 l2 = strlen(s2);
550 if (!l2)
551 return (char *) s1;
552 l1 = strlen(s1);
553 while (l1 >= l2) {
554 l1--;
555 if (!memcmp(s1,s2,l2))
556 return (char *) s1;
557 s1++;
559 return NULL;
561 #endif
563 #ifndef __HAVE_ARCH_MEMCHR
565 * memchr - Find a character in an area of memory.
566 * @s: The memory area
567 * @c: The byte to search for
568 * @n: The size of the area.
570 * returns the address of the first occurrence of @c, or %NULL
571 * if @c is not found
573 void *memchr(const void *s, int c, size_t n)
575 const unsigned char *p = s;
576 while (n-- != 0) {
577 if ((unsigned char)c == *p++) {
578 return (void *)(p-1);
581 return NULL;
584 #endif