Bump version to 1.32.0
[oweals/busybox.git] / procps / watch.c
index bb34124c0fff932e27fbaaf46e5e951de3202843..059eb1dda7c664064d1034c78b906fe899608b5d 100644 (file)
@@ -7,16 +7,12 @@
  *
  * 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. */
-
 //config:config WATCH
-//config:      bool "watch"
+//config:      bool "watch (4.4 kb)"
 //config:      default y
 //config:      help
-//config:        watch is used to execute a program periodically, showing
-//config:        output to the screen.
+//config:      watch is used to execute a program periodically, showing
+//config:      output to the screen.
 
 //applet:IF_WATCH(APPLET(watch, BB_DIR_BIN, BB_SUID_DROP))
 
@@ -26,7 +22,7 @@
 //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       -n SEC  Loop period (default 2)"
 //usage:     "\n       -t      Don't print header"
 //usage:
 //usage:#define watch_example_usage
 //usage:       "Mon Dec 17 10:31:42 GMT 2000\n"
 //usage:       "Mon Dec 17 10:31:44 GMT 2000"
 
+/* BB_AUDIT SUSv3 N/A */
+/* BB_AUDIT GNU defects -- only option -n is supported. */
+
 #include "libbb.h"
 
+#define ESC "\033"
+
 // procps 2.0.18:
 // watch [-d] [-n seconds]
 //   [--differences[=cumulative]] [--interval=seconds] command
@@ -50,8 +51,9 @@
 int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int watch_main(int argc UNUSED_PARAM, char **argv)
 {
+       duration_t period;
+       char *period_str = (char*) "2";
        unsigned opt;
-       unsigned period = 2;
        unsigned width, new_width;
        char *header;
        char *cmd;
@@ -62,9 +64,9 @@ int watch_main(int argc UNUSED_PARAM, char **argv)
        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);
+       // "+": stop at first non-option (procps 3.x only); -n NUM
+       // at least one param
+       opt = getopt32(argv, "^+" "dtn:" "\0" "-1", &period_str);
        argv += optind;
 
        // watch from both procps 2.x and 3.x does concatenation. Example:
@@ -73,11 +75,12 @@ int watch_main(int argc UNUSED_PARAM, char **argv)
        while (*++argv)
                cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
 
+       period = parse_duration_str(period_str);
        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");
+               printf(ESC"[H" ESC"[J");
                if (!(opt & 0x2)) { // no -t
                        const unsigned time_len = sizeof("1234-67-90 23:56:89");
 
@@ -87,7 +90,12 @@ int watch_main(int argc UNUSED_PARAM, char **argv)
                        if (new_width != width) {
                                width = new_width;
                                free(header);
-                               header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
+                               header = xasprintf("Every"
+                                       " %"IF_FLOAT_DURATION(".1")DURATION_FMT"s:"
+                                       " %-*s",
+                                       period,
+                                       (int)width, cmd
+                               );
                        }
                        if (time_len < width) {
                                strftime_YYYYMMDDHHMMSS(
@@ -105,7 +113,7 @@ int watch_main(int argc UNUSED_PARAM, char **argv)
                // and does not allow it to overflow the screen
                // (taking into account linewrap!)
                system(cmd);
-               sleep(period);
+               sleep_for_duration(period);
        }
        return 0; // gcc thinks we can reach this :)
 }