[net.sources] modified clock.c

jhall@ihuxu.UUCP (John R. Hall) (02/18/84)

Since I continue to get a fair number of requests for the modified
version of clock.c, I'm posting it here.
--John R. Hall, ihnp4!ihuxu!jhall

Summary of changes:
	- added constant "BSD". Uncomment the #define at line 36
	  before compiling under Berkeley, leave as-is for System V.
	  Look for the #ifdef's in the code to see the differences.
	- uncomment the #define at line 37 to print clock in standout mode,
	  leave as-is to print in nonstandout mode (standout is ugly-
	  underlining on my terminal)
	- changed code to call initscr before any other screen routines
	  (to discourage core dumps)
	- removed unnecessary save/restore of tty states.
	  initscr & endwin calls already save/restore tty states.
Open problems:
	- I didn't change the definition of in_char to int
	- Big letters print out 12-hr time, yet date line in
	  small letters below print 24-hr time.  My officemate points
	  out that it's easy to get the big letters in 24-hr time by
	  commenting out the code around line 330.
	- Would be nice to include a call to wrefresh(curscr) if user
	  types control-L so that the screen could be replotted if
	  trashed.
	- Curses under System V uses a different terminal capability
	  database (not /etc/termcap).  Your terminal may not be defined
	  in the new terminfo database.
---
/*   Program:         clock.
 *   Author:          Jim Earenluindil Trethewey.
 *   Version:         1.03.
 *   Index:           CSD255.
 *   Date:            21-Oct-1983.
 *   Last-edit:       07-Feb-1984.
 *		      2/9/84 j.r.hall
 *   Language:        C.
 *   Ppn:             /user/serial/flamer.
 *   For:             Intel Corporation, Hillsboro, Oregon.
 *
 *   This program will print a pretty clock on the terminal until the user 
 *   hits a key on the keyboard.
 *
 *   Usage:  clock [<refresh_time>]
 *
 *       <refresh_time> is the period (in seconds) at which the screen
 *                      display is refreshed.  Note that the first refresh
 *                      may come sooner, the program attempts to synchronize
 *                      itself on even minutes.  If not specified, defaults
 *                      to 60 (every minute).
 *
 *   Compile:  BSD: cc -o clock clock.c -lcurses -ltermcap -ljobs
 *             System V Release 2.0: cc -o clock clock.c -lcurses -ltermcap
 *
 *   If not running under BSD, change all sigset's to signal's.
 *
 *   You may want to substitute the characters used to make the numbers
 *   from #'s to something else depending on your terminal.  If your
 *   terminal has 'blotch' characters, they are most pleasing.  (OK, so
 *   we ought to have the termcap 'blotch=<char>' capability, and be able
 *   to look it up.)
 *
 */

/* #define BSD	*/	/* uncomment this line if running BSD */
/* #define STAND */	/* uncomment this line to print in standout mode */
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <curses.h>

#ifdef BSD
#include <ident.h>		/* system name stored in "myname" string */
#else
#include <sys/utsname.h>	/* define structure to store uname info */
struct utsname unstr, *un;	/* space to store uname info */
char	myname[40];		/* string to store system name */
#endif

WINDOW     *scr;		/* screen wher large numbers displayed */
int        done;
char       in_char;


int        refresh_time;
struct tm  *localtime ();
struct tm  *loc_time;

pr_colon (col)

   int col;

   {
      wmove (scr,  6, col);        wprintw (scr, "   ");
      wmove (scr,  7, col);        wprintw (scr, "   ");
      wmove (scr,  8, col);        wprintw (scr, "   ");
      wmove (scr,  9, col);        wprintw (scr, " # ");
      wmove (scr, 10, col);        wprintw (scr, "###");
      wmove (scr, 11, col);        wprintw (scr, " # ");
      wmove (scr, 12, col);        wprintw (scr, "   ");
      wmove (scr, 13, col);        wprintw (scr, "   ");
      wmove (scr, 14, col);        wprintw (scr, " # ");
      wmove (scr, 15, col);        wprintw (scr, "###");
      wmove (scr, 16, col);        wprintw (scr, " # ");
      wmove (scr, 17, col);        wprintw (scr, "   ");
      wmove (scr, 18, col);        wprintw (scr, "   ");
      wmove (scr, 19, col);        wprintw (scr, "   ");
   }

