xref: /petsc/src/sys/utils/fhost.c (revision 2fa5cd679192b9b390e47ae2d0650965e6b1d9fa)
1 
2 /*
3       Code for manipulating files.
4 */
5 #include <petscsys.h>
6 #if defined(PETSC_HAVE_STDLIB_H)
7 #include <stdlib.h>
8 #endif
9 #if defined(PETSC_HAVE_SYS_UTSNAME_H)
10 #include <sys/utsname.h>
11 #endif
12 #if defined(PETSC_HAVE_WINDOWS_H)
13 #include <windows.h>
14 #endif
15 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
16 #include <sys/systeminfo.h>
17 #endif
18 #if defined(PETSC_HAVE_UNISTD_H)
19 #include <unistd.h>
20 #endif
21 #if defined(PETSC_HAVE_NETDB_H)
22 #include <netdb.h>
23 #endif
24 
25 #undef __FUNCT__
26 #define __FUNCT__ "PetscGetHostName"
27 /*@C
28     PetscGetHostName - Returns the name of the host. This attempts to
29     return the entire Internet name. It may not return the same name
30     as MPI_Get_processor_name().
31 
32     Not Collective
33 
34     Input Parameter:
35 .   nlen - length of name
36 
37     Output Parameter:
38 .   name - contains host name.  Must be long enough to hold the name
39            This is the fully qualified name, including the domain.
40 
41     Level: developer
42 
43     Concepts: machine name
44     Concepts: host name
45 
46    Fortran Version:
47    In Fortran this routine has the format
48 
49 $       character*(64) name
50 $       call PetscGetHostName(name,ierr)
51 
52 .seealso: PetscGetUserName(),PetscGetArchType()
53 @*/
54 PetscErrorCode  PetscGetHostName(char name[],size_t nlen)
55 {
56   char           *domain;
57   PetscErrorCode ierr;
58 #if defined(PETSC_HAVE_UNAME) && !defined(PETSC_HAVE_GETCOMPUTERNAME)
59   struct utsname utname;
60 #endif
61 
62   PetscFunctionBegin;
63 #if defined(PETSC_HAVE_GETCOMPUTERNAME)
64   {
65     size_t nnlen = nlen;
66     GetComputerName((LPTSTR)name,(LPDWORD)(&nnlen));
67   }
68 #elif defined(PETSC_HAVE_UNAME)
69   uname(&utname);
70   ierr = PetscStrncpy(name,utname.nodename,nlen);CHKERRQ(ierr);
71 #elif defined(PETSC_HAVE_GETHOSTNAME)
72   gethostname(name,nlen);
73 #elif defined(PETSC_HAVE_SYSINFO_3ARG)
74   sysinfo(SI_HOSTNAME,name,nlen);
75 #endif
76   /* if there was not enough room then system call will not null terminate name */
77   name[nlen-1] = 0;
78 
79   /* See if this name includes the domain */
80   ierr = PetscStrchr(name,'.',&domain);CHKERRQ(ierr);
81   if (!domain) {
82     size_t l,ll;
83     ierr = PetscStrlen(name,&l);CHKERRQ(ierr);
84     if (l == nlen-1) PetscFunctionReturn(0);
85     name[l++] = '.';
86 #if defined(PETSC_HAVE_SYSINFO_3ARG)
87     sysinfo(SI_SRPC_DOMAIN,name+l,nlen-l);
88 #elif defined(PETSC_HAVE_GETDOMAINNAME)
89     if (getdomainname(name+l,nlen - l)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"getdomainname()");
90 #endif
91     /* check if domain name is not a dnsdomainname and nuke it */
92     ierr = PetscStrlen(name,&ll);CHKERRQ(ierr);
93     if (ll > 4) {
94       const char *suffixes[] = {".edu",".com",".net",".org",".mil",0};
95       PetscInt   index;
96       ierr = PetscStrendswithwhich(name,suffixes,&index);CHKERRQ(ierr);
97       if (!suffixes[index]) {
98         ierr      = PetscInfo1(0,"Rejecting domainname, likely is NIS %s\n",name);CHKERRQ(ierr);
99         name[l-1] = 0;
100       }
101     }
102   }
103   PetscFunctionReturn(0);
104 }
105