Microsoft KB Archive/63837

OS/2 COBOL Program to Get Current Directory Using DosQCurDir

PSS ID Number: Q63837 Article last modified on 07-18-1990

3.00 3.00a OS/2

Summary: Microsoft COBOL versions 3.00 and 3.00a protected mode programs can call the OS/2 API function DosQCurDir to get the current directory of a drive. These versions of COBOL can call API functions directly by prefixing their names with a double underscore (__) and linking with the PCOBOL.LIB and DOSCALLS.LIB libraries. This information applies to Microsoft COBOL Compiler versions 3.00 and 3.00a for MS OS/2.

More Information: DosQCurDir requires the following parameters:

Note that a WORD (2 bytes) has a picture clause of “9(4) COMP-5”. When a pointer to a parameter is required (PTR), it must be passed using the BY REFERENCE clause; otherwise, BY VALUE is used. In addition, the parameters must be passed in reverse order because API functions use a calling convention that is the reverse of COBOL’s. IMPORTANT NOTE: If DosQCurDir is passed a drive number that does not exist, the process will hang. Therefore, it is very important to get a list of all available drives beforehand. A separate article in this Knowledge Base explains how to do this; this article can be found by querying on the following words: DosQCurDisk and Available and COBOL For more information on calling OS/2 API functions from COBOL 3.00 and 3.00a, see the file OS2API.DOC included with version 3.00a. For more information on DosQCurDir, see Page 547 of “Advanced OS/2 Programming” by Ray Duncan (published by Microsoft Press, 1989). The following sample program (GETDIR.CBL) prompts you for a drive and displays the current directory for that drive. To compile and link the program: pcobol getdir; link /nop getdir,,,pcobol doscalls;

Code Example
$SET ANS85 $SET NOOSVS DATA DIVISION. WORKING-STORAGE SECTION. *  DriveLetter receives the drive letter from the user. *  This needs to be converted to a drive number and placed in  *   DriveCOMP-5. But since PIC X data items cannot redefine *  COMP-5 data items, a COMP-X intermediate data item is used. 01 DriveCOMP-X  PIC 99    COMP-X. 01 DriveLetter  PIC X            REDEFINES DriveCOMP-X. 01 DriveCOMP-5  PIC 9(4)  COMP-5. *  Length of Pathname. 01 PathLength   PIC 9(4)  COMP-5 VALUE 256. *  Used to reference the different bytes of Pathname. 01 Byte         PIC 99    COMP-5. *  Receives the directory from API call, terminated by a null. 01 Pathname     PIC X(256). PROCEDURE DIVISION. DISPLAY &quot;Enter drive letter (a-z):&quot;. ACCEPT DriveLetter. *   &quot;a&quot; has ASCII 97, but drive A is number 1. COMPUTE DriveCOMP-5 = DriveCOMP-X - 96. CALL &quot;__DosQCurDir&quot; USING BY REFERENCE PathLength, BY REFERENCE Pathname, BY VALUE    DriveCOMP-5. *   Scan through each byte of Pathname until the null is found. PERFORM VARYING Byte FROM 1 BY 1 UNTIL Pathname(Byte:1) = X&quot;00&quot; END-PERFORM. *   Display bytes of Pathname up to the null, inclusively. DISPLAY &quot;Current path:&quot;. DISPLAY Pathname(1:Byte). STOP RUN. Copyright Microsoft Corporation 1990.