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") ;
<diff>Quote:}
< tt = ttyname(1) ;
</diff>Quote:> tt=ttyname(0) ;
> if(!tt) tt = ttyname(1) ;
> if(!tt) tt = ttyname(2) ;
</Code Snip>-------