From: dspascha@eos.ncsu.edu (DAVID SCOTT PASCHAL)
Subject: Re: How can you set an environment variable in a program?
Originator: dspascha@c00089-100lez.eos.ncsu.edu
Reply-To: dspascha@eos.ncsu.edu (DAVID SCOTT PASCHAL)
Organization: North Carolina State University, Project Eos
Date: Tue, 1 Sep 1992 21:36:05 GMT


In article <1992Aug31.144636.2186@brandonu.ca>, dueck@brandonu.ca writes:
|> Path: taco!gatech!swrinde!sdd.hp.com!decwrl!access.usask.ca!ccu.umanitoba.ca!ciit85.ciit.nrc.ca!brandonu.ca!dueck
|> From: dueck@brandonu.ca
|> Newsgroups: comp.os.msdos.programmer
|> Subject: Re: How can you set an environment variable in a program?
|> Message-ID: <1992Aug31.144636.2186@brandonu.ca>
|> Date: Mon, 31 Aug 92 16:46:35 GMT+5:00
|> References: <1345@rand.mel.cocam.oz.au> <690@aardvark.Rain.COM> <1992Aug24.143641.1@camins.camosun.bc.ca>
|> Organization: Brandon University, Brandon, Manitoba, Canada
|> Lines: 49
|> 
|> In article <1992Aug24.143641.1@camins.camosun.bc.ca>, morley@camins.camosun.bc.ca writes:
|> > In article <690@aardvark.Rain.COM>, tyrant@Aardvark.Rain.COM (Rex Goode) writes:
|> >> dmh@superb (Daniel Hermans) writes:
|> >> 
|> >>>hi,
|> >>>i am using turbo c v2.0 to generate a temporary file name,
|> >>>place some data in the temporary file and then close the file. the temp
|> >>>file is then to be used on the command line of another program. both
|> >>>programs are called within a batch file something like:
|> >> 
|> >>>@echo off
|> >> 
|> >>>program1
|> >>>program2 < %tempfile%
|> >> 
|> >>>   system("set tempfile=tmpx.$$$");
|> >> 
|> >>>i tried using system(command) as above but the variable is not set when the
|> >>>program exits probably because system starts a new command.com shell each
|> >>>time
|> >> 
|> >> Absolutely right about the new command.com, with its own environment. That
|> >> is what happens when you use system(). Try setenv(),
|> >> 
|> >> setenv("TEMPFILE=TMPX.$$$);
|> >> 
|> > 
|> > Sorry, but setenv() also only modifies the "local" environment.  It will not
|> > change it "permanently".
|> > 
|> > MARK
|> > morley@camins.camosun.bc.ca
|> > 
|> 
|> The best way I have found to set a global environment variable is to use 
|> interrupt 2e, the back door to the command interpreter. Construct a string 
|> with a command like "set x=y\r" and pass it to INT 2E via DS:DX. 
|> 
|> Since 2E destroys all regs except CS:IP, you must restore the SS:SP from
|> a place addressable from CS, and restore DS from the stack. 
|> 
|> If you have set a global environment variable and wish to access it
|> subsequently using getenv(), free the program's local environment and
|> point the environment segment in the PSP at its parent's environment.
|> If you do this, set the environment segment in the PSP to zero before
|> exiting, or program cleanup will attempt to delete the master environment.
|> 
|> Gery Dueck
|> dueck@brandonu.ca
|> 

Hi.  First of all, don't you pass the string in DS:SI?  And you also have to
pass the length of the string (not counting the return) in the first byte.

Also, I have read (and
have found from experience) that INT 2E is extremely unpredictable, unstable,
and unportable.  You <might> be OK just using it to modify the master
environment, but even so, not all DOS shells support it.  Besides, if 'program1'
weren't run from the primary shell (i.e. if it were run in a secondary copy of
COMMAND.COM), then %tempfile% in the line "program2 <%tempfile%" would not be
defined.

Tschuess,
David Paschal