Please use the following patch to rdate. It is a little improved,
including option '-b'.
diff -ur rdate-1.1.3-old/docs/rdate.8 rdate-1.1.3-new/docs/rdate.8
--- rdate-1.1.3-old/docs/rdate.8	2009-04-10 15:49:34.000000000 -0300
+++ rdate-1.1.3-new/docs/rdate.8	2009-04-10 18:07:00.000000000 -0300
@@ -39,7 +39,9 @@
 .Sh SYNOPSIS
 .Nm rdate
 .Op Fl 46acnpsuv
+.Op Fl b Ar sec
 .Op Fl o Ar port
+.Op Fl t Ar msec
 .Ar host
 .Sh DESCRIPTION
 .Nm
@@ -68,6 +70,11 @@
 .Xr adjtime 2
 call to gradually skew the local time to the
 remote time rather than just hopping.
+.It Fl b Ar sec
+Use adjtime if clock difference is at most
+.Ar sec
+seconds or hop if difference is greater. This is the same as including of
+removing the option -a, based on clock difference.
 .It Fl c
 Correct leap seconds.
 Sometimes required when synchronizing to an NTP server.
@@ -88,6 +95,10 @@
 Do not print the time.
 .It Fl u
 Use UDP instead of TCP as transport.
+.It Fl t Ar msec
+Does not set time if it took more than
+.Ar msec
+mili-seconds to fetch time from network.
 .It Fl v
 Verbose output.
 Always show the adjustment.
diff -ur rdate-1.1.3-old/src/rdate.c rdate-1.1.3-new/src/rdate.c
--- rdate-1.1.3-old/src/rdate.c	2009-04-10 15:49:34.000000000 -0300
+++ rdate-1.1.3-new/src/rdate.c	2009-04-10 18:10:48.000000000 -0300
@@ -38,6 +38,7 @@
  *	Time is returned as the number of seconds since
  *	midnight January 1st 1900.
  */
+/* options -t and -b implemented by Pedro Zorzenon Neto - 2009-04-10 */
 #ifndef lint
 #if 0
 from: static char rcsid[] = "$NetBSD: rdate.c,v 1.3 1996/02/22 06:59:18 thorpej Exp $";
@@ -77,16 +78,19 @@
 void
 usage(void)
 {
-	(void) fprintf(stderr, "Usage: %s [-46acnpsv] [-o port] host\n", __progname);
+	(void) fprintf(stderr, "Usage: %s [-46acnpsv] [ -b sec ] [-o port] [ -t msec ] host\n", __progname);
 	(void) fprintf(stderr, "  -4: use IPv4 only\n");
 	(void) fprintf(stderr, "  -6: use IPv6 only\n");
 	(void) fprintf(stderr, "  -a: use adjtime instead of instant change\n");
+	(void) fprintf(stderr, "  -b num: use instant change if difference is greater than\n"
+		               "          num seconds, or else use adjtime\n");
 	(void) fprintf(stderr, "  -c: correct leap second count\n");
 	(void) fprintf(stderr, "  -n: use SNTP instead of RFC868 time protocol\n");
 	(void) fprintf(stderr, "  -o num: override time port with num\n");
 	(void) fprintf(stderr, "  -p: just print, don't set\n");
 	(void) fprintf(stderr, "  -s: just set, don't print\n");
 	(void) fprintf(stderr, "  -u: use UDP instead of TCP as transport\n");
+	(void) fprintf(stderr, "  -t msec: does not set clock if network delay greater than msec\n");
 	(void) fprintf(stderr, "  -v: verbose output\n");
 }
 
@@ -102,8 +106,13 @@
 	int		port = 0;
 
 	struct timeval new, adjust;
+	int		maxdelay = 0;
+	int		netdelay = 0;
+	struct timeval  netdelay1, netdelay2;
+	struct timezone tz;
+	int             conditionalslide = 0;
 
-	while ((c = getopt(argc, argv, "46psancvuo:")) != -1)
+	while ((c = getopt(argc, argv, "46psancvuo:t:b:")) != -1)
 		switch (c) {
 		case '4':
 			family = PF_INET;
@@ -145,6 +154,14 @@
 			port = atoi(optarg);
 			break;
 
+		case 't':
+			maxdelay = atoi(optarg);
+			break;
+
+		case 'b':
+			conditionalslide = atoi(optarg);
+			break;
+
 		default:
 			usage();
 			return 1;
@@ -156,12 +173,36 @@
 	}
 	hname = argv[optind];
 
+	if (maxdelay) {
+	  gettimeofday(&netdelay1, &tz);
+	}
 	if (ntp)
 		ntp_client(hname, family, &new, &adjust, corrleaps, port, verbose);
 	else
 		rfc868time_client(hname, family, &new, &adjust, corrleaps, useudp, port);
 
+	if (maxdelay) {
+	  gettimeofday(&netdelay2, &tz);
+	  netdelay = (netdelay2.tv_sec - netdelay1.tv_sec) * 1000;
+	  netdelay += (netdelay2.tv_usec - netdelay1.tv_usec) / 1000;
+	  if (netdelay > maxdelay) {
+	    fprintf(stderr, "%s: Network delay exceeded (%i msec)\n",
+		    __progname, netdelay);
+	    exit(2);
+	  }
+	}
+
 	if (!pr) {
+
+	  if (conditionalslide) {
+	    if (((adjust.tv_sec > 0) && (adjust.tv_sec > conditionalslide)) ||
+		((adjust.tv_sec < 0) && ((-adjust.tv_sec) > conditionalslide))) {
+	      slidetime = 0;
+	    } else {
+	      slidetime = 1;
+	    }
+	  }
+	    
 		if (!slidetime) {
 			logwtmp("|", "date", "");
 			if (settimeofday(&new, NULL) == -1)
@@ -186,15 +227,32 @@
 		adjsec  = adjust.tv_sec + adjust.tv_usec / 1.0e6;
 
 		if (slidetime || verbose) {
+		  char slidemsg[32];
+		  if (conditionalslide) {
+		    if (slidetime) {
+		      strcpy(slidemsg," (adjtime)");
+		    } else {
+		      strcpy(slidemsg," (instant change)");
+		    }
+		  } else {
+		    strcpy(slidemsg,"");
+		  }
 			if (ntp)
 				(void) fprintf(stdout,
-				   "%s: adjust local clock by %.6f seconds\n",
-				   __progname, adjsec);
+				   "%s: adjust local clock by %.6f seconds%s\n",
+					       __progname, adjsec, slidemsg);
 			else
 				(void) fprintf(stdout,
-				   "%s: adjust local clock by %ld seconds\n",
-				   __progname, adjust.tv_sec);
+				   "%s: adjust local clock by %ld seconds%s\n",
+				   __progname, adjust.tv_sec, slidemsg);
 		}
+
+		if (verbose && maxdelay) {
+		  (void) fprintf(stdout,
+				 "%s: network delay %i msecs\n",
+				 __progname, netdelay);		  
+		}
+
 	}
 
 	return 0;

Reply via email to