3 * utils.c -- various utility routines
7 * This code is Copyright (c) 2006, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
20 mh_xmalloc(size_t size)
25 adios(NULL, "Tried to malloc 0 bytes");
27 memory = malloc(size);
29 adios(NULL, "Malloc failed");
38 mh_xrealloc(void *ptr, size_t size)
43 adios(NULL, "Tried to realloc 0bytes");
45 memory = realloc(ptr, size);
47 adios(NULL, "Realloc failed");
53 * Return the present working directory, if the current directory does not
54 * exist, or is too long, make / the pwd.
60 static char curwd[PATH_MAX];
62 if (!getcwd (curwd, PATH_MAX)) {
63 admonish (NULL, "unable to determine working directory");
64 if (!mypath || !*mypath
65 || (strcpy (curwd, mypath), chdir (curwd)) == -1) {
72 if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
79 * add -- If "s1" is NULL, this routine just creates a
80 * -- copy of "s2" into newly malloc'ed memory.
82 * -- If "s1" is not NULL, then copy the concatenation
83 * -- of "s1" and "s2" (note the order) into newly
84 * -- malloc'ed memory. Then free "s1".
87 add (char *s2, char *s1)
90 size_t len1 = 0, len2 = 0;
97 cp = mh_xmalloc (len1 + len2 + 1);
99 /* Copy s1 and free it */
101 memcpy (cp, s1, len1);
107 memcpy (cp + len1, s2, len2);
109 /* Now NULL terminate the string */
110 cp[len1 + len2] = '\0';