[comp.unix.questions] consequences of stderr redirection

rns@se-sd.SanDiego.NCR.COM (Rick Schubert) (10/20/90)

In <4066@se-sd.SanDiego.NCR.COM> bhoule@se-sd.SanDiego.NCR.COM (Bill Houle) writes:

>Just curious: why does redirection of stderr give 'vi' and 'less'
>the fits?  What is happening internal to those programs when stderr 
>is not /dev/tty?  Also, 'pg' seems to work fine but more displays 
>the first page and quits....

It appears that these programs use stderr (file descriptor 2) for their
`ioctl' calls to put the terminal into whatever mode it needs to be in
to do single character input (probably either "raw" or "cbreak" mode).
`ioctl' needs a file descriptor to act on, even though it is a global and
process independent change to the terminal.  If stderr is redirected, the
`ioctl' is done (or fails) on whatever file stderr is redirected to; however,
`vi' and `less' assume that stdin (file descriptor 0) was affected.
There might be some reason for doing the `ioctl' on fd 2 rather than fd 0,
but I don't know what it is (maybe because stdin is redirected more often
than stderr).

-- Rick Schubert (rns@se-sd.sandiego.NCR.COM)

P.S. Normally I would have waited to respond to this article since 
there are usually many responses to questions like this.  I'm only
answering now since Bill is on the same machine I am, so my answer
will get to him quicker and may get to you at the same time that
his question did.  If I gave a good answer, no need for anyone else
to post an answer.  If I was wrong, I'm sure I'll hear about it.