Try stderr if we're asking for terminal size if stdout is not connected to a tty.

This commit is contained in:
Dianne Skoll
2025-12-29 17:30:56 -05:00
parent 59c434ce34
commit 6d4c38126e
2 changed files with 12 additions and 7 deletions

View File

@@ -3848,9 +3848,10 @@ integer.
.TP
.B columns([s_arg])
If called with no arguments, \fBcolumns()\fR behaves as follows:
If standard output is a TTY, returns the width of the terminal in columns.
If standard output is not a TTY, attempts to open "/dev/tty" to obtain
the terminal size. If this fails, returns -1.
If either standard output or standard error is a TTY, returns the
width of the terminal in columns. If neither standard output nor
standard error is a TTY, attempts to open "/dev/tty" to obtain the
terminal size. If this fails, returns -1.
.RS
.PP
If called with a single string argument, \fBcolumns(str)\fR returns
@@ -4638,9 +4639,10 @@ Returns the date as provided by the operating system. This is in contrast to
in calendar mode, or if a date has been supplied on the command line.
.TP
.B rows()
If standard output is a TTY, returns the height of the terminal in rows.
If standard output is not a TTY, attempts to open "/dev/tty" to obtain
the terminal size. If this fails, returns -1.
If either standard output or standard error is a TTY, returns the
height of the terminal in rows. If neither standard output nor
standard error is a TTY, attempts to open "/dev/tty" to obtain the
terminal size. If this fails, returns -1.
.TP
.B sgn(i_num)
Returns \-1 if \fInum\fR is negative, 1 if \fInum\fR is positive,

View File

@@ -4889,10 +4889,13 @@ rows_or_cols(func_info *info, int want_rows)
{
struct winsize w;
int fd = STDOUT_FILENO;
int opened = 0;
RetVal.type = INT_TYPE;
if (!isatty(fd)) {
/* Try STDERR fd if STDOUT fd is not a tty */
fd = STDERR_FILENO;
}
if (!isatty(fd)) {
fd = open("/dev/tty", O_RDONLY);
if (fd < 0) {