Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 2004 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

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 "busybox.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)
27 {
28 unsigned opt;
29 unsigned period = 2;
30 unsigned cmdlen = 1; // 1 for terminal NUL
31 char *header = NULL;
32 char *cmd;
33 char *tmp;
34 char **p;
35
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;
42
43 p = argv;
44 while (*p)
45 cmdlen += strlen(*p++) + 1;
46 tmp = cmd = xmalloc(cmdlen);
47 while (*argv) {
48 tmp += sprintf(tmp, " %s", *argv);
49 argv++;
50 }
51 cmd++; // skip initial space
52
53 while (1) {
54 printf("\033[H\033[J");
55 if (!(opt & 0x2)) { // no -t
56 int width, len;
57 char *thyme;
58 time_t t;
59
60 get_terminal_width_height(STDOUT_FILENO, &width, 0);
61 header = xrealloc(header, width--);
62 // '%-*s' pads header with spaces to the full width
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);
76 sleep(period);
77 }
78 return 0; // gcc thinks we can reach this :)
79 }