Magellan Linux

Contents of /trunk/mkinitrd-magellan/busybox/procps/watch.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 984 - (show annotations) (download)
Sun May 30 11:32:42 2010 UTC (13 years, 11 months ago) by niro
File MIME type: text/plain
File size: 2453 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
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 tarball for details.
9 */
10
11 /* BB_AUDIT SUSv3 N/A */
12 /* BB_AUDIT GNU defects -- only option -n is supported. */
13
14 #include "libbb.h"
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)
25
26 int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
27 int watch_main(int argc UNUSED_PARAM, char **argv)
28 {
29 unsigned opt;
30 unsigned period = 2;
31 unsigned width, new_width;
32 char *header;
33 char *cmd;
34
35 #if 0 // maybe ENABLE_DESKTOP?
36 // procps3 compat - "echo TEST | watch cat" doesn't show TEST:
37 close(STDIN_FILENO);
38 xopen("/dev/null", O_RDONLY);
39 #endif
40
41 opt_complementary = "-1:n+"; // at least one param; -n NUM
42 // "+": stop at first non-option (procps 3.x only)
43 opt = getopt32(argv, "+dtn:", &period);
44 argv += optind;
45
46 // watch from both procps 2.x and 3.x does concatenation. Example:
47 // watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
48 cmd = *argv;
49 while (*++argv)
50 cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
51
52 width = (unsigned)-1; // make sure first time new_width != width
53 header = NULL;
54 while (1) {
55 printf("\033[H\033[J");
56 if (!(opt & 0x2)) { // no -t
57 const unsigned time_len = sizeof("1234-67-90 23:56:89");
58 time_t t;
59
60 // STDERR_FILENO is procps3 compat:
61 // "watch ls 2>/dev/null" does not detect tty size
62 get_terminal_width_height(STDERR_FILENO, &new_width, NULL);
63 if (new_width != width) {
64 width = new_width;
65 free(header);
66 header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
67 }
68 time(&t);
69 if (time_len < width)
70 strftime(header + width - time_len, time_len,
71 "%Y-%m-%d %H:%M:%S", localtime(&t));
72
73 // compat: empty line between header and cmd output
74 printf("%s\n\n", header);
75 }
76 fflush_all();
77 // TODO: 'real' watch pipes cmd's output to itself
78 // and does not allow it to overflow the screen
79 // (taking into account linewrap!)
80 system(cmd);
81 sleep(period);
82 }
83 return 0; // gcc thinks we can reach this :)
84 }