Xref: utzoo comp.lang.c:21341 comp.lang.c++:4516 Path: utzoo!attcan!utgpu!watmath!att!dptg!rutgers!usc!ginosko!uunet!virtech!cpcahil From: cpcahil@virtech.UUCP (Conor P. Cahill) Newsgroups: comp.lang.c,comp.lang.c++ Subject: Re: ttyname() - question Message-ID: <1093@virtech.UUCP> Date: 31 Aug 89 03:39:13 GMT References: <99@lkbpyr.UUCP> <1088@virtech.UUCP> <14481@haddock.ima.isc.com> Organization: Virtual Technologies Inc Lines: 31 In article <14481@haddock.ima.isc.com>, karl@haddock.ima.isc.com (Karl Heuer) writes: > In article <1088@virtech.UUCP> cpcahil@virtech.UUCP (Conor P. Cahill) writes: > >Of course if the user redirects all three, you're out of luck (unless you > >open /dev/tty and then call ttyname() with the fd returned from the open). > > In which case you'll probably (depending on flavor) get the return string > "/dev/tty" rather than the true name of the terminal. (Assuming the open > succeeds in the first place. Not every process has a controlling terminal.) I beg to differ. If the open succeeded you will have gotten a file descriptor for the "controlling terminal", not /dev/tty. The gentty driver, which is the driver for /dev/tty, just obtains the major/minor device number for the current process and calls the device specific open routine for that device. I can't imagine that there is a kernel out there that will return a file descriptor associated with /dev/tty and then re-maps it to the appropriate device for every read and write (which it would have to do if the fd is associated with /dev/tty as opposed to the real terminal). Now that we have the file descriptor for the controlling terminal we pass this to ttyname() which will perform an fstat on that file descriptor and then look through the /dev directory for an entry that matches the device number obtained in the fstat(). -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+