source: MondoRescue/branches/3.2/mindi-busybox/procps/watch.c@ 3232

Last change on this file since 3232 was 3232, checked in by Bruno Cornec, 10 years ago
  • Update mindi-busybox to 1.21.1
  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini watch implementation for busybox
4 *
5 * Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
6 * Copyrigjt (C) Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
7 *
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 */
10
11/* BB_AUDIT SUSv3 N/A */
12/* BB_AUDIT GNU defects -- only option -n is supported. */
13
14//usage:#define watch_trivial_usage
15//usage: "[-n SEC] [-t] PROG ARGS"
16//usage:#define watch_full_usage "\n\n"
17//usage: "Run PROG periodically\n"
18//usage: "\n -n Loop period in seconds (default 2)"
19//usage: "\n -t Don't print header"
20//usage:
21//usage:#define watch_example_usage
22//usage: "$ watch date\n"
23//usage: "Mon Dec 17 10:31:40 GMT 2000\n"
24//usage: "Mon Dec 17 10:31:42 GMT 2000\n"
25//usage: "Mon Dec 17 10:31:44 GMT 2000"
26
27#include "libbb.h"
28
29// procps 2.0.18:
30// watch [-d] [-n seconds]
31// [--differences[=cumulative]] [--interval=seconds] command
32//
33// procps-3.2.3:
34// watch [-dt] [-n seconds]
35// [--differences[=cumulative]] [--interval=seconds] [--no-title] command
36//
37// (procps 3.x and procps 2.x are forks, not newer/older versions of the same)
38
39int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
40int watch_main(int argc UNUSED_PARAM, char **argv)
41{
42 unsigned opt;
43 unsigned period = 2;
44 unsigned width, new_width;
45 char *header;
46 char *cmd;
47
48#if 0 // maybe ENABLE_DESKTOP?
49 // procps3 compat - "echo TEST | watch cat" doesn't show TEST:
50 close(STDIN_FILENO);
51 xopen("/dev/null", O_RDONLY);
52#endif
53
54 opt_complementary = "-1:n+"; // at least one param; -n NUM
55 // "+": stop at first non-option (procps 3.x only)
56 opt = getopt32(argv, "+dtn:", &period);
57 argv += optind;
58
59 // watch from both procps 2.x and 3.x does concatenation. Example:
60 // watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
61 cmd = *argv;
62 while (*++argv)
63 cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
64
65 width = (unsigned)-1; // make sure first time new_width != width
66 header = NULL;
67 while (1) {
68 /* home; clear to the end of screen */
69 printf("\033[H""\033[J");
70 if (!(opt & 0x2)) { // no -t
71 const unsigned time_len = sizeof("1234-67-90 23:56:89");
72 time_t t;
73
74 // STDERR_FILENO is procps3 compat:
75 // "watch ls 2>/dev/null" does not detect tty size
76 get_terminal_width_height(STDERR_FILENO, &new_width, NULL);
77 if (new_width != width) {
78 width = new_width;
79 free(header);
80 header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
81 }
82 time(&t);
83 if (time_len < width)
84 strftime(header + width - time_len, time_len,
85 "%Y-%m-%d %H:%M:%S", localtime(&t));
86
87 // compat: empty line between header and cmd output
88 printf("%s\n\n", header);
89 }
90 fflush_all();
91 // TODO: 'real' watch pipes cmd's output to itself
92 // and does not allow it to overflow the screen
93 // (taking into account linewrap!)
94 system(cmd);
95 sleep(period);
96 }
97 return 0; // gcc thinks we can reach this :)
98}
Note: See TracBrowser for help on using the repository browser.