setenv from c

rs at mirror.UUCP rs at mirror.UUCP
Thu Sep 26 00:34:00 AEST 1985



> Is it possible to set a csh environment variable within a C program?

Presumably you want to do something like the BSD "tset" program, which
SEEMS LIKE it adds environment variables to its parent's process.

The only way to do this is to have your program write out a command
line that can be "sourced" in.  For example, this program:

	main()
	{
	    int p;

	    if (p = fork())
		printf("setenv CLOCKPID %d ; echo clock...", p), exit(0);
	    ...print a clock on the status line every once in a while...
	}

You can now add the pid of the clock "daemon" by doing something
like this:
	CSH			SH			EITHER
	clock >/tmp/$$		clock >/tmp/$$		eval `clock`
	source /tmp/$$		. /tmp/$$
	rm /tmp/$$		rm /tmp/$$

Note that if your environment variables contain shell meta-characters,
you might have problems (only CSH, I think).

--
Rich $alz	{mit-eddie, ihnp4!inmet, wjh12, cca, datacube} !mirror!rs
Mirror Systems	2067 Massachusetts Ave.
617-661-0777	Cambridge, MA, 02140



More information about the Comp.lang.c mailing list