]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 { | |
51a0f0f6 JJ |
42 | c1 = *s1; |
43 | c2 = *s2; | |
44 | s1++; | |
45 | s2++; | |
1da177e4 LT |
46 | 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 | } | |
1da177e4 LT |
60 | EXPORT_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 | */ | |
0c28130b | 69 | #undef strcpy |
51a0f0f6 | 70 | char *strcpy(char *dest, const char *src) |
1da177e4 LT |
71 | { |
72 | char *tmp = dest; | |
73 | ||
74 | while ((*dest++ = *src++) != '\0') | |
75 | /* nothing */; | |
76 | return tmp; | |
77 | } | |
78 | EXPORT_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. | |
25279526 | 90 | * |
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 | * | |
1da177e4 | 94 | */ |
51a0f0f6 | 95 | char *strncpy(char *dest, const char *src, size_t count) |
1da177e4 LT |
96 | { |
97 | char *tmp = dest; | |
98 | ||
99 | while (count) { | |
51a0f0f6 JJ |
100 | if ((*tmp = *src) != 0) |
101 | src++; | |
1da177e4 LT |
102 | tmp++; |
103 | count--; | |
104 | } | |
105 | return dest; | |
106 | } | |
107 | EXPORT_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 | */ | |
122 | size_t strlcpy(char *dest, const char *src, size_t size) | |
123 | { | |
124 | size_t ret = strlen(src); | |
125 | ||
126 | if (size) { | |
51a0f0f6 | 127 | size_t len = (ret >= size) ? size - 1 : ret; |
1da177e4 LT |
128 | memcpy(dest, src, len); |
129 | dest[len] = '\0'; | |
130 | } | |
131 | return ret; | |
132 | } | |
133 | EXPORT_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 | */ | |
0c28130b | 142 | #undef strcat |
51a0f0f6 | 143 | char *strcat(char *dest, const char *src) |
1da177e4 LT |
144 | { |
145 | char *tmp = dest; | |
146 | ||
147 | while (*dest) | |
148 | dest++; | |
149 | while ((*dest++ = *src++) != '\0') | |
150 | ; | |
151 | ||
152 | return tmp; | |
153 | } | |
154 | EXPORT_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 | */ | |
51a0f0f6 | 167 | char *strncat(char *dest, const char *src, size_t count) |
1da177e4 LT |
168 | { |
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 | } | |
1da177e4 LT |
181 | return tmp; |
182 | } | |
183 | EXPORT_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 | */ | |
193 | size_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 | } | |
210 | EXPORT_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 | */ | |
0c28130b | 219 | #undef strcmp |
51a0f0f6 | 220 | int strcmp(const char *cs, const char *ct) |
1da177e4 | 221 | { |
cc75fb71 | 222 | signed char __res; |
1da177e4 LT |
223 | |
224 | while (1) { | |
225 | if ((__res = *cs - *ct++) != 0 || !*cs++) | |
226 | break; | |
227 | } | |
1da177e4 LT |
228 | return __res; |
229 | } | |
230 | EXPORT_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 | */ | |
51a0f0f6 | 240 | int strncmp(const char *cs, const char *ct, size_t count) |
1da177e4 | 241 | { |
cc75fb71 | 242 | signed char __res = 0; |
1da177e4 LT |
243 | |
244 | while (count) { | |
245 | if ((__res = *cs - *ct++) != 0 || !*cs++) | |
246 | break; | |
247 | count--; | |
248 | } | |
1da177e4 LT |
249 | return __res; |
250 | } | |
251 | EXPORT_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 | */ | |
51a0f0f6 | 260 | char *strchr(const char *s, int c) |
1da177e4 | 261 | { |
51a0f0f6 | 262 | for (; *s != (char)c; ++s) |
1da177e4 LT |
263 | if (*s == '\0') |
264 | return NULL; | |
51a0f0f6 | 265 | return (char *)s; |
1da177e4 LT |
266 | } |
267 | EXPORT_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 | */ | |
51a0f0f6 | 276 | char *strrchr(const char *s, int c) |
1da177e4 LT |
277 | { |
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 | } | |
285 | EXPORT_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 | */ | |
295 | char *strnchr(const char *s, size_t count, int c) | |
296 | { | |
297 | for (; count-- && *s != '\0'; ++s) | |
51a0f0f6 JJ |
298 | if (*s == (char)c) |
299 | return (char *)s; | |
1da177e4 LT |
300 | return NULL; |
301 | } | |
302 | EXPORT_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 | */ | |
51a0f0f6 | 310 | size_t strlen(const char *s) |
1da177e4 LT |
311 | { |
312 | const char *sc; | |
313 | ||
314 | for (sc = s; *sc != '\0'; ++sc) | |
315 | /* nothing */; | |
316 | return sc - s; | |
317 | } | |
318 | EXPORT_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 | */ | |
51a0f0f6 | 327 | size_t strnlen(const char *s, size_t count) |
1da177e4 LT |
328 | { |
329 | const char *sc; | |
330 | ||
331 | for (sc = s; count-- && *sc != '\0'; ++sc) | |
332 | /* nothing */; | |
333 | return sc - s; | |
334 | } | |
335 | EXPORT_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 | */ | |
345 | size_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 | } | |
1da177e4 LT |
360 | return count; |
361 | } | |
362 | ||
363 | EXPORT_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 | */ | |
372 | size_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 | } | |
1da177e4 | 385 | return count; |
51a0f0f6 | 386 | } |
1da177e4 LT |
387 | EXPORT_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 | */ | |
51a0f0f6 | 395 | char *strpbrk(const char *cs, const char *ct) |
1da177e4 | 396 | { |
51a0f0f6 | 397 | const char *sc1, *sc2; |
1da177e4 | 398 | |
51a0f0f6 JJ |
399 | for (sc1 = cs; *sc1 != '\0'; ++sc1) { |
400 | for (sc2 = ct; *sc2 != '\0'; ++sc2) { | |
1da177e4 | 401 | if (*sc1 == *sc2) |
51a0f0f6 | 402 | return (char *)sc1; |
1da177e4 LT |
403 | } |
404 | } | |
405 | return NULL; | |
406 | } | |
407 | EXPORT_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 | */ | |
51a0f0f6 | 422 | char *strsep(char **s, const char *ct) |
1da177e4 | 423 | { |
51a0f0f6 JJ |
424 | char *sbegin = *s; |
425 | char *end; | |
1da177e4 LT |
426 | |
427 | if (sbegin == NULL) | |
428 | return NULL; | |
429 | ||
430 | end = strpbrk(sbegin, ct); | |
431 | if (end) | |
432 | *end++ = '\0'; | |
433 | *s = end; | |
1da177e4 LT |
434 | return sbegin; |
435 | } | |
1da177e4 LT |
436 | EXPORT_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 | */ | |
51a0f0f6 | 448 | void *memset(void *s, int c, size_t count) |
1da177e4 | 449 | { |
51a0f0f6 | 450 | char *xs = (char *)s; |
1da177e4 LT |
451 | |
452 | while (count--) | |
453 | *xs++ = c; | |
1da177e4 LT |
454 | return s; |
455 | } | |
456 | EXPORT_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 | */ | |
51a0f0f6 | 469 | void *memcpy(void *dest, const void *src, size_t count) |
1da177e4 | 470 | { |
51a0f0f6 JJ |
471 | char *tmp = (char *)dest; |
472 | char *s = (char *)src; | |
1da177e4 LT |
473 | |
474 | while (count--) | |
475 | *tmp++ = *s++; | |
1da177e4 LT |
476 | return dest; |
477 | } | |
478 | EXPORT_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 | */ | |
51a0f0f6 | 490 | void *memmove(void *dest, const void *src, size_t count) |
1da177e4 LT |
491 | { |
492 | char *tmp, *s; | |
493 | ||
494 | if (dest <= src) { | |
51a0f0f6 JJ |
495 | tmp = (char *)dest; |
496 | s = (char *)src; | |
1da177e4 LT |
497 | while (count--) |
498 | *tmp++ = *s++; | |
51a0f0f6 JJ |
499 | } else { |
500 | tmp = (char *)dest + count; | |
501 | s = (char *)src + count; | |
1da177e4 LT |
502 | while (count--) |
503 | *--tmp = *--s; | |
51a0f0f6 | 504 | } |
1da177e4 LT |
505 | return dest; |
506 | } | |
507 | EXPORT_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 | */ | |
0c28130b | 517 | #undef memcmp |
51a0f0f6 | 518 | int memcmp(const void *cs, const void *ct, size_t count) |
1da177e4 LT |
519 | { |
520 | const unsigned char *su1, *su2; | |
521 | int res = 0; | |
522 | ||
51a0f0f6 | 523 | for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) |
1da177e4 LT |
524 | if ((res = *su1 - *su2) != 0) |
525 | break; | |
526 | return res; | |
527 | } | |
528 | EXPORT_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 | */ | |
51a0f0f6 | 541 | void *memscan(void *addr, int c, size_t size) |
1da177e4 | 542 | { |
51a0f0f6 | 543 | unsigned char *p = (unsigned char *)addr; |
1da177e4 LT |
544 | |
545 | while (size) { | |
546 | if (*p == c) | |
51a0f0f6 | 547 | return (void *)p; |
1da177e4 LT |
548 | p++; |
549 | size--; | |
550 | } | |
51a0f0f6 | 551 | return (void *)p; |
1da177e4 LT |
552 | } |
553 | EXPORT_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 | */ | |
51a0f0f6 | 562 | char *strstr(const char *s1, const char *s2) |
1da177e4 LT |
563 | { |
564 | int l1, l2; | |
565 | ||
566 | l2 = strlen(s2); | |
567 | if (!l2) | |
51a0f0f6 | 568 | return (char *)s1; |
1da177e4 LT |
569 | l1 = strlen(s1); |
570 | while (l1 >= l2) { | |
571 | l1--; | |
51a0f0f6 JJ |
572 | if (!memcmp(s1, s2, l2)) |
573 | return (char *)s1; | |
1da177e4 LT |
574 | s1++; |
575 | } | |
576 | return NULL; | |
577 | } | |
578 | EXPORT_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 | */ | |
591 | void *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++) { | |
51a0f0f6 | 596 | return (void *)(p - 1); |
1da177e4 LT |
597 | } |
598 | } | |
599 | return NULL; | |
600 | } | |
601 | EXPORT_SYMBOL(memchr); | |
602 | #endif |