From 8d73c35916cbae67f5d0269b128de40f9992ddc6 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Fri, 20 Oct 2006 23:48:30 +0000 Subject: [PATCH] watch: execute command thru shell, not fork/exec. Other fixes --- coreutils/watch.c | 79 +++++++++++++++++++++++++++++++++++-------------------- include/usage.h | 7 ++--- 2 files changed, 54 insertions(+), 32 deletions(-) diff --git a/coreutils/watch.c b/coreutils/watch.c index b1a7d90..e3e9e06 100644 --- a/coreutils/watch.c +++ b/coreutils/watch.c @@ -13,45 +13,66 @@ #include "busybox.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) { - int width, len; + unsigned opt; unsigned period = 2; - char **watched_argv, *header; + unsigned cmdlen = 1; // 1 for terminal NUL + char *header = NULL; + char *cmd; + char *tmp; + char **p; - if (argc < 2) bb_show_usage(); + opt_complementary = "-1"; // at least one param please + opt = getopt32(argc, argv, "+dtn:", &tmp); + //if (opt & 0x1) // -d (ignore) + //if (opt & 0x2) // -t + if (opt & 0x4) period = xatou(tmp); + argv += optind; - get_terminal_width_height(STDOUT_FILENO, &width, 0); - header = xzalloc(width--); - - /* don't use getopt, because it permutes the arguments */ - ++argv; - if ((argc > 3) && argv[0][0] == '-' && argv[0][1] == 'n') { - period = xatou(argv[1]); - argv += 2; + p = argv; + while (*p) + cmdlen += strlen(*p++) + 1; + tmp = cmd = xmalloc(cmdlen); + while (*argv) { + tmp += sprintf(tmp, " %s", *argv); + argv++; } - watched_argv = argv; - - /* create header */ - len = snprintf(header, width, "Every %ds:", period); - while (*argv && len] COMMAND..." + "[-n ] [-t] COMMAND..." #define watch_full_usage \ - "Executes a program periodically.\n" \ + "Executes a program periodically\n\n" \ "Options:\n" \ - "\t-n\tLoop period in seconds - default is 2" + "\t-n\tLoop period in seconds - default is 2\n" + "\t-t\tDon't print header" #define watch_example_usage \ "$ watch date\n" \ "Mon Dec 17 10:31:40 GMT 2000\n" \ -- 2.7.4