Sun Studio 12: Fortran Programming Guide

2.1.4.2 Via Environment Variables and GETENV

Similarly, the library routine getenv(3F) can be used to read the value of any environment variable at runtime into a character variable that in turn is interpreted as a file name:


demo% cat testenv.f
         CHARACTER outfile*40
C  Get $OUTFILE as output file name for unit 51
         CALL getenv(’OUTFILE’,outfile)
         OPEN(51,FILE=outfile)
         WRITE(51,*) ’Writing to file: ’, outfile
         END
demo% f95 -o tstenv testenv.f
demo% setenv OUTFILE EnvFileName
demo% tstenv
demo% cat EnvFileName
 Writing to file: EnvFileName
demo%

When using getarg or getenv, care should be taken regarding leading or trailing blanks. (Fortran 95 programs can use the intrinsic function TRIM, or the older FORTRAN 77 library routine LNBLNK()) Additional flexibility to accept relative path names can be programmed along the lines of the FULLNAME function in the example at the beginning of this chapter.