get_window_size.c revision 55682
190075Sobrien/* 2169689Skan * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska H�gskolan 390075Sobrien * (Royal Institute of Technology, Stockholm, Sweden). 490075Sobrien * All rights reserved. 590075Sobrien * 690075Sobrien * Redistribution and use in source and binary forms, with or without 790075Sobrien * modification, are permitted provided that the following conditions 890075Sobrien * are met: 990075Sobrien * 1090075Sobrien * 1. Redistributions of source code must retain the above copyright 1190075Sobrien * notice, this list of conditions and the following disclaimer. 1290075Sobrien * 1390075Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1490075Sobrien * notice, this list of conditions and the following disclaimer in the 1590075Sobrien * documentation and/or other materials provided with the distribution. 1690075Sobrien * 1790075Sobrien * 3. Neither the name of the Institute nor the names of its contributors 1890075Sobrien * may be used to endorse or promote products derived from this software 19169689Skan * without specific prior written permission. 20169689Skan * 2190075Sobrien * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 2290075Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2390075Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2490075Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 2590075Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2690075Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2790075Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2890075Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2990075Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3090075Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3190075Sobrien * SUCH DAMAGE. 3290075Sobrien */ 3390075Sobrien 3490075Sobrien#ifdef HAVE_CONFIG_H 3590075Sobrien#include <config.h> 3690075SobrienRCSID("$Id: get_window_size.c,v 1.9 1999/12/02 16:58:46 joda Exp $"); 3790075Sobrien#endif 3890075Sobrien 3990075Sobrien#include <stdlib.h> 4090075Sobrien#ifdef HAVE_UNISTD_H 4190075Sobrien#include <unistd.h> 4290075Sobrien#endif 4390075Sobrien#ifdef HAVE_SYS_TYPES_H 44117395Skan#include <sys/types.h> 4590075Sobrien#endif 46117395Skan 4790075Sobrien#if 0 /* Where were those needed? /confused */ 4890075Sobrien#ifdef HAVE_SYS_PROC_H 4990075Sobrien#include <sys/proc.h> 5090075Sobrien#endif 5190075Sobrien 5290075Sobrien#ifdef HAVE_SYS_TTY_H 5390075Sobrien#include <sys/tty.h> 5490075Sobrien#endif 5590075Sobrien#endif 56132718Skan 5790075Sobrien#ifdef HAVE_TERMIOS_H 5890075Sobrien#include <termios.h> 5990075Sobrien#endif 60132718Skan 6190075Sobrien#include <roken.h> 6290075Sobrien 63132718Skanint 64169689Skanget_window_size(int fd, struct winsize *wp) 65169689Skan{ 66169689Skan int ret = -1; 6790075Sobrien 6890075Sobrien memset(wp, 0, sizeof(*wp)); 6990075Sobrien 7090075Sobrien#if defined(TIOCGWINSZ) 7190075Sobrien ret = ioctl(fd, TIOCGWINSZ, wp); 7290075Sobrien#elif defined(TIOCGSIZE) 73117395Skan { 7490075Sobrien struct ttysize ts; 7590075Sobrien 7690075Sobrien ret = ioctl(fd, TIOCGSIZE, &ts); 7790075Sobrien if(ret == 0) { 7890075Sobrien wp->ws_row = ts.ts_lines; 7990075Sobrien wp->ws_col = ts.ts_cols; 8090075Sobrien } 8190075Sobrien } 82117395Skan#elif defined(HAVE__SCRSIZE) 83169689Skan { 84169689Skan int dst[2]; 85169689Skan 86117395Skan _scrsize(dst); 87132718Skan wp->ws_row = dst[1]; 88169689Skan wp->ws_col = dst[0]; 89169689Skan ret = 0; 90132718Skan } 91132718Skan#endif 92132718Skan if (ret != 0) { 9390075Sobrien char *s; 9490075Sobrien if((s = getenv("COLUMNS"))) 95132718Skan wp->ws_col = atoi(s); 9690075Sobrien if((s = getenv("LINES"))) 97169689Skan wp->ws_row = atoi(s); 98169689Skan if(wp->ws_col > 0 && wp->ws_row > 0) 99169689Skan ret = 0; 100169689Skan } 10190075Sobrien return ret; 102} 103