From eb5e66b09a00ec4be3389eabf65097f08e4dee29 Mon Sep 17 00:00:00 2001 From: eschnett Date: Mon, 19 Nov 2012 16:27:28 +0000 Subject: Cache result of gethostname() call git-svn-id: http://svn.cactuscode.org/flesh/trunk@4914 17b73243-c579-4c4c-a9d2-2d5706c11dac --- src/util/Network.c | 57 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 17 deletions(-) (limited to 'src/util') diff --git a/src/util/Network.c b/src/util/Network.c index 27c3b43c..ca7a2018 100644 --- a/src/util/Network.c +++ b/src/util/Network.c @@ -14,12 +14,12 @@ #include #ifdef HAVE_UNISTD_H -#include +# include #endif /* HAVE_UNISTD_H */ #ifdef HAVE_NETDB_H -#include +# include #elif defined HAVE_WINSOCK2_H -#include +# include #endif /* HAVE_WINSOCK2_H */ static const char *rcsid = "$Header$"; @@ -73,25 +73,48 @@ CCTK_FILEVERSION(util_Network_c); @endvar @@*/ -void Util_GetHostName (char *name, int length) +void Util_GetHostName (char *returned_name, int length) { - gethostname (name, length); - - /* Does the name include the domain. */ - if (! strchr (name, '.')) + static int have_name = 0; + static char name[100]; + + if (! have_name) { + gethostname (name, sizeof name); + + /* Does the name include the domain name? */ + if (! strchr (name, '.')) + { #ifdef HAVE_GETHOSTBYNAME - struct hostent *thishostent=0; + struct hostent *thishostent = 0; #ifndef CRAY_XT - thishostent = gethostbyname (name); + thishostent = gethostbyname (name); #endif - - if (thishostent) - { - strncpy (name, thishostent->h_name, length); - name[length - 1] = 0; - } - else name[0]='\0'; + + if (thishostent) + { + strncpy (name, thishostent->h_name, sizeof name - 1); + name[sizeof name - 1] = '\0'; + } + else + { + name[0] = '\0'; + } #endif + } + + have_name = 1; + } + + if (! returned_name) + { + CCTK_WARN (CCTK_WARN_ABORT, "Argument \"name\" to Util_GetHostName is NULL"); + } + if (length < 1) + { + CCTK_WARN (CCTK_WARN_ABORT, "Argument \"length\" to Util_GetHostName is too small"); } + + strncpy (returned_name, name, length - 1); + returned_name[length - 1] = '\0'; } -- cgit v1.2.3