      Subroutine Filchk(Filnam,Isiz,Iflag)
c
c ***********************************************************************
c *                                                                     *
c *      Subroutine FILCHK - To see if a file already exists, or if     *
c *      a file name is bad                                             *
c *                                                                     *
c *      Input values:                                                  *
c *                                                                     *
c *      Filnam - Character string containing the file name to check    *
c *      Isiz   - Size of the file to create, in blocks                 *
c *                                                                     *
c *      Output values:                                                 *
c *                                                                     *
c *      Iflag  - Flag to see if there is a problem with the file       *
c *               name, or file size (OK If Iflag = 0)                  *
c *                                                                     *
c *        Programmed by Steven C. Fisher, DTNSRDC Code 1522            *
c *                                                                     *
c ***********************************************************************
c
      Byte Filnam(15)
c
 10   Continue
c
c         Check to see if the file name has reasonable syntax
c
      Call Chknam(Filnam,Iflag)
      If (Iflag .EQ. 1) Return
c
c         Try to open the file
c
c
      Open(UNIT=12,NAME=FILNAM,TYPE='OLD',ERR=20)
c
c         If no error, file already exists
c
      Close(Unit=12)
      Write(7,100)
100   Format(' File exists - do you want to write over it? (y/n) '$)
      Call Instrg('YyNn',4,Iflg)
      If (Iflg .GT. 2) Then
c
c             If entering new file name
c
 15      Continue
         Write(7,150)
150      Format(' Enter the new file name (return to stop)  '$)
         Read(5,200,END=15,ERR=15) Filnam
200      Format(15A1)
         If (Filnam(1) .EQ. ' ' .AND. Filnam(2) .EQ. ' ') Then
            Write(7,250) 7
250         Format(' Routine halted since no file name was given',1A1)
            Iflag = 1
            Return
         End If
         Go To 10
      End If
20    Continue
c
c          See If there is enough room for it
c
      Open(UNIT=12,NAME=Filnam,TYPE='NEW',INITIALSIZE=Isiz,
     $   ERR=40)
c
c     There is enough room.
c
      Iflag = 0
      Close(Unit=12,DISPOSE='DELETE')
      Return
c
c        Not enough room (or a bad device name)
c
40    Continue
      Write(7,300) 7,7
300   Format(' ERROR - Insufficient File Space - Try again',2A1)
      Close(Unit=12,DISPOSE='DELETE')
      Go To 15
      End
      Subroutine Chknam(Filnam,IFlag)
c
c ***********************************************************************
c *                                                                     *
c *        Subroutine Chknam - To see if a file name syntax is bad      *
c *        Note: The location of the colon in the string is returned    *
c *        since the routine does not know what are legitimate device   *
c *        names are.  Therefore, if ICOLON is nonzero, and there is an *
c *        error opening a file, a reason could be a bad device name.   *
c *                                                                     *
c *        Input values:                                                *
c *                                                                     *
c *        Filnam - Character string containing the file name to check  *
c *                                                                     *
c *        Output values:                                               *
c *                                                                     *
c *        Iflag  - Flag to tell program to abort option                *
c *                                                                     *
c *          Programmed by Steven C. Fisher, DTNSRDC Code 1522          *
c *                                                                     *
c ***********************************************************************
c
      Byte Filnam(15)
      Integer Period,Icolon,Blank
      Integer Devnam(10)
      Byte Dev(20)
      Data Devnam/'SY','DK','WF','WP','DP','LD','DL','DR','DU','DY'/
      Equivalence (Dev(1),Devnam(1))
c
      Iflag = 1
c
c        See if a file name was given
      If (Filnam(1) .EQ. ' ' .AND. Filnam(2) .EQ. ' ') Then
         Write(7,100) 7
100      Format(' Routine halted since no file name was given',1A1)
         Return
      End If
c
c        Set the pointers for legal characters which may be used
c        illegally
c
 10   Continue
c
      Period = 0
      Icolon = 0
      Blank = 0
      Noblnk = 0
c
c         Check each character out
c
      Do 20 I=1,15
c
         If (Filnam(I) .GE. 'a' .AND. Filnam(I) .LE. 'z') Filnam(I) =
     $            Filnam(I) - 32
         Ival = Filnam(I)
c
c           Check for the colons
c
         If (Ival .EQ. 58) Then
            If (Icolon .NE. 0 .OR. I .LT. 3 .OR. Period .NE. 0 .OR.
     $          I .GT. 4) Go To 70
            Icolon = I
         End If
c
c           Check for the periods; see if the name is too long;
c
         If (Ival .EQ. 46) Then
            If (Period .NE. 0 .OR. I .EQ. 1 .OR. (I-Icolon) .GT. 7
     $          .OR. (I-Icolon) .EQ. 1) Go To 70
            Period = I
         End If
c
c           Check for illegal blanks
c
         If (Ival .EQ. 32) Then
            If ( (I-Blank) .NE. 1 .AND. Blank .GT. 0) Go To 70
            Blank = I
         Else
c
c             See if the file extension is too long
c
            If ( (I-Period)  .GT. 3 .AND. Period .GT. 0) Go To 70
            Noblnk = I
         End If
c
c          Check for numbers and letters
c
c
c          Numbers:
c
         If (Ival .GE. 48 .AND. Ival .LE. 57) Go To 20
c
c          Uppercase letters
c
         If (Ival .GE. 65 .AND. Ival .LE. 90) Go To 20
c
c          Skip for periods, colons, and blanks
c
         If (Ival .EQ. 32 .OR. Ival .EQ. 58 .OR. Ival .EQ. 46) Go To 20
c
c          Look for the Ctrl-C characters, and call subroutine Abort
c
         If (Ival .EQ. 3) Call Abort
c
         Go To 70
c
 20   Continue
c
c           If there is a device name, check to see if it is good
c
      If (Icolon .GT. 0) Then
         Do 30 I = 1,10
            If (Filnam(1) .EQ. Dev((I-1)*2+1) .AND. Filnam(2) .EQ.
     $          Dev(I*2) .AND. ((Filnam(3) .GE. '0' .AND. Filnam(3)
     $          .LE. '9') .OR. Filnam(3) .EQ. ':')) Go To 40
 30      Continue
         Go To 70
 40      Continue
      End If
c
c        If the last character wasn't a blank, it's a bad file name
c        Also, if there are only blanks (Noblnk = 0), it's a bad name
c
      If (Noblnk .GT. 0) Then
         Iflag = 0
         Return
      End If
c
70    Continue
c
c        Bad file name - try to get a good one
c
      Write(7,200)
200   Format(' ERROR - Bad file name; please enter a new name: (return',
     $       ' to stop)')
      Read(5,300,END=70,ERR=70) Filnam
300   Format(15A1)
      If (Filnam(1) .EQ. ' ' .AND. Filnam(2) .EQ. ' ') Then
         Write(7,100) 7
         Return
      End If
      Go To 10
c
      End
