* 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)
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
-/* getopt not needed */
+/* BB_AUDIT SUSv3 N/A */
+/* BB_AUDIT GNU defects -- only option -n is supported. */
-#include <stdio.h>
-#include <errno.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-#include "busybox.h"
+#include "libbb.h"
-extern int watch_main(int argc, char **argv)
-{
- const char date_argv[2][10] = { "date", "" };
- const char clrscr[] =
- "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
- const int header_len = 40;
- char header[header_len + 1];
- int period = 2;
- char **cargv;
- int cargc;
- pid_t pid;
- int old_stdout;
- int i;
+// 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)
- if (argc < 2) {
- show_usage();
- } else {
- cargv = argv + 1;
- cargc = argc - 1;
-
- /* don't use getopt, because it permutes the arguments */
- if (argc >= 3 && !strcmp(argv[1], "-n")) {
- period = strtol(argv[2], NULL, 10);
- if (period < 1)
- show_usage();
- cargv += 2;
- cargc -= 2;
- }
- }
+int watch_main(int argc, char **argv);
+int watch_main(int argc, char **argv)
+{
+ unsigned opt;
+ unsigned period = 2;
+ unsigned cmdlen = 1; // 1 for terminal NUL
+ char *header = NULL;
+ 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);
+ argv += optind;
- /* create header */
- snprintf(header, header_len, "Every %ds: ", period);
- for (i = 0; i < cargc && (strlen(header) + strlen(cargv[i]) < header_len);
- i++) {
- strcat(header, cargv[i]);
- strcat(header, " ");
+ p = argv;
+ while (*p)
+ cmdlen += strlen(*p++) + 1;
+ tmp = cmd = xmalloc(cmdlen);
+ while (*argv) {
+ tmp += sprintf(tmp, " %s", *argv);
+ argv++;
}
-
- /* fill with blanks */
- for (i = strlen(header); i < header_len; i++)
- header[i] = ' ';
-
- header[header_len - 1] = '\0';
-
-
- /* thanks to lye, who showed me how to redirect stdin/stdout */
- old_stdout = dup(1);
+ cmd++; // skip initial space
while (1) {
- printf("%s%s", clrscr, header);
- date_main(1, (char **) date_argv);
- printf("\n");
+ printf("\033[H\033[J");
+ if (!(opt & 0x2)) { // no -t
+ int width, len;
+ char *thyme;
+ time_t t;
- pid = vfork(); /* vfork, because of ucLinux */
- if (pid > 0) {
- //parent
- wait(0);
- sleep(period);
- } else if (0 == pid) {
- //child
- close(1);
- dup(old_stdout);
- if (execvp(*cargv, cargv))
- error_msg_and_die("Couldn't run command\n");
- } else {
- error_msg_and_die("Couldn't vfork\n");
+ 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 EXIT_SUCCESS;
+ return 0; // gcc thinks we can reach this :)
}