pr_space ()
   {
      int i;

      for (i = 6; i < 20; i++)
	 {
	    wmove (scr, i, 9);
	    wprintw (scr, "                                   ");
	    wmove (scr, i, 42);
	    wprintw (scr, "                                   ");
         }
   }

pr_big (num, col)

   int num;
   int col;

   {
      if (num == 0) 
	 {
	    wmove (scr,  6, col);	wprintw (scr, "   ######   ");
	    wmove (scr,  7, col);	wprintw (scr, " ########## ");
	    wmove (scr,  8, col);	wprintw (scr, "####    ####");
	    wmove (scr,  9, col);	wprintw (scr, "###      ###");
	    wmove (scr, 10, col);	wprintw (scr, "###      ###");
	    wmove (scr, 11, col);	wprintw (scr, "###      ###");
	    wmove (scr, 12, col);	wprintw (scr, "###      ###");
	    wmove (scr, 13, col);	wprintw (scr, "###      ###");
	    wmove (scr, 14, col);	wprintw (scr, "###      ###");
	    wmove (scr, 15, col);	wprintw (scr, "###      ###");
	    wmove (scr, 16, col);	wprintw (scr, "###      ###");
	    wmove (scr, 17, col);	wprintw (scr, "####    ####");
	    wmove (scr, 18, col);	wprintw (scr, " ########## ");
	    wmove (scr, 19, col);	wprintw (scr, "   ######   ");
         }
      else if (num == 1)
	 {
	    wmove (scr,  6, col);	wprintw (scr, "      ##    ");
	    wmove (scr,  7, col);	wprintw (scr, "     ###    ");
	    wmove (scr,  8, col);	wprintw (scr, "    ####    ");
	    wmove (scr,  9, col);	wprintw (scr, "   #####    ");
	    wmove (scr, 10, col);	wprintw (scr, "     ###    ");
	    wmove (scr, 11, col);	wprintw (scr, "     ###    ");
	    wmove (scr, 12, col);	wprintw (scr, "     ###    ");
	    wmove (scr, 13, col);	wprintw (scr, "     ###    ");
	    wmove (scr, 14, col);	wprintw (scr, "     ###    ");
	    wmove (scr, 15, col);	wprintw (scr, "     ###    ");
	    wmove (scr, 16, col);	wprintw (scr, "     ###    ");
	    wmove (scr, 17, col);	wprintw (scr, "     ###    ");
	    wmove (scr, 18, col);	wprintw (scr, "   #######  ");
	    wmove (scr, 19, col);	wprintw (scr, "   #######  ");
         }
      else if (num == 2)
	 {
	    wmove (scr,  6, col);	wprintw (scr, "  ########  ");
	    wmove (scr,  7, col);	wprintw (scr, " ########## ");
	    wmove (scr,  8, col);	wprintw (scr, "###     ####");
	    wmove (scr,  9, col);	wprintw (scr, "###      ###");
	    wmove (scr, 10, col);	wprintw (scr, "         ###");
	    wmove (scr, 11, col);	wprintw (scr, "         ###");
	    wmove (scr, 12, col);	wprintw (scr, "         ###");
	    wmove (scr, 13, col);	wprintw (scr, "        ### ");
	    wmove (scr, 14, col);	wprintw (scr, "       ###  ");
	    wmove (scr, 15, col);	wprintw (scr, "     ####   ");
	    wmove (scr, 16, col);	wprintw (scr, "   ####     ");
	    wmove (scr, 17, col);	wprintw (scr, " ####       ");
	    wmove (scr, 18, col);	wprintw (scr, "############");
	    wmove (scr, 19, col);	wprintw (scr, "############");
         }
      else if (num == 3)
	 {
	    wmove (scr,  6, col);	wprintw (scr, "  ########  ");
	    wmove (scr,  7, col);	wprintw (scr, " ########## ");
	    wmove (scr,  8, col);	wprintw (scr, "###     ####");
	    wmove (scr,  9, col);	wprintw (scr, "###      ###");
	    wmove (scr, 10, col);	wprintw (scr, "         ###");
	    wmove (scr, 11, col);	wprintw (scr, "         ###");
	    wmove (scr, 12, col);	wprintw (scr, "    ####### ");
	    wmove (scr, 13, col);	wprintw (scr, "    ####### ");
	    wmove (scr, 14, col);	wprintw (scr, "         ###");
	    wmove (scr, 15, col);	wprintw (scr, "         ###");
	    wmove (scr, 16, col);	wprintw (scr, "###      ###");
	    wmove (scr, 17, col);	wprintw (scr, "###     ####");
	    wmove (scr, 18, col);	wprintw (scr, " ########## ");
	    wmove (scr, 19, col);	wprintw (scr, "  ########  ");
         }
      else if (num == 4)
	 {
	    wmove (scr,  6, col);	wprintw (scr, "       ###  ");
	    wmove (scr,  7, col);	wprintw (scr, "      ####  ");
	    wmove (scr,  8, col);	wprintw (scr, "     #####  ");
	    wmove (scr,  9, col);	wprintw (scr, "    ######  ");
	    wmove (scr, 10, col);	wprintw (scr, "   ### ###  ");
	    wmove (scr, 11, col);	wprintw (scr, "  ###  ###  ");
	    wmove (scr, 12, col);	wprintw (scr, " ###   ###  ");
	    wmove (scr, 13, col);	wprintw (scr, "############");
	    wmove (scr, 14, col);	wprintw (scr, "############");
	    wmove (scr, 15, col);	wprintw (scr, "       ###  ");
	    wmove (scr, 16, col);	wprintw (scr, "       ###  ");
	    wmove (scr, 17, col);	wprintw (scr, "       ###  ");
	    wmove (scr, 18, col);	wprintw (scr, "       ###  ");
	    wmove (scr, 19, col);	wprintw (scr, "       ###  ");
         }
      else if (num == 5)
	 {
	    wmove (scr,  6, col);	wprintw (scr, "############");
	    wmove (scr,  7, col);	wprintw (scr, "############");
	    wmove (scr,  8, col);	wprintw (scr, "###         ");
	    wmove (scr,  9, col);	wprintw (scr, "###         ");
	    wmove (scr, 10, col);	wprintw (scr, "### ######  ");
	    wmove (scr, 11, col);	wprintw (scr, "########### ");
	    wmove (scr, 12, col);	wprintw (scr, "###     ####");
	    wmove (scr, 13, col);	wprintw (scr, "         ###");
	    wmove (scr, 14, col);	wprintw (scr, "         ###");
	    wmove (scr, 15, col);	wprintw (scr, "         ###");
	    wmove (scr, 16, col);	wprintw (scr, "###      ###");
	    wmove (scr, 17, col);	wprintw (scr, "###     ####");
	    wmove (scr, 18, col);	wprintw (scr, " ########## ");
	    wmove (scr, 19, col);	wprintw (scr, "  ########  ");
         }
      else if (num == 6)
	 {
	    wmove (scr,  6, col);	wprintw (scr, "       ###  ");
	    wmove (scr,  7, col);	wprintw (scr, "      ###   ");
	    wmove (scr,  8, col);	wprintw (scr, "     ###    ");
	    wmove (scr,  9, col);	wprintw (scr, "    ###     ");
	    wmove (scr, 10, col);	wprintw (scr, "   ###      ");
	    wmove (scr, 11, col);	wprintw (scr, "  ###       ");
	    wmove (scr, 12, col);	wprintw (scr, " #########  ");
	    wmove (scr, 13, col);	wprintw (scr, "########### ");
	    wmove (scr, 14, col);	wprintw (scr, "###      ###");
	    wmove (scr, 15, col);	wprintw (scr, "###      ###");
	    wmove (scr, 16, col);	wprintw (scr, "###      ###");
	    wmove (scr, 17, col);	wprintw (scr, "###      ###");
	    wmove (scr, 18, col);	wprintw (scr, " ########## ");
	    wmove (scr, 19, col);	wprintw (scr, "   ######   ");
         }
      else if (num == 7)
	 {
	    wmove (scr,  6, col);	wprintw (scr, "############");
	    wmove (scr,  7, col);	wprintw (scr, "############");
	    wmove (scr,  8, col);	wprintw (scr, "###      ###");
	    wmove (scr,  9, col);	wprintw (scr, "         ###");
	    wmove (scr, 10, col);	wprintw (scr, "         ###");
	    wmove (scr, 11, col);	wprintw (scr, "        ### ");
	    wmove (scr, 12, col);	wprintw (scr, "       ###  ");
	    wmove (scr, 13, col);	wprintw (scr, "      ###   ");
	    wmove (scr, 14, col);	wprintw (scr, "     ###    ");
	    wmove (scr, 15, col);	wprintw (scr, "    ###     ");
	    wmove (scr, 16, col);	wprintw (scr, "   ###      ");
	    wmove (scr, 17, col);	wprintw (scr, "  ###       ");
	    wmove (scr, 18, col);	wprintw (scr, " ###        ");
	    wmove (scr, 19, col);	wprintw (scr, "###         ");
         }
      else if (num == 8)
	 {
	    wmove (scr,  6, col);	wprintw (scr, "   ######   ");
	    wmove (scr,  7, col);	wprintw (scr, " ########## ");
	    wmove (scr,  8, col);	wprintw (scr, "###      ###");
	    wmove (scr,  9, col);	wprintw (scr, "###      ###");
	    wmove (scr, 10, col);	wprintw (scr, "###      ###");
	    wmove (scr, 11, col);	wprintw (scr, "###      ###");
	    wmove (scr, 12, col);	wprintw (scr, " ########## ");
	    wmove (scr, 13, col);	wprintw (scr, " ########## ");
	    wmove (scr, 14, col);	wprintw (scr, "###      ###");
	    wmove (scr, 15, col);	wprintw (scr, "###      ###");
	    wmove (scr, 16, col);	wprintw (scr, "###      ###");
	    wmove (scr, 17, col);	wprintw (scr, "###      ###");
	    wmove (scr, 18, col);	wprintw (scr, " ########## ");
	    wmove (scr, 19, col);	wprintw (scr, "   ######   ");
         }
      else if (num == 9)
	 {
	    wmove (scr,  6, col);	wprintw (scr, "   ######   ");
	    wmove (scr,  7, col);	wprintw (scr, " ########## ");
	    wmove (scr,  8, col);	wprintw (scr, "###      ###");
	    wmove (scr,  9, col);	wprintw (scr, "###      ###");
	    wmove (scr, 10, col);	wprintw (scr, "###      ###");
	    wmove (scr, 11, col);	wprintw (scr, "###      ###");
	    wmove (scr, 12, col);	wprintw (scr, " ###########");
	    wmove (scr, 13, col);	wprintw (scr, "  ######### ");
	    wmove (scr, 14, col);	wprintw (scr, "       ###  ");
	    wmove (scr, 15, col);	wprintw (scr, "      ###   ");
	    wmove (scr, 16, col);	wprintw (scr, "     ###    ");
	    wmove (scr, 17, col);	wprintw (scr, "    ###     ");
	    wmove (scr, 18, col);	wprintw (scr, "   ###      ");
	    wmove (scr, 19, col);	wprintw (scr, "  ###       ");
         }
   }

