Rational Developer for System z

Accessing environment variables in a COBOL sample program

Sometimes it is necessary to dynamically retrieve the value of an environment variable at run time. This topic provides a brief COBOL sample that shows one way to access an environment variable.

  1. Switch to the z/OS® Projects perspective.
  2. Create a workstation COBOL or PL/I project. In the new project wizard, select the option to Create a new property group and associate it with the project.
  3. Edit the property group, and choose the Local COBOL Settings, Local PL/I Settings, and Link Options check boxes on the Categories tab. Accept the default values on these pages and save the property group.
  4. Create a COBOL program called Cblenv.cbl in the project and paste the following sample code into the file:
           Identification Division.
           Program-ID.  Cblenv.
    
           Data Division.
           Working-Storage Section.
           01 P pointer.
           01 ENVVAR pic x(32) value Z"PATH".
           01 P2 POINTER.
           01 ENVARSTG PIC X(1000).
           01 var-len pic 9(4) binary.
           Procedure Division.
                 Set P to address of ENVVAR.
                 Set P2 to address of ENVARSTG.
           >>CALLINT OPTLINK
                      Call "XENVVAR"
                             using BY value P,
                             BY VALUE P2.
           >>CALLINT
                 Display "PATH = " ENVARSTG
                 Move 0 to var-len
                 Inspect ENVARSTG tallying var-len for characters
                  before initial x"00"
                  Display "TRIMMED PATH = " ENVARSTG (1:var-len)
               Goback.
  5. Create a PL/I program called XENVAR.pli in the project and paste the following sample code into the file:
    %process macro;
     %dcl OUTBUFSZ char;
     %OUTBUFSZ = '1000';
     XENVVAR: proc(xp,xp2) options (nodescriptor);
       dcl envar char(32) based (xp);
       dcl xp ptr byvalue;
       dcl xp2 ptr byvalue;
       dcl enval char(OUTBUFSZ);
       dcl venval char(OUTBUFSZ) varyingz;
       dcl envlen fixed bin(31);
     
       /****************************************************************/
       /* This on unit is necessary to trap any unhandled condition.   */
       /* It must also have the goto statement to get out of the unit. */
       /****************************************************************/
       on anycond
         begin;
          display('  XENVAR ====> error occurred '||oncode());
           goto goto_label;
         end;
       call plifill(xp2, '00'x, OUTBUFSZ);
       enval = getenv(xp->envar);
       venval = trim(enval);
       envlen = length(venval);
          /*enforce null terminated */
       IF envlen > (OUTBUFSZ - 1) THEN envlen = (OUTBUFSZ - 1);
       call pliover( xp2, addr(venval),envlen);
     goto_label:
      return ;
     end;
  6. Right click Cblenv.cbl and select Nominate as Entry Point. This action causes the icon to change on Cblenv.cbl, indicating that the COBOL program is the main program.
  7. To rebuild the project, right-click the project and select Rebuild Project. After the project is built a folder named BuildOutput is added to the project. It should contain Cblenv.exe. If this file is not generated, fix the compile errors that are reported in the Remote Error List, and rebuild the project. If there are any unresolved references reported at link time, link errors can be found in the Remote Console View.

Terms of use | Feedback

This information center is powered by Eclipse technology. (http://www.eclipse.org)