summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreschnett <eschnett@17b73243-c579-4c4c-a9d2-2d5706c11dac>2012-11-19 16:27:28 +0000
committereschnett <eschnett@17b73243-c579-4c4c-a9d2-2d5706c11dac>2012-11-19 16:27:28 +0000
commiteb5e66b09a00ec4be3389eabf65097f08e4dee29 (patch)
treefb6b44ea8c92efd0d57c81dadd3ab7f8f7117441 /src
parentc04b6a776d25340da3d2a9d95acbebc7d2b2da1b (diff)
Cache result of gethostname() call
git-svn-id: http://svn.cactuscode.org/flesh/trunk@4914 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'src')
-rw-r--r--src/util/Network.c57
1 files changed, 40 insertions, 17 deletions
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 <string.h>
#ifdef HAVE_UNISTD_H
-#include <unistd.h>
+# include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_NETDB_H
-#include <netdb.h>
+# include <netdb.h>
#elif defined HAVE_WINSOCK2_H
-#include <winsock2.h>
+# include <winsock2.h>
#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';
}