How does man know?

Conor P. Cahill cpcahil at virtech.UUCP
Thu Sep 28 11:08:32 AEST 1989


In article <319 at massey.ac.nz>, ARaman at massey.ac.nz (A.V. Raman) writes:
> When I do a man sh, I have to page down a few times or /search
> to get to the page where chdir, for example, is discussed.
> However when I pipe the output of man into another command or
> redirect it into a file, man seems to somehow sense that it is now
> writing into a pipe rather than into a terminal because it does
> not pause after each screenful asking for keyboard input.

This is because man uses isatty() to determine if the file descriptor 
associated with stdout is a tty filedes or not.  If it is, the output
gets processed through a pager, otherwise it just dumps the output.

> My guess is that when shell forks and execs man, it passes down the
> file descriptor of stdin and stdout, which however, are now different
> from the actual stdin and stdout and man uses (stdout->_file == 1)?
> to determine whether the output is a tty or not (yuck).  But it does
> seem to be very convenient though.

Nope.  stdout->_file is still equal to 1.  The FILE pointer std* is not
passed to the child program, the file descriptors are.  The stdio library
always attatches stdout to file descriptor 1. 

You should note that this does not violate the "basic philosophy of 
transparancy in UNIX" because it is transparent.  The program does have
the ability to break through the transparancy, but the transparancy is
there.
-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+



More information about the Comp.unix.questions mailing list