From 2b88d3d72b6dde337ec7a3a437c74364f02b15ec Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 10 Apr 2013 17:16:01 +0200 Subject: [PATCH] lib/xalloc: add xstrndup() Signed-off-by: Karel Zak --- include/xalloc.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/xalloc.h b/include/xalloc.h index 1f29621ef7..6342793e4f 100644 --- a/include/xalloc.h +++ b/include/xalloc.h @@ -63,6 +63,21 @@ static inline char *xstrdup(const char *str) return ret; } +static inline char *xstrndup(const char *str, size_t size) +{ + char *ret; + + if (!str) + return NULL; + + ret = strndup(str, size); + + if (!ret) + err(XALLOC_EXIT_CODE, "cannot duplicate string"); + return ret; +} + + static inline int __attribute__ ((__format__(printf, 2, 3))) xasprintf(char **strp, const char *fmt, ...) { -- 2.47.3