APPNOTE: Accessing Command-Line Arguments with FORTRAN

Last reviewed: July 17, 1995
Article ID: Q65236
The information in this article applies to:
  • Microsoft FORTRAN for MS-DOS, versions 4.0, 4.01, and 4.1
  • Microsoft FORTRAN for OS/2, versions 4.0, 4.01, and 4.1

SUMMARY

An application note titled "Accessing Command-Line Arguments with FORTRAN," which explains how to access the command-line arguments under Microsoft FORTRAN versions 4.00, 4.01, and 4.10, is available from Microsoft Product Support Services.

This file has been removed from the Software Library but can be requested by calling Microsoft Product Support Services. The functions included in this application note use the PSP (Program Segment Prefix) to determine the location of the command-line arguments in memory. OS/2 does not implement the PSP, and therefore these functions do not work correctly under the OS/2 operating system. In addition, the environment that is also performed through the PSP is inaccessible.

FORTRAN versions 5.0 and 5.1 contain the intrinsic functions getarg and narg to access command line arguments from within a FORTRAN program.

MORE INFORMATION

This article contains the same information as the application note.

   Microsoft Product Support Services Application Note (Text File)
        HF0220: ACCESSING COMMAND-LINE ARGUMENTS WITH FORTRAN
                                                   Revision Date: 8/91
                                                      No Disk Included

The following information applies to Microsoft FORTRAN versions 4.0, 4.01, and 4.1.

 --------------------------------------------------------------------
| INFORMATION PROVIDED IN THIS DOCUMENT AND ANY SOFTWARE THAT MAY    |
| ACCOMPANY THIS DOCUMENT (collectively referred to as an            |
| Application Note) IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY      |
| KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO    |
| THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A     |
| PARTICULAR PURPOSE. The user assumes the entire risk as to the     |
| accuracy and the use of this Application Note. This Application    |
| Note may be copied and distributed subject to the following        |
| conditions: 1) All text must be copied without modification and    |
| all pages must be included; 2) If software is included, all files  |
| on the disk(s) must be copied without modification [the MS-DOS(R)  |
| utility DISKCOPY is appropriate for this purpose]; 3) All          |
| components of this Application Note must be distributed together;  |
| and 4) This Application Note may not be distributed for profit.    |
|                                                                    |
| Copyright 1990-1991, Microsoft Corporation. All Rights Reserved.   |
| Microsoft, MS-DOS, and the Microsoft logo are registered           |
| trademarks and Windows is a trademark of Microsoft Corporation.    |
 --------------------------------------------------------------------

Summary

This application note explains how to access command-line arguments with Microsoft FORTRAN versions 4.0, 4.01, and 4.1.

Below is the code for the following FORTRAN files:

  Filename      Description
  --------      -----------

  PSPTST.FOR    Main program that contains the INTERFACE to DMPCMD and
                calls DMPCMD

  DMPCMD.FOR    Subroutine that prints the contents of the command
                line

  Note: DMPCMD must be compiled in a separate source file.


How to Use PSPTST

Use the following command to compile the two programs:

  FL PSPTST.FOR DMPCMD.FOR

If PSPTST is invoked with a command line such as

  PSPTST hello

the output will be as follows:

  < hello>

In FORTRAN versions 5.0 and 5.1, command-line arguments can be accessed using the NARGS function and the GETARG procedure described on page 271 of the "Microsoft FORTRAN Reference" manual for version 5.0 or 5.1.

The PSPTST Program

c The INTERFACE statement is used to pass the addresses of the start c of the command line and the number of characters in the command line c to the separately compiled subroutine, DMPCMD, by value, instead of c by reference. The variables are passed by value because they are c themselves addresses. The DMPCMD subroutine is compiled separately c so that it will accept its arguments by reference. This results in c the two passed addresses being properly "dereferenced." c

      INTERFACE TO SUBROUTINE DMPCMD( II,JJ )
      INTEGER*4 II [VALUE], JJ [VALUE]

      END
c c --------------------------------------------------------------------
      PROGRAM PSPTST
      INTEGER*4 PSP, PSPNCH, OFFSET
c -------------------------------------------------------------------- c The method used in this program will work only if the Program c Segment Prefix precedes the main program, which it will by c default. Since the PSP starts 16 paragraphs (256 bytes) before c the main program, the first step is to load the variable OFFSET c with the hex value 0010:0000, as follows: c
      OFFSET = #00100000
c -------------------------------------------------------------------- c Use LOCFAR to find the segment:offset of the main program: c
      PSP = LOCFAR(PSPTST)
c --------------------------------------------------------------------

c To set PSP so that it points to the start of the text of the c command line, do the following: c c 1. Zero out the offset portion of the address in variable PSP. c c 2. Subtract 16 paragraphs from the segment:offset. c c 3. Add hex 81 so that PSP now points to the start of the text of the c command line: c

      PSP = (PSP-MOD(PSP,#10000))-OFFSET+#81
c -------------------------------------------------------------------- c PSPNCH points to the byte (80 hex) in the PSP that contains the c length of the command line: c
      PSPNCH = PSP-1
c -------------------------------------------------------------------- c Call DMPCMD to list out the command line: c
      CALL DMPCMD(PSP, PSPNCH)
      END


THE DMPCMD SUBROUTINE

Note: Compile this routine in a file separate from PSPTST.

      SUBROUTINE DMPCMD(CMDLIN, N)
      CHARACTER*80 CMDLIN
      INTEGER*1 N
c c Write out N characters from CMDLIN (N is PSP+80 hex; CMDLIN is c PSP+81 hex). c
      WRITE (*,*) '<',CMDLIN(:N),'>'
      RETURN
      END


Additional reference words: 4.00 4.10 appnote HF0220
KBCategory: kbprg
KBSubcategory: FORTLngIss


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.