diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-02-24 14:33:17 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-02-24 14:33:17 +0000 |
commit | 28b29916cbc532fafa1b195e3917fb49ae53bb8e (patch) | |
tree | 4d3f634acb1627dec4710e9d2344b455887d9d2f | |
parent | 400d8bb45ee25ce226bb343a3dfaab84e6d3a8e1 (diff) | |
download | busybox-w32-28b29916cbc532fafa1b195e3917fb49ae53bb8e.tar.gz busybox-w32-28b29916cbc532fafa1b195e3917fb49ae53bb8e.tar.bz2 busybox-w32-28b29916cbc532fafa1b195e3917fb49ae53bb8e.zip |
watch: shrink (by walter harms <WHarms AT bfs.de>)
watch_main 327 263 -64
-rw-r--r-- | procps/watch.c | 52 |
1 files changed, 23 insertions, 29 deletions
diff --git a/procps/watch.c b/procps/watch.c index 76d2a87e9..b4955258e 100644 --- a/procps/watch.c +++ b/procps/watch.c | |||
@@ -28,46 +28,40 @@ int watch_main(int argc, char **argv) | |||
28 | { | 28 | { |
29 | unsigned opt; | 29 | unsigned opt; |
30 | unsigned period = 2; | 30 | unsigned period = 2; |
31 | unsigned cmdlen; | 31 | int width, new_width; |
32 | char *header = NULL; | 32 | char *header; |
33 | char *cmd; | 33 | char *cmd; |
34 | char *tmp; | ||
35 | char **p; | ||
36 | 34 | ||
37 | opt_complementary = "-1"; // at least one param please | 35 | opt_complementary = "-1:n+"; // at least one param; -n NUM |
38 | opt = getopt32(argv, "+dtn:", &tmp); | 36 | // "+": stop at first non-option (procps 3.x only) |
39 | //if (opt & 0x1) // -d (ignore) | 37 | opt = getopt32(argv, "+dtn:", &period); |
40 | //if (opt & 0x2) // -t | ||
41 | if (opt & 0x4) period = xatou(tmp); | ||
42 | argv += optind; | 38 | argv += optind; |
43 | 39 | ||
44 | p = argv; | 40 | // watch from both procps 2.x and 3.x does concatenation. Example: |
45 | cmdlen = 1; // 1 for terminal NUL | 41 | // watch ls -l "a /tmp" "2>&1" -- ls won't see "a /tmp" as one param |
46 | while (*p) | 42 | cmd = *argv; |
47 | cmdlen += strlen(*p++) + 1; | 43 | while (*++argv) |
48 | tmp = cmd = xmalloc(cmdlen); | 44 | cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd |
49 | while (*argv) { | ||
50 | tmp += sprintf(tmp, " %s", *argv); | ||
51 | argv++; | ||
52 | } | ||
53 | cmd++; // skip initial space | ||
54 | 45 | ||
46 | width = -1; // make sure first time new_width != width | ||
47 | header = NULL; | ||
55 | while (1) { | 48 | while (1) { |
56 | printf("\033[H\033[J"); | 49 | printf("\033[H\033[J"); |
57 | if (!(opt & 0x2)) { // no -t | 50 | if (!(opt & 0x2)) { // no -t |
58 | int width, len; | 51 | const int time_len = sizeof("1234-67-90 23:56:89"); |
59 | char *thyme; | ||
60 | time_t t; | 52 | time_t t; |
61 | 53 | ||
62 | get_terminal_width_height(STDIN_FILENO, &width, 0); | 54 | get_terminal_width_height(STDIN_FILENO, &new_width, NULL); |
63 | header = xrealloc(header, width--); | 55 | if (new_width != width) { |
64 | // '%-*s' pads header with spaces to the full width | 56 | width = new_width; |
65 | snprintf(header, width, "Every %ds: %-*s", period, width, cmd); | 57 | free(header); |
58 | header = xasprintf("Every %us: %-*s", period, width, cmd); | ||
59 | } | ||
66 | time(&t); | 60 | time(&t); |
67 | thyme = ctime(&t); | 61 | if (time_len < width) |
68 | len = strlen(thyme); | 62 | strftime(header + width - time_len, time_len, |
69 | if (len < width) | 63 | "%Y-%m-%d %H:%M:%S", localtime(&t)); |
70 | strcpy(header + width - len, thyme); | 64 | |
71 | puts(header); | 65 | puts(header); |
72 | } | 66 | } |
73 | fflush(stdout); | 67 | fflush(stdout); |