blob: e289bf8cbf26f6b1b3ec199d87c3374ed436c2fc [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 */
10
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* BB_AUDIT SUSv3 N/A */
12/* BB_AUDIT GNU defects -- only option -n is supported. */
13
Pere Orga5bc8c002011-04-11 03:29:49 +020014//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"
18//usage: "\nOptions:"
19//usage: "\n -n Loop period in seconds (default 2)"
20//usage: "\n -t Don't print header"
21//usage:
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"
27
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Glenn L McGrath18b76e62002-09-16 09:10:04 +000029
Denis Vlasenko8d73c352006-10-20 23:48:30 +000030// procps 2.0.18:
31// watch [-d] [-n seconds]
32// [--differences[=cumulative]] [--interval=seconds] command
33//
34// procps-3.2.3:
35// watch [-dt] [-n seconds]
36// [--differences[=cumulative]] [--interval=seconds] [--no-title] command
37//
38// (procps 3.x and procps 2.x are forks, not newer/older versions of the same)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000039
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000040int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000041int watch_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath18b76e62002-09-16 09:10:04 +000042{
Denis Vlasenko8d73c352006-10-20 23:48:30 +000043 unsigned opt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 unsigned period = 2;
Denis Vlasenko55995022008-05-18 22:28:26 +000045 unsigned width, new_width;
Denis Vlasenko28b29912008-02-24 14:33:17 +000046 char *header;
Denis Vlasenko8d73c352006-10-20 23:48:30 +000047 char *cmd;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000048
Denys Vlasenko302af9e2010-01-19 02:26:38 +010049#if 0 // maybe ENABLE_DESKTOP?
50 // procps3 compat - "echo TEST | watch cat" doesn't show TEST:
51 close(STDIN_FILENO);
52 xopen("/dev/null", O_RDONLY);
53#endif
54
Denis Vlasenko28b29912008-02-24 14:33:17 +000055 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);
Denis Vlasenko8d73c352006-10-20 23:48:30 +000058 argv += optind;
Rob Landley399d2b52006-05-25 23:02:40 +000059
Denis Vlasenko28b29912008-02-24 14:33:17 +000060 // watch from both procps 2.x and 3.x does concatenation. Example:
Denys Vlasenko302af9e2010-01-19 02:26:38 +010061 // watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
Denis Vlasenko28b29912008-02-24 14:33:17 +000062 cmd = *argv;
63 while (*++argv)
64 cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
Glenn L McGrath18b76e62002-09-16 09:10:04 +000065
Denis Vlasenko55995022008-05-18 22:28:26 +000066 width = (unsigned)-1; // make sure first time new_width != width
Denis Vlasenko28b29912008-02-24 14:33:17 +000067 header = NULL;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000068 while (1) {
Denys Vlasenkod9a3e892010-05-16 23:42:13 +020069 /* home; clear to the end of screen */
70 printf("\033[H""\033[J");
Denis Vlasenko8d73c352006-10-20 23:48:30 +000071 if (!(opt & 0x2)) { // no -t
Denis Vlasenko55995022008-05-18 22:28:26 +000072 const unsigned time_len = sizeof("1234-67-90 23:56:89");
Denis Vlasenko8d73c352006-10-20 23:48:30 +000073 time_t t;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000074
Denys Vlasenko302af9e2010-01-19 02:26:38 +010075 // 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);
Denis Vlasenko28b29912008-02-24 14:33:17 +000078 if (new_width != width) {
79 width = new_width;
80 free(header);
Denis Vlasenko55995022008-05-18 22:28:26 +000081 header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
Denis Vlasenko28b29912008-02-24 14:33:17 +000082 }
Denis Vlasenko8d73c352006-10-20 23:48:30 +000083 time(&t);
Denis Vlasenko28b29912008-02-24 14:33:17 +000084 if (time_len < width)
85 strftime(header + width - time_len, time_len,
86 "%Y-%m-%d %H:%M:%S", localtime(&t));
87
Denys Vlasenko302af9e2010-01-19 02:26:38 +010088 // compat: empty line between header and cmd output
89 printf("%s\n\n", header);
Denis Vlasenko8d73c352006-10-20 23:48:30 +000090 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +010091 fflush_all();
Denis Vlasenko8d73c352006-10-20 23:48:30 +000092 // TODO: 'real' watch pipes cmd's output to itself
93 // and does not allow it to overflow the screen
94 // (taking into account linewrap!)
95 system(cmd);
Rob Landley399d2b52006-05-25 23:02:40 +000096 sleep(period);
Glenn L McGrath18b76e62002-09-16 09:10:04 +000097 }
Denis Vlasenko703aa132006-10-23 22:43:02 +000098 return 0; // gcc thinks we can reach this :)
Glenn L McGrath18b76e62002-09-16 09:10:04 +000099}