Microsoft KB Archive/63720

How to Change Working Directory in an OS/2 COBOL Program

PSS ID Number: Q63720 Article last modified on 07-11-1990

3.00 3.00a OS/2

Summary: A Microsoft COBOL version 3.00 or 3.00a program running in MS OS/2 (in protected mode) can change the directory of the current drive by calling the OS/2 API function DosChDir. This information applies to Microsoft COBOL Compiler versions 3.00 and 3.00a for MS OS/2.

More Information: COBOL versions 3.00 and 3.00a can directly call OS/2 API functions by prefixing the name of the function with a double underscore (__) and linking with the PCOBOL.LIB and DOSCALLS.LIB libraries. The API function DosChDir requires the following parameters:

The first parameter is a PIC X field of variable length that must contain a null (X“00”). The function includes all the characters up to the null as the pathname of the directory. If the pathname doesn’t exist on the current drive, the current directory remains unchanged and the function returns an error in the special COBOL variable RETURN-CODE. Since a pointer (PTR) to the first parameter is required, it must be passed using the BY REFERENCE clause. The second parameter is a PIC 9(8) COMP-5 field (double word) and must be set to 0. Since a pointer to the parameter is not required, it must be passed using the BY VALUE clause. Note also that the parameters must be passed in the reverse order because OS/2 API functions use a calling convention that is the reverse of COBOL’s. IMPORTANT NOTE: The current directory for each drive is maintained on a per-process basis. This means that even though you call DosChDir to change the current directory, when your COBOL program terminates, the current directory for the OS/2 command interpreter (a different process) will be the same as before you ran your program. For more information on calling OS/2 API functions from COBOL, see the file OS2API.DOC included with version 3.00a of the compiler. For more information on the API function DosChDir, see Page 505 of “Advanced OS/2 Programming” by Ray Duncan (Microsoft Press, 1989). The sample program below (CHDIR.CBL) prompts you for a directory pathname on the current drive. It makes that directory current and displays a listing of it by calling the special subprogram X“91”, function 35 (a DOS EXEC call). To compile and link the program, enter the following lines at the OS/2 command prompt: pcobol chdir; link /nop chdir adis adisinit adiskey,,,pcobol doscalls;

Code Example
$SET ANS85 DATA DIVISION. WORKING-STORAGE SECTION. *  Receives result of X&quot;91&quot; call. 01 Result         PIC 99     COMP-X. *  Function 35 is EXEC call. 01 Function       PIC 99     COMP-X VALUE 35. *  Reserved parameter for API call. 01 Reserved       PIC 9(8)   COMP-5 VALUE 0. *  Contains null-terminated directory pathname. 01 Directory      PIC X(257). *  Used to reference bytes of Directory. 01 Byte           PIC 9(4)   COMP-5. *  Parameter for function 35 of X&quot;91&quot; call. 01 Parameter91. *  Length of name of file to execute. 05 NameLength  PIC 99     COMP-X VALUE 6. *  Name of file to execute. 05 FileName    PIC X(6)          VALUE &quot;dir /w&quot;. PROCEDURE DIVISION. *   Insert X&quot;00&quot; (null) in all bytes of Directory so the name *   passed to the API function will be null-terminated. PERFORM VARYING Byte FROM 1 BY 1 UNTIL Byte > 257 MOVE X&quot;00&quot; TO Directory(Byte:1) END-PERFORM. *   Clear screen and prompt for directory pathname. CALL X&quot;E4&quot;. DISPLAY &quot;Enter pathname of directory:&quot; AT 0101. ACCEPT Directory AT 0130 WITH LEFT-JUSTIFY. *   Change the directory and display a listing of it. CALL &quot;__DosChDir&quot; USING BY VALUE    Reserved, BY REFERENCE Directory. CALL X&quot;91&quot; USING Result, Function, Parameter91. STOP RUN. Copyright Microsoft Corporation 1990.