- add libbb function str_tolower to convert a string to lowercase.
[oweals/busybox.git] / coreutils / watch.c
index c8b16b908f14ad15915b08c950b26d50d2e81cac..60a4a71ceabd85d678b43438618d2dec9e89b817 100644 (file)
@@ -3,6 +3,7 @@
  * Mini watch implementation for busybox
  *
  * 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.
  */
 /* BB_AUDIT SUSv3 N/A */
 /* BB_AUDIT GNU defects -- only option -n is supported. */
 
-/* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
- *
- * Removed dependency on date_main(), added proper error checking, and
- * reduced size.
- */
-
 #include "busybox.h"
 
+// procps 2.0.18:
+// watch [-d] [-n seconds]
+//   [--differences[=cumulative]] [--interval=seconds] command
+//
+// procps-3.2.3:
+// watch [-dt] [-n seconds]
+//   [--differences[=cumulative]] [--interval=seconds] [--no-title] command
+//
+// (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 width, len;
+       unsigned opt;
        unsigned period = 2;
-       char **watched_argv, *header;
+       unsigned cmdlen = 1; // 1 for terminal NUL
+       char *header = NULL;
+       char *cmd;
+       char *tmp;
+       char **p;
 
-       if (argc < 2) bb_show_usage();
+       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);
+       argv += optind;
 
-       get_terminal_width_height(1, &width, 0);
-       header = xzalloc(width--);
-
-       /* don't use getopt, because it permutes the arguments */
-       ++argv;
-       if ((argc > 3) && !strcmp(*argv, "-n")) {
-               period = bb_xgetularg10_bnd(argv[1], 1, UINT_MAX);
-               argv += 2;
+       p = argv;
+       while (*p)
+               cmdlen += strlen(*p++) + 1;
+       tmp = cmd = xmalloc(cmdlen);
+       while (*argv) {
+               tmp += sprintf(tmp, " %s", *argv);
+               argv++;
        }
-       watched_argv = argv;
-
-       /* create header */
-
-       len = snprintf(header, width, "Every %ds:", period);
-       while (*argv && len<width)
-               snprintf(header+len, width-len, " %s", *(argv++));
+       cmd++; // skip initial space
 
        while (1) {
-               char *thyme;
-               time_t t;
-
-               time(&t);
-               thyme = ctime(&t);
-               len = strlen(thyme);
-               if (len < width) header[width-len] = 0;
-               
-               printf("\033[H\033[J%s %s\n", header, thyme);
+               printf("\033[H\033[J");
+               if (!(opt & 0x2)) { // no -t
+                       int width, len;
+                       char *thyme;
+                       time_t t;
 
-               waitpid(xspawn(watched_argv),0,0);
+                       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);
+                       time(&t);
+                       thyme = ctime(&t);
+                       len = strlen(thyme);
+                       if (len < width)
+                               strcpy(header + width - len, thyme);
+                       puts(header);
+               }
+               fflush(stdout);
+               // TODO: 'real' watch pipes cmd's output to itself
+               // and does not allow it to overflow the screen
+               // (taking into account linewrap!)
+               system(cmd);
                sleep(period);
        }
+       return 0; // gcc thinks we can reach this :)
 }