get_terminal_width_height.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Determine the width and height of the terminal.
  4. *
  5. * Copyright (C) 1999-2004 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. #include <stdio.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <unistd.h>
  26. #include <termios.h>
  27. #include <sys/ioctl.h>
  28. #include "busybox.h"
  29. /* It is perfectly ok to pass in a NULL for either width or for
  30. * height, in which case that value will not be set. It is also
  31. * perfectly ok to have CONFIG_FEATURE_AUTOWIDTH disabled, in
  32. * which case you will always get 80x24 */
  33. void get_terminal_width_height(int fd, int *width, int *height)
  34. {
  35. struct winsize win = { 0, 0, 0, 0 };
  36. #ifdef CONFIG_FEATURE_AUTOWIDTH
  37. if (ioctl(fd, TIOCGWINSZ, &win) != 0) {
  38. win.ws_row = 24;
  39. win.ws_col = 80;
  40. }
  41. #endif
  42. if (win.ws_row <= 1) {
  43. win.ws_row = 24;
  44. }
  45. if (win.ws_col <= 1) {
  46. win.ws_col = 80;
  47. }
  48. if (height) {
  49. *height = (int) win.ws_row;
  50. }
  51. if (width) {
  52. *width = (int) win.ws_col;
  53. }
  54. }
  55. /* END CODE */
  56. /*
  57. Local Variables:
  58. c-file-style: "linux"
  59. c-basic-offset: 4
  60. tab-width: 4
  61. End:
  62. */