Terminal control/Dimensions: Difference between revisions
Content added Content deleted
(Add C using TIOCGWINSZ.) |
|||
Line 2: | Line 2: | ||
Determine the height and width of the terminal, and store this information into variables for subsequent use. |
Determine the height and width of the terminal, and store this information into variables for subsequent use. |
||
[[Terminal Control::task| ]] |
[[Terminal Control::task| ]] |
||
=={{header|C}}== |
|||
C provides no standard way to find the size of a terminal. |
|||
Some devices can do NAWS (Negotiate About Window Size). A terminal emulator like xterm(1) should set the size. A network server like sshd(1) should copy the size from its client. Other devices, such as plain serial ports, might not know the window size. |
|||
[[BSD]] systems (and some other [[Unix]] clones) have TIOCGWINSZ. This ioctl(2) call gets the "window size" of a tty(4) device. |
|||
{{libheader|BSD libc}} |
|||
{{works with|OpenBSD|4.9}} |
|||
<lang c>#include <sys/ioctl.h> /* ioctl, TIOCGWINSZ */ |
|||
#include <err.h> /* err */ |
|||
#include <fcntl.h> /* open */ |
|||
#include <stdio.h> /* printf */ |
|||
#include <unistd.h> /* close */ |
|||
int |
|||
main() |
|||
{ |
|||
struct winsize ws; |
|||
int fd; |
|||
/* Open the controlling terminal. */ |
|||
fd = open("/dev/tty", O_RDWR); |
|||
if (fd < 0) |
|||
err(1, "/dev/tty"); |
|||
/* Get window size of terminal. */ |
|||
if (ioctl(fd, TIOCGWINSZ, &ws) < 0) |
|||
err(1, "/dev/tty"); |
|||
printf("%d rows by %d columns\n", ws.ws_row, ws.ws_col); |
|||
printf("(%d by %d pixels)\n", ws.ws_xpixel, ws.ws_ypixel); |
|||
close(fd); |
|||
return 0; |
|||
}</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |