fa.openbsd.tech
[Top] [All Lists]

[patch] ps.c ps.1 Disregard terminal width when STDOUT is not a tty (tak

Subject: [patch] ps.c ps.1 Disregard terminal width when STDOUT is not a tty (take 2)
From: "Evan Clarke" <evan@xxxxxxx>
Date: Sun, 20 Jul 2008 03:11:48 UTC
Newsgroups: fa.openbsd.tech

Hello tech@,

A month or so ago I suggested a patch to make ps(1) ignore the
terminal width when outputting to a pipe - the primary goal was to
make it easier to grep for particular processes and also make some
scripts behave consistantly not matter what the size of the terminal
is.

A couple of suggestions were made - use isatty(3) instead of checking
if stdout is a pipe (which made the patch smaller and more
generalized), and also to update the manual page as well (thanks for
the reminder Theo).

Here I present take 2.
Please let me know what you think.

diff -ru /home/evan/bin/ps/ps.1 bin/ps/ps.1
--- /home/evan/bin/ps/ps.1      Sun Jul 20 12:35:33 2008
+++ bin/ps/ps.1 Sun Jul 20 12:36:11 2008
@@ -167,10 +167,12 @@
 .Dq /dev/drum .
 .It Fl w
 Use 132 columns to display information, instead of the default, which
-is the window size.
+is the window width if displaying to a terminal.
 If the
 .Fl w
-option is specified more than once,
+option is specified more than once or
+.Nm
+is not displaying to a terminal,
 .Nm
 will use as many columns as necessary without regard for window size.
 .It Fl x
diff -ru /home/evan/bin/ps/ps.c bin/ps/ps.c
--- /home/evan/bin/ps/ps.c      Sun Jul 20 12:35:33 2008
+++ bin/ps/ps.c Sun Jul 20 12:36:20 2008
@@ -117,13 +117,16 @@
        char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX];
        size_t size;

-       if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
-           ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
-           ioctl(STDIN_FILENO,  TIOCGWINSZ, &ws) == -1) ||
-           ws.ws_col == 0)
-               termwidth = 79;
+       if (isatty(STDOUT_FILENO))
+               if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
+                   ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
+                   ioctl(STDIN_FILENO,  TIOCGWINSZ, &ws) == -1) ||
+                   ws.ws_col == 0)
+                       termwidth = 79;
+               else
+                       termwidth = ws.ws_col - 1;
        else
-               termwidth = ws.ws_col - 1;
+               termwidth = UNLIMITED;

        if (argc > 1)
                argv[1] = kludge_oldps_options(argv[1]);


<Prev in Thread] Current Thread [Next in Thread>