From: nn1@Ra.MsState.Edu (N. Narenthiran)
Newsgroups: comp.lang.fortran
Subject: Re: home directory in unix
Keywords: getenv getarg
Date: 9 Oct 92 16:14:39 GMT
Article-I.D.: Ra.nn1.718647279
Organization: Mississippi State University

rvenable@helix.nih.gov (Richard M. Venable) writes:

>The construct ~/dir/file.ext should be resolved properly if you
>pass it as a command line argument, and fetch it using the standard
>Unix 'getarg' call.

>% program  ~/dir/file.ext

>	CHARACTER*80 fnam
>		:
>	CALL GETARG(1,fnam)
>	OPEN(20,FILE=fnam, [other options] )

>However, if you are getting the filename from UNIT 5 (stdin) or
>a script, or as an explicit string, it won't work.  The simplest
>solution is to parse the filename, checking for '~' as the first
>character, and substitute the text of the enviroment variable HOME
>for '~'.

>	CHARACTER*80 fnam,tmp
>		:
>	IF (fnam(1:1).EQ.'~') THEN
>	  CALL GETENV('HOME',tmp)
>          DO I=1,80
>            IF (tmp(I:I).EQ.' ') GOTO 150
>          ENDDO
>  150	  I=I-1
>          fnam = tmp(1:I)//fnam
> 	ENDIF

  I tried to do this.  For example I made the filename
 "/my_home/my_login/dir/filename"  which I checked and found to be the
correct Unix full path.  But, I still could not use an INQUIRE on this
file.  The system is a the sun Fortran compiler f77 on a Sun machine.
 (and an SGI Iris 4.0 (/4.1 ?) with the default compiler.)
I could use the "../filename" form, but this does not help me
at all (unless I were to subtract the current directory, calculate the
parent directories, etc,.. a mess).  This cannot be a command line
argument for my problem.    Do I need any special libraries
for the executable to find the file given the full, correct path  ?
( I checked and could not find any symbolic links /mounts in the path
 either).  I did not add the -lU77 option as the compiler complains
when I tried this (you say that is need for environment etc only;
the following works without this library) .

my code and output is,
phoenix:/home1/naren/desk/spice [ 32 ] more t.f
        character*80 casewd,string
        logical found
      CALL GETENV ('HOME',STRING)
      CALL INWORD (STRING,ISTART,IEND,NULLIN) (calculates first word
                                               indices)
      IF (NULLIN.EQ.1) THEN   (string is empty !)
        ISTART = 1
        IEND = 1
      END IF
      WRITE (CASEWD,FMT='(2X,A,''/.exrc'')')
     1    STRING(ISTART:IEND+1)
        print *,casewd
      INQUIRE (FILE=CASEWD,EXIST=FOUND)
      IF (.NOT.FOUND) THEN
        print *,'failed'
        else
        print *,'ok'
        open (9,file=casewd)
        endif
        stop
        end
 
phoenix:/home1/naren/desk/spice [ 33 ] a.out
  /home1/naren/.exrc                                                            
failed
phoenix:/home1/naren/desk/spice [ 34 ] ls -l ~/.exrc
-rw-r--r--  1 naren         161 Sep 12 20:04 /home1/naren/.exrc

   what's the deal?

>A similar construct may be used to obtain the value (as a string) of
>ANY environment variable, by checking for $ as the first char of a
>filename.  You usually need to include the f77 option   -lU77   to
>search the Unix libraries during the link step for this to work.

     thanks a lot.

have a nice day.
naren.
Newsgroups: comp.lang.fortran