aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Junos <petoju@gmail.com>2019-12-26 14:07:09 +0100
committerDavid Ahern <dsahern@gmail.com>2020-01-02 18:38:08 +0000
commitc4f58629945898722ad9078e0f407c96f1ec7d2b (patch)
tree3cf614146a87c69d948a91d4a8f28fcf599eec64
parent404f2de114f24c03677e441d9e5ce589e45a6527 (diff)
downloadiproute2-c4f58629945898722ad9078e0f407c96f1ec7d2b.tar.gz
ss: use compact output for undetected screen width
This change fixes calculation of width in case user pipes the output. SS output output works correctly when stdout is a terminal. When one pipes the output, it tries to use 80 or 160 columns. That adds a line-break if user has terminal width of 100 chars and output is of the similar width. No width is assumed here. To reproduce the issue, call ss | less and see every other line empty if your screen is between 80 and 160 columns wide. This second version of the patch fixes screen_width being set to arbitrary value. Signed-off-by: Peter Junos <petoju@gmail.com> Signed-off-by: David Ahern <dsahern@gmail.com>
-rw-r--r--misc/ss.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/misc/ss.c b/misc/ss.c
index 95f1d37a4..378b985c6 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -1135,10 +1135,10 @@ static void buf_free_all(void)
buffer.chunks = 0;
}
-/* Get current screen width, default to 80 columns if TIOCGWINSZ fails */
+/* Get current screen width, returns -1 if TIOCGWINSZ fails */
static int render_screen_width(void)
{
- int width = 80;
+ int width = -1;
if (isatty(STDOUT_FILENO)) {
struct winsize w;
@@ -1159,9 +1159,15 @@ static int render_screen_width(void)
*/
static void render_calc_width(void)
{
- int screen_width = render_screen_width();
+ int screen_width, first, len = 0, linecols = 0;
struct column *c, *eol = columns - 1;
- int first, len = 0, linecols = 0;
+ bool compact_output = false;
+
+ screen_width = render_screen_width();
+ if (screen_width == -1) {
+ screen_width = INT_MAX;
+ compact_output = true;
+ }
/* First pass: set width for each column to measured content length */
for (first = 1, c = columns; c - columns < COL_MAX; c++) {
@@ -1183,6 +1189,11 @@ static void render_calc_width(void)
first = 0;
}
+ if (compact_output) {
+ /* Compact output, skip extending columns. */
+ return;
+ }
+
/* Second pass: find out newlines and distribute available spacing */
for (c = columns; c - columns < COL_MAX; c++) {
int pad, spacing, rem, last;