From f8d844f4373c005773a7d1565f189c0f55d86312 Mon Sep 17 00:00:00 2001 From: Craig Small Date: Tue, 16 Jul 2013 21:39:09 +1000 Subject: pstree uses COLUMNS environement variable pstree previously only used the window size for determining number of columns, then a default of 132. With this change, pstree now checks the COLUMNS environment variable first and uses that if valid. env checking code nicked from top.c, by Jim Warner. Bug-Debian: http://bugs.debian.org/717017 --- ChangeLog | 1 + doc/pstree.1 | 10 +++++----- src/pstree.c | 28 ++++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index d29569d..d9e2db0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ Changes in 22.21 ================ * Missing comma in fuser(1) added Debian #702391 + * pstree uses COLUMN env variable Debian #717017 Changes in 22.20 ================ diff --git a/doc/pstree.1 b/doc/pstree.1 index fea80b6..6a9c3a8 100644 --- a/doc/pstree.1 +++ b/doc/pstree.1 @@ -1,12 +1,12 @@ .\" .\" Copyright 1993-2002 Werner Almesberger -.\" 2002-2012 Craig Small +.\" 2002-2013 Craig Small .\" This program is free software; you can redistribute it and/or modify .\" it under the terms of the GNU General Public License as published by .\" the Free Software Foundation; either version 2 of the License, or .\" (at your option) any later version. .\" -.TH PSTREE 1 "2012-07-28" "psmisc" "User Commands" +.TH PSTREE 1 "2013-07-16" "psmisc" "User Commands" .SH NAME pstree \- display a tree of processes .SH SYNOPSIS @@ -115,9 +115,9 @@ parentheses after each process name. implicitly disables compaction. If both PIDs and PGIDs are displayed then PIDs are shown first. .IP \fB\-l\fP -Display long lines. By default, lines are truncated to the display -width or 132 if output is sent to a non-tty or if the display width is -unknown. +Display long lines. By default, lines are truncated to either the COLUMNS +environment variable or the display width. If neither of these methods work, +the default of 132 columns is used. .IP \fB\-n\fP Sort processes with the same ancestor by PID instead of by name. (Numeric sort.) diff --git a/src/pstree.c b/src/pstree.c index b9a01cf..4eab5dd 100644 --- a/src/pstree.c +++ b/src/pstree.c @@ -139,6 +139,29 @@ static int dumped = 0; /* used by dump_by_user */ static int charlen = 0; /* length of character */ static void fix_orphans(security_context_t scontext); + +/* + * Determine the correct output width, what we use is: + */ +static int get_output_width(void) +{ + char *ep, *env_columns; + struct winsize winsz; + + env_columns = getenv("COLUMNS"); + if (env_columns && *env_columns) { + long t; + t = strtol(env_columns, &ep, 0); + if (!*ep && (t > 0) && (t < 0x7fffffffL)) + return (int)t; + } + if (ioctl(1, TIOCGWINSZ, &winsz) >= 0) + if (winsz.ws_col) + return winsz.ws_col; + return 132; + +} + /* * Allocates additional buffer space for width and more as needed. * The first call will allocate the first buffer. @@ -814,7 +837,6 @@ void print_version() int main(int argc, char **argv) { PROC *current; - struct winsize winsz; const struct passwd *pw; pid_t pid, highlight; char termcap_area[1024]; @@ -842,9 +864,7 @@ int main(int argc, char **argv) { 0, 0, 0, 0 } }; - if (ioctl(1, TIOCGWINSZ, &winsz) >= 0) - if (winsz.ws_col) - output_width = winsz.ws_col; + output_width = get_output_width(); pid = ROOT_PID; highlight = 0; pw = NULL; -- cgit v1.2.3