[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: tput returns wrong value?

From: Tim Allen
Subject: Re: tput returns wrong value?
Date: Sat, 23 May 2009 00:33:35 +1000
User-agent: Mutt/1.5.18 (2008-05-17)

On Fri, May 22, 2009 at 03:34:52PM +0200, address@hidden wrote:
> Dear maintainers of ncurses,
> recently i discovered a strange behavior of tput showing by the
> following code example. 

Every Unix process (as I'm sure you know) has three standard
file-descriptors - stdin, stdout and stderr. By default, all three are
connected to the controlling terminal.

To find out the current dimensions of a terminal, I believe the usual
list of fallbacks is:

 - Use an ioctl() on a file-descriptor connected to a terminal (that is,
   a descriptor for which istty() returns true) to ask the kernel what
   the dimensions are.
 - Use the shell's $LINES and $COLUMNS variables.
 - Use the default dimensions specified in the termcap/terminfo

Note that for the ioctl() to work, you need to find a file-descriptor
attached to a terminal.

> $cat testscript 
> tput cols

Here, tput is run with no redirection.

> i=$( tput cols )
> echo $i

Here, tput is run with its stdout redirected into a variable - but it
still has stderr connected to the terminal.

> $./testscript 
> 109
> 109

Here, the first tput reads the dimensions from stdout. The second tput's
stdout is redirected, so it uses stderr instead.

> $./testscript 2> /dev/null
> 109
> 80

Here, the first tput still reads from stdout. The second tput's stdout
is still redirected, but now its stderr is redirected too - so it gives
up trying to find a terminal to talk to and picks a likely number.

There are other alternatives tput might try if stdout and stderr are not
connected to a terminal - it could try stdin, or open /dev/tty to access
the controlling terminal even if all the standard file-descriptors are
redirected. Apparently tput doesn't try these, and I don't know why.

> $ tput cols; foo=$( tput cols ) ; echo $foo 2> /dev/null
> 109
> 109

If you redirect the stdout of the entire command-line, instead of just
redirecting the stdout of the echo command, it still exhibits the

$ ( tput cols; foo=$( tput cols ); echo $foo ) 2> /dev/null

reply via email to

[Prev in Thread] Current Thread [Next in Thread]