Denis Vlasenko | 9a3b7b1 | 2007-08-03 08:48:44 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denis Vlasenko | c01af95 | 2007-08-02 22:23:47 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Replacement for "stty size", which is awkward for shell script use. |
| 4 | * - Allows to request width, height, or both, in any order. |
Denis Vlasenko | 9a3b7b1 | 2007-08-03 08:48:44 +0000 | [diff] [blame] | 5 | * - Does not complain on error, but returns width 80, height 24. |
Denis Vlasenko | c01af95 | 2007-08-02 22:23:47 +0000 | [diff] [blame] | 6 | * - Size: less than 200 bytes |
Denis Vlasenko | 9a3b7b1 | 2007-08-03 08:48:44 +0000 | [diff] [blame] | 7 | * |
| 8 | * Copyright (C) 2007 by Denys Vlasenko <vda.linux@googlemail.com> |
| 9 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | c01af95 | 2007-08-02 22:23:47 +0000 | [diff] [blame] | 11 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 12 | //config:config TTYSIZE |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 13 | //config: bool "ttysize (432 bytes)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 14 | //config: default y |
| 15 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 16 | //config: A replacement for "stty size". Unlike stty, can report only width, |
| 17 | //config: only height, or both, in any order. It also does not complain on |
| 18 | //config: error, but returns default 80x24. |
| 19 | //config: Usage in shell scripts: width=`ttysize w`. |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 20 | |
Denys Vlasenko | 39194f0 | 2017-08-03 19:00:01 +0200 | [diff] [blame] | 21 | //applet:IF_TTYSIZE(APPLET_NOFORK(ttysize, ttysize, BB_DIR_USR_BIN, BB_SUID_DROP, ttysize)) |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 22 | |
| 23 | //kbuild:lib-$(CONFIG_TTYSIZE) += ttysize.o |
| 24 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 25 | //usage:#define ttysize_trivial_usage |
| 26 | //usage: "[w] [h]" |
| 27 | //usage:#define ttysize_full_usage "\n\n" |
Denys Vlasenko | 68e9805 | 2017-07-16 20:36:48 +0200 | [diff] [blame] | 28 | //usage: "Print dimensions of stdin tty, or 80x24" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 29 | |
Denis Vlasenko | c01af95 | 2007-08-02 22:23:47 +0000 | [diff] [blame] | 30 | #include "libbb.h" |
| 31 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 32 | int ttysize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 33 | int ttysize_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | c01af95 | 2007-08-02 22:23:47 +0000 | [diff] [blame] | 34 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 35 | unsigned w, h; |
Denis Vlasenko | c01af95 | 2007-08-02 22:23:47 +0000 | [diff] [blame] | 36 | struct winsize wsz; |
Denis Vlasenko | 319f8eb | 2007-08-13 11:09:30 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | c01af95 | 2007-08-02 22:23:47 +0000 | [diff] [blame] | 38 | w = 80; |
| 39 | h = 24; |
Denys Vlasenko | 68e9805 | 2017-07-16 20:36:48 +0200 | [diff] [blame] | 40 | if (ioctl(0, TIOCGWINSZ, &wsz) == 0 |
| 41 | || ioctl(1, TIOCGWINSZ, &wsz) == 0 |
| 42 | || ioctl(2, TIOCGWINSZ, &wsz) == 0 |
| 43 | ) { |
Denis Vlasenko | c01af95 | 2007-08-02 22:23:47 +0000 | [diff] [blame] | 44 | w = wsz.ws_col; |
| 45 | h = wsz.ws_row; |
| 46 | } |
| 47 | |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 48 | if (!argv[1]) { |
Denis Vlasenko | c01af95 | 2007-08-02 22:23:47 +0000 | [diff] [blame] | 49 | printf("%u %u", w, h); |
| 50 | } else { |
| 51 | const char *fmt, *arg; |
| 52 | |
| 53 | fmt = "%u %u" + 3; /* "%u" */ |
| 54 | while ((arg = *++argv) != NULL) { |
| 55 | char c = arg[0]; |
| 56 | if (c == 'w') |
| 57 | printf(fmt, w); |
| 58 | if (c == 'h') |
| 59 | printf(fmt, h); |
| 60 | fmt = "%u %u" + 2; /* " %u" */ |
| 61 | } |
| 62 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 63 | bb_putchar('\n'); |
Denis Vlasenko | c01af95 | 2007-08-02 22:23:47 +0000 | [diff] [blame] | 64 | return 0; |
| 65 | } |