[comp.lang.fortran] How to load executables and transfer variable name to FORTRAN

flatau@handel.CS.ColoState.Edu (flatau) (08/09/90)

Say, I have a file "prog.f"

     program main
     character*8 name
     ... possibly missing code ... (see explanation below)
     open(8,file=name, status='old')
     stop
     end

Compile it as (UNIX):

f77 prog.f -o prog

and execute "prog" with a parameter, e.g.

prog 'file.dat'

Is the above possible (loading step) ?
Is there a way to assign "name" to 'file.dat'  from within
a fortran code ? How should I modify "prog.f" to make it work ?

flatau@handel.cs.colostate.edu

khb@chiba.Eng.Sun.COM (Keith Bierman - SPD Advanced Languages) (08/09/90)

In article <8386@ccncsu.ColoState.EDU> flatau@handel.CS.ColoState.Edu (flatau) writes:

...
      and execute "prog" with a parameter, e.g.

   prog 'file.dat'

Well, this works on most unix systems (from the SunOS man pages)

NAME
     getarg, iargc - get the kth command line argument

SYNOPSIS
     subroutine getarg ( k, arg )
     character*(*) arg

     function iargc ()

DESCRIPTION
     The statement call getarg( k  ,  arg  )  will  get  the  kth
     command-line argument and put it into arg .

     The 0th argument is the command name.

     The function iargc returns the index of  the  last  command-
     line argument,
     and therefore the number  of  arguments  after  the  command
     name.

EXAMPLE
     demo% cat tesargs.f
          character argv*10
          integer i, iargc, m
          m = iargc()
          i = 1
          do while  ( i .le. m )
               call getarg ( i, argv )
               write( *, '( i2, 1x, a )' ) i, argv
               i = i + 1
          end do
          stop
          end
     demo % a.out first second last
      1 first
      2 second
      3 last
     demo%

FILES
     /usr/lang/SC0.0/libF77.a

SEE ALSO
     execve(2), getenv(3F)


Of course, this is not standard Fortran, not all operating systems
have a notion of command lines (my old Mac running MacOS (pre-MPW) for
example). 
--
----------------------------------------------------------------
Keith H. Bierman    kbierman@Eng.Sun.COM | khb@chiba.Eng.Sun.COM
SMI 2550 Garcia 12-33			 | (415 336 2648)   
    Mountain View, CA 94043

morreale@bierstadt.scd.ucar.edu (Peter Morreale) (08/09/90)

In article <8386@ccncsu.ColoState.EDU>, flatau@handel.CS.ColoState.Edu
(flatau) writes:
|> Say, I have a file "prog.f"
|> 
|> Compile it as (UNIX):
|> 
|> f77 prog.f -o prog
|> 
|> and execute "prog" with a parameter, e.g.
|> 
|> prog 'file.dat'
|> 
|> Is the above possible (loading step) ?
|> Is there a way to assign "name" to 'file.dat'  from within
|> a fortran code ? How should I modify "prog.f" to make it work ?
|> 
|> flatau@handel.cs.colostate.edu


  If the UNIX is SunOS (or UNICOS, I suspect some others as well) 
  you can use the routines:

		  call getarg(k,arg)

		  icnt = iarg()


 GETARG returns the K'th commandline argument to ARG.   ARG should be a 
 CHARACTER variable sufficently large enough to hold ARG.  

 IARGC retuns a count of the number of commandline arguments.

 Example:  (not tested)


	program tst

	character*80 arg
	integer ilen

c
c  Get the commandline argument, assume it is a valid filename
c
	call getarg(1,arg)
c
c  Find out where the last non-null character is in ARG...
c
	ilen= index(arg,char(0))-1

	open(file=arg(1:ilen),unit=10.....)

	..............

	end


Note, GETARG only retuns the blank separated word from the commandline.
You are responsible for checking the arguments, parsing, etc....

IARGC is useful to iterate a loop where more than one argument may be
encountered....


Other UNIXs may provide a Fortran interface to the C  routine: getopt

-PWM
------------------------------------------------------------------
Peter W. Morreale                  email:  morreale@ncar.ucar.edu
Nat'l Center for Atmos Research    voice:  (303) 497-1293
Scientific Computing Division     
Consulting Office
------------------------------------------------------------------