How to use hostnm subroutine?

I’m a bit stuck on using hostnm to print the hostname. This is my program:

program main
  implicit none
  character(20) :: name
  integer :: status
  
  call HostNm(name, status)

  print*, "status = ", status
  print*, "name = ", name

end program main

The problem is that when I compile this with pgf90, sometimes I get a load of gibberish at the end of the hostname because the character length is too long:

status = 32767
name = larchkC’Z

and at other times I just get a segmentation fault.

If I compile with gfortran I get this:

status = 0
name = larch

Which shows no error. What’s the correct way to use hostnm?

Hi Tom,

HostNM is a non-standard extension and the version we use is slightly different then the one GNU uses. Ours is in the form:

integer function hostnm(nm)
character*(*) nm

Full details can be found in the PGI Fortran Reference Guide

So a slight change in your program will get it to work correctly:

palomar02:/tmp% cat hostnm.F90 
program main
  implicit none
  character(20) :: name
  integer :: status
#ifdef __PGI
  integer :: HostNm
  status = HostNm(name)
#else
  call HostNm(name,status)
#endif
  print*, "status = ", status
  print*, "name = ", name

end program main 
palomar02:/tmp% pgfortran -Mpreprocess hostnm.F90; a.out
 status =             0
 name = palomar02

Hope this helps,
Mat