Actual source code: fhost.c

  1: /*
  2:       Code for manipulating files.
  3: */
 4:  #include petsc.h
 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: #include "petscfix.h"

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

 30:     Not Collective

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

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

 39:     Level: developer

 41:     Concepts: machine name
 42:     Concepts: host name

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

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

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