From 8efe967018dd79aacfe3ca1e2583f115d744e466 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Mon, 15 Sep 2003 08:33:45 +0000 Subject: [PATCH] Be entirely consistant when using ioctl(0, TIOCGWINSZ, &winsize) to ensure proper fallback behavior on, i.e. serial consoles. -Erik --- coreutils/ls.c | 15 +++---- editors/vi.c | 36 +++++------------ include/libbb.h | 7 ++-- libbb/get_terminal_width_height.c | 66 +++++++++++++++++++++++++++++++ networking/telnet.c | 10 +---- networking/wget.c | 11 ++---- procps/ps.c | 14 ++----- procps/top.c | 21 +++++----- shell/cmdedit.c | 8 ++-- util-linux/more.c | 17 +++----- 10 files changed, 110 insertions(+), 95 deletions(-) create mode 100644 libbb/get_terminal_width_height.c diff --git a/coreutils/ls.c b/coreutils/ls.c index a7f036b61..727529255 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -203,7 +203,7 @@ static int is_flask_enabled_flag; #endif #ifdef CONFIG_FEATURE_AUTOWIDTH -static unsigned short terminal_width = TERMINAL_WIDTH; +static int terminal_width = TERMINAL_WIDTH; static unsigned short tabstops = COLUMN_GAP; #else #define tabstops COLUMN_GAP @@ -915,10 +915,6 @@ extern int ls_main(int argc, char **argv) is_flask_enabled_flag = is_flask_enabled(); #endif -#ifdef CONFIG_FEATURE_AUTOWIDTH - struct winsize win = { 0, 0, 0, 0 }; -#endif - all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO #ifdef CONFIG_FEATURE_LS_TIMESTAMPS | TIME_MOD @@ -927,11 +923,10 @@ extern int ls_main(int argc, char **argv) | SORT_NAME | SORT_ORDER_FORWARD #endif ; -#ifdef CONFIG_FEATURE_AUTOWIDTH - ioctl(fileno(stdout), TIOCGWINSZ, &win); - if (win.ws_col > 0) - terminal_width = win.ws_col - 1; -#endif + /* Obtain the terminal width. */ + get_terminal_width_height(0, &terminal_width, NULL); + /* Go one less... */ + terminal_width--; nfiles = 0; #ifdef CONFIG_FEATURE_LS_COLOR diff --git a/editors/vi.c b/editors/vi.c index 144e9d760..e01ee1165 100644 --- a/editors/vi.c +++ b/editors/vi.c @@ -19,7 +19,7 @@ */ static const char vi_Version[] = - "$Id: vi.c,v 1.28 2003/03/19 09:11:45 mjn3 Exp $"; + "$Id: vi.c,v 1.29 2003/09/15 08:33:36 andersen Exp $"; /* * To compile for standalone use: @@ -197,9 +197,6 @@ static jmp_buf restart; // catch_sig() #if defined(CONFIG_FEATURE_VI_USE_SIGNALS) || defined(CONFIG_FEATURE_VI_CRASHME) static int my_pid; #endif -#ifdef CONFIG_FEATURE_VI_WIN_RESIZE -static struct winsize winsize; // remember the window size -#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */ #ifdef CONFIG_FEATURE_VI_DOT_CMD static int adding2q; // are we currently adding user input to q static Byte *last_modifying_cmd; // last modifying cmd for "." @@ -412,6 +409,14 @@ extern int vi_main(int argc, char **argv) return (0); } +#ifdef CONFIG_FEATURE_VI_WIN_RESIZE +//----- See what the window size currently is -------------------- +static inline void window_size_get(int fd) +{ + get_terminal_width_height(fd, &columns, &rows); +} +#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */ + static void edit_file(Byte * fn) { Byte c; @@ -2122,29 +2127,6 @@ static void cookmode(void) tcsetattr(0, TCSANOW, &term_orig); } -#ifdef CONFIG_FEATURE_VI_WIN_RESIZE -//----- See what the window size currently is -------------------- -static void window_size_get(int sig) -{ - int i; - - i = ioctl(0, TIOCGWINSZ, &winsize); - if (i != 0) { - // force 24x80 - winsize.ws_row = 24; - winsize.ws_col = 80; - } - if (winsize.ws_row <= 1) { - winsize.ws_row = 24; - } - if (winsize.ws_col <= 1) { - winsize.ws_col = 80; - } - rows = (int) winsize.ws_row; - columns = (int) winsize.ws_col; -} -#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */ - //----- Come here when we get a window resize signal --------- #ifdef CONFIG_FEATURE_VI_USE_SIGNALS static void winch_sig(int sig) diff --git a/include/libbb.h b/include/libbb.h index 4bfcc7a8b..a6ccff421 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -462,9 +462,10 @@ typedef struct llist_s { } llist_t; extern llist_t *llist_add_to(llist_t *old_head, char *new_item); -void print_login_issue(const char *issue_file, const char *tty); -void print_login_prompt(void); +extern void print_login_issue(const char *issue_file, const char *tty); +extern void print_login_prompt(void); -void vfork_daemon_rexec(int argc, char **argv, char *foreground_opt); +extern void vfork_daemon_rexec(int argc, char **argv, char *foreground_opt); +extern void get_terminal_width_height(int fd, int *width, int *height); #endif /* __LIBCONFIG_H__ */ diff --git a/libbb/get_terminal_width_height.c b/libbb/get_terminal_width_height.c new file mode 100644 index 000000000..69f6a17e5 --- /dev/null +++ b/libbb/get_terminal_width_height.c @@ -0,0 +1,66 @@ +/* vi: set sw=4 ts=4: */ +/* + * Determine the width and height of the terminal. + * + * Copyright (C) 1999-2003 by Erik Andersen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include +#include +#include "busybox.h" + +/* It is perfectly ok to pass in a NULL for either width or for + * height, in which case that value will not be set. It is also + * perfectly ok to have CONFIG_FEATURE_AUTOWIDTH disabled, in + * which case you will always get 80x24 */ +void get_terminal_width_height(int fd, int *width, int *height) +{ + struct winsize win = { 0, 0, 0, 0 }; +#ifdef CONFIG_FEATURE_AUTOWIDTH + if (ioctl(0, TIOCGWINSZ, &win) != 0) { + win.ws_row = 24; + win.ws_col = 80; + } +#endif + if (win.ws_row <= 1) { + win.ws_row = 24; + } + if (win.ws_col <= 1) { + win.ws_col = 80; + } + if (height) { + *height = (int) win.ws_row; + } + if (width) { + *width = (int) win.ws_col; + } +} + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ + diff --git a/networking/telnet.c b/networking/telnet.c index 88607f653..ac6ec98de 100644 --- a/networking/telnet.c +++ b/networking/telnet.c @@ -44,10 +44,6 @@ #include #include "busybox.h" -#ifdef CONFIG_FEATURE_AUTOWIDTH -# include -#endif - #if 0 static const int DOTRACE = 1; #endif @@ -585,11 +581,7 @@ extern int telnet_main(int argc, char** argv) #endif #ifdef CONFIG_FEATURE_AUTOWIDTH - struct winsize winp; - if( ioctl(0, TIOCGWINSZ, &winp) == 0 ) { - win_width = winp.ws_col; - win_height = winp.ws_row; - } + get_terminal_width_height(0, &win_width, &win_height); #endif #ifdef CONFIG_FEATURE_TELNET_TTYPE diff --git a/networking/wget.c b/networking/wget.c index b1cc35e34..597d61097 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -679,12 +679,9 @@ static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf) static int getttywidth(void) { - struct winsize winsize; - - if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) - return (winsize.ws_col ? winsize.ws_col : 80); - else - return (80); + int width=0; + get_terminal_width_height(0, &width, NULL); + return (width); } static void @@ -841,7 +838,7 @@ progressmeter(int flag) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: wget.c,v 1.59 2003/09/11 08:25:11 andersen Exp $ + * $Id: wget.c,v 1.60 2003/09/15 08:33:37 andersen Exp $ */ diff --git a/procps/ps.c b/procps/ps.c index 9dc45d35d..b9d15b861 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -44,12 +44,7 @@ extern int ps_main(int argc, char **argv) { procps_status_t * p; int i, len; -#ifdef CONFIG_FEATURE_AUTOWIDTH - struct winsize win = { 0, 0, 0, 0 }; int terminal_width = TERMINAL_WIDTH; -#else -#define terminal_width TERMINAL_WIDTH -#endif #ifdef CONFIG_SELINUX int use_selinux = 0; @@ -58,12 +53,9 @@ extern int ps_main(int argc, char **argv) use_selinux = 1; #endif - -#ifdef CONFIG_FEATURE_AUTOWIDTH - ioctl(fileno(stdout), TIOCGWINSZ, &win); - if (win.ws_col > 0) - terminal_width = win.ws_col - 1; -#endif + get_terminal_width_height(0, &terminal_width, NULL); + /* Go one less... */ + terminal_width--; #ifdef CONFIG_SELINUX if(use_selinux) diff --git a/procps/top.c b/procps/top.c index 2e1bd3286..cee1b52c1 100644 --- a/procps/top.c +++ b/procps/top.c @@ -373,10 +373,11 @@ static void display_status(int count, int col) sprintf(rss_str_buf, "%6ldM", s->rss/1024); else sprintf(rss_str_buf, "%7ld", s->rss); + printf( #ifdef FEATURE_CPU_USAGE_PERCENTAGE - printf("%5d %-8s %s %s %5d %2d.%d %2u.%u ", + "%5d %-8s %s %s %5d %2d.%d %2u.%u ", #else - printf("%5d %-8s %s %s %5d %2u.%u ", + "%5d %-8s %s %s %5d %2u.%u ", #endif s->pid, s->user, s->state, rss_str_buf, s->ppid, #ifdef FEATURE_CPU_USAGE_PERCENTAGE @@ -432,9 +433,6 @@ int top_main(int argc, char **argv) fd_set readfds; unsigned char c; struct sigaction sa; -#if defined CONFIG_FEATURE_AUTOWIDTH - struct winsize win = { 0, 0, 0, 0 }; -#endif #endif /* CONFIG_FEATURE_USE_TERMIOS */ /* Default update rate is 5 seconds */ @@ -478,17 +476,16 @@ int top_main(int argc, char **argv) sigaction (SIGINT, &sa, (struct sigaction *) 0); tcsetattr(0, TCSANOW, (void *) &new_settings); atexit(reset_term); -#if defined CONFIG_FEATURE_AUTOWIDTH - ioctl(0, TIOCGWINSZ, &win); - if (win.ws_row > 4) { - lines = win.ws_row - 5; + + get_terminal_width_height(0, &col, &lines); + if (lines > 4) { + lines -= 5; #ifdef FEATURE_CPU_USAGE_PERCENTAGE - col = win.ws_col - 80 + 35 - 6; + col = col - 80 + 35 - 6; #else - col = win.ws_col - 80 + 35; + col = col - 80 + 35; #endif } -#endif #endif /* CONFIG_FEATURE_USE_TERMIOS */ #ifdef FEATURE_CPU_USAGE_PERCENTAGE sort_function[0] = pcpu_sort; diff --git a/shell/cmdedit.c b/shell/cmdedit.c index 0ab195803..16825089d 100644 --- a/shell/cmdedit.c +++ b/shell/cmdedit.c @@ -167,15 +167,13 @@ static void cmdedit_setwidth(int w, int redraw_flg); static void win_changed(int nsig) { - struct winsize win = { 0, 0, 0, 0 }; static sighandler_t previous_SIGWINCH_handler; /* for reset */ /* emulate || signal call */ if (nsig == -SIGWINCH || nsig == SIGWINCH) { - ioctl(0, TIOCGWINSZ, &win); - if (win.ws_col > 0) { - cmdedit_setwidth(win.ws_col, nsig == SIGWINCH); - } + int width = 0; + get_terminal_width_height(0, &width, NULL); + cmdedit_setwidth(width, nsig == SIGWINCH); } /* Unix not all standart in recall signal */ diff --git a/util-linux/more.c b/util-linux/more.c index 1ec3007bf..f4018f5d1 100644 --- a/util-linux/more.c +++ b/util-linux/more.c @@ -69,10 +69,6 @@ extern int more_main(int argc, char **argv) FILE *file; int len, page_height; -#if defined CONFIG_FEATURE_AUTOWIDTH && defined CONFIG_FEATURE_USE_TERMIOS - struct winsize win = { 0, 0, 0, 0 }; -#endif - argc--; argv++; @@ -115,13 +111,12 @@ extern int more_main(int argc, char **argv) if(please_display_more_prompt>0) please_display_more_prompt = 0; -#if defined CONFIG_FEATURE_AUTOWIDTH && defined CONFIG_FEATURE_USE_TERMIOS - ioctl(fileno(stdout), TIOCGWINSZ, &win); - if (win.ws_row > 4) - terminal_height = win.ws_row - 2; - if (win.ws_col > 0) - terminal_width = win.ws_col - 1; -#endif + get_terminal_width_height(0, &terminal_width, &terminal_height); + if (terminal_height > 4) + terminal_height -= 2; + if (terminal_width > 0) + terminal_width -= 1; + len=0; lines = 0; page_height = terminal_height; -- 2.25.1