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
|
|
|
*
|
2006-05-25 23:02:40 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
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. */
|
|
|
|
|
2002-09-16 09:10:04 +00:00
|
|
|
#include "busybox.h"
|
|
|
|
|
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
|
|
|
|
2006-03-06 20:47:33 +00:00
|
|
|
int watch_main(int argc, 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;
|
2006-10-20 23:48:30 +00:00
|
|
|
unsigned cmdlen = 1; // 1 for terminal NUL
|
|
|
|
char *header = NULL;
|
|
|
|
char *cmd;
|
|
|
|
char *tmp;
|
|
|
|
char **p;
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2006-10-20 23:48:30 +00:00
|
|
|
opt_complementary = "-1"; // at least one param please
|
|
|
|
opt = getopt32(argc, argv, "+dtn:", &tmp);
|
|
|
|
//if (opt & 0x1) // -d (ignore)
|
|
|
|
//if (opt & 0x2) // -t
|
|
|
|
if (opt & 0x4) period = xatou(tmp);
|
|
|
|
argv += optind;
|
2006-05-25 23:02:40 +00:00
|
|
|
|
2006-10-20 23:48:30 +00:00
|
|
|
p = argv;
|
|
|
|
while (*p)
|
|
|
|
cmdlen += strlen(*p++) + 1;
|
|
|
|
tmp = cmd = xmalloc(cmdlen);
|
|
|
|
while (*argv) {
|
|
|
|
tmp += sprintf(tmp, " %s", *argv);
|
|
|
|
argv++;
|
2002-09-16 09:10:04 +00:00
|
|
|
}
|
2006-10-20 23:48:30 +00:00
|
|
|
cmd++; // skip initial space
|
2002-09-16 09:10:04 +00:00
|
|
|
|
|
|
|
while (1) {
|
2006-10-20 23:48:30 +00:00
|
|
|
printf("\033[H\033[J");
|
|
|
|
if (!(opt & 0x2)) { // no -t
|
|
|
|
int width, len;
|
|
|
|
char *thyme;
|
|
|
|
time_t t;
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2006-10-20 23:48:30 +00:00
|
|
|
get_terminal_width_height(STDOUT_FILENO, &width, 0);
|
|
|
|
header = xrealloc(header, width--);
|
2006-10-23 22:43:02 +00:00
|
|
|
// '%-*s' pads header with spaces to the full width
|
2006-10-20 23:48:30 +00:00
|
|
|
snprintf(header, width, "Every %ds: %-*s", period, width, cmd);
|
|
|
|
time(&t);
|
|
|
|
thyme = ctime(&t);
|
|
|
|
len = strlen(thyme);
|
|
|
|
if (len < width)
|
|
|
|
strcpy(header + width - len, thyme);
|
|
|
|
puts(header);
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
// 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
|
|
|
}
|