From 28eff5c356c6f04846d77b2b43d2a995e900903b Mon Sep 17 00:00:00 2001 From: Alexey Gladkov Date: Tue, 25 Jan 2011 00:32:42 +0300 Subject: [PATCH] Add xstrndup() Signed-off-by: Alexey Gladkov --- src/xmalloc.c | 8 ++++++++ src/xmalloc.h | 1 + 2 files changed, 9 insertions(+) diff --git a/src/xmalloc.c b/src/xmalloc.c index 0928fd7..e470164 100644 --- a/src/xmalloc.c +++ b/src/xmalloc.c @@ -40,6 +40,14 @@ xstrdup(char *p) { return q; } +char * +xstrndup(char *p, size_t n) { + char *q = strndup(p, n); + if (q == NULL) + nomem(); + return q; +} + void * xfree(void *p) { if (p != NULL) diff --git a/src/xmalloc.h b/src/xmalloc.h index 61c8b4f..a4fd0b9 100644 --- a/src/xmalloc.h +++ b/src/xmalloc.h @@ -5,6 +5,7 @@ extern void *xmalloc(size_t sz); extern void *xrealloc(void *p, size_t sz); extern char *xstrdup(char *p); +extern char *xstrndup(char *p, size_t n); extern void *xfree(void *p); #endif /* _XMALLOC_H */ -- 2.7.4