Actual source code: fhost.c

petsc-3.3-p7 2013-05-11
  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:    Fortran Version:
 47:    In Fortran this routine has the format

 49: $       character*(64) name
 50: $       call PetscGetHostName(name,ierr)

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

 63: #if defined(PETSC_HAVE_GETCOMPUTERNAME)
 64:  {
 65:     size_t nnlen = nlen;
 66:     GetComputerName((LPTSTR)name,(LPDWORD)(&nnlen));
 67:  }
 68: #elif defined(PETSC_HAVE_UNAME)
 69:   uname(&utname);
 70:   PetscStrncpy(name,utname.nodename,nlen);
 71: #elif defined(PETSC_HAVE_GETHOSTNAME)
 72:   gethostname(name,nlen);
 73: #elif defined(PETSC_HAVE_SYSINFO_3ARG)
 74:   sysinfo(SI_HOSTNAME,name,nlen);
 75: #endif
 76:   /* if there was not enough room then system call will not null terminate name */
 77:   name[nlen-1] = 0;

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