home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume24 / watch / watch.c < prev   
Encoding:
C/C++ Source or Header  |  1991-06-05  |  2.1 KB  |  112 lines

  1. #include <curses.h>
  2. #include <stdio.h>
  3. #include <signal.h>
  4. #include <sys/fcntl.h>
  5.  
  6. #ifdef ATT
  7. #define    crmode()    cbreak()
  8. #define bzero(s,n)    memset(s,0,n)
  9. #endif
  10.  
  11. void    die();
  12. extern    FILE         *popen();
  13. extern    int         pclose();
  14. extern    long        time();
  15. extern    char        *ctime();
  16.  
  17. main(argc, argv)
  18. int argc;
  19. char *argv[];
  20. {
  21.     int hor = 1, ver = 0;
  22.     FILE *piper;
  23.     char buf[180];
  24.     char cmd[128];
  25.     int count = 1;
  26.     long timer;
  27.     int nsecs = 2;
  28.  
  29.     if (argc < 2) {
  30.         fprintf(stderr, "Usage: %s command [args]\n", argv[0]);
  31.         exit(1);
  32.     } /* if */
  33.  
  34.     /* If -n is specified, convert the next argument to the numver
  35.      * for the number of seconds
  36.      */
  37.     if (strcmp(argv[1], "-n") == 0) {
  38.         nsecs = atoi(argv[2]);
  39.         count = 3;
  40.         if (nsecs == 0 || argc < 3) {
  41.             fprintf(stderr, "Usage: %s command [args]\n", argv[0]);
  42.             exit(1);
  43.         } /* if */
  44.     } /* if */
  45.  
  46.     /* Build command string to give to popen */
  47.     (void)bzero(cmd, sizeof(cmd));
  48.     strcpy(cmd, argv[count]);
  49.     while (++count < argc) {
  50.         strcat(cmd, " ");
  51.         strcat(cmd, argv[count]);
  52.     } /* while */
  53.  
  54.     /* Catch keyboard interrupts so we can
  55.      * put tty back in a sane state 
  56.      */
  57.     (void) signal(SIGINT, die);
  58.     (void) signal(SIGTERM, die);
  59.     (void) signal(SIGHUP, die);
  60.  
  61.     /* Set up tty for curses use */
  62.     initscr();
  63.     nonl();
  64.     noecho();
  65.     crmode();
  66.  
  67.     while(1) { /* loop forever */
  68.  
  69.         /* Put up time interval and current time */
  70.         move(hor, ver);
  71.         time(&timer);
  72.         printw("Every %d seconds\t\t%s\t\t%s", nsecs, cmd, ctime(&timer));
  73.         hor = 3;
  74.  
  75.         /* Open pipe to command */
  76.         if ((piper = popen(cmd, "r")) == (FILE *)NULL) {
  77.             perror("popen");
  78.             exit(2);
  79.         } /* if */
  80.  
  81.  
  82.         /* Read in output from the command and make sure 
  83.          * that it will fit on 1 screen 
  84.          */
  85.         while ((fgets(buf, sizeof(buf), piper) != NULL) && hor < LINES) {
  86.             buf[COLS-1] = '\0';
  87.             mvaddstr(hor, ver, buf);
  88.             hor++;
  89.         } /* while */
  90.         refresh();
  91.  
  92.         sleep(nsecs);
  93.         hor = 1; /* Go back to the top of screen */
  94.         pclose(piper);
  95.  
  96.     } /* while */
  97. } /* main */
  98.  
  99.  
  100. void
  101. die()
  102. {
  103.     /* Ignore interrupts while we clear the screen
  104.      * and reset the tty 
  105.      */
  106.     (void) signal(SIGINT, SIG_IGN);
  107.     clear(); 
  108.     refresh(); 
  109.     endwin();
  110.     exit(0);
  111. } /* die */
  112.