[Tinyx-devel] [PATCH 2/2] LIBC: Implement more of string.c
Status: Planning
Brought to you by:
davidcohen
From: Felipe B. <me...@fe...> - 2008-01-08 16:50:35
|
Missing only mem*() functions. Signed-off-by: Felipe Balbi <me...@fe...> --- include/libc/string.h | 8 +++ lib/libc/string.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 148 insertions(+), 1 deletions(-) diff --git a/include/libc/string.h b/include/libc/string.h index 8815a86..9106e7a 100644 --- a/include/libc/string.h +++ b/include/libc/string.h @@ -22,6 +22,14 @@ #ifndef __TINYX_LIBC_STRING_H #define __TINYX_LIBC_STRING_H +char *strcat(char *str1, const char *str2); +char *strchr(const char *str, int ch); +int strcoll(const char *str1, const char *str2); int strcmp(const char *str1, const char *str2); +char *strcpy(char *str1, const char *str2); +size_t strlen(const char *str); +char *strncat(char *str1, const char *str2, size_t count); +int strncmp(const char *str1, const char *str2, size_t count); +char *strncpy(char *str1, const char *str2, size_t count); #endif /* __TINYX_LIBC_STRING_H */ diff --git a/lib/libc/string.c b/lib/libc/string.c index d2e41a0..f54a876 100644 --- a/lib/libc/string.c +++ b/lib/libc/string.c @@ -21,7 +21,41 @@ #include <libc/string.h> -int strcmp(const char *str1, const char *str2) +/** + * strcat - Apend one string to another + * @str1: The destination string + * @str2: The source string + */ +char *strcat(char *str1, const char *str2) +{ + char *tmp = str1; + + while (*str1) + str1++; + while ((*str1++ = *str2++) != '\0') + ; + return tmp; +} + +/** + * strchr - Find the first ocurrence of a character in a string + * @str: The string to search into + * @ch: The character to find + */ +char *strchr(const char *str, int ch) +{ + for (; *str != (char)ch; ++s) + if (*str == '\0') + return NULL; + return (char *)str; +} + +/** + * strcoll - Compare two strings + * @str1: First string + * @str2: Second string + */ +int strcoll(const char *str1, const char *str2) { while ((*str1) == (*str2)) { if (!*str1++) @@ -32,3 +66,108 @@ int strcmp(const char *str1, const char *str2) return (*str1) ? 1 : -1; } + +/** + * strcmp - Lex comparation of two strings + * @str1: First string + * @str2: Second string + */ +int strcmp(const char *str1, const char *str2) +{ + signed char ret; + + while (1) { + if ((ret = *str1 - *str2++) != 0 || !*str1++) + break; + } + return ret; +} + +/** + * strcpy - Copy the contents of str2 into str1 + * @str1: The destination string + * @str2: The source string + */ +char *strcpy(char *str1, const char *str2) +{ + char *tmp = str1; + + while ((*str1++ = *str2++) != '\0') + + return tmp; +} + +/** + * strlen - Find the length of a string + * @str: The string to be sized + */ +size_t strlen(const char *str) +{ + const char *tmp; + + for (tmp = str; *tmp != '\0'; ++tmp); + + return tmp - str; +} + +/** + * strncat - Append count characters of str2 into str1 + * @str1: The destination string + * @str2: The source string + * @count: The number of characters to append + */ +char *strncat(char *str1, const char *str2, size_t count) +{ + char *tmp = str1; + + if (count) { + while (*str1) + str1++; + while ((*str1++ = *str2++) != 0) { + if (--count == 0) { + *str1 = '\0'; + break; + } + } + } + return tmp; +} + +/** + * strncmp - Comparison of count characters + * @str1: The destination string + * @str2: The source string + * @count: The number of characters + */ +int strncmp(const char *str1, const char *str2, size_t count) +{ + signed char ret; + + while (count) { + if ((ret = *str1 - *str2++) != 0 || !*str1++) + break; + count--; + } + return ret; +} + +/** + * strncpy - Copy count characters from str2 into str1 + * @str1: The destination string + * @str2: The source string + * @count: The number of characters + */ +char *strncpy(char *str1, const char *str2, size_t count) +{ + char *tmp = str1; + + while (count) { + if ((*tmp = *str2) != 0) + str2++; + tmp++; + count--; + } + + return str1; +} + -- 1.5.4.rc1.21.g0e545-dirty |