Microsoft KB Archive/63838

How to Create a Directory in a Protected Mode COBOL Program

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

3.00 3.00a OS/2

Summary: A Microsoft COBOL version 3.00 or 3.00a protected mode program can create a directory in any drive by calling the OS/2 API function DosMkDir. 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 DosMkDir requires the following parameters:

The first parameter is a PIC X field of variable length that must contain a null (X“00”) byte. The function will include all the characters up to the null byte as the pathname of the directory. Since a pointer (PTR) to this 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. 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 DosMkDir, see Page 534 of “Advanced OS/2 Programming” by Ray Duncan (Microsoft Press, 1989). The sample program below (MAKEDIR.CBL) prompts you for a directory name to be created in the current directory of the current drive (8 character limit). It creates the directory and displays a listing of all the directories by calling the special subprogram X“91”, function 35 (a DOS EXEC call). The newly created directory will be in the list. To compile and link the program, enter the following lines at the OS/2 command prompt: pcobol makedir; link /nop makedir adis adisinit adiskey,,,pcobol doscalls;

Code Example
$SET ANS85 $SET NOOSVS DATA DIVISION. WORKING-STORAGE SECTION. *  Reserved parameter for API call. 01 Reserved       PIC 9(8)   COMP-5 VALUE 0. *  Contains null-terminated directory pathname. 01 Directory. 05 Pathname    PIC X(8). 05 ASCII0      PIC X             VALUE X&quot;00&quot;. *  Used to reference bytes of Directory. 01 Byte           PIC 9(4)   COMP-5. *  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. *  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 *.&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 even if  *    it is less than 8 characters. PERFORM VARYING Byte FROM 1 BY 1 UNTIL Byte > 8 MOVE X&quot;00&quot; TO Pathname(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 Pathname AT 0130 WITH LEFT-JUSTIFY. *   Make the directory and display a listing of it of all the *   subdirectories in the current directory. CALL &quot;__DosMkDir&quot; USING BY VALUE    Reserved, BY REFERENCE Directory. CALL X&quot;91&quot; USING Result, Function, Parameter91. STOP RUN. Copyright Microsoft Corporation 1990.