+ static char *myname = NULL;
+
+ if (myname)
+ return (const char*) myname;
+
+ myname = xbt_new(char, 255);
+
+ if (gethostname(myname, 255) == -1) {
+ /* gethostname() failed! Trying with localhost instead.
+ We first need to query the DNS to make sure localhost is resolved
+ See the note in nws/Portability/dnsutil.c about {end,set}hostent() */
+ struct hostent *tmp;
+ sethostent(0);
+ tmp = gethostbyname("localhost");
+ endhostent();
+
+ if (tmp) {
+ strncat(myname, tmp->h_name, 255);
+ } else {
+ /* Erm. localhost cannot be resolved. There's something wrong in the user DNS setting */
+ sprintf(myname, "(misconfigured host)");
+ }
+ }
+
+ myname[254] = '\0';
+
+ return myname;