From 47cebfdbefaf502e557e63beab2d68bd1cc462a7 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 10 Jul 2008 10:45:05 -0700 Subject: [PATCH] New function xmemdup0(). --- include/util.h | 1 + lib/util.c | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/util.h b/include/util.h index 8b0bb402..99319d4c 100644 --- a/include/util.h +++ b/include/util.h @@ -81,6 +81,7 @@ void *xmalloc(size_t); void *xcalloc(size_t, size_t); void *xrealloc(void *, size_t); void *xmemdup(const void *, size_t); +char *xmemdup0(const char *, size_t); char *xstrdup(const char *); char *xasprintf(const char *format, ...) PRINTF_FORMAT(1, 2); diff --git a/lib/util.c b/lib/util.c index 04fcd90d..230a2470 100644 --- a/lib/util.c +++ b/lib/util.c @@ -83,10 +83,19 @@ xmemdup(const void *p_, size_t size) return p; } +char * +xmemdup0(const char *p_, size_t length) +{ + char *p = xmalloc(length + 1); + memcpy(p, p_, length); + p[length] = '\0'; + return p; +} + char * xstrdup(const char *s) { - return xmemdup(s, strlen(s) + 1); + return xmemdup0(s, strlen(s)); } char * -- 2.30.2