X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;f=sbr%2Futils.c;h=2120b84541cb77ee7d3933c00e07f6823ec90d5d;hb=8f0c973e6cfebb193010714453f70f55ceae5dd7;hp=7dfe900b6cdc674ddad9e54169b559042f772da2;hpb=008837e090c008e3afe7a9c8667070bafa091e62;p=mmh diff --git a/sbr/utils.c b/sbr/utils.c index 7dfe900..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) { @@ -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; +}