Returns the first logical unit that is not preconnected
Note
-1 if no free unit exists
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in), | optional | :: | file_name |
FUNCTION get_unit_number(file_name) RESULT(unit_number) !! Returns the first logical unit that is not preconnected !! @note !! -1 if no free unit exists CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: file_name INTEGER :: unit_number INTEGER :: ic, istat, nc LOGICAL :: exists, is_open IF (PRESENT(file_name)) THEN nc = SIZE(preconnected) ! Check for preconnected units DO ic = 3, nc ! Exclude the preconnected system units (< 3) IF (TRIM(preconnected(ic)%file_name) == TRIM(file_name)) THEN unit_number = preconnected(ic)%unit_number RETURN END IF END DO END IF ! Get a new unit number DO unit_number = 1, max_unit_number IF (ANY(unit_number == preconnected(:)%unit_number)) CYCLE INQUIRE (UNIT=unit_number, EXIST=exists, OPENED=is_open, IOSTAT=istat) IF (exists .AND. (.NOT. is_open) .AND. (istat == 0)) RETURN END DO unit_number = -1 END FUNCTION get_unit_number