Replace getcpy() and strdup() with mh_xstrdup()
[mmh] / sbr / utils.c
index f61456d..466b9ab 100644 (file)
@@ -107,7 +107,7 @@ pwd(void)
 /*
 ** add   -- If "s1" is NULL, this routine just creates a
 **       -- copy of "s2" into newly malloc'ed memory.
 /*
 ** add   -- If "s1" is NULL, this routine just creates a
 **       -- copy of "s2" into newly malloc'ed memory.
-**       -- (use getcpy() instead in this case)
+**       -- (use mh_xstrdup() instead in this case)
 **       --
 **       -- If "s1" is not NULL, then copy the concatenation
 **       -- of "s1" and "s2" (note the order) into newly
 **       --
 **       -- If "s1" is not NULL, then copy the concatenation
 **       -- of "s1" and "s2" (note the order) into newly
@@ -218,3 +218,19 @@ app_msgarg(struct msgs_array *msgs, char *cp)
        }
        msgs->msgs[msgs->size++] = cp;
 }
        }
        msgs->msgs[msgs->size++] = cp;
 }
+
+/*
+** mh_xstrdup() is a wrapper of strdup() to replace getcpy().  It returns
+** a copy of its argument if this is nonnull; otherwise, it returns a
+** string of length 0.
+*/
+char *
+mh_xstrdup(char * s)
+{
+       char * tmp;
+       tmp = strdup(s ? s : "");
+       if (!tmp) {
+               adios(EX_OSERR, "strdup", "can't copy string");
+       }
+       return tmp;
+}