aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2003-06-08 10:50:55 +0000
committerDimitri Sokolyuk <demon@dim13.org>2003-06-08 10:50:55 +0000
commit15f68b8e1f357e17169f808a373d01638bfc0d85 (patch)
treed91903582b820d0baa3471ec0832e59e30c07446
parentfaa7db5a97695b45e574e5e40a5d1ec3060afe49 (diff)
using curses
-rw-r--r--Makefile4
-rw-r--r--watch.c48
2 files changed, 38 insertions, 14 deletions
diff --git a/Makefile b/Makefile
index 6012e8e..533ce7a 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,7 @@
PROG= watch
BINDIR= /usr/local/bin
-MAN= watch.1
+NOMAN= watch.1
+PDADD= ${LIBCURSES}
+LDADD= -lcurses
. include <bsd.prog.mk> \ No newline at end of file
diff --git a/watch.c b/watch.c
index 011bb7f..5efee5f 100644
--- a/watch.c
+++ b/watch.c
@@ -23,24 +23,35 @@
#include <unistd.h>
#include <string.h>
#include <time.h>
+#include <signal.h>
+#include <curses.h>
#define MAXBUF 255
static char copyright[] = "Copyright (C) 2003 demon (demon@vhost.dyndns.org)";
-static char version[] = "0.1";
+static char version[] = "0.2";
static char terms[] = "This program comes with ABSOLUTELY NO WARANTY.\n\
This is a free software, and you are welcome to redistribute it\n\
under certain conditions. See the file COPYING for deatails.";
extern char *__progname;
time_t tval;
+int die_flag;
-void usage(void);
+void usage();
+void die();
int main (int argc, char *argv[]) {
int i;
int period=5;
char ch;
+ char cmd[MAXBUF];
char buf[MAXBUF];
char curtime[30];
+ FILE *pipe;
+
+ signal(SIGINT, die);
+ signal(SIGTERM, die);
+ signal(SIGHUP, die);
+
while ((ch = getopt(argc, argv, "s:v")) != -1)
switch (ch) {
case 'v':
@@ -61,27 +72,34 @@ int main (int argc, char *argv[]) {
argv += optind;
if(*argv) {
- strcpy(buf, *argv);
+ strcpy(cmd, *argv);
while(*++argv) {
- strcat(buf, " ");
- strcat(buf, *argv);
+ strcat(cmd, " ");
+ strcat(cmd, *argv);
}
} else {
if(isatty(fileno(stdin)))
fprintf(stderr, "Command: ");
- (void)fgets(buf, sizeof(buf), stdin);
- buf[strlen(buf) - 1] = '\0';
+ (void)fgets(cmd, sizeof(cmd), stdin);
+ cmd[strlen(cmd) - 1] = '\0';
}
- if(strlen(buf)) {
- while(1) {
+ if(strlen(cmd)) {
+ initscr();
+ while(!die_flag) {
time(&tval);
- strcpy(curtime,ctime(&tval));
+ strcpy(curtime, ctime(&tval));
curtime[strlen(curtime) - 6] = '\0';
- printf("\e[H\e[2J%s\tEvery %ds: %s\n\n", curtime, period, buf);
- system(buf);
+ move(0,0);
+ printw("%s\tEvery %ds: %s\n\n", curtime, period, cmd);
+ pipe = popen(cmd, "r");
+ while(fgets(buf, sizeof(buf), pipe))
+ printw("%s",buf);
+ refresh();
+ pclose(pipe);
sleep(period);
}
+ endwin();
} else {
usage();
}
@@ -89,7 +107,11 @@ int main (int argc, char *argv[]) {
exit(0);
}
-void usage(void) {
+void usage() {
(void)fprintf(stderr, "Usage: %s [-s <seconds> | -v ] [command]\n", __progname);
exit (1);
}
+
+void die() {
+ die_flag=1;
+}