What "isatty"?

Steve Summit scs at adam.pika.mit.edu
Tue May 23 08:55:16 AEST 1989


In article <2421 at Portia.Stanford.EDU> joe at hanauma.stanford.edu (Joe Dellinger) writes:
>	I have a graphics driver program which has 2 basic behaviours.
>If you do
>	pspen < plot.data > file
>it writes postscript code to "file". If you do
>	pspen < plot.data
>it spools the postscript for you.

In article <17659 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>Yuck.

Indeed.

>Programs that produce output should produce the output to stdout.  If
>they will rarely be used to write to anything but the spooler, they
>should be given a less-accessible name and the program the user runs
>should be a shell script:

Another common convention, used by the *roff family, is to use a
-t flag to mean "don't spool; send output to stdout."  (This
could easily be implemented inside of Chris's proposed shell
script.)

In general, if you're trying to print a stream off-line, it's
probably better to pipe your program directly to lpr or the
equivalent, rather than creating your own temp file and
"spooling" that.  That is, do

	prog | lpr -Ppostscript

rather than

	prog > tmpfile
	lpr -Ppostscript tmpfile
	rm tmpfile

Besides removing the opportunity for an unremoved tmp file, the
first approach allows the lpr program to control and optimize tmp
file usage: if it is somehow able to send directly to the device
(although I don't know of such a program) no tmp file need be
required at all.  On the other hand, some lpr programs always
create a temporary copy in a spool directory; by using the first
approach you'll only have generated one temporary copy, not two.

[Joe Dellinger again:]
>Questions:
>Where is stdout pointing in such a case? Where does stuff written to it go?

On some systems, stdout is automatically re-plumbed to /dev/null
when you log out, if it had been connected to a terminal.  (This
is the primary purpose of the infamous vhangup call.)

>How can I tell whether stdout is redirected or not on the command line,
>given that "isatty" turns out to not do the job?

In general, you can't.  Isatty tells you whether or not a file is
a terminal, not whether or not it has been redirected.  If I say

	prog > /dev/tty42

where tty42 is somebody else's terminal, isatty(1) within prog
(if prog runs at all) will return true even though prog's output
has been "redirected."  (prog will not run at all if the system
disallows writes to arbitrary terminals by revoking world write
permission.)

                                            Steve Summit
                                            scs at adam.pika.mit.edu



More information about the Comp.unix.wizards mailing list