2002-09-16 14:40:04 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini watch implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
|
2006-08-29 05:01:54 +05:30
|
|
|
* Copyrigjt (C) Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
|
2002-09-16 14:40:04 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2002-09-16 14:40:04 +05:30
|
|
|
*/
|
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
/* BB_AUDIT SUSv3 N/A */
|
|
|
|
/* BB_AUDIT GNU defects -- only option -n is supported. */
|
|
|
|
|
2016-11-23 10:53:44 +05:30
|
|
|
//config:config WATCH
|
|
|
|
//config: bool "watch"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: watch is used to execute a program periodically, showing
|
|
|
|
//config: output to the screen.
|
|
|
|
|
|
|
|
//applet:IF_WATCH(APPLET(watch, BB_DIR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_WATCH) += watch.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//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-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2002-09-16 14:40:04 +05:30
|
|
|
|
2006-10-21 05:18:30 +05:30
|
|
|
// 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-29 05:01:54 +05:30
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int watch_main(int argc UNUSED_PARAM, char **argv)
|
2002-09-16 14:40:04 +05:30
|
|
|
{
|
2006-10-21 05:18:30 +05:30
|
|
|
unsigned opt;
|
2003-03-19 14:43:01 +05:30
|
|
|
unsigned period = 2;
|
2008-05-19 03:58:26 +05:30
|
|
|
unsigned width, new_width;
|
2008-02-24 20:03:17 +05:30
|
|
|
char *header;
|
2006-10-21 05:18:30 +05:30
|
|
|
char *cmd;
|
2002-09-16 14:40:04 +05:30
|
|
|
|
2010-01-19 06:56:38 +05:30
|
|
|
#if 0 // maybe ENABLE_DESKTOP?
|
|
|
|
// procps3 compat - "echo TEST | watch cat" doesn't show TEST:
|
|
|
|
close(STDIN_FILENO);
|
|
|
|
xopen("/dev/null", O_RDONLY);
|
|
|
|
#endif
|
|
|
|
|
2016-07-07 01:28:02 +05:30
|
|
|
opt_complementary = "-1"; // at least one param; -n NUM
|
2008-02-24 20:03:17 +05:30
|
|
|
// "+": stop at first non-option (procps 3.x only)
|
2016-07-07 01:28:02 +05:30
|
|
|
opt = getopt32(argv, "+dtn:+", &period);
|
2006-10-21 05:18:30 +05:30
|
|
|
argv += optind;
|
2006-05-26 04:32:40 +05:30
|
|
|
|
2008-02-24 20:03:17 +05:30
|
|
|
// watch from both procps 2.x and 3.x does concatenation. Example:
|
2010-01-19 06:56:38 +05:30
|
|
|
// watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
|
2008-02-24 20:03:17 +05:30
|
|
|
cmd = *argv;
|
|
|
|
while (*++argv)
|
|
|
|
cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
|
2002-09-16 14:40:04 +05:30
|
|
|
|
2008-05-19 03:58:26 +05:30
|
|
|
width = (unsigned)-1; // make sure first time new_width != width
|
2008-02-24 20:03:17 +05:30
|
|
|
header = NULL;
|
2002-09-16 14:40:04 +05:30
|
|
|
while (1) {
|
2010-05-17 03:12:13 +05:30
|
|
|
/* home; clear to the end of screen */
|
|
|
|
printf("\033[H""\033[J");
|
2006-10-21 05:18:30 +05:30
|
|
|
if (!(opt & 0x2)) { // no -t
|
2008-05-19 03:58:26 +05:30
|
|
|
const unsigned time_len = sizeof("1234-67-90 23:56:89");
|
2002-09-16 14:40:04 +05:30
|
|
|
|
2010-01-19 06:56:38 +05:30
|
|
|
// STDERR_FILENO is procps3 compat:
|
|
|
|
// "watch ls 2>/dev/null" does not detect tty size
|
2015-10-23 05:14:22 +05:30
|
|
|
new_width = get_terminal_width(STDERR_FILENO);
|
2008-02-24 20:03:17 +05:30
|
|
|
if (new_width != width) {
|
|
|
|
width = new_width;
|
|
|
|
free(header);
|
2008-05-19 03:58:26 +05:30
|
|
|
header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
|
2008-02-24 20:03:17 +05:30
|
|
|
}
|
2013-03-29 17:00:33 +05:30
|
|
|
if (time_len < width) {
|
|
|
|
strftime_YYYYMMDDHHMMSS(
|
|
|
|
header + width - time_len,
|
|
|
|
time_len,
|
|
|
|
/*time_t*:*/ NULL
|
|
|
|
);
|
|
|
|
}
|
2008-02-24 20:03:17 +05:30
|
|
|
|
2010-01-19 06:56:38 +05:30
|
|
|
// compat: empty line between header and cmd output
|
|
|
|
printf("%s\n\n", header);
|
2006-10-21 05:18:30 +05:30
|
|
|
}
|
2009-11-02 18:49:51 +05:30
|
|
|
fflush_all();
|
2006-10-21 05:18:30 +05:30
|
|
|
// 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-26 04:32:40 +05:30
|
|
|
sleep(period);
|
2002-09-16 14:40:04 +05:30
|
|
|
}
|
2006-10-24 04:13:02 +05:30
|
|
|
return 0; // gcc thinks we can reach this :)
|
2002-09-16 14:40:04 +05:30
|
|
|
}
|