Respect the setting of the COLUMNS environment variable, use it instead of

the TTY width obtained by ioctl() when set & non-null. (SUSv3)
This commit is contained in:
tjr 2002-06-04 09:52:30 +00:00
parent f577e81d49
commit f91c8f40be
2 changed files with 28 additions and 5 deletions

View File

@ -118,6 +118,19 @@ or one of the special characters '|', '}' and '~'. Logouts produce
an output line without any user name. For more information on the
special characters, see
.Xr utmp 5 .
.Sh ENVIRONMENT
The following environment variables affect the execution of
.Nm :
.Bl -tag -width ".Ev COLUMNS"
.It Ev COLUMNS
If set, specifies the user's preferred output width in column positions.
This is used when the
.Fl q
option is specified to calculate how many user names to display per line.
By default,
.Nm
attempts to automatically determine the terminal width.
.El
.Sh FILES
.Bl -tag -width /var/log/wtmp.[0-6] -compact
.It Pa /var/run/utmp

View File

@ -32,7 +32,9 @@ __FBSDID("$FreeBSD$");
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <langinfo.h>
#include <limits.h>
#include <locale.h>
#include <paths.h>
#include <pwd.h>
@ -271,12 +273,20 @@ int
ttywidth(void)
{
struct winsize ws;
int width;
long width;
char *cols, *ep;
if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') {
errno = 0;
width = strtol(cols, &ep, 10);
if (errno || width <= 0 || width > INT_MAX || ep == cols ||
*ep != '\0')
warnx("invalid COLUMNS environment variable ignored");
else
return ((int)cols);
}
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)
width = ws.ws_col;
else
width = 80;
return (ws.ws_col);
return (width);
return (80);
}