!******************************************************************************
! 2003/07/25
!******************************************************************************
!
! If this subroutine finish correctly, ios = 0. 
!
!******************************************************************************

  subroutine findfu(fn, ios, fu)

    use vtype

    implicit none

    character(len=*), intent(in) :: fn
    integer(i4b), intent(out) :: ios
    integer(i4b), intent(out) :: fu


    ! Local variables
    logical :: od
    integer, parameter :: fus = 11, fue = 99


    inquire( file=fn, opened=od )
    if( od ) goto 901


    do fu = fus, fue
       inquire( unit=fu, opened=od, iostat=ios )
       if( .not. od ) goto 101
    enddo
    goto 902
101 continue

    ios = 0

    return


901 continue
    write( 6, * ) 'ERROR: In findfu: File ', trim(fn), ' is open.'
    ios = 901
    fu = -1
    return

902 continue
    write( 6, * ) 'ERROR: In findfu: Unable to find a file unit.'
    ios = 902
    fu = -1
    return

  end subroutine findfu
