blob: 3181e267a033deb85a7c0d2bb375a35083cb260a [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>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050025#include <linux/kernel.h>
26#include <linux/export.h>
Paul Gortmaker50af5ea2012-01-20 18:35:53 -050027#include <linux/bug.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050028#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Rasmus Villemoescd514e72014-10-13 15:54:25 -070030#ifndef __HAVE_ARCH_STRNCASECMP
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/**
Rasmus Villemoescd514e72014-10-13 15:54:25 -070032 * strncasecmp - Case insensitive, length-limited string comparison
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * @s1: One string
34 * @s2: The other string
35 * @len: the maximum number of characters to compare
36 */
Rasmus Villemoescd514e72014-10-13 15:54:25 -070037int strncasecmp(const char *s1, const char *s2, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 /* Yes, Virginia, it had better be unsigned */
40 unsigned char c1, c2;
41
André Goddard Rosaa11d2b62010-03-05 13:43:11 -080042 if (!len)
43 return 0;
44
45 do {
46 c1 = *s1++;
47 c2 = *s2++;
48 if (!c1 || !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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return (int)c1 - (int)c2;
58}
Rasmus Villemoescd514e72014-10-13 15:54:25 -070059EXPORT_SYMBOL(strncasecmp);
60#endif
61#ifndef __HAVE_ARCH_STRNICMP
62int strnicmp(const char *s1, const char *s2, size_t len)
63{
64 return strncasecmp(s1, s2, len);
65}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066EXPORT_SYMBOL(strnicmp);
67#endif
68
David S. Millerded220b2007-03-29 01:18:42 -070069#ifndef __HAVE_ARCH_STRCASECMP
70int strcasecmp(const char *s1, const char *s2)
71{
72 int c1, c2;
73
74 do {
75 c1 = tolower(*s1++);
76 c2 = tolower(*s2++);
77 } while (c1 == c2 && c1 != 0);
78 return c1 - c2;
79}
80EXPORT_SYMBOL(strcasecmp);
81#endif
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#ifndef __HAVE_ARCH_STRCPY
84/**
85 * strcpy - Copy a %NUL terminated string
86 * @dest: Where to copy the string to
87 * @src: Where to copy the string from
88 */
Paolo 'Blaisorblade' Giarrusso0c281302005-05-05 16:15:17 -070089#undef strcpy
Jesper Juhl51a0f0f2005-10-30 15:02:11 -080090char *strcpy(char *dest, const char *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 char *tmp = dest;
93
94 while ((*dest++ = *src++) != '\0')
95 /* nothing */;
96 return tmp;
97}
98EXPORT_SYMBOL(strcpy);
99#endif
100
101#ifndef __HAVE_ARCH_STRNCPY
102/**
Dan Carpenter0046dd92014-06-04 16:11:47 -0700103 * strncpy - Copy a length-limited, C-string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * @dest: Where to copy the string to
105 * @src: Where to copy the string from
106 * @count: The maximum number of bytes to copy
107 *
108 * The result is not %NUL-terminated if the source exceeds
109 * @count bytes.
walter harms25279522005-05-05 16:16:20 -0700110 *
111 * In the case where the length of @src is less than that of
112 * count, the remainder of @dest will be padded with %NUL.
113 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800115char *strncpy(char *dest, const char *src, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 char *tmp = dest;
118
119 while (count) {
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800120 if ((*tmp = *src) != 0)
121 src++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 tmp++;
123 count--;
124 }
125 return dest;
126}
127EXPORT_SYMBOL(strncpy);
128#endif
129
130#ifndef __HAVE_ARCH_STRLCPY
131/**
Dan Carpenter0046dd92014-06-04 16:11:47 -0700132 * strlcpy - Copy a C-string into a sized buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * @dest: Where to copy the string to
134 * @src: Where to copy the string from
135 * @size: size of destination buffer
136 *
137 * Compatible with *BSD: the result is always a valid
138 * NUL-terminated string that fits in the buffer (unless,
139 * of course, the buffer size is zero). It does not pad
140 * out the result like strncpy() does.
141 */
142size_t strlcpy(char *dest, const char *src, size_t size)
143{
144 size_t ret = strlen(src);
145
146 if (size) {
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800147 size_t len = (ret >= size) ? size - 1 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 memcpy(dest, src, len);
149 dest[len] = '\0';
150 }
151 return ret;
152}
153EXPORT_SYMBOL(strlcpy);
154#endif
155
156#ifndef __HAVE_ARCH_STRCAT
157/**
158 * strcat - Append one %NUL-terminated string to another
159 * @dest: The string to be appended to
160 * @src: The string to append to it
161 */
Paolo 'Blaisorblade' Giarrusso0c281302005-05-05 16:15:17 -0700162#undef strcat
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800163char *strcat(char *dest, const char *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 char *tmp = dest;
166
167 while (*dest)
168 dest++;
169 while ((*dest++ = *src++) != '\0')
170 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return tmp;
172}
173EXPORT_SYMBOL(strcat);
174#endif
175
176#ifndef __HAVE_ARCH_STRNCAT
177/**
Dan Carpenter0046dd92014-06-04 16:11:47 -0700178 * strncat - Append a length-limited, C-string to another
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * @dest: The string to be appended to
180 * @src: The string to append to it
181 * @count: The maximum numbers of bytes to copy
182 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800183 * Note that in contrast to strncpy(), strncat() ensures the result is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * terminated.
185 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800186char *strncat(char *dest, const char *src, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 char *tmp = dest;
189
190 if (count) {
191 while (*dest)
192 dest++;
193 while ((*dest++ = *src++) != 0) {
194 if (--count == 0) {
195 *dest = '\0';
196 break;
197 }
198 }
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return tmp;
201}
202EXPORT_SYMBOL(strncat);
203#endif
204
205#ifndef __HAVE_ARCH_STRLCAT
206/**
Dan Carpenter0046dd92014-06-04 16:11:47 -0700207 * strlcat - Append a length-limited, C-string to another
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * @dest: The string to be appended to
209 * @src: The string to append to it
210 * @count: The size of the destination buffer.
211 */
212size_t strlcat(char *dest, const char *src, size_t count)
213{
214 size_t dsize = strlen(dest);
215 size_t len = strlen(src);
216 size_t res = dsize + len;
217
218 /* This would be a bug */
219 BUG_ON(dsize >= count);
220
221 dest += dsize;
222 count -= dsize;
223 if (len >= count)
224 len = count-1;
225 memcpy(dest, src, len);
226 dest[len] = 0;
227 return res;
228}
229EXPORT_SYMBOL(strlcat);
230#endif
231
232#ifndef __HAVE_ARCH_STRCMP
233/**
234 * strcmp - Compare two strings
235 * @cs: One string
236 * @ct: Another string
237 */
Paolo 'Blaisorblade' Giarrusso0c281302005-05-05 16:15:17 -0700238#undef strcmp
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800239int strcmp(const char *cs, const char *ct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Linus Torvaldsa414f012009-11-18 22:31:52 +0100241 unsigned char c1, c2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 while (1) {
Linus Torvaldsa414f012009-11-18 22:31:52 +0100244 c1 = *cs++;
245 c2 = *ct++;
246 if (c1 != c2)
247 return c1 < c2 ? -1 : 1;
248 if (!c1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 break;
250 }
Linus Torvaldsa414f012009-11-18 22:31:52 +0100251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253EXPORT_SYMBOL(strcmp);
254#endif
255
256#ifndef __HAVE_ARCH_STRNCMP
257/**
258 * strncmp - Compare two length-limited strings
259 * @cs: One string
260 * @ct: Another string
261 * @count: The maximum number of bytes to compare
262 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800263int strncmp(const char *cs, const char *ct, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Linus Torvaldsa414f012009-11-18 22:31:52 +0100265 unsigned char c1, c2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 while (count) {
Linus Torvaldsa414f012009-11-18 22:31:52 +0100268 c1 = *cs++;
269 c2 = *ct++;
270 if (c1 != c2)
271 return c1 < c2 ? -1 : 1;
272 if (!c1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 break;
274 count--;
275 }
Linus Torvaldsa414f012009-11-18 22:31:52 +0100276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278EXPORT_SYMBOL(strncmp);
279#endif
280
281#ifndef __HAVE_ARCH_STRCHR
282/**
283 * strchr - Find the first occurrence of a character in a string
284 * @s: The string to be searched
285 * @c: The character to search for
286 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800287char *strchr(const char *s, int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800289 for (; *s != (char)c; ++s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (*s == '\0')
291 return NULL;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800292 return (char *)s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294EXPORT_SYMBOL(strchr);
295#endif
296
Grant Likely11d200e2014-03-14 17:00:14 +0000297#ifndef __HAVE_ARCH_STRCHRNUL
298/**
299 * strchrnul - Find and return a character in a string, or end of string
300 * @s: The string to be searched
301 * @c: The character to search for
302 *
303 * Returns pointer to first occurrence of 'c' in s. If c is not found, then
304 * return a pointer to the null byte at the end of s.
305 */
306char *strchrnul(const char *s, int c)
307{
308 while (*s && *s != (char)c)
309 s++;
310 return (char *)s;
311}
312EXPORT_SYMBOL(strchrnul);
313#endif
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#ifndef __HAVE_ARCH_STRRCHR
316/**
317 * strrchr - Find the last occurrence of a character in a string
318 * @s: The string to be searched
319 * @c: The character to search for
320 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800321char *strrchr(const char *s, int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 const char *p = s + strlen(s);
324 do {
325 if (*p == (char)c)
326 return (char *)p;
327 } while (--p >= s);
328 return NULL;
329}
330EXPORT_SYMBOL(strrchr);
331#endif
332
333#ifndef __HAVE_ARCH_STRNCHR
334/**
335 * strnchr - Find a character in a length limited string
336 * @s: The string to be searched
337 * @count: The number of characters to be searched
338 * @c: The character to search for
339 */
340char *strnchr(const char *s, size_t count, int c)
341{
342 for (; count-- && *s != '\0'; ++s)
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800343 if (*s == (char)c)
344 return (char *)s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return NULL;
346}
347EXPORT_SYMBOL(strnchr);
348#endif
349
Pekka Enberg481fad42006-06-23 02:05:44 -0700350/**
Randy Dunlapa6cd13f2009-12-21 14:37:22 -0800351 * skip_spaces - Removes leading whitespace from @str.
352 * @str: The string to be stripped.
André Goddard Rosaf6533982009-12-14 18:01:04 -0800353 *
Randy Dunlapa6cd13f2009-12-21 14:37:22 -0800354 * Returns a pointer to the first non-whitespace character in @str.
André Goddard Rosaf6533982009-12-14 18:01:04 -0800355 */
356char *skip_spaces(const char *str)
357{
358 while (isspace(*str))
359 ++str;
360 return (char *)str;
361}
362EXPORT_SYMBOL(skip_spaces);
363
364/**
KOSAKI Motohiroca54cb82009-12-14 18:01:15 -0800365 * strim - Removes leading and trailing whitespace from @s.
Pekka Enberg481fad42006-06-23 02:05:44 -0700366 * @s: The string to be stripped.
367 *
368 * Note that the first trailing whitespace is replaced with a %NUL-terminator
369 * in the given string @s. Returns a pointer to the first non-whitespace
370 * character in @s.
371 */
KOSAKI Motohiroca54cb82009-12-14 18:01:15 -0800372char *strim(char *s)
Pekka Enberg481fad42006-06-23 02:05:44 -0700373{
374 size_t size;
375 char *end;
376
377 size = strlen(s);
Pekka Enberg481fad42006-06-23 02:05:44 -0700378 if (!size)
379 return s;
380
381 end = s + size - 1;
Michael Holzheu6e6d9fa2006-10-28 10:38:47 -0700382 while (end >= s && isspace(*end))
Pekka Enberg481fad42006-06-23 02:05:44 -0700383 end--;
384 *(end + 1) = '\0';
385
Michael Holzheu66f69582011-10-31 17:12:37 -0700386 return skip_spaces(s);
Pekka Enberg481fad42006-06-23 02:05:44 -0700387}
KOSAKI Motohiroca54cb82009-12-14 18:01:15 -0800388EXPORT_SYMBOL(strim);
Pekka Enberg481fad42006-06-23 02:05:44 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#ifndef __HAVE_ARCH_STRLEN
391/**
392 * strlen - Find the length of a string
393 * @s: The string to be sized
394 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800395size_t strlen(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 const char *sc;
398
399 for (sc = s; *sc != '\0'; ++sc)
400 /* nothing */;
401 return sc - s;
402}
403EXPORT_SYMBOL(strlen);
404#endif
405
406#ifndef __HAVE_ARCH_STRNLEN
407/**
408 * strnlen - Find the length of a length-limited string
409 * @s: The string to be sized
410 * @count: The maximum number of bytes to search
411 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800412size_t strnlen(const char *s, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 const char *sc;
415
416 for (sc = s; count-- && *sc != '\0'; ++sc)
417 /* nothing */;
418 return sc - s;
419}
420EXPORT_SYMBOL(strnlen);
421#endif
422
423#ifndef __HAVE_ARCH_STRSPN
424/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800425 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * @s: The string to be searched
427 * @accept: The string to search for
428 */
429size_t strspn(const char *s, const char *accept)
430{
431 const char *p;
432 const char *a;
433 size_t count = 0;
434
435 for (p = s; *p != '\0'; ++p) {
436 for (a = accept; *a != '\0'; ++a) {
437 if (*p == *a)
438 break;
439 }
440 if (*a == '\0')
441 return count;
442 ++count;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return count;
445}
446
447EXPORT_SYMBOL(strspn);
448#endif
449
Kyle McMartin8833d322006-04-10 22:53:57 -0700450#ifndef __HAVE_ARCH_STRCSPN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800452 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * @s: The string to be searched
454 * @reject: The string to avoid
455 */
456size_t strcspn(const char *s, const char *reject)
457{
458 const char *p;
459 const char *r;
460 size_t count = 0;
461
462 for (p = s; *p != '\0'; ++p) {
463 for (r = reject; *r != '\0'; ++r) {
464 if (*p == *r)
465 return count;
466 }
467 ++count;
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return count;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800470}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471EXPORT_SYMBOL(strcspn);
Kyle McMartin8833d322006-04-10 22:53:57 -0700472#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474#ifndef __HAVE_ARCH_STRPBRK
475/**
476 * strpbrk - Find the first occurrence of a set of characters
477 * @cs: The string to be searched
478 * @ct: The characters to search for
479 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800480char *strpbrk(const char *cs, const char *ct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800482 const char *sc1, *sc2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800484 for (sc1 = cs; *sc1 != '\0'; ++sc1) {
485 for (sc2 = ct; *sc2 != '\0'; ++sc2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (*sc1 == *sc2)
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800487 return (char *)sc1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 }
490 return NULL;
491}
Kyle McMartin894b5772006-04-10 22:53:56 -0700492EXPORT_SYMBOL(strpbrk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493#endif
494
495#ifndef __HAVE_ARCH_STRSEP
496/**
497 * strsep - Split a string into tokens
498 * @s: The string to be searched
499 * @ct: The characters to search for
500 *
501 * strsep() updates @s to point after the token, ready for the next call.
502 *
503 * It returns empty tokens, too, behaving exactly like the libc function
504 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
505 * Same semantics, slimmer shape. ;)
506 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800507char *strsep(char **s, const char *ct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800509 char *sbegin = *s;
510 char *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 if (sbegin == NULL)
513 return NULL;
514
515 end = strpbrk(sbegin, ct);
516 if (end)
517 *end++ = '\0';
518 *s = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return sbegin;
520}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521EXPORT_SYMBOL(strsep);
522#endif
523
David Brownell34990cf2008-05-01 04:34:42 -0700524/**
525 * sysfs_streq - return true if strings are equal, modulo trailing newline
526 * @s1: one string
527 * @s2: another string
528 *
529 * This routine returns true iff two strings are equal, treating both
530 * NUL and newline-then-NUL as equivalent string terminations. It's
531 * geared for use with sysfs input strings, which generally terminate
532 * with newlines but are compared against values without newlines.
533 */
534bool sysfs_streq(const char *s1, const char *s2)
535{
536 while (*s1 && *s1 == *s2) {
537 s1++;
538 s2++;
539 }
540
541 if (*s1 == *s2)
542 return true;
543 if (!*s1 && *s2 == '\n' && !s2[1])
544 return true;
545 if (*s1 == '\n' && !s1[1] && !*s2)
546 return true;
547 return false;
548}
549EXPORT_SYMBOL(sysfs_streq);
550
Jonathan Camerond0f1fed2011-04-19 12:43:45 +0100551/**
552 * strtobool - convert common user inputs into boolean values
553 * @s: input string
554 * @res: result
555 *
556 * This routine returns 0 iff the first character is one of 'Yy1Nn0'.
557 * Otherwise it will return -EINVAL. Value pointed to by res is
558 * updated upon finding a match.
559 */
560int strtobool(const char *s, bool *res)
561{
562 switch (s[0]) {
563 case 'y':
564 case 'Y':
565 case '1':
566 *res = true;
567 break;
568 case 'n':
569 case 'N':
570 case '0':
571 *res = false;
572 break;
573 default:
574 return -EINVAL;
575 }
576 return 0;
577}
578EXPORT_SYMBOL(strtobool);
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580#ifndef __HAVE_ARCH_MEMSET
581/**
582 * memset - Fill a region of memory with the given value
583 * @s: Pointer to the start of the area.
584 * @c: The byte to fill the area with
585 * @count: The size of the area.
586 *
587 * Do not use memset() to access IO space, use memset_io() instead.
588 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800589void *memset(void *s, int c, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Jesper Juhl850b9242005-10-30 15:02:13 -0800591 char *xs = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 while (count--)
594 *xs++ = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return s;
596}
597EXPORT_SYMBOL(memset);
598#endif
599
600#ifndef __HAVE_ARCH_MEMCPY
601/**
602 * memcpy - Copy one area of memory to another
603 * @dest: Where to copy to
604 * @src: Where to copy from
605 * @count: The size of the area.
606 *
607 * You should not use this function to access IO space, use memcpy_toio()
608 * or memcpy_fromio() instead.
609 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800610void *memcpy(void *dest, const void *src, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Jesper Juhl850b9242005-10-30 15:02:13 -0800612 char *tmp = dest;
Jan-Benedict Glaw4c416ab2006-04-10 22:54:09 -0700613 const char *s = src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 while (count--)
616 *tmp++ = *s++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return dest;
618}
619EXPORT_SYMBOL(memcpy);
620#endif
621
622#ifndef __HAVE_ARCH_MEMMOVE
623/**
624 * memmove - Copy one area of memory to another
625 * @dest: Where to copy to
626 * @src: Where to copy from
627 * @count: The size of the area.
628 *
629 * Unlike memcpy(), memmove() copes with overlapping areas.
630 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800631void *memmove(void *dest, const void *src, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Paul Jackson82da2c32005-10-30 15:03:19 -0800633 char *tmp;
634 const char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 if (dest <= src) {
Jesper Juhl850b9242005-10-30 15:02:13 -0800637 tmp = dest;
638 s = src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 while (count--)
640 *tmp++ = *s++;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800641 } else {
Jesper Juhl850b9242005-10-30 15:02:13 -0800642 tmp = dest;
643 tmp += count;
644 s = src;
645 s += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 while (count--)
647 *--tmp = *--s;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return dest;
650}
651EXPORT_SYMBOL(memmove);
652#endif
653
654#ifndef __HAVE_ARCH_MEMCMP
655/**
656 * memcmp - Compare two areas of memory
657 * @cs: One area of memory
658 * @ct: Another area of memory
659 * @count: The size of the area.
660 */
Paolo 'Blaisorblade' Giarrusso0c281302005-05-05 16:15:17 -0700661#undef memcmp
Andi Kleena7330c92014-02-08 08:52:06 +0100662__visible int memcmp(const void *cs, const void *ct, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 const unsigned char *su1, *su2;
665 int res = 0;
666
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800667 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if ((res = *su1 - *su2) != 0)
669 break;
670 return res;
671}
672EXPORT_SYMBOL(memcmp);
673#endif
674
675#ifndef __HAVE_ARCH_MEMSCAN
676/**
677 * memscan - Find a character in an area of memory.
678 * @addr: The memory area
679 * @c: The byte to search for
680 * @size: The size of the area.
681 *
682 * returns the address of the first occurrence of @c, or 1 byte past
683 * the area if @c is not found
684 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800685void *memscan(void *addr, int c, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Jesper Juhl850b9242005-10-30 15:02:13 -0800687 unsigned char *p = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 while (size) {
690 if (*p == c)
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800691 return (void *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 p++;
693 size--;
694 }
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800695 return (void *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697EXPORT_SYMBOL(memscan);
698#endif
699
700#ifndef __HAVE_ARCH_STRSTR
701/**
702 * strstr - Find the first substring in a %NUL terminated string
703 * @s1: The string to be searched
704 * @s2: The string to search for
705 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800706char *strstr(const char *s1, const char *s2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Li Zefand5f1fb52010-01-14 10:53:55 +0800708 size_t l1, l2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 l2 = strlen(s2);
711 if (!l2)
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800712 return (char *)s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 l1 = strlen(s1);
714 while (l1 >= l2) {
715 l1--;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800716 if (!memcmp(s1, s2, l2))
717 return (char *)s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 s1++;
719 }
720 return NULL;
721}
722EXPORT_SYMBOL(strstr);
723#endif
724
Li Zefand5f1fb52010-01-14 10:53:55 +0800725#ifndef __HAVE_ARCH_STRNSTR
726/**
727 * strnstr - Find the first substring in a length-limited string
728 * @s1: The string to be searched
729 * @s2: The string to search for
730 * @len: the maximum number of characters to search
731 */
732char *strnstr(const char *s1, const char *s2, size_t len)
733{
André Goddard Rosad6a2eed2010-03-05 13:43:12 -0800734 size_t l2;
Li Zefand5f1fb52010-01-14 10:53:55 +0800735
736 l2 = strlen(s2);
737 if (!l2)
738 return (char *)s1;
André Goddard Rosad6a2eed2010-03-05 13:43:12 -0800739 while (len >= l2) {
740 len--;
Li Zefand5f1fb52010-01-14 10:53:55 +0800741 if (!memcmp(s1, s2, l2))
742 return (char *)s1;
743 s1++;
744 }
745 return NULL;
746}
747EXPORT_SYMBOL(strnstr);
748#endif
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750#ifndef __HAVE_ARCH_MEMCHR
751/**
752 * memchr - Find a character in an area of memory.
753 * @s: The memory area
754 * @c: The byte to search for
755 * @n: The size of the area.
756 *
757 * returns the address of the first occurrence of @c, or %NULL
758 * if @c is not found
759 */
760void *memchr(const void *s, int c, size_t n)
761{
762 const unsigned char *p = s;
763 while (n-- != 0) {
764 if ((unsigned char)c == *p++) {
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800765 return (void *)(p - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 }
768 return NULL;
769}
770EXPORT_SYMBOL(memchr);
771#endif
Akinobu Mita798248202011-10-31 17:08:07 -0700772
773static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
774{
775 while (bytes) {
776 if (*start != value)
777 return (void *)start;
778 start++;
779 bytes--;
780 }
781 return NULL;
782}
783
784/**
785 * memchr_inv - Find an unmatching character in an area of memory.
786 * @start: The memory area
787 * @c: Find a character other than c
788 * @bytes: The size of the area.
789 *
790 * returns the address of the first character other than @c, or %NULL
791 * if the whole buffer contains just @c.
792 */
793void *memchr_inv(const void *start, int c, size_t bytes)
794{
795 u8 value = c;
796 u64 value64;
797 unsigned int words, prefix;
798
799 if (bytes <= 16)
800 return check_bytes8(start, value, bytes);
801
Akinobu Mitaf43804b2012-03-23 15:02:14 -0700802 value64 = value;
Linus Torvalds72d93102014-09-13 11:14:53 -0700803#if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
Akinobu Mitaf43804b2012-03-23 15:02:14 -0700804 value64 *= 0x0101010101010101;
Linus Torvalds72d93102014-09-13 11:14:53 -0700805#elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
Akinobu Mitaf43804b2012-03-23 15:02:14 -0700806 value64 *= 0x01010101;
807 value64 |= value64 << 32;
808#else
809 value64 |= value64 << 8;
810 value64 |= value64 << 16;
811 value64 |= value64 << 32;
812#endif
Akinobu Mita798248202011-10-31 17:08:07 -0700813
Akinobu Mitaf43804b2012-03-23 15:02:14 -0700814 prefix = (unsigned long)start % 8;
Akinobu Mita798248202011-10-31 17:08:07 -0700815 if (prefix) {
Akinobu Mitaf43804b2012-03-23 15:02:14 -0700816 u8 *r;
817
818 prefix = 8 - prefix;
819 r = check_bytes8(start, value, prefix);
Akinobu Mita798248202011-10-31 17:08:07 -0700820 if (r)
821 return r;
822 start += prefix;
823 bytes -= prefix;
824 }
825
826 words = bytes / 8;
827
828 while (words) {
829 if (*(u64 *)start != value64)
830 return check_bytes8(start, value, 8);
831 start += 8;
832 words--;
833 }
834
835 return check_bytes8(start, value, bytes % 8);
836}
837EXPORT_SYMBOL(memchr_inv);