aboutsummaryrefslogtreecommitdiff
path: root/coreutils/stty.c
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2007-01-17 19:45:36 +0000
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2007-01-17 19:45:36 +0000
commitd4a745c3d4936690660a4813b0c5e2ca9990838f (patch)
treea317925b1edc137b8ec84b1b483b6686db3ab278 /coreutils/stty.c
parenta6e31ad83431eff9a76bf40dada68f2d925c9f70 (diff)
downloadbusybox-w32-d4a745c3d4936690660a4813b0c5e2ca9990838f.tar.gz
busybox-w32-d4a745c3d4936690660a4813b0c5e2ca9990838f.tar.bz2
busybox-w32-d4a745c3d4936690660a4813b0c5e2ca9990838f.zip
- peruse get_terminal_width_height and remove legacy code for ancient
slowaris versions.
Diffstat (limited to 'coreutils/stty.c')
-rw-r--r--coreutils/stty.c79
1 files changed, 10 insertions, 69 deletions
diff --git a/coreutils/stty.c b/coreutils/stty.c
index a78e15c07..05d91d8c1 100644
--- a/coreutils/stty.c
+++ b/coreutils/stty.c
@@ -484,21 +484,13 @@ static void wrapf(const char *message, ...)
484 current_col = 0; 484 current_col = 0;
485} 485}
486 486
487#ifdef TIOCGWINSZ 487static void set_window_size(const int rows, const int cols)
488
489static int get_win_size(int fd, struct winsize *win)
490{
491 return ioctl(fd, TIOCGWINSZ, (char *) win);
492}
493
494static void set_window_size(int rows, int cols)
495{ 488{
496 struct winsize win; 489 struct winsize win = { 0, 0, 0, 0};
497 490
498 if (get_win_size(STDIN_FILENO, &win)) { 491 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win)) {
499 if (errno != EINVAL) { 492 if (errno != EINVAL) {
500 perror_on_device("%s"); 493 goto bail;
501 return;
502 } 494 }
503 memset(&win, 0, sizeof(win)); 495 memset(&win, 0, sizeof(win));
504 } 496 }
@@ -508,77 +500,26 @@ static void set_window_size(int rows, int cols)
508 if (cols >= 0) 500 if (cols >= 0)
509 win.ws_col = cols; 501 win.ws_col = cols;
510 502
511# ifdef TIOCSSIZE
512 /* Alexander Dupuy <dupuy@cs.columbia.edu> wrote:
513 The following code deals with a bug in the SunOS 4.x (and 3.x?) kernel.
514 This comment from sys/ttold.h describes Sun's twisted logic - a better
515 test would have been (ts_lines > 64k || ts_cols > 64k || ts_cols == 0).
516 At any rate, the problem is gone in Solaris 2.x */
517
518 if (win.ws_row == 0 || win.ws_col == 0) {
519 struct ttysize ttysz;
520
521 ttysz.ts_lines = win.ws_row;
522 ttysz.ts_cols = win.ws_col;
523
524 win.ws_row = win.ws_col = 1;
525
526 if ((ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win) != 0)
527 || (ioctl(STDIN_FILENO, TIOCSSIZE, (char *) &ttysz) != 0)) {
528 perror_on_device("%s");
529 }
530 return;
531 }
532# endif
533
534 if (ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win)) 503 if (ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win))
504bail:
535 perror_on_device("%s"); 505 perror_on_device("%s");
536} 506}
537 507
538static void display_window_size(int fancy) 508static void display_window_size(const int fancy)
539{ 509{
540 const char *fmt_str = "%s\0%s: no size information for this device"; 510 const char *fmt_str = "%s\0%s: no size information for this device";
541 struct winsize win; 511 unsigned width, height;
542 512
543 if (get_win_size(STDIN_FILENO, &win)) { 513 if (get_terminal_width_height(STDIN_FILENO, &width, &height)) {
544 if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) { 514 if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
545 perror_on_device(fmt_str); 515 perror_on_device(fmt_str);
546 } 516 }
547 } else { 517 } else {
548 wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n", 518 wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n",
549 win.ws_row, win.ws_col); 519 height, width);
550 } 520 }
551} 521}
552 522
553#else /* !TIOCGWINSZ */
554
555static inline void display_window_size(int fancy) {}
556
557#endif /* !TIOCGWINSZ */
558
559static int screen_columns_or_die(void)
560{
561 const char *s;
562
563#ifdef TIOCGWINSZ
564 struct winsize win;
565
566 /* With Solaris 2.[123], this ioctl fails and errno is set to
567 EINVAL for telnet (but not rlogin) sessions.
568 On ISC 3.0, it fails for the console and the serial port
569 (but it works for ptys).
570 It can also fail on any system when stdout isn't a tty.
571 In case of any failure, just use the default */
572 if (get_win_size(STDOUT_FILENO, &win) == 0 && win.ws_col > 0)
573 return win.ws_col;
574#endif
575
576 s = getenv("COLUMNS");
577 if (s)
578 return xatoi_u(s);
579 return 80;
580}
581
582static const struct suffix_mult stty_suffixes[] = { 523static const struct suffix_mult stty_suffixes[] = {
583 {"b", 512 }, 524 {"b", 512 },
584 {"k", 1024}, 525 {"k", 1024},
@@ -1183,7 +1124,7 @@ invalid_argument:
1183 perror_on_device_and_die("%s"); 1124 perror_on_device_and_die("%s");
1184 1125
1185 if (option_mask32 & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) { 1126 if (option_mask32 & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) {
1186 max_col = screen_columns_or_die(); 1127 get_terminal_width_height(STDOUT_FILENO, &max_col, NULL);
1187 output_func(&mode); 1128 output_func(&mode);
1188 return EXIT_SUCCESS; 1129 return EXIT_SUCCESS;
1189 } 1130 }