[comp.lang.c] getting users' tty # into a C program

davek@lakesys.UUCP (Dave Kraft) (03/05/89)

Hi,
I am writing a C program that need a user's tty number (i.e. tty3A).  Is there
any internal/external function that can accomplish this?  Any help would be
appriciated.  Thanks in advance.

Dave

-- 
davek@lakesys.lakesys.com -or-
uunet!marque!lakesys!davek
"The meek will inherit the earth, the rest of us will go to the stars"
-- 'Omni' (magazine) button

john@chinet.chi.il.us (John Mundt) (03/06/89)

In article <441@lakesys.UUCP> davek@lakesys.UUCP (Dave Kraft) writes:
>I am writing a C program that need a user's tty number (i.e. tty3A).  Is there
>any internal/external function that can accomplish this? 

Try ttyname(3C) which will return a pointer to a null-terminated 
character string of the terminal device associated with the file
descriptor you send it.  So you can do something like

char *tty_ptr, *strrchr(), *ttyname();

	tty_ptr = strrchr(ttyname(0)) + 1;

assuming that the tty's have something like /dev/ttynnn or the
like.


-- 
---------------------
John Mundt   Teachers' Aide, Inc.  P.O. Box 1666  Highland Park, IL
john@chinet.chi.il.us
(312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem  

john@chinet.chi.il.us (John Mundt) (03/06/89)

In article <441@lakesys.UUCP> davek@lakesys.UUCP (Dave Kraft) writes:
>I am writing a C program that need a user's tty number (i.e. tty3A).  Is there
>any internal/external function that can accomplish this?

Bah! that last one should have been

	char *tty_ptr, *strrchr(), *ttyname();
	tty_ptr = strrchr(ttyname(0), '/') + 1;


-- 
---------------------
John Mundt   Teachers' Aide, Inc.  P.O. Box 1666  Highland Park, IL
john@chinet.chi.il.us
(312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem  

gwyn@smoke.BRL.MIL (Doug Gwyn ) (03/06/89)

In article <441@lakesys.UUCP> davek@lakesys.UUCP (Dave Kraft) writes:
>I am writing a C program that need a user's tty number (i.e. tty3A).  Is there
>any internal/external function that can accomplish this?

Most UNIX systems have a ttyname() library function for doing this.
It doesn't work perfectly...

spolsky-joel@CS.YALE.EDU (Joel Spolsky) (03/06/89)

In article <9794@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>In article <441@lakesys.UUCP> davek@lakesys.UUCP (Dave Kraft) writes:
>>I am writing a C program that need a user's tty number (i.e. tty3A).  Is there
>>any internal/external function that can accomplish this?
>
>Most UNIX systems have a ttyname() library function for doing this.


failing that try fd=popen("tty"); fscanf(fd,"%s",ttyname); close(fd)


+----------------+----------------------------------------------------------+
|  Joel Spolsky  | bitnet: spolsky@yalecs.bitnet     uucp: ...!yale!spolsky |
|                | internet: spolsky@cs.yale.edu     voicenet: 203-436-1483 |
+----------------+----------------------------------------------------------+
                                                      #include <disclaimer.h>

davek@lakesys.UUCP (Dave Kraft) (03/06/89)

In article <9794@smoke.BRL.MIL>, gwyn@smoke.BRL.MIL (Doug Gwyn ) writes:
> In article <441@lakesys.UUCP> davek@lakesys.UUCP (Dave Kraft) writes:
> >I am writing a C program that need a user's tty number (i.e. tty3A).  Is there
> >any internal/external function that can accomplish this?
> 
> Most UNIX systems have a ttyname() library function for doing this.
> It doesn't work perfectly...


Here's what I tried:

main()
{
	char *name;

	*name=ttyname();
	printf("%s\n", *name);
}

(Plus I grepped around in all of the include files for ttyname, and I couldn't
find it)  Is there anything I'm doing wrong in the above program?? 
Thanks in advance.
Dave

-- 
davek@lakesys.lakesys.com -or-
uunet!marque!lakesys!davek
"The meek will inherit the earth, the rest of us will go to the stars"
-- 'Omni' (magazine) button

marcoz@MARCOZ.BOLTZ.CS.CMU.EDU (Marco Zagha) (03/06/89)

In article 52746@yale-celray.yale.UUCP> spolsky-joel@CS.YALE.EDU (Joel Spolsky) writes:

> failing that try fd=popen("tty"); fscanf(fd,"%s",ttyname); close(fd)

popen returns a FILE pointer.  To close this file pointer use pclose(),
not close(). I think this is what should be done (as you said, 
failing ttyname()):

FILE *f;
f=popen("/usr/bin/tty","r"); fscanf(f,"%s",ttyname); pclose(f)

== Marco (marcoz@cs.cmu.edu)



-- 

karl@haddock.ima.isc.com (Karl Heuer) (03/07/89)

>FILE *f;
>f=popen("/usr/bin/tty","r"); fscanf(f,"%s",ttyname); pclose(f)

Better, but it should probably be noted that this is LESS likely to work than
the ttyname() function it's covering for.  (I'd be extremely surprised to hear
of a system where this works, but ttyname() doesn't.)

Use ttyname().  It isn't in the pANS, of course, but it's still more portable
than any form of the popen() hack.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

mount@hpcupt1.HP.COM (John Mount) (03/07/89)

>/ hpcupt1:comp.lang.c / davek@lakesys.UUCP (Dave Kraft) /  3:15 pm  Mar  5, 1989 /
>In article <9794@smoke.BRL.MIL>, gwyn@smoke.BRL.MIL (Doug Gwyn ) writes:
>> In article <441@lakesys.UUCP> davek@lakesys.UUCP (Dave Kraft) writes:
>> >I am writing a C program that need a user's tty number (i.e. tty3A).  Is there
>> >any internal/external function that can accomplish this?
>> 
>> Most UNIX systems have a ttyname() library function for doing this.
>> It doesn't work perfectly...
>
>
>Here's what I tried:
>
>main()
>{
>	char *name;
>
>	*name=ttyname();
>	printf("%s\n", *name);
>}
>
>(Plus I grepped around in all of the include files for ttyname, and I couldn't
>find it)  Is there anything I'm doing wrong in the above program?? 
>Thanks in advance.
>Dave
>
Read up!  ttyname needs a file descriptor as an argument and you shouldn't
be dereferencing name when you use it.  The following works on an Hp9000/360
running HPUX6.02:

#include <stdio.h>
extern char *ttyname();
main()
{
       char *name;

       name=ttyname(0);
       printf("%s\n", name);
       return(0);
}


John

nobody@tekecs.GWD.TEK.COM (-for inetd server command) (03/08/89)

In article <444@lakesys.UUCP> davek@lakesys.UUCP (Dave Kraft) writes:
>Here's what I tried:
>
>main()
>{
>	char *name;
>
>	*name=ttyname();
>	printf("%s\n", *name);
>}


Try this;  It should work.

main()
{
char name[1025];

	strcpy(name,ttyname());
	printf("%s\n",name);
}

>
>(Plus I grepped around in all of the include files for ttyname, and I couldn't
>find it)  Is there anything I'm doing wrong in the above program?? 
>Thanks in advance.

Close, but not quite.  Try this:

% ar t /lib/libc.a | grep ttyname

It should find a ttyname.o  This is included by the loader when ld'ing your
program automatically.


Hope this helps,

	Jeff Beadles

--
Jeff Beadles  Utek Sustaining Engineering
jeff@quark.WV.TEK.COM

ambarish@gandhi.UUCP (Ambarish Malpani) (03/09/89)

In article <444@lakesys.UUCP>, davek@lakesys.UUCP (Dave Kraft) writes:
> In article <9794@smoke.BRL.MIL>, gwyn@smoke.BRL.MIL (Doug Gwyn ) writes:
> > In article <441@lakesys.UUCP> davek@lakesys.UUCP (Dave Kraft) writes:
> > >I am writing a C program that need a user's tty number (i.e. tty3A).  Is there
> > >any internal/external function that can accomplish this?
> > 
> > Most UNIX systems have a ttyname() library function for doing this.
> > It doesn't work perfectly...

> Here's what I tried:
> 
> main()
> {
> 	char *name;
> 
> 	*name=ttyname();
> 	printf("%s\n", *name);
> }
> 
> Dave
> 

try:

main()
{
	char *name, *ttyname();

	name = ttyname(file_id_you_are_interested_in_eg_0_for_stdin);
	printf("%s\n", name);
}