aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2019-03-12 18:56:51 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2019-03-12 18:56:51 +0100
commit0ddc742c04538fdd8be51fb1d4dbcbd4309952db (patch)
treeaad3cbbb11ccfc718537fbf5497a17fd0eaf68a9
parent2452247ea33c1c70263bf4cbc11b3170366ff2ea (diff)
downloadbusybox-w32-0ddc742c04538fdd8be51fb1d4dbcbd4309952db.tar.gz
busybox-w32-0ddc742c04538fdd8be51fb1d4dbcbd4309952db.tar.bz2
busybox-w32-0ddc742c04538fdd8be51fb1d4dbcbd4309952db.zip
watch: support fractional -n SEC
function old new delta watch_main 212 232 +20 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--include/libbb.h2
-rw-r--r--procps/watch.c17
2 files changed, 14 insertions, 5 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 3366df30f..7a1e13875 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1040,9 +1040,11 @@ uint16_t xatou16(const char *numstr) FAST_FUNC;
1040#if ENABLE_FLOAT_DURATION 1040#if ENABLE_FLOAT_DURATION
1041typedef double duration_t; 1041typedef double duration_t;
1042void sleep_for_duration(duration_t duration) FAST_FUNC; 1042void sleep_for_duration(duration_t duration) FAST_FUNC;
1043#define DURATION_FMT "f"
1043#else 1044#else
1044typedef unsigned duration_t; 1045typedef unsigned duration_t;
1045#define sleep_for_duration(duration) sleep(duration) 1046#define sleep_for_duration(duration) sleep(duration)
1047#define DURATION_FMT "u"
1046#endif 1048#endif
1047duration_t parse_duration_str(char *str) FAST_FUNC; 1049duration_t parse_duration_str(char *str) FAST_FUNC;
1048 1050
diff --git a/procps/watch.c b/procps/watch.c
index dbe427aad..059eb1dda 100644
--- a/procps/watch.c
+++ b/procps/watch.c
@@ -22,7 +22,7 @@
22//usage: "[-n SEC] [-t] PROG ARGS" 22//usage: "[-n SEC] [-t] PROG ARGS"
23//usage:#define watch_full_usage "\n\n" 23//usage:#define watch_full_usage "\n\n"
24//usage: "Run PROG periodically\n" 24//usage: "Run PROG periodically\n"
25//usage: "\n -n Loop period in seconds (default 2)" 25//usage: "\n -n SEC Loop period (default 2)"
26//usage: "\n -t Don't print header" 26//usage: "\n -t Don't print header"
27//usage: 27//usage:
28//usage:#define watch_example_usage 28//usage:#define watch_example_usage
@@ -51,8 +51,9 @@
51int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 51int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
52int watch_main(int argc UNUSED_PARAM, char **argv) 52int watch_main(int argc UNUSED_PARAM, char **argv)
53{ 53{
54 duration_t period;
55 char *period_str = (char*) "2";
54 unsigned opt; 56 unsigned opt;
55 unsigned period = 2;
56 unsigned width, new_width; 57 unsigned width, new_width;
57 char *header; 58 char *header;
58 char *cmd; 59 char *cmd;
@@ -65,7 +66,7 @@ int watch_main(int argc UNUSED_PARAM, char **argv)
65 66
66 // "+": stop at first non-option (procps 3.x only); -n NUM 67 // "+": stop at first non-option (procps 3.x only); -n NUM
67 // at least one param 68 // at least one param
68 opt = getopt32(argv, "^+" "dtn:+" "\0" "-1", &period); 69 opt = getopt32(argv, "^+" "dtn:" "\0" "-1", &period_str);
69 argv += optind; 70 argv += optind;
70 71
71 // watch from both procps 2.x and 3.x does concatenation. Example: 72 // watch from both procps 2.x and 3.x does concatenation. Example:
@@ -74,6 +75,7 @@ int watch_main(int argc UNUSED_PARAM, char **argv)
74 while (*++argv) 75 while (*++argv)
75 cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd 76 cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
76 77
78 period = parse_duration_str(period_str);
77 width = (unsigned)-1; // make sure first time new_width != width 79 width = (unsigned)-1; // make sure first time new_width != width
78 header = NULL; 80 header = NULL;
79 while (1) { 81 while (1) {
@@ -88,7 +90,12 @@ int watch_main(int argc UNUSED_PARAM, char **argv)
88 if (new_width != width) { 90 if (new_width != width) {
89 width = new_width; 91 width = new_width;
90 free(header); 92 free(header);
91 header = xasprintf("Every %us: %-*s", period, (int)width, cmd); 93 header = xasprintf("Every"
94 " %"IF_FLOAT_DURATION(".1")DURATION_FMT"s:"
95 " %-*s",
96 period,
97 (int)width, cmd
98 );
92 } 99 }
93 if (time_len < width) { 100 if (time_len < width) {
94 strftime_YYYYMMDDHHMMSS( 101 strftime_YYYYMMDDHHMMSS(
@@ -106,7 +113,7 @@ int watch_main(int argc UNUSED_PARAM, char **argv)
106 // and does not allow it to overflow the screen 113 // and does not allow it to overflow the screen
107 // (taking into account linewrap!) 114 // (taking into account linewrap!)
108 system(cmd); 115 system(cmd);
109 sleep(period); 116 sleep_for_duration(period);
110 } 117 }
111 return 0; // gcc thinks we can reach this :) 118 return 0; // gcc thinks we can reach this :)
112} 119}