Usermode/ld-acess - Sorting and improvements to ld-acess
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / string.c
index 607039e..86ecb6f 100644 (file)
@@ -5,6 +5,7 @@
 #include <acess/sys.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <ctype.h>
 #include "lib.h"
 
 /**
@@ -32,6 +33,25 @@ EXPORT int strncmp(const char *s1, const char *s2, size_t n)
        return (int)*s1 - (int)*s2;
 }
 
+EXPORT int strcasecmp(const char *s1, const char *s2)
+{
+        int    rv;
+       while( (rv = toupper(*s1) - toupper(*s2)) == 0 && *s1 != '\0' && *s2 != '\0' ) {
+               s1++; s2++;
+       }
+       return rv;
+}
+
+EXPORT int strncasecmp(const char *s1, const char *s2, size_t n)
+{
+        int    rv = 0;
+       if( n == 0 )    return 0;
+       while(n -- && (rv = toupper(*s1) - toupper(*s2)) == 0 && *s1 != '\0' && *s2 != '\0') {
+               s1++; s2++;
+       }
+       return rv;
+}
+
 /**
  * \fn EXPORT char *strcpy(char *dst, const char *src)
  * \brief Copy a string to another
@@ -77,17 +97,27 @@ 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;
 }
 
+/**
+ * \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 size_t strnlen(const char *str, size_t maxlen)
+{
+       size_t  len;
+       for( len = 0; maxlen -- && *str; str ++, len ++ );
+       return len;
+}
+
 /**
  * \fn EXPORT char *strdup(const char *str)
  * \brief Duplicate a string using heap memory
@@ -243,3 +273,31 @@ EXPORT void *memchr(const void *ptr, int value, size_t num)
        }
        return NULL;
 }
+
+EXPORT size_t strcspn(const char *haystack, const char *reject)
+{
+       size_t  ret = 0;
+        int    i;
+       while( *haystack )
+       {
+               for( i = 0; reject[i] && reject[i] == *haystack; i ++ );
+
+               if( reject[i] ) return ret;
+               ret ++;
+       }
+       return ret;
+}
+
+EXPORT size_t strspn(const char *haystack, const char *accept)
+{
+       size_t  ret = 0;
+        int    i;
+       while( *haystack )
+       {
+               for( i = 0; accept[i] && accept[i] == *haystack; i ++ );
+
+               if( !accept[i] )        return ret;
+               ret ++;
+       }
+       return ret;
+}

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