aboutsummaryrefslogtreecommitdiff
path: root/miscutils/ttysize.c
diff options
context:
space:
mode:
Diffstat (limited to 'miscutils/ttysize.c')
-rw-r--r--miscutils/ttysize.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/miscutils/ttysize.c b/miscutils/ttysize.c
new file mode 100644
index 000000000..c1b702c2c
--- /dev/null
+++ b/miscutils/ttysize.c
@@ -0,0 +1,39 @@
1/*
2 * Replacement for "stty size", which is awkward for shell script use.
3 * - Allows to request width, height, or both, in any order.
4 * - Does not complain on error, but returns default 80x24.
5 * - Size: less than 200 bytes
6 */
7#include "libbb.h"
8
9int ttysize_main(int argc, char **argv);
10int ttysize_main(int argc, char **argv)
11{
12 unsigned w,h;
13 struct winsize wsz;
14
15 w = 80;
16 h = 24;
17 if (!ioctl(0, TIOCGWINSZ, &wsz)) {
18 w = wsz.ws_col;
19 h = wsz.ws_row;
20 }
21
22 if (argc == 1) {
23 printf("%u %u", w, h);
24 } else {
25 const char *fmt, *arg;
26
27 fmt = "%u %u" + 3; /* "%u" */
28 while ((arg = *++argv) != NULL) {
29 char c = arg[0];
30 if (c == 'w')
31 printf(fmt, w);
32 if (c == 'h')
33 printf(fmt, h);
34 fmt = "%u %u" + 2; /* " %u" */
35 }
36 }
37 putchar('\n');
38 return 0;
39}