Path: utzoo!attcan!uunet!mcvax!kth!draken!liuida!mikpe From: mikpe@mina.liu.se (Mikael Pettersson) Newsgroups: comp.unix.questions Subject: semantics of /dev/tty? Summary: stat("/dev/tty") != stat(controlling_terminal) Keywords: stat /dev/tty Message-ID: <1181@mina.liu.se> Date: 10 Feb 89 22:54:12 GMT Organization: CIS Dept, Univ of Linkoping, Sweden Lines: 30 Let me begin with a quote from the man page for tty(4): The file /dev/tty is, in each process, a synonym for that process' controlling terminal. Well, I have come to the conclusion that this is not quite true. Consider the case where a program tries to determine whether as given file descriptor `fd' denotes the controlling terminal (*). One strategy might be to fstat() both `fd' and `0' (assuming that standard input is connected to the terminal of course) and then check whether they both produce the same value in the `st_rdev' field in their stat structs. However, if `fd' comes from an open() on "/dev/tty", fstat() will report the of /dev/tty, *not* the ones of the controlling tty. Q1: Why does fstat() (and stat()) fail this "synonym" requirement? Q2: Given that fstat() is useless for the purpose of determining operational equivalence between two (terminal) file descriptors, what would be the `best' way to implement this test? I have hacked up a temporary solution using ioctl() calls to see whether a change in one terminals line discipline also changed the other's. Are there any better (i.e. less kludgey) solutions? /Mike -- (*) This is not just an academic problem: I have a very much real-world application (part of a run-time system) that really *needs* this functionality. -- Mikael Pettersson, Dept of Comp & Info Sci, University of Linkoping, Sweden email: mpe@ida.liu.se or ..!{mcvax,munnari,uunet}!enea!liuida!mpe