! call copen(lun,subdir,file,err) !open "CONFIG" readonly ! arguments are: int, char,char, logical ! COPEN tries in 4 locations to open a "config" type file: ! There are 4 places it looks: ! 1. Starting closest to current dir: config/file ! 1. Next closest: subdir/config/file ! 3. Then Users defined $ACC_USERCONFIG/subdir/file ! 4. Finally look in $ACC_CONFIG/subdir/file ! 4th is ("official"), but will not usually be current. ! If subdir string contains no nonblanks, copen will, as ! with "opener", Just expand file, ignore subdir and open ! wherever the expanded filename leads. ! If lun <= 0, then lunget will be called for it subroutine copen(ll,subdir,file,er) use cesr_utils implicit none integer ll,ierr,case,lb,le character*(*) file,subdir character*256 filex,befexp logical er,valid,hasconfig er=.false. !assume success if (ll <= 0) ll = lunget() hasconfig=(index(file,'_CONFIG').gt.0) !try to save user that forgets and ! includes environment variable in filename if((hasconfig).or.(len_trim(subdir).lt.1)) then !Oldfashioned user befexp=file !save with enviroment var call fullfilename(file,filex,valid) open(unit=ll,file=trim(filex),action='read',status='old',iostat=ierr) if(ierr.ne.0) goto 666 else !there is a directory specified, so try 4 possible cases do case=1,4 if(case.eq.1) filex='config/'//trim(file) if(case.eq.2) filex='~/config/'//trim(subdir)//'/'//trim(file) if(case.eq.3) filex='$ACC_USERCONFIG/'//trim(subdir)//'/'//trim(file) if(case.eq.4) filex='$CESR_CONFIG/'//trim(subdir)//'/'//trim(file) befexp=filex !file with possible enviro vars, before expand call fullfilename(filex,filex,valid) open(unit=ll,file=trim(filex),action='read',status='old',iostat=ierr) if((ierr.ne.0).and.(case.eq.4)) goto 666 !4 strikes! if(ierr.eq.0) then lb=len_trim(befexp) ; le=len_trim(filex) if((le+lb).lt.70) then if(copen_rpt.gt.0) print *,'CONFIG file: '//trim(befexp)//' ie: '//trim(filex) else if((copen_rpt == 1)) print *,'CONFIG file: '//trim(befexp) if((copen_rpt == 2)) print *,'IE: '//trim(filex) endif return endif enddo endif return 666 print *,' Subroutine copen err=',ierr,' Unit=',ll print *,' For file '//trim(befexp) print *,' That is: '//trim(filex) er=.true. return end subroutine copen