Index: Makefile
*** Makefile	Fri May 19 03:43:39 1989
--- Makefile	Wed Dec 13 13:18:52 1995
***************
*** 58,75 ****
  # 
  #
  DESTDIR=
! INCPATH=
  LDFLAGS=
  BINDIR=/usr/local/etc
  LINKDIR=/etc
! LIBS=
  
  VERS=3.4
  
! #CC=gcc -g -W -Wall
! CC=gcc -g -W
  
! INSTALL= install -c
  
  #
  # FEATURES include:
--- 58,74 ----
  # 
  #
  DESTDIR=
! INCPATH= -I.
  LDFLAGS=
  BINDIR=/usr/local/etc
  LINKDIR=/etc
! LIBS= -lsocket -lunix
  
  VERS=3.4
  
! CC=cc -4 -Q -O
  
! INSTALL= cp
  
  #
  # FEATURES include:
***************
*** 92,99 ****
  
  #FEATURES= -DBROADCAST_NTP -DSETTICKADJ -DDEBUG
  #FEATURES= -DSETTICKADJ -DDEBUG -DREFCLOCK -DPSTI
- FEATURES= -DSETTICKADJ -DDEBUG -DREFCLOCK
  
  # for 4.3 BSD
  DEFINES=
  
--- 91,100 ----
  
  #FEATURES= -DBROADCAST_NTP -DSETTICKADJ -DDEBUG
  #FEATURES= -DSETTICKADJ -DDEBUG -DREFCLOCK -DPSTI
  
+ #FEATURES= -DDEBUG -DREFCLOCK -DPSTI
+ FEATURES=
+ 
  # for 4.3 BSD
  DEFINES=
  
***************
*** 111,117 ****
  # for a NeXT system, define these pre-processor symbols.
  #DEFINES=-DSUN_FLT_BUG -DGENERIC_UNS_BUG 
  
! CFLAGS= -O ${DEFINES} ${FEATURES} ${INCPATH}
  #
  # Header files
  #
--- 112,118 ----
  # for a NeXT system, define these pre-processor symbols.
  #DEFINES=-DSUN_FLT_BUG -DGENERIC_UNS_BUG 
  
! CFLAGS= ${DEFINES} ${FEATURES} ${INCPATH}
  #
  # Header files
  #
***************
*** 120,132 ****
  # Source files
  #
  NTPDSRC= ntpd.c ntpsubs.c ntp_proto.c ntp_sock.c ntp_adjust.c read_local.c \
! 	read_psti.c
  SRCS=	ntp.c ntpdc.c ${NTPDSRC}
  
  # Object files
  #
  NTPDOBJ= ntpd.o ntpsubs.o ntp_proto.o ntp_sock.o ntp_adjust.o read_local.o \
! 	read_psti.o
  OBJS=	ntp.o ntpdc.o ${NTPDOBJ}
  
  
--- 121,133 ----
  # Source files
  #
  NTPDSRC= ntpd.c ntpsubs.c ntp_proto.c ntp_sock.c ntp_adjust.c read_local.c \
! 	read_psti.c qnx_time.c qnx_syslog.c
  SRCS=	ntp.c ntpdc.c ${NTPDSRC}
  
  # Object files
  #
  NTPDOBJ= ntpd.o ntpsubs.o ntp_proto.o ntp_sock.o ntp_adjust.o read_local.o \
! 	read_psti.o qnx_time.o qnx_syslog.o
  OBJS=	ntp.o ntpdc.o ${NTPDOBJ}
  
  
***************
*** 136,143 ****
  all:	${PROGS}
  	@./ntest
  
! ntp:	ntp.o ntpsubs.o
! 	${CC} ${LDFLAGS} -o ntp ntp.o ntpsubs.o ${LIBS}
  
  ntpd:	${NTPDOBJ}
  	${CC} ${LDFLAGS} -o ntpd ${NTPDOBJ} ${LIBS}
--- 137,144 ----
  all:	${PROGS}
  	@./ntest
  
! ntp:	ntp.o ntpsubs.o qnx_time.o
! 	${CC} ${LDFLAGS} -o ntp ntp.o ntpsubs.o qnx_time.o ${LIBS}
  
  ntpd:	${NTPDOBJ}
  	${CC} ${LDFLAGS} -o ntpd ${NTPDOBJ} ${LIBS}
Index: man/ntpd.8
*** man/ntpd.8	Fri May 19 03:38:51 1989
--- man/ntpd.8	Tue Dec 12 17:35:56 1995
***************
*** 3,9 ****
  ntpd \- time synchronization daemon implementing NTP
  .SH SYNOPSIS
  .B /usr/local/etc/ntpd
! [-a threshold] [-c file] [-d] [-D level] [-l] [-n] [-s] [-t]
  .SH OPTIONS
  .B -a 
  .I threshold
--- 3,9 ----
  ntpd \- time synchronization daemon implementing NTP
  .SH SYNOPSIS
  .B /usr/local/etc/ntpd
! [-a threshold] [-c file] [-d] [-D level] [-l] [-m limit] [-n] [-s] [-t]
  .SH OPTIONS
  .B -a 
  .I threshold
***************
*** 44,49 ****
--- 44,63 ----
  .B -l 
  option is specified, a message will be logged approximately every 2 minutes.
  .PP
+ .B -m 
+ .I limit
+ is used to set a limit on the network delay that
+ .B ntpd
+ considers acceptable.  It can be used with networks which may exhibit
+ large asymmetric delays for extended periods (e.g. heavily loaded SLIP
+ links) to prevent
+ .B ntpd
+ erroneously bouncing the clock around under these conditions.
+ .I limit
+ is to be specified in units of seconds, or the string
+ .B any
+ to defeat the check.
+ .PP
  .B -n
  will, on Ultrix systems, inhibit the
  .I ntpd
***************
*** 137,142 ****
--- 151,157 ----
  used to describe the intrinsic properties of the local host's timekeeping
  machinary.  The commands in this group are
  .B precision,
+ .B maxdelay
  and
  .B tickadj.
  .PP
***************
*** 151,156 ****
--- 166,180 ----
  .B _hz
  is defined in the namelist of /vmunix, this value is automatically set based
  on the value of hz.
+ .PP
+ The
+ .B maxdelay
+ command takes a number in units of seconds which sets the maximum network
+ delay that
+ .I ntpd
+ considers acceptable.  This is the
+ .B -m
+ option in disguise.
  .PP
  The
  .B tickadj
Index: ntp.c
*** ntp.c	Fri May 19 09:21:35 1989
--- ntp.c	Tue Dec 12 17:35:57 1995
***************
*** 1,6 ****
  #ifndef	lint
  static char *rcsid = "$Source: /usr/users/louie/ntp/RCS/ntp.c,v $ $Revision: 3.4.1.6 $ $Date: 89/05/18 18:21:29 $";
! #endif	lint
  
  /*
   *  $Log:	ntp.c,v $
--- 1,6 ----
  #ifndef	lint
  static char *rcsid = "$Source: /usr/users/louie/ntp/RCS/ntp.c,v $ $Revision: 3.4.1.6 $ $Date: 89/05/18 18:21:29 $";
! #endif	/* lint */
  
  /*
   *  $Log:	ntp.c,v $
***************
*** 84,92 ****
  
  #include <stdio.h>
  #include <sys/types.h>
! #include <sys/param.h>
! #include <sys/time.h>
! #include <sys/uio.h>
  #include <sys/socket.h>
  #include <sys/ioctl.h>
  
--- 84,90 ----
  
  #include <stdio.h>
  #include <sys/types.h>
! #include <time.h>
  #include <sys/socket.h>
  #include <sys/ioctl.h>
  
***************
*** 97,104 ****
  
  #include <arpa/inet.h>
  #include <netdb.h>
! #include <strings.h>
  #include <errno.h>
  #include "ntp.h"
  
  char *modename[8] = {
--- 95,103 ----
  
  #include <arpa/inet.h>
  #include <netdb.h>
! #include <string.h>
  #include <errno.h>
+ #include <sys/select.h>
  #include "ntp.h"
  
  char *modename[8] = {
Index: ntp.h
*** ntp.h	Fri May 19 09:22:21 1989
--- ntp.h	Wed Dec 13 10:14:31 1995
***************
*** 118,123 ****
--- 118,126 ----
  #ifndef	NTPDRIFTCOMP
  #define	NTPDRIFTCOMP	"/etc/ntp.drift"
  #endif
+ #ifndef	NTPLOGFILE
+ #define	NTPLOGFILE	"/usr/adm/ntp.log"
+ #endif
  
  struct list {
  	struct ntp_peer *head;
***************
*** 171,176 ****
--- 174,185 ----
   *  Clock parameters
   */
  #define	CLOCK_UPDATE	8	/* update interval (1<<CLOCK_UPDATE secs) */
+ #ifdef __QNX__
+ /* These numbers seem more appropriate for the average PC ... */
+ #define	CLOCK_ADJ	1	/* adjustment interval (1<<CLOCK_ADJ secs) */
+ #define	CLOCK_PHASE	8	/* phase shift */
+ #define	CLOCK_MAX	0.512	/* maximum aperture (milliseconds) */
+ #else
  #if	XTAL
  #define	CLOCK_ADJ	2	/* adjustment interval (1<<CLOCK_ADJ secs) */
  #define	CLOCK_PHASE	8	/* phase shift */
***************
*** 180,185 ****
--- 189,195 ----
  #define	CLOCK_PHASE	6	/* phase shift */
  #define	CLOCK_MAX	0.512	/* maximum aperture (milliseconds) */
  #endif
+ #endif /* __QNX__ */
  #define	CLOCK_FREQ	10	/* frequency shift */
  #define	CLOCK_TRACK	8
  #define	CLOCK_COMP	4
***************
*** 246,252 ****
  	u_char status;		/* status of local clock and leap info */
  	u_char stratum;		/* Stratum level */
  	u_char ppoll;		/* poll value */
! 	int precision:8;
  	struct s_fixedpt distance;
  	struct s_fixedpt dispersion;
  	u_long refid;
--- 256,262 ----
  	u_char status;		/* status of local clock and leap info */
  	u_char stratum;		/* Stratum level */
  	u_char ppoll;		/* poll value */
! 	char precision;
  	struct s_fixedpt distance;
  	struct s_fixedpt dispersion;
  	u_long refid;
***************
*** 379,385 ****
  	u_char leap;
  	u_char stratum;
  	u_char ppoll;
! 	int precision:8;
  
  	u_char hpoll;
  	u_char filler1;
--- 389,395 ----
  	u_char leap;
  	u_char stratum;
  	u_char ppoll;
! 	char precision;
  
  	u_char hpoll;
  	u_char filler1;
***************
*** 411,413 ****
--- 421,476 ----
  };
  
  
