[net.unix-wizards] Setting environment variables within a C program.

Bakin@HI-MULTICS.ARPA (Jerry Bakin) (04/24/85)

Hi,
  I understand that if I type

 freds_wife = Wilma
 export fred_wife

from the shell, I can access freds_wife within a C program by using
getenv.

How do I change the value of freds_wife from Wilma to Betty within the C
program, and get the environment to understand the change later?

Jerry <Bakin@HI-Multics>

jsdy@hadron.UUCP (Joseph S. D. Yao) (05/07/85)

>  freds_wife = Wilma
>  export fred_wife
> How do I change the value of freds_wife from Wilma to Betty within the C
> program, and get the environment to understand the change later?

You can't.  Environment variables are exported to children, not vice-
versa.  The C prog is child of the shell.  However, if the program
produces no other output, you can get the shell to change itself by
printing out the commands:
	printf("freds_wife=Betty; export freds_wife\n");
and then:
	$ eval `prog`
This can be used profitably with, e.g., 'tset' to set TERM and TERMCAP.

However, a second, more difficult obstacle is that Betty loves Barney
and Wilma won't divorce Fred.	;-)

	Joe Yao		hadron!jsdy@seismo.{ARPA,UUCP}

libes@nbs-amrf.UUCP (Don Libes) (05/08/85)

Since no one has posted a better reply than "no, you can't" let me
point out something.

I think what was wanted was a way to change environment variables in
the environment of the current process (and hence child processes).
The reason I think this, is because I have to do the same thing (see
last paragraph).  Obviously there is no way to change them in the
parent process.

I don't know the "right" way, but one way is to copy the environ array
pointers into a new array and make your change.  The reason you have to
use a new array is in case the change you are making adds a variable to
the environment.  If you are only resetting a variable, you just
reassign that one pointer to the variable value.  The reason this is
kludgey, of course, is that the space from the original environ becomes
unusable garbage.  That is, I haven't the faintest idea how to return
it (i.e.  free) to useful life.

I actually wrote a routine to do just this, so I know it works.  The
problem was on our Suns, I wanted to be able to create "tools" in
different fonts.  The only way you can do this is by setting the
environment variable DEFAULT_FONT.  Obviously, if you are controlling
the desktop with a C program (the window manager), then, you need to
set environment variables.

I'd be interested if someone could tell me the "right" way to do this.
I'd also like to know why fonts are selected via an environment
variable on the Sun.

Don Libes	{seismo,umcp-cs}!nbs-amrf!libes