big_nums (hrs, mins)

   int hrs, mins;

   {
      int h1, h2, m1, m2;

      h1 = hrs / 10;
      h2 = hrs % 10;
      m1 = mins / 10;
      m2 = mins % 10;
      
      if (h1 != 0)
	 {
	    pr_space ();
	    pr_big (h1, 9);
            pr_big (h2, 24);
            pr_colon (39);
            pr_big (m1, 45);
            pr_big (m2, 60);
         }
      else
	 {
	    pr_space ();
	    pr_big (h2, 16);
	    pr_colon (31);
	    pr_big (m1, 37);
	    pr_big (m2, 52);
         }
   }

time_update ()
   {
      long clock;
      char time_str [27];
      int t_hour, t_min, t_sec;
      int delta;

#ifdef BSD
      sigset (SIGALRM, time_update);
#else
      signal (SIGALRM, time_update);
#endif

      wmove (scr, 3, 0);
      wclrtoeol (scr);
      wmove (scr, 3, (COLS / 2 - strlen (myname) / 2));
      wprintw (scr, myname);

      time (&clock);
      wmove (scr, LINES - 2, 0);
      wclrtoeol (scr);
      strcpy (time_str, ctime (&clock));
      wmove (scr, LINES - 2, (COLS / 2 - strlen (time_str) / 2));
      wprintw (scr, time_str); 

      loc_time = localtime (&clock);
      t_hour = loc_time->tm_hour;
      if (t_hour > 12)
	 {
	    t_hour = t_hour - 12;
         }
      if (t_hour == 0)
	 {
	    t_hour = 12;
         }
      t_min = loc_time->tm_min;
      big_nums (t_hour, t_min);
      t_sec = loc_time->tm_sec;

      wmove (scr, LINES - 1, 0);
      wrefresh (scr);
      delta = refresh_time - (t_sec % refresh_time);
      if (delta > 0)
	 {
	    alarm (delta);
         }
      else
	 {
            alarm (refresh_time);
         }
   }