+ 
+ /* ================= Prototypes ================= */
+ 
+ /* ntp_adjust.c */
+ 
+ void init_logical_clock();
+ int adj_logical(double offset);
+ void adj_host_clock();
+ 
+ /* ntp_proto.c */
+ 
+ void transmit(struct ntp_peer *peer);
+ void refclock_input(struct ntp_peer *peer, struct ntpdata *pkt);
+ void receive(struct sockaddr_in *dst, struct ntpdata *pkt,
+ 	struct timeval *tvp, int sock);
+ void process_packet(struct sockaddr_in *dst, struct ntpdata *pkt,
+         struct timeval *tvp, struct ntp_peer *peer);
+ void clock_update(struct ntp_peer *peer);
+ void initialize();
+ void clear(struct ntp_peer *peer);
+ void poll_update(struct ntp_peer *peer, int new_hpoll);
+ void clock_filter(struct ntp_peer *peer, double new_delay, double new_offset);
+ int sanity_check(struct ntp_peer *peer);
+ 
+ /* ntp_sock.c */
+ 
+ create_sockets(unsigned int port);
+ 
+ /* ntpsubs.c */
+ 
+ double ul_fixed_to_double(struct l_fixedpt *t);
+ double l_fixed_to_double(struct l_fixedpt *t);
+ double s_fixed_to_double(struct s_fixedpt *t);
+ void double_to_l_fixed(struct l_fixedpt *t, double value);
+ void double_to_s_fixed(struct s_fixedpt *t, double value);
+ tstamp(struct l_fixedpt *stampp, struct timeval *tvp);
+ char *ntoa (struct in_addr in_addr);
+ 
+ /* qnx_time.c */
+ 
+ int gettimeofday(struct timeval *tp, struct timezone *tzp);
+ int settimeofday(struct timeval *tp, struct timezone *tzp);
+ int adjtime(struct timeval *delta, struct timeval *olddelta);
+ 
+ /* read_local.c */
+ 
+ init_clock_local(char *file);
+ read_clock_local(int cfd, struct timeval **tvp, struct timeval **mtvp);
+ 
+ /* read_psti.c */
+ 
+ init_clock_psti(char *timesource);
+ read_clock_psti(int cfd, struct timeval **tvpp, struct timeval **otvpp);
Index: ntp_adjust.c
*** ntp_adjust.c	Fri May 19 09:23:40 1989
--- ntp_adjust.c	Tue Dec 12 17:35:58 1995
***************
*** 62,72 ****
  
  #include <stdio.h>
  #include <sys/types.h>
- #include <sys/param.h>
  #include <sys/socket.h>
  #include <sys/time.h>
  #include <sys/ioctl.h>
- #include <sys/resource.h>
  
  #include <netinet/in.h>
  #include <netinet/in_systm.h>
--- 62,70 ----
***************
*** 75,81 ****
  
  #include <arpa/inet.h>
  #include <netdb.h>
! #include <strings.h>
  #include <errno.h>
  #include <syslog.h>
  
--- 73,79 ----
  
  #include <arpa/inet.h>
  #include <netdb.h>
! #include <string.h>
  #include <errno.h>
  #include <syslog.h>
  
***************
*** 87,93 ****
  
  extern int doset;
  extern int debuglevel;
- extern int kern_tickadj;
  extern char *ntoa();
  extern struct sysdata sys;
  
--- 85,90 ----
***************
*** 105,118 ****
  void
  init_logical_clock()
  {
! 	if (kern_tickadj)
! 		adj_precision = kern_tickadj;
! 	else
! 		adj_precision = 1;
! 	/*
! 	 *  If you have the "fix" for adjtime() installed in you kernel, you'll
! 	 *  have to make sure that adj_precision is set to 1 here.
! 	 */
  }
  
  
--- 102,108 ----
  void
  init_logical_clock()
  {
! 	adj_precision = 1;
  }
  
  
***************
*** 185,194 ****
  			firstpass = 0;
  		else if (update_timer > 0) {
  			ai = abs(compliance);
! 			ai = (double)(1<<CLOCK_COMP) - 
! 				(double)(1<<CLOCK_FACTOR) * ai;
! 			if (ai < 1.0)		/* max(... , 1.0) */
! 				ai = 1.0;
  			drift_comp += offset / (ai * (double)update_timer);
  		}
  
--- 175,184 ----
  			firstpass = 0;
  		else if (update_timer > 0) {
  			ai = abs(compliance);
! 			ai = (double)(1L<<CLOCK_COMP) - 
! 				(double)(1L<<CLOCK_FACTOR) * ai;
! 			if (ai < 0.5)		/* max(... , 0.5) */
! 				ai = 0.5;
  			drift_comp += offset / (ai * (double)update_timer);
  		}
  
Index: ntp_proto.c
*** ntp_proto.c	Fri May 19 09:25:35 1989
--- ntp_proto.c	Wed Dec 13 12:36:17 1995
***************
*** 112,126 ****
  
  #include <stdio.h>
  #include <sys/types.h>
- #include <sys/param.h>
- #include <sys/uio.h>
  #include <sys/socket.h>
  #include <sys/time.h>
  #include <sys/ioctl.h>
- #include <sys/resource.h>
  
  #include <net/if.h>
- 
  #include <netinet/in.h>
  #include <netinet/in_systm.h>
  #include <netinet/ip.h>
--- 112,122 ----
***************
*** 128,143 ****
  
  #include <arpa/inet.h>
  #include <netdb.h>
! #include <strings.h>
  #include <errno.h>
  #include <syslog.h>
  
  #include "ntp.h"
  
  int peer_switches, peer_sw_inhibited;
  
  struct ntp_peer dummy_peer;
  extern double WayTooBig;
  extern unsigned long clock_watchdog;
  #ifdef	DEBUG
  extern int debug;
--- 124,143 ----
  
  #include <arpa/inet.h>
  #include <netdb.h>
! #include <string.h>
  #include <errno.h>
  #include <syslog.h>
  
  #include "ntp.h"
  
+ #define MIN(a,b) (((a)<(b))?(a):(b))
+ #define MAX(a,b) (((a)>(b))?(a):(b))
+ 
  int peer_switches, peer_sw_inhibited;
  
  struct ntp_peer dummy_peer;
  extern double WayTooBig;
+ extern double MaxDelay;
  extern unsigned long clock_watchdog;
  #ifdef	DEBUG
  extern int debug;
