Creating & exporting environment variables in C

Mike Stefanik/78125 mike at bria.AIX
Fri Jan 4 14:04:08 AEST 1991


In article <1991Jan2.192939.25220 at cbnewsm.att.com> kalin at cbnewsm.att.com (andrew.j.kalinowitsch) writes:
>A question for all you UNIX gurus:
>
>I'm writing some C code to create and/or modify standard environment
>variables (SPOOLER, etc.) using putenv.  Is there a corresponding
>C command that exports these variables to all lower shells, or does
>putenv do this implicitly?
>
Putenv() will only modify the running process' environment; children do
not inherit this modified environment because putenv() avoids 'environ'.
I find this to be quite the pain, and therefore use a set of functions to
manipulate token lists (that just *happen* to look like an environment
list :-)  They are:

	inittok(tab,env)	initialize a token table
	puttok(tab,buf)		put a token in the table
	gettok(tab,buf)		get a token from the table
	rmtok(tab,buf)		remove a token from the table

Then, whenever you use an exec() use the execle() or execve() flavor, ie:
	
	execle("/bin/ls","ls",NULL,tab);

The added advantage is that you can create and manipulate more than one
token table, thereby allowing you to fudge with the environment in many
creative ways ...

If you want a copy of the functions, just send me mail at uunet!bria!mike
and I'll send 'em back shar'd.  Of course, this goes for anyone ...

-----------------------------------------------------------------------------
Michael Stefanik, Systems Engineer (JOAT), Briareus Corporation
UUCP: ...!uunet!bria!mike
"If it was hard to code, it should be harder to use!"



More information about the Comp.unix.questions mailing list