integer function GetLun () ! by Steve Lionel, Ron Shepherd, William Clodius, Cleve Page, ! Jeff Drummond. and others as posed at comp.lang.fortran on ! 9 September 1997 implicit none logical :: exs, opn integer :: i getlun = -1 ! returned if no units are available. do i=7,99 inquire (unit=i,exist=exs,opened=opn) if (exs .and. .not. opn) then getlun = i return end if end do write (*,*) "There are no free Fortran logical units available." return end function getlun program TestLun implicit none integer :: GetLun, gL open(unit=7,status="scratch") gL = GetLun() if (gL > 0) then write(*,*) "Free Fortran Logical Unit= ",gL endif end program TestLun