2002-09-16 09:10:04 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini watch implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
|
2006-08-28 23:31:54 +00:00
|
|
|
* Copyrigjt (C) Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
|
2002-09-16 09:10:04 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2002-09-16 09:10:04 +00:00
|
|
|
*/
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* BB_AUDIT SUSv3 N/A */
|
|
|
|
/* BB_AUDIT GNU defects -- only option -n is supported. */
|
|
|
|
|
2011-04-11 01:29:49 +00:00
|
|
|
//usage:#define watch_trivial_usage
|
|
|
|
//usage: "[-n SEC] [-t] PROG ARGS"
|
|
|
|
//usage:#define watch_full_usage "\n\n"
|
|
|
|
//usage: "Run PROG periodically\n"
|
|
|
|
//usage: "\n -n Loop period in seconds (default 2)"
|
|
|
|
//usage: "\n -t Don't print header"
|
|
|
|
//usage:
|
|
|
|
//usage:#define watch_example_usage
|
|
|
|
//usage: "$ watch date\n"
|
|
|
|
//usage: "Mon Dec 17 10:31:40 GMT 2000\n"
|
|
|
|
//usage: "Mon Dec 17 10:31:42 GMT 2000\n"
|
|
|
|
//usage: "Mon Dec 17 10:31:44 GMT 2000"
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2006-10-20 23:48:30 +00:00
|
|
|
// procps 2.0.18:
|
|
|
|
// watch [-d] [-n seconds]
|
|
|
|
// [--differences[=cumulative]] [--interval=seconds] command
|
|
|
|
//
|
|
|
|
// procps-3.2.3:
|
|
|
|
// watch [-dt] [-n seconds]
|
|
|
|
// [--differences[=cumulative]] [--interval=seconds] [--no-title] command
|
|
|
|
//
|
|
|
|
// (procps 3.x and procps 2.x are forks, not newer/older versions of the same)
|
2006-08-28 23:31:54 +00:00
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int watch_main(int argc UNUSED_PARAM, char **argv)
|
2002-09-16 09:10:04 +00:00
|
|
|
{
|
2006-10-20 23:48:30 +00:00
|
|
|
unsigned opt;
|
2003-03-19 09:13:01 +00:00
|
|
|
unsigned period = 2;
|
2008-05-18 22:28:26 +00:00
|
|
|
unsigned width, new_width;
|
2008-02-24 14:33:17 +00:00
|
|
|
char *header;
|
2006-10-20 23:48:30 +00:00
|
|
|
char *cmd;
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2010-01-19 01:26:38 +00:00
|
|
|
#if 0 // maybe ENABLE_DESKTOP?
|
|
|
|
// procps3 compat - "echo TEST | watch cat" doesn't show TEST:
|
|
|
|
close(STDIN_FILENO);
|
|
|
|
xopen("/dev/null", O_RDONLY);
|
|
|
|
#endif
|
|
|
|
|
2008-02-24 14:33:17 +00:00
|
|
|
opt_complementary = "-1:n+"; // at least one param; -n NUM
|
|
|
|
// "+": stop at first non-option (procps 3.x only)
|
|
|
|
opt = getopt32(argv, "+dtn:", &period);
|
2006-10-20 23:48:30 +00:00
|
|
|
argv += optind;
|
2006-05-25 23:02:40 +00:00
|
|
|
|
2008-02-24 14:33:17 +00:00
|
|
|
// watch from both procps 2.x and 3.x does concatenation. Example:
|
2010-01-19 01:26:38 +00:00
|
|
|
// watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
|
2008-02-24 14:33:17 +00:00
|
|
|
cmd = *argv;
|
|
|
|
while (*++argv)
|
|
|
|
cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2008-05-18 22:28:26 +00:00
|
|
|
width = (unsigned)-1; // make sure first time new_width != width
|
2008-02-24 14:33:17 +00:00
|
|
|
header = NULL;
|
2002-09-16 09:10:04 +00:00
|
|
|
while (1) {
|
2010-05-16 21:42:13 +00:00
|
|
|
/* home; clear to the end of screen */
|
|
|
|
printf("\033[H""\033[J");
|
2006-10-20 23:48:30 +00:00
|
|
|
if (!(opt & 0x2)) { // no -t
|
2008-05-18 22:28:26 +00:00
|
|
|
const unsigned time_len = sizeof("1234-67-90 23:56:89");
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2010-01-19 01:26:38 +00:00
|
|
|
// STDERR_FILENO is procps3 compat:
|
|
|
|
// "watch ls 2>/dev/null" does not detect tty size
|
|
|
|
get_terminal_width_height(STDERR_FILENO, &new_width, NULL);
|
2008-02-24 14:33:17 +00:00
|
|
|
if (new_width != width) {
|
|
|
|
width = new_width;
|
|
|
|
free(header);
|
2008-05-18 22:28:26 +00:00
|
|
|
header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
|
2008-02-24 14:33:17 +00:00
|
|
|
}
|
2013-03-29 11:30:33 +00:00
|
|
|
if (time_len < width) {
|
|
|
|
strftime_YYYYMMDDHHMMSS(
|
|
|
|
header + width - time_len,
|
|
|
|
time_len,
|
|
|
|
/*time_t*:*/ NULL
|
|
|
|
);
|
|
|
|
}
|
2008-02-24 14:33:17 +00:00
|
|
|
|
2010-01-19 01:26:38 +00:00
|
|
|
// compat: empty line between header and cmd output
|
|
|
|
printf("%s\n\n", header);
|
2006-10-20 23:48:30 +00:00
|
|
|
}
|
2009-11-02 13:19:51 +00:00
|
|
|
fflush_all();
|
2006-10-20 23:48:30 +00:00
|
|
|
// TODO: 'real' watch pipes cmd's output to itself
|
|
|
|
// and does not allow it to overflow the screen
|
|
|
|
// (taking into account linewrap!)
|
|
|
|
system(cmd);
|
2006-05-25 23:02:40 +00:00
|
|
|
sleep(period);
|
2002-09-16 09:10:04 +00:00
|
|
|
}
|
2006-10-23 22:43:02 +00:00
|
|
|
return 0; // gcc thinks we can reach this :)
|
2002-09-16 09:10:04 +00:00
|
|
|
}
|