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