ss: use COLUMNS from the environment, if TIOCGWINSZ fails

Use the COLUMNS environment variable [1] when determining the screen width,
if using TIOCGWINSZ isn't possible or if it fails.  This allows better use
of the available horizontal screen space in certain scenarios, and makes
the produced outputs more readable, as described further below.

All major shells can maintain the COLUMNS variable according to the current
screen size, [2][3][4] but this shell variable isn't actually an environment
variable, i.e. it doesn't get exported to the shell subprocesses by default.
For example, no COLUMNS environment variable reaches ss(8) when it's executed
as part of a shell pipeline or inside a shell script.

Though, users can opt to export the COLUMNS variable by hand, or they can
rely on some other utilities to do that for them.  A good example of such
utilities is watch(1) that exports COLUMNS as an environment variable to
the processes it executes. [5]  Using ss(8) together with watch(1) is rather
useful, and honoring the exported COLUMNS variable makes the outputs produced
by ss(8) in this scenario more readable.

The behavior of shells, which don't export the COLUMNS variable by default,
makes this change safe in the sense of not affecting the usual shell pipeline
workflows or various shell scripts that use ss(8).

[1] https://pubs.opengroup.org/onlinepubs/9699919799.2016edition/basedefs/V1_chap08.html
[2] https://man.archlinux.org/man/bash.1.en#COLUMNS
[3] https://man.archlinux.org/man/tcsh.1.en#Terminal_management_(+)
[4] https://man.archlinux.org/man/zshall.1.en#Configuration
[5] https://gitlab.com/procps-ng/procps/-/blob/master/NEWS?ref_type=heads#L623

Signed-off-by: Dragan Simic <dsimic@manjaro.org>
Signed-off-by: David Ahern <dsahern@kernel.org>
1 file changed