Path: blob/main/crypto/heimdal/lib/roken/get_window_size.c
39507 views
/*1* Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include <config.h>3435#include <stdlib.h>36#ifdef HAVE_UNISTD_H37#include <unistd.h>38#endif39#ifdef HAVE_SYS_TYPES_H40#include <sys/types.h>41#endif4243#if 0 /* Where were those needed? /confused */44#ifdef HAVE_SYS_PROC_H45#include <sys/proc.h>46#endif4748#ifdef HAVE_SYS_TTY_H49#include <sys/tty.h>50#endif51#endif5253#ifdef HAVE_TERMIOS_H54#include <termios.h>55#endif5657#include "roken.h"5859ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL60get_window_size(int fd, int *lines, int *columns)61{62char *s;6364#if defined(TIOCGWINSZ)65{66struct winsize ws;67int ret;68ret = ioctl(fd, TIOCGWINSZ, &ws);69if (ret != -1) {70if (lines)71*lines = ws.ws_row;72if (columns)73*columns = ws.ws_col;74return 0;75}76}77#elif defined(TIOCGSIZE)78{79struct ttysize ts;80int ret;81ret = ioctl(fd, TIOCGSIZE, &ts);82if (ret != -1) {83if (lines)84*lines = ts.ws_lines;85if (columns)86*columns = ts.ts_cols;87return 0;88}89}90#elif defined(HAVE__SCRSIZE)91{92int dst[2];9394_scrsize(dst);95if (lines)96*lines = dst[1];97if (columns)98*columns = dst[0];99return 0;100}101#elif defined(_WIN32)102{103intptr_t fh = 0;104CONSOLE_SCREEN_BUFFER_INFO sb_info;105106fh = _get_osfhandle(fd);107if (fh != (intptr_t) INVALID_HANDLE_VALUE &&108GetConsoleScreenBufferInfo((HANDLE) fh, &sb_info)) {109if (lines)110*lines = 1 + sb_info.srWindow.Bottom - sb_info.srWindow.Top;111if (columns)112*columns = 1 + sb_info.srWindow.Right - sb_info.srWindow.Left;113114return 0;115}116}117#endif118if (columns) {119if ((s = getenv("COLUMNS")))120*columns = atoi(s);121else122return -1;123}124if (lines) {125if ((s = getenv("LINES")))126*lines = atoi(s);127else128return -1;129}130return 0;131}132133134