tty, tty... where art thou ...?

tty, tty... where art thou ...?

Post by Santhosh Kud » Thu, 28 Mar 2002 11:56:03



Hi,

This code which I wrote gives the remote host from which I rlogged
from. Generally wrote this out of curiosity being a unix novice. But I
notice some strange behavior when used in the shell.

Code included below

I use a solaris 2.7 m/c running bash 2.05
Now if I tried something like

a.out 1>/dev/null
as expected no input, infact ttyname fails as stdout is re-directed to
null device

however if I try `a.out` I again get no output, this I found strange.
I thought that by enclosing a unix command in `command` it would just
execute it in a subshell... Is this true...? But it looks like my
output is being re-directed to somewhere else. Shouldn`t running a
command in `` (grave accent?) gaurantee the tty...? or am i doing
something wrong.

I got a workaround by checking each of stdin|stdout|stderr and so far
find that it usually suceeds on atleast one of them... This might just
be pure luck. (included as <diff> </diff> in snippet below)

finally, I am sure, this code can be written in a much better way,
please excuse the poor code :), If there is a better way please let me
know. My main aim was to get this working across all the shells I
have, sh,ksh,bash,zsh,csh

Thanks,
Cheers
Santhosh Kudva

<Code Snip>---------------
#include <stdio.h>
#include <stdlib.h>
#include <utmpx.h>
#include <unistd.h>

main()
{    
  struct utmpx *ut ;
  int current_user_index = 0 ;
  int flag = 0 ;
  char sttyname[100] ;
  char *tt ;

  while(ut = getutxent())
  {  
      strcpy(sttyname,"/dev/");
      strcat(sttyname,ut->ut_line) ;
      if(ut->ut_type == USER_PROCESS)
      {      
        tt = ttyname(1) ;

        if(tt && !strcasecmp(sttyname,tt) )
        {      
          flag = 1 ;
          fprintf(stdout,"%s\n",ut->ut_host) ;

        }      
      }      
  }  
  if(!flag) fprintf(stdout,"loghost\n") ;

Quote:}    

<diff>
<    tt = ttyname(1) ;

Quote:>    tt=ttyname(0) ;
>    if(!tt) tt = ttyname(1) ;
>       if(!tt) tt = ttyname(2) ;

</diff>
</Code Snip>-------
 
 
 

tty, tty... where art thou ...?

Post by Barry Margoli » Fri, 29 Mar 2002 04:20:48




>Hi,

>This code which I wrote gives the remote host from which I rlogged
>from. Generally wrote this out of curiosity being a unix novice. But I
>notice some strange behavior when used in the shell.

>Code included below

>I use a solaris 2.7 m/c running bash 2.05
>Now if I tried something like

>a.out 1>/dev/null
>as expected no input, infact ttyname fails as stdout is re-directed to
>null device

>however if I try `a.out` I again get no output, this I found strange.
>I thought that by enclosing a unix command in `command` it would just
>execute it in a subshell... Is this true...? But it looks like my
>output is being re-directed to somewhere else. Shouldn`t running a
>command in `` (grave accent?) gaurantee the tty...? or am i doing
>something wrong.

Stdout is being redirected to a pipe, so that the shell can read it.  How
else do you suppose that the output gets substituted into the command line
instead of being displayed on the terminal?

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

tty, tty... where art thou ...?

Post by Santhosh Kud » Fri, 29 Mar 2002 12:03:21


Hi,

Thanks for that clarification, I got it. I was thinking, that the
ttyname function should guarantee to give me the terminal name,
whatever be the case.

I figure it will as long as no other program is messing with the file
descriptor in question as was the case with stdin/stdout/stderr. As
you pointed out these descriptors are used by the shell to redirect
output and therefore, got to take that into account.

I guess one way is to open a new fd and check tty on that. Since that
file-descriptor will not be used by any other program. Is there a
better way to find out the tty...?

incidentally, I was able to achieve the same thing with a

who -m | sed 's;.*(\(.*\)).*;\1;g'

Thanks,

-Cheers
Santhosh Kudva




> >Hi,

> >This code which I wrote gives the remote host from which I rlogged
> >from. Generally wrote this out of curiosity being a unix novice. But I
> >notice some strange behavior when used in the shell.

> >Code included below

> >I use a solaris 2.7 m/c running bash 2.05
> >Now if I tried something like

> >a.out 1>/dev/null
> >as expected no input, infact ttyname fails as stdout is re-directed to
> >null device

> >however if I try `a.out` I again get no output, this I found strange.
> >I thought that by enclosing a unix command in `command` it would just
> >execute it in a subshell... Is this true...? But it looks like my
> >output is being re-directed to somewhere else. Shouldn`t running a
> >command in `` (grave accent?) gaurantee the tty...? or am i doing
> >something wrong.

> Stdout is being redirected to a pipe, so that the shell can read it.  How
> else do you suppose that the output gets substituted into the command line
> instead of being displayed on the terminal?

 
 
 

1. How to map a tty port to another tty port

Hi,

I just installed a new Linux system where users will connect to it using
telnet. On this server I installer a Digi port server (16 ports RS-232).
These 16 ports are connected to another Unix system that is maintained by
another company. I have no access whatsover to administrate this system. I
would like to have my users to connect directly through a telnet session to
one of the ports on my port server. For example how can I redirect input and
output from /dev/pts/2 to /dev/ttyd0.

Thanks for your time
--
/*
 *
 * Infocom enr.
 * Jean-Pierre Dub
 *
 */

2. Radius Authentication w/ Unix Terminal Servers

3. creating tty's, mknod, stuck after boot with wrong tty - help

4. ATI Mach64 with XFree86 3.1.2G?

5. problems with tty, U469036 - bos.rte.tty.4.3.2.9

6. XFree86 seems to run in bursts. Can this be helped?

7. - can one change tty modes for tty not stdio ?

8. Unix before sh

9. tty problem: only root and non-X get tty!

10. Good book on tty, pseudo tty, termio etc ?

11. new tty/old tty

12. Is is possible to make remote tty look like local tty

13. Reading TTY/Pseudo TTY Programming