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 | |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 39 | c1 = c2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David S. Miller | ded220b | 2007-03-29 01:18:42 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 97 | #undef strcpy |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 98 | char *strcpy(char *dest, const char *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
walter harms | 2527952 | 2005-05-05 16:16:20 -0700 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 123 | char *strncpy(char *dest, const char *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | char *tmp = dest; |
| 126 | |
| 127 | while (count) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 128 | if ((*tmp = *src) != 0) |
| 129 | src++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 155 | size_t len = (ret >= size) ? size - 1 : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 170 | #undef strcat |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 171 | char *strcat(char *dest, const char *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | char *tmp = dest; |
| 174 | |
| 175 | while (*dest) |
| 176 | dest++; |
| 177 | while ((*dest++ = *src++) != '\0') |
| 178 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 191 | * Note that in contrast to strncpy(), strncat() ensures the result is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | * terminated. |
| 193 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 194 | char *strncat(char *dest, const char *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 246 | #undef strcmp |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 247 | int strcmp(const char *cs, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame^] | 249 | unsigned char c1, c2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | while (1) { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame^] | 252 | c1 = *cs++; |
| 253 | c2 = *ct++; |
| 254 | if (c1 != c2) |
| 255 | return c1 < c2 ? -1 : 1; |
| 256 | if (!c1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | break; |
| 258 | } |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame^] | 259 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | EXPORT_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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 271 | int strncmp(const char *cs, const char *ct, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame^] | 273 | unsigned char c1, c2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | while (count) { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame^] | 276 | c1 = *cs++; |
| 277 | c2 = *ct++; |
| 278 | if (c1 != c2) |
| 279 | return c1 < c2 ? -1 : 1; |
| 280 | if (!c1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | break; |
| 282 | count--; |
| 283 | } |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame^] | 284 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | EXPORT_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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 295 | char *strchr(const char *s, int c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 297 | for (; *s != (char)c; ++s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | if (*s == '\0') |
| 299 | return NULL; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 300 | return (char *)s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | EXPORT_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 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 311 | char *strrchr(const char *s, int c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
| 320 | EXPORT_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 | */ |
| 330 | char *strnchr(const char *s, size_t count, int c) |
| 331 | { |
| 332 | for (; count-- && *s != '\0'; ++s) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 333 | if (*s == (char)c) |
| 334 | return (char *)s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return NULL; |
| 336 | } |
| 337 | EXPORT_SYMBOL(strnchr); |
| 338 | #endif |
| 339 | |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 340 | /** |
| 341 | * strstrip - Removes leading and trailing whitespace from @s. |
| 342 | * @s: The string to be stripped. |
| 343 | * |
| 344 | * Note that the first trailing whitespace is replaced with a %NUL-terminator |
| 345 | * in the given string @s. Returns a pointer to the first non-whitespace |
| 346 | * character in @s. |
| 347 | */ |
| 348 | char *strstrip(char *s) |
| 349 | { |
| 350 | size_t size; |
| 351 | char *end; |
| 352 | |
| 353 | size = strlen(s); |
| 354 | |
| 355 | if (!size) |
| 356 | return s; |
| 357 | |
| 358 | end = s + size - 1; |
Michael Holzheu | 6e6d9fa | 2006-10-28 10:38:47 -0700 | [diff] [blame] | 359 | while (end >= s && isspace(*end)) |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 360 | end--; |
| 361 | *(end + 1) = '\0'; |
| 362 | |
| 363 | while (*s && isspace(*s)) |
| 364 | s++; |
| 365 | |
| 366 | return s; |
| 367 | } |
| 368 | EXPORT_SYMBOL(strstrip); |
| 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | #ifndef __HAVE_ARCH_STRLEN |
| 371 | /** |
| 372 | * strlen - Find the length of a string |
| 373 | * @s: The string to be sized |
| 374 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 375 | size_t strlen(const char *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
| 377 | const char *sc; |
| 378 | |
| 379 | for (sc = s; *sc != '\0'; ++sc) |
| 380 | /* nothing */; |
| 381 | return sc - s; |
| 382 | } |
| 383 | EXPORT_SYMBOL(strlen); |
| 384 | #endif |
| 385 | |
| 386 | #ifndef __HAVE_ARCH_STRNLEN |
| 387 | /** |
| 388 | * strnlen - Find the length of a length-limited string |
| 389 | * @s: The string to be sized |
| 390 | * @count: The maximum number of bytes to search |
| 391 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 392 | size_t strnlen(const char *s, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
| 394 | const char *sc; |
| 395 | |
| 396 | for (sc = s; count-- && *sc != '\0'; ++sc) |
| 397 | /* nothing */; |
| 398 | return sc - s; |
| 399 | } |
| 400 | EXPORT_SYMBOL(strnlen); |
| 401 | #endif |
| 402 | |
| 403 | #ifndef __HAVE_ARCH_STRSPN |
| 404 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 405 | * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | * @s: The string to be searched |
| 407 | * @accept: The string to search for |
| 408 | */ |
| 409 | size_t strspn(const char *s, const char *accept) |
| 410 | { |
| 411 | const char *p; |
| 412 | const char *a; |
| 413 | size_t count = 0; |
| 414 | |
| 415 | for (p = s; *p != '\0'; ++p) { |
| 416 | for (a = accept; *a != '\0'; ++a) { |
| 417 | if (*p == *a) |
| 418 | break; |
| 419 | } |
| 420 | if (*a == '\0') |
| 421 | return count; |
| 422 | ++count; |
| 423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | return count; |
| 425 | } |
| 426 | |
| 427 | EXPORT_SYMBOL(strspn); |
| 428 | #endif |
| 429 | |
Kyle McMartin | 8833d32 | 2006-04-10 22:53:57 -0700 | [diff] [blame] | 430 | #ifndef __HAVE_ARCH_STRCSPN |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 432 | * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | * @s: The string to be searched |
| 434 | * @reject: The string to avoid |
| 435 | */ |
| 436 | size_t strcspn(const char *s, const char *reject) |
| 437 | { |
| 438 | const char *p; |
| 439 | const char *r; |
| 440 | size_t count = 0; |
| 441 | |
| 442 | for (p = s; *p != '\0'; ++p) { |
| 443 | for (r = reject; *r != '\0'; ++r) { |
| 444 | if (*p == *r) |
| 445 | return count; |
| 446 | } |
| 447 | ++count; |
| 448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return count; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | EXPORT_SYMBOL(strcspn); |
Kyle McMartin | 8833d32 | 2006-04-10 22:53:57 -0700 | [diff] [blame] | 452 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | #ifndef __HAVE_ARCH_STRPBRK |
| 455 | /** |
| 456 | * strpbrk - Find the first occurrence of a set of characters |
| 457 | * @cs: The string to be searched |
| 458 | * @ct: The characters to search for |
| 459 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 460 | char *strpbrk(const char *cs, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 462 | const char *sc1, *sc2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 464 | for (sc1 = cs; *sc1 != '\0'; ++sc1) { |
| 465 | for (sc2 = ct; *sc2 != '\0'; ++sc2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | if (*sc1 == *sc2) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 467 | return (char *)sc1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | return NULL; |
| 471 | } |
Kyle McMartin | 894b577 | 2006-04-10 22:53:56 -0700 | [diff] [blame] | 472 | EXPORT_SYMBOL(strpbrk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | #endif |
| 474 | |
| 475 | #ifndef __HAVE_ARCH_STRSEP |
| 476 | /** |
| 477 | * strsep - Split a string into tokens |
| 478 | * @s: The string to be searched |
| 479 | * @ct: The characters to search for |
| 480 | * |
| 481 | * strsep() updates @s to point after the token, ready for the next call. |
| 482 | * |
| 483 | * It returns empty tokens, too, behaving exactly like the libc function |
| 484 | * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. |
| 485 | * Same semantics, slimmer shape. ;) |
| 486 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 487 | char *strsep(char **s, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 489 | char *sbegin = *s; |
| 490 | char *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | if (sbegin == NULL) |
| 493 | return NULL; |
| 494 | |
| 495 | end = strpbrk(sbegin, ct); |
| 496 | if (end) |
| 497 | *end++ = '\0'; |
| 498 | *s = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | return sbegin; |
| 500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | EXPORT_SYMBOL(strsep); |
| 502 | #endif |
| 503 | |
David Brownell | 34990cf | 2008-05-01 04:34:42 -0700 | [diff] [blame] | 504 | /** |
| 505 | * sysfs_streq - return true if strings are equal, modulo trailing newline |
| 506 | * @s1: one string |
| 507 | * @s2: another string |
| 508 | * |
| 509 | * This routine returns true iff two strings are equal, treating both |
| 510 | * NUL and newline-then-NUL as equivalent string terminations. It's |
| 511 | * geared for use with sysfs input strings, which generally terminate |
| 512 | * with newlines but are compared against values without newlines. |
| 513 | */ |
| 514 | bool sysfs_streq(const char *s1, const char *s2) |
| 515 | { |
| 516 | while (*s1 && *s1 == *s2) { |
| 517 | s1++; |
| 518 | s2++; |
| 519 | } |
| 520 | |
| 521 | if (*s1 == *s2) |
| 522 | return true; |
| 523 | if (!*s1 && *s2 == '\n' && !s2[1]) |
| 524 | return true; |
| 525 | if (*s1 == '\n' && !s1[1] && !*s2) |
| 526 | return true; |
| 527 | return false; |
| 528 | } |
| 529 | EXPORT_SYMBOL(sysfs_streq); |
| 530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | #ifndef __HAVE_ARCH_MEMSET |
| 532 | /** |
| 533 | * memset - Fill a region of memory with the given value |
| 534 | * @s: Pointer to the start of the area. |
| 535 | * @c: The byte to fill the area with |
| 536 | * @count: The size of the area. |
| 537 | * |
| 538 | * Do not use memset() to access IO space, use memset_io() instead. |
| 539 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 540 | void *memset(void *s, int c, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 542 | char *xs = s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
| 544 | while (count--) |
| 545 | *xs++ = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | return s; |
| 547 | } |
| 548 | EXPORT_SYMBOL(memset); |
| 549 | #endif |
| 550 | |
| 551 | #ifndef __HAVE_ARCH_MEMCPY |
| 552 | /** |
| 553 | * memcpy - Copy one area of memory to another |
| 554 | * @dest: Where to copy to |
| 555 | * @src: Where to copy from |
| 556 | * @count: The size of the area. |
| 557 | * |
| 558 | * You should not use this function to access IO space, use memcpy_toio() |
| 559 | * or memcpy_fromio() instead. |
| 560 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 561 | void *memcpy(void *dest, const void *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 563 | char *tmp = dest; |
Jan-Benedict Glaw | 4c416ab | 2006-04-10 22:54:09 -0700 | [diff] [blame] | 564 | const char *s = src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
| 566 | while (count--) |
| 567 | *tmp++ = *s++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | return dest; |
| 569 | } |
| 570 | EXPORT_SYMBOL(memcpy); |
| 571 | #endif |
| 572 | |
| 573 | #ifndef __HAVE_ARCH_MEMMOVE |
| 574 | /** |
| 575 | * memmove - Copy one area of memory to another |
| 576 | * @dest: Where to copy to |
| 577 | * @src: Where to copy from |
| 578 | * @count: The size of the area. |
| 579 | * |
| 580 | * Unlike memcpy(), memmove() copes with overlapping areas. |
| 581 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 582 | void *memmove(void *dest, const void *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | { |
Paul Jackson | 82da2c3 | 2005-10-30 15:03:19 -0800 | [diff] [blame] | 584 | char *tmp; |
| 585 | const char *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | if (dest <= src) { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 588 | tmp = dest; |
| 589 | s = src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | while (count--) |
| 591 | *tmp++ = *s++; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 592 | } else { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 593 | tmp = dest; |
| 594 | tmp += count; |
| 595 | s = src; |
| 596 | s += count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | while (count--) |
| 598 | *--tmp = *--s; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 599 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | return dest; |
| 601 | } |
| 602 | EXPORT_SYMBOL(memmove); |
| 603 | #endif |
| 604 | |
| 605 | #ifndef __HAVE_ARCH_MEMCMP |
| 606 | /** |
| 607 | * memcmp - Compare two areas of memory |
| 608 | * @cs: One area of memory |
| 609 | * @ct: Another area of memory |
| 610 | * @count: The size of the area. |
| 611 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 612 | #undef memcmp |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 613 | int memcmp(const void *cs, const void *ct, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | { |
| 615 | const unsigned char *su1, *su2; |
| 616 | int res = 0; |
| 617 | |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 618 | for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | if ((res = *su1 - *su2) != 0) |
| 620 | break; |
| 621 | return res; |
| 622 | } |
| 623 | EXPORT_SYMBOL(memcmp); |
| 624 | #endif |
| 625 | |
| 626 | #ifndef __HAVE_ARCH_MEMSCAN |
| 627 | /** |
| 628 | * memscan - Find a character in an area of memory. |
| 629 | * @addr: The memory area |
| 630 | * @c: The byte to search for |
| 631 | * @size: The size of the area. |
| 632 | * |
| 633 | * returns the address of the first occurrence of @c, or 1 byte past |
| 634 | * the area if @c is not found |
| 635 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 636 | void *memscan(void *addr, int c, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 638 | unsigned char *p = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
| 640 | while (size) { |
| 641 | if (*p == c) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 642 | return (void *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | p++; |
| 644 | size--; |
| 645 | } |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 646 | return (void *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
| 648 | EXPORT_SYMBOL(memscan); |
| 649 | #endif |
| 650 | |
| 651 | #ifndef __HAVE_ARCH_STRSTR |
| 652 | /** |
| 653 | * strstr - Find the first substring in a %NUL terminated string |
| 654 | * @s1: The string to be searched |
| 655 | * @s2: The string to search for |
| 656 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 657 | char *strstr(const char *s1, const char *s2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | { |
| 659 | int l1, l2; |
| 660 | |
| 661 | l2 = strlen(s2); |
| 662 | if (!l2) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 663 | return (char *)s1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | l1 = strlen(s1); |
| 665 | while (l1 >= l2) { |
| 666 | l1--; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 667 | if (!memcmp(s1, s2, l2)) |
| 668 | return (char *)s1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | s1++; |
| 670 | } |
| 671 | return NULL; |
| 672 | } |
| 673 | EXPORT_SYMBOL(strstr); |
| 674 | #endif |
| 675 | |
| 676 | #ifndef __HAVE_ARCH_MEMCHR |
| 677 | /** |
| 678 | * memchr - Find a character in an area of memory. |
| 679 | * @s: The memory area |
| 680 | * @c: The byte to search for |
| 681 | * @n: The size of the area. |
| 682 | * |
| 683 | * returns the address of the first occurrence of @c, or %NULL |
| 684 | * if @c is not found |
| 685 | */ |
| 686 | void *memchr(const void *s, int c, size_t n) |
| 687 | { |
| 688 | const unsigned char *p = s; |
| 689 | while (n-- != 0) { |
| 690 | if ((unsigned char)c == *p++) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 691 | return (void *)(p - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | return NULL; |
| 695 | } |
| 696 | EXPORT_SYMBOL(memchr); |
| 697 | #endif |