Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 4 Mar 2017 23:58:36 +0100
From:      Ed Schouten <ed@nuxi.nl>
To:        Conrad Meyer <cem@freebsd.org>
Cc:        src-committers <src-committers@freebsd.org>, svn-src-all@freebsd.org,  svn-src-head@freebsd.org
Subject:   Re: svn commit: r314685 - head/bin/ps
Message-ID:  <CABh_MKnux-yfKhFeuApsZSNgyO1sktA_%2BDphhHQXaZezx92Cpw@mail.gmail.com>
In-Reply-To: <201703042238.v24McAD8008837@repo.freebsd.org>
References:  <201703042238.v24McAD8008837@repo.freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi Conrad,

2017-03-04 23:38 GMT+01:00 Conrad Meyer <cem@freebsd.org>:
> Log:
>   ps(1): Only detect terminal width if stdout is a tty

Nice!

> Modified: head/bin/ps/ps.c
> ==============================================================================
> --- head/bin/ps/ps.c    Sat Mar  4 22:23:59 2017        (r314684)
> +++ head/bin/ps/ps.c    Sat Mar  4 22:38:10 2017        (r314685)
> @@ -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) ||
>

I think you can actually go ahead and simplify this a bit:

- If something is a TTY, then our implementation of the TTY layer
guarantees that TIOCGWINSZ always works.
- If we're only interested in testing stdout whether it's a TTY, I
think it makes little sense to check TIOCGWINSZ on stdin, stderr.

I think there would therefore be very little harm to use something like this:

|         if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0')
|                 termwidth = atoi(cols);
|         else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) ==
-1 && ws.ws_row == 0)
|                 termwidth = UNLIMITED;
|        else
|                termwidth = ws.ws_col - 1;

-- 
Ed Schouten <ed@nuxi.nl>
Nuxi, 's-Hertogenbosch, the Netherlands
KvK-nr.: 62051717



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CABh_MKnux-yfKhFeuApsZSNgyO1sktA_%2BDphhHQXaZezx92Cpw>