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. */
14 //usage:#define watch_trivial_usage
15 //usage: "[-n SEC] [-t] PROG ARGS"
16 //usage:#define watch_full_usage "\n\n"
17 //usage: "Run PROG periodically\n"
19 //usage: "\n -n Loop period in seconds (default 2)"
20 //usage: "\n -t Don't print header"
22 //usage:#define watch_example_usage
23 //usage: "$ watch date\n"
24 //usage: "Mon Dec 17 10:31:40 GMT 2000\n"
25 //usage: "Mon Dec 17 10:31:42 GMT 2000\n"
26 //usage: "Mon Dec 17 10:31:44 GMT 2000"
31 // watch [-d] [-n seconds]
32 // [--differences[=cumulative]] [--interval=seconds] command
35 // watch [-dt] [-n seconds]
36 // [--differences[=cumulative]] [--interval=seconds] [--no-title] command
38 // (procps 3.x and procps 2.x are forks, not newer/older versions of the same)
40 int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
41 int watch_main(int argc UNUSED_PARAM, char **argv)
45 unsigned width, new_width;
49 #if 0 // maybe ENABLE_DESKTOP?
50 // procps3 compat - "echo TEST | watch cat" doesn't show TEST:
52 xopen("/dev/null", O_RDONLY);
55 opt_complementary = "-1:n+"; // at least one param; -n NUM
56 // "+": stop at first non-option (procps 3.x only)
57 opt = getopt32(argv, "+dtn:", &period);
60 // watch from both procps 2.x and 3.x does concatenation. Example:
61 // watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
64 cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
66 width = (unsigned)-1; // make sure first time new_width != width
69 /* home; clear to the end of screen */
70 printf("\033[H""\033[J");
71 if (!(opt & 0x2)) { // no -t
72 const unsigned time_len = sizeof("1234-67-90 23:56:89");
75 // STDERR_FILENO is procps3 compat:
76 // "watch ls 2>/dev/null" does not detect tty size
77 get_terminal_width_height(STDERR_FILENO, &new_width, NULL);
78 if (new_width != width) {
81 header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
85 strftime(header + width - time_len, time_len,
86 "%Y-%m-%d %H:%M:%S", localtime(&t));
88 // compat: empty line between header and cmd output
89 printf("%s\n\n", header);
92 // TODO: 'real' watch pipes cmd's output to itself
93 // and does not allow it to overflow the screen
94 // (taking into account linewrap!)
98 return 0; // gcc thinks we can reach this :)