Hello,
Thanks Scott, I would be interested in seeing how to do this. I am on
V5R4 and using ILE RPG . . .
It's really pretty simple if you're familiar with pointers. The system
stores environment variables as null-terminated strings (that is, C
style strings). There's a pointer that's exported from one of the C
runtime service programs called 'environ'. This pointer points to an
array of pointers. The array is variable-length. It consists of
pointers that point to environment variables, and the array is
terminated when an address is *NULL.
So if your job has 3 environment variables set, you'd have this:
ptr(1) = first env var.
ptr(2) = second env var.
ptr(3) = third env var
ptr(4) = *NULL.
Basically, you use *NULL to detect when you're at the end of the array.
Each environment variable is (as I mentioned) a null-terminated (i.e.
x'00' terminated) C-style string. It consists of the variable name, an
equal sign, and the variable's value.
So if you have a variable named 'LANG' that is set to
/QSYS.LIB/EN_US.LOCALE then in memory it looks like this:
LANG=/QSYS.LIB/EN_US.LOCALE<00>
Where <00> signifies the x'00' (null) terminator.
To deal with this in ILE RPG you have to do the following:
a) Include BNDDIR('QC2LE') to allow access to the ILE C runtime.
b) Use import('environ') to get the pointer to the array.
c) Base an array of pointers on the 'environ' pointer.
d) Loop through the array of pointers, looking for *NULL.
e) For each one that's not null, use the %STR() BIF to convert the
C-style string to an RPG varying string.
f) If need be, scan for = to separate the variable from the value.
Here's an RPG proof-of-concept program that just reads the list of
variables and prints them to a program-described printer file. This
might be a bad example since it chops off the variable name at 30
characters and the value at 100 characters (they can potentially be
longer than that) but I had to shorten them to fit in a typical printer
record. If you weren't printing them, you'd want to make the 'VAR' and
'VALUE' variables larger, and you'd probably want them to be VARYING as
well...
Anyway, enough babble... here's the code:
H DFTACTGRP(*NO) BNDDIR('QC2LE')
FQSYSPRT O F 132 PRINTER
D environ s * import('environ')
D env s * dim(32767)
D based(environ)
D x s 10i 0
D pos s 10i 0
D whole s 65535a varying
D var s 30a
D value s 100a
/free
for x = 1 to %elem(env);
if (env(x) = *null);
leave;
endif;
whole = %str(env(x));
var = '';
value = '';
pos = %scan('=': %str(env(x)) );
if (pos>1);
var = %subst(whole:1:pos-1);
endif;
if (pos < %len(whole));
value = %subst(whole:pos+1);
endif;
except;
endfor;
*inlr = *on;
/end-free
OQSYSPRT E
O Var
O Value +2
As an Amazon Associate we earn from qualifying purchases.