blob: 941f04c3a29b033c0034aeb686cdc2f34f1bf089 [file] [log] [blame]
Eric Andersen8efe9672003-09-15 08:33:45 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Determine the width and height of the terminal.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen8efe9672003-09-15 08:33:45 +00006 *
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen8efe9672003-09-15 08:33:45 +00008 */
9
10#include <stdio.h>
11#include <errno.h>
12#include <fcntl.h>
13#include <unistd.h>
14#include <unistd.h>
15#include <termios.h>
16#include <sys/ioctl.h>
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000017#include "libbb.h"
Eric Andersen8efe9672003-09-15 08:33:45 +000018
19/* It is perfectly ok to pass in a NULL for either width or for
Rob Landleye0537f62005-09-01 02:57:45 +000020 * height, in which case that value will not be set. */
21int get_terminal_width_height(int fd, int *width, int *height)
Eric Andersen8efe9672003-09-15 08:33:45 +000022{
23 struct winsize win = { 0, 0, 0, 0 };
Rob Landleye0537f62005-09-01 02:57:45 +000024 int ret = ioctl(fd, TIOCGWINSZ, &win);
25 if (win.ws_row <= 1) win.ws_row = 24;
26 if (win.ws_col <= 1) win.ws_col = 80;
27 if (height) *height = (int) win.ws_row;
28 if (width) *width = (int) win.ws_col;
Rob Landleycc1669b2005-09-05 05:36:15 +000029
30 return ret;
Eric Andersen8efe9672003-09-15 08:33:45 +000031}