***************
*** 205,212 ****
  	if ((peer->flags & (PEER_FL_BCAST|PEER_FL_REFCLOCK)) == 0) {
  		/* select correct socket to send reply on */
  		if (sendto(addrs[(peer->sock < 0 ? 0 : peer->sock)].fd,
! 			   (char *) pkt, sizeof(ntpframe),
! 			   0, &peer->src, sizeof(peer->src)) < 0) {
  			syslog(LOG_ERR, "sendto: %s  %m",
  			       ntoa(peer->src.sin_addr));
  			return;
--- 205,213 ----
  	if ((peer->flags & (PEER_FL_BCAST|PEER_FL_REFCLOCK)) == 0) {
  		/* select correct socket to send reply on */
  		if (sendto(addrs[(peer->sock < 0 ? 0 : peer->sock)].fd,
! 			   (char *) pkt, sizeof(ntpframe), 0,
! 			   (struct sockaddr *) &peer->src,
! 			   sizeof(peer->src)) < 0) {
  			syslog(LOG_ERR, "sendto: %s  %m",
  			       ntoa(peer->src.sin_addr));
  			return;
***************
*** 245,251 ****
  
  		refclock_input(peer, pkt);
  		return;
! #endif REFCLOCK
  	} else {
  #ifdef	BROADCAST_NTP
  		if (sendto(addrs[peer->sock].fd,
--- 246,252 ----
  
  		refclock_input(peer, pkt);
  		return;
! #endif /* REFCLOCK */
  	} else {
  #ifdef	BROADCAST_NTP
  		if (sendto(addrs[peer->sock].fd,
***************
*** 316,323 ****
  #ifdef REFCLOCK
  void
  refclock_input(peer, pkt)
- 	struct ntpdata *pkt;
  	struct ntp_peer *peer;
  {
  	struct timeval *tvp;
  	struct timeval *otvp;
--- 317,324 ----
  #ifdef REFCLOCK
  void
  refclock_input(peer, pkt)
  	struct ntp_peer *peer;
+ 	struct ntpdata *pkt;
  {
  	struct timeval *tvp;
  	struct timeval *otvp;
***************
*** 346,352 ****
  	receive((struct sockaddr_in *)peer, pkt, otvp, -1);
  	return;
  }
! #endif REFCLOCK
  
  /* 3.4.2. Receive Procedure */
  void
--- 347,353 ----
  	receive((struct sockaddr_in *)peer, pkt, otvp, -1);
  	return;
  }
! #endif /* REFCLOCK */
  
  /* 3.4.2. Receive Procedure */
  void
***************
*** 598,603 ****
--- 599,609 ----
  	delay = (t2 - t1) - (t3 - t4);
  	offset = ((t2 - t1) + (t3 - t4)) / 2.0;
  
+ #ifdef DEBUG
+ if (debug > 7)
+ 	printf("Incoming1: delay = %f, offset = %f\n",delay,offset);
+ #endif
+ 
  	delay += 1.0/(unsigned long)(1L << -sys.precision)
  #ifndef	REFCLOCK
  		+ NTP_MAXSKW;
***************
*** 607,613 ****
  	if (peer->precision < 0 && -peer->precision < sizeof(long)*NBBY)
  		delay += 1.0/(unsigned long)(1L << -peer->precision);
  
! 	if (delay < 0.0) {
  		peer->pkt_dropped++;
  		return;
  	}
--- 613,619 ----
  	if (peer->precision < 0 && -peer->precision < sizeof(long)*NBBY)
  		delay += 1.0/(unsigned long)(1L << -peer->precision);
  
! 	if (delay < 0.0 || delay > MaxDelay) {
  		peer->pkt_dropped++;
  		return;
  	}
***************
*** 726,731 ****
--- 732,739 ----
  			syslog(LOG_INFO, buf);
  #ifdef	DEBUG
  			if (debug)
+ 				puts(buf);
+ 			if (debug)
  				printf("Clockset from %s stratum %d offset %f\n",
  				       inet_ntoa(peer->src.sin_addr),
  				       peer->stratum, peer->estoffset);
***************
*** 743,754 ****
  		} else {
  			if (logstats) {
  				sprintf(buf,
! 			        "adjust: SLEW %s st %d off %f drft %f cmpl %f",
  					inet_ntoa(peer->src.sin_addr),
  					peer->stratum,
  					peer->estoffset, drift_comp,
  					compliance);
  				syslog(LOG_INFO, buf);
  			}
  		}
  	}
--- 751,766 ----
  		} else {
  			if (logstats) {
  				sprintf(buf,
! 				"adjust: SLEW %s st %d off %f drft %f cmpl %f",
  					inet_ntoa(peer->src.sin_addr),
  					peer->stratum,
  					peer->estoffset, drift_comp,
  					compliance);
  				syslog(LOG_INFO, buf);
+ #ifdef	DEBUG
+ 				if (debug)
+ 					puts(buf);
+ #endif
  			}
  		}
  	}
Index: ntp_sock.c
*** ntp_sock.c	Fri May 19 09:28:26 1989
--- ntp_sock.c	Wed Dec 13 10:11:19 1995
***************
*** 46,52 ****
   */
  
  #include <sys/types.h>
- #include <sys/param.h>
  #include <sys/ioctl.h>
  #include <sys/file.h>
  #include <sys/socket.h>
--- 46,51 ----
***************
*** 57,62 ****
--- 56,65 ----
  #include <stdio.h>
  #include "ntp.h"
  
+ #ifndef FNDELAY
+ #define FNDELAY O_NONBLOCK
+ #endif
+ 
  #define	MAX_INTF	10
  struct intf addrs[MAX_INTF];
  int nintf;
***************
*** 124,130 ****
  		/*NOTREACHED*/
  	}
  
! 	if (fcntl(addrs[i].fd, F_SETFL, FNDELAY) < 0) {
  		syslog(LOG_ERR, "fcntl(FNDELAY) fails: %m");
  #ifdef	TEST
  		perror("fcntl(FNDELAY) fails");
--- 127,133 ----
  		/*NOTREACHED*/
  	}
  
! 	if (fcntl(addrs[0].fd, F_SETFL, FNDELAY) < 0) {
  		syslog(LOG_ERR, "fcntl(FNDELAY) fails: %m");
  #ifdef	TEST
  		perror("fcntl(FNDELAY) fails");
Index: ntpd.c
*** ntpd.c	Fri May 19 09:31:08 1989
--- ntpd.c	Wed Dec 13 13:24:29 1995
***************
*** 1,6 ****
  #ifndef	lint
  static char *rcsid = "$Source: /usr/users/louie/ntp/RCS/ntpd.c,v $ $Revision: 3.4.1.9 $ $Date: 89/05/18 18:30:17 $";
! #endif	lint
  
  /*
   *  $Log:	ntpd.c,v $
--- 1,6 ----
  #ifndef	lint
  static char *rcsid = "$Source: /usr/users/louie/ntp/RCS/ntpd.c,v $ $Revision: 3.4.1.9 $ $Date: 89/05/18 18:30:17 $";
! #endif
  
  /*
   *  $Log:	ntpd.c,v $
***************
*** 82,94 ****
   */
  
  #include <stdio.h>
  #include <sys/types.h>
! #include <sys/param.h>
! #include <sys/uio.h>
  #include <sys/socket.h>
  #include <sys/time.h>
  #include <sys/ioctl.h>
- #include <sys/resource.h>
  #include <sys/file.h>
  #ifdef NOSWAP
  #include <sys/lock.h>
--- 82,93 ----
   */
  
  #include <stdio.h>
+ #include <stdlib.h>
  #include <sys/types.h>
! #include <sys/sched.h>
  #include <sys/socket.h>
  #include <sys/time.h>
  #include <sys/ioctl.h>
  #include <sys/file.h>
  #ifdef NOSWAP
  #include <sys/lock.h>
***************
*** 101,116 ****
  #include <netinet/ip.h>
  #include <netinet/udp.h>
  
  #include <arpa/inet.h>
  #include <netdb.h>
! #include <strings.h>
  #include <errno.h>
  #include <syslog.h>
! #include <nlist.h>
  
  #include "ntp.h"
  #include "patchlevel.h"
  
  #define	TRUE	1
  #define	FALSE	0
  
--- 100,120 ----
  #include <netinet/ip.h>
  #include <netinet/udp.h>
  
+ #include <signal.h>
+ #include <sys/select.h>
+ 
  #include <arpa/inet.h>
  #include <netdb.h>
! #include <string.h>
  #include <errno.h>
  #include <syslog.h>
! #include <unistd.h>
  
  #include "ntp.h"
  #include "patchlevel.h"
  
+ #define L_SET SEEK_SET
+ 
  #define	TRUE	1
  #define	FALSE	0
  
***************
*** 126,131 ****
--- 130,137 ----
  
  char *conf = NTPINITFILE;
  char *driftcomp_file = NTPDRIFTCOMP;
+ char *log_file = NULL;
+ 
  static int drift_fd = -1;
  
  #ifdef	DEBUG
***************
*** 150,155 ****
--- 156,163 ----
  double WayTooBig = WAYTOOBIG;
  unsigned long clock_watchdog;
  
+ double MaxDelay = 86400.0;	/* Drop packets with more delay than this */
+ 
  struct ntpdata ntpframe;
  struct sysdata sys;
  
***************
*** 157,169 ****
  extern char *malloc(), *ntoa();
  extern double s_fixed_to_double(), ul_fixed_to_double();
  
- void finish(), timeout(), tock(), make_new_peer(), init_ntp(), initialize(),
- 	init_kern_vars(), hourly();
  extern void transmit(), process_packet(), clock_update(),
  	clear(), clock_filter(), select_clock();
  
  extern void init_logical_clock();
  
  main(argc, argv)
  	int argc;
  	char *argv[];
--- 165,196 ----
  extern char *malloc(), *ntoa();
  extern double s_fixed_to_double(), ul_fixed_to_double();
  
  extern void transmit(), process_packet(), clock_update(),
  	clear(), clock_filter(), select_clock();
  
  extern void init_logical_clock();
  
+ /* Prototypes for internal functions */
+ 
+ void timeout();
+ void tock();
+ void hourly();
+ void incdebug();
+ void decdebug();
+ void init_kern_vars();
+ void finish(int sig);
+ void init_ntp(char *config);
+ init_clock(char *name, char *type);
+ void make_new_peer(struct ntp_peer *peer);
+ GetHostName(char *name, struct sockaddr_in *sin);
+ void demobilize(struct list *l, struct ntp_peer *peer);
+ enqueue(register struct list *l, struct ntp_peer *peer);
+ struct ntp_peer *check_peer(struct sockaddr_in *dst, int sock);
+ query_mode(struct sockaddr_in *dst, struct ntpdata *ntp, int sock);
+ read_clock(int cfd, struct timeval **tvpp, struct timeval **otvpp);
+ dump_pkt(struct sockaddr_in *dst, struct ntpdata *pkt, struct ntp_peer *peer);
+ 
+ 
  main(argc, argv)
  	int argc;
  	char *argv[];
***************
*** 176,181 ****
--- 203,209 ----
  	register int i;
  	extern char *optarg;
  	extern int atoi();
+ 	struct sched_param sched_param;
  
  #if	defined(DEBUG) && defined(SIGUSR1) && defined(SIGUSR2)
  	void incdebug(), decdebug();
***************
*** 184,190 ****
  				   then allow others to override default
  				   values */
  	prog_name = argv[0];
! 	while ((cc = getopt(argc, argv, "a:c:dD:lstn")) != EOF) {
  		switch (cc) {
  		case 'a':
  			if (strcmp(optarg, "any") == 0)
--- 212,218 ----
  				   then allow others to override default
  				   values */
  	prog_name = argv[0];
! 	while ((cc = getopt(argc, argv, "a:c:dD:lL:m:stn")) != EOF) {
  		switch (cc) {
  		case 'a':
  			if (strcmp(optarg, "any") == 0)
***************
*** 211,216 ****
--- 239,251 ----
  #endif
  			break;
  
+ 		case 'm':
+ 			if (strcmp(optarg, "any") == 0)
+ 				MaxDelay = 10e15;
+ 			else
+ 				MaxDelay = atof(optarg);
+ 			break;
+ 
  		case 's':
  			doset = 0;
  			break;
***************
*** 237,242 ****
--- 272,281 ----
  			logstats = 1;
  			break;
  
+ 		case 'L':
+ 			log_file = optarg;
+ 			break;
+ 
  		case 'c':
  			conf = optarg;
  			break;
***************
*** 252,284 ****
  #endif
  		if (fork())
  			exit(0);
! 
! 		{
! 			int s;
! 			for (s = getdtablesize(); s >= 0; s--)
! 				(void) close(s);
! 			(void) open("/", 0);
! 			(void) dup2(0, 1);
! 			(void) dup2(0, 2);
! 			(void) setpgrp(0, getpid());
! 			s = open("/dev/tty", 2);
! 			if (s >= 0) {
! 				(void) ioctl(s, (u_long) TIOCNOTTY, (char *) 0);
! 				(void) close(s);
! 			}
! 		}
! #ifdef	DEBUG
  	}
  #endif
  
! #ifndef	LOG_DAEMON
! 	openlog("ntpd", LOG_PID);
! #else
! 
! #ifndef	LOG_NTP
! #define	LOG_NTP	LOG_DAEMON
! #endif
! 	openlog("ntpd", LOG_PID | LOG_NDELAY, LOG_NTP);
  #ifdef	DEBUG
  	if (debug)
  		setlogmask(LOG_UPTO(LOG_DEBUG));
--- 291,310 ----
  #endif
  		if (fork())
  			exit(0);
! 		(void) chdir("/");
! 		(void) setpgid(getpid(), 0);
! 		for (i = sysconf(_SC_OPEN_MAX); i >= 0; i--)
! 			(void) close(i);
! #ifdef  DEBUG
! 		(void) open("/dev/null", 0);
! 		(void) dup2(0, 1);
! 		(void) dup2(0, 2);
  	}
  #endif
  
! 	/* Attempt to open the log file for writing. */
! 	if (log_file)
! 		openlog(log_file);
  #ifdef	DEBUG
  	if (debug)
  		setlogmask(LOG_UPTO(LOG_DEBUG));
***************
*** 285,291 ****
  	else
  #endif	/* DEBUG */
  		setlogmask(LOG_UPTO(LOG_INFO));
- #endif	/* LOG_DAEMON */
  
  	syslog(LOG_NOTICE, "%s version $Revision: 3.4.1.9 $", prog_name);
  	syslog(LOG_NOTICE, "patchlevel %d", PATCHLEVEL);
--- 311,316 ----
***************
*** 295,302 ****
  		printf("%s version $Revision: 3.4.1.9 $ patchlevel %d\n",
  		       prog_name, PATCHLEVEL);
  #endif
- 	(void) setpriority(PRIO_PROCESS, 0, -10);
  
  #ifdef	NOSWAP
  	if (noswap)
  		if (plock(PROCLOCK) != 0)  {
--- 320,330 ----
  		printf("%s version $Revision: 3.4.1.9 $ patchlevel %d\n",
  		       prog_name, PATCHLEVEL);
  #endif
  
+ 	memset(&sched_param, 0, sizeof(sched_param));
+ 	sched_param.sched_priority = PRIO_USER_DFLT + 10;
+ 	(void) sched_setscheduler((pid_t) 0, SCHED_OTHER, &sched_param);
+ 
  #ifdef	NOSWAP
  	if (noswap)
  		if (plock(PROCLOCK) != 0)  {
***************
*** 400,406 ****
  				      (struct sockaddr *) dst, &dstlen)) < 0) {
  
  				if (errno != EWOULDBLOCK) {
! 					syslog("recvfrom: %m");
  #ifdef	DEBUG
  					if(debug > 2)
  						perror("recvfrom");
--- 428,434 ----
  				      (struct sockaddr *) dst, &dstlen)) < 0) {
  
  				if (errno != EWOULDBLOCK) {
! 					syslog(LOG_ERR, "recvfrom: %m");
  #ifdef	DEBUG
  					if(debug > 2)
  						perror("recvfrom");
***************
*** 470,476 ****
  	       (int) htons(dst->sin_port));
  	printf("Leap %d, version %d, mode %d, poll %d, precision %d stratum %d",
  	       (pkt->status & LEAPMASK) >> 6, (pkt->status & VERSIONMASK) >> 3,
! 	       pkt->status & MODEMASK, pkt->ppoll, pkt->precision,
  	       pkt->stratum);
  	switch (pkt->stratum) {
  	case 0:
--- 498,504 ----
  	       (int) htons(dst->sin_port));
  	printf("Leap %d, version %d, mode %d, poll %d, precision %d stratum %d",
  	       (pkt->status & LEAPMASK) >> 6, (pkt->status & VERSIONMASK) >> 3,
! 	       pkt->status & MODEMASK, pkt->ppoll, (int)pkt->precision,
  	       pkt->stratum);
  	switch (pkt->stratum) {
  	case 0:
***************
*** 632,638 ****
   *  re-enable themselves.
   */
  void 
! tock() {
  	ticked = 1;
  }
  
--- 660,667 ----
   *  re-enable themselves.
   */
  void 
! tock()
! {
  	ticked = 1;
  }
  
***************
*** 660,666 ****
  	 */
  	while (peer != NULL) {
  #ifdef	DEBUG
! 		if (peer->next == NULL && peer != peer_list.tail) {
  			printf("Broken peer list\n");
  			syslog(LOG_ERR, "Broken peer list");
  			abort();
--- 689,695 ----
  	 */
  	while (peer != NULL) {
  #ifdef	DEBUG
! 		if (debug && peer->next == NULL && peer != peer_list.tail) {
  			printf("Broken peer list\n");
  			syslog(LOG_ERR, "Broken peer list");
  			abort();
***************
*** 730,737 ****
  	bzero((char *) &sin, sizeof(sin));
  	fp = fopen(config, "r");
  	if (fp == NULL) {
! 		fprintf(stderr,"Problem opening NTP initialization file %s\n",
  			config);
  		syslog(LOG_ERR,"Problem opening NTP initialization file %s",
  			config);
  		exit(1);
--- 759,769 ----
  	bzero((char *) &sin, sizeof(sin));
  	fp = fopen(config, "r");
  	if (fp == NULL) {
! #ifdef DEBUG
! 		if (debug)
! 		  fprintf(stderr,"Problem opening NTP initialization file %s\n",
  			config);
+ #endif
  		syslog(LOG_ERR,"Problem opening NTP initialization file %s",
  			config);
  		exit(1);
***************
*** 781,786 ****
--- 813,827 ----
  				else
  					WayTooBig = atof(name);
  			}
+ 		} else if (strcmp(name, "maxdelay") == 0) {
+ 			if (fscanf(fp, "%s", name) != 1)
+ 				error = TRUE;
+ 			else {
+ 				if (strcmp(name, "any") == 0)
+ 					MaxDelay = 10e15;
+ 				else
+ 					MaxDelay = atof(name);
+ 			}
  		} else if (strncmp(name, "debuglevel", 5) == 0) {
  			if (fscanf(fp, "%d", &debuglevel) != 1)
  				error = TRUE;
***************
*** 788,799 ****
  			else debug += debuglevel;
  #endif
  		} else if (strcmp(name, "stratum") == 0) {
! 			fprintf(stderr, "Obsolete command 'stratum'\n");
  			error = TRUE;
  		} else if (strcmp(name, "precision") == 0) {
  			if (fscanf(fp, "%d", &precision) != 1)
  				error = TRUE;
! 			else sys.precision = (char) precision;
  #ifdef	SETTICKADJ
  		} else if (strcmp(name, "tickadj") == 0) {
  			if (fscanf(fp, "%d", &i) != 1)
--- 829,843 ----
  			else debug += debuglevel;
  #endif
  		} else if (strcmp(name, "stratum") == 0) {
! #ifdef DEBUG
! 			if (debug)
! 				fprintf(stderr, "Obsolete command 'stratum'\n");
! #endif
  			error = TRUE;
  		} else if (strcmp(name, "precision") == 0) {
  			if (fscanf(fp, "%d", &precision) != 1)
  				error = TRUE;
! 			else sys.precision =  precision;
  #ifdef	SETTICKADJ
  		} else if (strcmp(name, "tickadj") == 0) {
  			if (fscanf(fp, "%d", &i) != 1)
***************
*** 864,870 ****
  			} else if (strcmp(name, "passive") == 0) {
  				mode = MODE_SYM_PAS;
  			} else {
! 				printf("can't happen\n");
  				abort();
  			}
  			if (fscanf(fp, "%s", name) != 1)
--- 908,917 ----
  			} else if (strcmp(name, "passive") == 0) {
  				mode = MODE_SYM_PAS;
  			} else {
! #ifdef DEBUG
! 				if (debug)
! 					printf("can't happen\n");
! #endif
  				abort();
  			}
  			if (fscanf(fp, "%s", name) != 1)
***************
*** 902,908 ****
  						printf("Could not init reference source %s (type %s)\n",
  							name, clk_type);
  					else
! #endif DEBUG
  						syslog(LOG_ERR, "Could not init reference source %s (type %s)",
  							name, clk_type);
  					error = TRUE;
--- 949,955 ----
  						printf("Could not init reference source %s (type %s)\n",
  							name, clk_type);
  					else
! #endif /* DEBUG */
  						syslog(LOG_ERR, "Could not init reference source %s (type %s)",
  							name, clk_type);
  					error = TRUE;
***************
*** 937,943 ****
  					       stratum, precision);
  				transmit(peer);	/* head start for REFCLOCK */
  			}
! #endif REFCLOCK
  			else if (GetHostName(name, &sin) == 0)
  				syslog(LOG_ERR, "%s: unknown host", name);
  			else {
--- 984,990 ----
  					       stratum, precision);
  				transmit(peer);	/* head start for REFCLOCK */
  			}
! #endif /* REFCLOCK */
  			else if (GetHostName(name, &sin) == 0)
  				syslog(LOG_ERR, "%s: unknown host", name);
  			else {
***************
*** 978,984 ****
  							peer->flags |= PEER_FL_SYNC;
  							break;
  						default:
! 							printf("can't happen\n");
  							abort();
  						}
  						peer->src = sin;
--- 1025,1034 ----
  							peer->flags |= PEER_FL_SYNC;
  							break;
  						default:
! #ifdef DEBUG
! 							if (debug) 
! 								printf("can't happen\n");
! #endif
  							abort();
  						}
  						peer->src = sin;
***************
*** 997,1007 ****
  				} else {
  					syslog(LOG_WARNING,
  				     "Duplicate peer %s in in config file",
! 					       inet_ntoa(sin));
  #ifdef	DEBUG
  					if(debug)
  						printf("Duplicate peer %s in in config file\n",
! 						       inet_ntoa(sin));
  #endif
  				}
  			}
--- 1047,1057 ----
  				} else {
  					syslog(LOG_WARNING,
  				     "Duplicate peer %s in in config file",
! 					       inet_ntoa(sin.sin_addr));
  #ifdef	DEBUG
  					if(debug)
  						printf("Duplicate peer %s in in config file\n",
! 						       inet_ntoa(sin.sin_addr));
  #endif
  				}
  			}
***************
*** 1017,1024 ****
  			c = fgetc(fp);
  		while (c != '\n' && c != EOF);	/* next line */
  	}			/* end while */
  	if (error) {
! 		fprintf(stderr, "init_ntp: %s: initialization error\n", config);
  		syslog(LOG_ERR, "init_ntp: %s: initialization error", config);
  
  		exit(1);
--- 1067,1080 ----
  			c = fgetc(fp);
  		while (c != '\n' && c != EOF);	/* next line */
  	}			/* end while */
