evil: add strndup().
authorMichelle Legrand <legrand.michelle@outlook.com>
Thu, 12 Feb 2015 13:45:07 +0000 (14:45 +0100)
committerCedric BAIL <cedric@osg.samsung.com>
Thu, 12 Mar 2015 06:43:58 +0000 (07:43 +0100)
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
src/lib/evil/evil_string.c
src/lib/evil/evil_string.h

index 7d7d88c..151f8e5 100644 (file)
  *
  */
 
+char *
+strndup(const char *str, size_t n)
+{
+   size_t slen = strlen(str);
+   char *ret;
+
+   if (slen > n) slen = n;
+   ret = malloc (slen + 1);
+   if (!ret) return NULL;
+
+   if (slen > 0) memcpy(ret, str, slen);
+   ret[slen] = '\0';
+   return ret;
+}
+
 int ffs(int i)
 {
    int size;
index a51475d..372a4e4 100644 (file)
  * bit related functions
  *
  */
+/**
+ * @brief Duplicate a string
+ *
+ * @param str String to be duplicated
+ * @param n size of new duplicated string
+ * @return The strndup() function returns a pointer to the duplicated string, or NULL if insufficient memory was available.
+ *
+ * This function returns a pointer to a new string which is a duplicate of the string str, 
+ * but only copies at most n bytes. If str is longer than n, only n bytes are copied,
+ * and a terminating null byte ('\0') is added.
+ *
+ * Conformity: BSD
+ *
+ * Supported OS: Windows XP.
+ */
+EAPI char *strndup(const char *str, size_t n);
 
 /**
  * @brief Return the position of the first (least significant) bit set in a word