Usermode/libc - Fixed implementation of atexit and printf
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / string.c
index 4e8dfe2..1c05236 100644 (file)
@@ -77,27 +77,25 @@ EXPORT char *strcat(char *dst, const char *src)
 }
 
 /**
- * \fn EXPORT int strlen(const char *str)
  * \brief Get the length of a string
  */
-EXPORT int strlen(const char *str)
+EXPORT size_t strlen(const char *str)
 {
-       int retval;
-       for(retval = 0; *str != '\0'; str++)
-               retval++;
+       size_t  retval;
+       for(retval = 0; *str != '\0'; str++, retval++);
        return retval;
 }
 
 /**
- * \fn EXPORT int strncmp(const char *s1, const char *s2, size_t len)
- * \brief Compare two strings with a limit
+ * \brief Get the length of a string, with a maximum of \a maxlen
+ * 
+ * Gets the length of a string (excluding the terminating \0 byte)
  */
-EXPORT int strncmp(const char *s1, const char *s2, size_t len)
+EXPORT size_t strnlen(const char *str, size_t maxlen)
 {
-       while(--len && *s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
-               s1++; s2++;
-       }
-       return (int)*s1 - (int)*s2;
+       size_t  len;
+       for( len = 0; maxlen -- && *str; str ++, len ++ );
+       return len;
 }
 
 /**
@@ -136,11 +134,12 @@ EXPORT char *strndup(const char *str, size_t maxlen)
  * \fn EXPORT char *strchr(char *str, int character)
  * \brief Locate a character in a string
  */
-EXPORT char *strchr(char *str, int character)
+EXPORT char *strchr(const char *str, int character)
 {
-       while(*str)
+       for(;*str;str++)
        {
-               if(*str == character)   return str;
+               if(*str == character)
+                       return (char*)str;
        }
        return NULL;
 }
@@ -149,13 +148,14 @@ EXPORT char *strchr(char *str, int character)
  * \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)
+EXPORT char *strrchr(const char *str, int character)
 {
         int    i;
        i = strlen(str)-1;
        while(i--)
        {
-               if(str[i] == character) return &str[i];
+               if(str[i] == character)
+                       return (void*)&str[i];
        }
        return NULL;
 }
@@ -168,12 +168,11 @@ EXPORT char *strstr(char *str1, const char *str2)
 {
        const char      *test = str2;
        
-       while(*str1)
+       for(;*str1;str1++)
        {
                if(*test == '\0')       return str1;
                if(*str1 == *test)      test++;
                else    test = str2;
-               str1 ++;
        }
        return NULL;
 }
@@ -211,7 +210,7 @@ EXPORT void *memmove(void *dest, const void *src, size_t count)
     char *sp = (char *)src;
     char *dp = (char *)dest;
        // Check if corruption will happen
-       if( (unsigned int)dest > (unsigned int)src && (unsigned int)dest < (unsigned int)src+count )
+       if( (intptr_t)dest > (intptr_t)src && (intptr_t)dest < (intptr_t)src+count )
                for(;count--;) dp[count] = sp[count];
        else
        for(;count--;) *dp++ = *sp++;
@@ -244,12 +243,12 @@ EXPORT int memcmp(const void *mem1, const void *mem2, size_t count)
  * \param value        Value to find
  * \param num  Size of memory area to check
  */
-EXPORT void *memchr(void *ptr, int value, size_t num)
+EXPORT void *memchr(const void *ptr, int value, size_t num)
 {
        while(num--)
        {
-               if( *(unsigned char*)ptr == (unsigned char)value )
-                       return ptr;
+               if( *(const unsigned char*)ptr == (unsigned char)value )
+                       return (void*)ptr;
                ptr ++;
        }
        return NULL;

UCC git Repository :: git.ucc.asn.au