+ 	fclose(fp);
+ 
  	if (error) {
! #ifdef DEBUG
! 		if (debug)
! 			fprintf(stderr, "init_ntp: %s: initialization error\n",
! 				config);
! #endif
  		syslog(LOG_ERR, "init_ntp: %s: initialization error", config);
  
  		exit(1);
***************
*** 1027,1187 ****
  	 *  Read saved drift compensation register value.
  	 */
  	if ((fp = fopen(driftcomp_file, "r")) != NULL) {
! 		if (fscanf(fp, "%lf", &j) == 1 && j > -1.0 && j < 1.0) {
  			drift_comp = j;
  			syslog(LOG_INFO,
  			       "Drift compensation value initialized to %f", j);
  		} else {
! 			fprintf(stderr,
! 				"init_ntp: bad drift compensation value\n");
  			syslog(LOG_ERR,
! 			       "init_ntp: bad drift compensation value\n");
  		}
  		fclose(fp);
  	}
  }
  
- int kern_tickadj, kern_hz, kern_tick;
- 
  void
! init_kern_vars() {
! 	int kmem;
! 	static char	*memory = "/dev/kmem";
! 	static struct nlist nl[] = {
! #ifndef	NeXT
! 		{"_tickadj"},
! 		{"_hz"},
! 		{"_tick"},
! 		{""},
! #else
! 		{{"_tickadj"}},
! 		{{"_hz"}},
! 		{{"_tick"}},
! 		{{""}},
! #endif
! 	};
! 	static int *kern_vars[] = {&kern_tickadj, &kern_hz, &kern_tick};
! 	int i;
! 	kmem = open(memory, O_RDONLY);
! 	if (kmem < 0) {
! 		syslog(LOG_ERR, "Can't open %s for reading: %m", memory);
! #ifdef	DEBUG
! 		if (debug)
! 			perror(memory);
! #endif
! 		return;
! 	}
  
! 	nlist("/vmunix", nl);
! 
! 	for (i = 0; i < (sizeof(kern_vars)/sizeof(kern_vars[0])); i++) {
! 		long where;
! 
! 		if ((where = nl[i].n_value) == 0) {
! 			syslog(LOG_ERR, "Unknown kernal var %s",
! #ifdef	NeXT
! 			       nl[i].n_un.n_name
! #else
! 			       nl[i].n_name
! #endif
! 			       );
! 			continue;
! 		}
! 		if (lseek(kmem, where, L_SET) == -1) {
! 			syslog(LOG_ERR, "lseek for %s fails: %m",
! #ifdef	NeXT
! 			       nl[i].n_un.n_name
! #else
! 			       nl[i].n_name
! #endif
! 			       );
! 			continue;
! 		}
! 		if (read(kmem, kern_vars[i], sizeof(int)) != sizeof(int)) {
! 			syslog(LOG_ERR, "read for %s fails: %m",
! #ifdef	NeXT
! 			       nl[i].n_un.n_name
! #else
! 			       nl[i].n_name
! #endif
! 			       );
! 
! 			*kern_vars[i] = 0;
! 		}
! 	}
! #ifdef	SETTICKADJ
! 	/*
! 	 *  If desired value of tickadj is not specified in the configuration
! 	 *  file, compute a "reasonable" value here, based on the assumption 
! 	 *  that we don't have to slew more than 2ms every 4 seconds.
! 	 *
! 	 *  TODO: the 500 needs to be parameterized.
! 	 */
! 	if (tickadj == 0 && kern_hz)
! 		tickadj = 500/kern_hz;
! 
! #ifdef	DEBUG
! 	if (debug) {
! 		printf("kernel vars: tickadj = %d, hz = %d, tick = %d\n",
! 		       kern_tickadj, kern_hz, kern_tick);
! 		printf("desired tickadj = %d, dotickadj = %d\n", tickadj,
! 		       dotickadj);
! 	}
! #endif
! 
! 	if (dotickadj && tickadj && (tickadj != kern_tickadj)) {
! 		close(kmem);
! 		if ((kmem = open(memory, O_RDWR)) >= 0) {
! 			if (lseek(kmem, (long)nl[0].n_value, L_SET) == -1) {
! 				syslog(LOG_ERR, "%s: lseek fails: %m", memory);
! 				close(kmem);
! 				tickadj = 0;
! 			}
! 			if (tickadj && write(kmem, &tickadj, sizeof(tickadj)) !=
! 			    sizeof(tickadj)) {
! 				syslog(LOG_ERR, "%s: tickadj set fails: %m", memory);
! #ifdef	DEBUG
! 				printf("tickadj set fails\n");
! #endif
! 				tickadj = 0;
! 			} 
! 			if (tickadj && tickadj != kern_tickadj)
! 				syslog(LOG_INFO,
! 				       "System tickadj SET to %d",
! 				       tickadj);
! #ifdef	DEBUG
! 			if (tickadj && debug)
! 				printf("System tickadj SET to %d\n",
! 				       tickadj);
! #endif
  		} else {
! 			syslog(LOG_ERR, "Can't open %s: %m", memory);
! 			printf("Can't open %s\n", memory);
  		}
  	}
- #endif	/* SETTICKADJ */
- 	close(kmem);
- 
- 	/*
- 	 *  If we have successfully discovered `hz' from the kernel, then we
- 	 *  can set sys.precision, if it has not already been specified.  If
- 	 *  no value of `hz' is available, then use default (-6)
- 	 */
- 	if (sys.precision == 0) {
- 		if (kern_hz <= 64)
- 			sys.precision = -6;
- 		else if (kern_hz <= 128)
- 			sys.precision = -7;
- 		else if (kern_hz <= 256)
- 			sys.precision = -8;
- 		else if (kern_hz <= 512)
- 			sys.precision = -9;
- 		else if (kern_hz <= 1024)
- 			sys.precision = -10;
- 		else sys.precision = -11;
- 		syslog(LOG_INFO, "sys.precision set to %d from sys clock of %d HZ",
- 		       sys.precision, kern_hz);
- 	}
  }
  
  
