blob: 7b9c6698adfbf9670fcbea82f2289f832852342d [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>
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +00006 * Copyrigjt (C) Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
Glenn L McGrath18b76e62002-09-16 09:10:04 +00007 *
Rob Landley399d2b52006-05-25 23:02:40 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath18b76e62002-09-16 09:10:04 +00009 */
10
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* BB_AUDIT SUSv3 N/A */
12/* BB_AUDIT GNU defects -- only option -n is supported. */
13
Glenn L McGrath18b76e62002-09-16 09:10:04 +000014#include "busybox.h"
15
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000016
Rob Landleydfba7412006-03-06 20:47:33 +000017int watch_main(int argc, char **argv)
Glenn L McGrath18b76e62002-09-16 09:10:04 +000018{
Rob Landley399d2b52006-05-25 23:02:40 +000019 int width, len;
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 unsigned period = 2;
Rob Landley399d2b52006-05-25 23:02:40 +000021 char **watched_argv, *header;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000022
Rob Landley399d2b52006-05-25 23:02:40 +000023 if (argc < 2) bb_show_usage();
24
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000025 get_terminal_width_height(STDOUT_FILENO, &width, 0);
Rob Landley399d2b52006-05-25 23:02:40 +000026 header = xzalloc(width--);
Glenn L McGrath18b76e62002-09-16 09:10:04 +000027
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 /* don't use getopt, because it permutes the arguments */
29 ++argv;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000030 if ((argc > 3) && argv[0][0] == '-' && argv[0][1] == 'n') {
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 period = bb_xgetularg10_bnd(argv[1], 1, UINT_MAX);
32 argv += 2;
33 }
34 watched_argv = argv;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000035
36 /* create header */
Rob Landley399d2b52006-05-25 23:02:40 +000037 len = snprintf(header, width, "Every %ds:", period);
38 while (*argv && len<width)
39 snprintf(header+len, width-len, " %s", *(argv++));
Glenn L McGrath18b76e62002-09-16 09:10:04 +000040
41 while (1) {
Rob Landley399d2b52006-05-25 23:02:40 +000042 char *thyme;
43 time_t t;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000044
Rob Landley399d2b52006-05-25 23:02:40 +000045 time(&t);
46 thyme = ctime(&t);
47 len = strlen(thyme);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000048 if (len < width)
49 header[width-len] = 0;
50 bb_printf("\033[H\033[J%s %s\n", header, thyme);
Rob Landley399d2b52006-05-25 23:02:40 +000051
Rob Landleyd921b2e2006-08-03 15:41:12 +000052 waitpid(xspawn(watched_argv),0,0);
Rob Landley399d2b52006-05-25 23:02:40 +000053 sleep(period);
Glenn L McGrath18b76e62002-09-16 09:10:04 +000054 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000055 if (ENABLE_FEATURE_CLEAN_UP)
56 free(header);
Glenn L McGrath18b76e62002-09-16 09:10:04 +000057}