X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fxgethostname.c;h=5178784baca5bffa889d7db3078a65f0b3f405e0;hb=f8c0b2e2488a24db4238c1d59dd1bb251c738a11;hp=1227cc994a9eba640587f668a5dd0b44f1aba239;hpb=e57b0bd6cce7d468a30356c0a2cdab8a0af58eca;p=gnulib.git diff --git a/lib/xgethostname.c b/lib/xgethostname.c index 1227cc994..5178784ba 100644 --- a/lib/xgethostname.c +++ b/lib/xgethostname.c @@ -21,6 +21,7 @@ # include #endif +#include #include #include @@ -45,6 +46,9 @@ int gethostname (); # define INITIAL_HOSTNAME_LENGTH 34 #endif +/* Return the current hostname in malloc'd storage. + If malloc fails, exit. + Upon any other failure, return NULL. */ char * xgethostname () { @@ -67,7 +71,12 @@ xgethostname () if (err >= 0 && hostname[k] == '\0') break; else if (err < 0 && errno != ENAMETOOLONG && errno != 0) - error (EXIT_FAILURE, errno, "gethostname"); + { + int saved_errno = errno; + free (hostname); + errno = saved_errno; + return NULL; + } size *= 2; hostname = xrealloc (hostname, size + 1); }