--- 1083,1125 ----
  	 *  Read saved drift compensation register value.
  	 */
  	if ((fp = fopen(driftcomp_file, "r")) != NULL) {
! 		if (fscanf(fp, "%lf", &j) == 1 && j > -4.0 && j < 4.0) {
  			drift_comp = j;
  			syslog(LOG_INFO,
  			       "Drift compensation value initialized to %f", j);
  		} else {
! #ifdef DEBUG
! 			if (debug)
! 				fprintf(stderr,
! 					"init_ntp: bad drift compensation value\n");
! #endif
  			syslog(LOG_ERR,
! 			       "init_ntp: bad drift compensation value");
  		}
  		fclose(fp);
  	}
  }
  
  void
! init_kern_vars()
! {
! 	long hz;
! 	struct timespec res;
  
! 	if (sys.precision == 0) {
! 		if (clock_getres(CLOCK_REALTIME, &res) < 0) {
! 			syslog(LOG_ERR, "clock_getres() failed: %m");
! 			sys.precision = -6;
  		} else {
! 			hz = 1.0e9 / res.tv_nsec;
! 			sys.precision = 1;
! 			while (hz > 0) {
! 				sys.precision -= 1;
! 				hz /= 2;
! 			}
  		}
+ 		syslog(LOG_INFO, "sys.precision set to %d", (int) sys.precision);
  	}
  }
  
  
***************
*** 1303,1310 ****
  	}
  	if (nip->count) {
  		nip->seq = seq;
! 		if ((sendto(addrs[sock].fd, (char *) packet, sizeof(packet), 0,
! 			    dst, sizeof(struct sockaddr_in))) < 0) {
  			syslog(LOG_ERR, "sendto: %s  %m", ntoa(dst->sin_addr));
  		}
  	}
--- 1241,1250 ----
  	}
  	if (nip->count) {
  		nip->seq = seq;
! 		if ((sendto(addrs[sock].fd, (char *) packet,
! 			    sizeof(packet), 0,
! 			    (struct sockaddr *) dst,
! 			    sizeof(struct sockaddr_in))) < 0) {
  			syslog(LOG_ERR, "sendto: %s  %m", ntoa(dst->sin_addr));
  		}
  	}
***************
*** 1312,1318 ****
  
  /* every hour, dump some useful information to the log */
  void
! hourly() {
  	char buf[200];
  	register int p = 0;
  	static double drifts[5] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
--- 1252,1259 ----
  
  /* every hour, dump some useful information to the log */
  void
! hourly()
! {
  	char buf[200];
  	register int p = 0;
  	static double drifts[5] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
***************
*** 1423,1429 ****
  int init_clock_local(), read_clock_local();
  #ifdef PSTI
  int init_clock_psti(), read_clock_psti();
! #endif PSTI
  
  init_clock(name, type)
  char *name, *type;
--- 1364,1370 ----
  int init_clock_local(), read_clock_local();
  #ifdef PSTI
  int init_clock_psti(), read_clock_psti();
! #endif /* PSTI */
  
  init_clock(name, type)
  char *name, *type;
***************
*** 1441,1447 ****
  		reader = read_clock_psti;
  		cfd = init_clock_psti(name);
  	}
! #endif PSTI
  	else {
  		if (debug) printf("Unknown reference type\n"); else
  		syslog(LOG_ERR, "Unknown reference clock type (%s)\n", type);
--- 1382,1388 ----
  		reader = read_clock_psti;
  		cfd = init_clock_psti(name);
  	}
! #endif /* PSTI */
  	else {
  		if (debug) printf("Unknown reference type\n"); else
  		syslog(LOG_ERR, "Unknown reference clock type (%s)\n", type);
Index: ntpdc.c
*** ntpdc.c	Fri May 19 09:31:41 1989
--- ntpdc.c	Tue Dec 12 17:36:01 1995
***************
*** 74,82 ****
   */
  
  #include <sys/types.h>
! #include <sys/param.h>
  #include <signal.h>
- #include <sys/uio.h>
  #include <sys/socket.h>
  #include <sys/time.h>
  #include <netinet/in.h>
--- 74,81 ----
   */
  
  #include <sys/types.h>
! #include <sys/select.h>
  #include <signal.h>
  #include <sys/socket.h>
  #include <sys/time.h>
  #include <netinet/in.h>
***************
*** 84,90 ****
  #include <errno.h>
  #include <stdio.h>
  #include <netdb.h>
! #include <strings.h>
  #include <arpa/inet.h>
  #include "ntp.h"
  
--- 83,89 ----
  #include <errno.h>
  #include <stdio.h>
  #include <netdb.h>
! #include <string.h>
  #include <arpa/inet.h>
  #include "ntp.h"
  
***************
*** 95,105 ****
  extern int errno;
  int debug;
  int s;
! int timedout, timeout();
  int nflag, vflag;
  
! struct sockaddr_in sin = {AF_INET};
  
  char packet[MAXPACKETSIZE];
  #ifndef	MAXHOSTNAMELEN
  #define	MAXHOSTNAMELEN	64
--- 94,112 ----
  extern int errno;
  int debug;
  int s;
! int timedout;
  int nflag, vflag;
  
! /* Function Prototypes */
! answer(char *host);
! int query(char *host);
! timeout();
! print_terse(struct clockinfo *n);
! print_verbose(struct clockinfo *n);
! char *cvthname(struct sockaddr_in *f);
  
+ struct sockaddr_in sin;
+ 
  char packet[MAXPACKETSIZE];
  #ifndef	MAXHOSTNAMELEN
  #define	MAXHOSTNAMELEN	64
***************
*** 113,122 ****
  	char *argv[];
  {
  	char *p;
! 	int on = 48*1024;
  
  	(void) gethostname(LocalHostName, sizeof LocalHostName);
! 	if (p = index(LocalHostName, '.')) {
  		*p++ = '\0';
  		LocalDomain = p;
  	}
--- 120,131 ----
  	char *argv[];
  {
  	char *p;
! 	int on = 24*1024;
  
+ 	sin.sin_family = AF_INET;
+ 
  	(void) gethostname(LocalHostName, sizeof LocalHostName);
! 	if (p = strchr(LocalHostName, '.')) {
  		*p++ = '\0';
  		LocalDomain = p;
  	}
***************
*** 157,165 ****
  			exit(2);
  		}
  #ifdef	SO_RCVBUF
! 		if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &on, sizeof (on)) < 0) {
! 			fprintf(stderr, "setsockopt SO_RCVBUF\n");
! 		}
  #endif
  		if (query(*argv))
  			answer(*argv);
--- 166,173 ----
  			exit(2);
  		}
  #ifdef	SO_RCVBUF
! 		if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &on, sizeof(on)) < 0)
! 			perror("setsockopt SO_RCVBUF");
  #endif
  		if (query(*argv))
  			answer(*argv);
