From: John Hodge Date: Sat, 31 Jan 2015 09:07:58 +0000 (+0800) Subject: Usermode/libc - Fixing some libc problems X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=cb89b333d47d5c8b542055be736636ec2c257844;p=tpg%2Facess2.git Usermode/libc - Fixing some libc problems --- diff --git a/Usermode/Libraries/libc.so_src/TEST_string.c b/Usermode/Libraries/libc.so_src/TEST_string.c index 46fcee5f..4ecca307 100644 --- a/Usermode/Libraries/libc.so_src/TEST_string.c +++ b/Usermode/Libraries/libc.so_src/TEST_string.c @@ -25,11 +25,27 @@ int main() ASSERT(strcasecmp("Hello", "Hello") == 0); ASSERT(strcasecmp("hellO", "Hello") == 0); - char buf[13]; + memset(buf, 127, sizeof(buf)); + ASSERT(buf[0] == 127); ASSERT(buf[4] == 127); + strncpy(buf, "hello", 4); + ASSERT(buf[3] == 'l'); ASSERT(buf[4] == 127); + strncpy(buf, "hello", 8); + ASSERT(buf[4] == 'o'); ASSERT(buf[5] == '\0'); ASSERT(buf[7] == '\0'); ASSERT(buf[8] == 127); + memset(buf, 0, 13); ASSERT(buf[0] == 0); ASSERT(buf[12] == 0); ASSERT(memchr("\xffhello", 'x', 6) == NULL); + + const char *teststr_foo = "foo"; + ASSERT(strchr(teststr_foo, 'f') == teststr_foo+0); + ASSERT(strchr(teststr_foo, 'o') == teststr_foo+1); + ASSERT(strchr(teststr_foo, '\0') == teststr_foo+3); + ASSERT(strchr(teststr_foo, 'X') == NULL); + ASSERT(strrchr(teststr_foo, 'f') == teststr_foo+0); + ASSERT(strrchr(teststr_foo, 'o') == teststr_foo+2); + ASSERT(strrchr(teststr_foo, '\0') == teststr_foo+3); + ASSERT(strrchr(teststr_foo, 'X') == NULL); } diff --git a/Usermode/Libraries/libc.so_src/string.c b/Usermode/Libraries/libc.so_src/string.c index ae91b13f..d61d51dd 100644 --- a/Usermode/Libraries/libc.so_src/string.c +++ b/Usermode/Libraries/libc.so_src/string.c @@ -87,8 +87,13 @@ EXPORT char *strcpy(char *dst, const char *src) EXPORT char *strncpy(char *dst, const char *src, size_t num) { char *to = dst; - while(*src && num--) *to++ = *src++; - *to = '\0'; + while(num --) + { + if(*src) + *to++ = *src++; + else + *to++ = '\0'; + } return dst; } @@ -125,9 +130,10 @@ EXPORT char *strncat(char *dst, const char *src, size_t n) */ EXPORT size_t strlen(const char *str) { - size_t retval; - for(retval = 0; *str != '\0'; str++, retval++); - return retval; + size_t len = 0; + while(str[len] != '\0') + len ++; + return len; } /** @@ -137,9 +143,9 @@ EXPORT size_t strlen(const char *str) */ EXPORT size_t strnlen(const char *str, size_t maxlen) { - size_t len; - for( len = 0; maxlen -- && *str; str ++, len ++ ) - ; + size_t len = 0; + while( len < maxlen && str[len] != '\0' ) + len ++; return len; } @@ -197,7 +203,7 @@ EXPORT char *strchr(const char *_str, int character) EXPORT char *strrchr(const char *_str, int character) { const unsigned char* str = (const unsigned char*)_str; - for( int i = strlen(_str); i--; ) + for( size_t i = strlen(_str); i--; ) { if(str[i] == character) return (void*)&str[i];