X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FLibraries%2Flibc.so_src%2Fstring.c;h=98692b215b9f00f510470ad2d5c68f7fc4f9c617;hb=45f9a29e481ce9ea7ca7121541f0e0f90147f5b1;hp=07c201791630846cd521a1869c51faf11d9065f4;hpb=9d3800f60f2212432e550a4e003ae65b498a4d36;p=tpg%2Facess2.git diff --git a/Usermode/Libraries/libc.so_src/string.c b/Usermode/Libraries/libc.so_src/string.c index 07c20179..98692b21 100644 --- a/Usermode/Libraries/libc.so_src/string.c +++ b/Usermode/Libraries/libc.so_src/string.c @@ -34,6 +34,35 @@ EXPORT char *strcpy(char *dst, const char *src) return _dst; } +/** + * \fn EXPORT char *strncpy(char *dst, const char *src) + * \brief Copy at most \a num characters from \a src to \a dst + * \return \a dst + */ +EXPORT char *strncpy(char *dst, const char *src, size_t num) +{ + char *to = dst; + while(*src && num--) *to++ = *src++; + *to = '\0'; + return dst; +} + +/** + * \fn EXPORT char *strcat(char *dst, const char *src) + * \brief Append a string onto another + */ +EXPORT char *strcat(char *dst, const char *src) +{ + char *to = dst; + // Find the end + while(*to) to++; + // Copy + while(*src) *to++ = *src++; + // End string + *to = '\0'; + return dst; +} + /** * \fn EXPORT int strlen(const char *str) * \brief Get the length of a string @@ -72,6 +101,52 @@ EXPORT char *strdup(const char *str) return ret; } +/** + * \fn EXPORT char *strchr(char *str, int character) + * \brief Locate a character in a string + */ +EXPORT char *strchr(char *str, int character) +{ + while(*str) + { + if(*str == character) return str; + } + return NULL; +} + +/** + * \fn EXPORT char *strrchr(char *str, int character) + * \brief Locate the last occurance of a character in a string + */ +EXPORT char *strrchr(char *str, int character) +{ + int i; + i = strlen(str)-1; + while(i--) + { + if(str[i] == character) return &str[i]; + } + return NULL; +} + +/** + * \fn EXPORT char *strstr(char *str1, const char *str2) + * \brief Search a \a str1 for the first occurance of \a str2 + */ +EXPORT char *strstr(char *str1, const char *str2) +{ + const char *test = str2; + + while(*str1) + { + if(*test == '\0') return str1; + if(*str1 == *test) test++; + else test = str2; + str1 ++; + } + return NULL; +} + // --- Memory --- /** * \fn EXPORT void *memset(void *dest, int val, size_t num) @@ -111,3 +186,40 @@ EXPORT void *memmove(void *dest, const void *src, size_t count) for(;count--;) *dp++ = *sp++; return dest; } + +/** + * \fn EXPORT int memcmp(const void *mem1, const void *mem2, size_t count) + * \brief Compare two regions of memory + * \param mem1 Region 1 + * \param mem2 Region 2 + * \param count Number of bytes to check + */ +EXPORT int memcmp(const void *mem1, const void *mem2, size_t count) +{ + while(count--) + { + if( *(unsigned char*)mem1 != *(unsigned char*)mem2 ) + return *(unsigned char*)mem1 - *(unsigned char*)mem2; + mem1 ++; + mem2 ++; + } + return 0; +} + +/** + * \fn EXPORT void *memchr(void *ptr, int value, size_t num) + * \brief Locates the first occurence of \a value starting at \a ptr + * \param ptr Starting memory location + * \param value Value to find + * \param num Size of memory area to check + */ +EXPORT void *memchr(void *ptr, int value, size_t num) +{ + while(num--) + { + if( *(unsigned char*)ptr == (unsigned char)value ) + return ptr; + ptr ++; + } + return NULL; +}