[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [BusyBox] bug#1224: [PATCH] - New applet watch for busybox
From:       Michael Habermann <MHabermann () gmx ! de>
Date:       2001-12-25 5:45:08
[Download RAW message or body]

Package: busybox
Version: busybox-unstable-20011225.tar.gz
Severity: wishlist

["watch.patch" (application/octet-stream)]

diff -Nur --exclude usage.h.org busybox_org/include/applets.h busybox/include/applets.h
--- busybox_org/include/applets.h	Thu Dec  6 23:16:43 2001
+++ busybox/include/applets.h	Tue Dec 25 20:15:41 2001
@@ -488,6 +488,9 @@
 #ifdef CONFIG_VI
 	APPLET(vi, vi_main, _BB_DIR_BIN)
 #endif
+#ifdef CONFIG_WATCH
+	APPLET(watch, watch_main, _BB_DIR_BIN)
+#endif
 #ifdef CONFIG_WATCHDOG
 	APPLET(watchdog, watchdog_main, _BB_DIR_SBIN)
 #endif
diff -Nur --exclude usage.h.org busybox_org/include/usage.h busybox/include/usage.h
--- busybox_org/include/usage.h	Tue Dec 18 22:06:02 2001
+++ busybox/include/usage.h	Tue Dec 25 20:15:41 2001
@@ -1907,6 +1907,18 @@
 	"Options:\n" \
 	"\t-R\tRead-only- do not write to the file." 
 
+#define watch_trivial_usage \
+	"[-n <seconds>] COMMAND..."
+#define watch_full_usage \
+	"Executes a program periodically.\n" \
+	"Options:\n" \
+	"\t-n\tLoop period in seconds - default is 2." 
+#define watch_example_usage \
+	"$ watch date\n" \
+	"Mon Dec 17 10:31:40 GMT 2000\n" \
+	"Mon Dec 17 10:31:42 GMT 2000\n" \
+	"Mon Dec 17 10:31:44 GMT 2000" 
+
 #define watchdog_trivial_usage \
 	"DEV"
 #define watchdog_full_usage \
diff -Nur --exclude usage.h.org busybox_org/shellutils/Makefile busybox/shellutils/Makefile
--- busybox_org/shellutils/Makefile	Thu Dec  6 23:16:43 2001
+++ busybox/shellutils/Makefile	Tue Dec 25 20:15:41 2001
@@ -46,6 +46,7 @@
 obj-$(CONFIG_TTY)		+= tty.o
 obj-$(CONFIG_UNAME)		+= uname.o
 obj-$(CONFIG_USLEEP)		+= usleep.o
+obj-$(CONFIG_WATCH)		+= watch.o
 obj-$(CONFIG_WHOAMI)		+= whoami.o
 obj-$(CONFIG_YES)		+= yes.o
 
diff -Nur --exclude usage.h.org busybox_org/shellutils/config.in busybox/shellutils/config.in
--- busybox_org/shellutils/config.in	Thu Dec  6 23:16:43 2001
+++ busybox/shellutils/config.in	Tue Dec 25 20:15:41 2001
@@ -35,6 +35,7 @@
 bool 'tty'		CONFIG_TTY
 bool 'uname'		CONFIG_UNAME
 bool 'usleep'		CONFIG_USLEEP
+bool 'watch'		CONFIG_WATCH
 bool 'whoami'		CONFIG_WHOAMI
 bool 'yes'		CONFIG_YES
 
diff -Nur --exclude usage.h.org busybox_org/shellutils/watch.c busybox/shellutils/watch.c
--- busybox_org/shellutils/watch.c	Thu Jan  1 07:30:00 1970
+++ busybox/shellutils/watch.c	Tue Dec 25 20:15:41 2001
@@ -0,0 +1,106 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini watch implementation for busybox
+ *
+ * Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
+ *
+ * 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
+ *
+ */
+
+/* getopt not needed */
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include "busybox.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"
+    "\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;
+
+  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;
+    }
+  }
+
+
+  //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," ");
+  }
+
+  //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);
+
+  while (1) {
+    printf("%s%s",clrscr,header);
+    date_main(1,(char**)date_argv);
+    printf("\n");
+
+    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");
+    }
+  }
+
+
+  return EXIT_SUCCESS;
+}
+
+


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic