      subroutine filnam (outfil,filext,iread)                           FILN001
c ----------------------------------------------------------------------FILN002
c     truncates filename and adds default extension                     FILN003
c ----------------------------------------------------------------------FILN004
c     This program module is part of the PhysiSoft X-ray Library        FILN005
c     and may not be sold or reproduced for commercial purposes.        FILN006
c     Although it has been tested carefully, there is no warranty       FILN007
c     regarding accuracy or freedom from side-effects.                  FILN008
c                   (C) 1986-1990 Bernhard Rupp                         FILN009
c ----------------------------------------------------------------------FILN010
                                                                        FILN011
      parameter (istr = 25)                                             FILN012
      parameter (iext = 4)                                              FILN013
                                                                        FILN014
      character s(istr), outfil*25, filext*4                            FILN015
      character form1*1, form2*6,form3*9                                FILN016
                                                                        FILN017
      if (iread.eq.1) then                                              FILN018
         write (*,'(a$)') '  File > '                                   FILN019
         read (*,'(25a1)') (s(i),i=1,istr)                              FILN020
         do 99 i=1,25                                                   FILN021
   99       call upstrg(s(i),1)                                         FILN022
      else                                                              FILN023
         call upstrg (outfil,25)                                        FILN024
         read (outfil,'(25a1)') (s(i),i=1,istr)                         FILN025
      end if                                                            FILN026
                                                                        FILN027
      if (filext.eq.'NONE') then                                        FILN028
         write (outfil,'(25a1)') (s(i),i=1,istr)                        FILN029
         return                                                         FILN030
      end if                                                            FILN031
                                                                        FILN032
c --  end after invalid char or space --                                FILN033
      do 3002 i=1,istr                                                  FILN034
         if(s(i).eq.' '.or.s(i).eq.'.'.or.s(i).eq.','.or.s(i).eq.';')   FILN035
     &   goto 0002                                                      FILN036
 3002 continue                                                          FILN037
 0002 i=i-1                                                             FILN038
      if (i.lt.1) then                                                  FILN039
         outfil='NULL'                                                  FILN040
         return                                                         FILN041
      end if                                                            FILN042
                                                                        FILN043
c --  create a run time format, form3 is the format string --           FILN044
      if(i.gt.(istr-iext)) i=istr-iext                                  FILN045
      form1='('                                                         FILN046
      if (filext.eq.'DEEX') then                                        FILN047
         form2='a1)'                                                    FILN048
         write(form3,'(a1,i2,a3)') form1,i,form2                        FILN049
         write(outfil,form3) (s(j),j=1,i)                               FILN050
      else                                                              FILN051
         form2='a1,a4)'                                                 FILN052
         write(form3,'(a1,i2,a6)') form1,i,form2                        FILN053
         write(outfil,form3) (s(j),j=1,i),filext                        FILN054
      end if                                                            FILN055
      return                                                            FILN056
      end                                                               FILN057
                                                                        FILN058
