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.
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.
%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;
Rational® Developer
for System z® does not support
local COBOL or PL/I builds on the Linux platform.