Actual source code: fhost.c

  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

 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().

 32:     Not Collective

 34:     Input Parameter:
 35: .   nlen - length of name

 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.

 41:     Level: developer

 43:     Concepts: machine name
 44:     Concepts: host name

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

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

 73:   /* See if this name includes the domain */
 74:   PetscStrchr(name,'.',&domain);
 75:   if (!domain) {
 76:     size_t  l,ll;
 77:     PetscStrlen(name,&l);
 78:     if (l == nlen-1) return(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:     if (getdomainname(name+l,nlen - l)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"getdomainname()");
 84: #endif
 85:     /* check if domain name is not a dnsdomainname and nuke it */
 86:     PetscStrlen(name,&ll);
 87:     if (ll > 4) {
 88:       const char *suffixes[] = {".edu",".com",".net",".org",".mil",0};
 89:       PetscInt   index;
 90:       PetscStrendswithwhich(name,suffixes,&index);
 91:       if (!suffixes[index]) {
 92:         PetscInfo1(0,"Rejecting domainname, likely is NIS %s\n",name);
 93:         name[l-1] = 0;
 94:       }
 95:     }
 96:   }
 97:   return(0);
 98: }