module new_unit_m IMPLICIT NONE contains SUBROUTINE new_unit(unit) integer, intent(out):: unit ! Variables local to the procedure: logical opened, exist !------------------------------------------------------ unit = 0 do inquire(unit=unit, opened=opened, exist=exist) if (exist .and. .not. opened) exit unit = unit + 1 END DO END SUBROUTINE new_unit end module new_unit_m