setenv from c

jpn at teddy.UUCP jpn at teddy.UUCP
Sat Sep 28 00:14:59 AEST 1985


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

In all the replies to this question, I have not yet seen my favorite techniqe.
This only works on BSD 4.X (at least as far as I know).  There is an
undocumented ioctl() which allows you to push data back onto your input queue
(i.e. simulate characters typed at the terminal).  Using this technique, one
can stuff strings like "setenv TERM xxx\n" into the parent shell's input.

A fragment of the code that does the work:

#include <sgtty.h>

eatthis(string)
register char *string;
    {
    int pendin = LPENDIN;

    noecho();				/* turn off echo mode */
    while (*string)
        {
        ioctl(0, TIOCSTI, string);	/* do the work */
        ++string;
        }
    echo();				/* turn echo back on */
    ioctl(0, TIOCLBIS, &pendin);	/* set the pending input flag */
    }



More information about the Comp.lang.c mailing list