Microsoft KB Archive/65036

How to Get Available Disk Drive Space in OS/2 COBOL Program

PSS ID Number: Q65036 Article last modified on 11-21-1990

3.00 3.00a 4.00 OS/2

Summary: A protected mode COBOL program can get the amount of available space on a disk drive by calling the OS/2 API function DosQFSInfo. Below is a sample program illustrating how to do this. This information applies to Microsoft COBOL Compiler versions 3.00 and 3.00a for MS OS/2, and to Microsoft COBOL Professional Development System (PDS) version 4.00 for MS OS/2.

More Information: COBOL can call OS/2 API functions directly by prefixing their names with a double underscore (__) and linking with the DOSCALLS.LIB library. DosQFSInfo takes the following parameters:

The amount of space available on the drive is calculated with the following formula: Bytes = UnitsAvail * UnitSectors * SectorBytes Note that in COBOL a WORD (2 bytes) has a picture clause of 9(4) COMP-5. Furthermore, data items prefixed with PTR must be passed BY REFERENCE; otherwise, they are passed BY VALUE. Because COBOL’s calling convention is the reverse of OS/2’s, the parameters must be mentioned in the reverse order in the CALL statement. For more information on calling OS/2 API functions from COBOL 3.00 and 3.00a, see the file OS2API.DOC included with 3.00a. For more information on DosQFSInfo, see pages 549-550 of “Advanced OS/2 Programming” by Ray Duncan (Microsoft Press, 1989). The following sample program (GETSPACE.CBL) prompts you for a drive letter and displays the amount of space available (in bytes) on that drive. To compile and link the program in COBOL 3.00/3.00a, enter the following at the OS/2 command prompt: pcobol getspace; link /nop getspace,,,pcobol doscalls; The compiling and linking lines for COBOL PDS 4.00 are as follows: cobol getspace; link getspace,,,coblib os2;

GETSPACE.CBL
$SET ANS85 OSVS IDENTIFICATION DIVISION. PROGRAM-ID. GetSpace. DATA DIVISION. WORKING-STORAGE SECTION. *  Receives drive letter from user. 01 DriveLetter     PIC X.  *   Converts DriveLetter to be used by DriveCode. 01 DriveNumber     PIC 99   COMP-X REDEFINES DriveLetter. *  Code number of drive (0 = current, 1 = A, 2 = B, etc.) 01 DriveCode       PIC 9(4) COMP-5. *  Information desired (1 = get drive characteristics). 01 InfoDesired     PIC 9(4) COMP-5 VALUE 1. *  Size of the buffer to receive information. 01 BufferSize      PIC 9(4) COMP-5 VALUE 18. *  Receives number of bytes available on the drive. 01 BytesAvail      PIC 9(8)   COMP-5. *  Receives the characteristics of the drive. 01 Buffer. 05 FileSysID    PIC 9(8) COMP-5. 05 UnitSectors  PIC 9(8) COMP-5. 05 TotalUnits   PIC 9(8) COMP-5. 05 UnitsAvail   PIC 9(8) COMP-5. 05 SectorBytes  PIC 9(4) COMP-5. PROCEDURE DIVISION. *   Prompt the user for a drive letter. DISPLAY &quot;Enter drive letter:&quot;. ACCEPT DriveLetter. *   Check if DriveNumber is in legal range (A-Z or a-z). IF ((DriveNumber >= 97) AND (DriveNumber <= 122)) THEN COMPUTE DriveNumber = DriveNumber - 32 ELSE IF ((DriveNumber < 65) OR (DriveNumber > 90)) THEN DISPLAY &quot;ERROR - Must enter a letter.&quot; CALL X&quot;E5&quot; STOP RUN END-IF END-IF. *   When DriveNumber = 65, DriveCode = 1, and so on. COMPUTE DriveCode = DriveNumber - 64. *   Make the API call. CALL &quot;__DosQFSInfo&quot; USING BY VALUE    BufferSize, BY REFERENCE Buffer, BY VALUE    InfoDesired, BY VALUE    DriveCode. *   If no error occurred, compute and display bytes available. IF RETURN-CODE = 0 THEN COMPUTE BytesAvail = UnitsAvail * UnitSectors * SectorBytes DISPLAY &quot;The are &quot; BytesAvail &quot; bytes remaining on drive&quot; DriveLetter &quot;.&quot; ELSE DISPLAY &quot;ERROR - Invalid drive.&quot; CALL X&quot;E5&quot; END-IF. STOP RUN. Copyright Microsoft Corporation 1990.