Unix Programming - Find out current tty name

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2006 > Find out current tty name





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Find out current tty name
Andre

2006-08-21, 7:26 pm

Hi,

Is there any way to programmatically find out the name of the current
device that STDOUT is associated with?

Andre

Nils O. Selåsdal

2006-08-21, 7:26 pm

Andre wrote:
> Hi,
>
> Is there any way to programmatically find out the name of the current
> device that STDOUT is associated with?


ttyname(0);
Rainer Temme

2006-08-21, 7:26 pm

Andre wrote:
> Is there any way to programmatically find out the name of the current
> device that STDOUT is associated with?


Andre,

try ttyname(fileno(stdout))

this works at least, if stdout is connected to a tty.

Rainer
Nils O. Selåsdal

2006-08-21, 7:26 pm

Nils O. Selåsdal wrote:
> Andre wrote:
>
> ttyname(0);

Sorry about that, stdout is fd 1 ,not 0 :-)
Or use fileno(stdout);
James Antill

2006-08-22, 1:39 am

On Mon, 21 Aug 2006 21:22:52 +0200, Nils O. Selåsdal wrote:

> Nils O. Selåsdal wrote:
> Sorry about that, stdout is fd 1 ,not 0 :-)
> Or use fileno(stdout);


One of the many reasons why STDOUT_FILENO is better ;)

--
James Antill -- james@and.org
http://www.and.org/and-httpd

Rainer Temme

2006-08-22, 7:32 am

James Antill wrote:
[vbcol=seagreen]
[vbcol=seagreen]
> One of the many reasons why STDOUT_FILENO is better ;)


IMO fileno(stdout) should be preferred!

stdout is NOT generally associated with filedescriptor 1

Rainer


Andre

2006-08-22, 1:22 pm

James Antill wrote:
> On Mon, 21 Aug 2006 21:22:52 +0200, Nils O. Sel=E5sdal wrote:
>
>
> One of the many reasons why STDOUT_FILENO is better ;)
>


Totally agree My issue came becase I was having issues with
tcgetatrr() and I needed to find out what device I was trying to get
attributes of, in order to understand if my environment was set up
properly. Turns out that I was not using a termios comaptible device,
and I needed to change a few things.

Thanks all for the help.

Andre

James Antill

2006-08-22, 7:35 pm

On Tue, 22 Aug 2006 10:05:20 +0200, Rainer Temme wrote:

> James Antill wrote:
>
>
>
> IMO fileno(stdout) should be preferred!


fileno() assumes you are using stdio, for various reasons that's not
always true[1].

> stdout is NOT generally associated with filedescriptor 1


I very rarely see any applications call freopen(x, y, stdout) ... maybe
your code is different, but I'd hope not.


[1] I realize using a minimal amount of stdio is pretty common, but
I'd certainly argue that not using it at all is at least as common as
freopen(stdout) ... and has a much better argument for doing so.

--
James Antill -- james@and.org
http://www.and.org/and-httpd

Rainer Temme

2006-08-22, 7:35 pm

James Antill wrote:
> fileno() assumes you are using stdio, for various reasons that's not
> always true[1].


Well, but the OP asks:

> Is there any way to programmatically find out the name of the current
> device that STDOUT is associated with?


I had reasons to interpret this as "how do I find out the device
my stdout is connected to".

Nevertheless, thats not a real reason to argue. The OP had been
told about both ways to determine the fd, so he'll know the way to
go.

Rainer
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com