commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / lib / string.c
1 /*
2  *  linux/lib/string.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
8  * stupid library routines.. The optimized versions should generally be found
9  * as inline code in <asm-xx/string.h>
10  *
11  * These are buggy as well..
12  *
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.
16  *
17  * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
18  *                    Matthew Hawkins <matt@mh.dropbear.id.au>
19  * -  Kissed strtok() goodbye
20  */
21  
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/ctype.h>
25 #include <linux/module.h>
26
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
33  */
34 int strnicmp(const char *s1, const char *s2, size_t len)
35 {
36         /* Yes, Virginia, it had better be unsigned */
37         unsigned char c1, c2;
38
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);
55         }
56         return (int)c1 - (int)c2;
57 }
58 #endif
59
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
65  */
66 char * strcpy(char * dest,const char *src)
67 {
68         char *tmp = dest;
69
70         while ((*dest++ = *src++) != '\0')
71                 /* nothing */;
72         return tmp;
73 }
74 #endif
75
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
82  *
83  * The result is not %NUL-terminated if the source exceeds
84  * @count bytes.
85  */
86 char * strncpy(char * dest,const char *src,size_t count)
87 {
88         char *tmp = dest;
89
90         while (count && (*dest++ = *src++) != '\0')
91                 count--;
92         while (count) {
93                 *dest++ = 0;
94                 count--;
95         }
96         return tmp;
97 }
98 #endif
99
100 #ifndef __HAVE_ARCH_STRLCPY
101 /**
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
106  *
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.
111  */
112 size_t strlcpy(char *dest, const char *src, size_t size)
113 {
114         size_t ret = strlen(src);
115
116         if (size) {
117                 size_t len = (ret >= size) ? size-1 : ret;
118                 memcpy(dest, src, len);
119                 dest[len] = '\0';
120         }
121         return ret;
122 }
123 EXPORT_SYMBOL(strlcpy);
124 #endif
125
126 #ifndef __HAVE_ARCH_STRCAT
127 /**
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
131  */
132 char * strcat(char * dest, const char * src)
133 {
134         char *tmp = dest;
135
136         while (*dest)
137                 dest++;
138         while ((*dest++ = *src++) != '\0')
139                 ;
140
141         return tmp;
142 }
143 #endif
144
145 #ifndef __HAVE_ARCH_STRNCAT
146 /**
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
151  *
152  * Note that in contrast to strncpy, strncat ensures the result is
153  * terminated.
154  */
155 char * strncat(char *dest, const char *src, size_t count)
156 {
157         char *tmp = dest;
158
159         if (count) {
160                 while (*dest)
161                         dest++;
162                 while ((*dest++ = *src++)) {
163                         if (--count == 0) {
164                                 *dest = '\0';
165                                 break;
166                         }
167                 }
168         }
169
170         return tmp;
171 }
172 #endif
173
174 #ifndef __HAVE_ARCH_STRLCAT
175 /**
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.
180  */
181 size_t strlcat(char *dest, const char *src, size_t count)
182 {
183         size_t dsize = strlen(dest);
184         size_t len = strlen(src);
185         size_t res = dsize + len;
186
187         /* This would be a bug */
188         BUG_ON(dsize >= count);
189
190         dest += dsize;
191         count -= dsize;
192         if (len >= count)
193                 len = count-1;
194         memcpy(dest, src, len);
195         dest[len] = 0;
196         return res;
197 }
198 EXPORT_SYMBOL(strlcat);
199 #endif
200
201 #ifndef __HAVE_ARCH_STRCMP
202 /**
203  * strcmp - Compare two strings
204  * @cs: One string
205  * @ct: Another string
206  */
207 int strcmp(const char * cs,const char * ct)
208 {
209         register signed char __res;
210
211         while (1) {
212                 if ((__res = *cs - *ct++) != 0 || !*cs++)
213                         break;
214         }
215
216         return __res;
217 }
218 #endif
219
220 #ifndef __HAVE_ARCH_STRNCMP
221 /**
222  * strncmp - Compare two length-limited strings
223  * @cs: One string
224  * @ct: Another string
225  * @count: The maximum number of bytes to compare
226  */
227 int strncmp(const char * cs,const char * ct,size_t count)
228 {
229         register signed char __res = 0;
230
231         while (count) {
232                 if ((__res = *cs - *ct++) != 0 || !*cs++)
233                         break;
234                 count--;
235         }
236
237         return __res;
238 }
239 #endif
240
241 #ifndef __HAVE_ARCH_STRCHR
242 /**
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
246  */
247 char * strchr(const char * s, int c)
248 {
249         for(; *s != (char) c; ++s)
250                 if (*s == '\0')
251                         return NULL;
252         return (char *) s;
253 }
254 #endif
255
256 #ifndef __HAVE_ARCH_STRRCHR
257 /**
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
261  */
262 char * strrchr(const char * s, int c)
263 {
264        const char *p = s + strlen(s);
265        do {
266            if (*p == (char)c)
267                return (char *)p;
268        } while (--p >= s);
269        return NULL;
270 }
271 #endif
272
273 #ifndef __HAVE_ARCH_STRLEN
274 /**
275  * strlen - Find the length of a string
276  * @s: The string to be sized
277  */
278 size_t strlen(const char * s)
279 {
280         const char *sc;
281
282         for (sc = s; *sc != '\0'; ++sc)
283                 /* nothing */;
284         return sc - s;
285 }
286 #endif
287
288 #ifndef __HAVE_ARCH_STRNLEN
289 /**
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
293  */
294 size_t strnlen(const char * s, size_t count)
295 {
296         const char *sc;
297
298         for (sc = s; count-- && *sc != '\0'; ++sc)
299                 /* nothing */;
300         return sc - s;
301 }
302 #endif
303
304 #ifndef __HAVE_ARCH_STRSPN
305 /**
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
310  */
311 size_t strspn(const char *s, const char *accept)
312 {
313         const char *p;
314         const char *a;
315         size_t count = 0;
316
317         for (p = s; *p != '\0'; ++p) {
318                 for (a = accept; *a != '\0'; ++a) {
319                         if (*p == *a)
320                                 break;
321                 }
322                 if (*a == '\0')
323                         return count;
324                 ++count;
325         }
326
327         return count;
328 }
329 #endif
330
331 /**
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
336  */
337 size_t strcspn(const char *s, const char *reject)
338 {
339         const char *p;
340         const char *r;
341         size_t count = 0;
342
343         for (p = s; *p != '\0'; ++p) {
344                 for (r = reject; *r != '\0'; ++r) {
345                         if (*p == *r)
346                                 return count;
347                 }
348                 ++count;
349         }
350
351         return count;
352 }       
353
354 #ifndef __HAVE_ARCH_STRPBRK
355 /**
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
359  */
360 char * strpbrk(const char * cs,const char * ct)
361 {
362         const char *sc1,*sc2;
363
364         for( sc1 = cs; *sc1 != '\0'; ++sc1) {
365                 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
366                         if (*sc1 == *sc2)
367                                 return (char *) sc1;
368                 }
369         }
370         return NULL;
371 }
372 #endif
373
374 #ifndef __HAVE_ARCH_STRSEP
375 /**
376  * strsep - Split a string into tokens
377  * @s: The string to be searched
378  * @ct: The characters to search for
379  *
380  * strsep() updates @s to point after the token, ready for the next call.
381  *
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. ;)
385  */
386 char * strsep(char **s, const char *ct)
387 {
388         char *sbegin = *s, *end;
389
390         if (sbegin == NULL)
391                 return NULL;
392
393         end = strpbrk(sbegin, ct);
394         if (end)
395                 *end++ = '\0';
396         *s = end;
397
398         return sbegin;
399 }
400 #endif
401
402 #ifndef __HAVE_ARCH_MEMSET
403 /**
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.
408  *
409  * Do not use memset() to access IO space, use memset_io() instead.
410  */
411 void * memset(void * s,int c,size_t count)
412 {
413         char *xs = (char *) s;
414
415         while (count--)
416                 *xs++ = c;
417
418         return s;
419 }
420 #endif
421
422 #ifndef __HAVE_ARCH_BCOPY
423 /**
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.
428  *
429  * Note that this is the same as memcpy(), with the arguments reversed.
430  * memcpy() is the standard, bcopy() is a legacy BSD function.
431  *
432  * You should not use this function to access IO space, use memcpy_toio()
433  * or memcpy_fromio() instead.
434  */
435 char * bcopy(const char * src, char * dest, int count)
436 {
437         char *tmp = dest;
438
439         while (count--)
440                 *tmp++ = *src++;
441
442         return dest;
443 }
444 #endif
445
446 #ifndef __HAVE_ARCH_MEMCPY
447 /**
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.
452  *
453  * You should not use this function to access IO space, use memcpy_toio()
454  * or memcpy_fromio() instead.
455  */
456 void * memcpy(void * dest,const void *src,size_t count)
457 {
458         char *tmp = (char *) dest, *s = (char *) src;
459
460         while (count--)
461                 *tmp++ = *s++;
462
463         return dest;
464 }
465 #endif
466
467 #ifndef __HAVE_ARCH_MEMMOVE
468 /**
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.
473  *
474  * Unlike memcpy(), memmove() copes with overlapping areas.
475  */
476 void * memmove(void * dest,const void *src,size_t count)
477 {
478         char *tmp, *s;
479
480         if (dest <= src) {
481                 tmp = (char *) dest;
482                 s = (char *) src;
483                 while (count--)
484                         *tmp++ = *s++;
485                 }
486         else {
487                 tmp = (char *) dest + count;
488                 s = (char *) src + count;
489                 while (count--)
490                         *--tmp = *--s;
491                 }
492
493         return dest;
494 }
495 #endif
496
497 #ifndef __HAVE_ARCH_MEMCMP
498 /**
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.
503  */
504 int memcmp(const void * cs,const void * ct,size_t count)
505 {
506         const unsigned char *su1, *su2;
507         int res = 0;
508
509         for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
510                 if ((res = *su1 - *su2) != 0)
511                         break;
512         return res;
513 }
514 #endif
515
516 #ifndef __HAVE_ARCH_MEMSCAN
517 /**
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.
522  *
523  * returns the address of the first occurrence of @c, or 1 byte past
524  * the area if @c is not found
525  */
526 void * memscan(void * addr, int c, size_t size)
527 {
528         unsigned char * p = (unsigned char *) addr;
529
530         while (size) {
531                 if (*p == c)
532                         return (void *) p;
533                 p++;
534                 size--;
535         }
536         return (void *) p;
537 }
538 #endif
539
540 #ifndef __HAVE_ARCH_STRSTR
541 /**
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
545  */
546 char * strstr(const char * s1,const char * s2)
547 {
548         int l1, l2;
549
550         l2 = strlen(s2);
551         if (!l2)
552                 return (char *) s1;
553         l1 = strlen(s1);
554         while (l1 >= l2) {
555                 l1--;
556                 if (!memcmp(s1,s2,l2))
557                         return (char *) s1;
558                 s1++;
559         }
560         return NULL;
561 }
562 #endif
563
564 #ifndef __HAVE_ARCH_MEMCHR
565 /**
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.
570  *
571  * returns the address of the first occurrence of @c, or %NULL
572  * if @c is not found
573  */
574 void *memchr(const void *s, int c, size_t n)
575 {
576         const unsigned char *p = s;
577         while (n-- != 0) {
578                 if ((unsigned char)c == *p++) {
579                         return (void *)(p-1);
580                 }
581         }
582         return NULL;
583 }
584
585 #endif