3
0

ttysize.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Replacement for "stty size", which is awkward for shell script use.
  4. * - Allows to request width, height, or both, in any order.
  5. * - Does not complain on error, but returns width 80, height 24.
  6. * - Size: less than 200 bytes
  7. *
  8. * Copyright (C) 2007 by Denys Vlasenko <vda.linux@googlemail.com>
  9. *
  10. * Licensed under GPLv2, see file LICENSE in this source tree.
  11. */
  12. //usage:#define ttysize_trivial_usage
  13. //usage: "[w] [h]"
  14. //usage:#define ttysize_full_usage "\n\n"
  15. //usage: "Print dimension(s) of stdin's terminal, on error return 80x25"
  16. #include "libbb.h"
  17. int ttysize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  18. int ttysize_main(int argc UNUSED_PARAM, char **argv)
  19. {
  20. unsigned w, h;
  21. struct winsize wsz;
  22. w = 80;
  23. h = 24;
  24. if (!ioctl(0, TIOCGWINSZ, &wsz)) {
  25. w = wsz.ws_col;
  26. h = wsz.ws_row;
  27. }
  28. if (!argv[1]) {
  29. printf("%u %u", w, h);
  30. } else {
  31. const char *fmt, *arg;
  32. fmt = "%u %u" + 3; /* "%u" */
  33. while ((arg = *++argv) != NULL) {
  34. char c = arg[0];
  35. if (c == 'w')
  36. printf(fmt, w);
  37. if (c == 'h')
  38. printf(fmt, h);
  39. fmt = "%u %u" + 2; /* " %u" */
  40. }
  41. }
  42. bb_putchar('\n');
  43. return 0;
  44. }