Xref: utzoo comp.lang.c:21302 comp.lang.c++:4501 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!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: <1088@virtech.UUCP> Date: 30 Aug 89 02:11:01 GMT References: <99@lkbpyr.UUCP> Organization: Virtual Technologies Inc Lines: 37 In article <99@lkbpyr.UUCP>, jonas@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 | +-----------------------------------------------------------------------+