diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-23 01:44:22 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-23 01:44:22 +0200 |
commit | 641caaec3d495f3a92f652f12ab70b02ba9312ac (patch) | |
tree | 2a1aeb78c4344d1cd4423cdd96b581a797392b58 | |
parent | a96074874857b31361d02ead97a1152164568918 (diff) | |
download | busybox-w32-641caaec3d495f3a92f652f12ab70b02ba9312ac.tar.gz busybox-w32-641caaec3d495f3a92f652f12ab70b02ba9312ac.tar.bz2 busybox-w32-641caaec3d495f3a92f652f12ab70b02ba9312ac.zip |
libbb: factor out code which queries screen width
function old new delta
get_terminal_width - 17 +17
stty_main 1196 1197 +1
pstree_main 321 319 -2
ls_main 735 731 -4
watch_main 232 225 -7
bb_progress_update 714 706 -8
ps_main 555 543 -12
run_applet_and_exit 708 695 -13
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/6 up/down: 18/-46) Total: -28 byte
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/ls.c | 2 | ||||
-rw-r--r-- | coreutils/stty.c | 2 | ||||
-rw-r--r-- | include/libbb.h | 1 | ||||
-rw-r--r-- | libbb/appletlib.c | 2 | ||||
-rw-r--r-- | libbb/progress.c | 9 | ||||
-rw-r--r-- | libbb/xfuncs.c | 6 | ||||
-rw-r--r-- | procps/ps.c | 4 | ||||
-rw-r--r-- | procps/pstree.c | 2 | ||||
-rw-r--r-- | procps/watch.c | 2 |
9 files changed, 15 insertions, 15 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c index 14c8beaff..c48498858 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c | |||
@@ -1105,7 +1105,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv) | |||
1105 | 1105 | ||
1106 | #if ENABLE_FEATURE_AUTOWIDTH | 1106 | #if ENABLE_FEATURE_AUTOWIDTH |
1107 | /* obtain the terminal width */ | 1107 | /* obtain the terminal width */ |
1108 | get_terminal_width_height(STDIN_FILENO, &G_terminal_width, NULL); | 1108 | G_terminal_width = get_terminal_width(STDIN_FILENO); |
1109 | /* go one less... */ | 1109 | /* go one less... */ |
1110 | G_terminal_width--; | 1110 | G_terminal_width--; |
1111 | #endif | 1111 | #endif |
diff --git a/coreutils/stty.c b/coreutils/stty.c index 378a848e7..b63b0b91a 100644 --- a/coreutils/stty.c +++ b/coreutils/stty.c | |||
@@ -1403,7 +1403,7 @@ int stty_main(int argc UNUSED_PARAM, char **argv) | |||
1403 | perror_on_device_and_die("%s"); | 1403 | perror_on_device_and_die("%s"); |
1404 | 1404 | ||
1405 | if (stty_state & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) { | 1405 | if (stty_state & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) { |
1406 | get_terminal_width_height(STDOUT_FILENO, &G.max_col, NULL); | 1406 | G.max_col = get_terminal_width(STDOUT_FILENO); |
1407 | output_func(&mode, display_all); | 1407 | output_func(&mode, display_all); |
1408 | return EXIT_SUCCESS; | 1408 | return EXIT_SUCCESS; |
1409 | } | 1409 | } |
diff --git a/include/libbb.h b/include/libbb.h index 28f57223d..82484f911 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1399,6 +1399,7 @@ extern void print_login_prompt(void) FAST_FUNC; | |||
1399 | char *xmalloc_ttyname(int fd) FAST_FUNC RETURNS_MALLOC; | 1399 | char *xmalloc_ttyname(int fd) FAST_FUNC RETURNS_MALLOC; |
1400 | /* NB: typically you want to pass fd 0, not 1. Think 'applet | grep something' */ | 1400 | /* NB: typically you want to pass fd 0, not 1. Think 'applet | grep something' */ |
1401 | int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FUNC; | 1401 | int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FUNC; |
1402 | int get_terminal_width(int fd) FAST_FUNC; | ||
1402 | 1403 | ||
1403 | int tcsetattr_stdin_TCSANOW(const struct termios *tp) FAST_FUNC; | 1404 | int tcsetattr_stdin_TCSANOW(const struct termios *tp) FAST_FUNC; |
1404 | 1405 | ||
diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 0f83eda4b..58bb2f1a0 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c | |||
@@ -623,7 +623,7 @@ static int busybox_main(char **argv) | |||
623 | output_width = 80; | 623 | output_width = 80; |
624 | if (ENABLE_FEATURE_AUTOWIDTH) { | 624 | if (ENABLE_FEATURE_AUTOWIDTH) { |
625 | /* Obtain the terminal width */ | 625 | /* Obtain the terminal width */ |
626 | get_terminal_width_height(0, &output_width, NULL); | 626 | output_width = get_terminal_width(2); |
627 | } | 627 | } |
628 | 628 | ||
629 | dup2(1, 2); | 629 | dup2(1, 2); |
diff --git a/libbb/progress.c b/libbb/progress.c index 372feb0c2..6154dca17 100644 --- a/libbb/progress.c +++ b/libbb/progress.c | |||
@@ -45,13 +45,6 @@ enum { | |||
45 | STALLTIME = 5 | 45 | STALLTIME = 5 |
46 | }; | 46 | }; |
47 | 47 | ||
48 | static unsigned int get_tty2_width(void) | ||
49 | { | ||
50 | unsigned width; | ||
51 | get_terminal_width_height(2, &width, NULL); | ||
52 | return width; | ||
53 | } | ||
54 | |||
55 | void FAST_FUNC bb_progress_init(bb_progress_t *p, const char *curfile) | 48 | void FAST_FUNC bb_progress_init(bb_progress_t *p, const char *curfile) |
56 | { | 49 | { |
57 | #if ENABLE_UNICODE_SUPPORT | 50 | #if ENABLE_UNICODE_SUPPORT |
@@ -148,7 +141,7 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p, | |||
148 | unsigned ratio = 100 * beg_and_transferred / totalsize; | 141 | unsigned ratio = 100 * beg_and_transferred / totalsize; |
149 | fprintf(stderr, "%4u%%", ratio); | 142 | fprintf(stderr, "%4u%%", ratio); |
150 | 143 | ||
151 | barlength = get_tty2_width() - 49; | 144 | barlength = get_terminal_width(2) - 49; |
152 | if (barlength > 0) { | 145 | if (barlength > 0) { |
153 | /* god bless gcc for variable arrays :) */ | 146 | /* god bless gcc for variable arrays :) */ |
154 | char buf[barlength + 1]; | 147 | char buf[barlength + 1]; |
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 0c9969640..206edb4a0 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -270,6 +270,12 @@ int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *heigh | |||
270 | *width = wh_helper(win.ws_col, 80, "COLUMNS", &err); | 270 | *width = wh_helper(win.ws_col, 80, "COLUMNS", &err); |
271 | return err; | 271 | return err; |
272 | } | 272 | } |
273 | int FAST_FUNC get_terminal_width(int fd) | ||
274 | { | ||
275 | unsigned width; | ||
276 | get_terminal_width_height(fd, &width, NULL); | ||
277 | return width; | ||
278 | } | ||
273 | 279 | ||
274 | int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp) | 280 | int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp) |
275 | { | 281 | { |
diff --git a/procps/ps.c b/procps/ps.c index bde5f9485..fbafa68a9 100644 --- a/procps/ps.c +++ b/procps/ps.c | |||
@@ -622,7 +622,7 @@ int ps_main(int argc UNUSED_PARAM, char **argv) | |||
622 | * and such large widths */ | 622 | * and such large widths */ |
623 | terminal_width = MAX_WIDTH; | 623 | terminal_width = MAX_WIDTH; |
624 | if (isatty(1)) { | 624 | if (isatty(1)) { |
625 | get_terminal_width_height(0, &terminal_width, NULL); | 625 | terminal_width = get_terminal_width(0); |
626 | if (--terminal_width > MAX_WIDTH) | 626 | if (--terminal_width > MAX_WIDTH) |
627 | terminal_width = MAX_WIDTH; | 627 | terminal_width = MAX_WIDTH; |
628 | } | 628 | } |
@@ -672,7 +672,7 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | |||
672 | if (w_count) { | 672 | if (w_count) { |
673 | terminal_width = (w_count == 1) ? 132 : MAX_WIDTH; | 673 | terminal_width = (w_count == 1) ? 132 : MAX_WIDTH; |
674 | } else { | 674 | } else { |
675 | get_terminal_width_height(0, &terminal_width, NULL); | 675 | terminal_width = get_terminal_width(0); |
676 | /* Go one less... */ | 676 | /* Go one less... */ |
677 | if (--terminal_width > MAX_WIDTH) | 677 | if (--terminal_width > MAX_WIDTH) |
678 | terminal_width = MAX_WIDTH; | 678 | terminal_width = MAX_WIDTH; |
diff --git a/procps/pstree.c b/procps/pstree.c index ed1a41289..c5fb83688 100644 --- a/procps/pstree.c +++ b/procps/pstree.c | |||
@@ -381,7 +381,7 @@ int pstree_main(int argc UNUSED_PARAM, char **argv) | |||
381 | 381 | ||
382 | INIT_G(); | 382 | INIT_G(); |
383 | 383 | ||
384 | get_terminal_width_height(0, &G.output_width, NULL); | 384 | G.output_width = get_terminal_width(0); |
385 | 385 | ||
386 | opt_complementary = "?1"; | 386 | opt_complementary = "?1"; |
387 | getopt32(argv, "p"); | 387 | getopt32(argv, "p"); |
diff --git a/procps/watch.c b/procps/watch.c index 0397f21bf..97aa04767 100644 --- a/procps/watch.c +++ b/procps/watch.c | |||
@@ -72,7 +72,7 @@ int watch_main(int argc UNUSED_PARAM, char **argv) | |||
72 | 72 | ||
73 | // STDERR_FILENO is procps3 compat: | 73 | // STDERR_FILENO is procps3 compat: |
74 | // "watch ls 2>/dev/null" does not detect tty size | 74 | // "watch ls 2>/dev/null" does not detect tty size |
75 | get_terminal_width_height(STDERR_FILENO, &new_width, NULL); | 75 | new_width = get_terminal_width(STDERR_FILENO); |
76 | if (new_width != width) { | 76 | if (new_width != width) { |
77 | width = new_width; | 77 | width = new_width; |
78 | free(header); | 78 | free(header); |