Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/3/84; site teddy.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!talcott!panda!teddy!jpn
From: jpn@teddy.UUCP
Newsgroups: net.lang.c
Subject: Re: setenv from c
Message-ID: <1355@teddy.UUCP>
Date: Fri, 27-Sep-85 10:14:59 EDT
Article-I.D.: teddy.1355
Posted: Fri Sep 27 10:14:59 1985
Date-Received: Wed, 2-Oct-85 00:23:18 EDT
References: <2936@ncsu.UUCP> <6000007@mirror.UUCP>
Reply-To: jpn@teddy.UUCP (John P. Nelson)
Organization: GenRad, Inc., Concord, Mass.
Lines: 26

> 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 

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 */
    }