powertop: new applet
[oweals/busybox.git] / procps / watch.c
index 2ad0564cd7c226d44d3396a699471b9160186d53..36f71c469ad59fdef21f62b1b7b8c40546491e3b 100644 (file)
@@ -5,7 +5,7 @@
  * Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
  * Copyrigjt (C) Mar 16, 2003 Manuel Novoa III   (mjn3@codepoet.org)
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
 /* BB_AUDIT SUSv3 N/A */
 //
 // (procps 3.x and procps 2.x are forks, not newer/older versions of the same)
 
-int watch_main(int argc, char **argv);
-int watch_main(int argc, char **argv)
+int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int watch_main(int argc UNUSED_PARAM, char **argv)
 {
        unsigned opt;
        unsigned period = 2;
-       unsigned cmdlen = 1; // 1 for terminal NUL
-       char *header = NULL;
+       unsigned width, new_width;
+       char *header;
        char *cmd;
-       char *tmp;
-       char **p;
 
-       opt_complementary = "-1"; // at least one param please
-       opt = getopt32(argc, argv, "+dtn:", &tmp);
-       //if (opt & 0x1) // -d (ignore)
-       //if (opt & 0x2) // -t
-       if (opt & 0x4) period = xatou(tmp);
+#if 0 // maybe ENABLE_DESKTOP?
+       // procps3 compat - "echo TEST | watch cat" doesn't show TEST:
+       close(STDIN_FILENO);
+       xopen("/dev/null", O_RDONLY);
+#endif
+
+       opt_complementary = "-1:n+"; // at least one param; -n NUM
+       // "+": stop at first non-option (procps 3.x only)
+       opt = getopt32(argv, "+dtn:", &period);
        argv += optind;
 
-       p = argv;
-       while (*p)
-               cmdlen += strlen(*p++) + 1;
-       tmp = cmd = xmalloc(cmdlen);
-       while (*argv) {
-               tmp += sprintf(tmp, " %s", *argv);
-               argv++;
-       }
-       cmd++; // skip initial space
+       // watch from both procps 2.x and 3.x does concatenation. Example:
+       // watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
+       cmd = *argv;
+       while (*++argv)
+               cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
 
+       width = (unsigned)-1; // make sure first time new_width != width
+       header = NULL;
        while (1) {
-               printf("\033[H\033[J");
+               /* home; clear to the end of screen */
+               printf("\033[H""\033[J");
                if (!(opt & 0x2)) { // no -t
-                       int width, len;
-                       char *thyme;
+                       const unsigned time_len = sizeof("1234-67-90 23:56:89");
                        time_t t;
 
-                       get_terminal_width_height(STDOUT_FILENO, &width, 0);
-                       header = xrealloc(header, width--);
-                       // '%-*s' pads header with spaces to the full width
-                       snprintf(header, width, "Every %ds: %-*s", period, width, cmd);
+                       // STDERR_FILENO is procps3 compat:
+                       // "watch ls 2>/dev/null" does not detect tty size
+                       get_terminal_width_height(STDERR_FILENO, &new_width, NULL);
+                       if (new_width != width) {
+                               width = new_width;
+                               free(header);
+                               header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
+                       }
                        time(&t);
-                       thyme = ctime(&t);
-                       len = strlen(thyme);
-                       if (len < width)
-                               strcpy(header + width - len, thyme);
-                       puts(header);
+                       if (time_len < width)
+                               strftime(header + width - time_len, time_len,
+                                       "%Y-%m-%d %H:%M:%S", localtime(&t));
+
+                       // compat: empty line between header and cmd output
+                       printf("%s\n\n", header);
                }
-               fflush(stdout);
+               fflush_all();
                // TODO: 'real' watch pipes cmd's output to itself
                // and does not allow it to overflow the screen
                // (taking into account linewrap!)