Quote:
> Hi.
> I am using f77 on an IBM RISC/6000 machine running under AIX4.1 (Unix).
> I would need to read the arguments I pass to a certain program on the command
> line. <snip>
Maybe the enclosed code can be useful.
--
M.Sc. Mech. Eng. Baldersg. 4, SE-411 02 GOETEBORG, SWEDEN
Phone: Int + 46 340 66 75 87, Fax: Int + 46 340 66 76 06
logical function getclarg(iarg, ciarg)
c --- ------------------------------------------------------------------
c ... the purpose of function GETCLARG - GET Command Line ARGuments -
c is to return command line argument number IARG in the character
c variable CIARG, and to return TRUE if this was successful.
c
c If argument number IARG does not exist, FALSE is returned.
c
c In case of more serious troubles, execution is stopped in this
c module.
c
c Anders Henoch, Akustikon Partner , 1991-03-15
c --- ------------------------------------------------------------------
c 1992-02-03 AHH Rewritten for UNIX/Silicon Graphics. Calls library
c functions GETARG and IARGC
c 1991-06-01 AHH Assignment of CIARG removed in case of GETCLARG
c returning false.
c --- ------------------------------------------------------------------
c
implicit none
c ... declarations parameter list
c
integer iarg
character*(*) ciarg
c ... local declarations
c
integer*2 status2
integer liargc
logical test
parameter (test = .false.)
character*128 cline
integer lena
external lena
c ... unix declarations:
c
integer iargc
external iargc
c
C --- ------------------------------------------------------------------
liargc = iargc() ! ... get number of command line arguments
if (liargc .lt. iarg) then
status2 = -1
else
call getarg(iarg, cline)
status2 = lena(cline)
endif
if (test) write(*,*) 'Status2 = ', status2
c ... (status2 .gt. 0) considered "normal" or "succesful"
c
if (status2 .gt. 0) then
c
c ...... in addition we test lengths of character variables, thus
c ensuring that nothing will be truncated. GETARG does not
c check this.
c
if (status2 .gt. len(cline)) goto 99903
if (status2 .gt. len(ciarg)) goto 99902
ciarg = cline
getclarg = .true.
return
elseif (status2 .eq. -1) then
c
c ...... (status2 .eq. -1) indicates that the requested command line
c argument was not present. This is "caught" by flag GETCLARG
c above.
c
getclarg = .false.
return
elseif (status2 .lt. -1) then
c ...... (status2 .lt. -1) indicates serious/unexpected problems
c and will here stop execution.
c
goto 99901
else
goto 99904
endif
return
c --- ------------------------------------------------------------------
c ... error returns
99901 continue
write(*,*) '*** ERROR *** - GETARG call failed'
write(*,*) 'STATUS = ', status2
goto 99999
99902 continue
write(*,'(1X, A)')
& '*** ERROR *** - insufficient length allocated for argument'
& , 'number 2 in the module calling GETCLARG.'
& , 'This argument must be a CHARACTER variable, large enough to'
& , 'hold any command line argument to be read by GETCLARG.'
write(*,*)
write(*,*) 'At present the size is: ', len(ciarg)
write(*,*) 'but should have been at least: ', status2
write(*,*) 'in order to hold the command line argument :'
write(*,*) '-->', cline(1:lena(cline)), '<--'
write(*,*)
write(*,'(1X,A)')
& 'Reduce the length of your command line arguments, '
& , 'or recompile the module calling GETCLARG with an increased '
& , 'length for the second argument to GETCLARG'
write(*,*)
goto 99999
99903 continue
write(*,'(1X, A)')
& '*** ERROR *** - insufficient length allocated for local'
& , 'variable CLINE in module GETCLARG'
& , 'This argument must be a CHARACTER variable, large enough to'
& , 'hold any command line argument to be read by GETCLARG.'
write(*,*)
write(*,*) 'At present the size is: ', len(cline)
write(*,*) 'but should have been at least: ', status2
write(*,*) 'in order to hold the (here truncated) '
& // 'command line argument :'
write(*,*) '-->', cline(1:lena(cline)), '<--'
write(*,*)
write(*,'(1X,A)')
& 'Reduce the length of your command line arguments, '
& , 'or recompile the module GETCLARG with an increased '
& , 'length for variable CLINE'
write(*,*)
goto 99999
99904 continue
write(*,*) 'ERROR - unexpected value of STATUS2 in module'
write(*,*) 'GETCLARG'
write(*,*) 'STATUS2 = ', status2
goto 99999
99999 continue
stop 'STOP in module GETCLARG'
end
c lena
c
integer function lena(string_in)
c --- -----------------------------------------------------------------
c ... the purpose of integer function LENA (LENgth Assigned) is to re-
c turn the length of character*(*) variable string_in exclusive
c trailing blanks or nulls
c Anders Henoch, SIGMA ENGINEERING, 1987-11-09
c --- -----------------------------------------------------------------
c 1992-05-14 AHH IBM AIX does not accept concatenation of string
c constants in parameter statements
c 1991-03-10 AHH The searching for both BLANKS and NULLS was once in-
c cluded since uninitialized string variables in some
c environments did contain nulls.
c However, this may differ from installation to instal-
c lation, and any use of uninitialized variables must
c be considered bad programming practice.
c Therefore, a flag BLANKS_ONLY is introduced, which
c may be disabled if the comparison is to be done with
c NULLS, too.
c In addition, a flag COMPILER is introduced, which,
c if set to 'MSF', makes use of some MicroSoft Fortran
c extensions.
c --- -----------------------------------------------------------------
c
implicit none
c ... declarations parameter list
c
character*(*) string_in
c
c --- -----------------------------------------------------------------
c ... local declarations
c
c ... define if NULLS should be checked for
c
logical BLANKS_ONLY, isblank
parameter (BLANKS_ONLY = .true.)
character*2 charset
c parameter (charset = CHAR(32) // CHAR(0))
integer*2 i, ic
c
c --- -----------------------------------------------------------------
c ... code begins here
c
charset = CHAR(32) // CHAR(0)
do i = len(string_in), 1, -1
ic = ichar(string_in(i:i))
if (BLANKS_ONLY) then
isblank = (ic .eq. 32)
else
isblank = ((ic .eq. 32) .or. (ic .eq. 0))
endif
if ( isblank) then
c do nothing
else
lena=i
return
endif
enddo
c
c ... loop run to completion -> string_in consists of all blanks
c and/or nulls -> lena = 0
c
lena = 0
return
end