ttyname() - question

Conor P. Cahill cpcahil at virtech.UUCP
Thu Aug 31 13:39:13 AEST 1989


In article <14481 at haddock.ima.isc.com>, karl at haddock.ima.isc.com (Karl Heuer) writes:
> In article <1088 at virtech.UUCP> cpcahil at 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     |
+-----------------------------------------------------------------------+



More information about the Comp.lang.c mailing list