busybox/procps/watch.c
Denys Vlasenko 237bedd499 getopt32: add new syntax of 'o:+' and 'o:*' for -o NUM and -o LIST
In many cases, this aqllows to drop use of opt_complementary.
Approximately -400 bytes:

function                                             old     new   delta
getopt32                                            1423    1502     +79
opt_string                                            17      18      +1
OPT_STR                                               24      25      +1
uniq_main                                            416     406     -10
timeout_main                                         279     269     -10
sulogin_main                                         270     260     -10
readprofile_main                                    1825    1815     -10
ps_main                                              543     533     -10
pidof_main                                           245     235     -10
pgrep_main                                           611     601     -10
od_main                                             2600    2590     -10
mkfs_minix_main                                     2684    2674     -10
mkfs_ext2_main                                      2603    2593     -10
microcom_main                                        712     702     -10
makemime_main                                        315     305     -10
ionice_main                                          282     272     -10
inetd_main                                          2074    2064     -10
ifplugd_main                                        1144    1134     -10
halt_main                                            353     343     -10
getopt_main                                          636     626     -10
fdisk_main                                          2854    2844     -10
env_main                                             206     196     -10
dmesg_main                                           319     309     -10
conspy_main                                         1214    1204     -10
awk_main                                             981     971     -10
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/22 up/down: 81/-220)         Total: -139 bytes
   text	   data	    bss	    dec	    hex	filename
 919373	    906	  14060	 934339	  e41c3	busybox_old
 918969	    906	  14060	 933935	  e402f	busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-07-06 21:58:02 +02:00

101 lines
2.9 KiB
C

/* vi: set sw=4 ts=4: */
/*
* Mini watch implementation for busybox
*
* Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
* Copyrigjt (C) Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
/* BB_AUDIT SUSv3 N/A */
/* BB_AUDIT GNU defects -- only option -n is supported. */
//usage:#define watch_trivial_usage
//usage: "[-n SEC] [-t] PROG ARGS"
//usage:#define watch_full_usage "\n\n"
//usage: "Run PROG periodically\n"
//usage: "\n -n Loop period in seconds (default 2)"
//usage: "\n -t Don't print header"
//usage:
//usage:#define watch_example_usage
//usage: "$ watch date\n"
//usage: "Mon Dec 17 10:31:40 GMT 2000\n"
//usage: "Mon Dec 17 10:31:42 GMT 2000\n"
//usage: "Mon Dec 17 10:31:44 GMT 2000"
#include "libbb.h"
// procps 2.0.18:
// watch [-d] [-n seconds]
// [--differences[=cumulative]] [--interval=seconds] command
//
// procps-3.2.3:
// watch [-dt] [-n seconds]
// [--differences[=cumulative]] [--interval=seconds] [--no-title] command
//
// (procps 3.x and procps 2.x are forks, not newer/older versions of the same)
int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int watch_main(int argc UNUSED_PARAM, char **argv)
{
unsigned opt;
unsigned period = 2;
unsigned width, new_width;
char *header;
char *cmd;
#if 0 // maybe ENABLE_DESKTOP?
// procps3 compat - "echo TEST | watch cat" doesn't show TEST:
close(STDIN_FILENO);
xopen("/dev/null", O_RDONLY);
#endif
opt_complementary = "-1"; // at least one param; -n NUM
// "+": stop at first non-option (procps 3.x only)
opt = getopt32(argv, "+dtn:+", &period);
argv += optind;
// watch from both procps 2.x and 3.x does concatenation. Example:
// watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
cmd = *argv;
while (*++argv)
cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
width = (unsigned)-1; // make sure first time new_width != width
header = NULL;
while (1) {
/* home; clear to the end of screen */
printf("\033[H""\033[J");
if (!(opt & 0x2)) { // no -t
const unsigned time_len = sizeof("1234-67-90 23:56:89");
// STDERR_FILENO is procps3 compat:
// "watch ls 2>/dev/null" does not detect tty size
new_width = get_terminal_width(STDERR_FILENO);
if (new_width != width) {
width = new_width;
free(header);
header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
}
if (time_len < width) {
strftime_YYYYMMDDHHMMSS(
header + width - time_len,
time_len,
/*time_t*:*/ NULL
);
}
// compat: empty line between header and cmd output
printf("%s\n\n", header);
}
fflush_all();
// TODO: 'real' watch pipes cmd's output to itself
// and does not allow it to overflow the screen
// (taking into account linewrap!)
system(cmd);
sleep(period);
}
return 0; // gcc thinks we can reach this :)
}