busybox/coreutils/watch.c

58 lines
1.3 KiB
C
Raw Normal View History

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>
* Copyrigjt (C) Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
2002-09-16 14:40:04 +05:30
*
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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. */
2002-09-16 14:40:04 +05:30
#include "busybox.h"
int watch_main(int argc, char **argv)
2002-09-16 14:40:04 +05:30
{
int width, len;
2003-03-19 14:43:01 +05:30
unsigned period = 2;
char **watched_argv, *header;
2002-09-16 14:40:04 +05:30
if (argc < 2) bb_show_usage();
get_terminal_width_height(STDOUT_FILENO, &width, 0);
header = xzalloc(width--);
2002-09-16 14:40:04 +05:30
2003-03-19 14:43:01 +05:30
/* don't use getopt, because it permutes the arguments */
++argv;
if ((argc > 3) && argv[0][0] == '-' && argv[0][1] == 'n') {
2003-03-19 14:43:01 +05:30
period = bb_xgetularg10_bnd(argv[1], 1, UINT_MAX);
argv += 2;
2002-09-16 14:40:04 +05:30
}
2003-03-19 14:43:01 +05:30
watched_argv = argv;
2002-09-16 14:40:04 +05:30
2003-03-19 14:43:01 +05:30
/* create header */
len = snprintf(header, width, "Every %ds:", period);
while (*argv && len<width)
snprintf(header+len, width-len, " %s", *(argv++));
2002-09-16 14:40:04 +05:30
while (1) {
char *thyme;
time_t t;
2003-03-19 14:43:01 +05:30
time(&t);
thyme = ctime(&t);
len = strlen(thyme);
if (len < width)
header[width-len] = 0;
bb_printf("\033[H\033[J%s %s\n", header, thyme);
2002-09-16 14:40:04 +05:30
waitpid(xspawn(watched_argv),0,0);
sleep(period);
2002-09-16 14:40:04 +05:30
}
if (ENABLE_FEATURE_CLEAN_UP)
free(header);
2002-09-16 14:40:04 +05:30
}