don't expose struct winsize needlessly.

This commit is contained in:
christos 2010-01-24 16:42:12 +00:00
parent b7f2eb1a7e
commit 6e3a01841c
3 changed files with 42 additions and 28 deletions

View File

@ -34,7 +34,7 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
__RCSID("$Heimdal: get_window_size.c 21005 2007-06-08 01:54:35Z lha $"
"$NetBSD: get_window_size.c,v 1.5 2010/01/20 15:03:50 tsutsui Exp $");
"$NetBSD: get_window_size.c,v 1.6 2010/01/24 16:42:12 christos Exp $");
#endif
#include <stdlib.h>
@ -62,22 +62,34 @@ __RCSID("$Heimdal: get_window_size.c 21005 2007-06-08 01:54:35Z lha $"
#include "roken.h"
int ROKEN_LIB_FUNCTION
get_window_size(int fd, struct winsize *wp)
get_window_size(int fd, int *lines, int *columns)
{
int ret = -1;
memset(wp, 0, sizeof(*wp));
int ret;
char *s;
#if defined(TIOCGWINSZ)
ret = ioctl(fd, TIOCGWINSZ, wp);
{
struct winsize ws;
ret = ioctl(fd, TIOCGWINSZ, &ws);
if (ret != -1) {
if (lines)
*lines = ws.ws_row;
if (columns)
*columns = ws.ws_col;
return 0;
}
}
#elif defined(TIOCGSIZE)
{
struct ttysize ts;
ret = ioctl(fd, TIOCGSIZE, &ts);
if(ret == 0) {
wp->ws_row = ts.ts_lines;
wp->ws_col = ts.ts_cols;
if (ret != -1) {
if (lines)
*lines = ts.ws_lines;
if (columns)
*columns = ts.ts_cols;
return 0;
}
}
#elif defined(HAVE__SCRSIZE)
@ -85,19 +97,24 @@ get_window_size(int fd, struct winsize *wp)
int dst[2];
_scrsize(dst);
wp->ws_row = dst[1];
wp->ws_col = dst[0];
ret = 0;
if (lines)
*lines = dst[1];
if (columns)
*columns = dst[0];
return 0;
}
#endif
if (ret != 0) {
char *s;
if((s = getenv("COLUMNS")))
wp->ws_col = atoi(s);
if (columns) {
` if ((s = getenv("COLUMNS")))
*columns = atoi(s);
else
return -1;
}
if (lines) {
if ((s = getenv("LINES")))
wp->ws_row = atoi(s);
if(wp->ws_col > 0 && wp->ws_row > 0)
ret = 0;
*lines = atoi(s);
else
return -1;
}
return ret;
return 0;
}

View File

@ -34,7 +34,7 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
__RCSID("$Heimdal: getarg.c 21005 2007-06-08 01:54:35Z lha $"
"$NetBSD: getarg.c,v 1.5 2010/01/20 15:03:50 tsutsui Exp $");
"$NetBSD: getarg.c,v 1.6 2010/01/24 16:42:12 christos Exp $");
#endif
#include <stdio.h>
@ -209,7 +209,6 @@ arg_printusage (struct getargs *args,
size_t max_len = 0;
char buf[128];
int col = 0, columns;
struct winsize ws;
if (progname == NULL)
progname = getprogname();
@ -218,9 +217,7 @@ arg_printusage (struct getargs *args,
mandoc_template(args, num_args, progname, extra_string);
return;
}
if(get_window_size(2, &ws) == 0)
columns = ws.ws_col;
else
if(get_window_size(2, NULL, &columns) == -1)
columns = 80;
col = 0;
col += fprintf (stderr, "Usage: %s", progname);

View File

@ -33,7 +33,7 @@
*/
/* $Heimdal: roken.h.in 18612 2006-10-19 16:35:16Z lha $
$NetBSD: roken.h.in,v 1.5 2008/03/22 08:37:21 mlelstv Exp $ */
$NetBSD: roken.h.in,v 1.6 2010/01/24 16:42:12 christos Exp $ */
#include <stdio.h>
#include <stdlib.h>
@ -429,7 +429,7 @@ struct winsize {
};
#endif
int ROKEN_LIB_FUNCTION get_window_size(int fd, struct winsize *);
int ROKEN_LIB_FUNCTION get_window_size(int, int *, int *);
#ifndef HAVE_VSYSLOG
void ROKEN_LIB_FUNCTION vsyslog(int, const char *, va_list);