Sun Studio 12: Fortran Library Reference

1.4.15 getarg, iargc: Get Command-Line Arguments

getarg and iargc access arguments on the command line (after expansion by the command-line preprocessor.

1.4.15.1 getarg: Get a Command-Line Argument

The subroutine is called by:

call getarg( k, arg )

k

INTEGER*4

Input 

Index of argument (0=first=command name) 

arg

character*n

Output 

kth argument

n

INTEGER*4

Size of arg

Large enough to hold longest argument 

1.4.15.2 iargc: Get the Number of Command-Line Arguments

The function is called by:

m = iargc()

Return value 

INTEGER*4

Output 

Number of arguments on the command line 

Example: iargc and getarg, get argument count and each argument:


demo% cat yarg.f
       character argv*10
       INTEGER*4 i, iargc, n
       n = iargc()
       do 1 i = 1, n
         call getarg( i, argv )
 1       write( *, ’( i2, 1x, a )’ ) i, argv
       end
demo% f95 yarg.f
demo% a.out *.f
1 first.f
2 yarg.f

See also execve(2) and getenv(3F).