Actual source code: fhost.c

petsc-3.7.7 2017-09-25
Report Typos and Errors
  1: #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for getdomainname */
  2: /*
  3:       Code for manipulating files.
  4: */
  5: #include <petscsys.h>
  6: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
  7: #include <sys/utsname.h>
  8: #endif
  9: #if defined(PETSC_HAVE_WINDOWS_H)
 10: #include <windows.h>
 11: #endif
 12: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 13: #include <sys/systeminfo.h>
 14: #endif
 15: #if defined(PETSC_HAVE_UNISTD_H)
 16: #include <unistd.h>
 17: #endif
 18: #if defined(PETSC_HAVE_NETDB_H)
 19: #include <netdb.h>
 20: #endif

 24: /*@C
 25:     PetscGetHostName - Returns the name of the host. This attempts to
 26:     return the entire Internet name. It may not return the same name
 27:     as MPI_Get_processor_name().

 29:     Not Collective

 31:     Input Parameter:
 32: .   nlen - length of name

 34:     Output Parameter:
 35: .   name - contains host name.  Must be long enough to hold the name
 36:            This is the fully qualified name, including the domain.

 38:     Level: developer

 40:     Concepts: machine name
 41:     Concepts: host name

 43:    Fortran Version:
 44:    In Fortran this routine has the format

 46: $       character*(64) name
 47: $       call PetscGetHostName(name,ierr)

 49: .seealso: PetscGetUserName(),PetscGetArchType()
 50: @*/
 51: PetscErrorCode  PetscGetHostName(char name[],size_t nlen)
 52: {
 53:   char           *domain;
 55: #if defined(PETSC_HAVE_UNAME) && !defined(PETSC_HAVE_GETCOMPUTERNAME)
 56:   struct utsname utname;
 57: #endif

 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:   PetscStrncpy(name,utname.nodename,nlen);
 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;

 76:   /* See if this name includes the domain */
 77:   PetscStrchr(name,'.',&domain);
 78:   if (!domain) {
 79:     size_t l,ll;
 80:     PetscStrlen(name,&l);
 81:     if (l == nlen-1) return(0);
 82:     name[l++] = '.';
 83:     name[l]   = 0;
 84: #if defined(PETSC_HAVE_SYSINFO_3ARG)
 85:     sysinfo(SI_SRPC_DOMAIN,name+l,nlen-l);
 86: #elif defined(PETSC_HAVE_GETDOMAINNAME)
 87:     if (getdomainname(name+l,nlen - l)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"getdomainname()");
 88: #endif
 89:     /* check if domain name is not a dnsdomainname and nuke it */
 90:     PetscStrlen(name,&ll);
 91:     if (ll > 4) {
 92:       const char *suffixes[] = {".edu",".com",".net",".org",".mil",0};
 93:       PetscInt   index;
 94:       PetscStrendswithwhich(name,suffixes,&index);
 95:       if (!suffixes[index]) {
 96:         PetscInfo1(0,"Rejecting domainname, likely is NIS %s\n",name);
 97:         name[l-1] = 0;
 98:       }
 99:     }
100:   }
101:   return(0);
102: }