watch.c 3.2 KB

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