1 /* vi: set sw=4 ts=4: */
3 * Mini watch implementation for busybox
5 * Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
6 * Copyrigjt (C) Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 /* BB_AUDIT SUSv3 N/A */
12 /* BB_AUDIT GNU defects -- only option -n is supported. */
15 //config: bool "watch (4.1 kb)"
18 //config: watch is used to execute a program periodically, showing
19 //config: output to the screen.
21 //applet:IF_WATCH(APPLET(watch, BB_DIR_BIN, BB_SUID_DROP))
23 //kbuild:lib-$(CONFIG_WATCH) += watch.o
25 //usage:#define watch_trivial_usage
26 //usage: "[-n SEC] [-t] PROG ARGS"
27 //usage:#define watch_full_usage "\n\n"
28 //usage: "Run PROG periodically\n"
29 //usage: "\n -n Loop period in seconds (default 2)"
30 //usage: "\n -t Don't print header"
32 //usage:#define watch_example_usage
33 //usage: "$ watch date\n"
34 //usage: "Mon Dec 17 10:31:40 GMT 2000\n"
35 //usage: "Mon Dec 17 10:31:42 GMT 2000\n"
36 //usage: "Mon Dec 17 10:31:44 GMT 2000"
43 // watch [-d] [-n seconds]
44 // [--differences[=cumulative]] [--interval=seconds] command
47 // watch [-dt] [-n seconds]
48 // [--differences[=cumulative]] [--interval=seconds] [--no-title] command
50 // (procps 3.x and procps 2.x are forks, not newer/older versions of the same)
52 int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
53 int watch_main(int argc UNUSED_PARAM, char **argv)
57 unsigned width, new_width;
61 #if 0 // maybe ENABLE_DESKTOP?
62 // procps3 compat - "echo TEST | watch cat" doesn't show TEST:
64 xopen("/dev/null", O_RDONLY);
67 // "+": stop at first non-option (procps 3.x only); -n NUM
69 opt = getopt32(argv, "^+" "dtn:+" "\0" "-1", &period);
72 // watch from both procps 2.x and 3.x does concatenation. Example:
73 // watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
76 cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
78 width = (unsigned)-1; // make sure first time new_width != width
81 /* home; clear to the end of screen */
82 printf(ESC"[H" ESC"[J");
83 if (!(opt & 0x2)) { // no -t
84 const unsigned time_len = sizeof("1234-67-90 23:56:89");
86 // STDERR_FILENO is procps3 compat:
87 // "watch ls 2>/dev/null" does not detect tty size
88 new_width = get_terminal_width(STDERR_FILENO);
89 if (new_width != width) {
92 header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
94 if (time_len < width) {
95 strftime_YYYYMMDDHHMMSS(
96 header + width - time_len,
102 // compat: empty line between header and cmd output
103 printf("%s\n\n", header);
106 // TODO: 'real' watch pipes cmd's output to itself
107 // and does not allow it to overflow the screen
108 // (taking into account linewrap!)
112 return 0; // gcc thinks we can reach this :)