watch.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini watch implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
  6. * Copyrigjt (C) Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. /* BB_AUDIT SUSv3 N/A */
  11. /* BB_AUDIT GNU defects -- only option -n is supported. */
  12. //usage:#define watch_trivial_usage
  13. //usage: "[-n SEC] [-t] PROG ARGS"
  14. //usage:#define watch_full_usage "\n\n"
  15. //usage: "Run PROG periodically\n"
  16. //usage: "\n -n Loop period in seconds (default 2)"
  17. //usage: "\n -t Don't print header"
  18. //usage:
  19. //usage:#define watch_example_usage
  20. //usage: "$ watch date\n"
  21. //usage: "Mon Dec 17 10:31:40 GMT 2000\n"
  22. //usage: "Mon Dec 17 10:31:42 GMT 2000\n"
  23. //usage: "Mon Dec 17 10:31:44 GMT 2000"
  24. #include "libbb.h"
  25. // procps 2.0.18:
  26. // watch [-d] [-n seconds]
  27. // [--differences[=cumulative]] [--interval=seconds] command
  28. //
  29. // procps-3.2.3:
  30. // watch [-dt] [-n seconds]
  31. // [--differences[=cumulative]] [--interval=seconds] [--no-title] command
  32. //
  33. // (procps 3.x and procps 2.x are forks, not newer/older versions of the same)
  34. int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  35. int watch_main(int argc UNUSED_PARAM, char **argv)
  36. {
  37. unsigned opt;
  38. unsigned period = 2;
  39. unsigned width, new_width;
  40. char *header;
  41. char *cmd;
  42. #if 0 // maybe ENABLE_DESKTOP?
  43. // procps3 compat - "echo TEST | watch cat" doesn't show TEST:
  44. close(STDIN_FILENO);
  45. xopen("/dev/null", O_RDONLY);
  46. #endif
  47. opt_complementary = "-1:n+"; // at least one param; -n NUM
  48. // "+": stop at first non-option (procps 3.x only)
  49. opt = getopt32(argv, "+dtn:", &period);
  50. argv += optind;
  51. // watch from both procps 2.x and 3.x does concatenation. Example:
  52. // watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
  53. cmd = *argv;
  54. while (*++argv)
  55. cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
  56. width = (unsigned)-1; // make sure first time new_width != width
  57. header = NULL;
  58. while (1) {
  59. /* home; clear to the end of screen */
  60. printf("\033[H""\033[J");
  61. if (!(opt & 0x2)) { // no -t
  62. const unsigned time_len = sizeof("1234-67-90 23:56:89");
  63. // STDERR_FILENO is procps3 compat:
  64. // "watch ls 2>/dev/null" does not detect tty size
  65. get_terminal_width_height(STDERR_FILENO, &new_width, NULL);
  66. if (new_width != width) {
  67. width = new_width;
  68. free(header);
  69. header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
  70. }
  71. if (time_len < width) {
  72. strftime_YYYYMMDDHHMMSS(
  73. header + width - time_len,
  74. time_len,
  75. /*time_t*:*/ NULL
  76. );
  77. }
  78. // compat: empty line between header and cmd output
  79. printf("%s\n\n", header);
  80. }
  81. fflush_all();
  82. // TODO: 'real' watch pipes cmd's output to itself
  83. // and does not allow it to overflow the screen
  84. // (taking into account linewrap!)
  85. system(cmd);
  86. sleep(period);
  87. }
  88. return 0; // gcc thinks we can reach this :)
  89. }