xref: /petsc/src/sys/utils/fhost.c (revision 0700a8246d308f50502909ba325e6169d3ee27eb)
1 #define PETSC_DLL
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 .seealso: PetscGetUserName()
47 @*/
48 PetscErrorCode PETSC_DLLEXPORT PetscGetHostName(char name[],size_t nlen)
49 {
50   char           *domain;
51   PetscErrorCode ierr;
52   PetscTruth     flag;
53 #if defined(PETSC_HAVE_UNAME) && !defined(PETSC_HAVE_GETCOMPUTERNAME)
54   struct utsname utname;
55 #endif
56 
57   PetscFunctionBegin;
58 #if defined(PETSC_HAVE_GETCOMPUTERNAME)
59  {
60     size_t nnlen = nlen;
61     GetComputerName((LPTSTR)name,(LPDWORD)(&nnlen));
62  }
63 #elif defined(PETSC_HAVE_UNAME)
64   uname(&utname);
65   ierr = PetscStrncpy(name,utname.nodename,nlen);CHKERRQ(ierr);
66 #elif defined(PETSC_HAVE_GETHOSTNAME)
67   gethostname(name,nlen);
68 #elif defined(PETSC_HAVE_SYSINFO_3ARG)
69   sysinfo(SI_HOSTNAME,name,nlen);
70 #endif
71   /* if there was not enough room then system call will not null terminate name */
72   name[nlen-1] = 0;
73 
74   /* See if this name includes the domain */
75   ierr = PetscStrchr(name,'.',&domain);CHKERRQ(ierr);
76   if (!domain) {
77     size_t  l,ll;
78     ierr = PetscStrlen(name,&l);CHKERRQ(ierr);
79     if (l == nlen-1) PetscFunctionReturn(0);
80     name[l++] = '.';
81 #if defined(PETSC_HAVE_SYSINFO_3ARG)
82     sysinfo(SI_SRPC_DOMAIN,name+l,nlen-l);
83 #elif defined(PETSC_HAVE_GETDOMAINNAME)
84     getdomainname(name+l,nlen - l);
85 #endif
86     /* check if domain name is not a dnsdomainname and nuke it */
87     ierr = PetscStrlen(name,&ll);CHKERRQ(ierr);
88     if (ll > 4) {
89       ierr = PetscStrcmp(name + ll - 4,".edu",&flag);CHKERRQ(ierr);
90       if (!flag) {
91         ierr = PetscStrcmp(name + ll - 4,".com",&flag);CHKERRQ(ierr);
92         if (!flag) {
93           ierr = PetscStrcmp(name + ll - 4,".net",&flag);CHKERRQ(ierr);
94           if (!flag) {
95             ierr = PetscStrcmp(name + ll - 4,".org",&flag);CHKERRQ(ierr);
96             if (!flag) {
97               ierr = PetscStrcmp(name + ll - 4,".mil",&flag);CHKERRQ(ierr);
98               if (!flag) {
99                 ierr = PetscInfo1(0,"Rejecting domainname, likely is NIS %s\n",name);CHKERRQ(ierr);
100                 name[l-1] = 0;
101               }
102             }
103           }
104         }
105       }
106     }
107   }
108   PetscFunctionReturn(0);
109 }
110