| 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 = c2 = 0; |
|---|
| 40 |
if (len) { |
|---|
| 41 |
do { |
|---|
| 42 |
c1 = *s1; |
|---|
| 43 |
c2 = *s2; |
|---|
| 44 |
s1++; |
|---|
| 45 |
s2++; |
|---|
| 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 |
} |
|---|
| 60 |
EXPORT_SYMBOL(strnicmp); |
|---|
| 61 |
#endif |
|---|
| 62 |
|
|---|
| 63 |
#ifndef __HAVE_ARCH_STRCASECMP |
|---|
| 64 |
int 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 |
} |
|---|
| 74 |
EXPORT_SYMBOL(strcasecmp); |
|---|
| 75 |
#endif |
|---|
| 76 |
|
|---|
| 77 |
#ifndef __HAVE_ARCH_STRNCASECMP |
|---|
| 78 |
int 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 |
} |
|---|
| 88 |
EXPORT_SYMBOL(strncasecmp); |
|---|
| 89 |
#endif |
|---|
| 90 |
|
|---|
| 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 |
*/ |
|---|
| 97 |
#undef strcpy |
|---|
| 98 |
char *strcpy(char *dest, const char *src) |
|---|
| 99 |
{ |
|---|
| 100 |
char *tmp = dest; |
|---|
| 101 |
|
|---|
| 102 |
while ((*dest++ = *src++) != '\0') |
|---|
| 103 |
/* nothing */; |
|---|
| 104 |
return tmp; |
|---|
| 105 |
} |
|---|
| 106 |
EXPORT_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. |
|---|
| 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 |
* |
|---|
| 122 |
*/ |
|---|
| 123 |
char *strncpy(char *dest, const char *src, size_t count) |
|---|
| 124 |
{ |
|---|
| 125 |
char *tmp = dest; |
|---|
| 126 |
|
|---|
| 127 |
while (count) { |
|---|
| 128 |
if ((*tmp = *src) != 0) |
|---|
| 129 |
src++; |
|---|
| 130 |
tmp++; |
|---|
| 131 |
count--; |
|---|
| 132 |
} |
|---|
| 133 |
return dest; |
|---|
| 134 |
} |
|---|
| 135 |
EXPORT_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 |
*/ |
|---|
| 150 |
size_t strlcpy(char *dest, const char *src, size_t size) |
|---|
| 151 |
{ |
|---|
| 152 |
size_t ret = strlen(src); |
|---|
| 153 |
|
|---|
| 154 |
if (size) { |
|---|
| 155 |
size_t len = (ret >= size) ? size - 1 : ret; |
|---|
| 156 |
memcpy(dest, src, len); |
|---|
| 157 |
dest[len] = '\0'; |
|---|
| 158 |
} |
|---|
| 159 |
return ret; |
|---|
| 160 |
} |
|---|
| 161 |
EXPORT_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 |
*/ |
|---|
| 170 |
#undef strcat |
|---|
| 171 |
char *strcat(char *dest, const char *src) |
|---|
| 172 |
{ |
|---|
| 173 |
char *tmp = dest; |
|---|
| 174 |
|
|---|
| 175 |
while (*dest) |
|---|
| 176 |
dest++; |
|---|
| 177 |
while ((*dest++ = *src++) != '\0') |
|---|
| 178 |
; |
|---|
| 179 |
return tmp; |
|---|
| 180 |
} |
|---|
| 181 |
EXPORT_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 |
* |
|---|
| 191 |
* Note that in contrast to strncpy(), strncat() ensures the result is |
|---|
| 192 |
* terminated. |
|---|
| 193 |
*/ |
|---|
| 194 |
char *strncat(char *dest, const char *src, size_t count) |
|---|
| 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 |
} |
|---|
| 208 |
return tmp; |
|---|
| 209 |
} |
|---|
| 210 |
EXPORT_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 |
*/ |
|---|
| 220 |
size_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 |
} |
|---|
| 237 |
EXPORT_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 |
*/ |
|---|
| 246 |
#undef strcmp |
|---|
| 247 |
int strcmp(const char *cs, const char *ct) |
|---|
| 248 |
{ |
|---|
| 249 |
signed char __res; |
|---|
| 250 |
|
|---|
| 251 |
while (1) { |
|---|
| 252 |
if ((__res = *cs - *ct++) != 0 || !*cs++) |
|---|
| 253 |
break; |
|---|
| 254 |
} |
|---|
| 255 |
return __res; |
|---|
| 256 |
} |
|---|
| 257 |
EXPORT_SYMBOL(strcmp); |
|---|
| 258 |
#endif |
|---|
| 259 |
|
|---|
| 260 |
#ifndef __HAVE_ARCH_STRNCMP |
|---|
| 261 |
/** |
|---|
| 262 |
* strncmp - Compare two length-limited strings |
|---|
| 263 |
* @cs: One string |
|---|
| 264 |
* @ct: Another string |
|---|
| 265 |
* @count: The maximum number of bytes to compare |
|---|
| 266 |
*/ |
|---|
| 267 |
int strncmp(const char *cs, const char *ct, size_t count) |
|---|
| 268 |
{ |
|---|
| 269 |
signed char __res = 0; |
|---|
| 270 |
|
|---|
| 271 |
while (count) { |
|---|
| 272 |
if ((__res = *cs - *ct++) != 0 || !*cs++) |
|---|
| 273 |
break; |
|---|
| 274 |
count--; |
|---|
| 275 |
} |
|---|
| 276 |
return __res; |
|---|
| 277 |
} |
|---|
| 278 |
EXPORT_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 |
*/ |
|---|
| 287 |
char *strchr(const char *s, int c) |
|---|
| 288 |
{ |
|---|
| 289 |
for (; *s != (char)c; ++s) |
|---|
| 290 |
if (*s == '\0') |
|---|
| 291 |
return NULL; |
|---|
| 292 |
return (char *)s; |
|---|
| 293 |
} |
|---|
| 294 |
EXPORT_SYMBOL(strchr); |
|---|
| 295 |
#endif |
|---|
| 296 |
|
|---|
| 297 |
#ifndef __HAVE_ARCH_STRRCHR |
|---|
| 298 |
/** |
|---|
| 299 |
* strrchr - Find the last occurrence of a character in a string |
|---|
| 300 |
* @s: The string to be searched |
|---|
| 301 |
* @c: The character to search for |
|---|
| 302 |
*/ |
|---|
| 303 |
char *strrchr(const char *s, int c) |
|---|
| 304 |
{ |
|---|
| 305 |
const char *p = s + strlen(s); |
|---|
| 306 |
do { |
|---|
| 307 |
if (*p == (char)c) |
|---|
| 308 |
return (char *)p; |
|---|
| 309 |
} while (--p >= s); |
|---|
| 310 |
return NULL; |
|---|
| 311 |
} |
|---|
| 312 |
EXPORT_SYMBOL(strrchr); |
|---|
| 313 |
#endif |
|---|
| 314 |
|
|---|
| 315 |
#ifndef __HAVE_ARCH_STRNCHR |
|---|
| 316 |
/** |
|---|
| 317 |
* strnchr - Find a character in a length limited string |
|---|
| 318 |
* @s: The string to be searched |
|---|
| 319 |
* @count: The number of characters to be searched |
|---|
| 320 |
* @c: The character to search for |
|---|
| 321 |
*/ |
|---|
| 322 |
char *strnchr(const char *s, size_t count, int c) |
|---|
| 323 |
{ |
|---|
| 324 |
for (; count-- && *s != '\0'; ++s) |
|---|
| 325 |
if (*s == (char)c) |
|---|
| 326 |
return (char *)s; |
|---|
| 327 |
return NULL; |
|---|
| 328 |
} |
|---|
| 329 |
EXPORT_SYMBOL(strnchr); |
|---|
| 330 |
#endif |
|---|
| 331 |
|
|---|
| 332 |
/** |
|---|
| 333 |
* strstrip - Removes leading and trailing whitespace from @s. |
|---|
| 334 |
* @s: The string to be stripped. |
|---|
| 335 |
* |
|---|
| 336 |
* Note that the first trailing whitespace is replaced with a %NUL-terminator |
|---|
| 337 |
* in the given string @s. Returns a pointer to the first non-whitespace |
|---|
| 338 |
* character in @s. |
|---|
| 339 |
*/ |
|---|
| 340 |
char *strstrip(char *s) |
|---|
| 341 |
{ |
|---|
| 342 |
size_t size; |
|---|
| 343 |
char *end; |
|---|
| 344 |
|
|---|
| 345 |
size = strlen(s); |
|---|
| 346 |
|
|---|
| 347 |
if (!size) |
|---|
| 348 |
return s; |
|---|
| 349 |
|
|---|
| 350 |
end = s + size - 1; |
|---|
| 351 |
while (end >= s && isspace(*end)) |
|---|
| 352 |
end--; |
|---|
| 353 |
*(end + 1) = '\0'; |
|---|
| 354 |
|
|---|
| 355 |
while (*s && isspace(*s)) |
|---|
| 356 |
s++; |
|---|
| 357 |
|
|---|
| 358 |
return s; |
|---|
| 359 |
} |
|---|
| 360 |
EXPORT_SYMBOL(strstrip); |
|---|
| 361 |
|
|---|
| 362 |
#ifndef __HAVE_ARCH_STRLEN |
|---|
| 363 |
/** |
|---|
| 364 |
* strlen - Find the length of a string |
|---|
| 365 |
* @s: The string to be sized |
|---|
| 366 |
*/ |
|---|
| 367 |
size_t strlen(const char *s) |
|---|
| 368 |
{ |
|---|
| 369 |
const char *sc; |
|---|
| 370 |
|
|---|
| 371 |
for (sc = s; *sc != '\0'; ++sc) |
|---|
| 372 |
/* nothing */; |
|---|
| 373 |
return sc - s; |
|---|
| 374 |
} |
|---|
| 375 |
EXPORT_SYMBOL(strlen); |
|---|
| 376 |
#endif |
|---|
| 377 |
|
|---|
| 378 |
#ifndef __HAVE_ARCH_STRNLEN |
|---|
| 379 |
/** |
|---|
| 380 |
* strnlen - Find the length of a length-limited string |
|---|
| 381 |
* @s: The string to be sized |
|---|
| 382 |
* @count: The maximum number of bytes to search |
|---|
| 383 |
*/ |
|---|
| 384 |
size_t strnlen(const char *s, size_t count) |
|---|
| 385 |
{ |
|---|
| 386 |
const char *sc; |
|---|
| 387 |
|
|---|
| 388 |
for (sc = s; count-- && *sc != '\0'; ++sc) |
|---|
| 389 |
/* nothing */; |
|---|
| 390 |
return sc - s; |
|---|
| 391 |
} |
|---|
| 392 |
EXPORT_SYMBOL(strnlen); |
|---|
| 393 |
#endif |
|---|
| 394 |
|
|---|
| 395 |
#ifndef __HAVE_ARCH_STRSPN |
|---|
| 396 |
/** |
|---|
| 397 |
* strspn - Calculate the length of the initial substring of @s which only contain letters in @accept |
|---|
| 398 |
* @s: The string to be searched |
|---|
| 399 |
* @accept: The string to search for |
|---|
| 400 |
*/ |
|---|
| 401 |
size_t strspn(const char *s, const char *accept) |
|---|
| 402 |
{ |
|---|
| 403 |
const char *p; |
|---|
| 404 |
const char *a; |
|---|
| 405 |
size_t count = 0; |
|---|
| 406 |
|
|---|
| 407 |
for (p = s; *p != '\0'; ++p) { |
|---|
| 408 |
for (a = accept; *a != '\0'; ++a) { |
|---|
| 409 |
if (*p == *a) |
|---|
| 410 |
break; |
|---|
| 411 |
} |
|---|
| 412 |
if (*a == '\0') |
|---|
| 413 |
return count; |
|---|
| 414 |
++count; |
|---|
| 415 |
} |
|---|
| 416 |
return count; |
|---|
| 417 |
} |
|---|
| 418 |
|
|---|
| 419 |
EXPORT_SYMBOL(strspn); |
|---|
| 420 |
#endif |
|---|
| 421 |
|
|---|
| 422 |
#ifndef __HAVE_ARCH_STRCSPN |
|---|
| 423 |
/** |
|---|
| 424 |
* strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject |
|---|
| 425 |
* @s: The string to be searched |
|---|
| 426 |
* @reject: The string to avoid |
|---|
| 427 |
*/ |
|---|
| 428 |
size_t strcspn(const char *s, const char *reject) |
|---|
| 429 |
{ |
|---|
| 430 |
const char *p; |
|---|
| 431 |
const char *r; |
|---|
| 432 |
size_t count = 0; |
|---|
| 433 |
|
|---|
| 434 |
for (p = s; *p != '\0'; ++p) { |
|---|
| 435 |
for (r = reject; *r != '\0'; ++r) { |
|---|
| 436 |
if (*p == *r) |
|---|
| 437 |
return count; |
|---|
| 438 |
} |
|---|
| 439 |
++count; |
|---|
| 440 |
} |
|---|
| 441 |
return count; |
|---|
| 442 |
} |
|---|
| 443 |
EXPORT_SYMBOL(strcspn); |
|---|
| 444 |
#endif |
|---|
| 445 |
|
|---|
| 446 |
#ifndef __HAVE_ARCH_STRPBRK |
|---|
| 447 |
/** |
|---|
| 448 |
* strpbrk - Find the first occurrence of a set of characters |
|---|
| 449 |
* @cs: The string to be searched |
|---|
| 450 |
* @ct: The characters to search for |
|---|
| 451 |
*/ |
|---|
| 452 |
char *strpbrk(const char *cs, const char *ct) |
|---|
| 453 |
{ |
|---|
| 454 |
const char *sc1, *sc2; |
|---|
| 455 |
|
|---|
| 456 |
for (sc1 = cs; *sc1 != '\0'; ++sc1) { |
|---|
| 457 |
for (sc2 = ct; *sc2 != '\0'; ++sc2) { |
|---|
| 458 |
if (*sc1 == *sc2) |
|---|
| 459 |
return (char *)sc1; |
|---|
| 460 |
} |
|---|
| 461 |
} |
|---|
| 462 |
return NULL; |
|---|
| 463 |
} |
|---|
| 464 |
EXPORT_SYMBOL(strpbrk); |
|---|
| 465 |
#endif |
|---|
| 466 |
|
|---|
| 467 |
#ifndef __HAVE_ARCH_STRSEP |
|---|
| 468 |
/** |
|---|
| 469 |
* strsep - Split a string into tokens |
|---|
| 470 |
* @s: The string to be searched |
|---|
| 471 |
* @ct: The characters to search for |
|---|
| 472 |
* |
|---|
| 473 |
* strsep() updates @s to point after the token, ready for the next call. |
|---|
| 474 |
* |
|---|
| 475 |
* It returns empty tokens, too, behaving exactly like the libc function |
|---|
| 476 |
* of that name. In fact, it was stolen from glibc2 and de-fancy-fied. |
|---|
| 477 |
* Same semantics, slimmer shape. ;) |
|---|
| 478 |
*/ |
|---|
| 479 |
char *strsep(char **s, const char *ct) |
|---|
| 480 |
{ |
|---|
| 481 |
char *sbegin = *s; |
|---|
| 482 |
char *end; |
|---|
| 483 |
|
|---|
| 484 |
if (sbegin == NULL) |
|---|
| 485 |
return NULL; |
|---|
| 486 |
|
|---|
| 487 |
end = strpbrk(sbegin, ct); |
|---|
| 488 |
if (end) |
|---|
| 489 |
*end++ = '\0'; |
|---|
| 490 |
*s = end; |
|---|
| 491 |
return sbegin; |
|---|
| 492 |
} |
|---|
| 493 |
EXPORT_SYMBOL(strsep); |
|---|
| 494 |
#endif |
|---|
| 495 |
|
|---|
| 496 |
#ifndef __HAVE_ARCH_MEMSET |
|---|
| 497 |
/** |
|---|
| 498 |
* memset - Fill a region of memory with the given value |
|---|
| 499 |
* @s: Pointer to the start of the area. |
|---|
| 500 |
* @c: The byte to fill the area with |
|---|
| 501 |
* @count: The size of the area. |
|---|
| 502 |
* |
|---|
| 503 |
* Do not use memset() to access IO space, use memset_io() instead. |
|---|
| 504 |
*/ |
|---|
| 505 |
void *memset(void *s, int c, size_t count) |
|---|
| 506 |
{ |
|---|
| 507 |
char *xs = s; |
|---|
| 508 |
|
|---|
| 509 |
while (count--) |
|---|
| 510 |
*xs++ = c; |
|---|
| 511 |
return s; |
|---|
| 512 |
} |
|---|
| 513 |
EXPORT_SYMBOL(memset); |
|---|
| 514 |
#endif |
|---|
| 515 |
|
|---|
| 516 |
#ifndef __HAVE_ARCH_MEMCPY |
|---|
| 517 |
/** |
|---|
| 518 |
* memcpy - Copy one area of memory to another |
|---|
| 519 |
* @dest: Where to copy to |
|---|
| 520 |
* @src: Where to copy from |
|---|
| 521 |
* @count: The size of the area. |
|---|
| 522 |
* |
|---|
| 523 |
* You should not use this function to access IO space, use memcpy_toio() |
|---|
| 524 |
* or memcpy_fromio() instead. |
|---|
| 525 |
*/ |
|---|
| 526 |
void *memcpy(void *dest, const void *src, size_t count) |
|---|
| 527 |
{ |
|---|
| 528 |
char *tmp = dest; |
|---|
| 529 |
const char *s = src; |
|---|
| 530 |
|
|---|
| 531 |
unsigned int val,offset=0; |
|---|
| 532 |
while(count--) //aligned copy |
|---|
| 533 |
{ |
|---|
| 534 |
if (((unsigned int)offset % 4) == 0) { |
|---|
| 535 |
val = *(unsigned int *)s; |
|---|
| 536 |
s += 4; |
|---|
| 537 |
} |
|---|
| 538 |
*tmp++ = ((unsigned char *)&val) + (offset++ & 3); |
|---|
| 539 |
} |
|---|
| 540 |
return dest; |
|---|
| 541 |
} |
|---|
| 542 |
|
|---|
| 543 |
EXPORT_SYMBOL(memcpy); |
|---|
| 544 |
#endif |
|---|
| 545 |
|
|---|
| 546 |
#ifndef __HAVE_ARCH_MEMMOVE |
|---|
| 547 |
|
|---|
| 548 |
void *memcpy_rev(void *dest, const void *src, size_t count) |
|---|
| 549 |
{ |
|---|
| 550 |
char *tmp = dest; |
|---|
| 551 |
const char *s = src; |
|---|
| 552 |
s+=count; |
|---|
| 553 |
tmp+=count; |
|---|
| 554 |
unsigned int val,offset=0; |
|---|
| 555 |
while(count--) //aligned copy |
|---|
| 556 |
{ |
|---|
| 557 |
if (((unsigned int)offset % 4) == 0) { |
|---|
| 558 |
val = *(unsigned int *)s; |
|---|
| 559 |
s -= 4; |
|---|
| 560 |
} |
|---|
| 561 |
*tmp-- = ((unsigned char *)&val) + (offset++ & 3); |
|---|
| 562 |
} |
|---|
| 563 |
return dest; |
|---|
| 564 |
} |
|---|
| 565 |
|
|---|
| 566 |
/** |
|---|
| 567 |
* memmove - Copy one area of memory to another |
|---|
| 568 |
* @dest: Where to copy to |
|---|
| 569 |
* @src: Where to copy from |
|---|
| 570 |
* @count: The size of the area. |
|---|
| 571 |
* |
|---|
| 572 |
* Unlike memcpy(), memmove() copes with overlapping areas. |
|---|
| 573 |
*/ |
|---|
| 574 |
void *memmove(void *dest, const void *src, size_t count) |
|---|
| 575 |
{ |
|---|
| 576 |
char *tmp; |
|---|
| 577 |
const char *s; |
|---|
| 578 |
|
|---|
| 579 |
if (dest <= src) { |
|---|
| 580 |
tmp = dest; |
|---|
| 581 |
s = src; |
|---|
| 582 |
memcpy(tmp,s,count); |
|---|
| 583 |
} else { |
|---|
| 584 |
tmp = dest; |
|---|
| 585 |
tmp += count; |
|---|
| 586 |
s = src; |
|---|
| 587 |
s += count; |
|---|
| 588 |
while (count--) |
|---|
| 589 |
memcpy_rev(tmp,s,count); |
|---|
| 590 |
} |
|---|
| 591 |
return dest; |
|---|
| 592 |
} |
|---|
| 593 |
EXPORT_SYMBOL(memmove); |
|---|
| 594 |
#endif |
|---|
| 595 |
|
|---|
| 596 |
#ifndef __HAVE_ARCH_MEMCMP |
|---|
| 597 |
/** |
|---|
| 598 |
* memcmp - Compare two areas of memory |
|---|
| 599 |
* @cs: One area of memory |
|---|
| 600 |
* @ct: Another area of memory |
|---|
| 601 |
* @count: The size of the area. |
|---|
| 602 |
*/ |
|---|
| 603 |
#undef memcmp |
|---|
| 604 |
int memcmp(const void *cs, const void *ct, size_t count) |
|---|
| 605 |
{ |
|---|
| 606 |
const unsigned char *su1, *su2; |
|---|
| 607 |
int res = 0; |
|---|
| 608 |
|
|---|
| 609 |
for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) |
|---|
| 610 |
if ((res = *su1 - *su2) != 0) |
|---|
| 611 |
break; |
|---|
| 612 |
return res; |
|---|
| 613 |
} |
|---|
| 614 |
EXPORT_SYMBOL(memcmp); |
|---|
| 615 |
#endif |
|---|
| 616 |
|
|---|
| 617 |
#ifndef __HAVE_ARCH_MEMSCAN |
|---|
| 618 |
/** |
|---|
| 619 |
* memscan - Find a character in an area of memory. |
|---|
| 620 |
* @addr: The memory area |
|---|
| 621 |
* @c: The byte to search for |
|---|
| 622 |
* @size: The size of the area. |
|---|
| 623 |
* |
|---|
| 624 |
* returns the address of the first occurrence of @c, or 1 byte past |
|---|
| 625 |
* the area if @c is not found |
|---|
| 626 |
*/ |
|---|
| 627 |
void *memscan(void *addr, int c, size_t size) |
|---|
| 628 |
{ |
|---|
| 629 |
unsigned char *p = addr; |
|---|
| 630 |
|
|---|
| 631 |
while (size) { |
|---|
| 632 |
if (*p == c) |
|---|
| 633 |
return (void *)p; |
|---|
| 634 |
p++; |
|---|
| 635 |
size--; |
|---|
| 636 |
} |
|---|
| 637 |
return (void *)p; |
|---|
| 638 |
} |
|---|
| 639 |
EXPORT_SYMBOL(memscan); |
|---|
| 640 |
#endif |
|---|
| 641 |
|
|---|
| 642 |
#ifndef __HAVE_ARCH_STRSTR |
|---|
| 643 |
/** |
|---|
| 644 |
* strstr - Find the first substring in a %NUL terminated string |
|---|
| 645 |
* @s1: The string to be searched |
|---|
| 646 |
* @s2: The string to search for |
|---|
| 647 |
*/ |
|---|
| 648 |
char *strstr(const char *s1, const char *s2) |
|---|
| 649 |
{ |
|---|
| 650 |
int l1, l2; |
|---|
| 651 |
|
|---|
| 652 |
l2 = strlen(s2); |
|---|
| 653 |
if (!l2) |
|---|
| 654 |
return (char *)s1; |
|---|
| 655 |
l1 = strlen(s1); |
|---|
| 656 |
while (l1 >= l2) { |
|---|
| 657 |
l1--; |
|---|
| 658 |
if (!memcmp(s1, s2, l2)) |
|---|
| 659 |
return (char *)s1; |
|---|
| 660 |
s1++; |
|---|
| 661 |
} |
|---|
| 662 |
return NULL; |
|---|
| 663 |
} |
|---|
| 664 |
EXPORT_SYMBOL(strstr); |
|---|
| 665 |
#endif |
|---|
| 666 |
|
|---|
| 667 |
#ifndef __HAVE_ARCH_MEMCHR |
|---|
| 668 |
/** |
|---|
| 669 |
* memchr - Find a character in an area of memory. |
|---|
| 670 |
* @s: The memory area |
|---|
| 671 |
* @c: The byte to search for |
|---|
| 672 |
* @n: The size of the area. |
|---|
| 673 |
* |
|---|
| 674 |
* returns the address of the first occurrence of @c, or %NULL |
|---|
| 675 |
* if @c is not found |
|---|
| 676 |
*/ |
|---|
| 677 |
void *memchr(const void *s, int c, size_t n) |
|---|
| 678 |
{ |
|---|
| 679 |
const unsigned char *p = s; |
|---|
| 680 |
while (n-- != 0) { |
|---|
| 681 |
if ((unsigned char)c == *p++) { |
|---|
| 682 |
return (void *)(p - 1); |
|---|
| 683 |
} |
|---|
| 684 |
} |
|---|
| 685 |
return NULL; |
|---|
| 686 |
} |
|---|
| 687 |
EXPORT_SYMBOL(memchr); |
|---|
| 688 |
#endif |
|---|