blob: 2ad0564cd7c226d44d3396a699471b9160186d53 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
Glenn L McGrath18b76e62002-09-16 09:10:04 +000015
Denis Vlasenko8d73c352006-10-20 23:48:30 +000016// procps 2.0.18:
17// watch [-d] [-n seconds]
18// [--differences[=cumulative]] [--interval=seconds] command
19//
20// procps-3.2.3:
21// watch [-dt] [-n seconds]
22// [--differences[=cumulative]] [--interval=seconds] [--no-title] command
23//
24// (procps 3.x and procps 2.x are forks, not newer/older versions of the same)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000025
Denis Vlasenko06af2162007-02-03 17:28:39 +000026int watch_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000027int watch_main(int argc, char **argv)
Glenn L McGrath18b76e62002-09-16 09:10:04 +000028{
Denis Vlasenko8d73c352006-10-20 23:48:30 +000029 unsigned opt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 unsigned period = 2;
Denis Vlasenko8d73c352006-10-20 23:48:30 +000031 unsigned cmdlen = 1; // 1 for terminal NUL
32 char *header = NULL;
33 char *cmd;
34 char *tmp;
35 char **p;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000036
Denis Vlasenko8d73c352006-10-20 23:48:30 +000037 opt_complementary = "-1"; // at least one param please
38 opt = getopt32(argc, argv, "+dtn:", &tmp);
39 //if (opt & 0x1) // -d (ignore)
40 //if (opt & 0x2) // -t
41 if (opt & 0x4) period = xatou(tmp);
42 argv += optind;
Rob Landley399d2b52006-05-25 23:02:40 +000043
Denis Vlasenko8d73c352006-10-20 23:48:30 +000044 p = argv;
45 while (*p)
46 cmdlen += strlen(*p++) + 1;
47 tmp = cmd = xmalloc(cmdlen);
48 while (*argv) {
49 tmp += sprintf(tmp, " %s", *argv);
50 argv++;
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 }
Denis Vlasenko8d73c352006-10-20 23:48:30 +000052 cmd++; // skip initial space
Glenn L McGrath18b76e62002-09-16 09:10:04 +000053
54 while (1) {
Denis Vlasenko8d73c352006-10-20 23:48:30 +000055 printf("\033[H\033[J");
56 if (!(opt & 0x2)) { // no -t
57 int width, len;
58 char *thyme;
59 time_t t;
Glenn L McGrath18b76e62002-09-16 09:10:04 +000060
Denis Vlasenko8d73c352006-10-20 23:48:30 +000061 get_terminal_width_height(STDOUT_FILENO, &width, 0);
62 header = xrealloc(header, width--);
Denis Vlasenko703aa132006-10-23 22:43:02 +000063 // '%-*s' pads header with spaces to the full width
Denis Vlasenko8d73c352006-10-20 23:48:30 +000064 snprintf(header, width, "Every %ds: %-*s", period, width, cmd);
65 time(&t);
66 thyme = ctime(&t);
67 len = strlen(thyme);
68 if (len < width)
69 strcpy(header + width - len, thyme);
70 puts(header);
71 }
72 fflush(stdout);
73 // TODO: 'real' watch pipes cmd's output to itself
74 // and does not allow it to overflow the screen
75 // (taking into account linewrap!)
76 system(cmd);
Rob Landley399d2b52006-05-25 23:02:40 +000077 sleep(period);
Glenn L McGrath18b76e62002-09-16 09:10:04 +000078 }
Denis Vlasenko703aa132006-10-23 22:43:02 +000079 return 0; // gcc thinks we can reach this :)
Glenn L McGrath18b76e62002-09-16 09:10:04 +000080}