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
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
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