+
+/*
+ * Safely call realloc
+ */
+void *
+mh_xrealloc(void *ptr, size_t size)
+{
+ void *memory;
+
+ if (size == 0)
+ adios(NULL, "Tried to realloc 0bytes");
+
+ memory = realloc(ptr, size);
+ if (!memory)
+ adios(NULL, "Realloc failed");
+
+ return memory;
+}
+
+/*
+ * Return the present working directory, if the current directory does not
+ * exist, or is too long, make / the pwd.
+ */
+char *
+pwd(void)
+{
+ register char *cp;
+ static char curwd[PATH_MAX];
+
+ if (!getcwd (curwd, PATH_MAX)) {
+ admonish (NULL, "unable to determine working directory");
+ if (!mypath || !*mypath
+ || (strcpy (curwd, mypath), chdir (curwd)) == -1) {
+ strcpy (curwd, "/");
+ chdir (curwd);
+ }
+ return curwd;
+ }
+
+ if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
+ *cp = '\0';
+
+ return curwd;
+}