      subroutine SETPSW
      include 'SIZE.INC'
      include 'INPAR.INC'
      include 'UNITS.INC'
      real RESULT
      integer CC, EOF, FIVE, INUNIT, LENPAS, RWOPT, WHICH
      integer NOPASS(5),MINPAS(5),IMBED,MATCH,IREC,II,NUSED,J
      character*1 CBUFF(VARCEC), PASOPT(23), BLANK
      character REQFMT(3)*63
      data FIVE,LENPAS/5,23/
      data REQFMT(1)/
     &'(''   Quit   -- cancel command and return to EXAMS prompt-> ''  )
     &'/
      data REQFMT(2)/
     &'('' Please enter the Store Access Password-> ''                 )
     &'/
      data REQFMT(3)/
     &'('' Enter new Password (up to six characters)-> ''              )
     &'/
      data BLANK,NOPASS,MINPAS /' ', 6,5,4,4,4, 1,1,1,1,1/
      data PASOPT /'R','E','C','A','L','L', 'S','T','O','R','E',
     &'B','O','T','H', 'H','E','L','P', 'Q','U','I','T'/
      REQFMT(1)(61:62) = CRSTOP(MACHNO)
      REQFMT(2)(46:47) = CRSTOP(MACHNO)
      REQFMT(3)(49:50) = CRSTOP(MACHNO)
      INUNIT = TTYIN(MACHNO)
      START = IMBED(INPUT,STOPIT)
      if (START.ne.-999) go to 110
100   continue
      write (TTYOUT(MACHNO),5000)
5000  format (/' Enter the type of password protection desired:'/'   ReC
     &all -- to prevent unauthorized access to the dataset'/'   Store  -
     &- to prevent unauthorized changes or erasure'/'   Both   -- to use
     & the same password for Read and Write'/'   Help   -- for further i
     &nformation')
      write (TTYOUT(MACHNO),REQFMT(1))
      call INREC (EOF,INUNIT,INPUT)
      if (EOF.eq.1) go to 180
      START = IMBED(INPUT,0)
      if (START.eq.-999) go to 100
110   continue
      call SKAN (INPUT,START,STOPIT,TYPE,BLANK,1)
      if (TYPE.eq.100) STOPIT = 81
      WHICH = MATCH(FIVE,LENPAS,NOPASS,PASOPT,MINPAS)
      if (WHICH.eq.0) then
         write (TTYOUT(MACHNO),5010)
5010     format (/' Response not understood; please try again.')
         go to 100
      endif
      go to (130,130,130,120,190), WHICH
120   continue
      write (TTYOUT(MACHNO),5020)
5020  format (/' Use the PASSWORD command to restrict access to'/' datas
     &ets in any of EXAMS four User DataBases.'/' You can prevent unauth
     &orized retrieval of the data (with'/' a Recall password), and/or u
     &nauthorized erasure or revision'/' of the dataset (with a Store pa
     &ssword).  You can use a'/' different character string for each pas
     &sword, or you'/' can set both passwords to the same value with "Bo
     &th."')
      go to 100
130   continue
      RWOPT = WHICH
      call DECOD1 (5,WHICH,CC,RESULT)
      if (CC.gt.1) go to 190
      IREC = ifix(RESULT)
      call DECOD2 (IREC,CC,WHICH,II,CBUFF)
      if (CC.eq.3) go to 190
      if (CC.eq.2) then
         write (TTYOUT(MACHNO),5030)
5030     format (' This dataset is empty ... passwords prohibited.')
         go to 190
      endif
      read (RANUNT(MACHNO),rec=II) CBUFF
      NUSED = 50
      do 140 J = 1, 6
         RPASS(WHICH)(J:J) = CBUFF(NUSED+1)
         WPASS(WHICH)(J:J) = CBUFF(NUSED+2)
         NUSED = NUSED+2
140   continue
      if (WPASS(WHICH).eq.'GLOBAL') go to 150
      write (TTYOUT(MACHNO),5040)
5040  format (/' This entry is protected from unauthorized changes.')
      write (TTYOUT(MACHNO),REQFMT(2))
      call INREC (EOF,INUNIT,INPUT)
      if (EOF.eq.1) then
         write (TTYOUT(MACHNO),5050)
5050  format (/' End-of-file while reading password; PASSWORD aborted.')
         go to 190
      endif
      START = IMBED(INPUT,0)
      if (START.eq.-999) then
         write (TTYOUT(MACHNO),5060)
5060     format (/' PASSWORD command cancelled.')
         go to 190
      endif
      if (INPUT(START:START+5).eq.WPASS(WHICH).or.INPUT(START:START+5)
     &   .eq.SYSPAS) then
         INPUT = ' '
         STOPIT = 0
         go to 150
      endif
      write (TTYOUT(MACHNO),5070)
5070  format (/' Password does not match.  Please check your records;'/'
     & The PASSWORD command has been cancelled.')
      go to 190
150   continue
      START = IMBED(INPUT,STOPIT)
      if (START.ne.-999) go to 160
      write (TTYOUT(MACHNO),REQFMT(3))
      call INREC (EOF,INUNIT,INPUT)
      if (EOF.eq.1) go to 180
      START = IMBED(INPUT,0)
      if (START.eq.-999) go to 190
160   continue
      if (RWOPT.eq.1) then
         RPASS(WHICH) = INPUT(START:START+5)
      elseif (RWOPT.eq.2) then
         WPASS(WHICH) = INPUT(START:START+5)
      elseif (RWOPT.eq.3) then
         RPASS(WHICH) = INPUT(START:START+5)
         WPASS(WHICH) = INPUT(START:START+5)
      endif
      NUSED = 50
      do 170 J = 1, 6
         CBUFF(NUSED+1) = RPASS(WHICH)(J:J)
         CBUFF(NUSED+2) = WPASS(WHICH)(J:J)
         NUSED = NUSED+2
170   continue
      write (RANUNT(MACHNO),rec=II) CBUFF
      go to 190
180   continue
      write (TTYOUT(MACHNO),5080)
5080  format (/1X,'End-of-file marker in input; command cancelled.')
      go to 190
190   continue
      WPASS(WHICH) = 'GLOBAL'
      RPASS(WHICH) = 'GLOBAL'
      return
      end
