[comp.sources.misc] v03i056: clock program

chad@anasaz.UUCP (06/17/88)

comp.sources.misc: Volume 3, Issue 56
Submitted-By: "A. Nonymous" <chad@anasaz.UUCP>
Archive-Name: clock

This program is rather simple-minded, but it gets a surprising amount
of use as the idle behavior on some terminals.  It is a full screen
date-and-time display for SysV.
	-crl
----(cut)----(cut)----(cut)----(cut)----(cut)----(cut)----(cut)----
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  Makefile clock.c
# Wrapped by chad@anasaz on Thu Jun 16 00:47:19 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(468 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
X#
X# Makefile for clock routine
X#	Last changed 4/30/88 01:35:39
X#
X
X# note: this should be able to be compiled with -DMINICURSES to save
X# size and speed, but an apparent bug causes noecho() to get broken
X
XBINDIR = /usr/local/bin
X
Xclock:	clock.c
X#	$(CC) $(CFLAGS) -DMINICURSES -o clock clock.c -lcurses
X	$(CC) $(CFLAGS) -o clock clock.c -lcurses
X
Xinstall:	clock
X	strip clock
X	-rm $(BINDIR)/clock
X	ln clock $(BINDIR)
X	touch install
X
Xlint:	clock.c
X	lint -pu clock.c >LINT
END_OF_FILE
if test 468 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'clock.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'clock.c'\"
else
echo shar: Extracting \"'clock.c'\" \(5019 characters\)
sed "s/^X//" >'clock.c' <<'END_OF_FILE'
X/*TITLE clock.c - Clock/calendar on terminal - 5/4/88 */
X
X/*
X** This program will place a full screen (readable across the room)
X** date and time display on your terminal.  The display will update
X** automagically.  To run it, make sure your TERM environmental
X** variable is set properly and type "clock".  To end the program,
X** type your "interrupt" character (usually cntl-c or delete).
X** The program also traps the "terminate" signal, so it can be
X** gracefully stopped by shutdown(1M) or a kill command from another
X** terminal.
X**
X**  It has been tested on several System V machines, but no others.
X**  To compile:  cc -O -s clock.c -o clock -lcurses
X*/
X
X/*  This program, and the procedures in it are Copyright 1988 by:
X**	DCF, Inc.
X**	14623 North 49th Place
X**	Scottsdale, AZ 85254
X**  You may use it as you wish, including re-distribution, as long
X**  as this notice remains intact and with the source code.
X**  This program was suggested by a similar one written by
X**  anasazi!duane.
X*/
X
X/*SUBTTL Includes, defines, and declarations */
X
Xstatic char	SCCS[] = "@(#)clock.c	1.4 4/30/88 09:04:54";
X
X#include <stdio.h>
X#include <curses.h>
X#include <string.h>
X#include <errno.h>
X#include <signal.h>
X#include <time.h>
X
X#define	MAXLINES	8	/* number of lines output by banner */
X#define BANNER		"/usr/bin/banner "	/* banner program to use */
X#define	DAY_LINE	0	/* screen location for display elements */
X#define	DATE_LINE	8
X#define	TIME_LINE	16
X
X
X/* global data */
Xchar		time_buf[24];	/* local time as a string */
Xstruct tm	*time_tm;	/* local time as a structure */
X
X/* forward references */
Xvoid		put_scr(), do_time(), do_date(), blammo(), end_win(), exit();
Xint		cat_int();
Xunsigned	sleep();
Xextern FILE	*popen();
Xextern long	time();
X
X
X/*SUBTTL Main procedure */
Xmain()
X{
X    long	ticks;		/* seconds since epoch */
X    int		old_day = -1;	/* previous julian day */
X
X    if ( !initscr() )
X        blammo("curses initialization failed", 0);
X
X    signal(SIGINT, cat_int);	/* provide an exit path */
X    signal(SIGTERM, cat_int);
X    clear();			/* setup */
X    noecho();
X
X    while (TRUE) {
X	/* get the local time for us to use */
X	ticks = time( (long *)NULL );
X	strncpy(time_buf, ctime(&ticks), 24);
X	time_tm = localtime(&ticks);
X
X	/* put date & time on the screen */
X	if (old_day != time_tm->tm_yday) {
X	    old_day = time_tm->tm_yday;
X	    do_date();		/* build a new date */
X	}
X        do_time();		/* build the time */
X	move(0, 0);		/* update the screen */
X	refresh();
X
X	/* wait for clock to roll over the next minute */
X	sleep( (unsigned)(60 - time_tm->tm_sec) );
X    }
X}
X
X
X/*SUBTTL do_date - display the date banner */
X/*
X**  Construct and display the date banner
X*/
Xvoid do_date()
X{
X    static char *days[] = {	/* days of the week */
X      "Sunday", "Monday", "Tuesday", "Wednesday",
X      "Thursday", "Friday", "Saturday" };
X    char	buf[7];
X
X    erase();	/* in case new date is shorter than current */
X    put_scr(DAY_LINE, days[time_tm->tm_wday]);
X    strncpy(buf, &time_buf[4], 6);
X    buf[6] = '\0';
X    put_scr(DATE_LINE, buf);
X}
X
X
X/*SUBTTL do_time - display the time banner */
X/*
X**  Construct and display the time banner
X*/
Xvoid do_time()
X{
X    char	buf[6];
X
X    strncpy(buf, &time_buf[11], 5);
X    buf[5] = '\0';
X    put_scr(TIME_LINE, buf);
X}
X
X
X/*SUBTTL put_scr - put a banner in the screen buffer */
X/*
X**  Construct and display a banner string
X*/
X
Xvoid	put_scr(where, what)
Xint	where;
Xchar	*what;
X{
X    char	ban_buf[MAXLINES][80];
X    char	buf[80];
X    FILE	*proc_stream;
X    int		length = 0;
X    register int line;
X
X    /* start a "banner" stream in our direction */
X    strncpy(buf, BANNER, 80);
X    strcat(buf, "'");
X    strcat(buf, what);
X    strcat(buf, "'");
X    if ( (proc_stream = popen(buf, "r") ) == (FILE *)NULL )
X        blammo("Popen", errno);
X
X    /* capture the stream in our buffer */
X    for (line = 0; line < MAXLINES; line++) {
X	fgets(ban_buf[line], 80, proc_stream);
X	if ( feof(proc_stream) )
X	    blammo("Premature eof", 0);
X    }
X    (void)pclose(proc_stream);
X
X    /* figure out how long this banner is (for centering) */
X    for (line = 0; line < MAXLINES; line++)
X	length = (length < strlen(ban_buf[line]) ?
X	  strlen(ban_buf[line]) : length);
X
X    /* put the banner in the screen buffer */
X    for (line = 0; line < MAXLINES; line++)
X	mvaddstr(where + line, (COLS - length) / 2, ban_buf[line]);
X}
X
X
X/*SUBTTL blammo - display message and terminate program */
X/*
X**  Reset the screen, issue an error message, and exit.
X*/
X
Xvoid blammo(msg, code)
Xchar	*msg;
Xint	code;
X{
X    end_win();
X    if (code)
X        fprintf(stderr, "%s error %d\n", msg, code);
X    else
X        fprintf(stderr, "%s\n", msg);
X    exit(-1);
X}
X
X
X/*SUBTTL end_win - clean up the window stuff */
X/*
X**  Terminate the window stuff - clear the window and move the cursor.
X*/
X
Xvoid end_win()
X{
X    clear();
X    move(LINES - 1, 0);
X    refresh();
X    endwin();
X}
X
X
X/*SUBTTL cat_int - signal catcher */
X/*
X**  Catch an interrupt signal and terminate the program.
X*/
X
Xint	cat_int()
X{
X    end_win();
X    exit(0);
X}
END_OF_FILE
if test 5019 -ne `wc -c <'clock.c'`; then
    echo shar: \"'clock.c'\" unpacked with wrong size!
fi
# end of 'clock.c'
fi
echo shar: End of shell archive.
exit 0