Actual source code: fhost.c

      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"

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

     31:     Not Collective

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

     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.

     40:     Level: developer

     42:     Concepts: machine name
     43:     Concepts: host name

     45: .seealso: PetscGetUserName()
     46: @*/
     47: PetscErrorCode PETSC_DLLEXPORT PetscGetHostName(char name[],size_t nlen)
     48: {
     49:   char           *domain;
     51:   PetscTruth     flag;
     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:     getdomainname(name+l,nlen - l);
     84: #endif
     85:     /* check if domain name is not a dnsdomainname and nuke it */
     86:     PetscStrlen(name,&ll);
     87:     if (ll > 4) {
     88:       PetscStrcmp(name + ll - 4,".edu",&flag);
     89:       if (!flag) {
     90:         PetscStrcmp(name + ll - 4,".com",&flag);
     91:         if (!flag) {
     92:           PetscStrcmp(name + ll - 4,".net",&flag);
     93:           if (!flag) {
     94:             PetscStrcmp(name + ll - 4,".org",&flag);
     95:             if (!flag) {
     96:               PetscStrcmp(name + ll - 4,".mil",&flag);
     97:               if (!flag) {
     98:                 PetscLogInfo((0,"PetscGetHostName:Rejecting domainname, likely is NIS %s\n",name));
     99:                 name[l-1] = 0;
    100:               }
    101:             }
    102:           }
    103:         }
    104:       }
    105:     }
    106:   }
    107:   return(0);
    108: }