ttyname() - question

Conor P. Cahill cpcahil at virtech.UUCP
Wed Aug 30 12:11:01 AEST 1989


In article <99 at lkbpyr.UUCP>, jonas at lkbpyr.UUCP (Jonas Heyman) writes:
> Hello,
> 
> I was wondering why this work:
> 

    [ example 1 with ttyname() in main() ]

> And why this don't:

    [ example 2 with ttyname() in test() called from main() ]

> 
> All the above should type out your current 'tty',example: '/dev/tty023'.
> 

This is really on of those RTFM kind of things.  ttyname() takes a
file descriptor as an argument.  Both times you were getting whatever
value happened to be on the stack at the time the ttyname was called,
you were lucky the first time (within main) probably because the
ttyname was called so early that the stack was relatively clean, but
even this is a bad assumption because other processing has already
been accomplished in the startup routines before main is called.

So add the appropriate file descriptor whose ttyname you wish to 
obtain.  (I usually use 2, to allow for redirection of stdout and
stdin and still get the ttyname.  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).



-- 
+-----------------------------------------------------------------------+
| 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