X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fstring.c;h=a485d75962af5f7a6bbb4f819aa780c812fe25ca;hb=a2f44b27303a5353859d77a3e96a1d3f33f56ab7;hp=603c7174f41e9695eafdb050b678eddf6e5b2a7e;hpb=bc77d24c47b89f1e0efed0b8e4be5f8aad102883;p=linux-2.6.git diff --git a/lib/string.c b/lib/string.c index 603c7174f..a485d7596 100644 --- a/lib/string.c +++ b/lib/string.c @@ -19,8 +19,6 @@ * - Kissed strtok() goodbye */ -#define IN_STRING_C 1 - #include #include #include @@ -38,11 +36,13 @@ int strnicmp(const char *s1, const char *s2, size_t len) /* Yes, Virginia, it had better be unsigned */ unsigned char c1, c2; - c1 = 0; c2 = 0; + c1 = c2 = 0; if (len) { do { - c1 = *s1; c2 = *s2; - s1++; s2++; + c1 = *s1; + c2 = *s2; + s1++; + s2++; if (!c1) break; if (!c2) @@ -57,7 +57,6 @@ int strnicmp(const char *s1, const char *s2, size_t len) } return (int)c1 - (int)c2; } - EXPORT_SYMBOL(strnicmp); #endif @@ -67,7 +66,8 @@ EXPORT_SYMBOL(strnicmp); * @dest: Where to copy the string to * @src: Where to copy the string from */ -char * strcpy(char * dest,const char *src) +#undef strcpy +char *strcpy(char *dest, const char *src) { char *tmp = dest; @@ -75,6 +75,7 @@ char * strcpy(char * dest,const char *src) /* nothing */; return tmp; } +EXPORT_SYMBOL(strcpy); #endif #ifndef __HAVE_ARCH_STRNCPY @@ -86,18 +87,24 @@ char * strcpy(char * dest,const char *src) * * The result is not %NUL-terminated if the source exceeds * @count bytes. + * + * In the case where the length of @src is less than that of + * count, the remainder of @dest will be padded with %NUL. + * */ -char * strncpy(char * dest,const char *src,size_t count) +char *strncpy(char *dest, const char *src, size_t count) { char *tmp = dest; while (count) { - if ((*tmp = *src) != 0) src++; + if ((*tmp = *src) != 0) + src++; tmp++; count--; } return dest; } +EXPORT_SYMBOL(strncpy); #endif #ifndef __HAVE_ARCH_STRLCPY @@ -117,7 +124,7 @@ size_t strlcpy(char *dest, const char *src, size_t size) size_t ret = strlen(src); if (size) { - size_t len = (ret >= size) ? size-1 : ret; + size_t len = (ret >= size) ? size - 1 : ret; memcpy(dest, src, len); dest[len] = '\0'; } @@ -132,7 +139,8 @@ EXPORT_SYMBOL(strlcpy); * @dest: The string to be appended to * @src: The string to append to it */ -char * strcat(char * dest, const char * src) +#undef strcat +char *strcat(char *dest, const char *src) { char *tmp = dest; @@ -140,9 +148,9 @@ char * strcat(char * dest, const char * src) dest++; while ((*dest++ = *src++) != '\0') ; - return tmp; } +EXPORT_SYMBOL(strcat); #endif #ifndef __HAVE_ARCH_STRNCAT @@ -155,23 +163,23 @@ char * strcat(char * dest, const char * src) * Note that in contrast to strncpy, strncat ensures the result is * terminated. */ -char * strncat(char *dest, const char *src, size_t count) +char *strncat(char *dest, const char *src, size_t count) { char *tmp = dest; if (count) { while (*dest) dest++; - while ((*dest++ = *src++)) { + while ((*dest++ = *src++) != 0) { if (--count == 0) { *dest = '\0'; break; } } } - return tmp; } +EXPORT_SYMBOL(strncat); #endif #ifndef __HAVE_ARCH_STRLCAT @@ -207,17 +215,18 @@ EXPORT_SYMBOL(strlcat); * @cs: One string * @ct: Another string */ -int strcmp(const char * cs,const char * ct) +#undef strcmp +int strcmp(const char *cs, const char *ct) { - register signed char __res; + signed char __res; while (1) { if ((__res = *cs - *ct++) != 0 || !*cs++) break; } - return __res; } +EXPORT_SYMBOL(strcmp); #endif #ifndef __HAVE_ARCH_STRNCMP @@ -227,18 +236,18 @@ int strcmp(const char * cs,const char * ct) * @ct: Another string * @count: The maximum number of bytes to compare */ -int strncmp(const char * cs,const char * ct,size_t count) +int strncmp(const char *cs, const char *ct, size_t count) { - register signed char __res = 0; + signed char __res = 0; while (count) { if ((__res = *cs - *ct++) != 0 || !*cs++) break; count--; } - return __res; } +EXPORT_SYMBOL(strncmp); #endif #ifndef __HAVE_ARCH_STRCHR @@ -247,13 +256,14 @@ int strncmp(const char * cs,const char * ct,size_t count) * @s: The string to be searched * @c: The character to search for */ -char * strchr(const char * s, int c) +char *strchr(const char *s, int c) { - for(; *s != (char) c; ++s) + for (; *s != (char)c; ++s) if (*s == '\0') return NULL; - return (char *) s; + return (char *)s; } +EXPORT_SYMBOL(strchr); #endif #ifndef __HAVE_ARCH_STRRCHR @@ -262,7 +272,7 @@ char * strchr(const char * s, int c) * @s: The string to be searched * @c: The character to search for */ -char * strrchr(const char * s, int c) +char *strrchr(const char *s, int c) { const char *p = s + strlen(s); do { @@ -271,6 +281,7 @@ char * strrchr(const char * s, int c) } while (--p >= s); return NULL; } +EXPORT_SYMBOL(strrchr); #endif #ifndef __HAVE_ARCH_STRNCHR @@ -283,18 +294,49 @@ char * strrchr(const char * s, int c) char *strnchr(const char *s, size_t count, int c) { for (; count-- && *s != '\0'; ++s) - if (*s == (char) c) - return (char *) s; + if (*s == (char)c) + return (char *)s; return NULL; } +EXPORT_SYMBOL(strnchr); #endif +/** + * strstrip - Removes leading and trailing whitespace from @s. + * @s: The string to be stripped. + * + * Note that the first trailing whitespace is replaced with a %NUL-terminator + * in the given string @s. Returns a pointer to the first non-whitespace + * character in @s. + */ +char *strstrip(char *s) +{ + size_t size; + char *end; + + size = strlen(s); + + if (!size) + return s; + + end = s + size - 1; + while (end >= s && isspace(*end)) + end--; + *(end + 1) = '\0'; + + while (*s && isspace(*s)) + s++; + + return s; +} +EXPORT_SYMBOL(strstrip); + #ifndef __HAVE_ARCH_STRLEN /** * strlen - Find the length of a string * @s: The string to be sized */ -size_t strlen(const char * s) +size_t strlen(const char *s) { const char *sc; @@ -302,6 +344,7 @@ size_t strlen(const char * s) /* nothing */; return sc - s; } +EXPORT_SYMBOL(strlen); #endif #ifndef __HAVE_ARCH_STRNLEN @@ -310,7 +353,7 @@ size_t strlen(const char * s) * @s: The string to be sized * @count: The maximum number of bytes to search */ -size_t strnlen(const char * s, size_t count) +size_t strnlen(const char *s, size_t count) { const char *sc; @@ -318,6 +361,7 @@ size_t strnlen(const char * s, size_t count) /* nothing */; return sc - s; } +EXPORT_SYMBOL(strnlen); #endif #ifndef __HAVE_ARCH_STRSPN @@ -342,13 +386,13 @@ size_t strspn(const char *s, const char *accept) return count; ++count; } - return count; } EXPORT_SYMBOL(strspn); #endif +#ifndef __HAVE_ARCH_STRCSPN /** * strcspn - Calculate the length of the initial substring of @s which does * not contain letters in @reject @@ -368,9 +412,10 @@ size_t strcspn(const char *s, const char *reject) } ++count; } - return count; -} +} +EXPORT_SYMBOL(strcspn); +#endif #ifndef __HAVE_ARCH_STRPBRK /** @@ -378,18 +423,19 @@ size_t strcspn(const char *s, const char *reject) * @cs: The string to be searched * @ct: The characters to search for */ -char * strpbrk(const char * cs,const char * ct) +char *strpbrk(const char *cs, const char *ct) { - const char *sc1,*sc2; + const char *sc1, *sc2; - for( sc1 = cs; *sc1 != '\0'; ++sc1) { - for( sc2 = ct; *sc2 != '\0'; ++sc2) { + for (sc1 = cs; *sc1 != '\0'; ++sc1) { + for (sc2 = ct; *sc2 != '\0'; ++sc2) { if (*sc1 == *sc2) - return (char *) sc1; + return (char *)sc1; } } return NULL; } +EXPORT_SYMBOL(strpbrk); #endif #ifndef __HAVE_ARCH_STRSEP @@ -404,9 +450,10 @@ char * strpbrk(const char * cs,const char * ct) * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. * Same semantics, slimmer shape. ;) */ -char * strsep(char **s, const char *ct) +char *strsep(char **s, const char *ct) { - char *sbegin = *s, *end; + char *sbegin = *s; + char *end; if (sbegin == NULL) return NULL; @@ -415,10 +462,8 @@ char * strsep(char **s, const char *ct) if (end) *end++ = '\0'; *s = end; - return sbegin; } - EXPORT_SYMBOL(strsep); #endif @@ -431,38 +476,15 @@ EXPORT_SYMBOL(strsep); * * Do not use memset() to access IO space, use memset_io() instead. */ -void * memset(void * s,int c,size_t count) +void *memset(void *s, int c, size_t count) { - char *xs = (char *) s; + char *xs = s; while (count--) *xs++ = c; - return s; } -#endif - -#ifndef __HAVE_ARCH_BCOPY -/** - * bcopy - Copy one area of memory to another - * @srcp: Where to copy from - * @destp: Where to copy to - * @count: The size of the area. - * - * Note that this is the same as memcpy(), with the arguments reversed. - * memcpy() is the standard, bcopy() is a legacy BSD function. - * - * You should not use this function to access IO space, use memcpy_toio() - * or memcpy_fromio() instead. - */ -void bcopy(const void * srcp, void * destp, size_t count) -{ - const char *src = srcp; - char *dest = destp; - - while (count--) - *dest++ = *src++; -} +EXPORT_SYMBOL(memset); #endif #ifndef __HAVE_ARCH_MEMCPY @@ -475,15 +497,16 @@ void bcopy(const void * srcp, void * destp, size_t count) * You should not use this function to access IO space, use memcpy_toio() * or memcpy_fromio() instead. */ -void * memcpy(void * dest,const void *src,size_t count) +void *memcpy(void *dest, const void *src, size_t count) { - char *tmp = (char *) dest, *s = (char *) src; + char *tmp = dest; + const char *s = src; while (count--) *tmp++ = *s++; - return dest; } +EXPORT_SYMBOL(memcpy); #endif #ifndef __HAVE_ARCH_MEMMOVE @@ -495,25 +518,27 @@ void * memcpy(void * dest,const void *src,size_t count) * * Unlike memcpy(), memmove() copes with overlapping areas. */ -void * memmove(void * dest,const void *src,size_t count) +void *memmove(void *dest, const void *src, size_t count) { - char *tmp, *s; + char *tmp; + const char *s; if (dest <= src) { - tmp = (char *) dest; - s = (char *) src; + tmp = dest; + s = src; while (count--) *tmp++ = *s++; - } - else { - tmp = (char *) dest + count; - s = (char *) src + count; + } else { + tmp = dest; + tmp += count; + s = src; + s += count; while (count--) *--tmp = *--s; - } - + } return dest; } +EXPORT_SYMBOL(memmove); #endif #ifndef __HAVE_ARCH_MEMCMP @@ -523,16 +548,18 @@ void * memmove(void * dest,const void *src,size_t count) * @ct: Another area of memory * @count: The size of the area. */ -int memcmp(const void * cs,const void * ct,size_t count) +#undef memcmp +int memcmp(const void *cs, const void *ct, size_t count) { const unsigned char *su1, *su2; int res = 0; - for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) + for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) if ((res = *su1 - *su2) != 0) break; return res; } +EXPORT_SYMBOL(memcmp); #endif #ifndef __HAVE_ARCH_MEMSCAN @@ -545,18 +572,19 @@ int memcmp(const void * cs,const void * ct,size_t count) * returns the address of the first occurrence of @c, or 1 byte past * the area if @c is not found */ -void * memscan(void * addr, int c, size_t size) +void *memscan(void *addr, int c, size_t size) { - unsigned char * p = (unsigned char *) addr; + unsigned char *p = addr; while (size) { if (*p == c) - return (void *) p; + return (void *)p; p++; size--; } - return (void *) p; + return (void *)p; } +EXPORT_SYMBOL(memscan); #endif #ifndef __HAVE_ARCH_STRSTR @@ -565,22 +593,23 @@ void * memscan(void * addr, int c, size_t size) * @s1: The string to be searched * @s2: The string to search for */ -char * strstr(const char * s1,const char * s2) +char *strstr(const char *s1, const char *s2) { int l1, l2; l2 = strlen(s2); if (!l2) - return (char *) s1; + return (char *)s1; l1 = strlen(s1); while (l1 >= l2) { l1--; - if (!memcmp(s1,s2,l2)) - return (char *) s1; + if (!memcmp(s1, s2, l2)) + return (char *)s1; s1++; } return NULL; } +EXPORT_SYMBOL(strstr); #endif #ifndef __HAVE_ARCH_MEMCHR @@ -598,10 +627,10 @@ void *memchr(const void *s, int c, size_t n) const unsigned char *p = s; while (n-- != 0) { if ((unsigned char)c == *p++) { - return (void *)(p-1); + return (void *)(p - 1); } } return NULL; } - +EXPORT_SYMBOL(memchr); #endif