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-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. */
|
|
|
|
|
|
|
|
/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
|
|
|
|
*
|
|
|
|
* Removed dependency on date_main(), added proper error checking, and
|
|
|
|
* reduced size.
|
|
|
|
*/
|
2002-09-16 09:10:04 +00:00
|
|
|
|
|
|
|
#include "busybox.h"
|
|
|
|
|
2006-03-06 20:47:33 +00:00
|
|
|
int watch_main(int argc, char **argv)
|
2002-09-16 09:10:04 +00:00
|
|
|
{
|
2006-05-25 23:02:40 +00:00
|
|
|
int width, len;
|
2003-03-19 09:13:01 +00:00
|
|
|
unsigned period = 2;
|
2006-05-25 23:02:40 +00:00
|
|
|
char **watched_argv, *header;
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2006-05-25 23:02:40 +00:00
|
|
|
if (argc < 2) bb_show_usage();
|
|
|
|
|
|
|
|
get_terminal_width_height(1, &width, 0);
|
|
|
|
header = xzalloc(width--);
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* don't use getopt, because it permutes the arguments */
|
|
|
|
++argv;
|
2006-05-25 23:02:40 +00:00
|
|
|
if ((argc > 3) && !strcmp(*argv, "-n")) {
|
2003-03-19 09:13:01 +00:00
|
|
|
period = bb_xgetularg10_bnd(argv[1], 1, UINT_MAX);
|
|
|
|
argv += 2;
|
2002-09-16 09:10:04 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
watched_argv = argv;
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* create header */
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2006-05-25 23:02:40 +00:00
|
|
|
len = snprintf(header, width, "Every %ds:", period);
|
|
|
|
while (*argv && len<width)
|
|
|
|
snprintf(header+len, width-len, " %s", *(argv++));
|
2002-09-16 09:10:04 +00:00
|
|
|
|
|
|
|
while (1) {
|
2006-05-25 23:02:40 +00:00
|
|
|
char *thyme;
|
|
|
|
time_t t;
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
time(&t);
|
2006-05-25 23:02:40 +00:00
|
|
|
thyme = ctime(&t);
|
|
|
|
len = strlen(thyme);
|
|
|
|
if (len < width) header[width-len] = 0;
|
|
|
|
|
|
|
|
printf("\033[H\033[J%s %s\n", header, thyme);
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2006-08-03 15:41:12 +00:00
|
|
|
waitpid(xspawn(watched_argv),0,0);
|
2006-05-25 23:02:40 +00:00
|
|
|
sleep(period);
|
2002-09-16 09:10:04 +00:00
|
|
|
}
|
|
|
|
}
|