      block data bargenv
      logical qop
      common /argenv/ iuae,qop
      data iuae/69/,qop/.false./
      end

      integer function open_argenv (iu)
      logical qop,qop69
      character s*16
      common /argenv/ iuae,qop

      if (.not.qop) then
         iuae=iu
 1       inquire (unit=iuae,opened=qop69)
         if (qop69) then
            iuae=iuae+1
            goto 1
         endif
c         s='69 B x.args'
c         write (s(1:2),'(i2)') iuae
c         call openfs (s)
         open (unit=iu,file='x.args',form='formatted')
         qop=.true.
      endif
      open_argenv=iuae
      return
      end

      integer function close_argenv ()
      logical qop,qop69
      character s*(*)
      common /argenv/ iuae,qop

      if (qop) then
         close (iuae)
      endif
      qop=.false.
      close_argenv=iuae
      return
      end

      subroutine get_arg (i,s)
      logical qop,qop69
      character s*(*)
      integer open_argenv
      common /argenv/ iuae,qop

      if (.not.qop) then
         iu=iuae
         ier=open_argenv(iu)
      endif
      if (qop) then
         rewind (iuae)
         do ii=1,i
            read (iuae,'(a)',end=99) s
            if (index(s,'-- ENVIRONMENT >').gt.0) goto 99
c                     1234567890123456
         enddo
      endif
      return
 99   s=' '
      return
      end

      subroutine get_env (pn,pv)
      implicit logical (q)
      character pn*(*),pv*(*),s*1024
      integer open_argenv
      common /argenv/ iuae,qop
      
      if (.not.qop) then
         iu=iuae
         ier=open_argenv(iu)
      endif
      pv=''
      if (qloc_in_file(iuae,'-- ENVIRONMENT >','Q')) then
         if (qloc_in_file(iuae,pn,'NBQ')) then
            read (iuae,'(a)') s
            i=index(s,'=')+1
            j=index_l_nonblank(s)
            pv=s(i:j)
         endif
      endif
      return
      end

      
