Redirection question

Rahul Dhesi dhesi%cirrusl at oliveb.ATC.olivetti.com
Fri Nov 16 06:13:45 AEST 1990


>>Is there a _portable_ way to recognize whether input comes from
>>stdin or from another stream ?

Absolutely none!

But isatty(fd) works reasonably well on many systems.  Beware of just
doing a single test, however (which you may have been thinking of doing
after seeing some other postings).  You might want to do it like this:

    if (isatty(fileno(stdin)) && isatty(fileno(stdout))) {
       ... interactive code ...
    } else {
       ... noninteractive code ...
    }

This way you don't print a prompt to the output stream unless it's
connected to a tty, and you don't print a prompt unless the input is
coming from a tty.  You can also explicitly open /dev/tty to print the
prompt, but that's much less portable than isatty() alone.
--
Rahul Dhesi <dhesi%cirrusl at oliveb.ATC.olivetti.com>
UUCP:  oliveb!cirrusl!dhesi



More information about the Comp.lang.c mailing list