***************
*** 298,304 ****
  	char *cvthname();
  	int flags;
  
- 	sin.sin_addr.s_addr = n->net_address;
  	for (i = 0; i < PEER_SHIFT; i++) {
  		delay[i] = (double) ((long) (ntohl(n->info_filter.delay[i])/1000.0));
  		offset[i] = (double) ((long) (ntohl(n->info_filter.offset[i])/1000.0));
--- 306,311 ----
***************
*** 392,399 ****
  		return ("???");
  	}
  	if (!nflag)
! 		hp = gethostbyaddr(&f->sin_addr, sizeof(struct in_addr),
! 				   f->sin_family);
  	else
  		return (inet_ntoa(f->sin_addr));
  
--- 399,406 ----
  		return ("???");
  	}
  	if (!nflag)
! 		hp = gethostbyaddr((char *) &f->sin_addr,
! 			sizeof(struct in_addr), f->sin_family);
  	else
  		return (inet_ntoa(f->sin_addr));
  
***************
*** 400,406 ****
  	if (hp == 0)
  		return (inet_ntoa(f->sin_addr));
  
! 	if ((p = index(hp->h_name, '.')) && strcmp(p + 1, LocalDomain) == 0)
  		*p = '\0';
  	return (hp->h_name);
  }
--- 407,413 ----
  	if (hp == 0)
  		return (inet_ntoa(f->sin_addr));
  
! 	if ((p = strchr(hp->h_name, '.')) && strcmp(p + 1, LocalDomain) == 0)
  		*p = '\0';
  	return (hp->h_name);
  }
Index: ntpsubs.c
*** ntpsubs.c	Fri May 19 09:33:52 1989
--- ntpsubs.c	Tue Dec 12 17:36:01 1995
***************
*** 1,6 ****
  #ifndef	lint
  static char *RCSid = "$Source: /usr/users/louie/ntp/RCS/ntpsubs.c,v $ $Revision: 3.4.1.3 $ $Date: 89/05/18 18:33:50 $";
! #endif	lint
  
  /*
   *  $Log:	ntpsubs.c,v $
--- 1,6 ----
  #ifndef	lint
  static char *RCSid = "$Source: /usr/users/louie/ntp/RCS/ntpsubs.c,v $ $Revision: 3.4.1.3 $ $Date: 89/05/18 18:33:50 $";
! #endif	/* lint */
  
  /*
   *  $Log:	ntpsubs.c,v $
***************
*** 44,57 ****
  
  #include <stdio.h>
  #include <sys/types.h>
- #include <sys/param.h>
  #include <sys/time.h>
- #include <sys/uio.h>
  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  #include <errno.h>
- #include <syslog.h>
  
  #include "ntp.h"
  
--- 44,54 ----
Index: qnx_syslog.c
*** /dev/null	Wed Dec 13 14:42:20 1995
--- qnx_syslog.c	Wed Dec 13 13:28:33 1995
***************
*** 0 ****
--- 1,87 ----
+ /* 
+  * Simplistic syslog() simulation for QNX ntpd.
+  *
+  * In the interests of authenticity, there are no internal-buffer
+  * overflow checks, just like the real syslog().
+  */
+ 
+ #include <errno.h>
+ #include <stdio.h>
+ #include <stdarg.h>
+ #include <string.h>
+ #include <syslog.h>
+ #include <fcntl.h>
+ 
+ static int log_fd = -1;
+ static int pri_mask = ~0;
+ 
+ 
+ void
+ openlog(file)
+ char *file;
+ {
+ 	int i;
+ 
+ 	if (file) {
+ 		i = open(file, O_WRONLY|O_CREAT|O_APPEND, 0644);
+ 		if (i >= 0)
+ 			log_fd = i;
+ 	}
+ }
+ 
+ 
+ int
+ setlogmask(maskpri)
+ int maskpri;
+ {
+ 	int old;
+ 
+ 	old = pri_mask;
+ 	pri_mask = maskpri;
+ 	return old;
+ }
+ 
+ 
+ void
+ syslog(priority, fmt, ...)
+ int priority;
+ char *fmt;
+ {
+ 	char *s, *msg, xfmt[1024], msgbuf[1024];
+ 	time_t now;
+ 	va_list arglist;
+ 
+ 	static char *ptable[] = {
+ 		"emerg:", "alert:", "crit: ", "error:",
+ 		"warn: ", "note: ", "info: ", "debug:"
+ 	};
+ 
+ 	/*
+ 	 * Substitute strerror() messages for any %m's in the format.
+ 	 * No buffer overflow checks, just like the real syslog()!
+ 	 */
+ 	s = xfmt;
+ 	while (*fmt) {
+ 		if (fmt[0] == '%' && fmt[1] == 'm') {
+ 			msg = strerror(errno);
+ 			while (*msg)
+ 				*s++ = *msg++;
+ 			fmt += 2;
+ 		} else {
+ 			*s++ = *fmt++;
+ 		}
+ 	}
+ 	*s = '\0';
+ 
+ 	if (log_fd >= 0 && (LOG_MASK(priority) & pri_mask)) {
+ 		now = time((long *) 0);
+ 		(void) strftime(msgbuf, sizeof(msgbuf), "%b %d %T  ",
+ 			localtime(&now));
+ 		va_start(arglist, fmt);
+ 		(void) vsprintf(msgbuf + strlen(msgbuf), xfmt, arglist);
+ 		va_end(arglist);
+ 		if (msgbuf[strlen(msgbuf)-1] != '\n')
+ 			strcat(msgbuf, "\n");
+ 		write(log_fd, msgbuf, strlen(msgbuf));
+ 	}
+ }
Index: qnx_time.c
*** /dev/null	Wed Dec 13 14:42:20 1995
--- qnx_time.c	Wed Dec 13 13:22:27 1995
***************
*** 0 ****
--- 1,92 ----
+ /* 
+  * Supporting routines not available, or broken, under QNX.
+  */
+ 
+ #include <stdio.h>
+ #include <sys/sched.h>
+ #include <sys/time.h>
+ #include <sys/timers.h>
+ 
+ 
+ int
+ gettimeofday(tp, tzp)
+ struct timeval *tp;
+ struct timezone *tzp;
+ {
+ 	int  status;
+ 	struct timespec dtm;
+ 
+ 	if ((status = clock_gettime(CLOCK_REALTIME, &dtm)) == 0) {
+ 	
+ 		tp->tv_sec = dtm.tv_sec;
+ 		tp->tv_usec = dtm.tv_nsec / 1000L;
+ 
+ 		if (tzp != NULL) {	
+ 			tzp->tz_minuteswest = 0;
+ 			tzp->tz_dsttime = 0;
+ 		}
+ 	}
+ 
+ 	return status;
+ }
+ 
+ 
+ int
+ settimeofday(tp, tzp)
+ struct timeval *tp;
+ struct timezone *tzp;
+ {
+ 	struct timespec dtm;
+ 
+ 	dtm.tv_sec = tp->tv_sec;
+ 	dtm.tv_nsec = tp->tv_usec * 1000L;
+ 
+ 	return clock_settime(CLOCK_REALTIME, &dtm);
+ }
+ 
+ 
+ int
+ adjtime(delta, olddelta)
+ struct timeval *delta;
+ struct timeval *olddelta;
+ {
+ 	int oldpolicy, status;
+ 	struct timespec dtm;
+ 	struct sched_param oldparam, newparam;
+ 
+ 	if (olddelta != NULL) {
+ 		olddelta->tv_sec = 0;
+ 		olddelta->tv_usec = 0;
+ 	}
+ 
+ 	/*
+ 	 * Since QNX lacks an adjtime() system call, we have to cheat
+ 	 * by using clock_gettime and clock_settime.  This causes a race
+ 	 * condition as the clock may tick between the get/set calls.
+ 	 * We try to work around this by elevating our priority and doing
+ 	 * a delay() so we run the critical code just after a clock
+ 	 * tick, hopefully finishing before the next tick.
+ 	 */
+ 
+ 	(void) sched_getparam((pid_t) 0, &oldparam);
+ 	newparam = oldparam;
+ 	newparam.sched_priority = PRIO_FIFO_MAX;
+ 	oldpolicy = sched_setscheduler((pid_t) 0, SCHED_FIFO, &newparam);
+ 	(void) delay((unsigned int) 1);
+ 
+ 	clock_gettime(CLOCK_REALTIME, &dtm);
+ 	dtm.tv_sec += delta->tv_sec;
+ 	dtm.tv_nsec += delta->tv_usec * 1000L;
+ 	if (dtm.tv_nsec >= 1000000000L) {
+ 		dtm.tv_sec++;
+ 		dtm.tv_nsec -= 1000000000L;
+ 	} else if (dtm.tv_nsec <= -1000000000L) {
+ 		dtm.tv_sec--;
+ 		dtm.tv_nsec += 1000000000L;
+ 	}
+ 	status = clock_settime(CLOCK_REALTIME, &dtm);
+ 
+ 	(void) sched_setscheduler((pid_t) 0, oldpolicy, &oldparam);
+ 
+ 	return status;
+ }
Index: read_psti.c
Prereq: 1.1
*** read_psti.c	Fri May 19 09:37:01 1989
--- read_psti.c	Wed Dec 13 14:47:07 1995
***************
*** 2,8 ****
  #ifndef	lint
  static char *rcsid = "$Header: /usr/users/louie/ntp/RCS/read_psti.c,v 3.4.1.1 89/05/18 18:36:45 louie Exp Locker: louie $";
  static char *sccsid = "@(#)read_psti.c	1.1	MS/ACF	89/02/17";
! #endif	lint
  
  #define	ERR_RATE	60	/* Repeat errors once an hour */
  
--- 2,8 ----
  #ifndef	lint
  static char *rcsid = "$Header: /usr/users/louie/ntp/RCS/read_psti.c,v 3.4.1.1 89/05/18 18:36:45 louie Exp Locker: louie $";
  static char *sccsid = "@(#)read_psti.c	1.1	MS/ACF	89/02/17";
! #endif	/* lint */
  
  #define	ERR_RATE	60	/* Repeat errors once an hour */
  
***************
*** 34,63 ****
   */
  #include <stdio.h>
  #include <syslog.h>
! #include <sys/time.h>
  #include <sys/types.h>
  #include <sys/ioctl.h>
- #if defined(sun)
  #include <termio.h>
! #endif
  
  #ifdef	DEBUG
  extern int debug;
! #endif	DEBUG
  
! static int nerrors = 0;
  static char clockdata[32];
  #define	MIN_READ 13		/* for Rev 4.01.001 */
  
  static double reltime();
  
  #ifdef STANDALONE
  
  #ifndef CLOCKDEV
