From d1d226cf12f0597145977b85604fa4d6db34eaeb Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 7 Nov 2013 09:38:53 -0800 Subject: [PATCH] Add xstrdup Which is analagous to xmalloc and friends. --- xmalloc.c | 16 ++++++++++++++++ xmalloc.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/xmalloc.c b/xmalloc.c index 8c97b0f..bc6be9f 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -23,7 +23,9 @@ */ #include "xmalloc.h" + #include +#include /* Allocate memory using malloc(), checking for errors. * @@ -60,3 +62,17 @@ xrealloc (void *ptr, size_t size) return ret; } + +char * +xstrdup (const char *s) +{ + char *ret; + + ret = strdup (s); + if (ret == NULL) { + fprintf (stderr, "Error: out of memory. Exiting.\n"); + exit (1); + } + + return ret; +} diff --git a/xmalloc.h b/xmalloc.h index 650616a..d926a91 100644 --- a/xmalloc.h +++ b/xmalloc.h @@ -75,4 +75,7 @@ xrealloc (void *ptr, size_t size); ((size) && (unsigned) (nmemb) >= INT32_MAX / (unsigned) (size) ? NULL : \ xrealloc(ptr, (unsigned) (nmemb) * (unsigned) (size))) +char * +xstrdup (const char *s); + #endif /* XMALLOC_H */ -- 2.43.0