Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Setting Env. variable from C

0 views
Skip to first unread message

Igor

unread,
May 24, 2000, 3:00:00 AM5/24/00
to
Hi All

How can I set Env. variable from C.
I tryed to use system (str) but it did not change any of
my Env. variable.

Thanks.

Igor


Kurt J. Lanza

unread,
May 24, 2000, 3:00:00 AM5/24/00
to
Igor wrote:
>
> Hi All
>
> How can I set Env. variable from C.
> I tryed to use system (str) but it did not change any of
> my Env. variable.
>
This must be in a FAQ somewhere (it's certainly asked
often enough), but here goes anyway: No process can
change its parent's environment. It can only change its
childrens' environment. Otherwise security in UNIX would
be as much of a joke as in Windoze. Not to mention the
CPU cost if it could be done at all. Don't even try to
change your parent's environment. Find another solution
to what you see as a problem.

Barry Margolin

unread,
May 24, 2000, 3:00:00 AM5/24/00
to
In article <392C0D38...@inforonics.com>,

Kurt J. Lanza <k...@inforonics.com> wrote:
>Igor wrote:
>>
>> Hi All
>>
>> How can I set Env. variable from C.
>> I tryed to use system (str) but it did not change any of
>> my Env. variable.
>>
>This must be in a FAQ somewhere (it's certainly asked
>often enough)

It's definitely in the comp.unix.questions FAQ, which is on www.faqs.org.

--
Barry Margolin, bar...@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

Michael J. Fromberger

unread,
May 24, 2000, 3:00:00 AM5/24/00
to

>Hi All

>How can I set Env. variable from C. I tryed to use system (str) but
>it did not change any of my Env. variable.


Hi there,

Most Unix systems implement either setenv() or putenv() as library
functions. Try doing 'man putenv' or 'man setenv', and see what turns
up. That's the usual mechanism for setting things in the current
process's environment (and, of course, thereby in the environment of
all your child processes).

Be aware, of course, that 'setenv' is usually also the name of a shell
command, so make sure you're looking at the appropriate manual page.

Cheers,
-M

--
Michael J. Fromberger Software Engineer, Thayer School of Engineering
sting <at> linguist.dartmouth.edu http://www.dartmouth.edu/~sting/
OfUb/Q/gvcIAtVDQ7kbZelrae4GIXJT8mmn38wXEpdUjthIOaDBPen8LcEij9zNFJzRNoe0W

The church is near, but the way is icy;
The tavern is far, but I will walk carefully. -- Ukrainian proverb


herzel Elmalme

unread,
May 25, 2000, 3:00:00 AM5/25/00
to
The Simple Way :

system("vriablename=value");
"Igor" <i.sok...@geac.com> wrote in message
news:392abf4f...@news.ca.geac.com...


> Hi All
>
> How can I set Env. variable from C.
> I tryed to use system (str) but it did not change any of
> my Env. variable.
>

> Thanks.
>
> Igor
>

Michael Sternberg

unread,
May 25, 2000, 3:00:00 AM5/25/00
to
herzel Elmalme wrote:
> system("vriablename=value");

This spawns a sub-shell which goes to great lengths for setting up PATHs
etc,
then does the command above, then quietly dies, whithout much of a trace in
the parent.


The question to ask is "What is it needed for?"

As has been pointed out before, there is no way a process can set an
environment variable of the parent.

However, it may be necessary for a process to set a variable for itself
(although there are more efficient ways of passing data around). Most
importantly, though, is to pass settings on to children (as e.g. login(1)
does with LOGNAME, HOME etc. before spawning a shell). For this purpose
exists putenv(3).

#include <stdlib.h>
int putenv(const char *string);


In any case, this belongs to comp.lang.c (F'up).


Regards,
--
Michael Sternberg, Dipl. Phys. | Uni-GH Paderborn
http://www.phys.uni-paderborn.de/~stern/ | FB6 Theoretische Physik
phone: +49-(0)5251-60-2329 fax: -3435 | 33098 Paderborn, Germany
"Who disturrrbs me at this time?" << Zaphod Beeblebrox IV >> <*>

Kurt J. Lanza

unread,
May 25, 2000, 3:00:00 AM5/25/00
to

The simple way, of course, does you absolutely no good.
System() runs the shell as a child. That shell sets its
environment variable, then exits. Variable gone. Oops.
So sorry. You can't set a variable in your parent, no
matter how hard you try.

0 new messages