Bug 164535

Summary: [patch] ps(1) truncates command to screen size even when stdout is not a tty
Product: Base System Reporter: Marcus Reid <marcus>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me CC: bdrewery, pstef
Priority: Normal    
Version: 9.0-STABLE   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description Marcus Reid 2012-01-27 09:50:09 UTC
ps(1) truncates long commands to the size of the screen even when stdout is not a terminal.  This is counter-intuitive and differs from another implementation I looked at.  Output of ps | grep differs depending on how big your terminal window is for example.

Fix: Patch included.  Tested; behavior remains consistent with docs: COLUMNS variable retains effect, and -w limits to 132 characters still.


--- bin/ps/ps.c.orig    2012-01-27 01:24:10.519024952 -0800
+++ bin/ps/ps.c 2012-01-27 01:24:20.350023629 -0800
@@ -187,6 +187,8 @@
 
        if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0')
                termwidth = atoi(cols);
+       else if (isatty(STDOUT_FILENO) == 0)
+               termwidth = UNLIMITED;
        else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
             ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
             ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||


Patch attached with submission follows:
How-To-Repeat: ps aux | grep
ps auxww | grep
Comment 1 Bruce Evans freebsd_committer freebsd_triage 2012-01-27 11:49:35 UTC
On Fri, 27 Jan 2012, Marcus Reid wrote:

>> Description:
> ps(1) truncates long commands to the size of the screen even when stdout is not a terminal.  This is counter-intuitive and differs from another implementation I looked at.  Output of ps | grep differs depending on how big your terminal window is for example.
>> How-To-Repeat:
> ps aux | grep
> ps auxww | grep
>> Fix:
> Patch included.  Tested; behavior remains consistent with docs: COLUMNS variable retains effect, and -w limits to 132 characters still.
>
>
> >
> --- bin/ps/ps.c.orig	2012-01-27 01:24:10.519024952 -0800
> +++ bin/ps/ps.c	2012-01-27 01:24:20.350023629 -0800
> @@ -187,6 +187,8 @@
>
> 	if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0')
> 		termwidth = atoi(cols);
> +	else if (isatty(STDOUT_FILENO) == 0)
> +		termwidth = UNLIMITED;

This change defeats the else clause, which checks all of STDOUT_FILENO,
STDERR_FILENO and STDIN_FILENO and uses the terminal width iff any if
these is a terminal according to the TIOCGWINSZ test for being a
terminal, and otherwise gives a default of 79 columns (not UNLIMITED,
which is only documented for ps -ww and only reachable using that and
via the missing sanity checking for COLUMNS=0).

> 	else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
> 	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
> 	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||

So the correct way to avoid terminalness when outputting to a file
is "ps </dev/null >foo 2>&1".  This is too strange for stdin, so
apparently checking all 3 fd's is a feature, precisely to get the
terminal width from somewhere even when stdout and stderr are
redirected to a file.  "COLUMNS=0 ps" is an easier way.  If this
feature is considered a bug, then remove the checks of STDERR_FILENO
and STDIN_FILENO.

Bruce
Comment 2 Eitan Adler freebsd_committer freebsd_triage 2017-12-31 07:58:27 UTC
For bugs matching the following criteria:

Status: In Progress Changed: (is less than) 2014-06-01

Reset to default assignee and clear in-progress tags.

Mail being skipped
Comment 3 Piotr Pawel Stefaniak freebsd_committer freebsd_triage 2020-06-21 07:02:54 UTC
In the light of r330091 and r330712 this PR can probably be closed.
Comment 4 Bryan Drewery freebsd_committer freebsd_triage 2022-06-08 15:28:34 UTC
(In reply to Piotr Pawel Stefaniak from comment #3)

Yup this appears to work fine now.