[comp.unix.questions] What is the name of /dev/tty?

aj@zyx.UUCP (Arndt Jonasson) (10/12/87)

Is there any way in Unix (either BSD or SYS5) to obtain the true name
of the process's controlling terminal?

In other words, if my program opens /dev/tty, how can it find out which
real terminal this corresponds to? The real name may be something like
/dev/ttyp2.

Examples (imagine 'prog' being run on /dev/ttyp2):

prog >foo		Here isatty(3) says that stdout is not a tty, so
			this case is easy.

prog			Here /dev/tty and stdout are 'the same'.

prog >/dev/ttyp6	Here they are not.

The reason why it would be interesting to know this is that I want my
program to know whether its standard output and its controlling terminal
are the same, in the sense that output to the two streams will mix on
the same window/screen. Error messages will use /dev/tty only if it is
not the same as stdout, to avoid mixing of output from different write
operations on the screen.
-- 
Arndt Jonasson, ZYX Sweden AB, Styrmansgatan 6, 114 54 Stockholm, Sweden
email address:	 <backbone>!mcvax!enea!!zyx!aj	=	aj@zyx.SE

ron@topaz.rutgers.edu (Ron Natalie) (10/17/87)

Error messages should be written to "stderr" (file descriptor 2 in
UNIX) not "stdout" (File descriptor 1).  This I think will solve
your problem, and be more like other UNIX applications.

-Ron

pdb@sei.cmu.edu (Patrick Barron) (10/18/87)

In article <1418@zyx.UUCP> aj@zyx.SE (Arndt Jonasson) writes:
>
>Is there any way in Unix (either BSD or SYS5) to obtain the true name
>of the process's controlling terminal?

On 4BSD, you can find out the terminal connected to /dev/tty by digging
it out of your process's u area.  If you don't have read access to /dev/kmem,
your process can fork off a child temporarily, and use ptrace(2) to get at
the info.  This will yield the major and minor device numbers of the
controlling tty (or zero, if your process isn't attached to a terminal).
You could also look for your own entry in /etc/utmp the way "who am i"
does.

>The reason why it would be interesting to know this is that I want my
>program to know whether its standard output and its controlling terminal
>are the same, in the sense that output to the two streams will mix on
>the same window/screen. Error messages will use /dev/tty only if it is
>not the same as stdout, to avoid mixing of output from different write
>operations on the screen.

Why not just use stderr for error messages instead?

--Pat.

chris@mimsy.UUCP (Chris Torek) (10/18/87)

In article <2903@aw.sei.cmu.edu> pdb@sei.cmu.edu (Patrick Barron) writes:
>On 4BSD, you can find out the terminal connected to /dev/tty by digging
>it out of your process's u area.  If you don't have read access to /dev/kmem,
>your process can fork off a child temporarily, and use ptrace(2) to get at
>the info.

<valley guy accent on> Ewwww, gross! <valley guy accent off>

On 4BSD Vaxen, at least, the u. area is easily read with

	struct user *u = (struct user *)(0x80000000 - UPAGES*NBPG);

where UPAGES and NBPG can be obtained with

	#include <sys/param.h>

You can then read `u->u_ttyd' and other miscellany directly.
(And no, you cannot write over them.)

This, too, is ugly (but effective).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris