From bc197ef01d34a4531dbb261cf8cff0f7efcbcbb2 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Wed, 19 Nov 2014 10:45:40 +1030 Subject: [PATCH] Add alloca_strndup() and strncpy_nul() --- str.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/str.h b/str.h index b51f3e04..90ded999 100644 --- a/str.h +++ b/str.h @@ -36,7 +36,18 @@ /* -------------------- Useful functions and macros -------------------- */ -#define alloca_strdup(str) strcpy(alloca(strlen(str) + 1), (str)) +#define alloca_strdup(str) strcpy(alloca(strlen(str) + 1), (str)) +#define alloca_strndup(str,len) strncpy_nul(alloca((len) + 1), (str), (len) + 1) + +/* Like strncpy(3) but ensures the string is nul terminated. + * + * @author Andrew Bettison + */ +__SERVAL_DNA__STR_INLINE char *strncpy_nul(char *dst, const char *src, size_t n) +{ + strncpy(dst, src, n - 1)[n - 1] = '\0'; + return dst; +} int is_all_matching(const unsigned char *ptr, size_t len, unsigned char value);