watch.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. //config:config WATCH
  11. //config: bool "watch (4.4 kb)"
  12. //config: default y
  13. //config: help
  14. //config: watch is used to execute a program periodically, showing
  15. //config: output to the screen.
  16. //applet:IF_WATCH(APPLET(watch, BB_DIR_BIN, BB_SUID_DROP))
  17. //kbuild:lib-$(CONFIG_WATCH) += watch.o
  18. //usage:#define watch_trivial_usage
  19. //usage: "[-n SEC] [-t] PROG ARGS"
  20. //usage:#define watch_full_usage "\n\n"
  21. //usage: "Run PROG periodically\n"
  22. //usage: "\n -n SEC Loop period (default 2)"
  23. //usage: "\n -t Don't print header"
  24. //usage:
  25. //usage:#define watch_example_usage
  26. //usage: "$ watch date\n"
  27. //usage: "Mon Dec 17 10:31:40 GMT 2000\n"
  28. //usage: "Mon Dec 17 10:31:42 GMT 2000\n"
  29. //usage: "Mon Dec 17 10:31:44 GMT 2000"
  30. /* BB_AUDIT SUSv3 N/A */
  31. /* BB_AUDIT GNU defects -- only option -n is supported. */
  32. #include "libbb.h"
  33. #define ESC "\033"
  34. // procps 2.0.18:
  35. // watch [-d] [-n seconds]
  36. // [--differences[=cumulative]] [--interval=seconds] command
  37. //
  38. // procps-3.2.3:
  39. // watch [-dt] [-n seconds]
  40. // [--differences[=cumulative]] [--interval=seconds] [--no-title] command
  41. //
  42. // (procps 3.x and procps 2.x are forks, not newer/older versions of the same)
  43. int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  44. int watch_main(int argc UNUSED_PARAM, char **argv)
  45. {
  46. duration_t period;
  47. char *period_str = (char*) "2";
  48. unsigned opt;
  49. unsigned width, new_width;
  50. char *header;
  51. char *cmd;
  52. #if 0 // maybe ENABLE_DESKTOP?
  53. // procps3 compat - "echo TEST | watch cat" doesn't show TEST:
  54. close(STDIN_FILENO);
  55. xopen("/dev/null", O_RDONLY);
  56. #endif
  57. // "+": stop at first non-option (procps 3.x only); -n NUM
  58. // at least one param
  59. opt = getopt32(argv, "^+" "dtn:" "\0" "-1", &period_str);
  60. argv += optind;
  61. // watch from both procps 2.x and 3.x does concatenation. Example:
  62. // watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
  63. cmd = *argv;
  64. while (*++argv)
  65. cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
  66. period = parse_duration_str(period_str);
  67. width = (unsigned)-1; // make sure first time new_width != width
  68. header = NULL;
  69. while (1) {
  70. /* home; clear to the end of screen */
  71. printf(ESC"[H" ESC"[J");
  72. if (!(opt & 0x2)) { // no -t
  73. const unsigned time_len = sizeof("1234-67-90 23:56:89");
  74. // STDERR_FILENO is procps3 compat:
  75. // "watch ls 2>/dev/null" does not detect tty size
  76. new_width = get_terminal_width(STDERR_FILENO);
  77. if (new_width != width) {
  78. width = new_width;
  79. free(header);
  80. header = xasprintf("Every"
  81. " %"IF_FLOAT_DURATION(".1")DURATION_FMT"s:"
  82. " %-*s",
  83. period,
  84. (int)width, cmd
  85. );
  86. }
  87. if (time_len < width) {
  88. strftime_YYYYMMDDHHMMSS(
  89. header + width - time_len,
  90. time_len,
  91. /*time_t*:*/ NULL
  92. );
  93. }
  94. // compat: empty line between header and cmd output
  95. printf("%s\n\n", header);
  96. }
  97. fflush_all();
  98. // TODO: 'real' watch pipes cmd's output to itself
  99. // and does not allow it to overflow the screen
  100. // (taking into account linewrap!)
  101. system(cmd);
  102. sleep_for_duration(period);
  103. }
  104. return 0; // gcc thinks we can reach this :)
  105. }