Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 42 | c1 = *s1; |
| 43 | c2 = *s2; |
| 44 | s1++; |
| 45 | s2++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 69 | #undef strcpy |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 70 | char *strcpy(char *dest, const char *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
walter harms | 2527952 | 2005-05-05 16:16:20 -0700 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 95 | char *strncpy(char *dest, const char *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
| 97 | char *tmp = dest; |
| 98 | |
| 99 | while (count) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 100 | if ((*tmp = *src) != 0) |
| 101 | src++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 127 | size_t len = (ret >= size) ? size - 1 : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 142 | #undef strcat |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 143 | char *strcat(char *dest, const char *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 167 | char *strncat(char *dest, const char *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 219 | #undef strcmp |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 220 | int strcmp(const char *cs, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
| 222 | register signed char __res; |
| 223 | |
| 224 | while (1) { |
| 225 | if ((__res = *cs - *ct++) != 0 || !*cs++) |
| 226 | break; |
| 227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 240 | int strncmp(const char *cs, const char *ct, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | register signed char __res = 0; |
| 243 | |
| 244 | while (count) { |
| 245 | if ((__res = *cs - *ct++) != 0 || !*cs++) |
| 246 | break; |
| 247 | count--; |
| 248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 260 | char *strchr(const char *s, int c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 262 | for (; *s != (char)c; ++s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (*s == '\0') |
| 264 | return NULL; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 265 | return (char *)s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 276 | char *strrchr(const char *s, int c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 298 | if (*s == (char)c) |
| 299 | return (char *)s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 310 | size_t strlen(const char *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 327 | size_t strnlen(const char *s, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return count; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 395 | char *strpbrk(const char *cs, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 397 | const char *sc1, *sc2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 399 | for (sc1 = cs; *sc1 != '\0'; ++sc1) { |
| 400 | for (sc2 = ct; *sc2 != '\0'; ++sc2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | if (*sc1 == *sc2) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 402 | return (char *)sc1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 422 | char *strsep(char **s, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 424 | char *sbegin = *s; |
| 425 | char *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | if (sbegin == NULL) |
| 428 | return NULL; |
| 429 | |
| 430 | end = strpbrk(sbegin, ct); |
| 431 | if (end) |
| 432 | *end++ = '\0'; |
| 433 | *s = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return sbegin; |
| 435 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 448 | void *memset(void *s, int c, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 450 | char *xs = (char *)s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | while (count--) |
| 453 | *xs++ = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 469 | void *memcpy(void *dest, const void *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 471 | char *tmp = (char *)dest; |
| 472 | char *s = (char *)src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
| 474 | while (count--) |
| 475 | *tmp++ = *s++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 490 | void *memmove(void *dest, const void *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
| 492 | char *tmp, *s; |
| 493 | |
| 494 | if (dest <= src) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 495 | tmp = (char *)dest; |
| 496 | s = (char *)src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | while (count--) |
| 498 | *tmp++ = *s++; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 499 | } else { |
| 500 | tmp = (char *)dest + count; |
| 501 | s = (char *)src + count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | while (count--) |
| 503 | *--tmp = *--s; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 517 | #undef memcmp |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 518 | int memcmp(const void *cs, const void *ct, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | { |
| 520 | const unsigned char *su1, *su2; |
| 521 | int res = 0; |
| 522 | |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 523 | for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 541 | void *memscan(void *addr, int c, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 543 | unsigned char *p = (unsigned char *)addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
| 545 | while (size) { |
| 546 | if (*p == c) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 547 | return (void *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | p++; |
| 549 | size--; |
| 550 | } |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 551 | return (void *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 562 | char *strstr(const char *s1, const char *s2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
| 564 | int l1, l2; |
| 565 | |
| 566 | l2 = strlen(s2); |
| 567 | if (!l2) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 568 | return (char *)s1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | l1 = strlen(s1); |
| 570 | while (l1 >= l2) { |
| 571 | l1--; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 572 | if (!memcmp(s1, s2, l2)) |
| 573 | return (char *)s1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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++) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame^] | 596 | return (void *)(p - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | return NULL; |
| 600 | } |
| 601 | EXPORT_SYMBOL(memchr); |
| 602 | #endif |