[git commit] libbb: factor out code which queries screen width

Denys Vlasenko vda.linux at googlemail.com
Thu Oct 22 23:44:22 UTC 2015


commit: http://git.busybox.net/busybox/commit/?id=641caaec3d495f3a92f652f12ab70b02ba9312ac
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

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 at googlemail.com>
---
 coreutils/ls.c    |    2 +-
 coreutils/stty.c  |    2 +-
 include/libbb.h   |    1 +
 libbb/appletlib.c |    2 +-
 libbb/progress.c  |    9 +--------
 libbb/xfuncs.c    |    6 ++++++
 procps/ps.c       |    4 ++--
 procps/pstree.c   |    2 +-
 procps/watch.c    |    2 +-
 9 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/coreutils/ls.c b/coreutils/ls.c
index 14c8bea..c484988 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -1105,7 +1105,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
 
 #if ENABLE_FEATURE_AUTOWIDTH
 	/* obtain the terminal width */
-	get_terminal_width_height(STDIN_FILENO, &G_terminal_width, NULL);
+	G_terminal_width = get_terminal_width(STDIN_FILENO);
 	/* go one less... */
 	G_terminal_width--;
 #endif
diff --git a/coreutils/stty.c b/coreutils/stty.c
index 378a848..b63b0b9 100644
--- a/coreutils/stty.c
+++ b/coreutils/stty.c
@@ -1403,7 +1403,7 @@ int stty_main(int argc UNUSED_PARAM, char **argv)
 		perror_on_device_and_die("%s");
 
 	if (stty_state & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) {
-		get_terminal_width_height(STDOUT_FILENO, &G.max_col, NULL);
+		G.max_col = get_terminal_width(STDOUT_FILENO);
 		output_func(&mode, display_all);
 		return EXIT_SUCCESS;
 	}
diff --git a/include/libbb.h b/include/libbb.h
index 28f5722..82484f9 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1399,6 +1399,7 @@ extern void print_login_prompt(void) FAST_FUNC;
 char *xmalloc_ttyname(int fd) FAST_FUNC RETURNS_MALLOC;
 /* NB: typically you want to pass fd 0, not 1. Think 'applet | grep something' */
 int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FUNC;
+int get_terminal_width(int fd) FAST_FUNC;
 
 int tcsetattr_stdin_TCSANOW(const struct termios *tp) FAST_FUNC;
 
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 0f83eda..58bb2f1 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -623,7 +623,7 @@ static int busybox_main(char **argv)
 		output_width = 80;
 		if (ENABLE_FEATURE_AUTOWIDTH) {
 			/* Obtain the terminal width */
-			get_terminal_width_height(0, &output_width, NULL);
+			output_width = get_terminal_width(2);
 		}
 
 		dup2(1, 2);
diff --git a/libbb/progress.c b/libbb/progress.c
index 372feb0..6154dca 100644
--- a/libbb/progress.c
+++ b/libbb/progress.c
@@ -45,13 +45,6 @@ enum {
 	STALLTIME = 5
 };
 
-static unsigned int get_tty2_width(void)
-{
-	unsigned width;
-	get_terminal_width_height(2, &width, NULL);
-	return width;
-}
-
 void FAST_FUNC bb_progress_init(bb_progress_t *p, const char *curfile)
 {
 #if ENABLE_UNICODE_SUPPORT
@@ -148,7 +141,7 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p,
 		unsigned ratio = 100 * beg_and_transferred / totalsize;
 		fprintf(stderr, "%4u%%", ratio);
 
-		barlength = get_tty2_width() - 49;
+		barlength = get_terminal_width(2) - 49;
 		if (barlength > 0) {
 			/* god bless gcc for variable arrays :) */
 			char buf[barlength + 1];
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 0c99696..206edb4 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
 		*width = wh_helper(win.ws_col, 80, "COLUMNS", &err);
 	return err;
 }
+int FAST_FUNC get_terminal_width(int fd)
+{
+	unsigned width;
+	get_terminal_width_height(fd, &width, NULL);
+	return width;
+}
 
 int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
 {
diff --git a/procps/ps.c b/procps/ps.c
index bde5f94..fbafa68 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -622,7 +622,7 @@ int ps_main(int argc UNUSED_PARAM, char **argv)
 	 * and such large widths */
 	terminal_width = MAX_WIDTH;
 	if (isatty(1)) {
-		get_terminal_width_height(0, &terminal_width, NULL);
+		terminal_width = get_terminal_width(0);
 		if (--terminal_width > MAX_WIDTH)
 			terminal_width = MAX_WIDTH;
 	}
@@ -672,7 +672,7 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 	if (w_count) {
 		terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
 	} else {
-		get_terminal_width_height(0, &terminal_width, NULL);
+		terminal_width = get_terminal_width(0);
 		/* Go one less... */
 		if (--terminal_width > MAX_WIDTH)
 			terminal_width = MAX_WIDTH;
diff --git a/procps/pstree.c b/procps/pstree.c
index ed1a412..c5fb836 100644
--- a/procps/pstree.c
+++ b/procps/pstree.c
@@ -381,7 +381,7 @@ int pstree_main(int argc UNUSED_PARAM, char **argv)
 
 	INIT_G();
 
-	get_terminal_width_height(0, &G.output_width, NULL);
+	G.output_width = get_terminal_width(0);
 
 	opt_complementary = "?1";
 	getopt32(argv, "p");
diff --git a/procps/watch.c b/procps/watch.c
index 0397f21..97aa047 100644
--- a/procps/watch.c
+++ b/procps/watch.c
@@ -72,7 +72,7 @@ int watch_main(int argc UNUSED_PARAM, char **argv)
 
 			// STDERR_FILENO is procps3 compat:
 			// "watch ls 2>/dev/null" does not detect tty size
-			get_terminal_width_height(STDERR_FILENO, &new_width, NULL);
+			new_width = get_terminal_width(STDERR_FILENO);
 			if (new_width != width) {
 				width = new_width;
 				free(header);


More information about the busybox-cvs mailing list