Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Determine the width and height of the terminal. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 8 | */ |
| 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-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 17 | #include "libbb.h" |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 18 | |
| 19 | /* It is perfectly ok to pass in a NULL for either width or for |
Rob Landley | e0537f6 | 2005-09-01 02:57:45 +0000 | [diff] [blame] | 20 | * height, in which case that value will not be set. */ |
| 21 | int get_terminal_width_height(int fd, int *width, int *height) |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 22 | { |
| 23 | struct winsize win = { 0, 0, 0, 0 }; |
Rob Landley | e0537f6 | 2005-09-01 02:57:45 +0000 | [diff] [blame] | 24 | 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 Landley | cc1669b | 2005-09-05 05:36:15 +0000 | [diff] [blame] | 29 | |
| 30 | return ret; |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 31 | } |