libreadline - Rework of library (less sucky now!)
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / string.c
index 98692b2..8d4f00b 100644 (file)
@@ -19,6 +19,19 @@ EXPORT int strcmp(const char *s1, const char *s2)
        return (int)*s1 - (int)*s2;
 }
 
+/**
+ * \fn EXPORT int strncmp(const char *s1, const char *s2)
+ * \brief Compare two strings
+ */
+EXPORT int strncmp(const char *s1, const char *s2, size_t n)
+{
+       if( n == 0 )    return 0;
+       while(n -- && *s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
+               s1++; s2++;
+       }
+       return (int)*s1 - (int)*s2;
+}
+
 /**
  * \fn EXPORT char *strcpy(char *dst, const char *src)
  * \brief Copy a string to another
@@ -75,18 +88,6 @@ EXPORT int strlen(const char *str)
        return retval;
 }
 
-/**
- * \fn EXPORT int strncmp(const char *s1, const char *s2, size_t len)
- * \brief Compare two strings with a limit
- */
-EXPORT int strncmp(const char *s1, const char *s2, size_t len)
-{
-       while(--len && *s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
-               s1++; s2++;
-       }
-       return (int)*s1 - (int)*s2;
-}
-
 /**
  * \fn EXPORT char *strdup(const char *str)
  * \brief Duplicate a string using heap memory
@@ -101,15 +102,34 @@ EXPORT char *strdup(const char *str)
        return ret;
 }
 
+/**
+ * \fn EXPORT char *strndup(const char *str, size_t maxlen)
+ * \brief Duplicate a string into the heap with a maximum length
+ * \param str  Input string buffer
+ * \param maxlen       Maximum valid size of the \a str buffer
+ * \return Heap string with the same value of \a str
+ */
+EXPORT char *strndup(const char *str, size_t maxlen)
+{
+       size_t  len;
+       char    *ret;
+       for( len = 0; len < maxlen && str[len]; len ++) ;
+       ret = malloc( len + 1);
+       memcpy( ret, str, len );
+       ret[len] = '\0';
+       return ret;
+}
+
 /**
  * \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;
 }
@@ -118,7 +138,7 @@ 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;
@@ -137,12 +157,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;
 }
@@ -180,7 +199,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++;
@@ -213,11 +232,11 @@ 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 )
+               if( *(const unsigned char*)ptr == (unsigned char)value )
                        return ptr;
                ptr ++;
        }

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