      SUBROUTINE FILES (LUNIT, NAME)
      IMPLICIT NONE
C
C AMIGA SPECIFIC ROUTINE TO ALLOCATE FILES
C   LUNIT = LOGICAL UNIT NUMBER
C   NAME = FILE NAME, 1 CHAR/WORD
C
      INTEGER LUNIT
      CHARACTER*1024 NAME
C
      INCLUDE MATLAB$KOM:IOP.KOM
C
      INTEGER J
C
C ***      AMIGA STUFF TO SQUEEZE NAME FROM ONE CHAR/WORD TO ONE CHAR/BYTE
      CHARACTER*1024 NAME2
      INTEGER*1 STRIP(4,256), STRIP2(32)
      CHARACTER*32 NAME3
      EQUIVALENCE (NAME2, STRIP), (NAME3, STRIP2)
C
C
      FE = 0
C
C ***      ERROR CATCHER
      IF (LUNIT.EQ.0) RETURN
C
C ***      PRINTER
      IF (LUNIT.EQ.6) RETURN
C
C ***      TERMINAL I/O
      IF (LUNIT.EQ.9) RETURN
C
C ***      HELP FILE
      IF (LUNIT.EQ.11) THEN
        OPEN (11, FILE = 'HELP.LIS', STATUS = 'OLD', ERR = 14)
        WRITE (9, 09)
09      FORMAT (/, ' HELP is available')
        RETURN
      ENDIF
      IF (LUNIT.EQ.-11 .AND. HIO.NE.0) THEN
        REWIND (11, ERR = 99)
        RETURN
      ENDIF
      IF (LUNIT.LT.0) THEN
        CLOSE (UNIT = -LUNIT, ERR = 99)
        RETURN
      ENDIF
C
C ***      ALL OTHER FILES
      NAME2 = NAME
      DO 37 J = 1, 32
        STRIP2(J) = STRIP(1,J)
37    CONTINUE
      OPEN (UNIT = LUNIT, FILE = NAME3, STATUS = 'UNKNOWN', ERR = 98)
      RETURN
C
C ***      HELP FILE NOT FOUND
14    CONTINUE
      WRITE (9, 15)
15    FORMAT (' HELP IS NOT AVAILABLE')
      HIO = 0
      RETURN
C
C ***      GENERAL FILE OPEN FAILURE
98    CONTINUE
      WRITE (9, 16)
16    FORMAT (' OPEN FILE FAILED')
      FE = 1
C
C ***      IF THIS WAS A DIARY FILE (OUTPUT), SET ITS FILE HANDLE TO 0
C           OTHERWISE, SET THE I/O TO TERMINAL I/O
      IF (LUNIT.EQ.8) THEN
        WIO = 0
      ELSE
        RIO = RTE
      ENDIF
      RETURN
C
99    CONTINUE
      RETURN
      END
