* mh_xmalloc(), mh_xrealloc(), pwd(): Add comments describing the purpose
[mmh] / sbr / utils.c
index 7dfe900..2120b84 100644 (file)
@@ -9,10 +9,13 @@
  * complete copyright information.
  */
 
-#include <h/nmh.h>
+#include <h/mh.h>
 #include <h/utils.h>
 #include <stdlib.h>
 
+/*
+ * Safely call malloc
+ */
 void *
 mh_xmalloc(size_t size)
 {
@@ -28,6 +31,9 @@ mh_xmalloc(size_t size)
     return memory;
 }
 
+/*
+ * Safely call realloc
+ */
 void *
 mh_xrealloc(void *ptr, size_t size)
 {
@@ -42,3 +48,29 @@ mh_xrealloc(void *ptr, size_t size)
 
     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;
+}