[net.micro.pc] set environment

marc@ur-univax.UUCP (03/11/85)

The DOS manual talks about adding things to the environment with the SET
ENVIRONMENT command and goes on to say that things in the environment can be
accessed from within an application.  Can anyone tell me how to do this or
where it is documented ?

Mark J. Dumic
...!{allegra|decvax|seismo}rochester!ur-univax!marc

jchapman@watcgl.UUCP (john chapman) (03/13/85)

> The DOS manual talks about adding things to the environment with the SET
> ENVIRONMENT command and goes on to say that things in the environment can be
> accessed from within an application.  Can anyone tell me how to do this or
> where it is documented ?
> 
> Mark J. Dumic
> ...!{allegra|decvax|seismo}rochester!ur-univax!marc
  
 This works the same way is in unix, e.g.
 set <name>=<string>
 (I can't recall off hand if the = is necessary or just a blank
  but you get the idea.  Accessing the environment is a different
  matter: when a program is executed part of the run time header
  (in first 100h bytes of programs code segment) is set to the
  address of the environment. The environment is stored as a 
  series of strings x=y.  So you would need some sort of routine
  to pick up the pointer and scn through the environment looking
  for the string of interest (I think they are all null terminated).
 
  Hope this is what you were looking for,
 
  John Chapman
  ...!watmath!watcgl!jchapman

shor@sphinx.UChicago.UUCP (Melinda Shore) (03/14/85)

[]
>From: marc@ur-univax.UUCP
>The DOS manual talks about adding things to the environment with the SET
>ENVIRONMENT command and goes on to say that things in the environment can be
>accessed from within an application.  Can anyone tell me how to do this or
>where it is documented ?

Check out the section of the manual on the DOS program segment.
Basically, the "environment variables" are strings (both identifiers
and values) in memory, the beginning address of which is located at
offset 2C of the program segment prefix of the program starting up.
When a program starts up, DS and ES point to the program segment
prefix.  As I recall, DS tells us which segment, ES the address
within the segment.

-- 
Melinda Shore 
University of Chicago Computation Center
uucp:     ...!ihnp4!gargoyle!sphinx!shor
Mailnet:  staff.melinda@uchicago.mailnet
Bitnet:	  shor%sphinx@uchicago.bitnet
ARPA:	  staff.melinda%uchicago.mailnet@mit-multics.arpa

gbs@voder.UUCP (George Smith) (03/15/85)

> The DOS manual talks about adding things to the environment with the SET
> ENVIRONMENT command and goes on to say that things in the environment can be
> accessed from within an application.  Can anyone tell me how to do this or
> where it is documented ?
> 
> Mark J. Dumic
> ...!{allegra|decvax|seismo}rochester!ur-univax!marc

The following procedure for Turbo Pascal illustrates how to use
this very powerful feature in MS-DOS.  Look at the description
of the exec function and the description of the PSP (Program Segment
Prefix) in the DOS Technical Ref manual and the SET command description
in the DOS manual for more info.
Hope this helps.

(*
** The following function will look for an environment variable
** and return it's definition if found.  If the variable is not
** found, then the returned definition variable will be empty.
** Compiled and run with Turbo Pascal v2.0 on an IBM PC/XT running
** PC-DOS v2.1.
**
** This code is entered into the Public Domain for free use by all.
** George B. Smith, March 15, 1985.
*)

{ type mstring = string[255]; }

procedure GetEnvParam(name: mstring;      { environment variable to search for }
                      var param: mstring  { returned definition }
                     );

var
    envseg: integer;     { segment address of environment from PSP }
    ei: integer;         { index into environment }
    envname: mstring;    { current environment string name }
    ch: char;            { current character from environment }
    found: boolean;      { if true then environment name was found }

begin
    envseg := MemW[CSeg:$2C];        { get address of environment from PSP }
    found := FALSE;
    ei := 0;
    ch := chr(Mem[envseg:ei]);       { get first char from environment }
    while (ch <> chr(0) and (not found) do begin
        envname := '';
        while ch <> '=' do begin     { get environment string name }
            envname := envname + ch;
            ei := ei + 1;
            ch := chr(Mem[envseg:ei])
        end;
        ei := ei + 1;                { skip over the EQUALS }
        param := '';
        ch := chr(Mem[envseg:ei]);
        while ch <> chr(0) do begin  { get environment string parameter }
            param := param + ch;
            ei := ei + 1;
            ch := chr(Mem[envseg:ei])
        end;
        if name = envname then       { check for a match }
            found := TRUE;
        ei := ei + 1;
        ch := chr(Mem[envseg:ei])
    end;
    if not found then
        param := ''
end;

            
-- 
George B. Smith
National Semiconductor
...!{ihnp4!nsc | decvax!decwrl!nsc | ucbvax}!voder!gbs

bill@absolut.UUCP (03/16/85)

>The DOS manual talks about adding things to the environment with the SET
>ENVIRONMENT command and goes on to say that things in the environment can be
>accessed from within an application.  Can anyone tell me how to do this or
>where it is documented ?

>Mark J. Dumic
>...!{allegra|decvax|seismo}rochester!ur-univax!marc
>----------

I don't know about DOS 2.xx but in DOS 3.0 you can use the ENVIRON and
ENVIRON$ commands in basic to access environment variables.

Sorry, that I can't be of more help.

William Gibbs               {ucbvax!cbosgd!ima!cfib, decvax!cca}!absolut!bill
Absolut Software            617-232-8377
2001 Beacon Street
Boston, MA  02146-4227

manis@ubc-cs.UUCP (Vince Manis) (03/18/85)

A pointer to the current environment variables is stored in the
segment prefix. I think that the offset is $5c, but I don't have
my DOS technical reference in front of me. The environment consists
of a sequence of strings, each terminated by a null byte ($00). The
last string is followed by a second null. 

Each string consists of a variable name, an equal sign, and a string.
There's no limitation other than a total environment limitation of 
32K. 

Thus to find the value of a given variable, simply search the environment
for a string beginning with the variable name followed by an equal sign.

A recent issue of PC Tech Journal had a package of Turbo Pascal routines
for doing DOS things. I didn't read the article carefully, but I believe
there was an example routine there. 

lauren@vortex.UUCP (Lauren Weinstein) (03/20/85)

While the theoretical limit on environment strings may be 32K, it
turns out that in 2.X (and I believe in 3.X as well) you can't
pass more than 128 bytes of environment down from autoexec to the
rest of the system.  Unfortunately, this tends to be exactly
the use you most often want to make.

--Lauren--