X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/3910a511d4d9ad19e5eeff769881f4e52f498947..c4db59aa4b58bca9cd146ae30f507c16218a67d7:/sbr/utils.c?ds=sidebyside diff --git a/sbr/utils.c b/sbr/utils.c index 0766809d..02e8cfb3 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -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.