blob: f93a506a2f04a0a1bdbe167c2cfaef669ad6acde [file] [log] [blame]
Denis Vlasenko9a3b7b12007-08-03 08:48:44 +00001/* vi: set sw=4 ts=4: */
Denis Vlasenkoc01af952007-08-02 22:23:47 +00002/*
3 * Replacement for "stty size", which is awkward for shell script use.
4 * - Allows to request width, height, or both, in any order.
Denis Vlasenko9a3b7b12007-08-03 08:48:44 +00005 * - Does not complain on error, but returns width 80, height 24.
Denis Vlasenkoc01af952007-08-02 22:23:47 +00006 * - Size: less than 200 bytes
Denis Vlasenko9a3b7b12007-08-03 08:48:44 +00007 *
8 * Copyright (C) 2007 by Denys Vlasenko <vda.linux@googlemail.com>
9 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoc01af952007-08-02 22:23:47 +000011 */
12#include "libbb.h"
13
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000014int ttysize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010015int ttysize_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc01af952007-08-02 22:23:47 +000016{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000017 unsigned w, h;
Denis Vlasenkoc01af952007-08-02 22:23:47 +000018 struct winsize wsz;
Denis Vlasenko319f8eb2007-08-13 11:09:30 +000019
Denis Vlasenkoc01af952007-08-02 22:23:47 +000020 w = 80;
21 h = 24;
22 if (!ioctl(0, TIOCGWINSZ, &wsz)) {
23 w = wsz.ws_col;
24 h = wsz.ws_row;
25 }
26
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010027 if (!argv[1]) {
Denis Vlasenkoc01af952007-08-02 22:23:47 +000028 printf("%u %u", w, h);
29 } else {
30 const char *fmt, *arg;
31
32 fmt = "%u %u" + 3; /* "%u" */
33 while ((arg = *++argv) != NULL) {
34 char c = arg[0];
35 if (c == 'w')
36 printf(fmt, w);
37 if (c == 'h')
38 printf(fmt, h);
39 fmt = "%u %u" + 2; /* " %u" */
40 }
41 }
Denis Vlasenko4daad902007-09-27 10:20:47 +000042 bb_putchar('\n');
Denis Vlasenkoc01af952007-08-02 22:23:47 +000043 return 0;
44}