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