Show the code
function get_env(NAME,DEFAULT) result(VALUE)
character(len=*),intent(in) :: NAME
character(len=*),intent(in),optional :: DEFAULT
character(len=:),allocatable :: VALUE
integer :: howbig
integer :: stat
integer :: length
! get length required to hold value
length=0
if(NAME /= '')then
call get_environment_variable(NAME, length=howbig,status=stat,trim_name=.true.)
select case (stat)
case (1)
!x!print *, NAME, " is not defined in the environment. Strange..."
VALUE=''
case (2)
!x!print *, "This processor doesn't support environment variables. Boooh!"
VALUE=''
case default
! make string to hold value of sufficient size
if(allocated(value))deallocate(value)
allocate(character(len=max(howbig,1)) :: VALUE)
! get value
call get_environment_variable(NAME,VALUE,status=stat,trim_name=.true.)
if(stat /= 0)VALUE=''
end select
else
VALUE=''
endif
if(VALUE == ''.and.present(DEFAULT))VALUE=DEFAULT
end function get_env