blob: 420e6d3d145cb6b9e8b31f989b59d1ab24b3a50c [file] [log] [blame]
Glenn L McGrath18b76e62002-09-16 09:10:04 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini watch implementation for busybox
4 *
5 * Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +00006 * Copyrigjt (C) Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
Glenn L McGrath18b76e62002-09-16 09:10:04 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath18b76e62002-09-16 09:10:04 +00009 */
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010010//config:config WATCH
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "watch (4.1 kb)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010012//config: default y
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: watch is used to execute a program periodically, showing
15//config: output to the screen.
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010016
17//applet:IF_WATCH(APPLET(watch, BB_DIR_BIN, BB_SUID_DROP))
18
19//kbuild:lib-$(CONFIG_WATCH) += watch.o
20
Pere Orga5bc8c002011-04-11 03:29:49 +020021//usage:#define watch_trivial_usage
22//usage: "[-n SEC] [-t] PROG ARGS"
23//usage:#define watch_full_usage "\n\n"
24//usage: "Run PROG periodically\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020025//usage: "\n -n Loop period in seconds (default 2)"
26//usage: "\n -t Don't print header"
27//usage:
28//usage:#define watch_example_usage
29//usage: "$ watch date\n"
30//usage: "Mon Dec 17 10:31:40 GMT 2000\n"
31//usage: "Mon Dec 17 10:31:42 GMT 2000\n"
32//usage: "Mon Dec 17 10:31:44 GMT 2000"
33
Denys Vlasenko0c4dbd42017-09-18 16:28:43 +020034/* BB_AUDIT SUSv3 N/A */
35/* BB_AUDIT GNU defects -- only option -n is supported. */
36
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000037#include "libbb.h"
Glenn L McGrath18b76e62002-09-16 09:10:04 +000038
Denys Vlasenko8187e012017-09-13 22:48:30 +020039#define ESC "\033"
40
Denis Vlasenko8d73c352006-10-20 23:48:30 +000041// procps 2.0.18:
42// watch [-d] [-n seconds]
43// [--differences[=cumulative]] [--interval=seconds] command
44//
45// procps-3.2.3:
46// watch [-dt] [-n seconds]
47// [--differences[=cumulative]] [--interval=seconds] [--no-title] command
48//
49// (procps 3.x and procps 2.x are forks, not newer/older versions of the same)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000050
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000051int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000052int watch_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath18b76e62002-09-16 09:10:04 +000053{
Denis Vlasenko8d73c352006-10-20 23:48:30 +000054 unsigned opt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 unsigned period = 2;
Denis Vlasenko55995022008-05-18 22:28:26 +000056 unsigned width, new_width;
Denis Vlasenko28b29912008-02-24 14:33:17 +000057 char *header;
Denis Vlasenko8d73c352006-10-20 23:48:30 +000058 char *cmd;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000059
Denys Vlasenko302af9e2010-01-19 02:26:38 +010060#if 0 // maybe ENABLE_DESKTOP?
61 // procps3 compat - "echo TEST | watch cat" doesn't show TEST:
62 close(STDIN_FILENO);
63 xopen("/dev/null", O_RDONLY);
64#endif
65
Denys Vlasenko22542ec2017-08-08 21:55:02 +020066 // "+": stop at first non-option (procps 3.x only); -n NUM
67 // at least one param
68 opt = getopt32(argv, "^+" "dtn:+" "\0" "-1", &period);
Denis Vlasenko8d73c352006-10-20 23:48:30 +000069 argv += optind;
Rob Landley399d2b52006-05-25 23:02:40 +000070
Denis Vlasenko28b29912008-02-24 14:33:17 +000071 // watch from both procps 2.x and 3.x does concatenation. Example:
Denys Vlasenko302af9e2010-01-19 02:26:38 +010072 // watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
Denis Vlasenko28b29912008-02-24 14:33:17 +000073 cmd = *argv;
74 while (*++argv)
75 cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
Glenn L McGrath18b76e62002-09-16 09:10:04 +000076
Denis Vlasenko55995022008-05-18 22:28:26 +000077 width = (unsigned)-1; // make sure first time new_width != width
Denis Vlasenko28b29912008-02-24 14:33:17 +000078 header = NULL;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000079 while (1) {
Denys Vlasenkod9a3e892010-05-16 23:42:13 +020080 /* home; clear to the end of screen */
Denys Vlasenko8187e012017-09-13 22:48:30 +020081 printf(ESC"[H" ESC"[J");
Denis Vlasenko8d73c352006-10-20 23:48:30 +000082 if (!(opt & 0x2)) { // no -t
Denis Vlasenko55995022008-05-18 22:28:26 +000083 const unsigned time_len = sizeof("1234-67-90 23:56:89");
Glenn L McGrath18b76e62002-09-16 09:10:04 +000084
Denys Vlasenko302af9e2010-01-19 02:26:38 +010085 // STDERR_FILENO is procps3 compat:
86 // "watch ls 2>/dev/null" does not detect tty size
Denys Vlasenko641caae2015-10-23 01:44:22 +020087 new_width = get_terminal_width(STDERR_FILENO);
Denis Vlasenko28b29912008-02-24 14:33:17 +000088 if (new_width != width) {
89 width = new_width;
90 free(header);
Denis Vlasenko55995022008-05-18 22:28:26 +000091 header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
Denis Vlasenko28b29912008-02-24 14:33:17 +000092 }
Denys Vlasenko8f2cb7a2013-03-29 12:30:33 +010093 if (time_len < width) {
94 strftime_YYYYMMDDHHMMSS(
95 header + width - time_len,
96 time_len,
97 /*time_t*:*/ NULL
98 );
99 }
Denis Vlasenko28b29912008-02-24 14:33:17 +0000100
Denys Vlasenko302af9e2010-01-19 02:26:38 +0100101 // compat: empty line between header and cmd output
102 printf("%s\n\n", header);
Denis Vlasenko8d73c352006-10-20 23:48:30 +0000103 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100104 fflush_all();
Denis Vlasenko8d73c352006-10-20 23:48:30 +0000105 // TODO: 'real' watch pipes cmd's output to itself
106 // and does not allow it to overflow the screen
107 // (taking into account linewrap!)
108 system(cmd);
Rob Landley399d2b52006-05-25 23:02:40 +0000109 sleep(period);
Glenn L McGrath18b76e62002-09-16 09:10:04 +0000110 }
Denis Vlasenko703aa132006-10-23 22:43:02 +0000111 return 0; // gcc thinks we can reach this :)
Glenn L McGrath18b76e62002-09-16 09:10:04 +0000112}