strndup
authorChristian Grothoff <christian@grothoff.org>
Wed, 10 Aug 2011 11:22:32 +0000 (11:22 +0000)
committerChristian Grothoff <christian@grothoff.org>
Wed, 10 Aug 2011 11:22:32 +0000 (11:22 +0000)
src/include/gnunet_common.h
src/util/common_allocation.c

index 503fda5c19cab65387a4db9dbacdfb069aaae058..7e4f82ad4890b19e5cd811271ced328dac9441b0 100644 (file)
@@ -392,7 +392,7 @@ unsigned long long GNUNET_htonll (unsigned long long n);
  * allocated with GNUNET_array_grow using GNUNET_array_grow(mem, size, 0) instead of GNUNET_free.
  *
  * @param ptr location where to free the memory. ptr must have
- *     been returned by GNUNET_strdup, GNUNET_malloc or GNUNET_array_grow earlier.
+ *     been returned by GNUNET_strdup, GNUNET_strndup, GNUNET_malloc or GNUNET_array_grow earlier.
  */
 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
 
@@ -413,6 +413,16 @@ unsigned long long GNUNET_htonll (unsigned long long n);
  */
 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
 
+/**
+ * Wrapper around GNUNET_strndup.  Makes a partial copy of the string
+ * pointed to by a.
+ *
+ * @param a pointer to a string
+ * @param length of the string to duplicate
+ * @return a partial copy of the string including zero-termination
+ */
+#define GNUNET_strndup(a,b) GNUNET_xstrndup_(a,b,__FILE__,__LINE__)
+
 /**
  * Grow a well-typed (!) array.  This is a convenience
  * method to grow a vector <tt>arr</tt> of size <tt>size</tt>
@@ -551,6 +561,16 @@ void GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
  */
 char *GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
 
+/**
+ * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the GNUNET_strndup macro.
+ * @param str string to duplicate
+ * @param len lenght of the string to duplicate
+ * @param filename where is this call being made (for debugging)
+ * @param linenumber line where this call is being made (for debugging)
+ * @return the duplicated string
+ */
+char *GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, int linenumber);
+
 /**
  * Grow an array, the new elements are zeroed out.
  * Grows old by (*oldCount-newCount)*elementSize
index 59fa2dc05209058bf75a572789c8288555e1115e..1ae01ca49f43b4a5885ddab3ceabcef1fd2abbfa 100644 (file)
@@ -215,6 +215,30 @@ GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
   return res;
 }
 
+
+/**
+ * Dup partially a string (same semantics as strndup).
+ *
+ * @param str the string to dup
+ * @param len the lenght of the string to dup
+ * @param filename where in the code was the call to GNUNET_strndup
+ * @param linenumber where in the code was the call to GNUNET_strndup
+ * @return strndup(str,len)
+ */
+char *
+GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, int linenumber)
+{
+  char *res;
+
+  GNUNET_assert_at (str != NULL, filename, linenumber);
+  len = GNUNET_MIN(len,strlen(str));
+  res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
+  memcpy (res, str, len);
+  res[len] = '\0';
+  return res;
+}
+
+
 /**
  * Grow an array.  Grows old by (*oldCount-newCount)*elementSize bytes
  * and sets *oldCount to newCount.