]> git.cworth.org Git - acre/commitdiff
Add xstrdup
authorCarl Worth <cworth@cworth.org>
Thu, 7 Nov 2013 17:38:53 +0000 (09:38 -0800)
committerCarl Worth <cworth@cworth.org>
Thu, 7 Nov 2013 17:38:53 +0000 (09:38 -0800)
Which is analagous to xmalloc and friends.

xmalloc.c
xmalloc.h

index 8c97b0f95f5d0921dbc081b2f7569b257528f98f..bc6be9fb112841f1f22fc9c6f7588c45003f4e45 100644 (file)
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -23,7 +23,9 @@
  */
 
 #include "xmalloc.h"
+
 #include <stdio.h>
+#include <string.h>
 
 /* 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;
+}
index 650616ae4cc55fe31b80cf9df6c597e3719f3483..d926a916de0fa0a1cbb6df5811bce15cef414305 100644 (file)
--- 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 */