diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-10-20 23:48:30 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-10-20 23:48:30 +0000 |
commit | 2f36b989836df0f58dada461a7d7da608182cc02 (patch) | |
tree | 7d6d89f17a37ee35f043e82e941748fa2395858e | |
parent | d26b90c3b2075077162dd944a90329ea3424b7bf (diff) | |
download | busybox-w32-2f36b989836df0f58dada461a7d7da608182cc02.tar.gz busybox-w32-2f36b989836df0f58dada461a7d7da608182cc02.tar.bz2 busybox-w32-2f36b989836df0f58dada461a7d7da608182cc02.zip |
watch: execute command thru shell, not fork/exec. Other fixes
git-svn-id: svn://busybox.net/trunk/busybox@16417 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r-- | coreutils/watch.c | 79 | ||||
-rw-r--r-- | include/usage.h | 7 |
2 files changed, 54 insertions, 32 deletions
diff --git a/coreutils/watch.c b/coreutils/watch.c index b1a7d9086..e3e9e06bb 100644 --- a/coreutils/watch.c +++ b/coreutils/watch.c | |||
@@ -13,45 +13,66 @@ | |||
13 | 13 | ||
14 | #include "busybox.h" | 14 | #include "busybox.h" |
15 | 15 | ||
16 | // procps 2.0.18: | ||
17 | // watch [-d] [-n seconds] | ||
18 | // [--differences[=cumulative]] [--interval=seconds] command | ||
19 | // | ||
20 | // procps-3.2.3: | ||
21 | // watch [-dt] [-n seconds] | ||
22 | // [--differences[=cumulative]] [--interval=seconds] [--no-title] command | ||
23 | // | ||
24 | // (procps 3.x and procps 2.x are forks, not newer/older versions of the same) | ||
16 | 25 | ||
17 | int watch_main(int argc, char **argv) | 26 | int watch_main(int argc, char **argv) |
18 | { | 27 | { |
19 | int width, len; | 28 | unsigned opt; |
20 | unsigned period = 2; | 29 | unsigned period = 2; |
21 | char **watched_argv, *header; | 30 | unsigned cmdlen = 1; // 1 for terminal NUL |
31 | char *header = NULL; | ||
32 | char *cmd; | ||
33 | char *tmp; | ||
34 | char **p; | ||
22 | 35 | ||
23 | if (argc < 2) bb_show_usage(); | 36 | opt_complementary = "-1"; // at least one param please |
37 | opt = getopt32(argc, argv, "+dtn:", &tmp); | ||
38 | //if (opt & 0x1) // -d (ignore) | ||
39 | //if (opt & 0x2) // -t | ||
40 | if (opt & 0x4) period = xatou(tmp); | ||
41 | argv += optind; | ||
24 | 42 | ||
25 | get_terminal_width_height(STDOUT_FILENO, &width, 0); | 43 | p = argv; |
26 | header = xzalloc(width--); | 44 | while (*p) |
27 | 45 | cmdlen += strlen(*p++) + 1; | |
28 | /* don't use getopt, because it permutes the arguments */ | 46 | tmp = cmd = xmalloc(cmdlen); |
29 | ++argv; | 47 | while (*argv) { |
30 | if ((argc > 3) && argv[0][0] == '-' && argv[0][1] == 'n') { | 48 | tmp += sprintf(tmp, " %s", *argv); |
31 | period = xatou(argv[1]); | 49 | argv++; |
32 | argv += 2; | ||
33 | } | 50 | } |
34 | watched_argv = argv; | 51 | cmd++; // skip initial space |
35 | |||
36 | /* create header */ | ||
37 | len = snprintf(header, width, "Every %ds:", period); | ||
38 | while (*argv && len<width) | ||
39 | snprintf(header+len, width-len, " %s", *(argv++)); | ||
40 | 52 | ||
41 | while (1) { | 53 | while (1) { |
42 | char *thyme; | 54 | printf("\033[H\033[J"); |
43 | time_t t; | 55 | if (!(opt & 0x2)) { // no -t |
44 | 56 | int width, len; | |
45 | time(&t); | 57 | char *thyme; |
46 | thyme = ctime(&t); | 58 | time_t t; |
47 | len = strlen(thyme); | ||
48 | if (len < width) | ||
49 | header[width-len] = 0; | ||
50 | bb_printf("\033[H\033[J%s %s\n", header, thyme); | ||
51 | 59 | ||
52 | waitpid(xspawn(watched_argv),0,0); | 60 | get_terminal_width_height(STDOUT_FILENO, &width, 0); |
61 | header = xrealloc(header, width--); | ||
62 | // We pad with spaces entire length | ||
63 | snprintf(header, width, "Every %ds: %-*s", period, width, cmd); | ||
64 | time(&t); | ||
65 | thyme = ctime(&t); | ||
66 | len = strlen(thyme); | ||
67 | if (len < width) | ||
68 | strcpy(header + width - len, thyme); | ||
69 | puts(header); | ||
70 | } | ||
71 | fflush(stdout); | ||
72 | // TODO: 'real' watch pipes cmd's output to itself | ||
73 | // and does not allow it to overflow the screen | ||
74 | // (taking into account linewrap!) | ||
75 | system(cmd); | ||
53 | sleep(period); | 76 | sleep(period); |
54 | } | 77 | } |
55 | if (ENABLE_FEATURE_CLEAN_UP) | ||
56 | free(header); | ||
57 | } | 78 | } |
diff --git a/include/usage.h b/include/usage.h index f9597763e..9a6febf46 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -3296,11 +3296,12 @@ USE_FEATURE_START_STOP_DAEMON_FANCY( \ | |||
3296 | "\t-a\tLock all VTs" | 3296 | "\t-a\tLock all VTs" |
3297 | 3297 | ||
3298 | #define watch_trivial_usage \ | 3298 | #define watch_trivial_usage \ |
3299 | "[-n <seconds>] COMMAND..." | 3299 | "[-n <seconds>] [-t] COMMAND..." |
3300 | #define watch_full_usage \ | 3300 | #define watch_full_usage \ |
3301 | "Executes a program periodically.\n" \ | 3301 | "Executes a program periodically\n\n" \ |
3302 | "Options:\n" \ | 3302 | "Options:\n" \ |
3303 | "\t-n\tLoop period in seconds - default is 2" | 3303 | "\t-n\tLoop period in seconds - default is 2\n" |
3304 | "\t-t\tDon't print header" | ||
3304 | #define watch_example_usage \ | 3305 | #define watch_example_usage \ |
3305 | "$ watch date\n" \ | 3306 | "$ watch date\n" \ |
3306 | "Mon Dec 17 10:31:40 GMT 2000\n" \ | 3307 | "Mon Dec 17 10:31:40 GMT 2000\n" \ |