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