blob: 542829742c85f28c7a160e4eb63616428bf71d83 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
34int 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 {
Jesper Juhl51a0f0f2005-10-30 15:02:11 -080042 c1 = *s1;
43 c2 = *s2;
44 s1++;
45 s2++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 if (!c1)
47 break;
48 if (!c2)
49 break;
50 if (c1 == c2)
51 continue;
52 c1 = tolower(c1);
53 c2 = tolower(c2);
54 if (c1 != c2)
55 break;
56 } while (--len);
57 }
58 return (int)c1 - (int)c2;
59}
Linus Torvalds1da177e2005-04-16 15:20:36 -070060EXPORT_SYMBOL(strnicmp);
61#endif
62
63#ifndef __HAVE_ARCH_STRCPY
64/**
65 * strcpy - Copy a %NUL terminated string
66 * @dest: Where to copy the string to
67 * @src: Where to copy the string from
68 */
Paolo 'Blaisorblade' Giarrusso0c281302005-05-05 16:15:17 -070069#undef strcpy
Jesper Juhl51a0f0f2005-10-30 15:02:11 -080070char *strcpy(char *dest, const char *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 char *tmp = dest;
73
74 while ((*dest++ = *src++) != '\0')
75 /* nothing */;
76 return tmp;
77}
78EXPORT_SYMBOL(strcpy);
79#endif
80
81#ifndef __HAVE_ARCH_STRNCPY
82/**
83 * strncpy - Copy a length-limited, %NUL-terminated string
84 * @dest: Where to copy the string to
85 * @src: Where to copy the string from
86 * @count: The maximum number of bytes to copy
87 *
88 * The result is not %NUL-terminated if the source exceeds
89 * @count bytes.
walter harms25279522005-05-05 16:16:20 -070090 *
91 * In the case where the length of @src is less than that of
92 * count, the remainder of @dest will be padded with %NUL.
93 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -080095char *strncpy(char *dest, const char *src, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 char *tmp = dest;
98
99 while (count) {
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800100 if ((*tmp = *src) != 0)
101 src++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 tmp++;
103 count--;
104 }
105 return dest;
106}
107EXPORT_SYMBOL(strncpy);
108#endif
109
110#ifndef __HAVE_ARCH_STRLCPY
111/**
112 * strlcpy - Copy a %NUL terminated string into a sized buffer
113 * @dest: Where to copy the string to
114 * @src: Where to copy the string from
115 * @size: size of destination buffer
116 *
117 * Compatible with *BSD: the result is always a valid
118 * NUL-terminated string that fits in the buffer (unless,
119 * of course, the buffer size is zero). It does not pad
120 * out the result like strncpy() does.
121 */
122size_t strlcpy(char *dest, const char *src, size_t size)
123{
124 size_t ret = strlen(src);
125
126 if (size) {
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800127 size_t len = (ret >= size) ? size - 1 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 memcpy(dest, src, len);
129 dest[len] = '\0';
130 }
131 return ret;
132}
133EXPORT_SYMBOL(strlcpy);
134#endif
135
136#ifndef __HAVE_ARCH_STRCAT
137/**
138 * strcat - Append one %NUL-terminated string to another
139 * @dest: The string to be appended to
140 * @src: The string to append to it
141 */
Paolo 'Blaisorblade' Giarrusso0c281302005-05-05 16:15:17 -0700142#undef strcat
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800143char *strcat(char *dest, const char *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 char *tmp = dest;
146
147 while (*dest)
148 dest++;
149 while ((*dest++ = *src++) != '\0')
150 ;
151
152 return tmp;
153}
154EXPORT_SYMBOL(strcat);
155#endif
156
157#ifndef __HAVE_ARCH_STRNCAT
158/**
159 * strncat - Append a length-limited, %NUL-terminated string to another
160 * @dest: The string to be appended to
161 * @src: The string to append to it
162 * @count: The maximum numbers of bytes to copy
163 *
164 * Note that in contrast to strncpy, strncat ensures the result is
165 * terminated.
166 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800167char *strncat(char *dest, const char *src, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 char *tmp = dest;
170
171 if (count) {
172 while (*dest)
173 dest++;
174 while ((*dest++ = *src++) != 0) {
175 if (--count == 0) {
176 *dest = '\0';
177 break;
178 }
179 }
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return tmp;
182}
183EXPORT_SYMBOL(strncat);
184#endif
185
186#ifndef __HAVE_ARCH_STRLCAT
187/**
188 * strlcat - Append a length-limited, %NUL-terminated string to another
189 * @dest: The string to be appended to
190 * @src: The string to append to it
191 * @count: The size of the destination buffer.
192 */
193size_t strlcat(char *dest, const char *src, size_t count)
194{
195 size_t dsize = strlen(dest);
196 size_t len = strlen(src);
197 size_t res = dsize + len;
198
199 /* This would be a bug */
200 BUG_ON(dsize >= count);
201
202 dest += dsize;
203 count -= dsize;
204 if (len >= count)
205 len = count-1;
206 memcpy(dest, src, len);
207 dest[len] = 0;
208 return res;
209}
210EXPORT_SYMBOL(strlcat);
211#endif
212
213#ifndef __HAVE_ARCH_STRCMP
214/**
215 * strcmp - Compare two strings
216 * @cs: One string
217 * @ct: Another string
218 */
Paolo 'Blaisorblade' Giarrusso0c281302005-05-05 16:15:17 -0700219#undef strcmp
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800220int strcmp(const char *cs, const char *ct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Jesper Juhlcc75fb72005-10-30 15:02:12 -0800222 signed char __res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 while (1) {
225 if ((__res = *cs - *ct++) != 0 || !*cs++)
226 break;
227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return __res;
229}
230EXPORT_SYMBOL(strcmp);
231#endif
232
233#ifndef __HAVE_ARCH_STRNCMP
234/**
235 * strncmp - Compare two length-limited strings
236 * @cs: One string
237 * @ct: Another string
238 * @count: The maximum number of bytes to compare
239 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800240int strncmp(const char *cs, const char *ct, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Jesper Juhlcc75fb72005-10-30 15:02:12 -0800242 signed char __res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 while (count) {
245 if ((__res = *cs - *ct++) != 0 || !*cs++)
246 break;
247 count--;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return __res;
250}
251EXPORT_SYMBOL(strncmp);
252#endif
253
254#ifndef __HAVE_ARCH_STRCHR
255/**
256 * strchr - Find the first occurrence of a character in a string
257 * @s: The string to be searched
258 * @c: The character to search for
259 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800260char *strchr(const char *s, int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800262 for (; *s != (char)c; ++s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (*s == '\0')
264 return NULL;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800265 return (char *)s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267EXPORT_SYMBOL(strchr);
268#endif
269
270#ifndef __HAVE_ARCH_STRRCHR
271/**
272 * strrchr - Find the last occurrence of a character in a string
273 * @s: The string to be searched
274 * @c: The character to search for
275 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800276char *strrchr(const char *s, int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 const char *p = s + strlen(s);
279 do {
280 if (*p == (char)c)
281 return (char *)p;
282 } while (--p >= s);
283 return NULL;
284}
285EXPORT_SYMBOL(strrchr);
286#endif
287
288#ifndef __HAVE_ARCH_STRNCHR
289/**
290 * strnchr - Find a character in a length limited string
291 * @s: The string to be searched
292 * @count: The number of characters to be searched
293 * @c: The character to search for
294 */
295char *strnchr(const char *s, size_t count, int c)
296{
297 for (; count-- && *s != '\0'; ++s)
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800298 if (*s == (char)c)
299 return (char *)s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return NULL;
301}
302EXPORT_SYMBOL(strnchr);
303#endif
304
305#ifndef __HAVE_ARCH_STRLEN
306/**
307 * strlen - Find the length of a string
308 * @s: The string to be sized
309 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800310size_t strlen(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 const char *sc;
313
314 for (sc = s; *sc != '\0'; ++sc)
315 /* nothing */;
316 return sc - s;
317}
318EXPORT_SYMBOL(strlen);
319#endif
320
321#ifndef __HAVE_ARCH_STRNLEN
322/**
323 * strnlen - Find the length of a length-limited string
324 * @s: The string to be sized
325 * @count: The maximum number of bytes to search
326 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800327size_t strnlen(const char *s, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 const char *sc;
330
331 for (sc = s; count-- && *sc != '\0'; ++sc)
332 /* nothing */;
333 return sc - s;
334}
335EXPORT_SYMBOL(strnlen);
336#endif
337
338#ifndef __HAVE_ARCH_STRSPN
339/**
340 * strspn - Calculate the length of the initial substring of @s which only
341 * contain letters in @accept
342 * @s: The string to be searched
343 * @accept: The string to search for
344 */
345size_t strspn(const char *s, const char *accept)
346{
347 const char *p;
348 const char *a;
349 size_t count = 0;
350
351 for (p = s; *p != '\0'; ++p) {
352 for (a = accept; *a != '\0'; ++a) {
353 if (*p == *a)
354 break;
355 }
356 if (*a == '\0')
357 return count;
358 ++count;
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return count;
361}
362
363EXPORT_SYMBOL(strspn);
364#endif
365
366/**
367 * strcspn - Calculate the length of the initial substring of @s which does
368 * not contain letters in @reject
369 * @s: The string to be searched
370 * @reject: The string to avoid
371 */
372size_t strcspn(const char *s, const char *reject)
373{
374 const char *p;
375 const char *r;
376 size_t count = 0;
377
378 for (p = s; *p != '\0'; ++p) {
379 for (r = reject; *r != '\0'; ++r) {
380 if (*p == *r)
381 return count;
382 }
383 ++count;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return count;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387EXPORT_SYMBOL(strcspn);
388
389#ifndef __HAVE_ARCH_STRPBRK
390/**
391 * strpbrk - Find the first occurrence of a set of characters
392 * @cs: The string to be searched
393 * @ct: The characters to search for
394 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800395char *strpbrk(const char *cs, const char *ct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800397 const char *sc1, *sc2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800399 for (sc1 = cs; *sc1 != '\0'; ++sc1) {
400 for (sc2 = ct; *sc2 != '\0'; ++sc2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (*sc1 == *sc2)
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800402 return (char *)sc1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404 }
405 return NULL;
406}
407EXPORT_SYMBOL(strpbrk);
408#endif
409
410#ifndef __HAVE_ARCH_STRSEP
411/**
412 * strsep - Split a string into tokens
413 * @s: The string to be searched
414 * @ct: The characters to search for
415 *
416 * strsep() updates @s to point after the token, ready for the next call.
417 *
418 * It returns empty tokens, too, behaving exactly like the libc function
419 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
420 * Same semantics, slimmer shape. ;)
421 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800422char *strsep(char **s, const char *ct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800424 char *sbegin = *s;
425 char *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 if (sbegin == NULL)
428 return NULL;
429
430 end = strpbrk(sbegin, ct);
431 if (end)
432 *end++ = '\0';
433 *s = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return sbegin;
435}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436EXPORT_SYMBOL(strsep);
437#endif
438
439#ifndef __HAVE_ARCH_MEMSET
440/**
441 * memset - Fill a region of memory with the given value
442 * @s: Pointer to the start of the area.
443 * @c: The byte to fill the area with
444 * @count: The size of the area.
445 *
446 * Do not use memset() to access IO space, use memset_io() instead.
447 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800448void *memset(void *s, int c, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800450 char *xs = (char *)s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 while (count--)
453 *xs++ = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return s;
455}
456EXPORT_SYMBOL(memset);
457#endif
458
459#ifndef __HAVE_ARCH_MEMCPY
460/**
461 * memcpy - Copy one area of memory to another
462 * @dest: Where to copy to
463 * @src: Where to copy from
464 * @count: The size of the area.
465 *
466 * You should not use this function to access IO space, use memcpy_toio()
467 * or memcpy_fromio() instead.
468 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800469void *memcpy(void *dest, const void *src, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800471 char *tmp = (char *)dest;
472 char *s = (char *)src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 while (count--)
475 *tmp++ = *s++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return dest;
477}
478EXPORT_SYMBOL(memcpy);
479#endif
480
481#ifndef __HAVE_ARCH_MEMMOVE
482/**
483 * memmove - Copy one area of memory to another
484 * @dest: Where to copy to
485 * @src: Where to copy from
486 * @count: The size of the area.
487 *
488 * Unlike memcpy(), memmove() copes with overlapping areas.
489 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800490void *memmove(void *dest, const void *src, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 char *tmp, *s;
493
494 if (dest <= src) {
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800495 tmp = (char *)dest;
496 s = (char *)src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 while (count--)
498 *tmp++ = *s++;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800499 } else {
500 tmp = (char *)dest + count;
501 s = (char *)src + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 while (count--)
503 *--tmp = *--s;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return dest;
506}
507EXPORT_SYMBOL(memmove);
508#endif
509
510#ifndef __HAVE_ARCH_MEMCMP
511/**
512 * memcmp - Compare two areas of memory
513 * @cs: One area of memory
514 * @ct: Another area of memory
515 * @count: The size of the area.
516 */
Paolo 'Blaisorblade' Giarrusso0c281302005-05-05 16:15:17 -0700517#undef memcmp
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800518int memcmp(const void *cs, const void *ct, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 const unsigned char *su1, *su2;
521 int res = 0;
522
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800523 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if ((res = *su1 - *su2) != 0)
525 break;
526 return res;
527}
528EXPORT_SYMBOL(memcmp);
529#endif
530
531#ifndef __HAVE_ARCH_MEMSCAN
532/**
533 * memscan - Find a character in an area of memory.
534 * @addr: The memory area
535 * @c: The byte to search for
536 * @size: The size of the area.
537 *
538 * returns the address of the first occurrence of @c, or 1 byte past
539 * the area if @c is not found
540 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800541void *memscan(void *addr, int c, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800543 unsigned char *p = (unsigned char *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 while (size) {
546 if (*p == c)
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800547 return (void *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 p++;
549 size--;
550 }
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800551 return (void *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553EXPORT_SYMBOL(memscan);
554#endif
555
556#ifndef __HAVE_ARCH_STRSTR
557/**
558 * strstr - Find the first substring in a %NUL terminated string
559 * @s1: The string to be searched
560 * @s2: The string to search for
561 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800562char *strstr(const char *s1, const char *s2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 int l1, l2;
565
566 l2 = strlen(s2);
567 if (!l2)
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800568 return (char *)s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 l1 = strlen(s1);
570 while (l1 >= l2) {
571 l1--;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800572 if (!memcmp(s1, s2, l2))
573 return (char *)s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 s1++;
575 }
576 return NULL;
577}
578EXPORT_SYMBOL(strstr);
579#endif
580
581#ifndef __HAVE_ARCH_MEMCHR
582/**
583 * memchr - Find a character in an area of memory.
584 * @s: The memory area
585 * @c: The byte to search for
586 * @n: The size of the area.
587 *
588 * returns the address of the first occurrence of @c, or %NULL
589 * if @c is not found
590 */
591void *memchr(const void *s, int c, size_t n)
592{
593 const unsigned char *p = s;
594 while (n-- != 0) {
595 if ((unsigned char)c == *p++) {
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800596 return (void *)(p - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 }
599 return NULL;
600}
601EXPORT_SYMBOL(memchr);
602#endif