]> diplodocus.org Git - nmh/blobdiff - sbr/utils.c
Oops, fc31cece had a syntax error that gcc ignored, clang doesn't.
[nmh] / sbr / utils.c
index 0766809d9df06c92e361ff96af0884df911f8d81..02e8cfb325af4c2955f47004f6819af5e9d8779b 100644 (file)
@@ -76,6 +76,26 @@ void *mh_xcalloc(size_t nelem, size_t elsize)
     return p;
 }
 
+/* Duplicate a NUL-terminated string, exit on failure. */
+char *mh_xstrdup(const char *src)
+{
+    size_t n;
+    char *dest;
+
+    n = strlen(src) + 1; /* Ignore possibility of overflow. */
+    dest = mh_xmalloc(n);
+    memcpy(dest, src, n);
+
+    return dest;
+}
+
+/* Call free(3), if ptr isn't NULL. */
+void mh_xfree(void *ptr)
+{
+    if (ptr)
+        free(ptr); /* Some very old platforms can't cope with NULL. */
+}
+
 /*
  * Return the present working directory, if the current directory does not
  * exist, or is too long, make / the pwd.