Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 1 | /* 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-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 6 | * Copyrigjt (C) Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 7 | * |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 11 | /* BB_AUDIT SUSv3 N/A */ |
| 12 | /* BB_AUDIT GNU defects -- only option -n is supported. */ |
| 13 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 16 | // 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-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 26 | int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 27 | int watch_main(int argc, char **argv) |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 28 | { |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 29 | unsigned opt; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | unsigned period = 2; |
Denis Vlasenko | f893da8 | 2007-08-09 08:27:24 +0000 | [diff] [blame] | 31 | unsigned cmdlen; |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 32 | char *header = NULL; |
| 33 | char *cmd; |
| 34 | char *tmp; |
| 35 | char **p; |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 37 | opt_complementary = "-1"; // at least one param please |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 38 | opt = getopt32(argv, "+dtn:", &tmp); |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 39 | //if (opt & 0x1) // -d (ignore) |
| 40 | //if (opt & 0x2) // -t |
| 41 | if (opt & 0x4) period = xatou(tmp); |
| 42 | argv += optind; |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 44 | p = argv; |
Denis Vlasenko | f893da8 | 2007-08-09 08:27:24 +0000 | [diff] [blame] | 45 | cmdlen = 1; // 1 for terminal NUL |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 46 | while (*p) |
| 47 | cmdlen += strlen(*p++) + 1; |
| 48 | tmp = cmd = xmalloc(cmdlen); |
| 49 | while (*argv) { |
| 50 | tmp += sprintf(tmp, " %s", *argv); |
| 51 | argv++; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 52 | } |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 53 | cmd++; // skip initial space |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 54 | |
| 55 | while (1) { |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 56 | printf("\033[H\033[J"); |
| 57 | if (!(opt & 0x2)) { // no -t |
| 58 | int width, len; |
| 59 | char *thyme; |
| 60 | time_t t; |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | f893da8 | 2007-08-09 08:27:24 +0000 | [diff] [blame] | 62 | get_terminal_width_height(STDIN_FILENO, &width, 0); |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 63 | header = xrealloc(header, width--); |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 64 | // '%-*s' pads header with spaces to the full width |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 65 | snprintf(header, width, "Every %ds: %-*s", period, width, cmd); |
| 66 | time(&t); |
| 67 | thyme = ctime(&t); |
| 68 | len = strlen(thyme); |
| 69 | if (len < width) |
| 70 | strcpy(header + width - len, thyme); |
| 71 | puts(header); |
| 72 | } |
| 73 | fflush(stdout); |
| 74 | // TODO: 'real' watch pipes cmd's output to itself |
| 75 | // and does not allow it to overflow the screen |
| 76 | // (taking into account linewrap!) |
| 77 | system(cmd); |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 78 | sleep(period); |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 79 | } |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 80 | return 0; // gcc thinks we can reach this :) |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 81 | } |