X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;f=sbr%2Futils.c;h=2120b84541cb77ee7d3933c00e07f6823ec90d5d;hb=8f0c973e6cfebb193010714453f70f55ceae5dd7;hp=bc171772c8a9b91b45941aafb370cbcbfa8b76e0;hpb=81a21a9a97d8633f6d6231e31fdb6e328d0d3ff2;p=mmh diff --git a/sbr/utils.c b/sbr/utils.c index bc17177..2120b84 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -9,10 +9,13 @@ * complete copyright information. */ -#include +#include #include #include +/* + * Safely call malloc + */ void * mh_xmalloc(size_t size) { @@ -27,3 +30,47 @@ mh_xmalloc(size_t size) return memory; } + +/* + * 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; +}