diff options
Diffstat (limited to 'lib/string.c')
| -rw-r--r-- | lib/string.c | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/lib/string.c b/lib/string.c index b19b87af65a3..9f75b4ec50b8 100644 --- a/lib/string.c +++ b/lib/string.c | |||
| @@ -246,13 +246,17 @@ EXPORT_SYMBOL(strlcat); | |||
| 246 | #undef strcmp | 246 | #undef strcmp |
| 247 | int strcmp(const char *cs, const char *ct) | 247 | int strcmp(const char *cs, const char *ct) |
| 248 | { | 248 | { |
| 249 | signed char __res; | 249 | unsigned char c1, c2; |
| 250 | 250 | ||
| 251 | while (1) { | 251 | while (1) { |
| 252 | if ((__res = *cs - *ct++) != 0 || !*cs++) | 252 | c1 = *cs++; |
| 253 | c2 = *ct++; | ||
| 254 | if (c1 != c2) | ||
| 255 | return c1 < c2 ? -1 : 1; | ||
| 256 | if (!c1) | ||
| 253 | break; | 257 | break; |
| 254 | } | 258 | } |
| 255 | return __res; | 259 | return 0; |
| 256 | } | 260 | } |
| 257 | EXPORT_SYMBOL(strcmp); | 261 | EXPORT_SYMBOL(strcmp); |
| 258 | #endif | 262 | #endif |
| @@ -266,14 +270,18 @@ EXPORT_SYMBOL(strcmp); | |||
| 266 | */ | 270 | */ |
| 267 | int strncmp(const char *cs, const char *ct, size_t count) | 271 | int strncmp(const char *cs, const char *ct, size_t count) |
| 268 | { | 272 | { |
| 269 | signed char __res = 0; | 273 | unsigned char c1, c2; |
| 270 | 274 | ||
| 271 | while (count) { | 275 | while (count) { |
| 272 | if ((__res = *cs - *ct++) != 0 || !*cs++) | 276 | c1 = *cs++; |
| 277 | c2 = *ct++; | ||
| 278 | if (c1 != c2) | ||
| 279 | return c1 < c2 ? -1 : 1; | ||
| 280 | if (!c1) | ||
| 273 | break; | 281 | break; |
| 274 | count--; | 282 | count--; |
| 275 | } | 283 | } |
| 276 | return __res; | 284 | return 0; |
| 277 | } | 285 | } |
| 278 | EXPORT_SYMBOL(strncmp); | 286 | EXPORT_SYMBOL(strncmp); |
| 279 | #endif | 287 | #endif |
| @@ -330,20 +338,34 @@ EXPORT_SYMBOL(strnchr); | |||
| 330 | #endif | 338 | #endif |
| 331 | 339 | ||
| 332 | /** | 340 | /** |
| 333 | * strstrip - Removes leading and trailing whitespace from @s. | 341 | * skip_spaces - Removes leading whitespace from @str. |
| 342 | * @str: The string to be stripped. | ||
| 343 | * | ||
| 344 | * Returns a pointer to the first non-whitespace character in @str. | ||
| 345 | */ | ||
| 346 | char *skip_spaces(const char *str) | ||
| 347 | { | ||
| 348 | while (isspace(*str)) | ||
| 349 | ++str; | ||
| 350 | return (char *)str; | ||
| 351 | } | ||
| 352 | EXPORT_SYMBOL(skip_spaces); | ||
| 353 | |||
| 354 | /** | ||
| 355 | * strim - Removes leading and trailing whitespace from @s. | ||
| 334 | * @s: The string to be stripped. | 356 | * @s: The string to be stripped. |
| 335 | * | 357 | * |
| 336 | * Note that the first trailing whitespace is replaced with a %NUL-terminator | 358 | * 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 | 359 | * in the given string @s. Returns a pointer to the first non-whitespace |
| 338 | * character in @s. | 360 | * character in @s. |
| 339 | */ | 361 | */ |
| 340 | char *strstrip(char *s) | 362 | char *strim(char *s) |
| 341 | { | 363 | { |
| 342 | size_t size; | 364 | size_t size; |
| 343 | char *end; | 365 | char *end; |
| 344 | 366 | ||
| 367 | s = skip_spaces(s); | ||
| 345 | size = strlen(s); | 368 | size = strlen(s); |
| 346 | |||
| 347 | if (!size) | 369 | if (!size) |
| 348 | return s; | 370 | return s; |
| 349 | 371 | ||
| @@ -352,12 +374,9 @@ char *strstrip(char *s) | |||
| 352 | end--; | 374 | end--; |
| 353 | *(end + 1) = '\0'; | 375 | *(end + 1) = '\0'; |
| 354 | 376 | ||
| 355 | while (*s && isspace(*s)) | ||
| 356 | s++; | ||
| 357 | |||
| 358 | return s; | 377 | return s; |
| 359 | } | 378 | } |
| 360 | EXPORT_SYMBOL(strstrip); | 379 | EXPORT_SYMBOL(strim); |
| 361 | 380 | ||
| 362 | #ifndef __HAVE_ARCH_STRLEN | 381 | #ifndef __HAVE_ARCH_STRLEN |
| 363 | /** | 382 | /** |