! #define CLOCKDEV "/dev/radioclock"
  #endif
  
  #define DEBUG	1
  int debug = 1;
  
  main(argc, argv)
--- 34,73 ----
   */
  #include <stdio.h>
  #include <syslog.h>
! #include <errno.h>
! #include <time.h>
  #include <sys/types.h>
  #include <sys/ioctl.h>
  #include <termio.h>
! #include <sys/select.h>
  
  #ifdef	DEBUG
  extern int debug;
! #endif	/* DEBUG */
  
! static long nerrors = 0;
! 
  static char clockdata[32];
  #define	MIN_READ 13		/* for Rev 4.01.001 */
  
  static double reltime();
  
+ /*
+  * Define this if you want to ignore the year mismatch error which
+  * occurs when the clock's idea of the year is different than that
+  * set by the internal switches.
+  */
+ #define IGNORE_YEAR_MISMATCH 1
+ 
  #ifdef STANDALONE
  
  #ifndef CLOCKDEV
! #define CLOCKDEV "/dev/ser2"	/* May have a mouse on ser1 */
  #endif
  
+ #ifndef DEBUG
  #define DEBUG	1
+ #endif
  int debug = 1;
  
  main(argc, argv)
***************
*** 65,80 ****
  char **argv;
  {
  	struct timeval *tvp, *otvp;
  
  	debug = argc;
! 	if (openclock(CLOCKDEV))
  		do {
! 			(void)readclock(&tvp, &otvp);
  			sleep(1);
  		} while (debug>1);
  	exit(0);
  }
! #endif STANDALONE
  
  
  init_clock_psti(timesource)
--- 75,91 ----
  char **argv;
  {
  	struct timeval *tvp, *otvp;
+ 	int cfd;
  
  	debug = argc;
! 	if (cfd = init_clock_psti(CLOCKDEV))
  		do {
! 			(void)read_clock_psti(cfd, &tvp, &otvp);
  			sleep(1);
  		} while (debug>1);
  	exit(0);
  }
! #endif /* STANDALONE */
  
  
  init_clock_psti(timesource)
***************
*** 90,96 ****
  	if ((cfd = open(timesource, 2)) < 0) {
  #ifdef DEBUG
  		if (debug) perror(timesource); else
! #endif DEBUG
  		syslog(LOG_ERR, "can't open %s: %m", timesource);
  		return(-1);
  	}
--- 101,107 ----
  	if ((cfd = open(timesource, 2)) < 0) {
  #ifdef DEBUG
  		if (debug) perror(timesource); else
! #endif /* DEBUG */
  		syslog(LOG_ERR, "can't open %s: %m", timesource);
  		return(-1);
  	}
***************
*** 98,104 ****
  	if (ioctl(cfd, TIOCEXCL, 0) < 0) {
  #ifdef DEBUG
  		if (debug) perror("TIOCEXCL on radioclock failed"); else
! #endif DEBUG
  		syslog(LOG_ERR, "TIOCEXCL on %s failed: %m", timesource);
  		return(-1);
  	}
--- 109,115 ----
  	if (ioctl(cfd, TIOCEXCL, 0) < 0) {
  #ifdef DEBUG
  		if (debug) perror("TIOCEXCL on radioclock failed"); else
! #endif /* DEBUG */
  		syslog(LOG_ERR, "TIOCEXCL on %s failed: %m", timesource);
  		return(-1);
  	}
***************
*** 107,117 ****
  	if (ioctl(cfd, TCGETA, &tty) < 0) {
  #ifdef DEBUG
  		if (debug) perror("ioctl on radioclock failed"); else
! #endif DEBUG
  		syslog(LOG_ERR, "ioctl on %s failed: %m", timesource);
  		return(-1);
  	}
! 	tty.c_cflag = (B9600<<16)|B9600|CS8|CLOCAL|CREAD;
  	tty.c_iflag = ICRNL;
  	tty.c_oflag = 0;
  	tty.c_lflag = 0;
--- 118,128 ----
  	if (ioctl(cfd, TCGETA, &tty) < 0) {
  #ifdef DEBUG
  		if (debug) perror("ioctl on radioclock failed"); else
! #endif /* DEBUG */
  		syslog(LOG_ERR, "ioctl on %s failed: %m", timesource);
  		return(-1);
  	}
! 	tty.c_cflag = B9600|CS8|CLOCAL|CREAD;
  	tty.c_iflag = ICRNL;
  	tty.c_oflag = 0;
  	tty.c_lflag = 0;
