aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-10-27 09:03:24 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-10-27 09:03:24 +0000
commit9bc0f50defc28a31a2d214e654ec48e2a1bcc897 (patch)
treecd34a619db35dccb76043c1bf118d297e17ac436
parent8b039ac7df3ae06554165c79429b5d06240080c1 (diff)
downloadbusybox-w32-9bc0f50defc28a31a2d214e654ec48e2a1bcc897.tar.gz
busybox-w32-9bc0f50defc28a31a2d214e654ec48e2a1bcc897.tar.bz2
busybox-w32-9bc0f50defc28a31a2d214e654ec48e2a1bcc897.zip
get_terminal_width_height: do not pass insanely large values
git-svn-id: svn://busybox.net/trunk/busybox@16452 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--coreutils/watch.c1
-rw-r--r--editors/vi.c2
-rw-r--r--libbb/xfuncs.c35
-rw-r--r--networking/telnet.c2
-rw-r--r--networking/wget.c2
-rw-r--r--procps/ps.c14
6 files changed, 33 insertions, 23 deletions
diff --git a/coreutils/watch.c b/coreutils/watch.c
index b188b4176..81856c867 100644
--- a/coreutils/watch.c
+++ b/coreutils/watch.c
@@ -58,7 +58,6 @@ int watch_main(int argc, char **argv)
58 time_t t; 58 time_t t;
59 59
60 get_terminal_width_height(STDOUT_FILENO, &width, 0); 60 get_terminal_width_height(STDOUT_FILENO, &width, 0);
61 if (width < 1) width = 1; // paranoia
62 header = xrealloc(header, width--); 61 header = xrealloc(header, width--);
63 // '%-*s' pads header with spaces to the full width 62 // '%-*s' pads header with spaces to the full width
64 snprintf(header, width, "Every %ds: %-*s", period, width, cmd); 63 snprintf(header, width, "Every %ds: %-*s", period, width, cmd);
diff --git a/editors/vi.c b/editors/vi.c
index 82985ced0..f1c79895e 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -2039,7 +2039,7 @@ static void winch_sig(int sig ATTRIBUTE_UNUSED)
2039{ 2039{
2040 signal(SIGWINCH, winch_sig); 2040 signal(SIGWINCH, winch_sig);
2041 if (ENABLE_FEATURE_VI_WIN_RESIZE) 2041 if (ENABLE_FEATURE_VI_WIN_RESIZE)
2042 get_terminal_width_height(0, &columns, &rows); 2042 get_terminal_width_height(0, &columns, &rows);
2043 new_screen(rows, columns); // get memory for virtual screen 2043 new_screen(rows, columns); // get memory for virtual screen
2044 redraw(TRUE); // re-draw the screen 2044 redraw(TRUE); // re-draw the screen
2045} 2045}
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 1144a67c6..c72265003 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -281,6 +281,8 @@ off_t fdlength(int fd)
281 281
282 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512; 282 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
283 283
284 // FIXME: explain why lseek(SEEK_END) is not used here!
285
284 // If not, do a binary search for the last location we can read. (Some 286 // If not, do a binary search for the last location we can read. (Some
285 // block devices don't do BLKGETSIZE right.) 287 // block devices don't do BLKGETSIZE right.)
286 288
@@ -382,7 +384,8 @@ DIR *xopendir(const char *path)
382// Die with an error message if we can't daemonize. 384// Die with an error message if we can't daemonize.
383void xdaemon(int nochdir, int noclose) 385void xdaemon(int nochdir, int noclose)
384{ 386{
385 if (daemon(nochdir, noclose)) bb_perror_msg_and_die("daemon"); 387 if (daemon(nochdir, noclose))
388 bb_perror_msg_and_die("daemon");
386} 389}
387#endif 390#endif
388 391
@@ -416,23 +419,31 @@ void xstat(char *name, struct stat *stat_buf)
416} 419}
417 420
418/* It is perfectly ok to pass in a NULL for either width or for 421/* It is perfectly ok to pass in a NULL for either width or for
419 * * height, in which case that value will not be set. */ 422 * height, in which case that value will not be set. */
420int get_terminal_width_height(int fd, int *width, int *height) 423int get_terminal_width_height(int fd, int *width, int *height)
421{ 424{
422 struct winsize win = { 0, 0, 0, 0 }; 425 struct winsize win = { 0, 0, 0, 0 };
423 int ret = ioctl(fd, TIOCGWINSZ, &win); 426 int ret = ioctl(fd, TIOCGWINSZ, &win);
424 if (!win.ws_row) { 427
425 char *s = getenv("LINES"); 428 if (height) {
426 if (s) win.ws_row = atoi(s); 429 if (!win.ws_row) {
430 char *s = getenv("LINES");
431 if (s) win.ws_row = atoi(s);
432 }
433 if (win.ws_row <= 1 || win.ws_row >= 30000)
434 win.ws_row = 24;
435 *height = (int) win.ws_row;
427 } 436 }
428 if (win.ws_row <= 1) win.ws_row = 24; 437
429 if (!win.ws_col) { 438 if (width) {
430 char *s = getenv("COLUMNS"); 439 if (!win.ws_col) {
431 if (s) win.ws_col = atoi(s); 440 char *s = getenv("COLUMNS");
441 if (s) win.ws_col = atoi(s);
442 }
443 if (win.ws_col <= 1 || win.ws_col >= 30000)
444 win.ws_col = 80;
445 *width = (int) win.ws_col;
432 } 446 }
433 if (win.ws_col <= 1) win.ws_col = 80;
434 if (height) *height = (int) win.ws_row;
435 if (width) *width = (int) win.ws_col;
436 447
437 return ret; 448 return ret;
438} 449}
diff --git a/networking/telnet.c b/networking/telnet.c
index 5b8c885e2..628e2e6e3 100644
--- a/networking/telnet.c
+++ b/networking/telnet.c
@@ -607,7 +607,7 @@ int telnet_main(int argc, char** argv)
607#endif 607#endif
608 608
609#ifdef CONFIG_FEATURE_TELNET_TTYPE 609#ifdef CONFIG_FEATURE_TELNET_TTYPE
610 ttype = getenv("TERM"); 610 ttype = getenv("TERM");
611#endif 611#endif
612 612
613 memset(&G, 0, sizeof G); 613 memset(&G, 0, sizeof G);
diff --git a/networking/wget.c b/networking/wget.c
index 425abc13f..c163209f2 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -676,7 +676,7 @@ getttywidth(void)
676{ 676{
677 int width=0; 677 int width=0;
678 get_terminal_width_height(0, &width, NULL); 678 get_terminal_width_height(0, &width, NULL);
679 return (width); 679 return width;
680} 680}
681 681
682static void 682static void
diff --git a/procps/ps.c b/procps/ps.c
index 0452a5046..4a917282b 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -41,7 +41,7 @@ int ps_main(int argc, char **argv)
41 /* if w is given once, GNU ps sets the width to 132, 41 /* if w is given once, GNU ps sets the width to 132,
42 * if w is given more than once, it is "unlimited" 42 * if w is given more than once, it is "unlimited"
43 */ 43 */
44 if(w_count) { 44 if (w_count) {
45 terminal_width = (w_count==1) ? 132 : INT_MAX; 45 terminal_width = (w_count==1) ? 132 : INT_MAX;
46 } else { 46 } else {
47 get_terminal_width_height(1, &terminal_width, NULL); 47 get_terminal_width_height(1, &terminal_width, NULL);
@@ -87,24 +87,24 @@ int ps_main(int argc, char **argv)
87 } 87 }
88 else 88 else
89#endif 89#endif
90 if(p->rss == 0) 90 if (p->rss == 0)
91 len = printf("%5d %-8s %s ", p->pid, p->user, p->state); 91 len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
92 else 92 else
93 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state); 93 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
94 94
95 i = terminal_width-len; 95 i = terminal_width-len;
96 96
97 if(namecmd && namecmd[0]) { 97 if (namecmd && namecmd[0]) {
98 if(i < 0) 98 if (i < 0)
99 i = 0; 99 i = 0;
100 if(strlen(namecmd) > (size_t)i) 100 if (strlen(namecmd) > (size_t)i)
101 namecmd[i] = 0; 101 namecmd[i] = 0;
102 printf("%s\n", namecmd); 102 printf("%s\n", namecmd);
103 } else { 103 } else {
104 namecmd = p->short_cmd; 104 namecmd = p->short_cmd;
105 if(i < 2) 105 if (i < 2)
106 i = 2; 106 i = 2;
107 if(strlen(namecmd) > ((size_t)i-2)) 107 if (strlen(namecmd) > ((size_t)i-2))
108 namecmd[i-2] = 0; 108 namecmd[i-2] = 0;
109 printf("[%s]\n", namecmd); 109 printf("[%s]\n", namecmd);
110 } 110 }