From: Karel Zak Date: Wed, 10 Apr 2013 15:16:01 +0000 (+0200) Subject: lib/xalloc: add xstrndup() X-Git-Tag: v2.23-rc2~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2b88d3d72b6dde337ec7a3a437c74364f02b15ec;p=thirdparty%2Futil-linux.git lib/xalloc: add xstrndup() Signed-off-by: Karel Zak --- 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, ...) {