]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/string.c
efi.h: use %pUl to print UUIDs
[mirror_ubuntu-artful-kernel.git] / lib / string.c
CommitLineData
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 */
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
850b9247 39 c1 = c2 = 0;
1da177e4
LT
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
60EXPORT_SYMBOL(strnicmp);
61#endif
62
ded220bd
DM
63#ifndef __HAVE_ARCH_STRCASECMP
64int strcasecmp(const char *s1, const char *s2)
65{
66 int c1, c2;
67
68 do {
69 c1 = tolower(*s1++);
70 c2 = tolower(*s2++);
71 } while (c1 == c2 && c1 != 0);
72 return c1 - c2;
73}
74EXPORT_SYMBOL(strcasecmp);
75#endif
76
77#ifndef __HAVE_ARCH_STRNCASECMP
78int strncasecmp(const char *s1, const char *s2, size_t n)
79{
80 int c1, c2;
81
82 do {
83 c1 = tolower(*s1++);
84 c2 = tolower(*s2++);
85 } while ((--n > 0) && c1 == c2 && c1 != 0);
86 return c1 - c2;
87}
88EXPORT_SYMBOL(strncasecmp);
89#endif
90
1da177e4
LT
91#ifndef __HAVE_ARCH_STRCPY
92/**
93 * strcpy - Copy a %NUL terminated string
94 * @dest: Where to copy the string to
95 * @src: Where to copy the string from
96 */
0c28130b 97#undef strcpy
51a0f0f6 98char *strcpy(char *dest, const char *src)
1da177e4
LT
99{
100 char *tmp = dest;
101
102 while ((*dest++ = *src++) != '\0')
103 /* nothing */;
104 return tmp;
105}
106EXPORT_SYMBOL(strcpy);
107#endif
108
109#ifndef __HAVE_ARCH_STRNCPY
110/**
111 * strncpy - Copy a length-limited, %NUL-terminated string
112 * @dest: Where to copy the string to
113 * @src: Where to copy the string from
114 * @count: The maximum number of bytes to copy
115 *
116 * The result is not %NUL-terminated if the source exceeds
117 * @count bytes.
25279526 118 *
119 * In the case where the length of @src is less than that of
120 * count, the remainder of @dest will be padded with %NUL.
121 *
1da177e4 122 */
51a0f0f6 123char *strncpy(char *dest, const char *src, size_t count)
1da177e4
LT
124{
125 char *tmp = dest;
126
127 while (count) {
51a0f0f6
JJ
128 if ((*tmp = *src) != 0)
129 src++;
1da177e4
LT
130 tmp++;
131 count--;
132 }
133 return dest;
134}
135EXPORT_SYMBOL(strncpy);
136#endif
137
138#ifndef __HAVE_ARCH_STRLCPY
139/**
140 * strlcpy - Copy a %NUL terminated string into a sized buffer
141 * @dest: Where to copy the string to
142 * @src: Where to copy the string from
143 * @size: size of destination buffer
144 *
145 * Compatible with *BSD: the result is always a valid
146 * NUL-terminated string that fits in the buffer (unless,
147 * of course, the buffer size is zero). It does not pad
148 * out the result like strncpy() does.
149 */
150size_t strlcpy(char *dest, const char *src, size_t size)
151{
152 size_t ret = strlen(src);
153
154 if (size) {
51a0f0f6 155 size_t len = (ret >= size) ? size - 1 : ret;
1da177e4
LT
156 memcpy(dest, src, len);
157 dest[len] = '\0';
158 }
159 return ret;
160}
161EXPORT_SYMBOL(strlcpy);
162#endif
163
164#ifndef __HAVE_ARCH_STRCAT
165/**
166 * strcat - Append one %NUL-terminated string to another
167 * @dest: The string to be appended to
168 * @src: The string to append to it
169 */
0c28130b 170#undef strcat
51a0f0f6 171char *strcat(char *dest, const char *src)
1da177e4
LT
172{
173 char *tmp = dest;
174
175 while (*dest)
176 dest++;
177 while ((*dest++ = *src++) != '\0')
178 ;
1da177e4
LT
179 return tmp;
180}
181EXPORT_SYMBOL(strcat);
182#endif
183
184#ifndef __HAVE_ARCH_STRNCAT
185/**
186 * strncat - Append a length-limited, %NUL-terminated string to another
187 * @dest: The string to be appended to
188 * @src: The string to append to it
189 * @count: The maximum numbers of bytes to copy
190 *
72fd4a35 191 * Note that in contrast to strncpy(), strncat() ensures the result is
1da177e4
LT
192 * terminated.
193 */
51a0f0f6 194char *strncat(char *dest, const char *src, size_t count)
1da177e4
LT
195{
196 char *tmp = dest;
197
198 if (count) {
199 while (*dest)
200 dest++;
201 while ((*dest++ = *src++) != 0) {
202 if (--count == 0) {
203 *dest = '\0';
204 break;
205 }
206 }
207 }
1da177e4
LT
208 return tmp;
209}
210EXPORT_SYMBOL(strncat);
211#endif
212
213#ifndef __HAVE_ARCH_STRLCAT
214/**
215 * strlcat - Append a length-limited, %NUL-terminated string to another
216 * @dest: The string to be appended to
217 * @src: The string to append to it
218 * @count: The size of the destination buffer.
219 */
220size_t strlcat(char *dest, const char *src, size_t count)
221{
222 size_t dsize = strlen(dest);
223 size_t len = strlen(src);
224 size_t res = dsize + len;
225
226 /* This would be a bug */
227 BUG_ON(dsize >= count);
228
229 dest += dsize;
230 count -= dsize;
231 if (len >= count)
232 len = count-1;
233 memcpy(dest, src, len);
234 dest[len] = 0;
235 return res;
236}
237EXPORT_SYMBOL(strlcat);
238#endif
239
240#ifndef __HAVE_ARCH_STRCMP
241/**
242 * strcmp - Compare two strings
243 * @cs: One string
244 * @ct: Another string
245 */
0c28130b 246#undef strcmp
51a0f0f6 247int strcmp(const char *cs, const char *ct)
1da177e4 248{
a414f01a 249 unsigned char c1, c2;
1da177e4
LT
250
251 while (1) {
a414f01a
LT
252 c1 = *cs++;
253 c2 = *ct++;
254 if (c1 != c2)
255 return c1 < c2 ? -1 : 1;
256 if (!c1)
1da177e4
LT
257 break;
258 }
a414f01a 259 return 0;
1da177e4
LT
260}
261EXPORT_SYMBOL(strcmp);
262#endif
263
264#ifndef __HAVE_ARCH_STRNCMP
265/**
266 * strncmp - Compare two length-limited strings
267 * @cs: One string
268 * @ct: Another string
269 * @count: The maximum number of bytes to compare
270 */
51a0f0f6 271int strncmp(const char *cs, const char *ct, size_t count)
1da177e4 272{
a414f01a 273 unsigned char c1, c2;
1da177e4
LT
274
275 while (count) {
a414f01a
LT
276 c1 = *cs++;
277 c2 = *ct++;
278 if (c1 != c2)
279 return c1 < c2 ? -1 : 1;
280 if (!c1)
1da177e4
LT
281 break;
282 count--;
283 }
a414f01a 284 return 0;
1da177e4
LT
285}
286EXPORT_SYMBOL(strncmp);
287#endif
288
289#ifndef __HAVE_ARCH_STRCHR
290/**
291 * strchr - Find the first occurrence of a character in a string
292 * @s: The string to be searched
293 * @c: The character to search for
294 */
51a0f0f6 295char *strchr(const char *s, int c)
1da177e4 296{
51a0f0f6 297 for (; *s != (char)c; ++s)
1da177e4
LT
298 if (*s == '\0')
299 return NULL;
51a0f0f6 300 return (char *)s;
1da177e4
LT
301}
302EXPORT_SYMBOL(strchr);
303#endif
304
305#ifndef __HAVE_ARCH_STRRCHR
306/**
307 * strrchr - Find the last occurrence of a character in a string
308 * @s: The string to be searched
309 * @c: The character to search for
310 */
51a0f0f6 311char *strrchr(const char *s, int c)
1da177e4
LT
312{
313 const char *p = s + strlen(s);
314 do {
315 if (*p == (char)c)
316 return (char *)p;
317 } while (--p >= s);
318 return NULL;
319}
320EXPORT_SYMBOL(strrchr);
321#endif
322
323#ifndef __HAVE_ARCH_STRNCHR
324/**
325 * strnchr - Find a character in a length limited string
326 * @s: The string to be searched
327 * @count: The number of characters to be searched
328 * @c: The character to search for
329 */
330char *strnchr(const char *s, size_t count, int c)
331{
332 for (; count-- && *s != '\0'; ++s)
51a0f0f6
JJ
333 if (*s == (char)c)
334 return (char *)s;
1da177e4
LT
335 return NULL;
336}
337EXPORT_SYMBOL(strnchr);
338#endif
339
f653398c
AGR
340/**
341 * skip_spaces - Removes leading whitespace from @s.
342 * @s: The string to be stripped.
343 *
344 * Returns a pointer to the first non-whitespace character in @s.
345 */
346char *skip_spaces(const char *str)
347{
348 while (isspace(*str))
349 ++str;
350 return (char *)str;
351}
352EXPORT_SYMBOL(skip_spaces);
353
481fad48
PE
354/**
355 * strstrip - Removes leading and trailing whitespace from @s.
356 * @s: The string to be stripped.
357 *
358 * Note that the first trailing whitespace is replaced with a %NUL-terminator
359 * in the given string @s. Returns a pointer to the first non-whitespace
360 * character in @s.
361 */
362char *strstrip(char *s)
363{
364 size_t size;
365 char *end;
366
84c95c9a 367 s = skip_spaces(s);
481fad48 368 size = strlen(s);
481fad48
PE
369 if (!size)
370 return s;
371
372 end = s + size - 1;
6e6d9fa6 373 while (end >= s && isspace(*end))
481fad48
PE
374 end--;
375 *(end + 1) = '\0';
376
84c95c9a 377 return s;
481fad48
PE
378}
379EXPORT_SYMBOL(strstrip);
380
1da177e4
LT
381#ifndef __HAVE_ARCH_STRLEN
382/**
383 * strlen - Find the length of a string
384 * @s: The string to be sized
385 */
51a0f0f6 386size_t strlen(const char *s)
1da177e4
LT
387{
388 const char *sc;
389
390 for (sc = s; *sc != '\0'; ++sc)
391 /* nothing */;
392 return sc - s;
393}
394EXPORT_SYMBOL(strlen);
395#endif
396
397#ifndef __HAVE_ARCH_STRNLEN
398/**
399 * strnlen - Find the length of a length-limited string
400 * @s: The string to be sized
401 * @count: The maximum number of bytes to search
402 */
51a0f0f6 403size_t strnlen(const char *s, size_t count)
1da177e4
LT
404{
405 const char *sc;
406
407 for (sc = s; count-- && *sc != '\0'; ++sc)
408 /* nothing */;
409 return sc - s;
410}
411EXPORT_SYMBOL(strnlen);
412#endif
413
414#ifndef __HAVE_ARCH_STRSPN
415/**
72fd4a35 416 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
1da177e4
LT
417 * @s: The string to be searched
418 * @accept: The string to search for
419 */
420size_t strspn(const char *s, const char *accept)
421{
422 const char *p;
423 const char *a;
424 size_t count = 0;
425
426 for (p = s; *p != '\0'; ++p) {
427 for (a = accept; *a != '\0'; ++a) {
428 if (*p == *a)
429 break;
430 }
431 if (*a == '\0')
432 return count;
433 ++count;
434 }
1da177e4
LT
435 return count;
436}
437
438EXPORT_SYMBOL(strspn);
439#endif
440
8833d328 441#ifndef __HAVE_ARCH_STRCSPN
1da177e4 442/**
72fd4a35 443 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
1da177e4
LT
444 * @s: The string to be searched
445 * @reject: The string to avoid
446 */
447size_t strcspn(const char *s, const char *reject)
448{
449 const char *p;
450 const char *r;
451 size_t count = 0;
452
453 for (p = s; *p != '\0'; ++p) {
454 for (r = reject; *r != '\0'; ++r) {
455 if (*p == *r)
456 return count;
457 }
458 ++count;
459 }
1da177e4 460 return count;
51a0f0f6 461}
1da177e4 462EXPORT_SYMBOL(strcspn);
8833d328 463#endif
1da177e4
LT
464
465#ifndef __HAVE_ARCH_STRPBRK
466/**
467 * strpbrk - Find the first occurrence of a set of characters
468 * @cs: The string to be searched
469 * @ct: The characters to search for
470 */
51a0f0f6 471char *strpbrk(const char *cs, const char *ct)
1da177e4 472{
51a0f0f6 473 const char *sc1, *sc2;
1da177e4 474
51a0f0f6
JJ
475 for (sc1 = cs; *sc1 != '\0'; ++sc1) {
476 for (sc2 = ct; *sc2 != '\0'; ++sc2) {
1da177e4 477 if (*sc1 == *sc2)
51a0f0f6 478 return (char *)sc1;
1da177e4
LT
479 }
480 }
481 return NULL;
482}
894b5779 483EXPORT_SYMBOL(strpbrk);
1da177e4
LT
484#endif
485
486#ifndef __HAVE_ARCH_STRSEP
487/**
488 * strsep - Split a string into tokens
489 * @s: The string to be searched
490 * @ct: The characters to search for
491 *
492 * strsep() updates @s to point after the token, ready for the next call.
493 *
494 * It returns empty tokens, too, behaving exactly like the libc function
495 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
496 * Same semantics, slimmer shape. ;)
497 */
51a0f0f6 498char *strsep(char **s, const char *ct)
1da177e4 499{
51a0f0f6
JJ
500 char *sbegin = *s;
501 char *end;
1da177e4
LT
502
503 if (sbegin == NULL)
504 return NULL;
505
506 end = strpbrk(sbegin, ct);
507 if (end)
508 *end++ = '\0';
509 *s = end;
1da177e4
LT
510 return sbegin;
511}
1da177e4
LT
512EXPORT_SYMBOL(strsep);
513#endif
514
34990cf7
DB
515/**
516 * sysfs_streq - return true if strings are equal, modulo trailing newline
517 * @s1: one string
518 * @s2: another string
519 *
520 * This routine returns true iff two strings are equal, treating both
521 * NUL and newline-then-NUL as equivalent string terminations. It's
522 * geared for use with sysfs input strings, which generally terminate
523 * with newlines but are compared against values without newlines.
524 */
525bool sysfs_streq(const char *s1, const char *s2)
526{
527 while (*s1 && *s1 == *s2) {
528 s1++;
529 s2++;
530 }
531
532 if (*s1 == *s2)
533 return true;
534 if (!*s1 && *s2 == '\n' && !s2[1])
535 return true;
536 if (*s1 == '\n' && !s1[1] && !*s2)
537 return true;
538 return false;
539}
540EXPORT_SYMBOL(sysfs_streq);
541
1da177e4
LT
542#ifndef __HAVE_ARCH_MEMSET
543/**
544 * memset - Fill a region of memory with the given value
545 * @s: Pointer to the start of the area.
546 * @c: The byte to fill the area with
547 * @count: The size of the area.
548 *
549 * Do not use memset() to access IO space, use memset_io() instead.
550 */
51a0f0f6 551void *memset(void *s, int c, size_t count)
1da177e4 552{
850b9247 553 char *xs = s;
1da177e4
LT
554
555 while (count--)
556 *xs++ = c;
1da177e4
LT
557 return s;
558}
559EXPORT_SYMBOL(memset);
560#endif
561
562#ifndef __HAVE_ARCH_MEMCPY
563/**
564 * memcpy - Copy one area of memory to another
565 * @dest: Where to copy to
566 * @src: Where to copy from
567 * @count: The size of the area.
568 *
569 * You should not use this function to access IO space, use memcpy_toio()
570 * or memcpy_fromio() instead.
571 */
51a0f0f6 572void *memcpy(void *dest, const void *src, size_t count)
1da177e4 573{
850b9247 574 char *tmp = dest;
4c416ab7 575 const char *s = src;
1da177e4
LT
576
577 while (count--)
578 *tmp++ = *s++;
1da177e4
LT
579 return dest;
580}
581EXPORT_SYMBOL(memcpy);
582#endif
583
584#ifndef __HAVE_ARCH_MEMMOVE
585/**
586 * memmove - Copy one area of memory to another
587 * @dest: Where to copy to
588 * @src: Where to copy from
589 * @count: The size of the area.
590 *
591 * Unlike memcpy(), memmove() copes with overlapping areas.
592 */
51a0f0f6 593void *memmove(void *dest, const void *src, size_t count)
1da177e4 594{
82da2c37
PJ
595 char *tmp;
596 const char *s;
1da177e4
LT
597
598 if (dest <= src) {
850b9247
JJ
599 tmp = dest;
600 s = src;
1da177e4
LT
601 while (count--)
602 *tmp++ = *s++;
51a0f0f6 603 } else {
850b9247
JJ
604 tmp = dest;
605 tmp += count;
606 s = src;
607 s += count;
1da177e4
LT
608 while (count--)
609 *--tmp = *--s;
51a0f0f6 610 }
1da177e4
LT
611 return dest;
612}
613EXPORT_SYMBOL(memmove);
614#endif
615
616#ifndef __HAVE_ARCH_MEMCMP
617/**
618 * memcmp - Compare two areas of memory
619 * @cs: One area of memory
620 * @ct: Another area of memory
621 * @count: The size of the area.
622 */
0c28130b 623#undef memcmp
51a0f0f6 624int memcmp(const void *cs, const void *ct, size_t count)
1da177e4
LT
625{
626 const unsigned char *su1, *su2;
627 int res = 0;
628
51a0f0f6 629 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
1da177e4
LT
630 if ((res = *su1 - *su2) != 0)
631 break;
632 return res;
633}
634EXPORT_SYMBOL(memcmp);
635#endif
636
637#ifndef __HAVE_ARCH_MEMSCAN
638/**
639 * memscan - Find a character in an area of memory.
640 * @addr: The memory area
641 * @c: The byte to search for
642 * @size: The size of the area.
643 *
644 * returns the address of the first occurrence of @c, or 1 byte past
645 * the area if @c is not found
646 */
51a0f0f6 647void *memscan(void *addr, int c, size_t size)
1da177e4 648{
850b9247 649 unsigned char *p = addr;
1da177e4
LT
650
651 while (size) {
652 if (*p == c)
51a0f0f6 653 return (void *)p;
1da177e4
LT
654 p++;
655 size--;
656 }
51a0f0f6 657 return (void *)p;
1da177e4
LT
658}
659EXPORT_SYMBOL(memscan);
660#endif
661
662#ifndef __HAVE_ARCH_STRSTR
663/**
664 * strstr - Find the first substring in a %NUL terminated string
665 * @s1: The string to be searched
666 * @s2: The string to search for
667 */
51a0f0f6 668char *strstr(const char *s1, const char *s2)
1da177e4
LT
669{
670 int l1, l2;
671
672 l2 = strlen(s2);
673 if (!l2)
51a0f0f6 674 return (char *)s1;
1da177e4
LT
675 l1 = strlen(s1);
676 while (l1 >= l2) {
677 l1--;
51a0f0f6
JJ
678 if (!memcmp(s1, s2, l2))
679 return (char *)s1;
1da177e4
LT
680 s1++;
681 }
682 return NULL;
683}
684EXPORT_SYMBOL(strstr);
685#endif
686
687#ifndef __HAVE_ARCH_MEMCHR
688/**
689 * memchr - Find a character in an area of memory.
690 * @s: The memory area
691 * @c: The byte to search for
692 * @n: The size of the area.
693 *
694 * returns the address of the first occurrence of @c, or %NULL
695 * if @c is not found
696 */
697void *memchr(const void *s, int c, size_t n)
698{
699 const unsigned char *p = s;
700 while (n-- != 0) {
701 if ((unsigned char)c == *p++) {
51a0f0f6 702 return (void *)(p - 1);
1da177e4
LT
703 }
704 }
705 return NULL;
706}
707EXPORT_SYMBOL(memchr);
708#endif