aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2003-09-15 08:33:45 +0000
committerEric Andersen <andersen@codepoet.org>2003-09-15 08:33:45 +0000
commit8efe967018dd79aacfe3ca1e2583f115d744e466 (patch)
tree7590704be6cf03c87242043f5909e12eb4150356
parentc4f72d1426e03f046ac54a00d733a4a52c3a5d4b (diff)
downloadbusybox-w32-8efe967018dd79aacfe3ca1e2583f115d744e466.tar.gz
busybox-w32-8efe967018dd79aacfe3ca1e2583f115d744e466.tar.bz2
busybox-w32-8efe967018dd79aacfe3ca1e2583f115d744e466.zip
Be entirely consistant when using ioctl(0, TIOCGWINSZ, &winsize)
to ensure proper fallback behavior on, i.e. serial consoles. -Erik
-rw-r--r--coreutils/ls.c15
-rw-r--r--editors/vi.c36
-rw-r--r--include/libbb.h7
-rw-r--r--libbb/get_terminal_width_height.c66
-rw-r--r--networking/telnet.c10
-rw-r--r--networking/wget.c11
-rw-r--r--procps/ps.c14
-rw-r--r--procps/top.c21
-rw-r--r--shell/cmdedit.c8
-rw-r--r--util-linux/more.c17
10 files changed, 110 insertions, 95 deletions
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;
203#endif 203#endif
204 204
205#ifdef CONFIG_FEATURE_AUTOWIDTH 205#ifdef CONFIG_FEATURE_AUTOWIDTH
206static unsigned short terminal_width = TERMINAL_WIDTH; 206static int terminal_width = TERMINAL_WIDTH;
207static unsigned short tabstops = COLUMN_GAP; 207static unsigned short tabstops = COLUMN_GAP;
208#else 208#else
209#define tabstops COLUMN_GAP 209#define tabstops COLUMN_GAP
@@ -915,10 +915,6 @@ extern int ls_main(int argc, char **argv)
915 is_flask_enabled_flag = is_flask_enabled(); 915 is_flask_enabled_flag = is_flask_enabled();
916#endif 916#endif
917 917
918#ifdef CONFIG_FEATURE_AUTOWIDTH
919 struct winsize win = { 0, 0, 0, 0 };
920#endif
921
922 all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO 918 all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO
923#ifdef CONFIG_FEATURE_LS_TIMESTAMPS 919#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
924 | TIME_MOD 920 | TIME_MOD
@@ -927,11 +923,10 @@ extern int ls_main(int argc, char **argv)
927 | SORT_NAME | SORT_ORDER_FORWARD 923 | SORT_NAME | SORT_ORDER_FORWARD
928#endif 924#endif
929 ; 925 ;
930#ifdef CONFIG_FEATURE_AUTOWIDTH 926 /* Obtain the terminal width. */
931 ioctl(fileno(stdout), TIOCGWINSZ, &win); 927 get_terminal_width_height(0, &terminal_width, NULL);
932 if (win.ws_col > 0) 928 /* Go one less... */
933 terminal_width = win.ws_col - 1; 929 terminal_width--;
934#endif
935 nfiles = 0; 930 nfiles = 0;
936 931
937#ifdef CONFIG_FEATURE_LS_COLOR 932#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 @@
19 */ 19 */
20 20
21static const char vi_Version[] = 21static const char vi_Version[] =
22 "$Id: vi.c,v 1.28 2003/03/19 09:11:45 mjn3 Exp $"; 22 "$Id: vi.c,v 1.29 2003/09/15 08:33:36 andersen Exp $";
23 23
24/* 24/*
25 * To compile for standalone use: 25 * To compile for standalone use:
@@ -197,9 +197,6 @@ static jmp_buf restart; // catch_sig()
197#if defined(CONFIG_FEATURE_VI_USE_SIGNALS) || defined(CONFIG_FEATURE_VI_CRASHME) 197#if defined(CONFIG_FEATURE_VI_USE_SIGNALS) || defined(CONFIG_FEATURE_VI_CRASHME)
198static int my_pid; 198static int my_pid;
199#endif 199#endif
200#ifdef CONFIG_FEATURE_VI_WIN_RESIZE
201static struct winsize winsize; // remember the window size
202#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */
203#ifdef CONFIG_FEATURE_VI_DOT_CMD 200#ifdef CONFIG_FEATURE_VI_DOT_CMD
204static int adding2q; // are we currently adding user input to q 201static int adding2q; // are we currently adding user input to q
205static Byte *last_modifying_cmd; // last modifying cmd for "." 202static Byte *last_modifying_cmd; // last modifying cmd for "."
@@ -412,6 +409,14 @@ extern int vi_main(int argc, char **argv)
412 return (0); 409 return (0);
413} 410}
414 411
412#ifdef CONFIG_FEATURE_VI_WIN_RESIZE
413//----- See what the window size currently is --------------------
414static inline void window_size_get(int fd)
415{
416 get_terminal_width_height(fd, &columns, &rows);
417}
418#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */
419
415static void edit_file(Byte * fn) 420static void edit_file(Byte * fn)
416{ 421{
417 Byte c; 422 Byte c;
@@ -2122,29 +2127,6 @@ static void cookmode(void)
2122 tcsetattr(0, TCSANOW, &term_orig); 2127 tcsetattr(0, TCSANOW, &term_orig);
2123} 2128}
2124 2129
2125#ifdef CONFIG_FEATURE_VI_WIN_RESIZE
2126//----- See what the window size currently is --------------------
2127static void window_size_get(int sig)
2128{
2129 int i;
2130
2131 i = ioctl(0, TIOCGWINSZ, &winsize);
2132 if (i != 0) {
2133 // force 24x80
2134 winsize.ws_row = 24;
2135 winsize.ws_col = 80;
2136 }
2137 if (winsize.ws_row <= 1) {
2138 winsize.ws_row = 24;
2139 }
2140 if (winsize.ws_col <= 1) {
2141 winsize.ws_col = 80;
2142 }
2143 rows = (int) winsize.ws_row;
2144 columns = (int) winsize.ws_col;
2145}
2146#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */
2147
2148//----- Come here when we get a window resize signal --------- 2130//----- Come here when we get a window resize signal ---------
2149#ifdef CONFIG_FEATURE_VI_USE_SIGNALS 2131#ifdef CONFIG_FEATURE_VI_USE_SIGNALS
2150static void winch_sig(int sig) 2132static 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 {
462} llist_t; 462} llist_t;
463extern llist_t *llist_add_to(llist_t *old_head, char *new_item); 463extern llist_t *llist_add_to(llist_t *old_head, char *new_item);
464 464
465void print_login_issue(const char *issue_file, const char *tty); 465extern void print_login_issue(const char *issue_file, const char *tty);
466void print_login_prompt(void); 466extern void print_login_prompt(void);
467 467
468void vfork_daemon_rexec(int argc, char **argv, char *foreground_opt); 468extern void vfork_daemon_rexec(int argc, char **argv, char *foreground_opt);
469extern void get_terminal_width_height(int fd, int *width, int *height);
469 470
470#endif /* __LIBCONFIG_H__ */ 471#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 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Determine the width and height of the terminal.
4 *
5 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <stdio.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <unistd.h>
26#include <unistd.h>
27#include <termios.h>
28#include <sys/ioctl.h>
29#include "busybox.h"
30
31/* It is perfectly ok to pass in a NULL for either width or for
32 * height, in which case that value will not be set. It is also
33 * perfectly ok to have CONFIG_FEATURE_AUTOWIDTH disabled, in
34 * which case you will always get 80x24 */
35void get_terminal_width_height(int fd, int *width, int *height)
36{
37 struct winsize win = { 0, 0, 0, 0 };
38#ifdef CONFIG_FEATURE_AUTOWIDTH
39 if (ioctl(0, TIOCGWINSZ, &win) != 0) {
40 win.ws_row = 24;
41 win.ws_col = 80;
42 }
43#endif
44 if (win.ws_row <= 1) {
45 win.ws_row = 24;
46 }
47 if (win.ws_col <= 1) {
48 win.ws_col = 80;
49 }
50 if (height) {
51 *height = (int) win.ws_row;
52 }
53 if (width) {
54 *width = (int) win.ws_col;
55 }
56}
57
58/* END CODE */
59/*
60Local Variables:
61c-file-style: "linux"
62c-basic-offset: 4
63tab-width: 4
64End:
65*/
66
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 @@
44#include <netinet/in.h> 44#include <netinet/in.h>
45#include "busybox.h" 45#include "busybox.h"
46 46
47#ifdef CONFIG_FEATURE_AUTOWIDTH
48# include <sys/ioctl.h>
49#endif
50
51#if 0 47#if 0
52static const int DOTRACE = 1; 48static const int DOTRACE = 1;
53#endif 49#endif
@@ -585,11 +581,7 @@ extern int telnet_main(int argc, char** argv)
585#endif 581#endif
586 582
587#ifdef CONFIG_FEATURE_AUTOWIDTH 583#ifdef CONFIG_FEATURE_AUTOWIDTH
588 struct winsize winp; 584 get_terminal_width_height(0, &win_width, &win_height);
589 if( ioctl(0, TIOCGWINSZ, &winp) == 0 ) {
590 win_width = winp.ws_col;
591 win_height = winp.ws_row;
592 }
593#endif 585#endif
594 586
595#ifdef CONFIG_FEATURE_TELNET_TTYPE 587#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)
679static int 679static int
680getttywidth(void) 680getttywidth(void)
681{ 681{
682 struct winsize winsize; 682 int width=0;
683 683 get_terminal_width_height(0, &width, NULL);
684 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) 684 return (width);
685 return (winsize.ws_col ? winsize.ws_col : 80);
686 else
687 return (80);
688} 685}
689 686
690static void 687static void
@@ -841,7 +838,7 @@ progressmeter(int flag)
841 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 838 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
842 * SUCH DAMAGE. 839 * SUCH DAMAGE.
843 * 840 *
844 * $Id: wget.c,v 1.59 2003/09/11 08:25:11 andersen Exp $ 841 * $Id: wget.c,v 1.60 2003/09/15 08:33:37 andersen Exp $
845 */ 842 */
846 843
847 844
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)
44{ 44{
45 procps_status_t * p; 45 procps_status_t * p;
46 int i, len; 46 int i, len;
47#ifdef CONFIG_FEATURE_AUTOWIDTH
48 struct winsize win = { 0, 0, 0, 0 };
49 int terminal_width = TERMINAL_WIDTH; 47 int terminal_width = TERMINAL_WIDTH;
50#else
51#define terminal_width TERMINAL_WIDTH
52#endif
53 48
54#ifdef CONFIG_SELINUX 49#ifdef CONFIG_SELINUX
55 int use_selinux = 0; 50 int use_selinux = 0;
@@ -58,12 +53,9 @@ extern int ps_main(int argc, char **argv)
58 use_selinux = 1; 53 use_selinux = 1;
59#endif 54#endif
60 55
61 56 get_terminal_width_height(0, &terminal_width, NULL);
62#ifdef CONFIG_FEATURE_AUTOWIDTH 57 /* Go one less... */
63 ioctl(fileno(stdout), TIOCGWINSZ, &win); 58 terminal_width--;
64 if (win.ws_col > 0)
65 terminal_width = win.ws_col - 1;
66#endif
67 59
68#ifdef CONFIG_SELINUX 60#ifdef CONFIG_SELINUX
69 if(use_selinux) 61 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)
373 sprintf(rss_str_buf, "%6ldM", s->rss/1024); 373 sprintf(rss_str_buf, "%6ldM", s->rss/1024);
374 else 374 else
375 sprintf(rss_str_buf, "%7ld", s->rss); 375 sprintf(rss_str_buf, "%7ld", s->rss);
376 printf(
376#ifdef FEATURE_CPU_USAGE_PERCENTAGE 377#ifdef FEATURE_CPU_USAGE_PERCENTAGE
377 printf("%5d %-8s %s %s %5d %2d.%d %2u.%u ", 378 "%5d %-8s %s %s %5d %2d.%d %2u.%u ",
378#else 379#else
379 printf("%5d %-8s %s %s %5d %2u.%u ", 380 "%5d %-8s %s %s %5d %2u.%u ",
380#endif 381#endif
381 s->pid, s->user, s->state, rss_str_buf, s->ppid, 382 s->pid, s->user, s->state, rss_str_buf, s->ppid,
382#ifdef FEATURE_CPU_USAGE_PERCENTAGE 383#ifdef FEATURE_CPU_USAGE_PERCENTAGE
@@ -432,9 +433,6 @@ int top_main(int argc, char **argv)
432 fd_set readfds; 433 fd_set readfds;
433 unsigned char c; 434 unsigned char c;
434 struct sigaction sa; 435 struct sigaction sa;
435#if defined CONFIG_FEATURE_AUTOWIDTH
436 struct winsize win = { 0, 0, 0, 0 };
437#endif
438#endif /* CONFIG_FEATURE_USE_TERMIOS */ 436#endif /* CONFIG_FEATURE_USE_TERMIOS */
439 437
440 /* Default update rate is 5 seconds */ 438 /* Default update rate is 5 seconds */
@@ -478,17 +476,16 @@ int top_main(int argc, char **argv)
478 sigaction (SIGINT, &sa, (struct sigaction *) 0); 476 sigaction (SIGINT, &sa, (struct sigaction *) 0);
479 tcsetattr(0, TCSANOW, (void *) &new_settings); 477 tcsetattr(0, TCSANOW, (void *) &new_settings);
480 atexit(reset_term); 478 atexit(reset_term);
481#if defined CONFIG_FEATURE_AUTOWIDTH 479
482 ioctl(0, TIOCGWINSZ, &win); 480 get_terminal_width_height(0, &col, &lines);
483 if (win.ws_row > 4) { 481 if (lines > 4) {
484 lines = win.ws_row - 5; 482 lines -= 5;
485#ifdef FEATURE_CPU_USAGE_PERCENTAGE 483#ifdef FEATURE_CPU_USAGE_PERCENTAGE
486 col = win.ws_col - 80 + 35 - 6; 484 col = col - 80 + 35 - 6;
487#else 485#else
488 col = win.ws_col - 80 + 35; 486 col = col - 80 + 35;
489#endif 487#endif
490 } 488 }
491#endif
492#endif /* CONFIG_FEATURE_USE_TERMIOS */ 489#endif /* CONFIG_FEATURE_USE_TERMIOS */
493#ifdef FEATURE_CPU_USAGE_PERCENTAGE 490#ifdef FEATURE_CPU_USAGE_PERCENTAGE
494 sort_function[0] = pcpu_sort; 491 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);
167 167
168static void win_changed(int nsig) 168static void win_changed(int nsig)
169{ 169{
170 struct winsize win = { 0, 0, 0, 0 };
171 static sighandler_t previous_SIGWINCH_handler; /* for reset */ 170 static sighandler_t previous_SIGWINCH_handler; /* for reset */
172 171
173 /* emulate || signal call */ 172 /* emulate || signal call */
174 if (nsig == -SIGWINCH || nsig == SIGWINCH) { 173 if (nsig == -SIGWINCH || nsig == SIGWINCH) {
175 ioctl(0, TIOCGWINSZ, &win); 174 int width = 0;
176 if (win.ws_col > 0) { 175 get_terminal_width_height(0, &width, NULL);
177 cmdedit_setwidth(win.ws_col, nsig == SIGWINCH); 176 cmdedit_setwidth(width, nsig == SIGWINCH);
178 }
179 } 177 }
180 /* Unix not all standart in recall signal */ 178 /* Unix not all standart in recall signal */
181 179
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)
69 FILE *file; 69 FILE *file;
70 int len, page_height; 70 int len, page_height;
71 71
72#if defined CONFIG_FEATURE_AUTOWIDTH && defined CONFIG_FEATURE_USE_TERMIOS
73 struct winsize win = { 0, 0, 0, 0 };
74#endif
75
76 argc--; 72 argc--;
77 argv++; 73 argv++;
78 74
@@ -115,13 +111,12 @@ extern int more_main(int argc, char **argv)
115 if(please_display_more_prompt>0) 111 if(please_display_more_prompt>0)
116 please_display_more_prompt = 0; 112 please_display_more_prompt = 0;
117 113
118#if defined CONFIG_FEATURE_AUTOWIDTH && defined CONFIG_FEATURE_USE_TERMIOS 114 get_terminal_width_height(0, &terminal_width, &terminal_height);
119 ioctl(fileno(stdout), TIOCGWINSZ, &win); 115 if (terminal_height > 4)
120 if (win.ws_row > 4) 116 terminal_height -= 2;
121 terminal_height = win.ws_row - 2; 117 if (terminal_width > 0)
122 if (win.ws_col > 0) 118 terminal_width -= 1;
123 terminal_width = win.ws_col - 1; 119
124#endif
125 len=0; 120 len=0;
126 lines = 0; 121 lines = 0;
127 page_height = terminal_height; 122 page_height = terminal_height;