1 /*
2    (C) 2001 by Argonne National Laboratory.
3        See COPYRIGHT in top-level directory.
4 */
5 #ifdef HAVE_MPICHCONF_H
6 #include "mpichconf.h"
7 #else
8 #include "mpe_misc_conf.h"
9 #endif
10 #include "mpe_misc.h"
11 
12 #ifdef HAVE_STRING_H
13 #include <string.h>
14 #endif
15 
16 #if defined(HAVE_UNAME)
17 #include <sys/utsname.h>
18 #endif
19 #if defined(HAVE_GETHOSTBYNAME)
20 #if defined(HAVE_NETDB_H)
21 /* Some Solaris systems can't compile netdb.h */
22 #include <netdb.h>
23 #else
24 #undef HAVE_GETHOSTBYNAME
25 #endif
26 #endif /* HAVE_GETHOSTBYNAME */
27 #if defined(HAVE_SYSINFO)
28 #if defined(HAVE_SYS_SYSTEMINFO_H)
29 #include <sys/systeminfo.h>
30 #else
31 #ifdef HAVE_SYSINFO
32 #undef HAVE_SYSINFO
33 #endif
34 #endif
35 #endif
36 
37 /*@
38     MPE_GetHostName -
39 
40     Parameters:
41 + name : character pointer
42 - nlen : integer
43 @*/
MPE_GetHostName(char * name,int nlen)44 void MPE_GetHostName( char *name, int nlen )
45 {
46 /* This is the prefered form, IF IT WORKS. */
47 #if defined(HAVE_UNAME) && defined(HAVE_GETHOSTBYNAME)
48     struct utsname utname;
49     struct hostent *he;
50     uname( &utname );
51     he = gethostbyname( utname.nodename );
52     /* We must NOT use strncpy because it will null pad to the full length
53        (nlen).  If the user has not allocated MPI_MAX_PROCESSOR_NAME chars,
54        then this will unnecessarily overwrite storage.
55      */
56     /* strncpy(name,he->h_name,nlen); */
57     {
58 	char *p_out = name;
59 	char *p_in  = he->h_name;
60 	int  i;
61 	for (i=0; i<nlen-1 && *p_in; i++)
62 	    *p_out++ = *p_in++;
63 	*p_out = 0;
64     }
65 #else
66 #if defined(HAVE_UNAME)
67     struct utsname utname;
68     uname(&utname);
69     /* We must NOT use strncpy because it will null pad to the full length
70        (nlen).  If the user has not allocated MPI_MAX_PROCESSOR_NAME chars,
71        then this will unnecessarily overwrite storage.
72      */
73     /* strncpy(name,utname.nodename,nlen); */
74     {
75 	char *p_out = name;
76 	char *p_in  = utname.nodename;
77 	int  i;
78 	for (i=0; i<nlen-1 && *p_in; i++)
79 	    *p_out++ = *p_in++;
80 	*p_out = 0;
81     }
82 #elif defined(HAVE_GETHOSTNAME)
83     gethostname( name, nlen );
84 #elif defined(HAVE_SYSINFO)
85     sysinfo(SI_HOSTNAME, name, nlen);
86 #else
87     strncpy( name, "Unknown!", nlen );
88 #endif
89 /* See if this name includes the domain */
90     if (!strchr(name,'.')) {
91     int  l;
92     l = strlen(name);
93     name[l++] = '.';
94     name[l] = 0;  /* In case we have neither SYSINFO or GETDOMAINNAME */
95 #if defined(HAVE_SYSINFO) && defined(SI_SRPC_DOMAIN)
96     sysinfo( SI_SRPC_DOMAIN,name+l,nlen-l);
97 #elif defined(HAVE_GETDOMAINNAME)
98     getdomainname( name+l, nlen - l );
99 #endif
100     }
101 #endif
102 }
103