[comp.sys.sun] Finding your tty

dmd@uunet.uu.net (dave driver) (04/26/89)

cstw01!meulenbr@uunet.uu.net (Frans Meulenbroeks) writes:
> Is there a way to detect your login tty?  

/usr/bin/tty will show the name of the tty for your current shell (RTFM
would have been good advice here), but if you specifically want to know
the tty on which you logged in, try setting an environment variable from
your .login file, i.e.

setenv LOGTTY `tty`

N.B. this will not work in a remote shell since the environment will not
be passed in this case due to a documented bug.

 Dave Driver (D^2)		dmd@cel.co.uk
 Crosfield Electronics		mcvax!ukc!uk.co.cel!dmd
 +44 442 230000 ext 3355

[[ RTFOP might be appropriate for this message (OP == "original posting"):
"ttyname(3) only gives me the name of the pty of the current window or
rlogin session."  He doesn't want the name of the tty associated with the
current window (which is what /usr/bin/tty gives you).  He wants the name
of the tty you originally (as in before starting suntools) logged in on.
To answer that question:  other than looking in utmp, the only way I can
think of doing it is walking back through the chain of parent process proc
structures to find the one for the login process and extract the
controlling terminal id from there.  Not a pleasant procedure.  --wnl ]]

matt@uhura.cc.rochester.edu (05/08/89)

Dave Driver actually came up with a pretty good solution to the problem of
finding what terminal you originally logged in on.  Solving the "rlogin"
problem can be tricky, but since Dave's solution would return the pty of
the original rlogin, it seems to be a good compromise (i.e. it would only
return the "wrong" info if you rlogged into the same machine).

The reason his solution works is that the .login file only gets executed
when the shell is a "login" shell.  An environment variable would then be
propagated along containing the tty name of the login shell.  Pretty much
what was asked for.  It would certainly work "right" for suntools, just a
little questionable for rlogins...

THE PROVERBIAL EXAMPLE (a slightly doctored script):

Script started on Wed Apr 26 10:24:37 1989
flash% cat .login
setenv LOGTTY `tty`
flash% cat .cshrc
setenv MYTTY `tty`
flash% rlogin dash
Last login: Fri Mar 31 10:23:36 from flash
Sun UNIX 4.2 Release 3.2 (DASH) #29: Tue Feb 21 14:09:11 EST 1989
You have mail.
dash% tty
/dev/ttyp0
dash% echo $LOGTTY
/dev/ttyp0
dash% echo $MYTTY
/dev/ttyp0
dash% screen			/* screen starts up three sub-windows */
dash% echo $MYTTY
/dev/ttyp7
dash% echo $LOGTTY
/dev/ttyp0
dash% ^D			/* log out of first */
dash% echo $MYTTY
/dev/ttyp5
dash% echo $LOGTTY
/dev/ttyp0
dash% ^D			/* log out of second */
dash% ^D			/* exit screen program */
dash% logout			/* logout of machine "dash" */
Connection closed.
flash% ^D
script done on Wed Apr 26 10:26:39 1989

-----
- uucp:		{rutgers,ames}!rochester!srs!matt	Matt Goheen 	-
- internet:	matt@srs.uucp OR matt%srs.uucp@harvard.harvard.edu	-

gorpong@uunet.uu.net (Gordon C. Galligher) (05/18/89)

In article <8904061109.AA02168@sun103.cel.uucp> mcvax!cel!dmd@uunet.uu.net 
	(dave driver) writes:
>X-Sun-Spots-Digest: Volume 7, Issue 248, message 7 of 13
>cstw01!meulenbr@uunet.uu.net (Frans Meulenbroeks) writes:
>> Is there a way to detect your login tty?  
>
[..RTFM message deleted..]
>
>setenv LOGTTY `tty`
>...
>RTFOP might be appropriate for this message (OP == "original posting"):
>"ttyname(3) only gives me the name of the pty of the current window or
>rlogin session."...--wnl

Not quite.  Dave is actually right, almost.  He does want the `tty` from the
login terminal, so what Frans needs to add to .login is:

	if ( ! $?LOGTTY ) setenv LOGTTY `tty`

This will set LOGTTY if and ONLY IF it is not currently set (as in a
virgin login session).  It is not required to go through the parent
process proc structures.  Each case of the subshells created through
sunview/suntools will inherit the parent's environment (as they should) in
which case the LOGTTY variable will be set.  Since the setenv LOGTTY is in
the .login it will only be executed when the person logs in (or on a
latter 'source' command), subshells created by suntools/sunview do use
pseudo-ttys, but they do not execute the .login file.  If, of course, he
does an 'rlogin' through the initialization file for suntools then there
is no way that the environment will be set, because he is doing a totally
complete login session, in which case the LOGTTY will be correctly set for
that system.

For those of you sh/ksh hackers, you should add this to .profile:

	if [ ${LOGTTY:-foobar} = foobar ] ; then
		LOGTTY=`tty`
		export LOGTTY
	fi


		-- Gordon.
-- 
Gordon C. Galligher  <|> ...!uunet!telxon!gorpong <|> gorpong@teleng.uucp.uu.net
Telxon Corporation
Akron, Ohio, 44313   
(216) 867-3700 (3512)