Rational Developer for System z
COBOL for Windows, Version 7.5, Programming Guide


Example: preinitializing the COBOL environment

The following figure illustrates how the preinitialized COBOL environment works. The example shows a C program initializing the COBOL environment, calling COBOL programs, then terminating the COBOL environment.

This image shows control flow from a C program to _iwzCOBOLInit to initialize the COBOL environment, as described in preceding sections.

The following example shows the use of COBOL preinitialization. A C main program calls the COBOL program XIO several times. The first call to XIO opens the file, the second call writes one record, and so on. The final call closes the file. The C program then uses C-stream I/O to open and read the file.

To test and run the program, enter the following commands from a command window:

cob2 -c xio.cbl
cl testinit.c xio.obj
testinit

The result is:

_iwzCOBOLinit got 0
xio entered with x=0000000000
xio entered with x=0000000001
xio entered with x=0000000002
xio entered with x=0000000003
xio entered with x=0000000004
xio entered with x=0000000099
StopArg=0
_iwzCOBOLTerm expects rc=0 and got rc=0
FILE1 contains ----
11111
22222
33333
---- end of FILE1

Note that in this example, the run unit was not terminated by a COBOL STOP RUN; it was terminated when the main program called _iwzCOBOLTerm.

The following C program is in the file testinit.c:

#ifdef _AIX
typedef int (*PFN)();
#define LINKAGE
#else
#include <windows.h>
#define LINKAGE _System
#endif

#include   <stdio.h>
#include   <setjmp.h>

extern void _iwzCOBOLInit(int fcode, PFN StopFun, int *err_code, void *StopArg);
extern void _iwzCOBOLTerm(int fcode, int *err_code);
extern void LINKAGE XIO(long *k);

jmp_buf Jmpbuf;
long StopArg = 0;

int LINKAGE
StopFun(long *stoparg)
{
        printf(“inside StopFun\n”);
        *stoparg = 123;
        longjmp(Jmpbuf,1);
}

main()
{
        int rc;
        long k;
        FILE *s;
        int c;

        if (setjmp(Jmpbuf) ==0) {
                _iwzCOBOLInit(1, StopFun, &rc, &StopArg);
                printf( “_iwzCOBOLinit got %d\n”,rc);
                for (k=0; k <= 4; k++) XIO(&k);
                k = 99; XIO(&k);
        }
        else printf(“return after STOP RUN\n”);
        printf(“StopArg=%d\n”, StopArg);
        _iwzCOBOLTerm(1, &rc);
        printf(“_iwzCOBOLTerm expects rc=0 and got rc=%d\n”,rc);
        printf(“FILE1 contains ---- \n”);
        s = fopen(“FILE1”, “r”);
        if (s) {
                while (  (c = fgetc(s) ) != EOF ) putchar(c);
        }
        printf(“---- end of FILE1\n”);
}

The following COBOL program is in the file xio.cbl:

 IDENTIFICATION DIVISION.
 PROGRAM-ID.     xio.
******************************************************************
 ENVIRONMENT    DIVISION.
 CONFIGURATION   SECTION.
 INPUT-OUTPUT    SECTION.
 FILE-CONTROL.
     SELECT file1 ASSIGN TO FILE1
       ORGANIZATION IS LINE SEQUENTIAL
       FILE STATUS IS file1-status.
 . . .
 DATA           DIVISION.
 FILE SECTION.
 FD FILE1.
 01 file1-id pic x(5).
 . . .
 WORKING-STORAGE SECTION.
 01 file1-status  pic xx    value is zero.
 . . .
 LINKAGE SECTION.
*
 01 x               PIC S9(8) COMP-5.
 . . .
 PROCEDURE DIVISION using x.
 . . .
     display “xio entered with x=” x
     if x = 0 then
       OPEN output FILE1
     end-if
     if x = 1  then
        MOVE ALL “1” to file1-id
        WRITE file1-id
     end-if
     if x = 2 then
        MOVE ALL “2” to file1-id
        WRITE file1-id
     end-if
     if x = 3 then
        MOVE ALL “3” to file1-id
        WRITE file1-id
     end-if
     if x = 99 then
       CLOSE file1
     end-if
     GOBACK.

Terms of use | Feedback

Copyright IBM Corporation 1996, 2008.
This information center is powered by Eclipse technology. (http://www.eclipse.org)