Be entirely consistant when using ioctl(0, TIOCGWINSZ, &winsize)
authorEric Andersen <andersen@codepoet.org>
Mon, 15 Sep 2003 08:33:45 +0000 (08:33 -0000)
committerEric Andersen <andersen@codepoet.org>
Mon, 15 Sep 2003 08:33:45 +0000 (08:33 -0000)
to ensure proper fallback behavior on, i.e. serial consoles.
 -Erik

coreutils/ls.c
editors/vi.c
include/libbb.h
libbb/get_terminal_width_height.c [new file with mode: 0644]
networking/telnet.c
networking/wget.c
procps/ps.c
procps/top.c
shell/cmdedit.c
util-linux/more.c

index a7f036b61c0b8520d1211f5c94cb377987de8f13..7275292554c842bf106d7d29711aa56cc82be65f 100644 (file)
@@ -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
index 144e9d7601e36f1a79360b8cd5e5804fd5e56a4c..e01ee11659bbc43483d201e576dd3dcfed931302 100644 (file)
@@ -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)
index 4bfcc7a8b1b6b6aec5e69a5f9275f1b5a4314714..a6ccff421777c91fac4eeb6b65435c05c9141bd3 100644 (file)
@@ -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 (file)
index 0000000..69f6a17
--- /dev/null
@@ -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 <andersen@codepoet.org>
+ *
+ * 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 <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <unistd.h>
+#include <termios.h>
+#include <sys/ioctl.h>
+#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:
+*/
+
index 88607f6531e5e6aacf7d91972d39116d3768ea86..ac6ec98ded4bf4b30dcb96f1bfb35d786ddf2eec 100644 (file)
 #include <netinet/in.h>
 #include "busybox.h"
 
-#ifdef CONFIG_FEATURE_AUTOWIDTH
-#   include <sys/ioctl.h>
-#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
index b1cc35e34b28b4ffbc9c5fe7fcff92430d74a98f..597d610972fe2b7054266e1f4b75ef52f5a41265 100644 (file)
@@ -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 $
  */
 
 
index 9dc45d35d45940caa68b1545d08794241923d77c..b9d15b8619131094ce568222505fb4c92a28c382 100644 (file)
@@ -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)
index 2e1bd3286d38d324a874f0c10f668c249809a876..cee1b52c18e492d66725d65ec1042dbbf51323b4 100644 (file)
@@ -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;
index 0ab1958038742d1e9312c41cc26da972def40164..16825089d5e4ef5a4b222bf8a4b862a563498e3c 100644 (file)
@@ -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 */
 
index 1ec3007bfb721a6fad175ada952face9b621e372..f4018f5d188aba1f112c9bf5bad96212e1882978 100644 (file)
@@ -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;