home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2405 < prev    next >
Internet Message Format  |  1990-12-28  |  4KB

  1. From: jak@sactoh0.SAC.CA.US (Jay A. Konigsberg)
  2. Newsgroups: alt.sources
  3. Subject: A simple date/time program for Wyse terminals
  4. Message-ID: <4533@sactoh0.SAC.CA.US>
  5. Date: 25 Dec 90 17:05:42 GMT
  6.  
  7. The following is a clock that resides in the F8 function key label
  8. (the date goes into F7).
  9.  
  10. The features are:
  11.  
  12. 1) Clock will sync to change time with the system (to the second).
  13. 2) The date is displayed on startup and again when the time is 00:00
  14.    (in case you left your terminal logged in overnight).
  15. 3) SIGQUIT (logout) and SIGTERM (kill -15) will clear the clock from
  16.    the labels (too bad about SIGKILL :-( ).
  17. 4) The display is done with write(2) to avoid system buffering (using
  18.    puts(3S) or printf(3S) with clock sometimes results in the time
  19.    being displayed across the screen - yes I could have used setbuf(3S)
  20.    and fflush(3S), but I chose write(2) ).
  21. 5) The ESC codes can be easily changed and the code recomplied.
  22.  
  23. The bugs are:
  24.  
  25. 1) No command line options (i.e. you can't turn the date or time off).
  26. 2) Only Wyse ESC codes are included.
  27.  
  28. If there is enough interest (and requests) I'll modify the program
  29. to accept command line options for:
  30.  
  31.     - [-t[wyse|ansi|vt100] | -c clockESC -d dateESC] -C -D
  32.  
  33.     (C & D would be clock_only and date_only)
  34.  
  35. anyway, here is the source:
  36.  
  37. --------------------------------- cut here -----------------------------------
  38. /*LINTLIBRARY*/
  39. #include        <stdio.h>
  40. #include        <sys/types.h>
  41. #include        <time.h>
  42. #include        <utmp.h>
  43. #include    <signal.h>
  44.  
  45. #define         F7LABEL "\033z6"        /* esc code for F7 label */
  46. #define         F8LABEL "\033z7"        /* esc code for F8 label */
  47.  
  48. main()
  49. {
  50. extern    int        sprintf(),
  51.             exit();
  52.  
  53. extern    void    cleanup();
  54.  
  55. void        date_display(),
  56.         time_display();
  57.  
  58. unsigned    sleep();
  59. struct    tm      *locatime();
  60. time_t        time();
  61.  
  62. struct  tm    *timenow;
  63. time_t        clock;
  64.  
  65. /* initial display of the date and time */
  66. clock = time(&clock);
  67. timenow = localtime((time_t *)&clock);
  68. date_display(timenow);
  69. time_display(timenow);
  70.  
  71. if ( fork() == 0 )
  72.     {
  73.     signal(SIGINT,  SIG_IGN);
  74.     signal(SIGQUIT, SIG_IGN);
  75.     signal(SIGHUP,  cleanup);
  76.     signal(SIGTERM, cleanup);
  77.  
  78.     for (;;)
  79.     {
  80.     /* sync the time to change on the exact boundry. Once initilized,
  81.      * it should never loop more than once or twice */
  82.     do
  83.         {
  84.         sleep(1);
  85.         clock = time(&clock);
  86.         timenow = localtime((time_t *)&clock);
  87.         }
  88.     while ( timenow->tm_sec != 0 );
  89.  
  90.     /* change the date at midnight */
  91.     if ( timenow->tm_hour == 0 && timenow->tm_min == 0 ) 
  92.         date_display(timenow);
  93.  
  94.     time_display(timenow);
  95.     sleep (58);
  96.     }
  97.     }
  98. return(0);
  99. }
  100.  
  101. /*
  102.  * display the date
  103.  */
  104. void    date_display(timenow)
  105. struct    tm    *timenow;
  106. {
  107. char    date_buff[10];
  108.  
  109. sprintf(date_buff, "%2d/%2d/%2d", timenow->tm_mon+1,
  110.     timenow->tm_mday, timenow->tm_year);
  111. write ( 1, F7LABEL, sizeof(F7LABEL));
  112. write ( 1, date_buff, (unsigned)strlen(date_buff));
  113. }
  114.  
  115. /*
  116.  * display the time
  117.  */
  118. void    time_display(timenow)
  119. struct    tm    *timenow;
  120. {
  121. char    meridian[3],            /* anti-meridian, post-meridian (am/pm) */
  122.     time_buff[10];
  123.  
  124. /* setup 12 hour AM/PM notation */
  125. if (timenow->tm_hour == 0)
  126.     {
  127.     timenow->tm_hour = 12;
  128.     meridian[0]='A';
  129.     }
  130. else
  131.     if (timenow->tm_hour == 12)
  132.     meridian[0]='P';
  133.     else
  134.     if (timenow->tm_hour > 12)
  135.         {
  136.         timenow->tm_hour -= 12;
  137.         meridian[0]='P';
  138.         }
  139.     else /* its in the AM */
  140.         meridian[0]='A';
  141.  
  142. meridian[1]='M';
  143. meridian[2]='\0';
  144.  
  145. if (timenow->tm_min >= 10)
  146.     sprintf (time_buff, "%2d:%d %s", timenow->tm_hour, timenow->tm_min, meridian);
  147. else
  148.     sprintf (time_buff, "%2d:0%1d %s", timenow->tm_hour, timenow->tm_min, meridian);
  149. write(1, F8LABEL, sizeof(F8LABEL));
  150. write(1, time_buff, (unsigned)strlen(time_buff));
  151. }
  152.  
  153. /*
  154.  * remove the date and time at logout
  155.  */
  156. void    cleanup()
  157. {
  158. write(1, F7LABEL, sizeof(F7LABEL));
  159. write(1, "        ", 8);
  160. write(1, F8LABEL, sizeof(F8LABEL));
  161. write(1, "        ", 8);
  162. exit(0);
  163. }
  164. -- 
  165. -------------------------------------------------------------
  166. Jay @ SAC-UNIX, Sacramento, Ca.   UUCP=...pacbell!sactoh0!jak
  167. If something is worth doing, it's worth doing correctly.
  168.