From 15e4e5ef56c35935b57f2b1efc40548c54234d3e Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 10 Aug 2011 11:22:32 +0000 Subject: [PATCH] strndup --- src/include/gnunet_common.h | 22 +++++++++++++++++++++- src/util/common_allocation.c | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h index 503fda5c1..7e4f82ad4 100644 --- a/src/include/gnunet_common.h +++ b/src/include/gnunet_common.h @@ -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 arr of size size @@ -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 diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c index 59fa2dc05..1ae01ca49 100644 --- a/src/util/common_allocation.c +++ b/src/util/common_allocation.c @@ -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. -- 2.25.1