diff options
author | Conrad Meyer <cem@FreeBSD.org> | 2017-03-04 22:38:10 +0000 |
---|---|---|
committer | Conrad Meyer <cem@FreeBSD.org> | 2017-03-04 22:38:10 +0000 |
commit | 9c4241c3d7a90548df819a624cbdcf8e5235ca82 (patch) | |
tree | 3400d2c888322c63968612dc4afe3c83468d70ef /bin/ps | |
parent | ac0577afe964ea3e643aac877ef8369261cd5a69 (diff) | |
download | src-test2-9c4241c3d7a90548df819a624cbdcf8e5235ca82.tar.gz src-test2-9c4241c3d7a90548df819a624cbdcf8e5235ca82.zip |
ps(1): Only detect terminal width if stdout is a tty
If stdout isn't a tty, use unlimited width output rather than truncating to
79 characters. This is helpful for shell scripts or e.g., 'ps | grep foo'.
This hardcoded width has some history: In The Beginning of History[0], the
width of ps was hardcoded as 80 bytes. In 1985, Bloom@ added detection
using TIOCGWINSZ on stdin.[1] In 1986, Kirk merged a change to check
stdout's window size instead. In 1990, the fallback checks to stderr and
stdin's TIOCGWINSZ were added by Marc@, with the commit message "new
version."[2]
OS X Darwin has a very similar modification to ps(1), which simply sets
UNLIMITED for all non-tty outputs.[3] I've chosen to respect COLUMNS
instead of behaving identically to Darwin here, but I don't feel strongly
about that. We could match OS X for parity if that is desired.
[0]: https://svnweb.freebsd.org/csrg/bin/ps/ps.c?annotate=1065
[1]: https://svnweb.freebsd.org/csrg/bin/ps/ps.c?r1=18105&r2=18106
[2]:
https://svnweb.freebsd.org/csrg/bin/ps/ps.c?r1=40675&r2=40674&pathrev=40675
[3]:
https://opensource.apple.com/source/adv_cmds/adv_cmds-168/ps/ps.c.auto.html
PR: 217159
Reported by: Deepak Nagaraj <n.deepak at gmail.com>
Notes
Notes:
svn path=/head/; revision=314685
Diffstat (limited to 'bin/ps')
-rw-r--r-- | bin/ps/ps.c | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/bin/ps/ps.c b/bin/ps/ps.c index 12a6f81c99ad..4fb0be9722ca 100644 --- a/bin/ps/ps.c +++ b/bin/ps/ps.c @@ -194,6 +194,8 @@ main(int argc, char *argv[]) if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') termwidth = atoi(cols); + else if (!isatty(STDOUT_FILENO)) + 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) || |