X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/3910a511d4d9ad19e5eeff769881f4e52f498947..4fab388731424637f56bc87214ecd950189a9adc:/sbr/utils.c?ds=inline diff --git a/sbr/utils.c b/sbr/utils.c index 0766809d..d9b903e7 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. @@ -83,7 +103,7 @@ void *mh_xcalloc(size_t nelem, size_t elsize) char * pwd(void) { - register char *cp; + char *cp; static char curwd[PATH_MAX]; if (!getcwd (curwd, PATH_MAX)) { @@ -340,6 +360,19 @@ nmh_strcasestr (const char *s1, const char *s2) { } +/* TrimSuffixC deletes c from the end of non-NULL string s if it's + * present, shortening s by 1. Only one instance of c is removed. */ +void TrimSuffixC(char *s, int c) +{ + if (!*s) + return; + + s += strlen(s) - 1; + if (*s == c) + *s = '\0'; +} + + int nmh_init(const char *argv0, int read_context) { if (! setlocale(LC_ALL, "")) {