From: aswx266@chpc.utexas.edu (Michael Lemke)
Subject: Re: home directory: better question
Organization: The University of Texas System - CHPC
Date: Sun, 11 Oct 92 00:00:25 GMT

In article <1992Oct10.050200.22047@ncar.ucar.edu> rlink@cedar.hao.ucar.edu (Richard Link) writes:
>I guess I did not define my problem very well when I posted an earlier
>question on home directory expansions in unix.
>
>My problem is this:
>
>I need to deliver some portable software. It has to run on VMS, UNIX,
>and MS-DOS (at least).
>
>The programs need to access invariant database files.
>
>I want to specify these files without hard-wiring the path within the
>fortran program, since this would require modification of the
>fortran source for every site, which may number in the hundreds.
>
>If I knew where the `home' directory was, this would not be a problem.
>Using unix getarg() and command line arguments does not work for VMS,
>and I would like to make the code as system-independent as possible.
>
>In VMS, one can do the following in a command shell script (*.com) :
>
>$  user = f$getjpi("","username")               ! /path/.../whoami
>$  user = f$extract(0,f$locate(" ",user),user)  ! whoami
>$  data_dir:=['user'.dbase]                     ! concatenate directory
>$  assign 'data_dir'fname.dat FNAME             ! concatenate filename
>
>This assigns the equivalent of ~/whoami/dbase/fname.dat to a variable
>called FNAME within the command shell.
>
>VMS fortran recognizes the assigned file name as an environment variable,
>so to open a file, one can use:
>
>      character*48 fname/'FNAME'/
>      open(unit=iunit, file=fname, status='old')
>
>Thus, the command shell script gets the username and concatenates the
>directory and filename, which is then passed to the fortran program.
>Neither the shell script nor the fortran code need to know the explicit
>path.
>
>So my question is this: how can I get around specifying the full path
>in the fortran code portable across these operating systems?
>Or, more realistically, what can I do to make sure that the fortran
>programs find the data files in the most portable manner?
>

For this kind of problem I would suggest you use logical names or 
environment variables, say RDBDIR.  You can default them to someyhing
like SYS$LOGIN_DEVICE:[user.dbase] or ~/dbase.  In any case you will
need a little routine which looks slightly differnt on the individual
machines but will not contain any explicit reference to the user's home
directory.  For VMS I'd suggest something like (warning, not tested!)


	subroutine get_dbasse_location( location, length )
	implicit none
	character*(*) location
	integer length

	integer lib$sys_trnlog, lib$signal
	integer istat
	character*255 rdbdir

	istat = lib$sys_trnlog( 'RDBDIR', length, rdbdir )  ! Try if RDBDIR
                                                            ! is defined

	if( .not.istat .or. rdbdir .eq. ' ' ) then
C ***
C ***	    not defined, so default it to SYS$LOGIN_DEVICE:[user.dbase]
C ***	    I use SYS$LOGIN because it saves me the triuble to get the username
C ***
	    istat = lib$sys_trnlog( 'SYS$LOGIN', length, rdbdir )
	    if( .not.istat ) call lib$signal( %val(istat) )	! error check
	    rdbdir( length - 1: ) = '.dbase]'
	    length = length + 6
	end if

	end

On Unix try something like

	subroutine get_dbasse_location( location, length )
	character*(*) location
	integer length

	character*255 rdbdir

	call getenv( 'RDBDIR', rdbdir )
	if( rdbdir .eq. ' ' ) then
	   call getenv( 'HOME', rdbdir )
	   rdbdir( lnblnk(rdbdir) + 1: ) = '/dbase/'  ! lnblnk returns last
                                                      ! non-blank char in a 
                             ! string; might not be available on every Unix box
	end if
	length = lnblnk( rdbdir )

	end

For DOS, no idea but as far as I know it has envrironment variables too.

Then you can open your file with

	character*255 rdbdir

	call get_dbase_location( rdbdir, length )
	OPEN( 1, file = rdbdir(:length)//'blabla.dat' )


This method has the additional advantage that the database can be relocated
to any place without a need for changing the program.  Only RDBDIR 
needs to be redefined like DEFINE RDBDIR DISK$1:[USER.FOO.BAR] or 
setenv RDBDIR /usr1/user/foo/bar  You also don't need any command 
procedures/scripts.

Hope this gives you some ideas how to proceed.
-- 
Michael Lemke
Astronomy, UT Austin, Texas
(michael@io.as.utexas.edu or UTSPAN::UTADNX::IO::MICHAEL [SPAN])
Newsgroups: comp.lang.fortran