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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
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 | |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 14 | //config:config WATCH |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 15 | //config: bool "watch (4.1 kb)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 16 | //config: default y |
| 17 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 18 | //config: watch is used to execute a program periodically, showing |
| 19 | //config: output to the screen. |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 20 | |
| 21 | //applet:IF_WATCH(APPLET(watch, BB_DIR_BIN, BB_SUID_DROP)) |
| 22 | |
| 23 | //kbuild:lib-$(CONFIG_WATCH) += watch.o |
| 24 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 25 | //usage:#define watch_trivial_usage |
| 26 | //usage: "[-n SEC] [-t] PROG ARGS" |
| 27 | //usage:#define watch_full_usage "\n\n" |
| 28 | //usage: "Run PROG periodically\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 29 | //usage: "\n -n Loop period in seconds (default 2)" |
| 30 | //usage: "\n -t Don't print header" |
| 31 | //usage: |
| 32 | //usage:#define watch_example_usage |
| 33 | //usage: "$ watch date\n" |
| 34 | //usage: "Mon Dec 17 10:31:40 GMT 2000\n" |
| 35 | //usage: "Mon Dec 17 10:31:42 GMT 2000\n" |
| 36 | //usage: "Mon Dec 17 10:31:44 GMT 2000" |
| 37 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 38 | #include "libbb.h" |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 40 | // procps 2.0.18: |
| 41 | // watch [-d] [-n seconds] |
| 42 | // [--differences[=cumulative]] [--interval=seconds] command |
| 43 | // |
| 44 | // procps-3.2.3: |
| 45 | // watch [-dt] [-n seconds] |
| 46 | // [--differences[=cumulative]] [--interval=seconds] [--no-title] command |
| 47 | // |
| 48 | // (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] | 49 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 50 | int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 51 | int watch_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 52 | { |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 53 | unsigned opt; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 54 | unsigned period = 2; |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 55 | unsigned width, new_width; |
Denis Vlasenko | 28b2991 | 2008-02-24 14:33:17 +0000 | [diff] [blame] | 56 | char *header; |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 57 | char *cmd; |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 58 | |
Denys Vlasenko | 302af9e | 2010-01-19 02:26:38 +0100 | [diff] [blame] | 59 | #if 0 // maybe ENABLE_DESKTOP? |
| 60 | // procps3 compat - "echo TEST | watch cat" doesn't show TEST: |
| 61 | close(STDIN_FILENO); |
| 62 | xopen("/dev/null", O_RDONLY); |
| 63 | #endif |
| 64 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 65 | // "+": stop at first non-option (procps 3.x only); -n NUM |
| 66 | // at least one param |
| 67 | opt = getopt32(argv, "^+" "dtn:+" "\0" "-1", &period); |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 68 | argv += optind; |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 28b2991 | 2008-02-24 14:33:17 +0000 | [diff] [blame] | 70 | // watch from both procps 2.x and 3.x does concatenation. Example: |
Denys Vlasenko | 302af9e | 2010-01-19 02:26:38 +0100 | [diff] [blame] | 71 | // watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param |
Denis Vlasenko | 28b2991 | 2008-02-24 14:33:17 +0000 | [diff] [blame] | 72 | cmd = *argv; |
| 73 | while (*++argv) |
| 74 | cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 76 | width = (unsigned)-1; // make sure first time new_width != width |
Denis Vlasenko | 28b2991 | 2008-02-24 14:33:17 +0000 | [diff] [blame] | 77 | header = NULL; |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 78 | while (1) { |
Denys Vlasenko | d9a3e89 | 2010-05-16 23:42:13 +0200 | [diff] [blame] | 79 | /* home; clear to the end of screen */ |
| 80 | printf("\033[H""\033[J"); |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 81 | if (!(opt & 0x2)) { // no -t |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 82 | const unsigned time_len = sizeof("1234-67-90 23:56:89"); |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 83 | |
Denys Vlasenko | 302af9e | 2010-01-19 02:26:38 +0100 | [diff] [blame] | 84 | // STDERR_FILENO is procps3 compat: |
| 85 | // "watch ls 2>/dev/null" does not detect tty size |
Denys Vlasenko | 641caae | 2015-10-23 01:44:22 +0200 | [diff] [blame] | 86 | new_width = get_terminal_width(STDERR_FILENO); |
Denis Vlasenko | 28b2991 | 2008-02-24 14:33:17 +0000 | [diff] [blame] | 87 | if (new_width != width) { |
| 88 | width = new_width; |
| 89 | free(header); |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 90 | header = xasprintf("Every %us: %-*s", period, (int)width, cmd); |
Denis Vlasenko | 28b2991 | 2008-02-24 14:33:17 +0000 | [diff] [blame] | 91 | } |
Denys Vlasenko | 8f2cb7a | 2013-03-29 12:30:33 +0100 | [diff] [blame] | 92 | if (time_len < width) { |
| 93 | strftime_YYYYMMDDHHMMSS( |
| 94 | header + width - time_len, |
| 95 | time_len, |
| 96 | /*time_t*:*/ NULL |
| 97 | ); |
| 98 | } |
Denis Vlasenko | 28b2991 | 2008-02-24 14:33:17 +0000 | [diff] [blame] | 99 | |
Denys Vlasenko | 302af9e | 2010-01-19 02:26:38 +0100 | [diff] [blame] | 100 | // compat: empty line between header and cmd output |
| 101 | printf("%s\n\n", header); |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 102 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 103 | fflush_all(); |
Denis Vlasenko | 8d73c35 | 2006-10-20 23:48:30 +0000 | [diff] [blame] | 104 | // TODO: 'real' watch pipes cmd's output to itself |
| 105 | // and does not allow it to overflow the screen |
| 106 | // (taking into account linewrap!) |
| 107 | system(cmd); |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 108 | sleep(period); |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 109 | } |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 110 | return 0; // gcc thinks we can reach this :) |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 111 | } |