home *** CD-ROM | disk | FTP | other *** search
- From: jak@sactoh0.SAC.CA.US (Jay A. Konigsberg)
- Newsgroups: alt.sources
- Subject: A simple date/time program for Wyse terminals
- Message-ID: <4533@sactoh0.SAC.CA.US>
- Date: 25 Dec 90 17:05:42 GMT
-
- The following is a clock that resides in the F8 function key label
- (the date goes into F7).
-
- The features are:
-
- 1) Clock will sync to change time with the system (to the second).
- 2) The date is displayed on startup and again when the time is 00:00
- (in case you left your terminal logged in overnight).
- 3) SIGQUIT (logout) and SIGTERM (kill -15) will clear the clock from
- the labels (too bad about SIGKILL :-( ).
- 4) The display is done with write(2) to avoid system buffering (using
- puts(3S) or printf(3S) with clock sometimes results in the time
- being displayed across the screen - yes I could have used setbuf(3S)
- and fflush(3S), but I chose write(2) ).
- 5) The ESC codes can be easily changed and the code recomplied.
-
- The bugs are:
-
- 1) No command line options (i.e. you can't turn the date or time off).
- 2) Only Wyse ESC codes are included.
-
- If there is enough interest (and requests) I'll modify the program
- to accept command line options for:
-
- - [-t[wyse|ansi|vt100] | -c clockESC -d dateESC] -C -D
-
- (C & D would be clock_only and date_only)
-
- anyway, here is the source:
-
- --------------------------------- cut here -----------------------------------
- /*LINTLIBRARY*/
- #include <stdio.h>
- #include <sys/types.h>
- #include <time.h>
- #include <utmp.h>
- #include <signal.h>
-
- #define F7LABEL "\033z6" /* esc code for F7 label */
- #define F8LABEL "\033z7" /* esc code for F8 label */
-
- main()
- {
- extern int sprintf(),
- exit();
-
- extern void cleanup();
-
- void date_display(),
- time_display();
-
- unsigned sleep();
- struct tm *locatime();
- time_t time();
-
- struct tm *timenow;
- time_t clock;
-
- /* initial display of the date and time */
- clock = time(&clock);
- timenow = localtime((time_t *)&clock);
- date_display(timenow);
- time_display(timenow);
-
- if ( fork() == 0 )
- {
- signal(SIGINT, SIG_IGN);
- signal(SIGQUIT, SIG_IGN);
- signal(SIGHUP, cleanup);
- signal(SIGTERM, cleanup);
-
- for (;;)
- {
- /* sync the time to change on the exact boundry. Once initilized,
- * it should never loop more than once or twice */
- do
- {
- sleep(1);
- clock = time(&clock);
- timenow = localtime((time_t *)&clock);
- }
- while ( timenow->tm_sec != 0 );
-
- /* change the date at midnight */
- if ( timenow->tm_hour == 0 && timenow->tm_min == 0 )
- date_display(timenow);
-
- time_display(timenow);
- sleep (58);
- }
- }
- return(0);
- }
-
- /*
- * display the date
- */
- void date_display(timenow)
- struct tm *timenow;
- {
- char date_buff[10];
-
- sprintf(date_buff, "%2d/%2d/%2d", timenow->tm_mon+1,
- timenow->tm_mday, timenow->tm_year);
- write ( 1, F7LABEL, sizeof(F7LABEL));
- write ( 1, date_buff, (unsigned)strlen(date_buff));
- }
-
- /*
- * display the time
- */
- void time_display(timenow)
- struct tm *timenow;
- {
- char meridian[3], /* anti-meridian, post-meridian (am/pm) */
- time_buff[10];
-
- /* setup 12 hour AM/PM notation */
- if (timenow->tm_hour == 0)
- {
- timenow->tm_hour = 12;
- meridian[0]='A';
- }
- else
- if (timenow->tm_hour == 12)
- meridian[0]='P';
- else
- if (timenow->tm_hour > 12)
- {
- timenow->tm_hour -= 12;
- meridian[0]='P';
- }
- else /* its in the AM */
- meridian[0]='A';
-
- meridian[1]='M';
- meridian[2]='\0';
-
- if (timenow->tm_min >= 10)
- sprintf (time_buff, "%2d:%d %s", timenow->tm_hour, timenow->tm_min, meridian);
- else
- sprintf (time_buff, "%2d:0%1d %s", timenow->tm_hour, timenow->tm_min, meridian);
- write(1, F8LABEL, sizeof(F8LABEL));
- write(1, time_buff, (unsigned)strlen(time_buff));
- }
-
- /*
- * remove the date and time at logout
- */
- void cleanup()
- {
- write(1, F7LABEL, sizeof(F7LABEL));
- write(1, " ", 8);
- write(1, F8LABEL, sizeof(F8LABEL));
- write(1, " ", 8);
- exit(0);
- }
- --
- -------------------------------------------------------------
- Jay @ SAC-UNIX, Sacramento, Ca. UUCP=...pacbell!sactoh0!jak
- If something is worth doing, it's worth doing correctly.
-