***************
*** 119,134 ****
  	tty.c_cc[VMIN] = MIN_READ;
  	tty.c_cc[VTIME] = 0;
  	if (ioctl(cfd, TCSETA, &tty) < 0) {
! #else TCSETA	/* Use older Berkeley style IOCTL's */
  	bzero((char *)&tty, sizeof tty);
  	tty.sg_ispeed = tty.sg_ospeed = B9600;
  	tty.sg_flags = ANYP|CRMOD;
  	tty.sg_erase = tty.sg_kill = '\0';
  	if (ioctl(cfd, TIOCSETP, &tty) < 0) {
! #endif TCSETA
  #ifdef DEBUG
  		if (debug) perror("ioctl on radioclock failed"); else
! #endif DEBUG
  		syslog(LOG_ERR, "ioctl on %s failed: %m", timesource);
  		return(-1);
  	}
--- 130,145 ----
  	tty.c_cc[VMIN] = MIN_READ;
  	tty.c_cc[VTIME] = 0;
  	if (ioctl(cfd, TCSETA, &tty) < 0) {
! #else /* TCSETA */	/* Use older Berkeley style IOCTL's */
  	bzero((char *)&tty, sizeof tty);
  	tty.sg_ispeed = tty.sg_ospeed = B9600;
  	tty.sg_flags = ANYP|CRMOD;
  	tty.sg_erase = tty.sg_kill = '\0';
  	if (ioctl(cfd, TIOCSETP, &tty) < 0) {
! #endif /* TCSETA */
  #ifdef DEBUG
  		if (debug) perror("ioctl on radioclock failed"); else
! #endif /* DEBUG */
  		syslog(LOG_ERR, "ioctl on %s failed: %m", timesource);
  		return(-1);
  	}
***************
*** 135,141 ****
  	if (write(cfd, "xxxxxxsn\r", 9) != 9) {
  #ifdef DEBUG
  		if (debug) perror("init write to radioclock failed"); else
! #endif DEBUG
  		syslog(LOG_ERR, "init write to %s failed: %m", timesource);
  		return(-1);
  	}
--- 146,152 ----
  	if (write(cfd, "xxxxxxsn\r", 9) != 9) {
  #ifdef DEBUG
  		if (debug) perror("init write to radioclock failed"); else
! #endif /* DEBUG */
  		syslog(LOG_ERR, "init write to %s failed: %m", timesource);
  		return(-1);
  	}
***************
*** 155,161 ****
  	struct tm *mtm;
  	struct tm radio_tm, *rtm = &radio_tm;
  	register int i;
! 	register int millis;
  	register double diff;
  	int stat1, stat2;
  	fd_set readfds;
--- 166,172 ----
  	struct tm *mtm;
  	struct tm radio_tm, *rtm = &radio_tm;
  	register int i;
! 	register long millis;
  	register double diff;
  	int stat1, stat2;
  	fd_set readfds;
***************
*** 163,169 ****
  #ifndef TCSETA
  	register char *cp;
  	int  need;
! #endif TCSETA
  
  	FD_ZERO(&readfds);
  	FD_SET(cfd, &readfds);
--- 174,180 ----
  #ifndef TCSETA
  	register char *cp;
  	int  need;
! #endif /* TCSETA */
  
  	FD_ZERO(&readfds);
  	FD_SET(cfd, &readfds);
***************
*** 175,184 ****
  	/* BEGIN TIME CRITICAL CODE SECTION!!!!!! */
  	/* EVERY CYCLE FROM THIS POINT OUT ADDS TO THE INACCURACY OF
  	     THE READ CLOCK VALUE!!!!! */
  	if (write(cfd, "\003qu0000", 7) != 7) {
  #ifdef DEBUG
  		if (debug) printf("radioclock write failed\n"); else
! #endif DEBUG
  		if ((nerrors++%ERR_RATE) == 0)
  			syslog(LOG_ERR, "write to radioclock failed: %m");
  		return(1);
--- 186,196 ----
  	/* BEGIN TIME CRITICAL CODE SECTION!!!!!! */
  	/* EVERY CYCLE FROM THIS POINT OUT ADDS TO THE INACCURACY OF
  	     THE READ CLOCK VALUE!!!!! */
+ 	(void) tcflush(cfd, TCIOFLUSH);
  	if (write(cfd, "\003qu0000", 7) != 7) {
  #ifdef DEBUG
  		if (debug) printf("radioclock write failed\n"); else
! #endif /* DEBUG */
  		if ((nerrors++%ERR_RATE) == 0)
  			syslog(LOG_ERR, "write to radioclock failed: %m");
  		return(1);
***************
*** 186,192 ****
  	if(select(cfd+1, &readfds, 0, 0, &timeout) != 1) {
  #ifdef DEBUG
  		if (debug) printf("radioclock poll timed out\n"); else
! #endif DEBUG
  		if ((nerrors++%ERR_RATE) == 0)
  			syslog(LOG_ERR, "poll of radioclock failed: %m");
  		return(1);
--- 198,204 ----
  	if(select(cfd+1, &readfds, 0, 0, &timeout) != 1) {
  #ifdef DEBUG
  		if (debug) printf("radioclock poll timed out\n"); else
! #endif /* DEBUG */
  		if ((nerrors++%ERR_RATE) == 0)
  			syslog(LOG_ERR, "poll of radioclock failed: %m");
  		return(1);
***************
*** 194,200 ****
  	if ((i = read(cfd, clockdata, sizeof clockdata)) < MIN_READ) {
  #ifdef DEBUG
  		if (debug) printf("radioclock read error (%d)\n", i); else
! #endif DEBUG
  		if ((nerrors++%ERR_RATE) == 0)
  			syslog(LOG_ERR, "radioclock read error (%d!=13): %m", i);
  		return(1);
--- 206,212 ----
  	if ((i = read(cfd, clockdata, sizeof clockdata)) < MIN_READ) {
  #ifdef DEBUG
  		if (debug) printf("radioclock read error (%d)\n", i); else
! #endif /* DEBUG */
  		if ((nerrors++%ERR_RATE) == 0)
  			syslog(LOG_ERR, "radioclock read error (%d!=13): %m", i);
  		return(1);
***************
*** 207,213 ****
  #ifdef DEBUG
  		if (debug) printf("radioclock format error1 (%.12s)(0x%x)\n",
  			clockdata, clockdata[12]); else
! #endif DEBUG
  		if ((nerrors++%ERR_RATE) == 0)
  			syslog(LOG_ERR, "radioclock format error1 (%.12s)(0x%x)",
  				clockdata, clockdata[12]);
--- 219,225 ----
  #ifdef DEBUG
  		if (debug) printf("radioclock format error1 (%.12s)(0x%x)\n",
  			clockdata, clockdata[12]); else
! #endif /* DEBUG */
  		if ((nerrors++%ERR_RATE) == 0)
  			syslog(LOG_ERR, "radioclock format error1 (%.12s)(0x%x)",
  				clockdata, clockdata[12]);
***************
*** 217,223 ****
  		if (clockdata[i] < '0' || clockdata[i] > 'o') {
  #ifdef DEBUG
  			if (debug) printf("radioclock format error2\n"); else
! #endif DEBUG
  			if ((nerrors++%ERR_RATE) == 0)
  				syslog(LOG_ERR, "radioclock format error2\n");
  			return(1);
--- 229,235 ----
  		if (clockdata[i] < '0' || clockdata[i] > 'o') {
  #ifdef DEBUG
  			if (debug) printf("radioclock format error2\n"); else
! #endif /* DEBUG */
  			if ((nerrors++%ERR_RATE) == 0)
  				syslog(LOG_ERR, "radioclock format error2\n");
  			return(1);
***************
*** 246,252 ****
--- 258,269 ----
  		}
  	}
  
+ #ifdef IGNORE_YEAR_MISMATCH
+ 	/* Ignore Year mismatch error, just a nuisance */
+ 	if ((stat1 & ~0x2) != 0x4 && (nerrors++%ERR_RATE)==0) {
+ #else
  	if (stat1 != 0x4 && (nerrors++%ERR_RATE)==0) {
+ #endif
  #ifdef DEBUG
  		if (debug) printf("radioclock fault #%d 0x%x:%s%s%s%s%s%s\n",
  			nerrors, stat1,
***************
*** 257,263 ****
  			stat1&0x2?" Year Mismatch,":"",
  			stat1&0x1?" Clock Reset,":"");
  		else {
! #endif DEBUG
  			sprintf(message, "radioclock fault #%d 0x%x:%s%s%s%s%s%s\n",
  				nerrors, stat1,
  				stat1&0x20?" Out of Spec,":"",
--- 274,280 ----
  			stat1&0x2?" Year Mismatch,":"",
  			stat1&0x1?" Clock Reset,":"");
  		else {
! #endif /* DEBUG */
  			sprintf(message, "radioclock fault #%d 0x%x:%s%s%s%s%s%s\n",
  				nerrors, stat1,
  				stat1&0x20?" Out of Spec,":"",
***************
*** 278,284 ****
  			nerrors, rtm->tm_yday, rtm->tm_hour,
  			rtm->tm_min, rtm->tm_sec, millis);
  		else
! #endif DEBUG
  		sprintf(message, "radioclock bogon #%d: %dd %dh %dm %ds %dms\n",
  			nerrors, rtm->tm_yday, rtm->tm_hour,
  			rtm->tm_min, rtm->tm_sec, millis);
--- 295,301 ----
  			nerrors, rtm->tm_yday, rtm->tm_hour,
  			rtm->tm_min, rtm->tm_sec, millis);
  		else
! #endif /* DEBUG */
  		sprintf(message, "radioclock bogon #%d: %dd %dh %dm %ds %dms\n",
  			nerrors, rtm->tm_yday, rtm->tm_hour,
  			rtm->tm_min, rtm->tm_sec, millis);
***************
*** 293,311 ****
  		printf("Clock time:  19%d day %03d %02d:%02d:%02d.%03d diff %.3f\n",
  			rtm->tm_year, rtm->tm_yday, rtm->tm_hour,
  			rtm->tm_min, rtm->tm_sec, millis, diff);
! #endif DEBUG
  	
! 	if (diff > (90*24*60*60.0) && (nerrors++%ERR_RATE)==0) {
  #ifdef DEBUG
  		if (debug)
  			printf("offset excessive (system 19%d/%d, clock 19%d/%d)\n",
  				mtm->tm_year, mtm->tm_yday,
! 				rtm->tm_year, mtm->tm_yday);
  		else
! #endif DEBUG
  		syslog(LOG_ERR, "offset excessive (system 19%d/%d, clock 19%d/%d)\n",
  				mtm->tm_year, mtm->tm_yday,
! 				rtm->tm_year, mtm->tm_yday);
  		return(1);
  	}
  
--- 310,328 ----
  		printf("Clock time:  19%d day %03d %02d:%02d:%02d.%03d diff %.3f\n",
  			rtm->tm_year, rtm->tm_yday, rtm->tm_hour,
  			rtm->tm_min, rtm->tm_sec, millis, diff);
! #endif /* DEBUG */
  	
! 	if (diff > (90.0*24.0*60.0*60.0) && (nerrors++%ERR_RATE)==0) {
  #ifdef DEBUG
  		if (debug)
  			printf("offset excessive (system 19%d/%d, clock 19%d/%d)\n",
  				mtm->tm_year, mtm->tm_yday,
! 				rtm->tm_year, rtm->tm_yday);
  		else
! #endif /* DEBUG */
  		syslog(LOG_ERR, "offset excessive (system 19%d/%d, clock 19%d/%d)\n",
  				mtm->tm_year, mtm->tm_yday,
! 				rtm->tm_year, rtm->tm_yday);
  		return(1);
  	}
  
***************
*** 334,351 ****
  				stat2&0x1?" DST??? -1,":"");
  		printf("\n");
  	}
! #endif DEBUG
  	/* If necessary, acknowledge "Clock Reset" flag bit */
  	if (stat1 & 0x1) {
  		if (write(cfd, "si0", 3) != 3) {
  #ifdef DEBUG
  			if (debug) printf("radioclock reset write failed\n"); else
! #endif DEBUG
  			syslog(LOG_ERR, "reset write to radioclock failed: %m");
  			return(1);
  		}
  	}
  	if (nerrors && stat1==0x4) {
  		syslog(LOG_ERR, "radioclock OK (after %d errors)", nerrors);
  		nerrors = 0;
  	}
--- 351,373 ----
  				stat2&0x1?" DST??? -1,":"");
  		printf("\n");
  	}
! #endif /* DEBUG */
  	/* If necessary, acknowledge "Clock Reset" flag bit */
  	if (stat1 & 0x1) {
  		if (write(cfd, "si0", 3) != 3) {
  #ifdef DEBUG
  			if (debug) printf("radioclock reset write failed\n"); else
! #endif /* DEBUG */
  			syslog(LOG_ERR, "reset write to radioclock failed: %m");
  			return(1);
  		}
  	}
+ #ifdef IGNORE_YEAR_MISMATCH
+ 	/* Ignore Year mismatch, just a nuisance */
+ 	if (nerrors && (stat1 & ~0x2) ==0x4) {
+ #else
  	if (nerrors && stat1==0x4) {
+ #endif
  		syslog(LOG_ERR, "radioclock OK (after %d errors)", nerrors);
  		nerrors = 0;
  	}
***************
*** 357,369 ****
  static double
  reltime(tm, usec)
  register struct tm *tm;
! register int usec;
  {
! 	return(tm->tm_year*(366.0*24.0*60.0*60.0) +
! 	       tm->tm_yday*(24.0*60.0*60.0) +
! 	       tm->tm_hour*(60.0*60.0) +
! 	       tm->tm_min*(60.0) +
! 	       tm->tm_sec +
! 	       usec/1000000.0);
  }
  #endif
--- 379,391 ----
  static double
  reltime(tm, usec)
  register struct tm *tm;
! register long usec;
  {
! 	return((double)tm->tm_year*(366.0*24.0*60.0*60.0) +
! 	       (double)tm->tm_yday*(24.0*60.0*60.0) +
! 	       (double)tm->tm_hour*(60.0*60.0) +
! 	       (double)tm->tm_min*(60.0) +
! 	       (double)tm->tm_sec +
! 	       (double)usec/1000000.0);
  }
  #endif
Index: syslog.h
*** /dev/null	Wed Dec 13 14:42:20 1995
--- syslog.h	Tue Feb  1 14:23:59 1994
***************
*** 0 ****
--- 1,35 ----
+ /*
+  * Simplistic syslog simulation for QNX ntp.
+  */
+ 
+ #ifndef _SYSLOG_H_INCLUDED
+ 
+ #define _SYSLOG_H_INCLUDED
+ 
+ #define	LOG_EMERG	0	/* system is unusable */
+ #define	LOG_ALERT	1	/* action must be taken immediately */
+ #define	LOG_CRIT	2	/* critical conditions */
+ #define	LOG_ERR		3	/* error conditions */
+ #define	LOG_WARNING	4	/* warning conditions */
+ #define	LOG_NOTICE	5	/* normal but significant condition */
+ #define	LOG_INFO	6	/* informational */
+ #define	LOG_DEBUG	7	/* debug-level messages */
+ 
+ #define LOG_PRIMASK     0x07    /* mask to extract priority part (internal) */
+                                 /* extract priority */
+ #define LOG_PRI(p)      ((p) & LOG_PRIMASK)
+ 
+ /*
+  * arguments to setlogmask.
+  */
+ #define LOG_MASK(pri)   (1 << (pri))            /* mask for one priority */
+ #define LOG_UPTO(pri)   ((1 << ((pri)+1)) - 1)  /* all priorities through pri */
+ 
+ 
+ /* Prototypes */
+ 
+ void openlog(char *file);
+ int setlogmask(int maskpri);
+ void syslog(int priority, char *format, ...);
+ 
+ #endif /* _SYSLOG_H_INCLUDED */
Index: test.c
*** test.c	Fri May 19 09:37:41 1989
--- test.c	Tue Dec 12 17:36:04 1995
***************
*** 45,58 ****
   */
  
  #include <stdio.h>
- #include <sys/param.h>
  #include <sys/types.h>
  #include <sys/time.h>
  #include <sys/socket.h>
  #include <netinet/in.h>
- #include <netinet/in_systm.h>
- #include <netinet/ip.h>
- #include <netinet/udp.h>
  #include <netdb.h>
  #include <sys/ioctl.h>
  #include <signal.h>
--- 45,54 ----
***************
*** 72,79 ****
  	char **argv;
  {
  	if (argc > 1 && strcmp(argv[1], "-v") == 0) {
! 		exit(test1(1)
! 		     + test2(1)
  		     + test3(1)
  		     + test4(1));
  	} else {
--- 68,75 ----
  	char **argv;
  {
  	if (argc > 1 && strcmp(argv[1], "-v") == 0) {
! 		exit(test1()
! 		     + test2()
  		     + test3(1)
  		     + test4(1));
  	} else {
