home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sources.unix
- From: rushpc!jjr@csn.org (John Rushford)
- Subject: v26i081: nisttime - Set the system clock to the NIST master clock, Part01/01
- Sender: unix-sources-moderator@pa.dec.com
- Approved: vixie@pa.dec.com
-
- Submitted-By: rushpc!jjr@csn.org (John Rushford)
- Posting-Number: Volume 26, Issue 81
- Archive-Name: nisttime/part01
-
- Saw settime.c from John Walker in comp.unix.sources for a SPARC II.
- Attached is a program similar to settime.c however, it is written
- for System V Release 3. This shell archive includes a manual page
- and a Makefile. Just unbundle this archive using "sh". You must
- be logged in as root to 'make install' this program.
-
- John Rushford
- rushpc!jjr@csn.org
-
- [ moderator's extract of the included man page...
-
- NAME
- nisttime - Set the system clock to the NIST master clock
- usnotime - Set the system clock to the USNO master clock
-
- SYNOPSIS
- nisttime [ -sv ]
- usnotime [ -sv ]
-
- DESCRIPTION
- This program calls a master clock reference and compares the
- system clock with the reference. nisttime calls the National
- Institute of Standards and Technology's master clock in
- Boulder Colorado, (303) 494-4774 @ 1200 baud. usnotime
- calls the U.S. Naval Observatory master clock in Washington
- DC, (202) 653-0351 @ 1200 baud.
-
- --vix ]
-
- #! /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 archive 1 (of 1)."
- # Contents: MANIFEST Makefile README nisttime.1 nisttime.c
- # Wrapped by vixie@cognition.pa.dec.com on Sun Dec 27 15:21:47 1992
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'MANIFEST' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'MANIFEST'\"
- else
- echo shar: Extracting \"'MANIFEST'\" \(269 characters\)
- sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
- X File Name Archive # Description
- X-----------------------------------------------------------
- X MANIFEST 1 This shipping list
- X Makefile 1
- X README 1
- X nisttime.1 1
- X nisttime.c 1
- END_OF_FILE
- if test 269 -ne `wc -c <'MANIFEST'`; then
- echo shar: \"'MANIFEST'\" unpacked with wrong size!
- fi
- # end of 'MANIFEST'
- fi
- if test -f 'Makefile' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'Makefile'\"
- else
- echo shar: Extracting \"'Makefile'\" \(603 characters\)
- sed "s/^X//" >'Makefile' <<'END_OF_FILE'
- X# Makefile for 'nisttime' and 'usnotime'
- X#
- X# Log in as 'root' and type 'make install'
- X#
- X
- DESTROOT =
- DESTPATH = $(DESTROOT)/usr/local
- DESTBIN = $(DESTPATH)/bin
- DESTMAN = $(DESTPATH)/man
- X
- CFLAGS= -O
- LDFLAGS= -s
- X
- all: nisttime
- X
- install: nisttime
- X cp nisttime $(DESTBIN)/nisttime
- X chown uucp $(DESTBIN)/nisttime
- X chgrp uucp $(DESTBIN)/nisttime
- X chmod 4111 $(DESTBIN)/nisttime
- X ln $(DESTBIN)/nisttime $(DESTBIN)/usnotime
- X
- install.man: nisttime.1
- X cp nisttime.1 $(DESTMAN)/man1/nisttime.1
- X echo .so $(DESTMAN)/man1/nisttime.1 >$(DESTMAN)/man1/usnotime.1
- X
- clean:; rm -f *.o *.CKP *.BAK *~ a.out core nisttime
- END_OF_FILE
- if test 603 -ne `wc -c <'Makefile'`; then
- echo shar: \"'Makefile'\" unpacked with wrong size!
- fi
- # end of 'Makefile'
- fi
- if test -f 'README' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'README'\"
- else
- echo shar: Extracting \"'README'\" \(969 characters\)
- sed "s/^X//" >'README' <<'END_OF_FILE'
- Saw settime.c from John Walker in comp.unix.sources for a SPARC II.
- Attached is a program similar to settime.c however, it is written
- for System V Release 3. This shell archive includes a manual page
- and a Makefile. Just unbundle this archive using "sh". You must
- be logged in as root to 'make install' this program.
- X
- John Rushford
- rushpc!jjr@csn.org
- X
- X[ moderator's extract of the included man page...
- X
- NAME
- X nisttime - Set the system clock to the NIST master clock
- X usnotime - Set the system clock to the USNO master clock
- X
- SYNOPSIS
- X nisttime [ -sv ]
- X usnotime [ -sv ]
- X
- DESCRIPTION
- X This program calls a master clock reference and compares the
- X system clock with the reference. nisttime calls the National
- X Institute of Standards and Technology's master clock in
- X Boulder Colorado, (303) 494-4774 @ 1200 baud. usnotime
- X calls the U.S. Naval Observatory master clock in Washington
- X DC, (202) 653-0351 @ 1200 baud.
- X
- X --vix ]
- END_OF_FILE
- if test 969 -ne `wc -c <'README'`; then
- echo shar: \"'README'\" unpacked with wrong size!
- fi
- # end of 'README'
- fi
- if test -f 'nisttime.1' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'nisttime.1'\"
- else
- echo shar: Extracting \"'nisttime.1'\" \(1324 characters\)
- sed "s/^X//" >'nisttime.1' <<'END_OF_FILE'
- X.TH NISTTIME 1
- X.UC 4
- X.SH NAME
- nisttime \- Set the system clock to the NIST master clock
- usnotime \- Set the system clock to the USNO master clock
- X.SH SYNOPSIS
- X.B nisttime
- X[
- X.B \-sv
- X]
- X.br
- X.B usnotime
- X[
- X.B \-sv
- X]
- X.SH DESCRIPTION
- This program calls a master clock reference and compares the
- system clock with the reference.
- X.B
- nisttime
- calls the National Institute of Standards and Technology's
- master clock in Boulder Colorado, (303) 494-4774 @ 1200 baud.
- X.B
- usnotime
- calls the U.S. Naval Observatory master clock in Washington DC,
- X(202) 653-0351 @ 1200 baud.
- X.SH OPTIONS
- X.TP 10
- X.B \-s
- The system clock will be reset to the time of the called master clock.
- Only root may use this option.
- X.TP 10
- X.B \-v
- The program will print to stdout all data received from the master
- clock time service.
- X.SH NOTE
- UNIX System V. Release 3 using BNU UUCP. Requires entries in the
- UUCP Systems file for
- X.B nisttime
- and
- X.B usnotime:
- X.PP
- nisttime Never ACU 1200 13034944774
- X.br
- usnotime Never ACU 1200 12026530351
- X.PP
- This program should be owned by 'uucp' and set to mode O'4111.
- X.B nisttime
- and
- X.B usnotime
- should be linked.
- X.SH FILES
- X/usr/lib/uucp/Devices
- X.br
- X/usr/lib/uucp/Dialcodes
- X.br
- X/usr/lib/uucp/Dialers
- X.br
- X/usr/lib/uucp/Systems
- X.SH AUTHOR
- X John J. Rushford
- X.br
- X Phone: (303) 465-4070
- X.br
- X Usenet: rushpc!jjr@csn.org
- END_OF_FILE
- if test 1324 -ne `wc -c <'nisttime.1'`; then
- echo shar: \"'nisttime.1'\" unpacked with wrong size!
- fi
- # end of 'nisttime.1'
- fi
- if test -f 'nisttime.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'nisttime.c'\"
- else
- echo shar: Extracting \"'nisttime.c'\" \(10373 characters\)
- sed "s/^X//" >'nisttime.c' <<'END_OF_FILE'
- X/*
- X** NAME
- X** nisttime - set the system clock to the NIST master clock.
- X** usnotime - set the system clock to the USNO master clock.
- X**
- X** SYNOPSIS
- X** nisttime [-sv]
- X** usnotime [-sv]
- X**
- X** DESCRIPTION
- X** This program calls a master clock reference and compares the
- X** system clock with the reference. nisttime calls the National
- X** Institute of Standards and Technology's master clock in Boulder
- X** Colorado, (303) 494-4774 @ 1200 baud. usnotime calls the U.S.
- X** Naval Observatory master clock in Washington DC, (202) 653-0351
- X** @ 1200 baud.
- X**
- X** If the -s option is used, the system clock is reset to the time
- X** of the called master clock. Only root may use the -s option.
- X** If the -v option is used, the program will print to 'stdout'
- X** all data received from the master clock time service.
- X**
- X** NOTE
- X** UNIX System V. Release 3 using BNU UUCP. Requires entries in
- X** the UUCP Systems file for 'nisttime' and 'usnotime':
- X**
- X** nisttime Never ACU 1200 13034944774
- X** usnotime Never ACU 1200 12026530351
- X**
- X** This program should be owned by 'uucp' and set to mode O'4111.
- X** nisttime and usnotime should be linked.
- X**
- X** FILES
- X** /usr/lib/uucp/Devices
- X** /usr/lib/uucp/Dialcodes
- X** /usr/lib/uucp/Dialers
- X** /usr/lib/uucp/Systems
- X**
- X** COMPILE AND INSTALLATION
- X** cc -s -O nisttime.c -o /usr/bin/nisttime
- X** chown uucp /usr/bin/nisttime
- X** chgrp uucp /usr/bin/nisttime
- X** ln /usr/bin/nisttime /usr/bin/usnotime
- X** chmod 4111 /usr/bin/nisttime
- X**
- X** PROGRAMMER
- X** John J. Rushford
- X** (303) 465-4070
- X** rushpc!jjr@csn.org
- X*/
- X
- static char Sccsid[] = "@(#)rushpc!jjr@csn.org:nisttime.c 2.7, 11 Mar 1992";
- X
- X#include <dial.h>
- X#include <errno.h>
- X#include <stdio.h>
- X#include <string.h>
- X#include <sys/types.h>
- X#include <time.h>
- X
- X#define BAUD 1200 /* modem speed */
- X#define EPOCH 40587 /* MJD for Jan 1, 1970 00:00:00 GMT */
- X#define HOUR (60 * 60) /* seconds in an hour */
- X#define LINE 80 /* input line buffer size */
- X#define NIST_SYSNAME "nisttime" /* system name for NIST */
- X#define OTMA '*' /* nominal on-time mark for USNO & NIST */
- X#define OTMB '#' /* on-time mark after delay compensation */
- X#define ONEDAY (HOUR * 24) /* seconds in a day */
- X#define USNO_SYSNAME "usnotime" /* system name for Naval Observatory */
- X
- X/* scanf(3S) format strings: */
- X#define NISTFMT "%d%*s%2d%*[:]%2d%*[:]%2d" /* for nisttime */
- X#define USNOFMT "%d%*d%2d%2d%2d" /* for usnotime */
- X
- X/* output message macros: */
- X#define CLKERR(a) (void)fprintf(stderr, \
- X "%s: cannot set the clock, difference > one day.\n", a)
- X#define CLKSET(a) (void)fprintf(stdout,"%s: reset the system clock.\n", a)
- X#define DATAERR(a) (void)fprintf(stderr,"%s: received invalid data.\n", a)
- X#define DELTA(a,b,c) (void)fprintf(stdout, \
- X "%s: the system clock is %ld second(s) %s.\n", a, b, c)
- X#define DIALERR(a,b) (void)fprintf(stderr, "%s: %s.\n", a, b)
- X#define ONTIME(a) (void)fprintf(stdout, \
- X "%s: the system clock is on time.\n", a)
- X#define SETERR(a) (void)fprintf(stderr, \
- X "%s: only root may set the system clock.\n", a)
- X#define USAGE(a) (void)fprintf(stderr, "Usage: %s [-sv]\n", a)
- X
- X#ifndef __STDC__
- X typedef enum boolean { B_FALSE, B_TRUE } boolean_t;
- X#endif /* __STDC__ */
- X
- X/* dial(3C) error messages indexed by translated error return code: */
- static char *error_msg[13] = {
- X "interrupt occured",
- X "dialer hung, no return from write",
- X "no answer, caller script failed",
- X "illegal baud-rate",
- X "acu problem, open() failure",
- X "line problem, open() failure",
- X "can't open Devices file",
- X "requested device not available",
- X "requested device not known",
- X "no device available at requested baud rate",
- X "no device known at requested baud rate",
- X "requested speed does not match",
- X "system not known and/or can't read Systems file"
- X};
- X
- X/* returns a pointer to the base name component of the path name: */
- static char *
- X#ifndef __STDC__
- basename(pathname)
- char *pathname;
- X#else
- basename(char *pathname)
- X#endif
- X{
- X char *Basename; /* pointer to resulting basename */
- X
- X if ((Basename = strrchr(pathname, '/')) == NULL)
- X Basename = pathname; /* not a path name*/
- X else Basename++; /* move past last slash in the path name */
- X
- X return Basename;
- X}
- X
- X/* returns the number of seconds since 00:00:00 GMT on January 1, 1970 */
- static time_t
- X#ifndef __STDC__
- Since1970(Julian, Hour, Min, Sec)
- int Julian, Hour, Min, Sec;
- X#else
- Since1970(int Julian, int Hour, int Min, int Sec)
- X#endif
- X{
- X int no_days = 0; /* days since January 1, 1970 */
- X
- X no_days = Julian - EPOCH;
- X return ((no_days * ONEDAY) + (Hour * HOUR) + (Min * 60) + Sec);
- X}
- X
- X#ifndef __STDC__
- main(argc, argv)
- int argc;
- char **argv;
- X#else
- main(int argc, char **argv)
- X#endif
- X{
- X boolean_t clkset = B_FALSE, /* clock was reset flag */
- X complete = B_FALSE, /* call complete flag */
- X errflag = B_FALSE, /* invalid option flag */
- X no_data = B_FALSE, /* invalid time data flag */
- X sflag = B_FALSE, /* reset the system clock flag */
- X usnoflag = B_FALSE, /* U.S Naval Observatory flag */
- X vflag = B_FALSE; /* verbose output flag */
- X CALL call; /* dial structure */
- X char line[LINE], /* input line storage buffer */
- X *prog; /* base name of this program */
- X FILE *modem_rfp; /* stream pointer to modem */
- X int c, /* option character */
- X Hour = 0, /* hours (00 - 23) */
- X lineno = 0, /* number of received input lines */
- X Min = 0, /* minutes (00 - 59) */
- X Mjd = 0, /* modified julian Date */
- X modem_fd, /* dial(3C) file descriptor */
- X Sample = 0, /* number of USNO Samples */
- X Sec = 0, /* seconds (00 - 60) */
- X getopt(), /* getopt(3C) */
- X stime(); /* stime(2) */
- X extern int optind; /* index of next argument */
- X struct termio la; /* termio table */
- X time_t delta = 0, /* diff. between sysclk & timbuf */
- X sysclk, /* seconds since the epoch */
- X timbuf; /* seconds since the epoch */
- X unsigned short getuid(); /* getuid(2) */
- X void exit(); /* exit(2) */
- X
- X /* initialize the termio structure */
- X la.c_iflag = (0 | IGNBRK | IGNPAR | IXON | IXOFF);
- X la.c_oflag = 0;
- X la.c_cflag = (0 | B1200 | CS8 | CREAD | HUPCL);
- X la.c_lflag = (0 | ICANON | ECHO | ECHONL);
- X
- X /* initialize the call structure */
- X call.attr = &la;
- X call.baud = -1;
- X call.speed = BAUD;
- X call.telno = NIST_SYSNAME; /* call the NIST, default */
- X
- X /* get the program base name: */
- X prog = basename(argv[0]);
- X
- X /* process command-line arguments: */
- X while ((c = getopt(argc, argv, "sv")) != EOF) {
- X switch(c) {
- X case 's': /* request to set the system clock */
- X if (getuid() == 0) sflag = B_TRUE;
- X else {
- X SETERR(prog);
- X errflag = B_TRUE;
- X }
- X break;
- X case 'v': /* request for verbose output */
- X vflag = B_TRUE;
- X break;
- X case '?': /* unknown request */
- X errflag = B_TRUE;
- X break;
- X } /* end of switch statement */
- X } /* end of while loop */
- X
- X /* exit on command line errors: */
- X if (argc > optind || errflag) {
- X USAGE(prog);
- X exit(1);
- X }
- X /* check to see if this is a call to the U.S. Naval Observatory: */
- X if (strcmp(prog, USNO_SYSNAME) == 0) {
- X usnoflag = B_TRUE;
- X call.telno = USNO_SYSNAME; /* reset the call structure */
- X }
- X /* connect to the remote system: */
- X if ((modem_fd = dial(call)) < 0) {
- X DIALERR(prog, error_msg[((modem_fd * -1) - 1)]);
- X exit(2);
- X }
- X if ((modem_rfp = fdopen(modem_fd, "r")) == NULL) {
- X perror(prog);
- X exit(3);
- X }
- X /* read and process input from the remote system: */
- X while (!complete && fgets(line, LINE, modem_rfp) != NULL) {
- X if (usnoflag) { /* process Naval Observatory data */
- X if (line[0] == OTMA) ++Sample; /* saw an on-time mark */
- X if (Sample == 5) complete = B_TRUE; /* 5 samples is good enough */
- X else /* process input data: */
- X (void)sscanf(line, USNOFMT, &Mjd, &Hour, &Min, &Sec);
- X }
- X else { /* else process NIST data */
- X (void)sscanf(line, NISTFMT, &Mjd, &Hour, &Min, &Sec);
- X /* check for on-time mark, prefer an OTMB mark */
- X if(line[49] == OTMB || (lineno > 20 && line[49] == OTMA))
- X complete = B_TRUE;
- X }
- X if (complete) { /* done sampling the remote system so */
- X (void)time(&sysclk); /* get the local system clocks time */
- X /* compute the current time according to the remote system: */
- X timbuf = Since1970(Mjd, Hour, Min, Sec);
- X /* calculate the difference: */
- X if (timbuf > sysclk) delta = timbuf - sysclk;
- X else delta = sysclk - timbuf;
- X /* check to see if the difference is out of bounds: */
- X if (delta > ONEDAY || delta == 0) sflag = B_FALSE;
- X /* if everything is ok, reset the system clock: */
- X if (sflag && stime(&timbuf) == 0) clkset = B_TRUE;
- X }
- X if (vflag) { /* if verbose output was requested then print it: */
- X (void)fflush(modem_rfp);
- X (void)fprintf(stdout, "%s", line);
- X }
- X /* if all we've got is bum data, give up: */
- X if (++lineno > 30 && !complete) complete = no_data = B_TRUE;
- X } /* end of while loop */
- X
- X /* terminate the call and close the modem stream: */
- X undial(modem_fd);
- X (void)fclose(modem_rfp);
- X
- X /* if there were no good samples, report and exit: */
- X if (no_data) {
- X DATAERR(prog);
- X exit(4);
- X }
- X /* call succeeded so report the results: */
- X if (timbuf > sysclk) DELTA(prog, delta, "slow");
- X if (sysclk > timbuf) DELTA(prog, delta, "fast");
- X if (sysclk == timbuf) ONTIME(prog);
- X if (delta > ONEDAY) CLKERR(prog);
- X if (clkset) CLKSET(prog);
- X
- X return 0;
- X}
- X
- END_OF_FILE
- if test 10373 -ne `wc -c <'nisttime.c'`; then
- echo shar: \"'nisttime.c'\" unpacked with wrong size!
- fi
- # end of 'nisttime.c'
- fi
- echo shar: End of archive 1 \(of 1\).
- cp /dev/null ark1isdone
- MISSING=""
- for I in 1 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have the archive.
- rm -f ark[1-9]isdone
- else
- echo You still need to unpack the following archives:
- echo " " ${MISSING}
- fi
- ## End of shell archive.
- exit 0
-