system_gethostname(3f) - [M_system:QUERY] get name of current host
(LICENSE:PD)
subroutine system_gethostname(string,ierr)
character(len=:),allocatable,intent(out) :: NAME
integer,intent(out) :: IERR
The system_gethostname(3f) procedure returns the standard host
name for the current machine.
string returns the hostname. Must be an allocatable CHARACTER variable.
ierr Upon successful completion, 0 shall be returned; otherwise, -1
shall be returned.
Sample program:
program demo_system_gethostname
use M_system, only : system_gethostname
implicit none
character(len=:),allocatable :: name
integer :: ierr
call system_gethostname(name,ierr)
if(ierr.eq.0)then
write(*,'("hostname[",a,"]")')name
else
write(*,'(a)')'ERROR: could not get hostname'
endif
end program demo_system_gethostname
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=:), | intent(out), | allocatable | :: | NAME | ||
integer, | intent(out) | :: | IERR |
subroutine system_gethostname(NAME,IERR)
implicit none
! ident_29="@(#) M_system system_gethostname(3f) get name of current host by calling gethostname(3c)"
character(len=:),allocatable,intent(out) :: NAME
integer,intent(out) :: IERR
character(kind=c_char,len=1) :: C_BUFF(HOST_NAME_MAX+1)
! describe the C routine to Fortran
!int gethostname(char *name, size_t namelen);
interface
function system_gethostname_c(c_buf,c_buflen) bind(C,NAME='gethostname')
import c_char, c_int
implicit none
integer(kind=c_int) :: system_gethostname_c
character(KIND=C_CHAR),intent(out) :: c_buf(*)
integer(kind=c_int),intent(in),value :: c_buflen
end function system_gethostname_c
end interface
C_BUFF=' '
ierr=system_gethostname_c(C_BUFF,HOST_NAME_MAX) ! Host names are limited to {HOST_NAME_MAX} bytes.
NAME=trim(arr2str(C_BUFF))
end subroutine system_gethostname