blob: c8b16b908f14ad15915b08c950b26d50d2e81cac [file] [log] [blame]
Glenn L McGrath18b76e62002-09-16 09:10:04 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini watch implementation for busybox
4 *
5 * Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
6 *
Rob Landley399d2b52006-05-25 23:02:40 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath18b76e62002-09-16 09:10:04 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 N/A */
11/* BB_AUDIT GNU defects -- only option -n is supported. */
12
13/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
14 *
15 * Removed dependency on date_main(), added proper error checking, and
16 * reduced size.
17 */
Glenn L McGrath18b76e62002-09-16 09:10:04 +000018
Glenn L McGrath18b76e62002-09-16 09:10:04 +000019#include "busybox.h"
20
Rob Landleydfba7412006-03-06 20:47:33 +000021int watch_main(int argc, char **argv)
Glenn L McGrath18b76e62002-09-16 09:10:04 +000022{
Rob Landley399d2b52006-05-25 23:02:40 +000023 int width, len;
Manuel Novoa III cad53642003-03-19 09:13:01 +000024 unsigned period = 2;
Rob Landley399d2b52006-05-25 23:02:40 +000025 char **watched_argv, *header;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000026
Rob Landley399d2b52006-05-25 23:02:40 +000027 if (argc < 2) bb_show_usage();
28
29 get_terminal_width_height(1, &width, 0);
30 header = xzalloc(width--);
Glenn L McGrath18b76e62002-09-16 09:10:04 +000031
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 /* don't use getopt, because it permutes the arguments */
33 ++argv;
Rob Landley399d2b52006-05-25 23:02:40 +000034 if ((argc > 3) && !strcmp(*argv, "-n")) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 period = bb_xgetularg10_bnd(argv[1], 1, UINT_MAX);
36 argv += 2;
37 }
38 watched_argv = argv;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000039
40 /* create header */
Glenn L McGrath18b76e62002-09-16 09:10:04 +000041
Rob Landley399d2b52006-05-25 23:02:40 +000042 len = snprintf(header, width, "Every %ds:", period);
43 while (*argv && len<width)
44 snprintf(header+len, width-len, " %s", *(argv++));
Glenn L McGrath18b76e62002-09-16 09:10:04 +000045
46 while (1) {
Rob Landley399d2b52006-05-25 23:02:40 +000047 char *thyme;
48 time_t t;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000049
Rob Landley399d2b52006-05-25 23:02:40 +000050 time(&t);
51 thyme = ctime(&t);
52 len = strlen(thyme);
53 if (len < width) header[width-len] = 0;
54
55 printf("\033[H\033[J%s %s\n", header, thyme);
56
Rob Landleyd921b2e2006-08-03 15:41:12 +000057 waitpid(xspawn(watched_argv),0,0);
Rob Landley399d2b52006-05-25 23:02:40 +000058 sleep(period);
Glenn L McGrath18b76e62002-09-16 09:10:04 +000059 }
Glenn L McGrath18b76e62002-09-16 09:10:04 +000060}