[comp.lang.c] Setting environment from inside a pg.

gangwani@andromeda.rutgers.edu.rutgers.edu (Sunil Gangwani) (11/25/90)

Hi,

With the system() function call a new shell is executed in
DOS or UNIX.  I would like the set an environment variable from
inside a program so that the variable exists when I exit the
program??

Ex. in DOS

system("set var=Hello");

The above will not work because a new command.com is executed
and exited immediately.  How can I overcome this simple problem??

Please respond directly to gangwani@andromeda.rutgers.edu because
due to heavy traffic our facility removes USENET stuff every two
days...

thanks in advance


Sunil Gangwani

herrj@valnet.UUCP (Jonathan R. Herr) (11/27/90)

gangwani@andromeda.rutgers.edu.rutgers.edu (Sunil Gangwani) writes:

> 
> Hi,
> 
> With the system() function call a new shell is executed in
> DOS or UNIX.  I would like the set an environment variable from
> inside a program so that the variable exists when I exit the
> program??
> 
> Ex. in DOS
> 
> system("set var=Hello");
>
> The above will not work because a new command.com is executed
> and exited immediately.  How can I overcome this simple problem??

Well, you don't say what C you're using.  But, there is a way to set
environment variables in Turbo C 2.0 by use of the putenv(const char *name)
command.  It is also availble on Unix systems according to the
reference guide.  Something like this might work:

/* ========================================== */

#include <stdlib.h>

putenv("set var=Hello");

/* ========================================== */

My syntax may be off base.  I'm not super-experienced in C like some
of these folk.  I only know this much as I've been using getenv() to access
some environment variables for a program I've been writing for Waffle
BBS in which I have to locate a definition file that is defined as an
environment variable and I saw the reference to putenv().

> Please respond directly to gangwani@andromeda.rutgers.edu because
> due to heavy traffic our facility removes USENET stuff every two
> days...

I'll forward it to you.

> Sunil Gangwani

Jon Herr



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|Jonathan R. Herr (aka Jon)	|  herrj@silver.ucs.indiana.edu |
|                               |  herrj@valnet.UUCP            |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

jwindley@matt.ksu.ksu.edu (Jay Windley) (11/28/90)

Assuming pg means "program" here's an answer:

In UNIX the environment is changed with the putenv(3) function.  You pass
it a pointer to a string of the format <var>=<value> (e.g., "HOME=/usrc/foo").
The caveat here is that the string must be stored in static memory; either
a global or a static character array.

Note that this only changes the environment of the current process, not
its parent.  There is no way under UNIX for a process to change the
environment of its parent, since the environment it receives upon
invocation is merely a copy of its parent's.  Children of the process
will receive the altered evironment if they are spawned after the change.
--
Jay Windley - CIS Dept. - Kansas State University
NET: jwindley@matt.ksu.ksu.edu  VOICE: (913) 532-5968  FAX: (913) 532-6722
USnail: 323 Seaton Hall, Kansas State Univ., Manhattan, KS 66506
Obligatory quote:  ""  -- /dev/null

carroll@cs.uiuc.edu (Alan M. Carroll) (11/28/90)

Here is how you set environment variables in a parent shell in UNIX:

Let us say you have a program named "bob" that wants to set environment
variables. Under sh you would write a function like

dave() { eval `bob` ; }

Under ksh or csh, you would use an alias. The bob program then generates
output that looks like

VAR=VALUE
VAR2=VALUE2
...

for sh or ksh, and some bizarro-nonsense format that may or may not have an
"=" sign in it for csh.

You then use "dave" instead of "bob" to set the variables. "bob" in effect
generates commands for the shell, and "dave" causes the shell to execute them.
-- 
Alan M. Carroll                "It's psychosomatic. You need a lobotomy,
Epoch Development Team          I'll get a saw."
CS Grad / U of Ill @ Urbana    ...{ucbvax,pur-ee,convex}!cs.uiuc.edu!carroll

turbo@evax.arl.utexas.edu (Chris Turbeville) (11/29/90)

Functions like putenv (getenv, etc...) are available for most all
compilers but they do not set the parents environment just the programs.
I could be off base for some compilers but I know MSC sets only its COPY
of the environment not its parents.
Chris