on_intr ()
   {
      done = TRUE;
   }

main(argc, argv)

   int argc;
   char *argv [];

   {
      if (argc == 1)
	 {
	    refresh_time = 60;
         }
      else if (argc == 2)
	 {
	    refresh_time = atoi (argv [1]);
         }
      else
	 {
	    printf ("Usage: %s [<refresh_time>]\n", argv [0]);
	    exit (0);
         }
	initscr();		/* init screen routines first */

#ifdef	BSD
      crmode ();
#else
      cbreak ();
      un = &unstr;
      uname(un);		/* fills structure with system name */
      strcpy(myname, un->sysname); /* current UNIX system */
      strcat(myname, "  ");
      strcat(myname, un->machine); /* machine hardware type (vax-780) */
#endif

      noecho ();
      nonl();
      scr = newwin (LINES, COLS, 0, 0);
      scrollok (scr, FALSE);
      leaveok (scr, FALSE);
#ifdef BSD
      sigset (SIGPIPE, SIG_IGN);
      sigset (SIGALRM, time_update);
      sigset (SIGINT, on_intr);
#else
      signal (SIGPIPE, SIG_IGN);
      signal (SIGALRM, time_update);
      signal (SIGINT, on_intr);
#endif

      wclear (scr);
#ifdef STAND
      wstandout (scr);
#endif
      done = FALSE;
      time_update ();
      while (!done)
	 {
	    in_char = getchar ();
	    if ((in_char != EOF) && (in_char != NULL))
	       {
		  done = TRUE;
               }
         }
      wclear (scr);
      wmove (scr, LINES - 1, 0);
      wclrtoeol (scr);
      wrefresh (scr);
#ifdef STAND
      wstandend (scr);
#endif
      endwin ();		/* restores tty state */
      exit(0);
   }
-- 
--John R. Hall, ...ihnp4!ihuxu!jhall

nrf@whuxle.UUCP (02/18/84)

#R:ihuxu:-24300:whuxle:21600001:000:477
whuxle!nrf    Feb 18 13:03:00 1984

Those with 3Bs (and hopefully the author) will benefit from the following
tidbit:

Some machines, such as 3Bs, don't support signed chars.  so if you do the
following:

	char x;

	x=getchar();

	if( x == EOF ) {
		do something;
	}

x will NEVER == EOF on a such a machine. the net result is that in order
for clock to work on a 3b, one must do the following:

	/char in_char/s/char/int/


Neal Fildes, A.T.&T. - Bell Laboratories Whippany, NJ, U.S.A.
whuxlg!nrf / 201-386-6493

-- 
Neal Fildes, A.T.&T. - Bell Laboratories Whippany, NJ, U.S.A.
whuxlg!nrf / 201-386-6493