1
2
3
4
5
6
7
8
9
10#include "libbb.h"
11
12#define ESC "\033"
13
14#define old_termios_p ((struct termios*)&bb_common_bufsiz1)
15
16static void
17onintr(int sig UNUSED_PARAM)
18{
19 tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
20 _exit(EXIT_FAILURE);
21}
22
23int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
24int resize_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
25{
26 struct termios new;
27 struct winsize w = { 0, 0, 0, 0 };
28 int ret;
29
30
31
32
33
34
35
36 tcgetattr(STDERR_FILENO, old_termios_p);
37 memcpy(&new, old_termios_p, sizeof(new));
38 new.c_cflag |= (CLOCAL | CREAD);
39 new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
40 bb_signals(0
41 + (1 << SIGINT)
42 + (1 << SIGQUIT)
43 + (1 << SIGTERM)
44 + (1 << SIGALRM)
45 , onintr);
46 tcsetattr(STDERR_FILENO, TCSANOW, &new);
47
48
49
50
51
52
53
54 fprintf(stderr, ESC"7" ESC"[r" ESC"[999;999H" ESC"[6n");
55 alarm(3);
56 scanf(ESC"[%hu;%huR", &w.ws_row, &w.ws_col);
57 fprintf(stderr, ESC"8");
58
59
60
61
62 ret = ioctl(STDERR_FILENO, TIOCSWINSZ, &w);
63
64 tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
65
66 if (ENABLE_FEATURE_RESIZE_PRINT)
67 printf("COLUMNS=%d;LINES=%d;export COLUMNS LINES;\n",
68 w.ws_col, w.ws_row);
69
70 return ret;
71}
72