setenv from c

John Mashey mash at mips.UUCP
Mon Sep 23 14:25:23 AEST 1985


Rick Johnson (decvax!mcnc!ncsu!rlj) writes:
> Is it possible to set a csh environment variable within a C program? I know
> about getenv(), but have failed at attempts to set an environment variable
> from a C program. Basically, what I want to do is "source" a new environment
> variable for the current csh process. Any comments or suggestions would be
> appreciated. Thanks.

I assume that you're not interested in just modifying the current environment,
[which is fairly straightforward, and has been posted somewhere in last
6 months], but in getting changes back to the parent shell [csh or sh].

You can't modify the parent shell's environment directly, but only by
returning a character string to be "eval"ed.  Suppose C pgm "glurp" ends with:
	printf("setenv a b\n"); 
Invoke it with: 	eval `glurp`
to set a = b.  More complex effects can be gotten by changing the printf string.

A process cannot modify its parent's environment, on purpose.  At one point,
such a feature was considered, and rejected, because it had no simple,
understandable semantics.  For example, many otherwise deterministic
computations turn nondeterministic, depending on process scheduling.
Consider a pipeline like a | b | c, where all of a, b, c could modify
their parent's environment, or worse, nohup a& (and logoff).  [Exactly
what is a modifying?]
-- 
-john mashey
UUCP: 	{decvax,ucbvax,ihnp4}!decwrl!mips!mash
DDD:  	415-960-1200
USPS: 	MIPS Computer Systems, 1330 Charleston Rd, Mtn View, CA 94043



More information about the Comp.lang.c mailing list